結果
問題 | No.215 素数サイコロと合成数サイコロ (3-Hard) |
ユーザー | beet |
提出日時 | 2020-12-19 16:28:52 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,384 ms / 4,000 ms |
コード長 | 7,678 bytes |
コンパイル時間 | 3,165 ms |
コンパイル使用メモリ | 229,072 KB |
実行使用メモリ | 6,940 KB |
最終ジャッジ日時 | 2024-09-21 10:23:57 |
合計ジャッジ時間 | 7,640 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,384 ms
6,812 KB |
testcase_01 | AC | 1,379 ms
6,940 KB |
ソースコード
// verification-helper: PROBLEM https://yukicoder.me/problems/444 #include <bits/stdc++.h> using namespace std; #define call_from_test template<typename T, T MOD = 1000000007> struct Mint{ static constexpr T mod = MOD; T v; Mint():v(0){} Mint(signed v):v(v){} Mint(long long t){v=t%MOD;if(v<0) v+=MOD;} Mint pow(long long k){ Mint res(1),tmp(v); while(k){ if(k&1) res*=tmp; tmp*=tmp; k>>=1; } return res; } static Mint add_identity(){return Mint(0);} static Mint mul_identity(){return Mint(1);} Mint inv(){return pow(MOD-2);} Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;} Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;} Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;} Mint& operator/=(Mint a){return (*this)*=a.inv();} Mint operator+(Mint a) const{return Mint(v)+=a;} Mint operator-(Mint a) const{return Mint(v)-=a;} Mint operator*(Mint a) const{return Mint(v)*=a;} Mint operator/(Mint a) const{return Mint(v)/=a;} Mint operator-() const{return v?Mint(MOD-v):Mint(v);} bool operator==(const Mint a)const{return v==a.v;} bool operator!=(const Mint a)const{return v!=a.v;} bool operator <(const Mint a)const{return v <a.v;} static Mint comb(long long n,int k){ Mint num(1),dom(1); for(int i=0;i<k;i++){ num*=Mint(n-i); dom*=Mint(i+1); } return num/dom; } }; template<typename T, T MOD> constexpr T Mint<T, MOD>::mod; template<typename T, T MOD> ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;} namespace FFT{ using dbl = double; struct num{ dbl x,y; num(){x=y=0;} num(dbl x,dbl y):x(x),y(y){} }; inline num operator+(num a,num b){ return num(a.x+b.x,a.y+b.y); } inline num operator-(num a,num b){ return num(a.x-b.x,a.y-b.y); } inline num operator*(num a,num b){ return num(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x); } inline num conj(num a){ return num(a.x,-a.y); } int base=1; vector<num> rts={{0,0},{1,0}}; vector<int> rev={0,1}; const dbl PI=asinl(1)*2; void ensure_base(int nbase){ if(nbase<=base) return; rev.resize(1<<nbase); for(int i=0;i<(1<<nbase);i++) rev[i]=(rev[i>>1]>>1)+((i&1)<<(nbase-1)); rts.resize(1<<nbase); while(base<nbase){ dbl angle=2*PI/(1<<(base+1)); for(int i=1<<(base-1);i<(1<<base);i++){ rts[i<<1]=rts[i]; dbl angle_i=angle*(2*i+1-(1<<base)); rts[(i<<1)+1]=num(cos(angle_i),sin(angle_i)); } base++; } } void fft(vector<num> &as){ int n=as.size(); assert((n&(n-1))==0); int zeros=__builtin_ctz(n); ensure_base(zeros); int shift=base-zeros; for(int i=0;i<n;i++) if(i<(rev[i]>>shift)) swap(as[i],as[rev[i]>>shift]); for(int k=1;k<n;k<<=1){ for(int i=0;i<n;i+=2*k){ for(int j=0;j<k;j++){ num z=as[i+j+k]*rts[j+k]; as[i+j+k]=as[i+j]-z; as[i+j]=as[i+j]+z; } } } } template<typename T> vector<long long> multiply(vector<T> &as,vector<T> &bs){ int need=as.size()+bs.size()-1; int nbase=0; while((1<<nbase)<need) nbase++; ensure_base(nbase); int sz=1<<nbase; vector<num> fa(sz); for(int i=0;i<sz;i++){ T x=(i<(int)as.size()?as[i]:0); T y=(i<(int)bs.size()?bs[i]:0); fa[i]=num(x,y); } fft(fa); num r(0,-0.25/sz); for(int i=0;i<=(sz>>1);i++){ int j=(sz-i)&(sz-1); num z=(fa[j]*fa[j]-conj(fa[i]*fa[i]))*r; if(i!=j) fa[j]=(fa[i]*fa[i]-conj(fa[j]*fa[j]))*r; fa[i]=z; } fft(fa); vector<long long> res(need); for(int i=0;i<need;i++) res[i]=round(fa[i].x); return res; } }; template<typename T> struct ArbitraryMod{ using dbl=FFT::dbl; using num=FFT::num; vector<T> multiply(vector<T> as,vector<T> bs){ int need=as.size()+bs.size()-1; int sz=1; while(sz<need) sz<<=1; vector<num> fa(sz),fb(sz); for(int i=0;i<(int)as.size();i++) fa[i]=num(as[i].v&((1<<15)-1),as[i].v>>15); for(int i=0;i<(int)bs.size();i++) fb[i]=num(bs[i].v&((1<<15)-1),bs[i].v>>15); fft(fa);fft(fb); dbl ratio=0.25/sz; num r2(0,-1),r3(ratio,0),r4(0,-ratio),r5(0,1); for(int i=0;i<=(sz>>1);i++){ int j=(sz-i)&(sz-1); num a1=(fa[i]+conj(fa[j])); num a2=(fa[i]-conj(fa[j]))*r2; num b1=(fb[i]+conj(fb[j]))*r3; num b2=(fb[i]-conj(fb[j]))*r4; if(i!=j){ num c1=(fa[j]+conj(fa[i])); num c2=(fa[j]-conj(fa[i]))*r2; num d1=(fb[j]+conj(fb[i]))*r3; num d2=(fb[j]-conj(fb[i]))*r4; fa[i]=c1*d1+c2*d2*r5; fb[i]=c1*d2+c2*d1; } fa[j]=a1*b1+a2*b2*r5; fb[j]=a1*b2+a2*b1; } fft(fa);fft(fb); vector<T> cs(need); using ll = long long; for(int i=0;i<need;i++){ ll aa=T(llround(fa[i].x)).v; ll bb=T(llround(fb[i].x)).v; ll cc=T(llround(fa[i].y)).v; cs[i]=T(aa+(bb<<15)+(cc<<30)); } return cs; } }; // construct a charasteristic equation from sequence // return a monic polynomial in O(n^2) template<typename T> vector<T> berlekamp_massey(vector<T> &as){ using Poly = vector<T>; int n=as.size(); Poly bs({-T(1)}),cs({-T(1)}); T y(1); for(int ed=1;ed<=n;ed++){ int l=cs.size(),m=bs.size(); T x(0); for(int i=0;i<l;i++) x+=cs[i]*as[ed-l+i]; bs.emplace_back(0); m++; if(x==T(0)) continue; T freq=x/y; if(m<=l){ for(int i=0;i<m;i++) cs[l-1-i]-=freq*bs[m-1-i]; continue; } auto ts=cs; cs.insert(cs.begin(),m-l,T(0)); for(int i=0;i<m;i++) cs[m-1-i]-=freq*bs[m-1-i]; bs=ts; y=x; } for(auto &c:cs) c/=cs.back(); return cs; } // Find k-th term of linear recurrence template<typename T> struct BostanMori{ using Poly = vector<T>; using Conv = function<Poly(Poly, Poly)>; Conv conv; BostanMori(Conv conv_):conv(conv_){} Poly sub(Poly as,int odd){ Poly bs((as.size()+!odd)/2); for(int i=odd;i<(int)as.size();i+=2) bs[i/2]=as[i]; return bs; } // as: initial values // cs: monic polynomial T build(long long k,Poly as,Poly cs){ reverse(cs.begin(),cs.end()); assert(cs[0]==T(1)); int n=cs.size()-1; as.resize(n,0); Poly bs=conv(as,cs); bs.resize(n); while(k){ Poly ds(cs); for(int i=1;i<(int)ds.size();i+=2) ds[i]=-ds[i]; bs=sub(conv(bs,ds),k&1); cs=sub(conv(cs,ds),0); k>>=1; } return bs[0]; } }; #undef call_from_test signed main(){ cin.tie(0); ios::sync_with_stdio(0); using M = Mint<int>; using Poly = vector<M>; ArbitraryMod<M> arb; auto conv=[&](auto as,auto bs){return arb.multiply(as,bs);}; long long n; cin>>n; n--; int p,c; cin>>p>>c; const int d = 606 * 13; auto calc=[&](int l,vector<int> vs){ int m=vs.size(); vector<Poly> dp(m,Poly(d)); for(int i=0;i<m;i++) dp[i][0]=M(1); for(int t=0;t<l;t++){ for(int i=0;i<m;i++){ for(int j=d-1;j>=0;j--){ dp[i][j]=0; if(i) dp[i][j]+=dp[i-1][j]; if(j>=vs[i]) dp[i][j]+=dp[i][j-vs[i]]; } } } return dp.back(); }; Poly cf({M(1)}); cf=conv(cf,calc(p,vector<int>({2,3,5,7,11,13}))); cf=conv(cf,calc(c,vector<int>({4,6,8,9,10,12}))); cf.resize(d,M(0)); Poly dp(d*3,0),as(d*3,0); dp[0]=M(1); for(int i=0;i<(int)dp.size();i++){ for(int j=0;j<d&&i+j<(int)dp.size();j++) dp[i+j]+=dp[i]*cf[j]; for(int j=1;j<d&&i+j<(int)dp.size();j++) as[i]+=dp[i+j]; } as.resize(d*2); BostanMori<M> seq(conv); cout<<seq.build(n,as,berlekamp_massey(as))<<endl; return 0; }