結果
問題 | No.3037 Restricted Lucas (Hard) |
ユーザー | algon_320 |
提出日時 | 2018-04-01 23:27:10 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,247 bytes |
コンパイル時間 | 1,361 ms |
コンパイル使用メモリ | 93,760 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-26 06:07:53 |
合計ジャッジ時間 | 1,789 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
ソースコード
#include <iostream> #include <string> #include <vector> #include <functional> #include <cassert> #include <cmath> using namespace std; #define int long long #define SZ(c) (int)(c.size()) vector<int> dummy_zero; #define ZERO SZ(dummy_zero) vector<int> dummy_one = {ZERO}; #define ONE SZ(dummy_one) int add(int a, int b) { while (b != ZERO) { int c = (a & b) << 1; a ^= b; b = c; } return a; } void increment(int &x) { x = add(x, ONE); } int neg(int x) { return add((~x),1); } #define SEVEN (add(add(add(add(add(add(ONE, ONE), ONE), ONE), ONE), ONE), ONE)) #define TEN (add(add(add(add(add(add(add(add(add(ONE, ONE), ONE), ONE), ONE), ONE), ONE), ONE), ONE), ONE)) template<typename T> void resize_matrix(vector<vector<T>> &A, int h, int w, T fill) { A.resize(h); for (int i = ZERO; i < h; increment(i)) { A[i].resize(w, fill); } } template<typename T> vector<vector<T>> multiple_matrix(vector<vector<T>> &A, vector<vector<T>> &B, function<T(T, T)> add, function<T(T, T)> mul, T zero = ZERO) { int m = A.size(); int n = A[ZERO].size(); assert(n == B.size()); int l = B[ZERO].size(); vector<vector<T>> res; resize_matrix(res, m, l, zero); for (int i = ZERO; i < m; increment(i)) { for (int j = ZERO; j < l; increment(j)) { T tmp = zero; for (int k = ZERO; k < n; increment(k)) { T p = mul(A[i][k], B[k][j]); tmp = add(tmp, p); } res[i][j] = tmp; } } return res; } template <typename T> vector<vector<T>> pow_matrix(vector<vector<T>> A, int k, function<T(T, T)> add, function<T(T, T)> mul, T e = ONE, T zero = ZERO) { int n = A.size(); assert(n == A[ZERO].size()); vector<vector<T>> res; resize_matrix(res, n, n, zero); for (int i = ZERO; i < n; increment(i)) res[i][i] = e; while (k > ZERO) { if (k & ONE) res = multiple_matrix(res, A, add, mul, zero); A = multiple_matrix(A, A, add, mul, zero); k >>= ONE; } return res; } const int MOD = add(pow(TEN, add(SEVEN, add(ONE, ONE))), SEVEN); function<int(int, int)> mod_add = [](int a, int b) { int ret = add(a, b); while (ret >= MOD) ret -= MOD; return ret; }; function<int(int, int)> mod_mul = [](int a, int b) { int ret = ZERO; while (b > ZERO) { if (b & ONE) { ret += a; while (ret >= MOD) ret = add(ret, neg(MOD)); } a += a; while (a >= MOD) a = add(a, neg(MOD)); b >>= ONE; } return ret; }; signed main() { int T; cin >> T; while (T--) { int N; cin >> N; vector<vector<int>> v = { {(ONE + ONE + ONE)}, {ONE}, {(ONE + ONE)}, }; vector<vector<int>> m = { {ONE, ONE, ZERO}, {ONE, ZERO, ZERO}, {ZERO, ONE, ZERO}, }; if (N <= (ONE + ONE)) { cout << v[N][ZERO] << endl; } else { m = pow_matrix(m, N-(ONE + ONE), mod_add, mod_mul); auto ans = multiple_matrix(m, v, mod_add, mod_mul); cout << ans[ZERO][ZERO] << endl; } } return ZERO; }