[BZOJ-3751]解方程

Description

已知多项式方程: a0+a1x+a2x^2+…+an*x^n=0
求这个方程在[1,m]内的整数解(n和m均为正整数)。
ai ≤ 10^10000

Solution

这是一道[NOIP2014]的题。
显然无法直接做,所以我们要用到hash的思想。
我们选取几个素数,如果一个数x在模这些素数意义下都使方程成立,那我们就认为这是方程的一个解。

Notice

注意ai的范围。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define Rep(i, a, b) for (int i = (a); i < (b); i++)
#define travel(i, u) for (int i = head[u]; ~i; i = edge[i].next)

const ll INF = 1e9, Mo = 998244353;
const int N = 100, M = 50000;
const double eps = 1e-6;
namespace slow_IO
{
ll read()
{
ll x = 0; int zf = 1; char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * zf;
}
void write(ll y)
{
if (y < 0) putchar('-'), y = -y;
if (y > 9) write(y / 10);
putchar(y % 10 + '0');
}
}
using namespace slow_IO;

int X[6][N + 5], Prime[6] = {0, 11261, 14843, 19997, 21893, 22877}, F[6][M + 5], Ans[M + 5];
int n, m, num = 5, ans = 0;
char st[M + 5];
int Calc(int x, int cnt)
{
int now = X[cnt][0], mi = 1;
rep(i, 1, n)
{
mi = mi * x % Prime[cnt];
now = (now + X[cnt][i] * mi) % Prime[cnt];
}
return now == 0;
}

int sqz()
{
n = read(), m = read();
rep(i, 0, n)
{
scanf("%s", st);
int len = strlen(st), flag;
if (st[0] == '-') flag = 0;
else flag = st[0] - '0';
rep(j, 1, num)
{
X[j][i] = flag;
Rep(k, 1, len) X[j][i] = (X[j][i] * 10 + st[k] - '0') % Prime[j];
if (!flag) X[j][i] = -X[j][i];
}
}
rep(i, 1, num)
Rep(j, 0, Prime[i]) F[i][j] = Calc(j, i);
rep(i, 1, m)
{
int flag = 1;
rep(j, 1, num) if (!F[j][i % Prime[j]])
{
flag = 0;
break;
}
if (flag) Ans[++ans] = i;
}
printf("%d\n", ans);
rep(i, 1, ans) printf("%d\n", Ans[i]);
return 0;
}
文章目录
  1. 1. Description
  2. 2. Solution
  3. 3. Notice
  4. 4. Code