結果
問題 | No.3046 yukicoderの過去問 |
ユーザー | beet |
提出日時 | 2020-02-15 01:24:25 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 151 ms / 2,000 ms |
コード長 | 13,138 bytes |
コンパイル時間 | 2,635 ms |
コンパイル使用メモリ | 222,656 KB |
実行使用メモリ | 26,828 KB |
最終ジャッジ日時 | 2024-10-06 13:35:45 |
合計ジャッジ時間 | 4,223 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
6,816 KB |
testcase_01 | AC | 3 ms
6,820 KB |
testcase_02 | AC | 3 ms
6,816 KB |
testcase_03 | AC | 140 ms
26,564 KB |
testcase_04 | AC | 3 ms
6,816 KB |
testcase_05 | AC | 138 ms
26,432 KB |
testcase_06 | AC | 149 ms
26,828 KB |
testcase_07 | AC | 147 ms
26,760 KB |
testcase_08 | AC | 151 ms
26,824 KB |
ソースコード
#define PROBLEM "https://yukicoder.me/problems/2744" #include<bits/stdc++.h> using namespace std; #define call_from_test #ifndef call_from_test #include<bits/stdc++.h> using namespace std; #endif //BEGIN CUT HERE struct FastIO{ FastIO(){ cin.tie(0); ios::sync_with_stdio(0); } }fastio_beet; //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 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 //INSERT ABOVE HERE signed ABC127_E(){ cin.tie(0); ios::sync_with_stdio(0); int h,w,k; cin>>h>>w>>k; using M = Mint<int>; M ans{0}; for(int d=1;d<h;d++) ans+=M(d)*M(h-d)*M(w)*M(w); for(int d=1;d<w;d++) ans+=M(d)*M(w-d)*M(h)*M(h); ans*=M::comb(h*w-2,k-2); cout<<ans<<endl; return 0; } /* verified on 2019/06/12 https://atcoder.jp/contests/abc127/tasks/abc127_e */ signed main(){ //ABC127_E(); 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; #endif //BEGIN CUT HERE template<typename T> struct FormalPowerSeries{ using Poly = vector<T>; 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,T(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,T(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,T k){ for(auto &a:as) a*=k; return as; } // F(0) must not be 0 Poly inv(Poly as,int deg){ assert(as[0]!=T(0)); Poly rs({T(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; } // not zero Poly div(Poly as,Poly bs){ while(as.back()==T(0)) as.pop_back(); while(bs.back()==T(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; } Poly 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()==T(0)) as.pop_back(); return as; } // F(0) must be 1 Poly sqrt(Poly as,int deg){ assert(as[0]==T(1)); T inv2=T(1)/T(2); Poly ss({T(1)}); for(int i=1;i<deg;i<<=1){ ss=pre(add(ss,mul(pre(as,i<<1),inv(ss,i<<1))),i<<1); for(T &x:ss) x*=inv2; } return ss; } Poly diff(Poly as){ int n=as.size(); Poly rs(n-1); for(int i=1;i<n;i++) rs[i-1]=as[i]*T(i); return rs; } Poly integral(Poly as){ int n=as.size(); Poly rs(n+1); rs[0]=T(0); for(int i=0;i<n;i++) rs[i+1]=as[i]/T(i+1); return rs; } // F(0) must be 1 Poly log(Poly as,int deg){ return pre(integral(mul(diff(as),inv(as,deg))),deg); } // F(0) must be 0 Poly exp(Poly as,int deg){ Poly f({T(1)}); as[0]+=T(1); for(int i=1;i<deg;i<<=1) f=pre(mul(f,sub(pre(as,i<<1),log(f,i<<1))),i<<1); return f; } Poly partition(int n){ Poly rs(n+1); rs[0]=T(1); for(int k=1;k<=n;k++){ if(1LL*k*(3*k+1)/2<=n) rs[k*(3*k+1)/2]+=T(k%2?-1LL:1LL); if(1LL*k*(3*k-1)/2<=n) rs[k*(3*k-1)/2]+=T(k%2?-1LL:1LL); } return inv(rs,n+1); } Poly bernoulli(int n){ Poly rs(n+1,1); for(int i=1;i<=n;i++) rs[i]=rs[i-1]/T(i+1); rs=inv(rs,n+1); T tmp(1); for(int i=1;i<=n;i++){ tmp*=T(i); rs[i]*=tmp; } return rs; } }; //END CUT HERE #ifndef call_from_test #define call_from_test #include "../mod/mint.cpp" #include "../convolution/numbertheoretictransform.cpp" #include "../mod/sqrt.cpp" #include "../tools/fastio.cpp" #undef call_from_test //INSERT ABOVE HERE signed HAPPYQUERY_E(){ int n,m,q; cin>>n>>m>>q; vector<int> ls(q),rs(q); for(int i=0;i<q;i++) cin>>ls[i]>>rs[i],ls[i]--; vector<int> as(n); for(int i=0;i<n;i++) cin>>as[i]; if(as==vector<int>(n,0)){ for(int i=0;i<m;i++){ if(i) cout<<" "; cout<<0; } cout<<endl; return 0; } vector<int> cs(n-m+1,0); for(int l:ls) cs[l]++; NTT<0> ntt; using M = NTT<0>::M; auto conv=[&](auto as,auto bs){return ntt.multiply(as,bs);}; FormalPowerSeries<M> FPS(conv); vector<M> ps(as.size()),qs(cs.size()); for(int i=0;i<(int)ps.size();i++) ps[i]=M(as[i]); for(int i=0;i<(int)qs.size();i++) qs[i]=M(cs[i]); auto bs=FPS.div(ps,qs); for(int i=0;i<m;i++){ if(i) cout<<" "; cout<<bs[i]; } cout<<endl; return 0; } /* verified on 2019/09/17 https://www.hackerrank.com/contests/happy-query-contest/challenges/array-restoring */ signed CFR250_E(){ int n,m; cin>>n>>m; vector<int> cs(n); for(int i=0;i<n;i++) cin>>cs[i]; NTT<2> ntt; using M = NTT<2>::M; auto conv=[&](auto as,auto bs){return ntt.multiply(as,bs);}; FormalPowerSeries<M> FPS(conv); const int deg=1<<18; vector<M> as(deg,0); as[0]=M(1); for(int c:cs) as[c]-=M(4); auto bs=FPS.sqrt(as,deg); bs[0]+=M(1); vector<M> vs({2}); auto ans=FPS.mul(vs,FPS.inv(bs,deg)); for(int i=1;i<=m;i++) cout<<ans[i]<<"\n"; cout<<flush; return 0; } /* verified on 2019/09/17 https://codeforces.com/contest/438/problem/E */ signed LOJ_150(){ NTT<2> ntt; using M = NTT<2>::M; auto conv=[&](auto as,auto bs){return ntt.multiply(as,bs);}; FormalPowerSeries<M> FPS(conv); int n,k; cin>>n>>k; vector<M> F(n+1); for(int i=0;i<=n;i++) cin>>F[i].v; const int deg = 1<<17; auto as=FPS.log(FPS.mul(F,F[0].inv()),deg); auto bs=FPS.exp(FPS.mul(as,M((ntt.md-1)/2)),deg); M s(mod_sqrt(F[0].v,ntt.md)[0]); auto cs=FPS.integral(FPS.mul(bs,s.inv())); auto ds=FPS.exp(cs,deg); auto es=FPS.sub(F,ds); es[0]+=M(2); es[0]-=F[0]; auto fs=FPS.log(es,deg); fs[0]+=M(1); auto gs=FPS.log(fs,deg); auto hs=FPS.mul(gs,M(k)); auto is=FPS.exp(hs,deg); auto G=FPS.diff(is); for(int i=0;i<n;i++){ if(i) cout<<" "; cout<<G[i]; } cout<<endl; return 0; } /* verified on 2019/09/17 https://loj.ac/problem/150 */ signed CODECHEF_PSUM(){ NTT<2> ntt; using M = NTT<2>::M; auto conv=[&](auto as,auto bs){return ntt.multiply(as,bs);}; FormalPowerSeries<M> FPS(conv); int n,s,k; cin>>n>>s>>k; vector<int> cs(n),vs(n); for(int i=0;i<n;i++) cin>>cs[i]>>vs[i]; const int deg = 1<<11; vector< vector<M> > dp(s+1,vector<M>(deg,0)); dp[0][0]=M(1); auto nx=dp; for(int i=0;i<n;i++){ auto ps=FPS.exp(vector<M>({M(0),M(vs[i])}),deg); for(int j=0;j+cs[i]<=s;j++){ auto rs=FPS.mul(ps,dp[j]); for(int l=0;l<deg;l++) nx[j+cs[i]][l]+=rs[l]; } dp=nx; } M ans{0}; for(int i=1;i<=s;i++) ans+=dp[i][k]; for(int i=1;i<=k;i++) ans*=M(i); cout<<ans<<endl; return 0; } /* verified on 2019/09/24 https://www.codechef.com/problems/PSUM */ signed main(){ //HAPPYQUERY_E(); //CFR250_E(); //LOJ_150(); //CODECHEF_PSUM(); return 0; } #endif #undef call_from_test signed main(){ int k,n; cin>>k>>n; vector<int> xs(n); for(int i=0;i<n;i++) cin>>xs[i]; using M = Mint<int>; ArbitraryMod<M> arb; auto conv=[&](auto as,auto bs){return arb.multiply(as,bs);}; FormalPowerSeries<M> FPS(conv); const int sz=1<<17; vector<M> bs(sz,M(0)); bs[0]=1; for(int x:xs) bs[x]-=M(1); cout<<FPS.inv(bs,k+1)[k]<<endl; return 0; }