結果

問題 No.720 行列のできるフィボナッチ数列道場 (2)
ユーザー rpy3cpprpy3cpp
提出日時 2018-08-11 19:43:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,918 bytes
コンパイル時間 2,205 ms
コンパイル使用メモリ 212,376 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 13:59:54
合計ジャッジ時間 3,100 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using T = long long;
using vvt = vector<vector<long long>>;
constexpr long long MOD = 1'000'000'007;
long long umod(long long x, const long long mod = MOD) {return ((x % mod) + mod) % mod;} // unsigned mod. always returns a non-negative value
long long inv(long long x, const long long mod = MOD) { // Finds a multiplicative inverse of x modulo mod by Extended Euclidean Algorithm
    long long a = umod(x);
    long long b = mod;
    long long c = 1LL;
    long long d = 0LL;
    long long q;
    long long r;
    while (b != 1){
        q = a / b;
        r = a % b;
        a = b;
        b = r;
        long long tmp = c;
        c = d;
        d = tmp - q * d;
    }
    return umod(d);
}

auto identity_mat(int n){
    vvt I(n, vector<T>(n, 0));
    for (int i = 0; i != n; ++i) I[i][i] = 1;
    return I;
}

auto matmul(vvt a, vvt b, const long long mod = MOD){
    int n_row = a.size();
    int n_col = b[0].size();
    assert(a[0].size() == b.size());
    vvt c(n_row, vector<T>(n_col, 0));
    for (int i = 0; i != n_row; ++i){
        for (int j = 0; j != n_col; ++j){
            for (int k = 0; k != b.size(); ++k){
                c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
            }
        }
    }
    return c;
}

auto matpow(vector<vector<long long>> mat, long long n, const long long mod = MOD){
    // a^n を求める。
    assert(mat.size() == mat[0].size());
    auto ans = identity_mat(mat.size());
    while (n){
        if (n & 1) ans = matmul(ans, mat, mod);
        mat = matmul(mat, mat, mod);
        n >>= 1;
    }
    return ans;
}

auto invmat(const vector<vector<long long>> &mat, const long long mod = MOD){
    vvt ans = {{0LL, 0LL}, {0LL, 0LL}};
    long long a = mat[0][0];
    long long b = mat[0][1];
    long long c = mat[1][0];
    long long d = mat[1][1];
    if (d != 0LL){
        ans[0][0] = inv(a - ((b * c) % mod) * inv(d) % mod);
    }else{
        ans[0][0] = 0LL;
    }
    ans[1][0] = inv(b - ((a * d) % mod) * inv(c) % mod);
    ans[0][1] = inv(c - ((d * a) % mod) * inv(b) % mod);
    ans[1][1] = inv(d - ((c * b) % mod) * inv(a) % mod);
    return ans;
}

auto matminus(vector<vector<long long>> a, vector<vector<long long>> b){
    int n_row = a.size();
    int n_col = a[0].size();
    assert(b.size() == n_row);
    assert(b[0].size() == n_col);
    vvt c(n_row, vector<long long>(n_col, 0));
    for (int i = 0; i != n_row; ++i){
        for (int j = 0; j != n_col; ++j){
            c[i][j] = umod(a[i][j] - b[i][j]);
        }
    }
    return c;
}

int main() {
    long long N;
    int M;
    cin >> N >> M;
    vvt mat = {{1LL, 1LL},{1LL, 0LL}};
    auto Fm = matpow(mat, M);
    auto Fmn1 = matpow(Fm, N + 1LL);
    auto Fm_minus_I = matminus(Fm, identity_mat(2));
    auto Finv = invmat(Fm_minus_I);
    auto Fcum = matmul(Finv, matminus(Fmn1, Fm));
    cout << Fcum[1][0] << endl;
    return 0;
}
0