[noip2014] 解方程

题目描述

已知多项式方程:

a0+a1x+a2x2+…+anxn=0

求这个方程在[1, m ] 内的整数解(n 和m 均为正整数)

大水题,注意取mod
要会快读,否则输入时可能会溢出

#include
#include
#include
using namespace std;
#define ll long long

const int maxn = 1000000 + 100;
const int mod = 1000000007;
ll n,m,a[maxn];
ll val[maxn],cnt = 0;

ll read() {
    ll x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9') {
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = ( (x << 1) + (x << 3) + ch - '0' ) % mod;
        ch = getchar();
    }
    return x * f;
}

bool cal(ll x) {
	ll sum = 0;
	for(int i = n; i >= 0; i--) {
		sum = (sum * x + a[i]) % mod;
	}
	return !sum;
}

int main() {
	n = read(), m = read();
	for(int i = 0; i <= n; i++) a[i] = read();
	for(ll i = 1; i <= m; i++) {
		if(cal(i)) {
			val[++cnt] = i;
		}
	}
	printf("%d\n",cnt);
	for(int i = 1; i <= cnt; i++) {
		printf("%d\n",val[i]);
	}
	return 0;
}

你可能感兴趣的:(#,Noip)