結果

問題 No.891 隣接3項間の漸化式
ユーザー mikianaaamikianaaa
提出日時 2020-10-16 21:16:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,508 bytes
コンパイル時間 1,420 ms
コンパイル使用メモリ 173,380 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-28 02:31:20
合計ジャッジ時間 3,158 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define INF 1000000000000000000
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;

template <class T> struct Mat_cul {
    using Matrix = vector<vector<T>>;

    Matrix mat_add(Matrix A, Matrix B, const long long MOD) {
        int m = A.size();
        int n = B[0].size();
        Matrix C(m, vector<T>(n));
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                C[i][j] = A[i][j] + B[i][j];
                C[i][j] %= MOD;
            }
        }

        return C;
    }

    Matrix mat_sub(Matrix A, Matrix B, const long long MOD) {
        int m = A.size();
        int n = B[0].size();
        Matrix C(m, vector<T>(n));

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                C[i][j] = A[i][j] - B[i][j] + MOD;
                C[i][j] %= MOD;
            }
        }

        return C;
    }

    Matrix mat_mul(Matrix A, Matrix B, const long long MOD) {
        int m = A.size();
        int l = B.size();
        int n = B[0].size();
        Matrix C(m, vector<T>(n));

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < l; k++) {
                    C[i][j] += A[i][k] * B[k][j];
                    C[i][j] %= MOD;
                }
            }
        }

        return C;
    }

    Matrix mat_pow_mod(Matrix A, ll p, const long long MOD) {
        Matrix ret(A.size(), vector<T>(A.size(), 0));
        for (int i = 0; i < A.size(); i++)
            ret[i][i] = 1;
        while (p > 0) {
            if (p & 1)
                ret = mat_mul(ret, A, MOD);
            A = mat_mul(A, A, MOD);
            p >>= 1;
        }
        return ret;
    }
};

const ll MOD = pow(10, 9) + 7;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    Mat_cul<ll> M;

    ll a, b, n;
    cin >> a >> b >> n;

    if (n == 0) {
        cout << 0 << endl;
        return 0;
    } else if (n == 1) {
        cout << 1 << endl;
        return 0;
    }

    vector<vector<ll>> A(2, vector<ll>(2)), B(2, vector<ll>(1));
    A[0][0] = a, A[1][0] = b, A[0][1] = 1, A[1][1] = 0;
    B[0][0] = 1, B[1][0] = 0;

    vector<vector<ll>> C = M.mat_pow_mod(A, n - 1, MOD),
                       D = M.mat_mul(C, B, MOD);

    cout << D[0][0] << endl;
}
0