結果

問題 No.980 Fibonacci Convolution Hard
ユーザー tko919tko919
提出日時 2020-05-05 03:06:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,542 ms / 2,000 ms
コード長 4,636 bytes
コンパイル時間 3,233 ms
コンパイル使用メモリ 198,228 KB
実行使用メモリ 406,492 KB
最終ジャッジ日時 2023-09-07 22:16:12
合計ジャッジ時間 35,686 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,485 ms
406,216 KB
testcase_01 AC 1,508 ms
406,128 KB
testcase_02 AC 1,515 ms
406,160 KB
testcase_03 AC 1,526 ms
406,280 KB
testcase_04 AC 1,542 ms
406,168 KB
testcase_05 AC 1,491 ms
406,492 KB
testcase_06 AC 1,494 ms
406,276 KB
testcase_07 AC 1,515 ms
406,284 KB
testcase_08 AC 1,532 ms
406,132 KB
testcase_09 AC 1,535 ms
406,284 KB
testcase_10 AC 1,520 ms
406,212 KB
testcase_11 AC 1,531 ms
406,168 KB
testcase_12 AC 1,503 ms
406,484 KB
testcase_13 AC 1,541 ms
406,304 KB
testcase_14 AC 1,495 ms
406,220 KB
testcase_15 AC 1,465 ms
406,344 KB
testcase_16 AC 1,467 ms
406,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end

template<unsigned mod=1000000007>struct fp {
   unsigned v;
   static unsigned get_mod(){return mod;}
   unsigned inv() const{
      int tmp,a=v,b=mod,x=1,y=0;
      while(b)tmp=a/b,a-=tmp*b,swap(a,b),x-=tmp*y,swap(x,y);
      if(x<0)x+=mod; return x;
   }
   fp():v(0){}
   fp(ll x):v(x>=0?x%mod:mod+(x%mod)){}
   fp operator-()const{return fp(-v);}
   fp pow(ll t){fp res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;} return res;}
   fp& operator+=(const fp& x){if((v+=x.v)>=mod)v-=mod; return *this;}
   fp& operator-=(const fp& x){if((v+=mod-x.v)>=mod)v-=mod; return *this;}
   fp& operator*=(const fp& x){v=ll(v)*x.v%mod; return *this;}
   fp& operator/=(const fp& x){v=ll(v)*x.inv()%mod; return *this;}
   fp operator+(const fp& x)const{return fp(*this)+=x;}
   fp operator-(const fp& x)const{return fp(*this)-=x;}
   fp operator*(const fp& x)const{return fp(*this)*=x;}
   fp operator/(const fp& x)const{return fp(*this)/=x;}
   bool operator==(const fp& x)const{return v==x.v;}
   bool operator!=(const fp& x)const{return v!=x.v;}
}; using Fp=fp<>;
template<typename T>struct factorial {
   vector<T> Fact,Finv,Inv;
   factorial(int maxx){
      Fact.resize(maxx); Finv.resize(maxx); Inv.resize(maxx);
      Fact[0]=Fact[1]=Finv[0]=Finv[1]=Inv[1]=1; unsigned mod=Fp::get_mod();
      rep(i,2,maxx){
         Fact[i]=Fact[i-1]*i;
         Inv[i]=Inv[mod%i]*(mod-mod/i);
         Finv[i]=Finv[i-1]*Inv[i];
      }
   }
   T fact(int n,bool inv=0){if(inv)return Finv[n];else return Fact[n];}
   T inv(int n){return Inv[n];}
   T nPr(int n,int r){if(n<0||n<r||r<0)return Fp(0);else return Fact[n]*Finv[n-r];}
   T nCr(int n,int r){if(n<0||n<r||r<0)return Fp(0);else return Fact[n]*Finv[r]*Finv[n-r];}
};

template<typename T,unsigned p>struct NTT{
   vector<T> rt,irt;
   NTT(int lg=23){
      const unsigned m=T(-1).v; T prt=p;
      rt.resize(1<<lg,1); irt.resize(1<<lg,1);
      for(int w=0;w<lg;w++){
         int mask=1<<w; T g=prt.pow(m>>w),ig=g.inv();
         for(int i=0;i<mask-1;i++){
            rt[mask+i+1]=g*rt[mask+i];
            irt[mask+i+1]=ig*irt[mask+i];
         }
      }
   }
   void ntt(vector<T>& f,bool inv=0){
      int n=f.size();
      if(inv){
         for(int i=1;i<n;i<<=1)for(int j=0;j<n;j+=i*2)for(int k=0;k<i;k++){
            f[i+j+k]*=irt[i*2+k]; T tmp=f[j+k]-f[i+j+k];
            f[j+k]+=f[i+j+k]; f[i+j+k]=tmp;
         } T mul=T(n).inv(); rep(i,0,n)f[i]*=mul;
      }else{
         for(int i=n>>1;i;i>>=1)for(int j=0;j<n;j+=i*2)for(int k=0;k<i;k++){
            T tmp=f[j+k]-f[i+j+k]; f[j+k]+=f[i+j+k]; f[i+j+k]=tmp*rt[i*2+k];
         }
      }
   }
   vector<T> conv(vector<T> a,vector<T> b,bool same){
      int n=a.size()+b.size()-1,m=1; while(m<n)m<<=1;
      a.resize(m); ntt(a);
      if(same)rep(i,0,m)a[i]*=a[i];
      else{b.resize(m); ntt(b); rep(i,0,m)a[i]*=b[i];}
      ntt(a,1); a.resize(n); return a;
   }
};

using M1=fp<935329793>; using M2=fp<943718401>; using M3=fp<998244353>;
NTT<M1,3> N1; NTT<M2,7> N2; NTT<M3,3> N3;
inline vector<Fp> multiply(vector<Fp> a,vector<Fp> b,bool same=0){
   int n=a.size()+b.size()-1; vector<Fp> res(n); vector<int> vals[3];
   vector<int> aa(a.size()),bb(b.size());
   rep(i,0,a.size())aa[i]=a[i].v; rep(i,0,b.size())bb[i]=b[i].v;
   vector<M1> a1(ALL(aa)),b1(ALL(bb)),c1=N1.conv(a1,b1,same);
   vector<M2> a2(ALL(aa)),b2(ALL(bb)),c2=N2.conv(a2,b2,same);
   vector<M3> a3(ALL(aa)),b3(ALL(bb)),c3=N3.conv(a3,b3,same);
   for(M1 x:c1)vals[0].push_back(x.v);
   for(M2 x:c2)vals[1].push_back(x.v);
   for(M3 x:c3)vals[2].push_back(x.v);
   M2 r_12=M2(M1::get_mod()).inv();
   M3 r_13=M3(M1::get_mod()).inv(),r_23=M3(M2::get_mod()).inv(),r_1323=r_13*r_23;
   Fp w1(M1::get_mod()); Fp w2=w1*Fp(M2::get_mod());
   rep(i,0,n){
      ll a=vals[0][i];
      ll b=(vals[1][i]+M2::get_mod()-a)*r_12.v%M2::get_mod();
      ll c=((vals[2][i]+M3::get_mod()-a)*r_1323.v+
         (M3::get_mod()-b)*r_23.v)%M3::get_mod();
      res[i]=(a+b*w1.v+c*w2.v);
   } return res;
}

int main(){
   int p,q,x; cin>>p>>q;
   vector<Fp> a(2000010); a[2]=1;
   rep(i,3,2000001)a[i]=a[i-1]*p+a[i-2];
   auto res=multiply(a,a,1);
   while(q--){
      scanf("%d",&x); printf("%d\n",res[x].v);
   }
   return 0;
}
0