poj 1597 搜索NO,数论YES

题意:对于产生伪随机数的公式:seed(x+1) = [ seed(x) + STEP ] % MOD。题目给定step和mod,问在0~mod-1中是否有一个种子值能产生0~mod-1之间的所有数。如果能,输出Good Choice,否则输出Bad Choice。

思路:自己写的暴搜,可以AC。后来知道实际上就是判断STEP和MOD是否互素

#include <stdio.h>
#include <string.h>
#define N 100002
int s[N];
int n,m,res;
int main(){
	freopen("a.txt","r",stdin);
	while(scanf("%d %d",&n,&m)!=EOF){
		int i,j,x;
		for(i = 0;i<m;i++){
			res = 0;
			memset(s,0,sizeof(s));
			x = i;
			while(!s[x]){
				res ++;
				s[x] = 1;
				x = (x+n)%m;
			}
			if(res == m)
				break;
		}
		printf("%10d%10d    ",n,m);
		if(i == m)
			printf("Bad Choice\n\n");
		else
			printf("Good Choice\n\n");
	}
	return 0;
}

求个最大公约就OK:

#include <stdio.h>
#include <string.h>
#define N 100002
int s[N];
int n,m,res;
int gcd(int x,int y){
	int a;
	while(y){
		a = x%y;
		x = y;	
		y = a;
	}
	return x;
}
int main(){
	freopen("a.txt","r",stdin);
	while(scanf("%d %d",&n,&m)!=EOF){
		printf("%10d%10d    ",n,m);
		if(gcd(n,m) != 1)
			printf("Bad Choice\n\n");
		else
			printf("Good Choice\n\n");
	}
	return 0;
}


你可能感兴趣的:(poj 1597 搜索NO,数论YES)