2013
12-11

Hdu 1488 Flavius Josephus Reloaded[解题报告] C++

Flavius Josephus once was trapped in a cave together with his comrade soldiers surrounded by Romans. All of Josephus’ fellow soldiers preferred not to surrender but to commit suicide. So they all formed a circle and agreed on a number k. Every k-th person in the circle would then commit suicide. However, Josephus had different priorities and didn’t want to die just yet. According to the legend he managed to find the safe spot in the circle where he would be the last one to commit suicide. He surrendered to the Romans and became a citizen of Rome a few years later.

It is a lesser known fact that the souls of Josephus and his comrades were all born again in modern times. Obviously Josephus and his reborn fellow soldiers wanted to avoid a similar fiasco in the future. Thus they asked a consulting company to work out a better decision scheme. The company came up with the following scheme:

1.For the sake of tradition all soldiers should stand in a circle. This way a number between 0 and N-1 is assigned to each soldier, where N is the number of soldiers.
2.As changing numbers in the old scheme turned out to be horribly inefficient, the number assigned to a soldier will not change throughout the game.
3.The consulting company will provide two numbers a and b which will be used to calculate the number of the next soldier as follows: Let x be the number of the current soldier, then the number of the next soldier is the remainder of a*x^2 + b mod N.
4.We start with the soldier with number 0 and each soldier calculates the number of the next soldier according to the formula above.
5.As everyone deserves a second chance a soldier will commit suicide once his number is calculated for the second time.
6.In the event that the number of a soldier is calculated for the third time the game will end and all remaining soldiers will surrender.

You are to write a program that given the number of soldiers N and the constants a and b determines the number of survivors.

The input file consists of several test cases. Each test case consists of a single line containing the three integers N (2 ≤ N ≤ 109), a and b (0 ≤ a,b < N) separated by white space. You may safely assume that the first soldier dies after no more than one million (106) steps. The input is terminated by a single number 0 which should not be processed.

For each test case output a single line containing the number of soldiers that survive.

2 1 1
5 1 1
10 3 7
101 9 2
698253463 1 181945480
1000000000 999999999 999999999
0

0
2
4
96
698177783
999999994

#include<stdio.h>
#define inf 1000009
struct node
{
__int64 q;
int  num;
int  next;

}p[inf];
void init()
{
for(int  i=0;i<=inf;i++)
{

}
m=1;

}
int  find(__int64 q,int  x)
{
int i;
{
if(q==p[i].q)
{
p[i].num++;
return  p[i].num;
}
}

return 0;
}
{
p[m].q=q;
p[m].num=1;
}
int  main()
{
int n,a,b;
while(scanf("%d",&n),n)
{
scanf("%d%d",&a,&b);
init();
__int64  x=b%n;
int   ans = 0;
while(1)
{
int temp=find(x,x%inf);
if(temp==0)
else
if(temp==2)ans++;
else if(temp==3)break;

x=(((a*x)%n*x)%n+b)%n;
}
printf("%d\n",n-ans);

}
}

1. I like your publish. It is great to see you verbalize from the coronary heart and clarity on this essential subject matter can be easily noticed.

2. Thanks for taking the time to examine this, I really feel strongly about it and love studying a lot more on this topic. If possible, as you acquire experience

3. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks