結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー anisak1sanisak1s
提出日時 2020-03-22 15:34:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,262 bytes
コンパイル時間 949 ms
コンパイル使用メモリ 105,144 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-26 04:06:21
合計ジャッジ時間 1,649 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <list>
#include <set>
#include <unordered_set>
#include <vector>
#include <utility>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdint>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>

#define rep(i, n) for(ll (i) = 0;(i) < (n);(i)++)
#define mp(a, b) make_pair((a),(b))
#define bs(n) ((ull)1<<(ull)n)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

constexpr ll inf = INT64_MAX / 4;
constexpr double pi = asin(1) * 2;
constexpr ll mod = 1000000007;
constexpr double eps = 0.000000001;

typedef struct {
    ll a[2][2];
} mat;

map<ll, mat> d;
ll n, m;

mat kk(mat a, mat b) {
    mat r;
    rep(i, 2)rep(j, 2)r.a[i][j] = (a.a[i][0] * b.a[0][j] + a.a[i][1] * b.a[1][j]) % m;
    return r;
}

mat cs(int n) {
    if (d.find(n) != d.end())return d[n];
    d[n] = kk(cs(n / 2), cs(n / 2));
    if (n % 2)d[n] = kk(d[n], cs(1));
    return d[n];
}

int main() {
    mat a1;
    a1.a[0][0] = 1;
    a1.a[0][1] = 1;
    a1.a[1][0] = 1;
    a1.a[1][1] = 0;
    d[1] = a1;
    cin >> n >> m;
    if (n < 2) {
        cout << n - 1 << endl;
        return 0;
    }
    cout << cs(n - 1).a[1][0] << endl;
}
0