結果
問題 | No.215 素数サイコロと合成数サイコロ (3-Hard) |
ユーザー | beet |
提出日時 | 2020-11-22 15:52:40 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2,154 ms / 4,000 ms |
コード長 | 13,484 bytes |
コンパイル時間 | 3,460 ms |
コンパイル使用メモリ | 235,492 KB |
実行使用メモリ | 6,816 KB |
最終ジャッジ日時 | 2024-07-23 16:37:09 |
合計ジャッジ時間 | 10,170 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2,059 ms
6,816 KB |
testcase_01 | AC | 2,154 ms
6,816 KB |
ソースコード
#ifndef call_from_test #include <bits/stdc++.h> using namespace std; #endif #define call_from_test #ifndef call_from_test #include <bits/stdc++.h> using namespace std; #endif //BEGIN CUT HERE template<typename M_> class Enumeration{ using M = M_; protected: static vector<M> fact,finv,invs; public: static void init(int n){ n=min<decltype(M::mod)>(n,M::mod-1); int m=fact.size(); if(n<m) return; fact.resize(n+1,1); finv.resize(n+1,1); invs.resize(n+1,1); if(m==0) m=1; for(int i=m;i<=n;i++) fact[i]=fact[i-1]*M(i); finv[n]=M(1)/fact[n]; for(int i=n;i>=m;i--) finv[i-1]=finv[i]*M(i); for(int i=m;i<=n;i++) invs[i]=finv[i]*fact[i-1]; } static M Fact(int n){ init(n); return fact[n]; } static M Finv(int n){ init(n); return finv[n]; } static M Invs(int n){ init(n); return invs[n]; } static M C(int n,int k){ if(n<k or k<0) return M(0); init(n); return fact[n]*finv[n-k]*finv[k]; } static M P(int n,int k){ if(n<k or k<0) return M(0); init(n); return fact[n]*finv[n-k]; } // put n identical balls into k distinct boxes static M H(int n,int k){ if(n<0 or k<0) return M(0); if(!n and !k) return M(1); init(n+k); return C(n+k-1,n); } }; template<typename M> vector<M> Enumeration<M>::fact=vector<M>(); template<typename M> vector<M> Enumeration<M>::finv=vector<M>(); template<typename M> vector<M> Enumeration<M>::invs=vector<M>(); //END CUT HERE #ifndef call_from_test //INSERT ABOVE HERE signed main(){ return 0; } #endif #ifndef call_from_test #include <bits/stdc++.h> using namespace std; #define call_from_test #include "../combinatorics/enumeration.cpp" #undef call_from_test #endif // http://beet-aizu.hatenablog.com/entry/2019/09/27/224701 //BEGIN CUT HERE template<typename M_> struct FormalPowerSeries : Enumeration<M_> { using M = M_; using super = Enumeration<M>; using super::fact; using super::finv; using super::invs; using Poly = vector<M>; using Conv = function<Poly(Poly, Poly)>; Conv conv; FormalPowerSeries(Conv conv):conv(conv){} Poly pre(const Poly &as,int deg){ return Poly(as.begin(),as.begin()+min((int)as.size(),deg)); } Poly add(Poly as,Poly bs){ int sz=max(as.size(),bs.size()); Poly cs(sz,M(0)); for(int i=0;i<(int)as.size();i++) cs[i]+=as[i]; for(int i=0;i<(int)bs.size();i++) cs[i]+=bs[i]; return cs; } Poly sub(Poly as,Poly bs){ int sz=max(as.size(),bs.size()); Poly cs(sz,M(0)); for(int i=0;i<(int)as.size();i++) cs[i]+=as[i]; for(int i=0;i<(int)bs.size();i++) cs[i]-=bs[i]; return cs; } Poly mul(Poly as,Poly bs){ return conv(as,bs); } Poly mul(Poly as,M k){ for(auto &a:as) a*=k; return as; } // F(0) must not be 0 Poly inv(Poly as,int deg); // not zero Poly div(Poly as,Poly bs); // not zero Poly mod(Poly as,Poly bs); // F(0) must be 1 Poly sqrt(Poly as,int deg); Poly diff(Poly as); Poly integral(Poly as); // F(0) must be 1 Poly log(Poly as,int deg); // F(0) must be 0 Poly exp(Poly as,int deg); // not zero Poly pow(Poly as,long long k,int deg); // x <- x + c Poly shift(Poly as,M c); }; //END CUT HERE #ifndef call_from_test //INSERT ABOVE HERE signed main(){ return 0; } #endif #ifndef call_from_test #include <bits/stdc++.h> using namespace std; #endif //BEGIN CUT HERE // 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; } //END CUT HERE #ifndef call_from_test signed main(){ return 0; } #endif #undef call_from_test //BEGIN CUT HERE template<typename M> struct Sequence : FormalPowerSeries<M>{ using FormalPowerSeries<M>::FormalPowerSeries; using Poly = vector<M>; struct Calculator{ const Poly as,cs; Sequence* seq; Calculator(const Poly as_,const Poly cs_,Sequence *seq_): as(as_),cs(cs_),seq(seq_){} M operator()(long long n){ Poly rs({M(1)}),ts({M(0),M(1)}); n--; while(n){ if(n&1) rs=seq->mod(seq->mul(rs,ts),cs); ts=seq->mod(seq->mul(ts,ts),cs); n>>=1; } M res(0); rs.resize(cs.size(),M(0)); for(int i=0;i<(int)cs.size();i++) res+=as[i]*rs[i]; return res; } }; Calculator build(vector<M> as){ return Calculator(as,berlekamp_massey(as),this); } }; //END CUT HERE #ifndef call_from_test //INSERT ABOVE HERE #define call_from_test #ifndef call_from_test #include <bits/stdc++.h> using namespace std; #endif //BEGIN CUT HERE 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;} //END CUT HERE #ifndef call_from_test signed main(){ return 0; } #endif #ifndef call_from_test #include<bits/stdc++.h> using namespace std; #endif //BEGIN CUT HERE 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; } }; //END CUT HERE #ifndef call_from_test signed main(){ return 0; } #endif #ifndef call_from_test #include <bits/stdc++.h> using namespace std; #define call_from_test #include "fastfouriertransform.cpp" #undef call_from_test #endif //BEGIN CUT HERE 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; } }; //END CUT HERE #ifndef call_from_test signed main(){ return 0; } #endif #ifndef call_from_test #include <bits/stdc++.h> using namespace std; #define call_from_test #include "../combinatorics/enumeration.cpp" #include "base.cpp" #undef call_from_test #endif //BEGIN CUT HERE template<typename M> vector<M> FormalPowerSeries<M>::inv(Poly as,int deg){ assert(as[0]!=M(0)); Poly rs({M(1)/as[0]}); for(int i=1;i<deg;i<<=1) rs=pre(sub(add(rs,rs),mul(mul(rs,rs),pre(as,i<<1))),i<<1); return rs; } //END CUT HERE #ifndef call_from_test //INSERT ABOVE HERE signed main(){ return 0; } #endif #ifndef call_from_test #include <bits/stdc++.h> using namespace std; #define call_from_test #include "../combinatorics/enumeration.cpp" #include "base.cpp" #include "inv.cpp" #undef call_from_test #endif //BEGIN CUT HERE template<typename M> vector<M> FormalPowerSeries<M>::div(Poly as,Poly bs){ while(as.back()==M(0)) as.pop_back(); while(bs.back()==M(0)) bs.pop_back(); if(bs.size()>as.size()) return Poly(); reverse(as.begin(),as.end()); reverse(bs.begin(),bs.end()); int need=as.size()-bs.size()+1; Poly ds=pre(mul(as,inv(bs,need)),need); reverse(ds.begin(),ds.end()); return ds; } //END CUT HERE #ifndef call_from_test //INSERT ABOVE HERE signed main(){ return 0; } #endif #ifndef call_from_test #include <bits/stdc++.h> using namespace std; #define call_from_test #include "../combinatorics/enumeration.cpp" #include "base.cpp" #include "inv.cpp" #include "div.cpp" #undef call_from_test #endif //BEGIN CUT HERE template<typename M> vector<M> FormalPowerSeries<M>::mod(Poly as,Poly bs){ if(as==Poly(as.size(),0)) return Poly({0}); as=sub(as,mul(div(as,bs),bs)); if(as==Poly(as.size(),0)) return Poly({0}); while(as.back()==M(0)) as.pop_back(); return as; } //END CUT HERE #ifndef call_from_test //INSERT ABOVE HERE signed main(){ return 0; } #endif #undef call_from_test signed main(){ using M = Mint<int>; using Poly = vector<M>; ArbitraryMod<M> arb; auto conv=[&](auto as,auto bs){return arb.multiply(as,bs);}; 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(); }; long long n; int p,c; cin>>n>>p>>c; 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); Sequence<M> seq(conv); cout<<seq.build(as)(n)<<endl; return 0; } #endif