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)
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!"
题目大意:给你一个最高4次幂的多项式,求一个x,满足f(x) mod phi² = 0。
思路:先枚举x = [0, phi),如果f(x) mod phi = 0,再枚举x2 = x,每次加phi,直到f(x) mod phi² = 0,输出结果。找不到输出No solution。
PS:我也不知道为什么是对的我看别人说是这么做的……我数论知识很少……我只知道要满足f(x) mod phi² = 0就要先满足f(x) mod phi = 0……
代码(62MS):
1 #include <cstdio>
2 #include <iostream>
3 #include <cstring>
4 #include <queue>
5 using namespace std;
6 typedef long long LL;
7
8 const int MAXN = 8;
9
10 int T, deg;
11 LL phi;
12 LL a[MAXN];
13
14 LL f(LL x, LL m) {
15 LL ret = 0, xx = 1;
16 for(int i = 0; i <= deg; ++i) {
17 ret = (ret + a[i] * xx) % m;
18 xx = (xx * x) % m;
19 }
20 return ret;
21 }
22
23 LL ans, ret;
24 int t;
25
26 void solve() {
27 for(ans = 0; ans < phi; ++ans) {
28 if(f(ans, phi) == 0) {
29 for(ret = ans; ret <= phi * phi; ret += phi)
30 if(f(ret, phi * phi) == 0) {
31 printf("Case #%d: %d\n", t, (int)ret);
32 return ;
33 }
34 }
35 }
36 printf("Case #%d: No solution!\n", t);
37 }
38
39 int main() {
40 cin>>T;
41 for(t = 1; t <= T; ++t) {
42 cin>>deg;
43 for(int i = deg; i >= 0; --i) cin>>a[i];
44 cin>>phi;
45 solve();
46 }
47 }
View Code