解方程

这道题要用到秦九韶算法,总体感觉不算太难,但是考场想不一定能知道这是秦九韶公式,但是应该不难想到提取公因式,其实就是秦九韶公式的核心。

然后就因为快读取模的时候位置取错了就得了\(30\)分.

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<queue>
using namespace std;
const int p=1000000007;
bool t=true;
int n,m,ans,cnt,sum=0;
int a[107],k[1000003];
#define scy(x) freopen(x".in","r",stdin); freopen(x".out","w",stdout);
inline long long read(){
  long long x=0,f=1;
  char ch=getchar();
  while(ch>'9'||ch<'0'){
    if(ch=='-') f=-1;
    ch=getchar();
  }
  while(ch>='0'&&ch<='9'){
    x=((x<<1)+(x<<3)+(ch^48))%p;
    ch=getchar();
  }
  return x*f;
}
bool calc (long long x){
  sum=0;
  for(long long i=n;i>=1;i--){
    sum=((a[i]+sum)*x)%p;
     //这里套用秦九韶算法求多项式的值 注意a[0]并不需要乘x!
   }
   sum=(sum+a[0])%p;
   return !sum;
 }//如果答案是0说明x值为该多项式的解,返回1(true)
 int main(){
   scy("in");
   n=read(),m=read();
   for(long long i=0;i<=n;i++){
     a[i]=read();
   }
   for(long long i=1;i<=m;i++){
     if(calc(i)){
       t=false;
       ans++;
       k[++cnt]=i;
     }
   }
   if(t){
    cout<<ans<<endl;
     return 0;
   }
   printf("%d\n",ans);
  for(long long i=1;i<=cnt;i++){
    printf("%d\n",k[i]);
  }
  return 0;
}

posted on 2020-10-14 10:44  fishsit  阅读(104)  评论(0编辑  收藏  举报

导航