[NOIp提高组2014]解方程

题目大意

求方程

\[ \sum_{i=0}^{n}a_ix^i=0 \]

在\([1,m]\)内的整数解

\(1<=|a_i|<=10^{10000},a_i\neq 0,1<=n<=100,1<=m<10^6\)

解题思路

最朴素的做法就是尝试所有解,判断左边多项式值是否为零

但还有一个高精问题(如果你真要用高精我也不拦你)

可以考虑一种类哈希做法,如果所有运算都对一个大质数取模,答案的正确性基本可以保证,而且可以省掉高精

实测计算左侧多项式秦九昭算法和直接算的时间相差不大(只要你不是用快速幂算的)

\(O(nm)\)做法,貌似\(10^8\)再加一堆*%运算,卡常?

没错,我在Calc函数内写ret*=x;ret+=X[i];ret%=P就被卡掉QAQ

#include<iostream>
#include<cstdio>
#include<vector>

const long long P=998244353;

int n,m;

long long X[200];

int all;
std::vector<int> V;

void read(long long &x){
    char ch;
    while (isspace(ch=getchar()));
    bool flag=false;
    if (ch=='-'){flag=true;ch=getchar();}
    x=ch&15;
    while (isdigit(ch=getchar())){x=(x<<1)+(x<<3)+(ch&15);x%=P;}
    if (flag) x=-x+P;
}

long long Calc(int x){
    long long ret=X[n];
    for (int i=n-1;i>=0;i--) ret=(ret*x+X[i])%P;
    return ret;
}

int main(){
    scanf("%d%d",&n,&m);
    for (int i=0;i<=n;i++) read(X[i]);
    for (int x=1;x<=m;x++){
        if (!Calc(x)){
            all++;
            V.push_back(x);
        }
    }
    printf("%d\n",all);
    for (std::vector<int>::iterator it=V.begin();it!=V.end();it++) printf("%d\n",*it);
}

猜你喜欢

转载自www.cnblogs.com/ytxytx/p/9499662.html
今日推荐