結果

問題 No.720 行列のできるフィボナッチ数列道場 (2)
ユーザー milanis48663220milanis48663220
提出日時 2019-09-23 21:09:30
言語 C++11
(gcc 11.4.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,103 bytes
コンパイル時間 1,397 ms
コンパイル使用メモリ 67,516 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 08:18:13
合計ジャッジ時間 5,517 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘bool prod(matrix<T>, matrix<T>, matrix<T>&) [with T = long long int]’:
main.cpp:33:1: warning: control reaches end of non-void function [-Wreturn-type]
   33 | }
      | ^

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
const ll MOD = 1000000007;

template <typename T>
struct matrix{
    int n, m;
    vector<vector<T>> dat;
    matrix(int n_, int m_){
        n = n_; m = m_;
        for(int i = 0; i < n; i++){
            dat.push_back(vector<T>(m, 0));
        }
    }
};

template <typename T>
bool prod(matrix<T> a, matrix<T> b, matrix<T> &ans){
    if(a.m != b.n) return false;
    for(int i = 0; i < a.n; i++){
        for(int j = 0; j < a.m; j++){
            ans.dat[i][j] = 0;
            for(int k = 0; k < b.m; k++){
                ans.dat[i][j] += a.dat[i][k]*b.dat[k][j];
                ans.dat[i][j] %= MOD;
            }
        }
    }
}

template <typename T>
void copy_mat(matrix<T> a, matrix<T> &b){
    for(int i = 0; i < a.n; i++){
        for(int j = 0; j < a.m; j++){
            b.dat[i][j] = a.dat[i][j];
        }
    }
}

template <typename T>
void pow(matrix<T> a, ll n, matrix<T> &ans){
    matrix<T> buf(a.n, a.n);
    matrix<T> tmp(a.n, a.n);
    copy_mat(a, tmp);

    for(int i = 0; i < a.n; i++) {
        for(int j = 0; j < a.n; j++){
            ans.dat[i][j] = 0;
        }
        ans.dat[i][i] = 1;
    }
    
    for(int i = 0; i <= 60; i++){
        ll m = (ll)1 << i;
        if(m&n){
            prod(tmp, ans, buf);
            copy_mat(buf, ans);
        }
        prod(tmp, tmp, buf);
        copy_mat(buf, tmp);
    }
}

template <typename T>
void print_mat(matrix<T> a){
    for(int i = 0; i < a.n; i++){
        for(int j = 0; j < a.m; j++){
            cout << a.dat[i][j] << ' ';
        }
        cout << endl;
    }
}

int main(){
    ll N, M;
    cin >> N >> M;
    matrix<ll> a(2, 2);
    a.dat[0][0] = 1;
    a.dat[1][0] = 1;
    a.dat[0][1] = 1;
    a.dat[1][1] = 0;
    matrix<ll> m(2, 2);
    pow(a, M, m);
    //print_mat(m);
    matrix<ll> b(4, 4), ans(4, 4);
    copy_mat(m, b);
    for(int i = 0; i < 2; i++){
        b.dat[i+2][i] = 1;
        b.dat[i+2][i+2] = 1;
    }
    //print_mat(b);
    pow(b, N+1, ans);
    //print_mat(ans);
    cout << ans.dat[3][0] << endl;
}
0