結果

問題 No.214 素数サイコロと合成数サイコロ (3-Medium)
ユーザー HIR180HIR180
提出日時 2020-04-17 11:32:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 290 ms / 3,000 ms
コード長 9,317 bytes
コンパイル時間 4,001 ms
コンパイル使用メモリ 250,420 KB
実行使用メモリ 9,600 KB
最終ジャッジ日時 2024-10-03 10:11:21
合計ジャッジ時間 5,480 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 290 ms
9,600 KB
testcase_01 AC 227 ms
9,600 KB
testcase_02 AC 247 ms
9,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//Let's join Kaede Takagaki Fan Club !!
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
template<class T>
void dmp(T a){
	rep(i,a.size()) cout << a[i] << " ";
	cout << endl;
}
template<class T>
bool chmax(T&a, T b){
	if(a < b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
bool chmin(T&a, T b){
	if(a > b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const int mod = 1000000007;//998244353
template<class T>
void add(T&a,T b){
	a+=b;
	while(a >= mod) a-=mod;
	while(a < 0) a += mod;
}
template<const int md>
struct ntt{
	inline void add(int &a, int b) { a += b; if(a >= md) a -= md; }
	inline void sub(int &a, int b) { a -= b; if(a < 0) a += md; }
	inline int add2(int a, int b) { a += b; if(a >= md) a -= md; return a;}
	inline int sub2(int a, int b) { a -= b; if(a < 0) a += md; return a;}
	inline int mul(int a, int b) { return (int)((ll)a*b%md); }
	inline int power(int a, long long b) {
		int res = 1;
		while (b > 0) {
			if (b & 1) res = mul(res, a);
	    	a = mul(a, a);
			b >>= 1;
		}
		return res;
	}
	inline int inv(int a) {
		a %= md;
		if (a < 0) a += md;
		int b = md, u = 0, v = 1;
		while (a) {
			int t = b / a;
			b -= t * a; swap(a, b);
			u -= t * v; swap(u, v);
		}
		assert(b == 1);
		if (u < 0) u += md;
		return u;
	}
	
 	int max_base, root;
	vector<int> dw, idw;
	ntt() {
		int tmp = md - 1;
		max_base = 0;
		while (tmp % 2 == 0) {
			tmp /= 2;
			max_base++;
		}
		root = 2;
		while (power(root, (md-1)>>1) == 1) root++;
		dw.resize(max_base); idw.resize(max_base);
		rep(i, max_base){
			sub(dw[i], power(root, (md-1) >> (i+2)));
			idw[i] = inv(dw[i]);
		}
	}
	void fft(vector<int> &a, bool inv) {
		const int n = a.size();
		assert((n & (n - 1)) == 0);
		assert(__builtin_ctz(n) <= max_base);
		if(!inv){
			for(int m=n;m>>=1;){
				int w = 1;
				for(int s=0,k=0; s<n; s += 2*m){
					for(int i=s, j=s+m ; i < s+m; ++i, ++j) {
						int x = a[i], y = mul(a[j], w);
						a[j] = (x>=y?x-y:x+md-y);
						a[i] = (x+y>=md?x+y-md:x+y);
					}
					w = mul(w, dw[__builtin_ctz(++k)]);
				}
			}
		}
		else{
			for(int m=1;m<n;m*=2){
				int w = 1;
				for(int s=0,k=0; s<n; s += 2*m){
					for(int i=s, j=s+m ; i < s+m; ++i, ++j) {
						int x = a[i], y = a[j];
						a[j] = (x>=y?x-y:x+md-y);
						a[j] = mul(a[j], w);
						a[i] = (x+y>=md?x+y-md:x+y);
					}
					w = mul(w, idw[__builtin_ctz(++k)]);
				}
			}
			const int inv_sz = this->inv(n);
			for(auto &&e:a) e = mul(e, inv_sz);
		}
	}
	vector<int> multiply(vector<int> a, vector<int> b, int eq = 0) {
		int need = a.size() + b.size() - 1;
		int nbase = 0;
		while ((1 << nbase) < need) nbase++;
		int sz = 1 << nbase;
		a.resize(sz);
		b.resize(sz);
		fft(a, 0);
		if (eq) b = a; else fft(b, 0);
		rep(i, sz) a[i] = mul(a[i], b[i]);
		fft(a, 1);
		a.resize(need);
		return a;
	}
	vector<int> square(vector<int> a) {
		return multiply(a, a, 1);
	}
};
//fast_ntt.cpp
/*
167772161; //= 2^25 * 5 + 1
469762049; //= 2^26 * 7 + 1
754974721; //= 2^24 * 45 + 1
1045430273; //= 2^20 * 997 + 1
1051721729; //= 2^20 * 1003 + 1
1053818881; //= 2^20 * 1005 + 1
*/

template<const int md>
vector<int> anyntt(vector<int>&a, vector<int>&b, int eq = 0) {
	//今回は2^20以下だからこっちの方が速い (らしい) (:maroon_kansha:)
	const int m1 = 167772161, m2 = 469762049, m3 = 754974721;
	ntt<m1>n1;
	ntt<m2>n2;
	ntt<m3>n3;
	ntt<md>nn;
	auto a1  = n1.multiply(a, b, eq);
	auto a2  = n2.multiply(a, b, eq);
	auto a3  = n3.multiply(a, b, eq);
	const int n = a1.size();
	vector<int>ret(n);
	vector<ll>m; m = {m1, m2, m3};
	vector<ll>r; r = {1, n2.inv(m1), n3.inv(n3.mul(m1, m2))};
	int mm = nn.mul(m1, m2);
	rep(i, n){
		n2.add(a2[i], n2.sub2(m2, a1[i]));
		int v1 = n2.mul(a2[i], r[1]);
		n3.add(a3[i], n3.sub2(m3, n3.add2(a1[i], n3.mul(m1, v1))));
		int v2 = n3.mul(a3[i], r[2]);
		nn.add(a1[i], nn.add2(nn.mul(m1, v1), nn.mul(mm, v2)));
		ret[i] = a1[i];
	}
	return ret;
}
ll modpow(ll x,ll n){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}
typedef vector<int> vi;
vi shrink(vi a){
	while(a.size() && a.back() == 0) a.pop_back();
	return a;
}
vi mul_int(vi a, int M){
	for(auto &b: a) b = (int)((ll)(b) * M) % mod;
	return a;
}
vi mul(vi a, vi b, int eq = 0){
	return anyntt<mod>(a, b, eq); //kaede.multiply(a, b, eq);
}
vi add(vi a, vi b,int M=-1){
    if(a.size() < b.size()) swap(a,b);
    for(int i=0;i<b.size();i++){
        a[i]+=b[i];
        if(a[i] < 0) a[i] += mod;
        if(a[i] >= mod) a[i] -= mod;
    }
    if(M >= 0 && a.size() > M) a.resize(M);
    return a;
}
vi sub(vi a, vi b,int M=-1){
    if(a.size() < b.size()) a.resize(b.size(), 0);
    for(int i=0;i<b.size();i++){
        a[i]-=b[i];
        if(a[i] < 0) a[i] += mod;
        if(a[i] >= mod) a[i] -= mod;
    }
    if(M >= 0 && a.size() > M) a.resize(M);
    return a;
}
vi lw(vi a, int x){
	if(a.size() > x) a.resize(x);
	return a;
}
/*vi inv(vi a,int M){
	if(a.empty() || a[0] == 0) return vi();
	vi ret(1);
	ret[0] = modpow(a[0],mod-2);
	int cur = 1;
	int nxt = 1;
	while(cur < M){
		auto g = lw(ret, cur);
		auto f = lw(a, cur*2);
		f.resize(cur << 1); g.resize(cur << 1);
		kaede.fft(f, 0);
		kaede.fft(g, 0);
		rep(i, f.size()) f[i] = kaede.mul(f[i], g[i]);
		kaede.fft(f, 1);
		f.erase(begin(f), begin(f) + cur);
        f.resize(cur << 1), kaede.fft(f, 0);
        rep(i, f.size()) {
            f[i] = mod - kaede.mul(f[i], g[i]);
        }
        kaede.fft(f, 1);
        ret.insert(end(ret), begin(f), begin(f)+ cur);
		nxt++;
		cur <<= 1;
	}
	assert(ret.size() >= M);
	ret.resize(M);
	return ret;
}*/
//slow 
vi inv(vi a,int M){
	if(a.empty() || a[0] == 0) return vi();
	vi ret(M);
	ret[0] = modpow(a[0],mod-2);
	int cur = 1;
	int nxt = 1;
	while(cur < M){
		auto at = lw(ret, cur);
		ret = sub(add(at, at), mul(mul(at, at, 1), lw(a, cur*2)));
		ret.resize(cur << 1);
		nxt++;
		cur <<= 1;
	}
	assert(ret.size() >= M);
	ret.resize(M);
	return ret;
}
 
vi modpow(vi a, ll n, vi b){
	vi rb = b;
	reverse(all(rb)); rb = inv(rb, rb.size());
	
	auto get_mod = [&](vi v){
		vi dv, u = v;
		if(v.size() < b.size()) dv = {};
		else{
			int sz = v.size() - b.size() + 1;
			vi y = lw(rb, sz);
			reverse(all(v)); v = lw(v, sz);
			dv = mul(v, y);
			dv.resize(sz);
			reverse(all(dv));
		}
		u = sub(u, mul(dv, b));
		return shrink(u);
	};
	if(a.size() >= b.size()){
		a = get_mod(a);
	}
	assert(a.size() < b.size());
	vi ret = {1};
	while(n){
		if(n & 1){
			ret = mul(ret, a);
			ret = get_mod(ret);
		}
		n >>= 1;
		a = mul(a, a, 1);
		a = get_mod(a);
	}
	return ret;
}

template<class T>
vector<T>BerlekampMassey(vector<T>x){
	vector<T>ls,cur;
	T lf,ld;
	rep(i,x.size()){
		T t = 0;
		for(int j=0;j<cur.size();j++){
			t = (t+(ll)x[i-j-1]*cur[j])%mod;
		}
		if( ((t-x[i])%mod+mod)%mod == 0 ) continue;
		if(!cur.size()){
			cur.resize(i+1); lf = i; ld = (t-x[i])%mod;
			continue;
		}
		T k = (ll) -(x[i]-t)*modpow(ld,mod-2)%mod;
		vector<T>c(i-lf-1);
		c.pb(k);
		rep(j,ls.size()) c.pb((ll)-ls[j]*k%mod);
		if(c.size() < cur.size()) c.resize(cur.size());
		rep(j,cur.size()){
			c[j]=(c[j]+cur[j]);
			while(c[j] < 0) c[j] += mod;
			while(c[j] >= mod) c[j] -= mod;
		}
		if(i-lf+(int)(ls.size()) >= (int)(cur.size())){
			ls = cur, lf = i, ld = (t-x[i])%mod;
		}
		cur = c;
	}
	rep(i,cur.size()) cur[i] = (cur[i]%mod+mod)%mod;
	return cur;
}

inline int mul(int a, int b) {return ((ll)a * b % mod); }
typedef vector<int> vi;
ll n;
int p, c, dp[2][305][3905];
vi f;
int v[2][6] = {{2, 3, 5, 7, 11, 13}, {4, 6, 8, 9, 10, 12}};

void make(int id){
	dp[id][0][0] = 1;
	for(int x=0;x<6;x++){
		rep(i, 300){
			rep(j, 3900){
				if(dp[id][i][j] == 0) continue;
				add(dp[id][i+1][j+v[id][x]], dp[id][i][j]);
			}
		}
	}
}
int main(){
	scanf("%lld%d%d",&n, &p, &c);
	make(0);
	make(1);
	f.resize(p*13 + c*12 + 1);
	
	rep(i, 3905) rep(j, 3905){
		if(dp[0][p][i] == 0 || dp[1][c][j] == 0) continue;
		add(f[i+j], mul(dp[0][p][i], dp[1][c][j]));
	}
	for(auto &a:f) a = (mod-a)%mod;
	f[0] = 1;
	int A = p*13 + c*12;
	auto ans = inv(f, 3*A);
	auto g = f;
	reverse(all(g));
	int ret = 0;

	if(n <= 3*A){
		for(int j=max(0, (int)n-A);j < n;j++){
		    int coef = 0;
			for(int k=1;k<=A;k++){
				if(j+k >= n){
				    add(coef, mod-f[k]);
				}
				add(ret, mul(ans[j], coef));
			}
		}
		printf("%d\n", ret);
	}
	else{
		auto v = modpow({0, 1}, n-A, g);
		for(ll j=n-A;j<n;j++){
			int M = 0;
			rep(x,v.size()) add(M, mul(v[x], ans[x+(j-(n-A))]));
			int coef = 0;
			for(int k=1;k<=A;k++){
				if(j+k >= n){
					add(coef, mod-f[k]);
				}
			}
			add(ret, mul(M, coef));
		}
		printf("%d\n", ret);
	}
}
0