結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー NOSSNOSS
提出日時 2018-07-27 23:18:34
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,650 bytes
コンパイル時間 1,826 ms
コンパイル使用メモリ 175,104 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 13:57:57
合計ジャッジ時間 2,799 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 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 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<ll, pair<ll, ll> > P3;

const ll MOD = ll(1e9 + 7);
const ll LLINF = LLONG_MAX;
const int IINF = INT_MAX;
const int MAX_N = int(2e3) + 5;
const double EPS = 1e-8;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)

typedef vector<vector<ll> > mat;

mat init(mat m, int xlen, int ylen){
    m.resize(xlen);
    for(int i=0; i<xlen; i++){
        m[i].resize(ylen);
        for(int j=0; j<ylen; j++){
            m[i][j] = 0;
        }
    }
    return m;
}

mat mul(mat a, mat b){
    mat c;
    c = init(c,a.size(),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)%MOD;
            }
        }
    }
    return c;
}

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

ll fibo(ll n){
    mat a;
    a.resize(2);
    a[0].resize(2);
    a[1].resize(2);
    a[0][0] = 1; a[0][1] = 1;
    a[1][0] = 1; a[1][1] = 0;
    a = pow(a,n);
    return a[0][0]*a[1][0]%MOD;
}



int main() {
    ll n;
    cin >> n;
    cout << fibo(n) << endl;
    return 0;
}
0