結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー nerungonerungo
提出日時 2022-08-25 13:33:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,495 bytes
コンパイル時間 2,152 ms
コンパイル使用メモリ 170,752 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-20 22:24:51
合計ジャッジ時間 2,285 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n+1); i++)
#define all(x) (x).begin(), (x).end()

//using mint = modint1000000007;
//using mint = modint998244353;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

const int INF = 1<<30;
const long long INFL = (ll)1<<60;
const double PI = 3.14159265358979;

vvl mat_mul(vvl &a, vvl &b, int mod){
    int m = a.size(),n = b[0].size(), l = a[0].size();
    vvl c(m, vl(n, 0));
    rep(i, m){
        rep(k, l){
            rep(j, n){
                c[i][j] += a[i][k] * b[k][j];
                c[i][j] %= mod;
            }
        }
    }
    return c;
}

vvl mat_pow(vvl &a, int n, int mod){
    int size = a.size();
    vvl ret(size, vl(size, 0));
    vvl b(size, vl(size));
    rep(i, size){
        rep(j, size){
            if(i == j)ret[i][j] = 1;
            b[i][j] = a[i][j];
        }
    }
    int k = 1;
    while((1<<k) <= n)k++;
    rep(i, k){
        if(n & (1<<i)){
            ret = mat_mul(ret, b, mod);
        }
        b = mat_mul(b, b, mod);
    }
    return ret;
}

int main() {
  int n, m;
  cin >> n >> m;
  vvl a(2, vl(2, 1));
  a[1][1] = 0;
  vvl a_pow = mat_pow(a, n-1, m);
  cout << a_pow[1][0] << endl;
  return 0;
}
0