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

TOJ 1112 POJ 2939 Flavius Josephus Reloaded/哈希链求地址法

2014年08月29日 ⁄ 综合 ⁄ 共 2838字 ⁄ 字号 评论关闭

Flavius Josephus Reloaded

时间限制(普通/Java):1000MS/10000MS     运行内存限制:65536KByte
总提交: 30            测试通过: 17

描述

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:

  • For the sake of tradition all soldiers should stand in a circle. This way a number between0 and
    N-1 is assigned to each soldier, where N is the number of soldiers.
    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.

    • 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·x2 + b mod N.

      • We start with the soldier with number0 and each soldier calculates the number of the next soldier according to the formula above.

        • As everyone deserves a second chance a soldier will commit suicide once his number is calculated for the second time.

          • 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 soldiersN 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 integersN (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 number0 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>
#include<string.h>
#include<stdlib.h>
#define inf 1000007
struct node
{
	__int64 num,count;
	node *next;
}hash[1000010];
__int64 count,n;
bool find(__int64 x)
{
	__int64 y=x%inf;
	node *p=&hash[y],*q;	
	if(p->num==-1)
	{
		p->num=x;
		p->count=1;
		p->next=NULL;
		return false;
	}
	while(p!=NULL&&p->num!=x)
	{
		q=p;
		p=p->next;
	}
	if(p==NULL)
	{
		p=(node*)malloc(sizeof(node));
		p->count=1;
		p->num=x;
		p->next=NULL;
		q->next=p;
		return false;
	}
    if(p->count==1)
	{
		p->count=2;
		count++;
		return false ;
	}
    if(p->count==2)
		return true;
}
main()
{
	__int64 a,b,x,m;
	while(scanf("%I64d",&n),n)
	{
		memset(hash,-1,sizeof(hash));
		scanf("%I64d %I64d",&a,&b);
		x=count=0;
		while(!find(x))
            x=((x*a)%n*x%n+b)%n;
		printf("%I64d\n",n-count);
	}
}

抱歉!评论已关闭.