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.
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)
Output for each equation f (x) 0 (mod Pri * Pri), first output the case number, then output anyone of X if there are running X fitting the equation, else output "no solution! "
Sample Input
42 1 1 -5 71 5 -2995 99292 1 -96255532 8930 98114 14 5458 7754 4946 -2210 9601
Sample output
Case #1: No solution!Case #2: 599Case #3: 96255626Case #4: No solution!
Source2013
ACM-ICPC Changsha Division national invitational Competition -- reproduction of questions
Recommendzhoujiaqi2010 we found that the data range of the question is determined to be saved using int64. Let's take a look at this question, which mainly requires a multiple of Pri * Pri, we can first find a multiple of PRI. In this way, we can add PRI to this number, which can greatly reduce the number of enumerations and find the final solution! If the Pri * Pri still does not find the solution, then there is no solution. Why? Because, if a number is greater than Pri * Pri, it must be equal to or less than a certain number of Pri * Pri, that is, the result of MOD Pri * Pri is the same, because there is no solution within Pri * Pri, the solution that is greater than Pri * Pri will naturally not be solved!
#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;}