結果

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

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
const int mod = 1e9 + 7;
#define fast() cin.tie(0), ios::sync_with_stdio(false)

const Int MOD = 1e9 + 7; //<- alert!!!
typedef vector<Int> arr;
typedef vector<arr> mat;
inline arr mul(const mat &a, arr &b, Int mod)
{
    arr res(b.size(), 0);
    for (Int i = 0; i < (Int)b.size(); i++)
        for (Int j = 0; j < (Int)a[i].size(); j++)
            (res[i] += a[i][j] * b[j]) %= mod;
    return res;
}
inline mat mul(const mat &a, const mat &b, Int mod)
{
    mat res(a.size(), arr(b[0].size(), 0));
    for (Int i = 0; i < (Int)a.size(); i++)
        for (Int j = 0; j < (Int)b[0].size(); j++)
            for (Int k = 0; k < (Int)b.size(); k++)
                (res[i][j] += a[i][k] * b[k][j]) %= mod;
    return res;
}
inline mat mat_pow(mat a, Int n, Int mod)
{
    mat res(a);
    for (Int i = 0; i < (Int)a.size(); i++)
        for (Int j = 0; j < (Int)a[i].size(); j++)
            res[i][j] = (i == j);
    while (n)
    {
        if (n & 1)
            res = mul(a, res, mod);
        a = mul(a, a, mod);
        n >>= 1;
    }
    return res;
}

Int fib(Int a, Int b, Int n)
{
    mat A(2, arr(2));
    A[0][0] = a, A[0][1] = b, A[1][0] = 1, A[1][1] = 0;
    A = mat_pow(A, n, MOD);
    return A[1][0];
}

Int a, b, N;
int main()
{
    cin >> a >> b >> N;
    if (N == 0)
    {
        cout << 0 << endl;
        return 0;
    }
    else if (N == 1)
    {
        cout << 1 << endl;
        return 0;
    }
    Int ans = fib(a, b, N);
    cout << ans << endl;
}
0