結果
問題 | No.215 素数サイコロと合成数サイコロ (3-Hard) |
ユーザー | btk |
提出日時 | 2015-05-23 10:30:50 |
言語 | C++11 (gcc 11.4.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 4,223 bytes |
コンパイル時間 | 889 ms |
コンパイル使用メモリ | 86,760 KB |
実行使用メモリ | 110,464 KB |
最終ジャッジ日時 | 2024-07-06 06:06:09 |
合計ジャッジ時間 | 5,903 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ソースコード
#include<iostream> #include<vector> #include<list> #include<algorithm> #include<utility> #include<complex> #include<functional> using namespace std; #define reE(i,a,b) for(auto (i)=(a);(i)<=(b);(i)++) #define rE(i,b) reE(i,0,b) #define reT(i,a,b) for(auto (i)=(a);(i)<(b);(i)++) #define rT(i,b) reT(i,0,b) #define rep(i,a,b) reE(i,a,b); #define rev(i,a,b) for(auto (i)=(b)-1;(i)>=(a);(i)--) #define itr(i,b) for(auto (i)=(b).begin();(i)!=(b).end();++(i)) #define LL long long #define LL long long #define BUFFSIZE (long long)7500 #define MAX_LOGN 60 template <class T> class Mr{ public: vector<T> first; vector<T> C; vector<vector<T>> bin; list<int> usefulL; list<int> usefulR; T zero, one; int M; //n(1,,,2M)をn(1,,,M)に修正、O(M^2) void form(vector<T> &n){ rev(i, M + 1, 2 * M + 1) if(n[i].val){ reE(j, 1, M)n[i - j] = (n[i - j] + (C[M - j] * n[i])); n[i] = zero; } } //lとrを足し合わせる、O(M^2) inline void add(vector<T> &l, vector<T> &r, vector<T> &ans){ usefulL.clear(); usefulR.clear(); reE(i,1,M){ if(l[i].val)usefulL.push_back(i); if(r[i].val)usefulR.push_back(i); } reE(i, 1, 2 * M)ans[i] = zero; for(auto &i:usefulL)for(auto &j:usefulR)ans[i + j] = (ans[i + j] + (l[i] * r[j])); form(ans); } //初期化、O(M*MAX_LOGN) Mr(const vector<T>& f, const vector<T>& c, int m, T e1, T e0){ M = m; first.reserve(M + 1); C.reserve(M); zero = e0, one = e1; first.push_back(zero); rT(i, M){ first.push_back(f[i]); C.push_back(c[i]); } bin.resize(MAX_LOGN); rT(i, MAX_LOGN)bin[i].resize(2 * M + 1); rE(i, 2 * M)bin[0][i] = zero; bin[0][1] = one; reT(i, 1, MAX_LOGN){ add(bin[i - 1], bin[i - 1], bin[i]); } } //N項目の計算、戻り値がTの形であることに注意、O(M^2*logN) vector<T> calc(LL n){ n--; vector<T> tmp, result = bin[0]; for (int b = 0; n; b++, n >>= 1) if (1 & n){ tmp = result; add(tmp, bin[b], result); } //reE(i, 1, M)ans = ans + (result[i] * first[i]); return result; } }; //テンプレート、デフォルトコンストラクタのオーバーロードを忘れない #define MOD 1000000007 struct X{ LL val; X(LL v){ val = v; } X(){ val = 0; } LL operator=(const X &another){ return val = another.val; } LL operator*(const X &another)const{ return (val*another.val) % MOD; } LL operator+(const X &another)const{ return (val + another.val) % MOD; } }; LL comb[BUFFSIZE+1]; LL temp2[3][BUFFSIZE+1][6] = { { {1} } }; LL temp1[301][BUFFSIZE+1][6]; LL dp[2*BUFFSIZE+1] = { 1 }; LL sumc[BUFFSIZE+1] = { 0 }; int sosuu[] = { 2, 3, 5, 7, 11, 13 }, gousei[] = { 4, 6, 8, 9, 10, 12 }; int p, c; vector<X> C(BUFFSIZE+1); int main(void){ LL N; cin >> N >> p >> c; //combの計算 for(int n=0;n<p;n++) for (int j = 0; j < 6; j++) for (int i = sosuu[j]; i <= BUFFSIZE; i++) for (int k = 0; k <= j; k++) temp2[n + 1][i][j] = (temp2[n + 1][i][j] + temp2[n][i - sosuu[j]][k]) % MOD; for (int i = 0; i <= BUFFSIZE; i++) for (int j = 0; j < 6; j++) temp1[0][i][0] = (temp1[0][i][0] + temp2[p][i][j]) % MOD; for(int n=0;n<c;n++) for (int j = 0; j < 6; j++) for (int i = gousei[j]; i <= BUFFSIZE; i++) for (int k = 0; k <= j; k++) temp1[n + 1][i][j] = (temp1[n + 1][i][j] + temp1[n][i - gousei[j]][k]) % MOD; for (int i = 0; i <= BUFFSIZE; i++) for (int j = 0; j < 6; j++) comb[i] = (comb[i] + temp1[c][i][j]) % MOD; //漸化式の初項部分 for (LL i = 0; i <= 2*BUFFSIZE; i++){ for (LL j = 1; j <= min(i,BUFFSIZE); j++){ dp[i] = (dp[i] + comb[j] * dp[i - j]) % MOD; } } //漸化式の係数部分 for (int i = 1; i <= BUFFSIZE; i++){ C[BUFFSIZE - i] = X(comb[i]); } for (int i = BUFFSIZE-1; i >= 0; i--){ sumc[i] = (sumc[i + 1] + comb[i]) % MOD; } LL res = 0; if (N < BUFFSIZE+100){ for (LL i = 0; i < min(N,BUFFSIZE); i++) res = (res + dp[i] * sumc[min(N,BUFFSIZE) - i])%MOD; } else{ Mr<X> mr(C, C, BUFFSIZE, X(1), X(0)); auto cc = mr.calc(N + 1 - BUFFSIZE); for (LL i = 0; i < BUFFSIZE; i++) if(sumc[BUFFSIZE-i]){ LL x = 0; for (int j = 0; j < BUFFSIZE; j++) x = (x + dp[i + j] * cc[j + 1].val) % MOD; res = (res + x*sumc[BUFFSIZE - i])%MOD; } } cout << res << endl; return(0); }