POJ 题目1012Joseph(数学,约瑟夫环)
2014-09-17 22:58
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
Sample Output
Source
Central Europe 1995
a题目大意:NYOJ题目191
思路:
a(n) = [a(n-1)+m-1]mod(2k-n+1)
要求a(n) > k;n = 1,2,3,...,k
其中2k-n+1是第i-1次踢人后剩下的人数。
可以设计如下算法:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
bool Joseph(int k, int m) // 这个算法确定对于给定的k,m是否满足上面的要求
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int n;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(n=1;n<=k;n++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
a = (a+m-1)%(k2-n+1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(a == 0) a = k2-n+1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(a<=k && a>=1) return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
然后,我们注意到,第一次踢的人是 m%2k,我们要求 m%2k > k,也就是 m = 2k*r+h,h>k,那么就可以设计如下的算法找出最小的m:
for(r=0;;r++)
{
for(h=k+1;h<=2*k;h++)
{
m = 2*k*r+h;
if(Joseph(k,m)) goto end; // 找到m跳出
}
}
end:
ac代码
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 48185 | Accepted: 18184 |
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
Central Europe 1995
a题目大意:NYOJ题目191
思路:
a(n) = [a(n-1)+m-1]mod(2k-n+1)
要求a(n) > k;n = 1,2,3,...,k
其中2k-n+1是第i-1次踢人后剩下的人数。
可以设计如下算法:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
bool Joseph(int k, int m) // 这个算法确定对于给定的k,m是否满足上面的要求
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int n;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(n=1;n<=k;n++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
a = (a+m-1)%(k2-n+1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(a == 0) a = k2-n+1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(a<=k && a>=1) return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
然后,我们注意到,第一次踢的人是 m%2k,我们要求 m%2k > k,也就是 m = 2k*r+h,h>k,那么就可以设计如下的算法找出最小的m:
for(r=0;;r++)
{
for(h=k+1;h<=2*k;h++)
{
m = 2*k*r+h;
if(Joseph(k,m)) goto end; // 找到m跳出
}
}
end:
ac代码
#include<stdio.h> #include<string.h> int k,m; int a[220]; int find(int k,int m) { int n,a=1; for(n=1;n<=k;n++) { a=(a+m-1)%(2*k-n+1); if(!a) a=2*k-n+1; if(a<=k&&a>=1) return 0; } return 1; } int main() { int n; while(scanf("%d",&k)!=EOF,k) { int r,h,w=0; if(!a[k]) { for(r=0;;r++) { for(h=k+1;h<=2*k;h++) { m=2*k*r+h; if(find(k,m)) { w=1; break; } } if(w==1) break; } } else w=1; if(w) { if(!a[k]) a[k]=m; printf("%d\n",a[k]); } } }
相关文章推荐
- POJ 1012 Joseph(约瑟夫环)(枚举)
- poj1012Joseph(约瑟夫环)
- NYOJ 191 && POJ 1012 Joseph(约瑟夫环问题)
- NYOJ 191 && POJ 1012 Joseph(约瑟夫环问题)
- poj 1012 Joseph 约瑟夫环变形
- POJ 1012 Joseph 变形约瑟夫环
- poj 1012 & hdu 1443 Joseph(约瑟夫环变形)
- poj1012.Joseph(数学推论)
- poj 1012 Joseph(约瑟夫环求每次出圈人的序号)
- poj 1012 小白算法练习 Joseph 约瑟夫环 打表
- poj 1012 -- Joseph (约瑟夫环)
- UVALive5520 UVA305 POJ1012 HDU1443 Joseph【数学计算+打表+水题】
- poj 1012 & hdu 1443 Joseph(约瑟夫环变形)
- poj 1012 Joseph(约瑟夫环)
- 【约瑟夫环】poj 1012 Joseph
- Poj 1012 Joseph (约瑟夫环)
- POJ 1012 Joseph 推导,暴力,约瑟夫环,打表 难度:2
- 【Joseph问题】poj1012 Joseph
- poj 1012 Joseph
- POJ 1012 Joseph