1. 程式人生 > >HDU 1443解題報告

HDU 1443解題報告

Joseph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1768    Accepted Submission(s): 1095


Problem Description The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, . . ., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.

Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy. 

Input The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14. 

Output The output file will consist of separate lines containing m corresponding to k in the input file. 

Sample Input 3 4 0
Sample Output 5 30
Source
Recommend lcy

       這道題屬於一道比較經典的模擬題。但是如果方法選用不當,模擬的複雜度太高的話會導致TLE。。因此我們需要格外注意,降低模擬的複雜度。利用好取模的性質進行模擬。假設j是要數到的編號,那麼假設數到j這個數的人位置為t,這個人被殺以後,我們需要進行的操作是將這個人後邊一直到末尾的編號都減1,相當於補充被殺的這個人的位置缺口。這時我們計算下一個被殺的人的位置。就是(t+j-1)%num(num表示總人數,這時的num是減去1之後的,因為殺掉一個人num就要減1)。這個式子得出的位置結果就是下一個要被殺的人的位置。這個式子很巧妙。t是被殺的人的位置,而我們需要做的是將被殺的那個人之後的所有人的編號都減小1,那麼原先的(t+1)位置的人現在編號為t,(t+2)位置的人現在為(t+1),所以把當前的t位置當為起始位置,以後的標號就為0,1,2,3……j-1,總共j個人。所以用(t+j-1)%num就是下一個被殺人的位置。因而要掌握好這個式子表示的含義,也就能很方便的理解約瑟夫環的過程。這個式子比其他的模擬過程要簡潔的多,也可以較快的求出結果。

      參考程式碼:

#include<cstdio>
int a[15];
int main()
{
    int n;
    for(int i=1;i<=14;i++)
    {
        for(int j=i+1;;j++)
        {
            int num=2*i,t=j%num;  //num表示總人數,t表示被殺人的位置
            if(t==0) t=num;
            int cnt=0;
            while(t>i)  //因為要保證i+1~2*i的人先被殺,所以被殺人的編號t要始終大於i,如果不大於i,則退出迴圈,那麼被殺的人的個數也就達不到i
            {
                num--;
                t=(t+j-1)%num;
                if(t==0) t=num;
                cnt++;             //cnt表示被殺的人的個數
            }
            if(cnt==i)
            {
                a[i]=j;
                break;
            }
        }
    }
    while(~scanf("%d",&n)&&n)
    {
        printf("%d\n",a[n]);
    }
}

相關推薦

HDU 1443解題報告

Joseph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1768    Accepted Submiss

Hdu-1098解題報告

什麽 php break AC style %d fff font 正整數 Hdu-1098解題報告 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1098 題意:已知存在一個等式f(x)=5*x^13+13*x^5+k*a*

hdu 4734 解題報告

題目: F(x) Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9119 Accepted Submission(s): 360

hdu 2084 解題報告

數塔 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 20532    Accepted Submission

Hdu 2073解題報告

Problem Description 甜甜從小就喜歡畫圖畫,最近他買了一支智慧畫筆,由於剛剛接觸,所以甜甜只會用它來畫直線,於是他就在平面直角座標系中畫出如下的圖形: 甜甜的好朋友蜜蜜發現上面的圖

hdu 4001解題報告

-hdu- 4001 解題報告 題目大意:有n塊大小、高度不一的矩形磚塊,且型別為0,1,2三種,分別有不同疊放順序。求最終能夠得到的最大高度。 題目解析:dp動態規劃的型別,但先要進行對磚塊按長度、寬度和高度排序。接下去就是分三種情況進行dp,狀態dp[i]表示的是第

hdu 5375 - Gray code(dp) 解題報告

auto 當前 width ive data -i int code original Gray code Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth

HDU 2141(二分&三分 _B題)解題報告

opened span print -c name tac str 報告 nlog 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 -------------------------------------------

HDU 2044(遞推&遞歸_A題)解題報告

多少 size 思路 pri pan -c mat lis log 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2044 ---------------------------------------------------

HDU 1016(DFS_B題)解題報告

兩個 oid spl string tor -s b- 個數 code 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 -------------------------------------------------

HDU 1312(DFS_C題)解題報告

spa close .cn algorithm -s for pla sin bit 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 ------------------------------------------

解題報告hdu 1073 Online Judge

字符串 turn 保留 inpu 時也 空字符串 cep span one 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1073 題目是英文,在這裏用中文顯示: 伊格內修斯正在建立一個在線法官,現在他已經研究出除法官系統以外

HDU-1009的解題報告

str 鏈接 scan n) 思路 AI 準備 stdio.h javabean Hdu-1009 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1009 題意:Fatmouse準備M磅的貓食,準備與貓守衛倉庫有他最愛吃的食品

解題報告HDU 4089 Activation

解題報告 之 HDU 4089 Activation Description After 4 years' waiting, the game "Chinese Paladin 5" finally comes out.

解題報告HDU 4405 Aeroplane chess

解題報告 之 HDU 4405 Aeroplane chess Description Hzz loves aeroplane chess very much. The chess map contains N+1 gr

HDU 4383 To The Moon 解題報告

HDU 4383 To The Moon 題意翻譯 已知一個長為\(n\)的序列\(a\),你需要進行下面的四種操作。 C l r d 將區間\([l,r]\)中的數加上\(d\),同時時間加\(1\) Q l r 查詢當前時間區間\([l,r]\)中所有數的和 H l r t 查詢時間為\(t\)

HDU-6321___Dynamic Graph Matching——解題報告 狀壓DP

題目大意:   給出n個點和m個操作,每次操作可以可以在兩個點之間連線或者刪除這兩個點之間的連線,問匹配數為1、2...n/2的邊的數量,意思就是求出互不相交的j條邊的組數,j為1~n/2。 解題思路:   用圖的思想來理解點,首先最多有10個點,每個點都有被佔用和

解題報告】 Leapin' Lizards HDU 2732 網路流

【解題報告】 Leapin' Lizards HDU 2732 網路流 題外話   在正式講這個題目之前我想先說幾件事  1. 如果大家要做網路流的題目,我在網上看到一個傢伙,他那裡列出了一堆網路流的題目,而且還給他們分門別類,並且標註了難度,感覺挺好的,網址是[夏天的風](https://blog.cs

hdu 1253 勝利大逃亡 三維bfs 解題報告

勝利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 31687    Accepted Submis

hdu 4704 Sum(費馬小定理)解題報告

Problem Description Sample Input 2 Sample Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multip