您的位置:首页 > 其它

HDU 1443 Joseph

2014-12-21 00:01 288 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1443

Joseph

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1711 Accepted Submission(s): 1063



Problem 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


给你数n,代表有n个人,依次编号1,2,3,,,n,前面n/2个人是好人,后面n/2的人是坏人,然后,从1开始报数,报到m则杀死,再以下一个编号为第一个然后又数到m,又杀人,一直这样循环下去,直到把坏人全部杀光,你现在的任务算出最小的m,满足在杀第一个好人之前把所有的坏人都杀掉的条件。

m从n+1开始枚举即可。然后模拟这个过程,看是否满足条件。

#include<cstdio>
int dp[100];
int solve(int k)
{
    if(dp[k]) return dp[k];
    int n,p;
    for(int i=k+1;;i++)
    {
        for(n=k<<1,p=0;n>k;n--)
        {
            p=(p+i-1)%n;
            if(p<k) n=-1;
        }
        if(p==k)
        {
            dp[k]=i;
            return i;
        }
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        printf("%d\n",solve(n));
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: