Flavius Josephus Reloaded(hash)

题源:传送门

Flavius Josephus Reloaded

Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 285 Accepted Submission(s): 103

Problem Description
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.

Input
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.

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

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

Sample Output
0
2
4
96
698177783
999999994

哈希链地址法,就是将余数相同的放在一个链表里
第一次使用哈希

#include<iostream>
#include<cstring>
using namespace std;
const int MAXL=1000005;
struct soldier  //士兵
{
	int number;   //编号
	int time;     //被选中的次数
	soldier* next; 
 } *p[MAXL];    //哈希链数组,编号%MAXL相同的士兵放在同一个哈希连中
 soldier s[MAXL]; //士兵数组
__int64 n,a,b;
 int total;  //自杀士兵数量
 int index;  //士兵数组索引
 bool select(__int64 num,__int64 x)   //选择士兵,num表示该士兵应该所在的哈希链的编号,x表示该士兵的编号 
 {
 	soldier* q;
 	int flag=0;
 	for(q=p[num];q!=NULL;q=q->next)   //搜索该士兵所在的哈希表 
 	{
 		if(q->number==x)    //找到该士兵 
 		{
 			q->time++;
 			if(q->time==2) total++;     //被选中2次,该士兵自杀
			 else if(q->time==3) return true;   //被选中3次结束 
			 flag=1;
			 break ; 
		 }
	 }
	if(flag==0) //在哈希表中未找到该士兵的编号,即第一次选中该士兵
	{
		q=&s[index++];   //将该士兵存入士兵数组 
		q->number=x;
		q->time=1;
		q->next=p[num];   //将该士兵加入其应该所在的哈希链 
		p[num]=q;     //更新哈希链的头指针 
	 } 
	 return false;
  } 
  int main()
  {
  	while(cin>>n)
  	{
  		if(n==0) break;
  		cin>>a>>b;
  		memset(p,NULL,sizeof(p));
  		total=0;
  		index=0;
  		__int64 num=b%n;    //每次选中的士兵编号
		while(select(num%MAXL,num)==false)
		num=(((a*num)%n*num)+b)%n;
		cout<<n-total<<endl;   
  		
	  }
	  return 0;
  }

注意:__int64是两个短的下划线

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值