洛谷P2312 解方程题解

洛谷P2312 解方程题解

题目描述

已知多项式方程:

\[a_0+a_1x+a_2x^2+\cdots+a_nx^n=0\]
求这个方程在 \([1,m]\) 内的整数解(\(n\)\(m\) 均为正整数)。

输入格式

输入共 \(n + 2\) 行。

第一行包含 \(2\) 个整数 \(n, m\) ,每两个整数之间用一个空格隔开。

接下来的 \(n+1\) 行每行包含一个整数,依次为 \(a_0,a_1,a_2\ldots a_n\).

输出格式

第一行输出方程在 \([1,m]\) 内的整数解的个数。

接下来每行一个整数,按照从小到大的顺序依次输出方程在 [1,m][1,m] 内的一个整数解。

输入输出样例

输入 #1 复制

2 10
1
-2
1

输出 #1 复制

1
1

输入 #2 复制

2 10
2
-3
1

输出 #2 复制

2
1
2

输入 #3 复制

2 10
1
3
2

输出 #3 复制

0
说明/提示
对于 \(30%\) 的数据:\(0<n\le 2\),\(|a_i|\le 100\),\(a_n≠0\),\(m<1000\)
对于 \(50%\) 的数据:\(0<n\le 100,|a_i|\le 10^{100},a_n≠0,m<1000\)
对于 \(70%\) 的数据:\(0<n\le 100,|a_i|\le 10^{10000},a_n≠0,m<10^4\)

对于 \(100%\) 的数据:\(0<n\le 100,|a_i|\le 10^{10000},a_n≠0,m<10^6\)

解析:

秦九韶公式 + 快读
输入要注意,因为输入的\(a[i]\)范围比较大,
所以就对一个质数取模
\(1\)\(m\)进行枚举,枚举的是\(x\)
然后利用秦九韶公式进行求解
如果返回的值是\(0\),那么就记录
反之继续。

#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;
}

猜你喜欢

转载自www.cnblogs.com/handsomegodzilla/p/11482644.html
今日推荐