poj 2939 Flavius Josephus Reloaded

http://poj.org/problem?id=2939

题意:但某个人点到两次时这个人自杀,当某个人点到三次时,循环结束,求结束时剩余的人数;

思路:用哈希写,经典的题啊;(代码是参考其他队友的)

代码:

View Code
#include <iostream>
#include <cstdio>
#include <algorithm>
#define inf 1000009
using namespace std;
struct node
{
long long data;
int num;
int next;
}node[inf];
int link[inf];
long long x = 0;
int a = 0;
int b = 0;
int m = 0;
int n = 0;
void init()
{
for(int i = 0;i < inf; ++i)
link[i] = 0;
m = 1;
}
int find(long long q,int x)
{
for(int i = link[x];i ; i = node[i].next)
{
if(node[i].data == q)
{
++node[i].num;
//printf("%d jj\n",node[i].num);
//getchar();
return node[i].num;
}
}
return 0;
}
void add(long long q,int x)
{
node[m].data = q;
node[m].num = 1;
node[m].next=link[x];
link[x] = m++;
}
int main()
{
while(scanf("%d",&n),n != 0)
{
init();
scanf("%d%d",&a,&b);
x = b%n;
int s = 0;
while(1)
{
int temp = find(x,x%inf);
//printf("%d gg\n",temp);
if( temp == 0)
add(x,x%inf);
else
if(temp == 2)
++s;
else
if(temp == 3)
{
printf("%d\n",n - s);
break;
}
x = (((a*x)%n*x)+b)%n;
}
}
return 0;
}



posted @ 2012-03-25 10:58  LT-blogs  阅读(256)  评论(0编辑  收藏  举报