結果
問題 |
No.526 フィボナッチ数列の第N項をMで割った余りを求める
|
ユーザー |
|
提出日時 | 2024-11-07 20:28:36 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,614 bytes |
コンパイル時間 | 6,920 ms |
コンパイル使用メモリ | 336,580 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-07 20:28:46 |
合計ジャッジ時間 | 7,829 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> using namespace std; using namespace atcoder; using ll = int64_t; using ull = uint64_t; using ld = long double; template <typename T, typename V> void chmax(T& target, const V& cand) { target = max(target, static_cast<T>(cand)); } template <typename T, typename V> void chmin(T& target, const V& cand) { target = min(target, static_cast<T>(cand)); } #define fae(type, var, from, to) \ for (type var = static_cast<type>(from); var < static_cast<type>(to); ++var) #define fai(type, var, from, to) \ for (type var = static_cast<type>(from); var <= static_cast<type>(to); ++var) #define fdi(type, var, from, to) \ for (type var = static_cast<type>(from); var >= static_cast<type>(to); --var) int main() { ll N, M; cin >> N >> M; vector<vector<ll>> P[32]; P[0] = {{2, 1}, {1, 1}}; auto prod = [&](auto& a, auto& b) { auto c = a; c[0][0] = (a[0][0] * b[0][0] + a[0][1] * b[1][0]) % M; c[0][1] = (a[0][0] * b[0][1] + a[0][1] * b[1][1]) % M; c[1][0] = (a[1][0] * b[0][0] + a[1][1] * b[1][0]) % M; c[1][1] = (a[1][0] * b[0][1] + a[1][1] * b[1][1]) % M; return c; }; auto gen = [&](ll k) { vector<vector<ll>> G{{1, 0}, {0, 1}}; ll i = 0; while (k > 0) { if (k & 1) { G = prod(P[i], G); } P[i + 1] = prod(P[i], P[i]); k = k >> 1; i++; } return G; }; auto k = (N - 1) / 2; auto G = gen(k); if (N % 2 == 0) { cout << (G[0][0] % M) << endl; } else { cout << (G[1][0] % M) << endl; } }