結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー kironbotkironbot
提出日時 2020-05-07 12:16:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,280 bytes
コンパイル時間 1,629 ms
コンパイル使用メモリ 171,988 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-16 07:50:52
合計ジャッジ時間 2,513 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using stkll = vector<pll>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
#ifndef ONLINE_JUDGE
    #define debug(x) cerr << #x << ": " << x << endl;
#else
    #define debug(x)
#endif



using mat = vvll;
ll N, M;

mat mul(mat &A, mat &B) {
    mat C(A.size(), vll(B[0].size()));
    rep(i, A.size()) rep(k, B.size()) rep(j, B[0].size()) {
        C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % M;
    }
    return C;
}

mat pow(mat A, ll n) {
    mat B(A.size(), vll(A.size()));
    rep(i, A.size()) B[i][i] = 1;
    while(n > 0) {
        if(n % 2 == 1) B = mul(B, A);
        A = mul(A, A);
        n /= 2;
    }
    return B;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    cin >> N >> M;
    mat A(2, vll(2));
    A[0][0] = 1; A[0][1] = 1;
    A[1][0] = 1; A[1][1] = 0;

    A = pow(A, N);
    cout << A[1][0] << endl;
}
0