约瑟夫环问题 poj 1012 && poj 2244
2015-07-13 13:58
405 查看
百度百科:http://baike.baidu.com/link?url=msOmvPMbbYAhxiaRjsBLi1WRRFL1WuDXZXdlrQE-vOvnslpORlOP_4_hgNWgH5DU
约瑟夫问题:
问题描述:n个人(编号0~(n-1)),从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数。求胜利者的编号。
我们知道第一个人(编号一定是(m-1) mod n) 出列之后,剩下的n-1个人组成了一个新的约瑟夫环(以编号为k=m mod n的人开始):
k k+1 k+2 … n-2,n-1,0,1,2,… k-2
并且从k开始报0。
我们把他们的编号做一下转换:
k –> 0
k+1 –> 1
k+2 –> 2
…
…
k-2 –> n-2
变换后就完完全全成为了(n-1)个人报数的子问题,假如我们知道这个子问题的解:例如x是最终的胜利者,那么根据上面这个表把这个x变回去不刚好就是n个人情况的解吗?!!变回去的公式很简单,相信大家都可以推出来:x’=(x+k) mod n
如何知道(n-1)个人报数的问题的解?对,只要知道(n-2)个人的解就行了。(n-2)个人的解呢?当然是先求(n-3)的情况 —- 这显然就是一个倒推问题!好了,思路出来了,下面写递推公式:
令f表示i个人玩游戏报m退出最后胜利者的编号,最后的结果自然是f
递推公式
f[1]=0;
f=(f+m) mod i; (i>1)
有了这个公式,我们要做的就是从1-n顺序算出f的数值,最后结果是f
。因为实际生活中编号总是从1开始,我们输出f
+1
由于是逐级递推,不需要保存每个f,程序也是异常简单:
Joseph
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 50099 Accepted: 19031
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
Central Europe 1995
题意:比较有意思 有k个好蛋 k个坏蛋 要坏蛋都比好蛋先死完的情况下循环数是多少(m)
思路:数学模拟Joseph 如果好蛋破了 重来 m++;╮(╯▽╰)╭ 当坏蛋都是笨蛋吗?
Eeny Meeny Moo
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3295 Accepted: 2283
Description
Surely you have made the experience that when too many people use the Internet simultaneously, the net becomes very, very slow.
To put an end to this problem, the University of Ulm has developed a contingency scheme for times of peak load to cut off net access for some cities of the country in a systematic, totally fair manner. Germany’s cities were enumerated randomly from 1 to n. Freiburg was number 1, Ulm was number 2, Karlsruhe was number 3, and so on in a purely random order.
Then a number m would be picked at random, and Internet access would first be cut off in city 1 (clearly the fairest starting point) and then in every mth city after that, wrapping around to 1 after n, and ignoring cities already cut off. For example, if n=17 and m=5, net access would be cut off to the cities in the order [1,6,11,16,5,12,2,9,17,10,4,15,14,3,8,13,7]. The problem is that it is clearly fairest to cut off Ulm last (after all, this is where the best programmers come from), so for a given n, the random number m needs to be carefully chosen so that city 2 is the last city selected.
Your job is to write a program that will read in a number of cities n and then determine the smallest integer m that will ensure that Ulm can surf the net while the rest of the country is cut off.
Input
The input will contain one or more lines, each line containing one integer n with 3 <= n < 150, representing the number of cities in the country.
Input is terminated by a value of zero (0) for n.
Output
For each line of the input, print one line containing the integer m fulfilling the requirement specified above.
Sample Input
3
4
5
6
7
8
9
10
11
12
0
Sample Output
2
5
2
4
3
11
2
3
8
16
Source
Ulm Local 1996
题意:第一个人必须报数 所以就可以看成满足第一个(二号)为win的条件下 m为多少的问题
约瑟夫问题:
问题描述:n个人(编号0~(n-1)),从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数。求胜利者的编号。
我们知道第一个人(编号一定是(m-1) mod n) 出列之后,剩下的n-1个人组成了一个新的约瑟夫环(以编号为k=m mod n的人开始):
k k+1 k+2 … n-2,n-1,0,1,2,… k-2
并且从k开始报0。
我们把他们的编号做一下转换:
k –> 0
k+1 –> 1
k+2 –> 2
…
…
k-2 –> n-2
变换后就完完全全成为了(n-1)个人报数的子问题,假如我们知道这个子问题的解:例如x是最终的胜利者,那么根据上面这个表把这个x变回去不刚好就是n个人情况的解吗?!!变回去的公式很简单,相信大家都可以推出来:x’=(x+k) mod n
如何知道(n-1)个人报数的问题的解?对,只要知道(n-2)个人的解就行了。(n-2)个人的解呢?当然是先求(n-3)的情况 —- 这显然就是一个倒推问题!好了,思路出来了,下面写递推公式:
令f表示i个人玩游戏报m退出最后胜利者的编号,最后的结果自然是f
递推公式
f[1]=0;
f=(f+m) mod i; (i>1)
有了这个公式,我们要做的就是从1-n顺序算出f的数值,最后结果是f
。因为实际生活中编号总是从1开始,我们输出f
+1
由于是逐级递推,不需要保存每个f,程序也是异常简单:
Joseph
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 50099 Accepted: 19031
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
Central Europe 1995
题意:比较有意思 有k个好蛋 k个坏蛋 要坏蛋都比好蛋先死完的情况下循环数是多少(m)
思路:数学模拟Joseph 如果好蛋破了 重来 m++;╮(╯▽╰)╭ 当坏蛋都是笨蛋吗?
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; int a[15]={0};//标记 int main () { int k; while(~scanf("%d",&k),k) { if(a[k]) { printf("%d\n",a[k]); continue; } int n = 2*k; int f[50]={0}; int m = 1; for(int i=1;i<=k;i++) { f[i] = (f[i-1] + m-1) % (n-i+1);//(n-i+1)为剩下的人数 if(f[i]<k) { m++; i=0; } } a[k] = m; printf("%d\n",m); } }
Eeny Meeny Moo
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3295 Accepted: 2283
Description
Surely you have made the experience that when too many people use the Internet simultaneously, the net becomes very, very slow.
To put an end to this problem, the University of Ulm has developed a contingency scheme for times of peak load to cut off net access for some cities of the country in a systematic, totally fair manner. Germany’s cities were enumerated randomly from 1 to n. Freiburg was number 1, Ulm was number 2, Karlsruhe was number 3, and so on in a purely random order.
Then a number m would be picked at random, and Internet access would first be cut off in city 1 (clearly the fairest starting point) and then in every mth city after that, wrapping around to 1 after n, and ignoring cities already cut off. For example, if n=17 and m=5, net access would be cut off to the cities in the order [1,6,11,16,5,12,2,9,17,10,4,15,14,3,8,13,7]. The problem is that it is clearly fairest to cut off Ulm last (after all, this is where the best programmers come from), so for a given n, the random number m needs to be carefully chosen so that city 2 is the last city selected.
Your job is to write a program that will read in a number of cities n and then determine the smallest integer m that will ensure that Ulm can surf the net while the rest of the country is cut off.
Input
The input will contain one or more lines, each line containing one integer n with 3 <= n < 150, representing the number of cities in the country.
Input is terminated by a value of zero (0) for n.
Output
For each line of the input, print one line containing the integer m fulfilling the requirement specified above.
Sample Input
3
4
5
6
7
8
9
10
11
12
0
Sample Output
2
5
2
4
3
11
2
3
8
16
Source
Ulm Local 1996
题意:第一个人必须报数 所以就可以看成满足第一个(二号)为win的条件下 m为多少的问题
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int Jos(int n,int m)//约瑟夫环 { int s = 0; for(int i=2;i<=n;i++) s = (s+m) % i; return s; } int main() { int n; while(~scanf("%d",&n),n) { int i = 1; while(Jos(n-1,i) != 0)//因为第一个必须kill 所以看做n-1的环 i++; printf("%d\n",i); } }
相关文章推荐
- OpenGL一维纹理映射练习
- 思考本质
- cocos2d-x中使用tinyxml遇到的问题及解决
- VMProtect修复导入表的插件
- Oracle中的Procedure書寫過程中的注意事項
- 关于yum安装时报Error: xz compression not available
- centos安装apache
- Android 颜色搭配工具
- linux多线程大神博客网址
- PAT 1046 Shortest Distance
- 【动态规划】[POJ 1229]Wild Domains
- Cookie文件内容的含义
- 安卓常用开源项目总结
- 生产者消费者
- 内核中修改和保存defconfig的方法
- DLL与LIB库文件的生成和使用---C++
- 程序员的出路
- 判断字符串是否为空的宏定义
- 移动端开发——javascript
- 转-浅谈HTTP中Get与Post的区别