Special equations
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 184 Accepted Submission(s): 92
Special Judge
Problem Description
Let f(x) = anxn +...+ a1x +a0, in which ai (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.
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 an to a0 (0 < abs(an) <= 100; abs(ai) <= 10000 when deg >= 3, otherwise
abs(ai) <= 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 an to a0 (0 < abs(an) <= 100; abs(ai) <= 10000 when deg >= 3, otherwise
abs(ai) <= 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
Recommend
zhoujiaqi2010
我们发现题目的数据范围,确定是用int64是可以存下的,我们再看看这题,主要是要求是pri*pri的倍数,那么,我们可以先找到一个是pri的倍数啊,这样,我们就可以通过,这个数不断加上pri,这样就可以大大减少枚举的次数,找到最终的解!如果到了pri*pri,还是没找到解,那么就没有解了,为什么呢?因为,如果一个数大于pri*pri,那么他一定和小于,pri*pri的某一数是相对应的,也就是mod pri*pri 的结果是一样的,因为在pri*pri之内的都不是解,那么大于pri*pri,的自然也没有解了啊!
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; int a[6],n; __int64 ff(int x) { int i; __int64 sum=0; for(i=0;i<n;i++) { sum=(sum+a[i])*x; } return sum+a[i]; } int main() { int tcase,i,j,t=1,pri; scanf("%d",&tcase); while(tcase--) { scanf("%d",&n); for(i=0;i<=n;i++) { scanf("%d",&a[i]); } scanf("%d",&pri); printf("Case #%d: ",t++); __int64 mod=pri*pri; if(n==0&&a[n]%mod!=0) { printf("No solution!\n"); continue; } int temp=0; if(ff(0)%pri==0) { printf("0\n"); continue; } for(i=0;i<pri;i++) { if(ff(i)%pri==0) { temp=i; } } if(temp==0) { printf("No solution!\n"); continue; } bool flag=true; while(temp<mod) { if(ff(temp)%mod==0) { printf("%d\n",temp); flag=false; break; } temp+=pri; } if(flag) printf("No solution!\n"); } return 0; }