結果

問題 No.215 素数サイコロと合成数サイコロ (3-Hard)
ユーザー 👑 kmjpkmjp
提出日時 2015-05-29 03:26:39
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 5,197 bytes
コンパイル時間 2,298 ms
コンパイル使用メモリ 174,948 KB
実行使用メモリ 17,412 KB
最終ジャッジ日時 2023-09-20 15:23:49
合計ジャッジ時間 4,923 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------


struct Kitamasa_fast {
	vector<ll> P;
	static ll ext_gcd(ll p,ll q,ll& x, ll& y) { // get px+qy=gcd(p,q)
		if(q==0) return x=1,y=0,p;
		ll g=ext_gcd(q,p%q,y,x);
		y-=p/q*x;
		return g;
	}
	static ll inv(ll p,ll q) { // return (1/p)%q ( p,q is co-prime)
		static map<pair<ll,ll>,ll> MM;
		if(MM.count(make_pair(p,q))) return MM[make_pair(p,q)];
		ll xx,yy,g=ext_gcd(p,q,xx,yy);
		if(xx<0) xx+=q, yy-=p;
		return MM[make_pair(p,q)] = xx;
	}
	static ll modpow(ll a, ll n, ll mo) {
		ll r=1;
		while(n) r=(n%2)?(r*a)%mo:r,a=a*a%mo,n>>=1;
		return r;
	}
	static vector<ll> FMT(vector<ll> v, ll mo, bool rev=false) { // v.size()=2^k, mo = 2^22*k+1, 
		int i,j,k,n=v.size(); // n=2^k;
		ll pf=modpow(3,(mo-1)/n,mo);
		if(rev) pf=modpow(pf,mo-2,mo);
		
		for(i=0,j=1;j<n-1;j++) {
			for(int k=n>>1;k>(i^=k);k>>=1);
			if(i>j) swap(v[i],v[j]);
		}
		/*
		for(int m=2; m<=n; m*=2) {
			ll ba=modpow(pf,n/m,mo);
			ll w=1;
			FOR(i,m/2) {
				for(int j1=i,j2=i+m/2;j2<n;j1+=m,j2+=m) if(v[j2]==0) {
					v[j2]=v[j1];
				}
				else {
					ll t1=v[j1],t2=w*v[j2];
					if(t2>=mo) t2%=mo;
					v[j1]=t1+t2, v[j2]=t1-t2;
					if(v[j1]>=mo) v[j1]-=mo;
					if(v[j2]<0) v[j2]+=mo;
				}
				w*=ba;
				if(w>=mo) w%=mo;
			}
		}
		*/
		if(rev) {
			ll rn=modpow(n,mo-2,mo);
			FOR(i,n) v[i]=v[i]*rn%mo;
		}
		return v;
		
	}
	static ll CRT_garner(vector<pair<ll,ll> > V,ll mo=1000000007) {
		int x,y,N=V.size();
		FOR(y,N) FOR(x,y) {
			ll k=V[y].second-V[x].second;
			if(k<0) k+=V[y].first;
			V[y].second = k*inv(V[x].first,V[y].first)%V[y].first;
		}
		ll ret=0;
		for(x=N-1;x>=0;x--) ret = (ret*V[x].first+V[x].second)%mo;
		return ret;
	}
	
	static vector<ll> convol_sub(vector<ll> a,vector<ll> b, int mo,int t=99999999) { // mo = 2^k+1
		int i,n=1;
		while(n<a.size()+b.size()-1) n*=2;
		
		a.resize(n); b.resize(n);
		a=FMT(a,mo); b=FMT(b,mo);
		FOR(i,n) (a[i]*=b[i])%=mo;
		return FMT(a,mo,true);
	}
	static vector<ll> convol(vector<ll>& a,vector<ll>& b, ll mo,int t=99999999) {
		ll mop[3]={0xA000001,0x1C000001,0x49000001};
		auto x = convol_sub(a,b,mop[0],t);
		auto y = convol_sub(a,b,mop[1],t);
		auto z = convol_sub(a,b,mop[2],t);
		t=min(t,(int)x.size());
		
		vector<ll> R(t);
		vector<pair<ll,ll> > P{{mop[0],0},{mop[1],0},{mop[2],0}};
		for(int i=0;i<t;i++) { // garner
			P[0].second = x[i];
			P[1].second = y[i];
			P[2].second = z[i];
			R[i] = CRT_garner(P,mo);
		}
		return R;
	}
	
	static vector<ll> mult(vector<ll>& v,vector<ll>& v2,vector<ll>& D,vector<ll>& id, ll mo) {
		int k=v.size(),i;
		vector<ll> res(k,0);
		vector<ll> beta=convol(v,v2,mo);
		for(i=k-1;i<beta.size()&&i-(k-1)<k;i++) res[i-(k-1)]=beta[i];
		beta.resize(k-1);
		vector<ll> q=convol(beta,id,mo,k-1);
		q=convol(q,D,mo);
		for(i=k-1;i<q.size()&&i-(k-1)<k;i++) (res[i-(k-1)]+=q[i])%=mo;
		return res;
	}

	vector<ll> getid(vector<ll> D, ll mo) {
		int t=1,k=D.size();
		vector<ll> id(1,1);
		while(t<=k) {
			t=min(2*t,k+1);
			vector<ll> D2=D;
			D2.resize(t);
			vector<ll> cur=convol(D2,id,mo,t);
			cur.resize(t);
			cur[0]+=2;
			id=convol(id,cur,mo,t);
			
			id.resize(t);
		}
		
		return id;
	}
	void calc(ll N, vector<ll> D, ll mo) {
		int n=D.size();
		vector<ll> p(n,0),v(n,0);
		p[0]=v[1]=1;
		reverse(ALL(D));reverse(ALL(p));reverse(ALL(v));
		D.insert(D.begin(),mo-1);
		vector<ll> id=getid(D,mo);
		while(N) {
			if(N%2) p=mult(p,v,D,id,mo);
			v=mult(v,v,D,id,mo);
			N/=2;
		}
		reverse(ALL(p));
		P=p;
	}
	ll calc(ll N, vector<ll> A, vector<ll> D, ll mo) {
		// A_K=A0*D0+A1*D1+A2*D2..+A_K-1*D_K-1 return A_N
		int i;ll ret=0;
		calc(N,D,mo);
		FOR(i,A.size()) ret += A[i]*P[i]%mo;
		return ret%mo;
	}
};

ll N;
int P,C,M;
int A[6]={2,3,5,7,11,13};
int B[6]={4,6,8,9,10,12};

ll dp[2][400][4000];
ll dp2[8000];
ll single[8010];
ll mo=1000000007;

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>P>>C;
	dp[0][0][0]=dp[1][0][0]=1;
	
	FOR(x,6) FOR(y,P) FOR(i,y*13+1) if((dp[0][y+1][i+A[x]] += dp[0][y][i])>=mo) dp[0][y+1][i+A[x]]-=mo;
	FOR(x,6) FOR(y,C) FOR(i,y*12+1) if((dp[1][y+1][i+B[x]] += dp[1][y][i])>=mo) dp[1][y+1][i+B[x]]-=mo;
	FOR(x,300*13+1) if(dp[0][P][x]) FOR(y,300*12+1) (single[x+y] += dp[0][P][x]*dp[1][C][y])%=mo;
	M=P*13+C*12;
	
	ll tot=0;
	if(N<=2*M) {
		dp2[0]=1;
		for(i=1;i<=2*M+2;i++) {
			FOR(x,M+1) if(i>=x) dp2[i] += dp2[i-x]*single[x]%mo;
			dp2[i] %= mo;
		}
		for(ll v=max(0LL,N-M);v<N;v++) {
			for(x=1;x<=M;x++) if(v+x>=N) tot += dp2[v]*single[x]%mo;
			tot %= mo;
		}
	}
	else {
		Kitamasa_fast kf;
		vector<ll> A(M,1),V(M,0);
		FOR(i,M) V[i]=single[M-i];
		tot = kf.calc(N+M-1,A,V,mo);
	}
	
	cout<<tot%mo<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0