Problem Description
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.
Input
The first line is the number of equations T, T<=50.
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)
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)
Output
For each equation f(x) 0 (mod pri*pri), first output the case number, then output anyone of x if there are many x fitting the equation, else output "No solution!"
Sample Input
4
2 1 1 -5 7
1 5 -2995 9929
2 1 -96255532 8930 9811
4 14 5458 7754 4946 -2210 9601
Sample Output
Case #1: No solution!
Case #2: 599
Case #3: 96255626
Case #4: No solution!
Source
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <stdio.h> 2 typedef long long ll; 3 4 ll a[10],b[10010]; 5 ll f(ll n,ll x) 6 { 7 if(n==0) return a[0]; 8 else if(n==1) return a[1]*x+a[0]; 9 else if(n==2) return a[2]*x*x+a[1]*x+a[0]; 10 else if(n==3) return a[3]*x*x*x+a[2]*x*x+a[1]*x+a[0]; 11 else return a[4]*x*x*x*x+a[3]*x*x*x+a[2]*x*x+a[1]*x+a[0]; 12 } 13 14 int main() 15 { 16 ll T,n,cases=0,i,j; 17 scanf("%I64d",&T); 18 while(cases++<T) 19 { 20 ll flag=0,t=0; 21 scanf("%I64d",&n); 22 for(i=n; i>=0; i--) scanf("%I64d",&a[i]); 23 ll pri; 24 scanf("%I64d",&pri); 25 for(i=0; i<=pri; i++) 26 if(f(n,i)%pri==0) b[++t]=i; 27 28 printf("Case #%d: ",cases); 29 if(t==0) { puts("No solution!"); continue;} 30 31 for(i=1; i<=t; i++) 32 { 33 for(j=0; j<pri; j++) 34 { 35 if(f(n,b[i]+pri*j)%(pri*pri)==0) 36 { 37 printf("%I64d\n",b[i]+pri*j); 38 flag=1; 39 break; 40 } 41 } 42 if(flag==1) break; 43 } 44 if(flag==0) puts("No solution!"); 45 } 46 47 return 0; 48 }