hdu1014

http://acm.hdu.edu.cn/showproblem.php?pid=1014

题目看了好久,终于看完了,发现是群论里的,离散也差多忘了。。。

说白了就是求生成元,求mod N的生成元,即生成元与N互质就OK了

#include<iostream>
int gcd(int a,int b)
{
if(!b) return a;
return gcd(b,a%b);
}
int main()
{
int s,m;
while(scanf("%d%d",&s,&m)!=EOF)
{
if(gcd(s,m)==1) printf("%10d%10d Good Choice\n\n",s,m);
else printf("%10d%10d Bad Choice\n\n",s,m);
}
return 0;
}

另一种暴力法

#include<iostream>
int main()
{
int s,m,i;
while(scanf("%d%d",&s,&m)!=EOF)
{
int a[100001]={0},seed=0;
while(!a[seed])
{
a[seed]=1;
seed=(seed+s)%m;
}
for(i=0;i<m;i++)
{
if(a[i]==0)
{
printf("%10d%10d Bad Choice\n\n",s,m);break;
}
}
if(i==m) printf("%10d%10d Good Choice\n\n",s,m);
}
return 0;
}



原文地址:https://www.cnblogs.com/bersaty/p/2334216.html