洛谷P2312 解方程(暴力)

题意

题目链接

Sol

出这种题会被婊死的吧。。。

首先不难想到暴力判断,然后发现连读入都是个问题。

对于(a[i])取模之后再判断就行了。注意判断可能会出现误差,可以多找几个模数

#include<bits/stdc++.h>
#define Fin(x) {freopen(x, "r", stdin);}
#define int long long 
using namespace std;
const int MAXN = 2e5 + 10, mod = 19997;
inline int read() {
	char c = getchar(); int x = 0, f = 1;
	while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
	while(c >= '0' && c <= '9') x = (x * 10 + c - '0') % mod, c = getchar();
	return x * f;
}
int N, M, a[MAXN];
int add(int x, int y) {
	if(x + y < 0) return x + y + mod;
	else return x + y >= mod ? x + y - mod : x + y;
}
int mul(int x, int y) {
	return 1ll * x * y % mod;
}
int check(int val) {
	int now = 0;
	for(int i = N; i >= 0; i--) now = mul(now, val), now = add(now, a[i]);
	return now == 0;
}
signed main() {
	N = read(); M = read();
	for(int i = 0; i <= N; i++) a[i] = read();
	int ans = 0; vector<int> out;
	for(int i = 1; i <= M; i++) if(check(i)) ans++, out.push_back(i);
	printf("%d
", ans);
	for(int i = 0; i < out.size(); i++) printf("%d
", out[i]);
	return 0;
}
原文地址:https://www.cnblogs.com/zwfymqz/p/9835781.html