結果

問題 No.891 隣接3項間の漸化式
ユーザー ForestedForested
提出日時 2020-05-26 13:40:40
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 1,014 bytes
コンパイル時間 932 ms
コンパイル使用メモリ 99,656 KB
実行使用メモリ 814,592 KB
最終ジャッジ日時 2024-10-13 02:40:32
合計ジャッジ時間 2,781 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10 MLE * 1 -- * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
using namespace std;

constexpr int mod = 1000000007;
using mat = vector<vector<int>>;

mat matrix_mul(mat &A, mat &B) {
    mat C(A.size(), vector<int>(B[0].size(), 0));
    rep(i, A.size()) rep(j, B[0].size()) rep(k, B.size()) {
        C[i][j] += (long long)A[i][k] * B[k][j] % mod;
        C[i][j] %= mod;
    }
    return C;
}

mat matrix_pow(mat &A, int n) {
    if (n == 1) return A;
    mat B = matrix_pow(A, n / 2);
    mat C = matrix_mul(B, B);
    if (n % 2) return matrix_mul(C, A);
    return matrix_mul(B, B);
}

int main() {
    int a, b, n;
    cin >> a >> b >> n;
    
    mat A = {{a, b}, {1, 0}};
    mat B = {{1}, {0}};
    mat C = matrix_pow(A, n);
    mat ans = matrix_mul(C, B);
    cout << ans[1][0] << endl;
    return 0;
}
0