結果
問題 | No.3037 Restricted Lucas (Hard) |
ユーザー | algon_320 |
提出日時 | 2018-04-01 23:32:59 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 10 ms / 2,000 ms |
コード長 | 3,341 bytes |
コンパイル時間 | 1,216 ms |
コンパイル使用メモリ | 93,512 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-26 06:09:19 |
合計ジャッジ時間 | 1,845 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
5,248 KB |
testcase_01 | AC | 7 ms
5,376 KB |
testcase_02 | AC | 10 ms
5,376 KB |
testcase_03 | AC | 9 ms
5,376 KB |
testcase_04 | AC | 9 ms
5,376 KB |
testcase_05 | AC | 10 ms
5,376 KB |
testcase_06 | AC | 9 ms
5,376 KB |
ソースコード
#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) << ONE; a ^= b; b = c; } return a; } void increment(int &x) { x = add(x, ONE); } int neg(int x) { return add((~x), ONE); } #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 = add(ret, neg(MOD)); return ret; }; function<int(int, int)> mod_mul = [](int a, int b) { int ret = ZERO; while (b > ZERO) { if (b & ONE) { ret = add(ret, a); while (ret >= MOD) ret = add(ret, neg(MOD)); } a = add(a, a); while (a >= MOD) a = add(a, neg(MOD)); b >>= ONE; } return ret; }; signed main() { int T; cin >> T; while (T) { T = add(T, neg(ONE)); int N; cin >> N; vector<vector<int>> v = { {add(add(ONE, ONE), ONE)}, {ONE}, {add(ONE, ONE)}, }; vector<vector<int>> m = { {ONE, ONE, ZERO}, {ONE, ZERO, ZERO}, {ZERO, ONE, ZERO}, }; if (N <= add(ONE, ONE)) { cout << v[N][ZERO] << endl; } else { m = pow_matrix(m, add(N,neg(add(ONE, ONE))), mod_add, mod_mul); auto ans = multiple_matrix(m, v, mod_add, mod_mul); cout << ans[ZERO][ZERO] << endl; } } return ZERO; }