結果

問題 No.534 フィボナッチフィボナッチ数
ユーザー uenokuuenoku
提出日時 2017-08-25 01:08:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 2,207 bytes
コンパイル時間 882 ms
コンパイル使用メモリ 92,888 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-23 14:17:22
合計ジャッジ時間 1,953 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
typedef long long int lli;
lli mod = 1000000007;
struct Matrix {
    vector<lli> a;
    lli n;
    Matrix(lli n) : n(n)
    {
        a.assign(n * n, 0);
    };
    Matrix operator*(const Matrix r) const
    {
        Matrix v(n);
        rep(i, n) rep(j, n) rep(k, n)
        {
            v.a[i * n + j] += a[i * n + k] * r.a[k * n + j];
            v.a[i * n + j] %= mod;
        }
        return v;
    }
    Matrix operator+(const Matrix r) const
    {
        Matrix v(n);
        rep(i, n) rep(j, n)
        {
            v.a[i * n + j] = (a[i * n + j] + r.a[i * n + j]) % mod;
        }
        return v;
    }
    void set(int b, int d, int c)
    {
        a[b * n + d] = c;
    }
    void operator*=(const Matrix r)
    {
        Matrix v(n);
        rep(i, n) rep(j, n) rep(k, n)
        {
            v.a[i * n + j] += a[i * n + k] * r.a[k * n + j];
            v.a[i * n + j] %= mod;
        }
        *this = v;
    }
    void show()
    {
        rep(i, n) rep(j, n)
        {
            cerr << a[i * n + j] << ' ';
            if (j == n - 1)
                cerr << endl;
        }
    }
};
Matrix idn(lli n)
{
    Matrix a(n);
    rep(i, n)
    {
        a.a[i * n + i] = 1;
    }
    return a;
}
Matrix pow_mat(Matrix a, lli n)
{
    Matrix an = idn(a.n);
    while (n > 0) {
        if (n & 1)
            an *= a;
        a *= a;
        n /= 2;
    }
    return an;
}
Matrix parseStr2Matrix(int n, vector<string> s)
{
    Matrix g(10);
    rep(i, n)
    {
        rep(j, n)
            g.set(i, j, s[i][j] - '0');
    }
    return g;
}
int main()
{
    vector<string> s = {
        "11",
        "10",
    };

    auto m = parseStr2Matrix(2, s);
    lli temp = 2 * mod + 2;

    swap(temp, mod);
    lli n;
    cin >> n;
    if (!n) {
        cout << 0 << endl;
        return 0;
    }

    auto g = pow_mat(m, n - 1);

    lli t = g.a[0];

    swap(temp, mod);
    g = pow_mat(m, t - 1);

    cout << g.a[0] << endl;
}
0