結果
問題 | No.1086 桁和の桁和2 |
ユーザー | snrnsidy |
提出日時 | 2021-10-05 01:16:41 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2,053 ms / 3,000 ms |
コード長 | 5,228 bytes |
コンパイル時間 | 2,812 ms |
コンパイル使用メモリ | 222,192 KB |
実行使用メモリ | 11,348 KB |
最終ジャッジ日時 | 2024-07-23 01:52:14 |
合計ジャッジ時間 | 31,619 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
6,816 KB |
testcase_01 | AC | 4 ms
6,944 KB |
testcase_02 | AC | 4 ms
7,048 KB |
testcase_03 | AC | 3 ms
6,944 KB |
testcase_04 | AC | 3 ms
7,028 KB |
testcase_05 | AC | 37 ms
8,120 KB |
testcase_06 | AC | 4 ms
6,944 KB |
testcase_07 | AC | 3 ms
6,944 KB |
testcase_08 | AC | 3 ms
6,940 KB |
testcase_09 | AC | 4 ms
7,504 KB |
testcase_10 | AC | 24 ms
6,952 KB |
testcase_11 | AC | 9 ms
7,584 KB |
testcase_12 | AC | 5 ms
6,944 KB |
testcase_13 | AC | 30 ms
7,748 KB |
testcase_14 | AC | 21 ms
7,356 KB |
testcase_15 | AC | 247 ms
7,120 KB |
testcase_16 | AC | 1,426 ms
9,860 KB |
testcase_17 | AC | 121 ms
6,948 KB |
testcase_18 | AC | 1,974 ms
11,056 KB |
testcase_19 | AC | 1,630 ms
10,304 KB |
testcase_20 | AC | 201 ms
8,272 KB |
testcase_21 | AC | 1,392 ms
9,944 KB |
testcase_22 | AC | 1,966 ms
11,172 KB |
testcase_23 | AC | 1,207 ms
9,256 KB |
testcase_24 | AC | 834 ms
8,528 KB |
testcase_25 | AC | 1,664 ms
10,444 KB |
testcase_26 | AC | 1,367 ms
9,820 KB |
testcase_27 | AC | 985 ms
9,076 KB |
testcase_28 | AC | 819 ms
8,360 KB |
testcase_29 | AC | 921 ms
9,264 KB |
testcase_30 | AC | 2,041 ms
11,232 KB |
testcase_31 | AC | 2,028 ms
11,100 KB |
testcase_32 | AC | 2,037 ms
11,228 KB |
testcase_33 | AC | 2,042 ms
11,216 KB |
testcase_34 | AC | 2,053 ms
11,348 KB |
testcase_35 | AC | 38 ms
7,944 KB |
コンパイルメッセージ
main.cpp: In function 'std::vector<int> berlekamp_massey(std::vector<int>)': main.cpp:32:44: warning: 'ld' may be used uninitialized [-Wmaybe-uninitialized] 32 | lint k = -(x[i] - t) * ipow(ld, mod - 2) % mod; | ~~~~^~~~~~~~~~~~~ main.cpp:19:17: note: 'ld' was declared here 19 | int lf, ld; | ^~ main.cpp:33:32: warning: 'lf' may be used uninitialized [-Wmaybe-uninitialized] 33 | vector<int> c(i-lf-1); | ~^~~ main.cpp:19:13: note: 'lf' was declared here 19 | int lf, ld; | ^~
ソースコード
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; const long long int MOD = 1e9 + 7; using lint = long long; lint ipow(lint x, lint p){ lint ret = 1, piv = x; while(p){ if(p & 1) ret = ret * piv % mod; piv = piv * piv % mod; p >>= 1; } return ret; } vector<int> berlekamp_massey(vector<int> x){ vector<int> ls, cur; int lf, ld; for(int i=0; i<x.size(); i++){ lint t = 0; for(int j=0; j<cur.size(); j++){ t = (t + 1ll * x[i-j-1] * cur[j]) % mod; } if((t - x[i]) % mod == 0) continue; if(cur.empty()){ cur.resize(i+1); lf = i; ld = (t - x[i]) % mod; continue; } lint k = -(x[i] - t) * ipow(ld, mod - 2) % mod; vector<int> c(i-lf-1); c.push_back(k); for(auto &j : ls) c.push_back(-j * k % mod); if(c.size() < cur.size()) c.resize(cur.size()); for(int j=0; j<cur.size(); j++){ c[j] = (c[j] + cur[j]) % mod; } if(i-lf+(int)ls.size()>=(int)cur.size()){ tie(ls, lf, ld) = make_tuple(cur, i, (t - x[i]) % mod); } cur = c; } for(auto &i : cur) i = (i % mod + mod) % mod; return cur; } int get_nth(vector<int> rec, vector<int> dp, lint n){ int m = rec.size(); vector<int> s(m), t(m); s[0] = 1; if(m != 1) t[1] = 1; else t[0] = rec[0]; auto mul = [&rec](vector<int> v, vector<int> w){ int m = v.size(); vector<int> t(2 * m); for(int j=0; j<m; j++){ for(int k=0; k<m; k++){ t[j+k] += 1ll * v[j] * w[k] % mod; if(t[j+k] >= mod) t[j+k] -= mod; } } for(int j=2*m-1; j>=m; j--){ for(int k=1; k<=m; k++){ t[j-k] += 1ll * t[j] * rec[k-1] % mod; if(t[j-k] >= mod) t[j-k] -= mod; } } t.resize(m); return t; }; while(n){ if(n & 1) s = mul(s, t); t = mul(t, t); n >>= 1; } lint ret = 0; for(int i=0; i<m; i++) ret += 1ll * s[i] * dp[i] % mod; return ret % mod; } int guess_nth_term(vector<int> x, lint n){ if(n < x.size()) return x[n]; vector<int> v = berlekamp_massey(x); if(v.empty()) return 0; return get_nth(v, x, n); } struct elem{int x, y, v;}; // A_(x, y) <- v, 0-based. no duplicate please.. vector<int> get_min_poly(int n, vector<elem> M){ // smallest poly P such that A^i = sum_{j < i} {A^j \times P_j} vector<int> rnd1, rnd2; mt19937 rng(0x14004); auto randint = [&rng](int lb, int ub){ return uniform_int_distribution<int>(lb, ub)(rng); }; for(int i=0; i<n; i++){ rnd1.push_back(randint(1, mod - 1)); rnd2.push_back(randint(1, mod - 1)); } vector<int> gobs; for(int i=0; i<2*n+2; i++){ int tmp = 0; for(int j=0; j<n; j++){ tmp += 1ll * rnd2[j] * rnd1[j] % mod; if(tmp >= mod) tmp -= mod; } gobs.push_back(tmp); vector<int> nxt(n); for(auto &i : M){ nxt[i.x] += 1ll * i.v * rnd1[i.y] % mod; if(nxt[i.x] >= mod) nxt[i.x] -= mod; } rnd1 = nxt; } auto sol = berlekamp_massey(gobs); reverse(sol.begin(), sol.end()); return sol; } lint det(int n, vector<elem> M){ vector<int> rnd; mt19937 rng(0x14004); auto randint = [&rng](int lb, int ub){ return uniform_int_distribution<int>(lb, ub)(rng); }; for(int i=0; i<n; i++) rnd.push_back(randint(1, mod - 1)); for(auto &i : M){ i.v = 1ll * i.v * rnd[i.y] % mod; } auto sol = get_min_poly(n, M)[0]; if(n % 2 == 0) sol = mod - sol; for(auto &i : rnd) sol = 1ll * sol * ipow(i, mod - 2) % mod; return sol; } long long int L[100005]; long long int R[100005]; long long int D[100005]; vector<int> d[100005]; int n; vector <int> v[10]; long long int dp[31][10]; int main(void) { cin.tie(0); ios::sync_with_stdio(false); cin >> n; for(int i=0;i<n;i++) { cin >> L[i]; } for(int i=0;i<n;i++) { cin >> R[i]; } bool ok = true; for(int i=0;i<n;i++) { cin >> D[i]; if(D[i]!=0) { ok = false; } else { if(i > 0 && ok==false) { cout << 0 << '\n'; return 0; } } } if(ok) { cout << 1 << '\n'; return 0; } d[0].push_back(D[0]); for(int i=1;i<n;i++) { if(D[i-1]==D[i]) { d[i].push_back(0); d[i].push_back(9); } else if(D[i-1] < D[i]) { d[i].push_back(D[i] - D[i-1]); } else { long long int x = D[i] + 10 - D[i-1] - 1; d[i].push_back(x); } } memset(dp,0,sizeof(dp)); dp[0][0] = 1; for(int i=1;i<10;i++) { dp[1][i] = 1; } for(int i=1;i<30;i++) { for(int j=1;j<10;j++) { for(int k=0;k<10;k++) { int temp = (j + k); int x = 0; while(temp > 0) { x += (temp%10); temp/=10; } dp[i+1][x] += dp[i][j]; dp[i+1][x]%=MOD; } } } for(int i=0;i<10;i++) { long long int sum = 0; for(int j=0;j<=30;j++) { sum += dp[j][i]; sum%=MOD; v[i].push_back(sum); } } long long int res = 1; for(int i=0;i<n;i++) { long long int way = 0; for(int j=0;j<d[i].size();j++) { //cout << i << ' ' << j << ' ' << d[i][j] << '\n'; if(d[i][j]==0) { way += 1; continue; } way += guess_nth_term(v[d[i][j]],R[i]); way%=MOD; way -= guess_nth_term(v[d[i][j]],L[i]); way%=MOD; if(way < 0) way += MOD; } res *= way; res%=MOD; } cout << res << '\n'; return 0; }