結果
| 問題 |
No.526 フィボナッチ数列の第N項をMで割った余りを求める
|
| コンテスト | |
| ユーザー |
anisak1s
|
| 提出日時 | 2020-03-22 15:34:02 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 1,262 bytes |
| コンパイル時間 | 1,366 ms |
| コンパイル使用メモリ | 106,636 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-12-24 18:00:35 |
| 合計ジャッジ時間 | 2,340 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 |
ソースコード
#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;
}
anisak1s