题解:
如果x满足f(x)%(pri*pri)==0,那么一定有f(x)%p==0,那么一定会有f(x+p)%pri==0,于是可以先找到0到prime中满足f(x)%p==0的x,然后从x开始遍历到pri*pri,每次遍历+pri,如果有满足f(i)%(pri*pri)存在,输出i即可
#include <cstdio> #include <cstring> #include <algorithm> #define ll long long using namespace std; int T,deg,a[5],pri; ll cal(ll k,ll mod) { ll ans=0,tmp=1; for(int i=0;i<=deg;i++) { ans=(ans+a[i]*tmp)%mod; tmp=tmp*k%mod; } return (ans%mod+mod)%mod; } int main() { //freopen("G:\\input.txt","r",stdin); scanf("%d",&T); int cas=0; while(T--) { scanf("%d",°); for(int i=deg;i>=0;i--) scanf("%d",a+i); scanf("%d",&pri); bool flag=0; for(int k=0;k<pri;k++) { int tmp=cal(k,pri); if(tmp==0) { for(int x=k;x<=pri*pri;x+=pri) { ll tmp2=cal(x,pri*pri); if(tmp2==0) { printf("Case #%d: %d\n",++cas,x); flag=1; break; } } } if(flag) break; } if(!flag) printf("Case #%d: No solution!\n",++cas); } return 0; }