Luo Gu P2312 solution to a problem solving equations

Luo Gu P2312 solution to a problem solving equations

Title Description

Known polynomial equation:

\ [a_0 + a_1x + a_2x ^
2 + \ cdots + a_nx ^ n = 0 \] seeking the equation \ ([1, m] \ ) integer solutions within ( \ (n-\) and \ (m \) are It is a positive integer).

Input Format

Input common \ (n + 2 \) line.

The first line contains \ (2 \) integers \ (n-, m \) , each between two integers separated by a space.

The next \ (n + 1 \) lines contains an integer, followed by \ (a_0, A_1, A_2 \ ldots A_N \) .

Output Format

In the first line of output equation \ ([1, m] \ ) the number of integers in the solution.

Next, each line an integer, an integer in the equation [1, m] [1, m] in solution in the order from small to large are sequentially output.

Sample input and output

Input # 1 replication

2 10
1
-2
1

Copy Output # 1

1
1

Input # 2 Copy

2 10
2
-3
1

Copy Output # 2

2
1
2

Input # 3 Copy

2 10
1
3
2

Copy Output # 3

0
Description / prompt
for the \ (30% \) data: \ (0 <n-\ Le 2 \) , \ (| a_i | \ Le 100 \) , \ (A_N ≠ 0 \) , \ (m <1000 \ )
for \ (50% \) data: \ (0 <n-\ Le 100, | a_i | \ Le 10 ^ {100}, A_N ≠ 0, m <1000 \)
to \ (70% \) data: \ (0 <n-\ Le 100, | a_i | \ Le ^ {10} 10000, A_N ≠ 0, m <10. 4 ^ \) .

For \ (100% \) Data: \ (0 <n-\ Le 100, | a_i | \ Le ^ {10} 10000, A_N ≠ 0, m <10. 6 ^ \) .

Resolution:

Qin Jiushao formula + Fast Read
input to be noted that, since the input \ (a [i] \) relatively large range,
so it modulo a prime number to
the \ (1 \) to \ (m \) enumeration, enumeration is \ (the X-\) ,
and then use a formula to solve Qin Jiushao
if the value returned is \ (0 \) , then record
the other hand continues.

#include <cstdio>
#include <iostream>
#include <cmath>
#include <queue>
#include <stack>
#include <cstring>
#include <vector>
#include <algorithm>
#include <iomanip>
#define Max 105
#define re register
#define D double
#define int long long
int n,m,a[Max],ans = 0, Ans[1000012];
const int mod = 19260817;
int read() {
    char ch = getchar(); int f = 1, s = 0;
    while(ch < '0' || ch > '9') {
        if(ch == '-') f = -1;
        ch =getchar();
    }
    while(ch >= '0' && ch <= '9') {
        s = (10 * s + ch - '0') % mod;
        ch = getchar();
    }
    return s * f;
}
int work(int x) {
    int ANS = 0;
    for(re int i = n ; i >= 1 ; -- i)
        ANS = ((ANS + a[i]) * x)% mod;
    ANS = (ANS + a[0]) % mod;
    return ANS;
}
void Main() {
    scanf("%lld%lld",&n,&m);
    for(re int i = 0; i <= n; ++ i) a[i] = read();
    for(re int i = 1; i <= m; ++ i)
        if(work(i) == 0) ans ++, Ans[ans] = i;
    printf("%lld\n",ans);
    for(re int i = 1; i <= ans; ++ i) printf("%lld\n",Ans[i]);
}
signed main() {
    Main();
    return 0;
}

Guess you like

Origin www.cnblogs.com/handsomegodzilla/p/11482644.html