結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー fu2tian2fu2tian2
提出日時 2021-03-12 16:25:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,324 bytes
コンパイル時間 2,103 ms
コンパイル使用メモリ 203,124 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 03:49:24
合計ジャッジ時間 2,748 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; //int:2*10**9
typedef long double ld;
typedef pair<ll,ll> P;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define pb push_back
#define MOD 1000000007 //998244353
#define PI 3.141592653
#define INF 100000000000000 //14
//cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);

vector<vector<ll>> mat_mul(vector<vector<ll>> a, vector<vector<ll>> b, ll m) {
    vector<vector<ll>> c(a.size(),vector<ll>(b[0].size(),0));
    REP(i,a.size()) REP(k,b[0].size()) REP(j,b.size()) {
        c[i][k]=(c[i][k]+(a[i][j]*b[j][k])%m)%m;
    }
    return c;
}

vector<vector<ll>> mat_pow(vector<vector<ll>> x, ll n, ll m) {
    vector<vector<ll>> y(x.size(),vector<ll>(x.size(),0));
    REP(i,x.size()) y[i][i]=1;
    while (n) {
        if (n&1) y=mat_mul(x,y,m);
        x = mat_mul(x,x,m);
        n>>=1;
    }
    return y;
}

int main(){
    ll n, m; cin >> n >> m;
    vector<vector<ll>> fib = {{0,1}};
    vector<vector<ll>> t = {{0,1},{1,1}};
    vector<vector<ll>> y = mat_pow(t,n-1,m);
    // cout << "test" << endl;
    // cout << y[0][0] << " " << y[0][1] << endl;
    // cout << y[1][0] << " " << y[1][1] << endl;
    y = mat_mul(fib,y,m);
    cout << y[0][0] << endl;
}
0