結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー nerungonerungo
提出日時 2022-09-03 01:56:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,614 bytes
コンパイル時間 3,256 ms
コンパイル使用メモリ 233,856 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 01:41:13
合計ジャッジ時間 3,937 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 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 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 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 int uint;
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;
typedef vector<double> vd;
typedef vector<vd> vvd;

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

//行列積 a*b (mod mod) の計算
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;
}

//行列累乗 a^n (mod mod) の計算 計算量:O(size^3 * logn)
vvl mat_pow(vvl &a, int n, int mod){
    int size = a.size();
    vvl ret(size, vl(size, 0));
    vvl b = a;
    rep(i, size){
        rep(j, size){
            if(i == j)ret[i][j] = 1;
        }
    }
    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 = {{1, 1}, {1, 0}};
  vvl tmp = mat_pow(a, n-1, m);
  cout << tmp[1][0] << endl;
  return 0;
}
0