现在的位置: 首页 > 综合 > 正文

ACdream 1020 The Game about KILL 典型的约瑟夫环

2013年10月29日 ⁄ 综合 ⁄ 共 1359字 ⁄ 字号 评论关闭

传送门  http://www.acdream.net/problem.php?id=1020

看到了一道约瑟夫环的问题

要注意这个定理就可以了~~

f[1]=0;

f[p]=(f[p-1]+num)%p;

p 代表玩游戏的人数 num代表报的数

得出的结果f[n]+1 即为最后的报数剩下的人(即胜利者)

下面我就模拟一下 报数为2的过程

                                                 带<>号的代表玩游戏的人数,<>号上面的数字代表最后的胜利者

胜利者 1                
人数 1                
胜利者 1 3              
人数 2 3              
胜利者 1 3 5 7          
人数 4 5 6 7          
胜利者 1 3 5 7 9 11 13 15  
人数 8 9 10 11 12 13 14 15  

1020: The Game about KILL

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 255  Solved: 124
[Submit][Status][Web
Board
]

Description

Teacher HU and his 40 students were trapped by the brigands. To show their power, the head of the brigands want to select one people to kill.
Teacher HU and his 40 students will stand in a circle, and every second person would leave, and the last people in the circle would be killed. For example, if there are 5 persons in the circle, counting proceeds as 2, 4, 1, 5 and person 3 will be killed.
To make his students alive, teacher HU calculated the position to be the last man standing, and sacrifice himself.
Now we consider a more common condition, if teacher HU has N - 1 students, which place should he stand to be the last person.

Input

There are multiple test cases.
Each test case only contains an integer N. (1 <= N <= 1,000,000,000)

Output

For each test case, output an integer indicating which place should teacher HU stand.

Sample Input

2
3

Sample Output

1
3

HINT

Source

看到这么大的数据 直接想到的是找规律求解!~~结果就发现上文说的规律了。

post code:

#include<stdio.h>
int main()
{
    int i,num;
    while(scanf("%d",&num)!=EOF)
         {
             i=0;
             int n=num,sum=1;
             while(n)
             {
               n=n/2;
               sum=sum*2;        
             }             
             sum=sum/2-1;
             printf("%d\n",(num-sum)*2-1);
         }        
}

【上篇】
【下篇】

抱歉!评论已关闭.