洛谷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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值