解方程

题目

这里写图片描述


题解

–这道题就真的很恶心了,一开始,直接就拼死拼活地打高精度(顺便把operator学会了……)
然后就被某个神奇的人讲:这道题只是判断,不需要把准确值求出来
当时我就很崩溃了
所以学到了一种玄妙的防止爆int的方法:不停取一个质数的模(比如10000007)

明白了这个之后,再使用美丽的秦九韶算法,加上枚举就行了

还有这道题的输入,真是恶心死我了
是需要输入输出优化的(没打了,另找大佬把)


代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=105;
const int mod=100000007;

int n,m;
long long a[MAXN];
long long sum,ans[1000005];

long long read(){
    char b;
    bool p=0;
    b=getchar();
    while(b<'0'||b>'9'){
        if(b=='-')
            p=1;
        b=getchar();
    }
    int x=0;
    while(b>='0'&&b<='9'){
        x*=10;
        x+=b-'0';
        x%=mod;
        b=getchar();
    }
    if(p)
        x*=-1;
    return x;
}

int main(){
//  freopen("equation.in","r",stdin);
//  freopen("equation.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=n+1;i++){
        a[i]=read();
    }
    for(int i=1;i<=m;i++){
        long long now=a[n+1];
        for(int j=n;j>=1;j--){
            now*=i;
            now+=a[j];
            now%=mod;
        }
        if(now==0)
            ans[++sum]=i;
    }
    cout<<sum<<endl;
    for(int i=1;i<=sum;i++)
        cout<<ans[i]<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41709770/article/details/80612135
今日推荐