PKU1012 约瑟夫问题
2010-03-17 22:29
399 查看
Joseph
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
经过这题才知道原来有打表这东西
……
Time Limit: 1000MS | | Memory Limit: 10000K |
Total Submissions: 27412 | | Accepted: 10260 |
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
经过这题才知道原来有打表这东西
……
//PKU1012 #include<iostream> using namespace std; int main() { int i,j,k,l,p,n,m,a[14]; for (k=1;k<14;++k) { m=1; p=1; l=0; while(p) { l++; if (l&1) m+=k;else m++; n=k<<1; i=1; while (1) { if (i+m-1>n) i=(i+m-1)%n;else i=i+m-1; if (i==0) i=n; if (i<=k) break; n--; } if (n==k) p=0; a[k]=m; } } while (scanf("%d",&k)&&k) printf("%d/n",a[k]); return 0; }
相关文章推荐
- 新随笔-约瑟夫问题(pku 1012)
- PKU1012 Joseph 约瑟夫问题
- 约瑟夫问题 pku1012(转自奋斗青春(亚伟)blog)
- POJ 1012 约瑟夫问题
- 约瑟夫问题及变形:poj 1012
- PKU ACM 1012 JOSEPH问题
- POJ 1012 && HDU 1443 Joseph(约瑟夫问题)
- pku1781,pku1012,pku2244(约瑟夫环问题)
- POJ-1012 Joseph-约瑟夫问题好人坏人
- poj 1012 Joseph (约瑟夫问题)
- 约瑟夫 (Joseph) -- ACM PKU 1012 解题报告
- 1012 Joseph 约瑟夫问题 总结了一个公式 n人报m第t轮出列的人的编号(从0到n-1)
- POJ-1012 约瑟夫问题
- POJ 1012 Joseph 约瑟夫问题
- poj 1012 约瑟夫问题
- poj 1012 Joseph 约瑟夫问题 (★★☆☆☆)
- poj 1012 Joseph (约瑟夫问题)
- POJ 1012(约瑟夫问题)
- poj 1012(约瑟夫问题。。。)
- 1012-Joseph 约瑟夫问题