結果
問題 | No.980 Fibonacci Convolution Hard |
ユーザー | tko919 |
提出日時 | 2020-02-02 13:38:19 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 8,653 bytes |
コンパイル時間 | 2,285 ms |
コンパイル使用メモリ | 199,816 KB |
実行使用メモリ | 111,092 KB |
最終ジャッジ日時 | 2024-09-18 20:42:16 |
合計ジャッジ時間 | 40,783 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | TLE | - |
testcase_01 | TLE | - |
testcase_02 | TLE | - |
testcase_03 | TLE | - |
testcase_04 | TLE | - |
testcase_05 | TLE | - |
testcase_06 | TLE | - |
testcase_07 | TLE | - |
testcase_08 | RE | - |
testcase_09 | TLE | - |
testcase_10 | TLE | - |
testcase_11 | TLE | - |
testcase_12 | RE | - |
testcase_13 | TLE | - |
testcase_14 | TLE | - |
testcase_15 | TLE | - |
testcase_16 | WA | - |
ソースコード
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; //template #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rrep(i,a,b) for(int i=(a);i>(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; void tostr(ll x,string& res){while(x)res+=('0'+(x%10)),x/=10; reverse(ALL(res)); return;} template<class T> inline bool chmax(T& a,T b){ if(a<b){a=b;return 1;}return 0; } template<class T> inline bool chmin(T& a,T b){ if(a>b){a=b;return 1;}return 0; } //template end template<ll mod=1000000007>struct mint { ll val; static ll get_mod(){return mod;} ll inv() const{ ll tmp,a=val,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; } mint():val(0){} mint(ll x):val(x>=0?x%mod:mod+(x%mod)){} mint pow(ll t){mint res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;}return res;} mint& operator+=(const mint& x){if((val+=x.val)>=mod)val-=mod;return *this;} mint& operator-=(const mint& x){if((val+=mod-x.val)>=mod)val-=mod; return *this;} mint& operator*=(const mint& x){val=val*x.val%mod; return *this;} mint& operator/=(const mint& x){val=val*x.inv()%mod; return *this;} mint operator+(const mint& x)const{return mint(*this)+=x;} mint operator-(const mint& x)const{return mint(*this)-=x;} mint operator*(const mint& x)const{return mint(*this)*=x;} mint operator/(const mint& x)const{return mint(*this)/=x;} bool operator==(const mint& x)const{return val==x.val;} bool operator!=(const mint& x)const{return val!=x.val;} }; template<ll mod>struct factorial { using Mint=mint<mod>; vector<Mint> Fact, Finv; public: factorial(int maxx){ Fact.resize(maxx+1),Finv.resize(maxx+1); Fact[0]=Mint(1); rep(i,0,maxx)Fact[i+1]=Fact[i]*(i+1); Finv[maxx]=Mint(1)/Fact[maxx]; rrep(i,maxx,0)Finv[i-1]=Finv[i]*i; } Mint fact(int n,bool inv=0){if(inv)return Finv[n];else return Fact[n];} Mint nPr(int n,int r){if(n<0||n<r||r<0)return Mint(0);else return Fact[n]*Finv[n-r];} Mint nCr(int n,int r){if(n<0||n<r||r<0)return Mint(0);else return Fact[n]*Finv[r]*Finv[n-r];} }; vector<ll> rt,irt; template<ll mod,ll p>void init(int lg=21){ using Mint=mint<mod>; Mint prt=p; rt.resize(1<<lg,1); irt.resize(1<<lg,1); rep(w,0,lg){ int mask=(1<<w)-1,t=Mint(-1).val>>w; Mint g=prt.pow(t),ig=prt.pow(mod-1-t); rep(i,0,mask){ rt[mask+i+1]=(g*rt[mask+i]).val; irt[mask+i+1]=(ig*irt[mask+i]).val; } } } template<ll mod,ll p>struct FPS{ using Mint=mint<mod>; vector<Mint> f; Mint prt=p; FPS():f({1}){} FPS(int _n):f(_n){} FPS(vector<Mint> _f):f(_f){} Mint& operator[](const int i){return f[i];} Mint eval(Mint x){ Mint res,w=1; for(Mint v:f)res+=w*v,w*=x; return res; } FPS inv()const{ assert(f[0]!=0); int n=f.size(); FPS res(n); res.f[0]=f[0].inv(); for(int k=1;k<n;k<<=1){ FPS g(k*2),h(k*2); rep(i,0,min(k*2,n))g[i]=f[i]; rep(i,0,k)h[i]=res[i]; g.ntt(); h.ntt(); rep(i,0,k*2)g[i]*=h[i]; g.ntt(1); rep(i,0,k)g[i]=0,g[i+k]*=-1; g.ntt(); rep(i,0,k*2)g[i]*=h[i]; g.ntt(1); rep(i,k,min(k*2,n))res[i]=g[i]; } return res; } void ntt(bool inv=0){ int n=f.size(); if(n==1)return; if(inv){ for(int i=1;i<n;i<<=1){ for(int j=0;j<n;j+=i*2){ rep(k,0,i){ f[i+j+k]*=irt[i*2-1+k]; const Mint tmp=f[j+k]-f[i+j+k]; f[j+k]+=f[i+j+k]; f[i+j+k]=tmp; } } } Mint mul=Mint(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){ rep(k,0,i){ const Mint tmp=f[j+k]-f[i+j+k]; f[j+k]+=f[i+j+k]; f[i+j+k]=tmp*rt[i*2-1+k]; } } } } } FPS operator+(const FPS& g)const{return FPS(*this)+=g;} FPS operator-(const FPS& g)const{return FPS(*this)-=g;} FPS operator*(const FPS& g)const{return FPS(*this)*=g;} template<class T>FPS operator*(T t)const{return FPS(*this)*=t;} FPS operator/(const FPS& g)const{return FPS(*this)/=g;} template<class T>FPS operator/(T t)const{return FPS(*this)/=t;} FPS operator%(const FPS& g)const{return FPS(*this)%=g;} FPS& operator+=(FPS g){ if(g.f.size()>f.size())f.resize(g.f.size()); rep(i,0,g.f.size())f[i]+=g[i]; return *this; } FPS& operator-=(FPS g){ if(g.f.size()>f.size())f.resize(g.f.size()); rep(i,0,g.f.size())f[i]-=g[i]; return *this; } FPS& operator*=(FPS g){ int m=f.size()+g.f.size()-1,n=1; while(n<m)n<<=1; f.resize(n); g.f.resize(n); ntt(); g.ntt(); rep(i,0,n)f[i]*=g[i]; ntt(1); f.resize(m); return *this; } template<class T>FPS& operator*=(T t){for(Mint x:f)x*=t; return *this;} FPS& operator/=(FPS g){ if(g.f.size()>f.size())return *this=FPS({0}); reverse(ALL(f)); reverse(ALL(g.f)); int n=f.size()-g.f.size()+1; f.resize(n); g.f.resize(n); FPS mul=g.inv(); *this*=mul; f.resize(n); reverse(ALL(f)); return *this; } template<class T>FPS& operator/=(T t){for(Mint x:f)x/=t; return *this;} FPS& operator%=(FPS g){ *this-=*this/g*g; while(!f.empty()&&f.back()==0)f.pop_back(); return *this; } FPS sqrt(){ int n=f.size(); FPS res(1); res[0]=1; for(int k=1;k<n;k<<=1){ FPS ff=*this; res.f.resize(k*2); res+=ff/res; res/=2; } res.f.resize(n); return res; } FPS diff(){ FPS res=*this; rep(i,0,res.f.size()-1)res[i]=res[i+1]*(i+1); res.f.pop_back(); return res; } FPS inte(){ FPS res=*this; res.f.push_back(0); rrep(i,res.f.size()-1,0)res[i]=res[i-1]/i; res[0]=0; return res; } FPS log(){ assert(f[0]==1); FPS res=diff()/(*this); res.f.resize(f.size()-1); res=res.inte(); return res; } FPS exp(){ assert(f[0]==0); int m=f.size(),n=1; while(n<m)n<<=1; f.resize(n); FPS d=diff(),res(n); vector<FPS> pre; for(int k=n;k;k>>=1){ FPS g=d; g.f.resize(k); g.ntt(); pre.push_back(g); } auto dfs=[&](auto dfs,int l,int r,int dep)->void{ if(r-l==1){if(l>0)res[l]/=l; return;} int m=(l+r)>>1; dfs(dfs,l,m,dep+1); FPS g(r-l); rep(i,0,m-l)g[i]=res[l+i]; g.ntt(); rep(i,0,r-l)g[i]*=pre[dep][i]; g.ntt(1); rep(i,m,r)res[i]+=g[i-l-1]; dfs(dfs,m,r,dep+1); }; res[0]=1; dfs(dfs,0,n,0); res.f.resize(m); return res; } };//need to initialize using P=pair<ll,ll>; using M1=mint<1045430273>; using M2=mint<1051721729>; using M3=mint<1053818881>; template<ll mod=1000000007> vector<ll> optional_convolution(vector<ll> a,vector<ll> b){ using Mint=mint<mod>; vector<Mint> aa(a.size()),bb(b.size()); rep(i,0,a.size())aa[i]=Mint(a[i]); rep(i,0,b.size())bb[i]=Mint(b[i]); vector<ll> vals[3]; vector<M1> a1(a.size()),b1(b.size()); rep(i,0,a.size())a1[i]=M1(a[i]); rep(i,0,b.size())b1[i]=M1(b[i]); init<1045430273,3>(); FPS<1045430273,3> x1(a1),y1(b1); x1*=y1; auto f=x1.f; for(auto v:f)vals[0].push_back(v.val); vector<M2> a2(a.size()),b2(b.size()); rep(i,0,a.size())a2[i]=M2(a[i]); rep(i,0,b.size())b2[i]=M2(b[i]); init<1051721729,6>(); FPS<1051721729,6> x2(a2),y2(b2); x2*=y2; auto g=x2.f; for(auto v:g)vals[1].push_back(v.val); vector<M3> a3(a.size()),b3(b.size()); rep(i,0,a.size())a3[i]=M3(a[i]); rep(i,0,b.size())b3[i]=M3(b[i]); init<1053818881,7>(); FPS<1053818881,7> x3(a3),y3(b3); x3*=y3; auto h=x3.f; for(auto v:h)vals[2].push_back(v.val); int n=vals[0].size(); vector<ll> res(n); M2 r_12=M2(M1::get_mod()).inv(); M3 r_13=M3(M1::get_mod()).inv(); M3 r_23=M3(M2::get_mod()).inv(); M3 r_1323=r_13*r_23; Mint w1(M1::get_mod()); Mint w2=w1*Mint(M2::get_mod()); rep(i,0,n){ ll a=vals[0][i]; ll b=(vals[1][i]+M2::get_mod()-a)*r_12.val%M2::get_mod(); ll c=((vals[2][i]+M3::get_mod()-a)*r_1323.val+(M3::get_mod()-b)*r_23.val)%M3::get_mod(); res[i]=(a+b*w1.val+c*w2.val)%mod; } return res; } const int sz=1<<18; vector<ll> a,res; int main(){ int p; scanf("%d",&p); a.resize(sz); a[0]=0; a[1]=1; rep(i,2,sz)a[i]=a[i-1]*p+a[i-2]; res=optional_convolution(a,a); int q; scanf("%d",&q); while(q--){ int x; scanf("%d",&x); printf("%d\n",res[x-2]); } return 0; }