Description
Said two elder brother when learning data structure when met that monkey count off the topic, in fact, this is the classic Joseph problem.
But then the second elder brother is a hair boy, will only use the method of simulation, and other students have used some to make two elder brother completely can not touch the mind of the method.
...... Two elder brother Rage changed the topic ...
When the Huaguoshan monkeys to choose the King, the election method is as follows:
All monkeys are seated in a circle by 1-m number, two elder brother standing in the center of the circle, by two elder brother designate an integer kn,
After the monkeys from number 1th in order to count off, report kn of the monkeys out of the circle, second brother again reported an integer kn+1,
Then by the monkey just quit the next monkey began to count, so cycle off, until only a monkey in the circle, the monkey is the king.
Because two elder brother hope through this kind of method control Huaguoshan, so now two elder brother put his set of integer sequence tell you, hope you help him in advance calculate that the monkey will become king.
Input Format
The first line, an integer m, means a total of M monkeys.
The second line to the M line, an integer for each line represents the M-1 integer that is about to be specified by the second brother. These numbers are greater than 0.
Output Format
An integer that indicates the number of the last monkey left.
Hint
For 40% of data, m<=1000, k<=1000
For 70% of data, m<=10000, k<=10000
For 100% of data, m<=10000, k<=100000000
Sample Input
51234
Sample Output
4
1. The link list simulation method is very simple also can AC optimization is to the K to take the remainder operation
But one thing to be aware of is that I used to use 1 2 to do code in the remainder may appear when the K is a multiple of CNT, so we have to deal with this alone can be replaced by code 0 1 2 ... to solve
Analog Code:
#include <iostream>using namespacestd;intfront[10000+Ten];intnex[10000+Ten];//Simulation Methodvoid out(intx) {Nex[front[x]]=Nex[x]; FRONT[NEX[X]]=front[x];}intMainintargcChar Const*argv[]) { intM; CIN>>M; for(inti =1; I <= M; ++i) {Front[i]= (i==1) ? m:i-1; Nex[i]= (i==m)?1: i+1; } intCur =1; intCNT =M; intK; while(1) {cin>>K; K%=(CNT); if(k==0) K+=CNT; for(inti =0; I < K1; ++i) {cur=Nex[cur]; } out(cur); Cur=Nex[cur]; CNT--;//go out a monkey if(CNT = =1) {cout<<cur<<Endl; Break; } } return 0;}
Chain List Simulation method
2. Mathematical strategy Inverse Method O (n)
We can find that each time a person is kicked out, it is a new Joseph ring problem, and there is a relationship between the two adjacent Joseph ring problems.
Suppose Y1---->y2 kicked out of the Y1 in the number of people in the S1 = K1-1 (because of the number of people starting from No. 0), in Y1 the person numbered s1+1 in Y2 number 0 and so on
If we had known that the man who was kicked out of Y2 was S2.
Then S2 and S1 can build relationships
S2 ID in Y1 = s2 + S1 + 1 = (s2 + K1)% num[1]
Which means that the person who was kicked out in the second inning is numbered in the first inning, the number in the second inning, the last K, and then the number of first innings.
We know the number of the last one out is 0 (only one person)
So you can reverse the introduction of this person in the first inning number is the answer
#include <iostream>using namespacestd;intk[10000]={0};intMainintargcChar Const*argv[]) { intM; CIN>>M; intAns =0;//The last man was the only person in the last Joseph Ring. for(inti =1; I <= m1; ++i) {cin>>K[i]; } //starting from the penultimate Joseph Ring, a total of M-Joseph rings for(inti =2; I <= M; ++i) {ans= (ans+k[m-i+1])% i;//at this time, I have a number of people in the last game k[m-i+1] only to the ANS//using 0 To identify the initial position can effectively avoid the problem of 0 after taking the remainder} cout<<ans+1<<Endl; return 0;}//2 4 3 5 9 4
inverse push State planning
Algorithm Learning Note 54. Joseph problem simulation, inverse push regulation SJTU OJ 10,382 Brother Joseph