终于有一个SPOJ题目是我自己独立做出来的,ORZ,太感动了。
题目意思是给你一个素数,问你一个数r是否满足,r,r^2,r^3,……,r^p-1,全不相同。
以前做过这种类型的题目额。是这样的。
根据欧拉定理我们知道,对于任意一个x<p,我们都有 x^(p-1)==1(mod p),这样我们只要判断x是否对于p-1的所有因数y是否都不满足 x^y!=1,如果存在等于1的情况,那说明就是NO咯。
上代码:
#include <cstdio>
#define ll long long
using namespace std; ll power(ll a,ll b,ll p)
{
ll ans=;
while (b)
{
if (b&) ans=(ans*a)%p;
b>>=;
a=(a*a)%p;
}
return ans;
} bool check(ll x,ll p)
{
for (ll i= ;i*i<p; i++)
{
if ((p-)%i!=) continue;
if (power(x,i,p)==) return false;
if (power(x,(p-)/i,p)==) return false;
}
return true;
} int main()
{
ll n,p,x;
while (scanf("%lld%lld",&p,&n) && (n|p))
{
while (n--)
{
scanf("%lld",&x);
x%=p;
if (x==)
{
printf("NO\n");
continue;
}
if (check(x,p)) printf("YES\n");
else printf("NO\n");
}
}
return ;
}