結果

問題 No.891 隣接3項間の漸化式
ユーザー face4face4
提出日時 2019-09-20 21:37:06
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 999 bytes
コンパイル時間 875 ms
コンパイル使用メモリ 73,984 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-14 16:48:13
合計ジャッジ時間 2,112 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;

typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
ll mod = 1000000007;

mat mul(mat &A, mat &B){
    mat C(A.size(), vec(B[0].size()));
    for(int i = 0; i < A.size(); i++){
        for(int k = 0; k < B.size(); k++){
            for(int j = 0; j < B[0].size(); j++){
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod;
            }
        }
    }
    return C;
}

mat pow(mat A, ll n){
    mat B(A.size(), vec(A.size()));
    for(int i = 0; i < A.size(); i++){
        B[i][i] = 1ll;
    }
    while(n > 0){
        if(n & 1)   B = mul(B, A);
        A = mul(A, A);
        n >>= 1;
    }
    return B;
}

// 使用例. yukicoder No.718
int main(){
    ll a, b, n;
    cin >> a >> b >> n;

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

    mat A(2, vec(2));
    A[0][0] = a; A[0][1] = b;
    A[1][0] = 1; A[1][1] = 0;
    A = pow(A, n-1);

    cout << A[0][0] << endl;
    return 0;
}
0