Joseph
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 53862 |
|
Accepted: 20551 |
Description
The Joseph ' s problem is notoriously known. For those who is familiar with the original Problem:from among n people, numbered 1, 2, ..., n, standing in circle Every mth is going to being executed and only the last remaining person would be saved. Joseph is smart enough to choose the position of the last remaining person, thus saving he life to give us the message a Bout the incident. For example if n = 6 and M = 5 then the people would be executed in the order 5, 4, 6, 2, 3 and 1 would be saved.
Suppose that there is k good guys and k bad guys. In the circle the first k is good guys and the last k bad guys. You had to determine such minimal m so all the bad guys would 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 would consist of separate lines containing m corresponding to K in the input file.
Sample Input
340
Sample Output
530
Source
Central Europe 1995
Test instructions: The same Joseph problem, just now have k a good man, K bad guy, determine a step, yes before the first good man was cleared all the bad guys were cleared.
Exercises
1. The order in which the bad guys are cleared is irrelevant, knowing that the next one is good or bad. 2. Since the good guys have always been k, every bad guy gets rid of the bad guys-1, so the total number of queues every time-1 but good guys have been the top K 3. The next person purged in the queue is the "relative" ordinal of S = (s+m-1)% (n-i);
4. As long as s>=k, then the next to be removed is the villain
5. Next talk about MRange of values: Let's just look at the rest.K+1Personal situation, that is, the bad guy has not been executed, then in this round the end of the position must be the last villain, then where is the starting position? This will need to find the k+2 personal end position, howeverK+2The end position of the individual must be the firstK+2Individual or sectionK+1Individuals, there are two sequential situations:Gggg ..... GggxbOrgggg ... gggbx (Xk+2k+1k+1m There are two possibilities: or t (k+1) +1; t>=1; mm
The AC code is given below:
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>
1 #include <cstdio> 2 //#include <bits/stdc++.h> 3 using namespace std; 4 int a[14]; 5 int f (int k,int m) 6 { 7 int n,i,s; 8 n=2*k; 9 s=0;10 for (i=0;i<k;i++) 11 {12 s= (s+m-1)% (n-i);13 if (s<k) return 0;14 }15 return 1;16 }17 int main () 18 {19 int i,k,n;20 for (k=1;k<=14;k++) 21 {22 i=k+1;23 while (1) 24 {25 if (f (k,i)) 26 {27 a[k]=i;28 break;29 }30 else if (f (k,i+1)) 31 {32 a[k]=i+1;33 break;34 }35 i+=k+1;36 }37 }38 while (scanf ("% D ", &n) &&n) 39 printf ("%d\n ", A[n]);40 return 0;41 }
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>
POJ 1012 Joseph