結果
| 問題 | No.526 フィボナッチ数列の第N項をMで割った余りを求める | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-05-07 12:18:34 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 2,000 ms | 
| コード長 | 1,282 bytes | 
| コンパイル時間 | 1,648 ms | 
| コンパイル使用メモリ | 174,052 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-03 09:23:29 | 
| 合計ジャッジ時間 | 2,258 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 12 | 
ソースコード
#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-1);
    cout << A[1][0] << endl;
}
            
            
            
        