Joseph - acm.uva.305(约瑟夫环)
2006-11-14 20:23
387 查看
假设有2k个人围着一个圆桌坐着,前k个是好人,后k个是坏人 。现在开始,每m个人踢掉一个,比如有6个人,m=5,那么,被踢掉的人依次是5,4,6,2,3,1。现在要求,在踢掉第一个好人前,必需把所有的坏人踢掉,问,给定一个k,求满足这个要求的最小的m。
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.
这个题目其实就是要求前k次踢掉的都是坏人,假设第i次踢掉的人是i,则i>k。根据题意,可以得到如下关系:
设 ai 是第i次踢掉的人在第i-1次踢掉后剩下的人中是第几个。那么
a(n) = [a(n-1)+m-1]mod(2k-n+1)
要求a(n) > k;n = 1,2,3,...,k
其中2k-n+1是第i-1次踢人后剩下的人数。
可以设计如下算法:
bool Joseph(int k, int m) // 这个算法确定对于给定的k,m是否满足上面的要求
...{
int n;
for(n=1;n<=k;n++)
...{
a = (a+m-1)%(k2-n+1);
if(a == 0) a = k2-n+1;
if(a<=k && a>=1) return false;
}
return true;
}
然后,我们注意到,第一次踢的人是 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:
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.
这个题目其实就是要求前k次踢掉的都是坏人,假设第i次踢掉的人是i,则i>k。根据题意,可以得到如下关系:
设 ai 是第i次踢掉的人在第i-1次踢掉后剩下的人中是第几个。那么
a(n) = [a(n-1)+m-1]mod(2k-n+1)
要求a(n) > k;n = 1,2,3,...,k
其中2k-n+1是第i-1次踢人后剩下的人数。
可以设计如下算法:
bool Joseph(int k, int m) // 这个算法确定对于给定的k,m是否满足上面的要求
...{
int n;
for(n=1;n<=k;n++)
...{
a = (a+m-1)%(k2-n+1);
if(a == 0) a = k2-n+1;
if(a<=k && a>=1) return false;
}
return true;
}
然后,我们注意到,第一次踢的人是 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:
相关文章推荐
- uva 305 Joseph(约瑟夫环解+打表)
- UVA 305 Joseph (约瑟夫环 打表)
- UVa 305 / POJ 1012 Joseph (如何得到约瑟夫环的下一个位置?)
- UVA 305 Joseph (约瑟夫环 打表)
- uva 305 Joseph
- UVA 305 Joseph
- Uva 305 - Joseph
- UVALive5520 UVA305 POJ1012 HDU1443 Joseph【数学计算+打表+水题】
- UVa 305 - Joseph
- UVA305 - Joseph(数论 + 打表)
- Uva 305 Joseph(数学+打表)
- UVA305 - Joseph(数论 + 打表)
- uva 305 Joseph
- uva 305 - Joseph(暴力)
- uva305 - Joseph 约瑟夫问题变形
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- uva 305 Joseph
- Uva305——Joseph
- UVA 305 - Joseph(模拟)
- UVA, 305 Joseph