[NOIp提高组2014]解方程

题目大意

求方程

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

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

\(1 \leq |a_i| \leq 10^{10000},a_i\neq 0,1 \leq n \leq 100,1 \leq 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);
}

转载于:https://www.cnblogs.com/ytxytx/p/9499662.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值