結果
| 問題 |
No.526 フィボナッチ数列の第N項をMで割った余りを求める
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-11-09 16:15:06 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 3,554 bytes |
| コンパイル時間 | 4,115 ms |
| コンパイル使用メモリ | 238,148 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-22 22:13:05 |
| 合計ジャッジ時間 | 4,845 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 |
ソースコード
#if 1
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
typedef modint998244353 mint93;
typedef modint1000000007 mint17;
typedef modint mint;
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pll = pair<ll, ll>;
#define vec vector
template <class T>
using v = vector<T>;
template <class T>
using vv = v<v<T>>;
template <class T>
using vvv = v<vv<T>>;
using vl = v<ll>;
using vvl = vv<ll>;
using vvvl = vvv<ll>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
#define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, N) for (ll i = 0; i < (ll)(N); i++)
#define rep1(i, N) for (ll i = 1; i <= (ll)(N); i++)
#define fore(i, a) for (auto &i : a)
#define fs first
#define sc second
#define eb emplace_back
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define YES(x) cout << ((x) ? "YES" : "NO") << endl;
#define Yes(x) cout << ((x) ? "Yes" : "No") << endl;
#define yes(x) cout << ((x) ? "yes" : "no") << endl;
#define drop(x) \
{ \
cout << (x) << endl; \
return; \
}
template <class T, class U>
void chmin(T &t, const U &u) {
if (t > u)
t = u;
}
template <class T, class U>
void chmax(T &t, const U &u) {
if (t < u)
t = u;
}
#define inf (1 << 29)
#define infl (1LL << 60)
int popcnt(uint x) { return __builtin_popcount(x); }
int popcnt(ull x) { return __builtin_popcountll(x); }
int bsr(uint x) { return 31 - __builtin_clz(x); }
int bsr(ull x) { return 31 - __builtin_clzll(x); }
int bsf(uint x) { return __builtin_ctz(x); }
int bsf(ull x) { return __builtin_ctzll(x); }
template <typename T>
istream &operator>>(istream &is, vector<T> &x) {
for (auto &y : x) {
is >> y;
}
return is;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &x) {
for (size_t e = 0; e < x.size(); e++) {
os << x[e] << (e == x.size() - 1 ? "" : " ");
}
return os;
}
template <class T, class S>
istream &operator>>(istream &is, pair<T, S> &x) {
is >> x.first >> x.second;
return is;
}
template <class T, class S>
ostream &operator<<(ostream &os, pair<T, S> &x) {
os << x.first << " " << x.second;
return os;
}
struct StopWatch {
bool f = false;
clock_t st;
void start() {
f = true;
st = clock();
}
int msecs() {
assert(f);
return (clock() - st) * 1000 / CLOCKS_PER_SEC;
}
};
ll rand_int(ll l, ll r) { // [l, r]
static random_device rd;
static mt19937 gen(rd());
return uniform_int_distribution<ll>(l, r)(gen);
}
#endif
ll MOD = 1;
vvl mul_matrix(vvl a, vvl b) {
ll n = a.size(), m = b.size(), l = b[0].size();
vvl res(n, vl(l, 0));
rep(i, n) {
rep(j, l) {
rep(k, m) {
res[i][j] += a[i][k] * b[k][j];
res[i][j] %= MOD;
}
}
}
return res;
}
void solve() {
ll n, m;
cin >> n >> m;
n--;
MOD = m;
vvvl dp(31, vvl(2, vl(2)));
dp[0][1][1] = 1;
dp[0][0][1] = 1;
dp[0][1][0] = 1;
rep(i, 30) {
dp[i + 1] = mul_matrix(dp[i], dp[i]);
}
/*
rep(i, 31){
cout << i << ": " << dp[i] << endl;
}
*/
vvl fibb = {{0}, {1}};
ll cur = 0;
while (n) {
if (n & (1ll << cur)) {
fibb = mul_matrix(dp[cur], fibb);
// cout << cur << ": " << fibb << endl;
n ^= 1ll << cur;
}
cur++;
}
cout << fibb[0][0] << endl;
}
int main() {
// cin.tie(nullptr);
// ios::sync_with_stdio(false);
// cout << fixed << setprecision(20);
ll t = 1;
// cin >> t;
while (t--)
solve();
}