結果

問題 No.213 素数サイコロと合成数サイコロ (3-Easy)
ユーザー antaanta
提出日時 2015-05-23 00:13:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 7 ms / 3,000 ms
コード長 7,302 bytes
コンパイル時間 986 ms
コンパイル使用メモリ 96,104 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-06 05:44:14
合計ジャッジ時間 1,396 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,812 KB
testcase_01 AC 7 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

template<int MOD>
struct ModInt {
	static const int Mod = MOD;
	unsigned x;
	ModInt(): x(0) { }
	ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
	ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
	int get() const { return (int)x; }
	
	ModInt &operator+=(ModInt that) { if((x += that.x) >= MOD) x -= MOD; return *this; }
	ModInt &operator-=(ModInt that) { if((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
	ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
	ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
	
	ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
	ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
	ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
	ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
	
	ModInt inverse() const {
		signed a = x, b = MOD, u = 1, v = 0;
		while(b) {
			signed t = a / b;
			a -= t * b; std::swap(a, b);
			u -= t * v; std::swap(u, v);
		}
		if(u < 0) u += Mod;
		ModInt res; res.x = (unsigned)u;
		return res;
	}

	bool operator==(ModInt that) const { return x == that.x; }
	bool operator!=(ModInt that) const { return x != that.x; }
	ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
typedef ModInt<1000000007> mint;

vector<mint> doDP(const int a[6], int n) {
	int maxX = a[5] * n;
	vector<vector<mint> > dp(n+1, vector<mint>(maxX+1));
	dp[0][0] = 1;
	rep(k, 6) {
		int t = a[k];
		for(int i = n; i >= 0; -- i) {
			int maxx = k == 0 ? 0 : i * a[k-1];
			rer(j, 0, maxx) {
				mint x = dp[i][j];
				if(x.get() == 0) continue;
				rer(l, 1, n-i)
					dp[i+l][j + l * t] += x;
			}
		}
	}
	return dp[n];
}

struct Polynomial {
	typedef mint Coef; typedef Coef Val;
	vector<Coef> coef;	//... + coef[2] x^2 + coef[1] x + coef[0]
	Polynomial() {}
	explicit Polynomial(int n): coef(n) {}
	static Polynomial One() {
		Polynomial r(1);
		r.coef[0] = 1;
		return r;
	}
	bool iszero() const { return coef.empty(); }
	int degree1() const { return coef.size(); }	//degree + 1
	int resize(int d) { if(degree1() < d) coef.resize(d); return d; }
	const Coef operator[](int i) const {
		return i >= degree1() ? Coef() : coef[i];
	}
	void canonicalize() {
		int i = coef.size();
		while(i > 0 && coef[i-1] == Coef()) i --;
		coef.resize(i);
	}
	Val evalute(Val x) const {
		int d = degree1();
		Val t = 0, y = 1;
		rep(i, d) {
			t += y * coef[i];
			y *= x;
		}
		return t;
	}
	Polynomial &operator+=(const Polynomial &that) {
		int d = resize(that.degree1());
		for(int i = 0; i < d; i ++) coef[i] += that[i];
		canonicalize();
		return *this;
	}
	Polynomial operator+(const Polynomial &that) const { return Polynomial(*this) += that; }
	Polynomial &operator-=(const Polynomial &that) {
		int d = resize(that.degree1());
		for(int i = 0; i < d; i ++) coef[i] -= that[i];
		canonicalize();
		return *this;
	}
	Polynomial operator-(const Polynomial &that) const { return Polynomial(*this) -= that; }
	Polynomial operator-() const {
		int d = degree1();
		Polynomial res(d);
		for(int i = 0; i < d; i ++) res.coef[i] = - coef[i];
		return res;
	}
	//naive
	Polynomial operator*(const Polynomial &that) const {
		if(iszero() || that.iszero()) return Polynomial();
		int x = degree1(), y = that.degree1(), d = x + y - 1;
		Polynomial res(d);
		rep(i, x) rep(j, y)
			res.coef[i+j] += coef[i] * that.coef[j];
		res.canonicalize();
		return res;
	}
	//long division
	pair<Polynomial, Polynomial> divmod(const Polynomial &that) const {
		int x = degree1() - 1, y = that.degree1() - 1;
		int d = max(0, x - y);
		Polynomial q(d + 1), r = *this;
		for(int i = x; i >= y; i --) {
			Coef t = r.coef[i] / that.coef[y];
			q.coef[i - y] = t;
			assert(t * that.coef[y] == r.coef[i]);
			r.coef[i] = 0;
			for(int j = 0; j < y; j ++)
				r.coef[i - y + j] -= t * that.coef[j];
		}
		q.canonicalize(); r.canonicalize();
		return make_pair(q, r);
	}
	Polynomial operator/(const Polynomial &that) const { return divmod(that).first; }
	Polynomial operator%(const Polynomial &that) const { return divmod(that).second; }
};
Polynomial powmod(Polynomial a, Polynomial m, unsigned long long k) {
	Polynomial r = Polynomial::One();
	while(k) {
		if(k & 1) r = r * a % m;
		a = a * a % m;
		k >>= 1;
	}
	return r;
}

vector<mint> solveSmall(int N, const vector<mint> &ways) {
	vector<mint> dp(N+1);
	dp[0] = 1;
	int X = (int)ways.size() - 1;
	rep(i, N) {
		mint x = dp[i];
		if(x.get() == 0) continue;
		rer(j, 1, X)
			dp[min(N, i + j)] += x * ways[j];
	}
	return dp;
}

int main() {
	long long N;
	int P, C;
	while(~scanf("%lld%d%d", &N, &P, &C)) {
		const int primes[6] = { 2, 3, 5, 7, 11, 13 };
		const int composites[6] = { 4, 6, 8, 9, 10, 12 };
		vector<mint> cntP = doDP(primes, P);
		vector<mint> cntC = doDP(composites, C);
		int X = P * primes[5] + C * composites[5];
		vector<mint> ways(X+1);
		rep(i, cntP.size()) rep(j, cntC.size())
			ways[i + j] += cntP[i] * cntC[j];
//		rer(i, 0, 100)
//			cerr << "ans " << i << " = " << solveSmall(i, ways)[i].get() << endl;
		if(N <= X * 2) {
			vector<mint> v = solveSmall((int)N, ways);
			mint ans = v[(int)N];
			printf("%d\n", ans.get());
			continue;
		}
		vector<mint> firstX = solveSmall(X, ways);
		Polynomial f(X+1);
		f.coef[X] = 1;
		rer(i, 1, X) f.coef[X - i] = -ways[i];
		Polynomial x(2); x.coef[1] = 1;
		/*
		{	Polynomial d = Polynomial::One();
			rep(k, 30) {
				mint sum;
				rer(j, 0, X)
					sum += d[j] * firstX[j];
				cerr << "a " << k << ": " << sum.get() << endl;
				d = d * x;
				d = d % f;
			}
		}*/
		vector<mint> lastX(X);
		/*
		vector<mint> v = solveSmall(N + 2, ways);
		rep(i, X)
			lastX[i] = v[N - X + i];
		*/
//		/*
		Polynomial d = powmod(x, f, N - X);
		rep(i, X) {
			mint sum;
			rer(j, 0, X)
				sum += d[j] * firstX[j];
			lastX[i] = sum;
//			cerr << N - X + i << ": " << sum.get() << endl;
			d = d * x;
			d = d % f;
		}
//		*/
		mint ans;
		rep(i, X) {
			mint x = lastX[i];
			rer(j, X - i, X)
				ans += x * ways[j];
		}
		printf("%d\n", ans.get());
	}
	return 0;
}
0