結果

問題 No.891 隣接3項間の漸化式
ユーザー ganariyaganariya
提出日時 2019-09-20 22:11:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,027 bytes
コンパイル時間 1,540 ms
コンパイル使用メモリ 147,680 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-12 19:30:13
合計ジャッジ時間 3,315 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
#include <climits>
#include <set>
#include <unordered_set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <complex>
#include <regex>
#include <locale>
#include <random>
#include <type_traits>

using namespace std;

#define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}}

using LL = long long;

//------------------------------------------
//------------------------------------------

template<class T>
using Matrix = vector<vector<T>>;

template<class T>
Matrix<T> mat_add(Matrix<T> A, Matrix<T> B, const LL MOD) {
    int m = A.size();
    int n = B[0].size();
    Matrix<T> 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;
}

template<class T>
Matrix<T> mat_sub(Matrix<T> A, Matrix<T> B, const LL MOD) {
    int m = A.size();
    int n = B[0].size();
    Matrix<T> 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;
}

template<class T>
Matrix<T> mat_mul(Matrix<T> A, Matrix<T> B, const LL MOD) {
    int m = A.size();
    int l = B.size();
    int n = B[0].size();
    Matrix<T> 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;
}

template<class T>
Matrix<T> mat_pow_mod(Matrix<T> A, LL p, const LL MOD) {
    Matrix<T> 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;
}

constexpr LL mod = 1e9 + 7;

int main() {

    LL a, b, n;
    cin >> a >> b >> n;

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

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

    Matrix<LL> B(2, vector<LL>(1));
    B[0][0] = 1;
    B[0][1] = 0;

    auto ans = mat_mul(mat_pow_mod(A, n - 1, mod), B, mod);
    cout << ans[0][0] << endl;

    return 0;
}


































0