結果

問題 No.720 行列のできるフィボナッチ数列道場 (2)
ユーザー PachicobuePachicobue
提出日時 2018-07-28 00:41:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,131 bytes
コンパイル時間 2,064 ms
コンパイル使用メモリ 199,612 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-19 05:24:49
合計ジャッジ時間 3,266 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,384 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,384 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//=================================
// Created on: 2018/07/28 00:18:13
//=================================
#include <bits/stdc++.h>
#define show(x) std::cerr << #x << " = " << x << std::endl
using ll = long long;
constexpr ll MOD = 1000000007LL;
using V = std::array<ll, 4>;
using M = std::array<V, 4>;
M mul(const M& m1, const M& m2)
{
    M ans{{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}};
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            for (int k = 0; k < 4; k++) { (ans[i][j] += m1[i][k] * m2[k][j]) %= MOD; }
        }
    }
    return ans;
}
M pow(const M& m, const ll n)
{
    if (n == 0) { return M{{{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}}; }
    if (n % 2 == 1) {
        return mul(pow(m, n - 1), m);
    } else {
        const auto pp = pow(m, n / 2);
        return mul(pp, pp);
    }
}
int main()
{
    ll N, MM;
    std::cin >> N >> MM;
    M m{{{1, 1, 0, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}};
    m = pow(m, MM);
    m[2][0] = m[2][2] = m[3][1] = m[3][3] = 1;
    m = pow(m, N + 1);
    std::cout << m[2][1] << std::endl;
    return 0;
}
0