結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー とくまるproとくまるpro
提出日時 2020-05-05 18:59:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,649 bytes
コンパイル時間 1,738 ms
コンパイル使用メモリ 175,344 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-09 07:31:19
合計ジャッジ時間 2,548 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i < (n);i++)
#define ALL(obj) (obj).begin(),(obj).end()
#define SCANF(i) int i;scanf("%d",&i)
using namespace std;
using P = pair<int,int>;
const long long INF = 1LL << 60;
const int IINF=100000000;
const int MOD = (int)1e9 + 7;
typedef long long ll;
//約数列挙
vector<long long> divisor(long long n) {
    vector<long long> ret;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    //sort(ret.begin(), ret.end()); // 昇順に並べる
    return ret;
}
vector<int> dx={1,0,-1,0};vector<int> dy={0,-1,0,1};

ll N,M;

vector<vector<ll>> matcalc(vector<vector<ll>> &A,vector<vector<ll>> &B){
    vector<vector<ll>> C(A.size(),vector<ll>(B[0].size(),0));
    for(ll i=0;i<A.size();i++){
        for(ll k =0;k<B.size();k++){
            for(ll j=0;j<B[0].size();j++){
                C[i][j]=(C[i][j]+A[i][k]*B[k][j]) % M;
            }
        }
    }
    return C;
}

vector<vector<ll>> matpow(vector<vector<ll>> A, ll n){
    vector<vector<ll>> B(A.size(),vector<ll>(A.size(),0));
    //単位行列
    for(ll i=0;i<A.size();i++){
        B[i][i]=1;
    }
    n--;
    while(n>0){
        if(n&1)B = matcalc(B,A);
        A = matcalc(A,A);
        n >>=1;
    }
    return B;
}
signed main () {
    cin >> N >> M;
    vector<vector<ll>> mat(2,vector<ll>(2));
    mat[0][0]=1;mat[0][1]=1;mat[1][0]=1;mat[1][1]=0;
    mat = matpow(mat,N);
    cout << mat[1][0] << endl;
}
0