1. 程式人生 > 實用技巧 >解方程

解方程

這道題要用到秦九韶演算法,總體感覺不算太難,但是考場想不一定能知道這是秦九韶公式,但是應該不難想到提取公因式,其實就是秦九韶公式的核心。

然後就因為快讀取模的時候位置取錯了就得了\(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;
}