【刷题】【秦九昭式】解方程

将复杂乘法改为线性加法,程序跑的飞快

高精度范围的操作,只需要判断相等之类的,可以直接去mod大质数,或者多mod几个都行

#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
const int p=1000000007;
int n,m,ans,sum;
int A[103],key[1000003];
ll read()
{
    ll sum=0,fg=1;
    char c=getchar();
    while(c < '0' || c > '9')
    {
        if(c=='-') fg=-1;
        c=getchar();
    }
    while(c >='0' && c <='9')
    {
        sum=((sum*10)+c-'0')%p;
        c=getchar();
    }
    return sum*fg;
}
void print(int x)
{
    if(x<0)
        putchar('-'),x=-x; 
    if(x>9)
        print(x/10);
    putchar(x%10+'0');
}
bool calc(ll x)
{
    sum=0;
    for(ll i=n;i>=1;i--)
        sum=((A[i]+sum)*x)%p;
    sum=(sum+A[0])%p;
    return !sum;
}
int main()
{
    n=read();
    m=read();
    for(ll i=0;i<=n;i++)
        A[i]=read();
    for(ll i=1;i<=m;i++)
        if(calc(i))
            key[++ans]=i;
    if(!ans)
        cout<<"0"<<endl;
    else
    {
        print(ans);
        putchar('
');
        for(ll i=1;i<=ans;i++)
        {
            print(key[i]);
            putchar('
');
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/xwww666666/p/11820697.html