Let f(x) = a
nx
n +...+ a
1x +a
0, in which a
i (0 <= i <= n) are all known integers. We call f(x) 0 (mod m) congruence equation. If m is a composite, we can factor m into powers of primes and solve every such single equation after which we merge them using the Chinese Reminder Theorem. In this problem, you are asked to solve a much simpler version of such equations, with m to be prime's square.
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!
#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long LL;
LL a[5];
LL f(int n,LL x)
{
LL sum=0;
for(int i=1;i<=n;i++)
{
sum+=a[i]*x;
x*=x;
}
sum+=a[0];
return sum;
}
void solve(int n,LL pri)
{
for(LL i=0;i<pri;i++)
if(f(n,i)%pri==0)
for(LL j=i;j<=pri*pri;j+=pri)
if(f(n,j)%(pri*pri)==0)
{
printf("%lld\n",j);
return;
}
printf("No solution!\n");
}
int main()
{
int t,n;
LL pri;
cin>>t;
for(int ca=1;ca<=t;ca++)
{
scanf("%d",&n);
for(int i=n;i>=0;i--)
scanf("%lld",&a[i]);
scanf("%lld",&pri);
printf("Case #%d: ",ca);
solve(n,pri);
}
return 0;
}