Special equations
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 379 Accepted Submission(s): 232
Special Judge
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
有点郁闷,纠结很久还是不懂。。。
思路:能被pi*pi整除,必定能被pi整除,所以可以先判断nengfoubeipi整除,以此优化算法
问题:不懂为什么求出一个能被pi整除的数后每次循环是加pi。。。
另注:打表会T
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int T,n,a[5],pri,flag,ncase,i,ans,j;
long long fx(int x,int p)//求f(x)取模P的函数
{
long long sum = 0;
int i;
for(i = n; i >= 0; i--)
sum = (a[i] + sum * x ) % p;
return (sum % p);
}
int main()
{
cin >> T;
while(T--)
{
ncase++;
memset(a,0,sizeof(a));
flag = 0;
cin >> n;
for(i = n; i >= 0; i--)
cin >> a[i];
cin >> pri;
for(j = 0; j < pri; j++)
{
if(fx(j,pri) == 0)
for(i = j; i < pri * pri; i+=pri)
//这里不懂 {
if(fx(i,pri * pri) == 0)
{
if(!flag) ans = i;
flag++;
}
if(flag > 1) break;
}
}
printf("Case #%d: ",ncase);
if(!flag ) printf("No solution!\n");
else printf("%d\n",ans);
}
return 0 ;
}
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
int T,n,a[5],pri,flag,ncase,i,ans,j;
long long fx(int x,int p)//求f(x)取模P的函数
{
long long sum = 0;
int i;
for(i = n; i >= 0; i--)
sum = (a[i] + sum * x ) % p;
return (sum % p);
}
int main()
{
cin >> T;
while(T--)
{
ncase++;
memset(a,0,sizeof(a));
flag = 0;
cin >> n;
for(i = n; i >= 0; i--)
cin >> a[i];
cin >> pri;
for(j = 0; j < pri; j++)
{
if(fx(j,pri) == 0)
for(i = j; i < pri * pri; i+=pri)
{
if(fx(i,pri * pri) == 0)
{
if(!flag) ans = i;
flag++;
}
if(flag > 1) break;
}
}
printf("Case #%d: ",ncase);
if(!flag ) printf("No solution!\n");
else printf("%d\n",ans);
}
return 0 ;
}