您的位置:首页 > 其它

HDU 4569 Special equations(思维)——2013 ACM-ICPC长沙赛区全国邀请赛

2017-05-28 18:35 573 查看
  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.
[align=left]Input[/align]   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)
[align=left]Output[/align]   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!"
[align=left]Sample Input[/align]
4
2 1 1 -5 7
1 5 -2995 9929
2 1 -96255532 8930 9811
4 14 5458 7754 4946 -2210 9601

[align=left]Sample Output[/align]
Case #1: No solution!
Case #2: 599
Case #3: 96255626
Case #4: No solution!


题目大意:

有一个多项式 f(x)=anxn+an−1xn−1+...+a1x+a0,让你求的是 f(x)%m==0 的解。

给出一个 T, 表示有 T 组数据,每组数据首先给出一个 n,然后有 n+1 个数,从左往右分别表示 an,an−1...a1,a0, 然后给出一个数 m ,求 f(x)%m2==0 的解,如果解有多个输出任意一个,否则输出 No solution!

解题思路:

因为 f(x)%m2==0,就一定满足 f(x)%m==0,那么我们就把 i 从0 遍历到 m−1 ,因为在往后计算的话就是重复了,循环节就是 m,这个地方需要好好理解一下,当我们找到这样的数之后在对其判断 f(i)%m2 是不是等于 0,如果等于 0,那么我们就找到解了,然后输出,否则的话,我们将 i+=m(因为 i 必须满足是 m 的倍数),直到 i≥m2 (如同上一个一样,这里的循环节就是 m2 ),将多项式乘加起来然后计算 f(i)%m2 就OK了,如果直到i≥m2 还没有找到解的话,那么就是无解,输出 No solution!

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef long long LL;
LL Pow(LL a, LL b, LL MOD){
LL ans = 1;
while(b){
if(b & 1) ans = (ans*a) % MOD;
b>>=1;
a = (a*a) % MOD;
}
return ans;
}
LL a[5];
int main()
{
int T, n;
scanf("%d",&T);
for(int cas=1; cas<=T; cas++){
LL MOD;
scanf("%d", &n);
for(int i=n; i>=0; i--) scanf("%lld",&a[i]);
scanf("%lld",&MOD);
printf("Case #%d: ",cas);
LL mm = MOD*MOD;
for(LL i=0; i<MOD; i++){
LL sum = 0;
for(int j=0; j<=n; j++)
sum = (sum + a[j]*Pow(i, j, MOD))%MOD;
if(sum % MOD == 0){
LL jj = i;
while(jj < mm){
sum = 0;
for(int j=0; j<=n; j++)
sum = (sum + a[j]*Pow(jj, j, mm))%mm;
if(sum == 0){
printf("%lld\n",jj);
goto endW;
}
jj += MOD;
}
}
}
puts("No solution!");
endW:;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐