結果
問題 | No.3046 yukicoderの過去問 |
ユーザー | ei1333333 |
提出日時 | 2019-04-01 21:26:04 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,333 ms / 2,000 ms |
コード長 | 4,513 bytes |
コンパイル時間 | 2,408 ms |
コンパイル使用メモリ | 214,184 KB |
実行使用メモリ | 54,192 KB |
最終ジャッジ日時 | 2024-11-26 11:01:48 |
合計ジャッジ時間 | 9,447 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,820 KB |
testcase_02 | AC | 2 ms
6,820 KB |
testcase_03 | AC | 1,287 ms
53,800 KB |
testcase_04 | AC | 2 ms
6,820 KB |
testcase_05 | AC | 1,286 ms
53,796 KB |
testcase_06 | AC | 1,317 ms
54,192 KB |
testcase_07 | AC | 1,306 ms
54,000 KB |
testcase_08 | AC | 1,333 ms
54,056 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; template< int mod, int primitiveroot > struct NumberTheoreticTransform { vector< vector< int > > rts, rrts; void ensure_base(int N) { if(rts.size() >= N) return; rts.resize(N), rrts.resize(N); for(int i = 1; i < N; i <<= 1) { if(rts[i].size()) continue; int w = mod_pow(primitiveroot, (mod - 1) / (i * 2)); int rw = inverse(w); rts[i].resize(i), rrts[i].resize(i); rts[i][0] = 1, rrts[i][0] = 1; for(int k = 1; k < i; k++) { rts[i][k] = mul(rts[i][k - 1], w); rrts[i][k] = mul(rrts[i][k - 1], rw); } } } inline int mod_pow(int x, int n) { int ret = 1; while(n > 0) { if(n & 1) ret = mul(ret, x); x = mul(x, x); n >>= 1; } return ret; } inline int inverse(int x) { return mod_pow(x, mod - 2); } inline int add(unsigned x, int y) { x += y; if(x >= mod) x -= mod; return x; } inline int mul(int a, int b) { return int(1uLL * a * b % mod); } void DiscreteFourierTransform(vector< int > &F, bool rev) { const int N = (int) F.size(); ensure_base(N); for(int i = 0, j = 1; j + 1 < N; j++) { for(int k = N >> 1; k > (i ^= k); k >>= 1); if(i > j) swap(F[i], F[j]); } for(int i = 1; i < N; i <<= 1) { for(int j = 0; j < N; j += i * 2) { for(int k = 0; k < i; k++) { int s = F[j + k], t = mul(F[j + k + i], rev ? rrts[i][k] : rts[i][k]); F[j + k] = add(s, t), F[j + k + i] = add(s, mod - t); } } } if(rev) { int temp = inverse(N); for(int i = 0; i < N; i++) F[i] = mul(F[i], temp); } } vector< int > Multiply(const vector< int > &A, const vector< int > &B) { int sz = 1; while(sz < A.size() + B.size() - 1) sz <<= 1; vector< int > F(sz), G(sz); for(int i = 0; i < A.size(); i++) F[i] = A[i]; for(int i = 0; i < B.size(); i++) G[i] = B[i]; DiscreteFourierTransform(F, false); DiscreteFourierTransform(G, false); for(int i = 0; i < sz; i++) F[i] = mul(F[i], G[i]); DiscreteFourierTransform(F, true); F.resize(A.size() + B.size() - 1); return F; } }; // http://math314.hateblo.jp/entry/2015/05/07/014908 inline int add(unsigned x, int y, int mod) { x += y; if(x >= mod) x -= mod; return (x); } inline int mul(int a, int b, int mod) { unsigned long long x = (long long) a * b; unsigned xh = (unsigned) (x >> 32), xl = (unsigned) x, d, m; asm("divl %4; \n\t" : "=a" (d), "=d" (m) : "d" (xh), "a" (xl), "r" (mod)); return (m); } inline int mod_pow(int x, int n, int mod) { int ret = 1; while(n > 0) { if(n & 1) ret = mul(ret, x, mod); x = mul(x, x, mod); n >>= 1; } return ret; } inline int inverse(int x, int mod) { return (mod_pow(x, mod - 2, mod)); } const int mod1 = 167772161; const int mod2 = 469762049; const int mod3 = 1224736769; NumberTheoreticTransform< 167772161, 3 > ntt1; NumberTheoreticTransform< 469762049, 3 > ntt2; NumberTheoreticTransform< 1224736769, 3 > ntt3; vector< int > AnyModNTTMultiply(vector< int > &a, vector< int > &b, int mod) { for(auto &x : a) x %= mod; for(auto &x : b) x %= mod; auto x = ntt1.Multiply(a, b); auto y = ntt2.Multiply(a, b); auto z = ntt3.Multiply(a, b); const int m1 = mod1, m2 = mod2, m3 = mod3; const int m1_inv_m2 = inverse(m1, m2); const int m12_inv_m3 = inverse(mul(m1, m2, m3), m3); const int m12_mod = mul(m1, m2, mod); vector< int > ret(x.size()); for(int i = 0; i < x.size(); i++) { int v1 = mul(add(y[i], m2 - x[i], m2), m1_inv_m2, m2); int v2 = mul(add(z[i], m3 - add(x[i], mul(m1, v1, m3), m3), m3), m12_inv_m3, m3); ret[i] = add(x[i], add(mul(m1, v1, mod), mul(m12_mod, v2, mod), mod), mod); } return ret; } const int mod = 1e9 + 7; int N, K; vector< int > A; int dp[200002]; void rec(int left, int right) { if(left + 1 >= right) return; int mid = (left + right) >> 1; rec(mid, right); vector< int > x(right - mid), y(right - left); for(int i = mid; i < right; i++) { x[i - mid] = dp[i]; } for(auto &p : A) { if(p < y.size()) y[p] = 1; else break; } reverse(begin(y), end(y)); auto z = AnyModNTTMultiply(x, y, mod); for(int i = left; i < mid; i++) { dp[i] += z[right - left - 1 - mid + i]; if(dp[i] >= mod) dp[i] -= mod; } rec(left, mid); } int main() { cin >> K >> N; A.resize(N); for(auto &p : A) cin >> p; dp[K] = 1; rec(0, K + 1); cout << dp[0] << endl; }