Then comes T lines, each line starts with an integer deg (1<=deg<=4), meaning that f(x)’s degree is deg. Then follows deg integers, representing a n to a 0 (0 < abs(a n) <= 100; abs(a i) <= 10000 when deg >= 3, otherwise abs(a i) <= 100000000, i<n). The last integer is prime pri (pri<=10000).
Remember, your task is to solve f(x) 0 (mod pri*pri)
4 2 1 1 -5 7 1 5 -2995 9929 2 1 -96255532 8930 9811 4 14 5458 7754 4946 -2210 9601
Case #1: No solution! Case #2: 599 Case #3: 96255626 Case #4: No solution!
题目大意:
有一个多项式
f(x)=anxn+an−1xn−1+...+a1x+a0
,让你求的是
f(x)%m==0
的解。
给出一个
T
, 表示有
解题思路:
因为
f(x)%m2==0
,就一定满足
f(x)%m==0
,那么我们就把
i
从
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef long long LL;
LL Pow(LL a, LL b, LL MOD){
LL ans = 1;
while(b){
if(b & 1) ans = (ans*a) % MOD;
b>>=1;
a = (a*a) % MOD;
}
return ans;
}
LL a[5];
int main()
{
int T, n;
scanf("%d",&T);
for(int cas=1; cas<=T; cas++){
LL MOD;
scanf("%d", &n);
for(int i=n; i>=0; i--) scanf("%lld",&a[i]);
scanf("%lld",&MOD);
printf("Case #%d: ",cas);
LL mm = MOD*MOD;
for(LL i=0; i<MOD; i++){
LL sum = 0;
for(int j=0; j<=n; j++)
sum = (sum + a[j]*Pow(i, j, MOD))%MOD;
if(sum % MOD == 0){
LL jj = i;
while(jj < mm){
sum = 0;
for(int j=0; j<=n; j++)
sum = (sum + a[j]*Pow(jj, j, mm))%mm;
if(sum == 0){
printf("%lld\n",jj);
goto endW;
}
jj += MOD;
}
}
}
puts("No solution!");
endW:;
}
return 0;
}