先求出模p的根,然后加上p的倍数枚举求模p的平方的根。
ACcode:
#include<cstdio>
#include<cstring>
typedef long long LL;
const int NS=10010;
int deg,p,q,a[10],ans;
int b[NS],top;
int cal(int d,int x,int mod)
{
LL t;
int res=0;
for (int i=0;i<=d;i++)
{
t=a[i];
for (int j=0;j<i;j++)
t=(t*x)%mod;
res=(res+t)%mod;
}
return res;
}
int main()
{
int T,cas=0;
scanf("%d",&T);
while (~scanf("%d",°))
{
for (int i=deg;i>=0;i--)
scanf("%d",&a[i]);
scanf("%d",&p);
ans=-1,top=0,q=p*p;
for (int i=0;i<p;i++)
if (cal(deg,i,p)==0) b[top++]=i;
for (int i=0;i<top;i++)
{
for (int j=b[i];j<q;j+=p)
if (cal(deg,j,q)==0)
{
ans=j;
break;
}
if (ans>=0) break;
}
if (ans>=0) printf("Case #%d: %d\n",++cas,ans);
else printf("Case #%d: No solution!\n",++cas);
}
return 0;
}