結果
| 問題 |
No.526 フィボナッチ数列の第N項をMで割った余りを求める
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-10-16 01:03:26 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,789 bytes |
| コンパイル時間 | 2,183 ms |
| コンパイル使用メモリ | 207,432 KB |
| 実行使用メモリ | 7,716 KB |
| 最終ジャッジ日時 | 2025-10-16 01:03:29 |
| 合計ジャッジ時間 | 3,370 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int mod = 998244353;
#include <chrono>
mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1};
int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};
template<class T>
bool chmin(T& a, const T& b){
if (b < a){
a = b;
return true;
}
else {
return false;
}
}
template<class T>
bool chmax(T& a, const T& b){
if (a < b){
a = b;
return true;
}
else {
return false;
}
}
template<typename T>
struct Matrix{
vector<vector<T>> identity(int n){
vector<vector<T>> vec(n, vector<T> (n));
for (int i = 0; i < n; i++){
vec[i][i] = T(1);
}
return vec;
}
//新しい操作を左に持ってくる
vector<vector<T>> multiply(const vector<vector<T>>& a, const vector<vector<T>>& b){
int n = a.size();
int t = a[0].size();
int m = b[0].size();
vector<vector<T>> vec(n, vector<T> (m, 0));
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
for (int k = 0; k < t; k++){
T A = a[i][k];
T B = b[k][j];
T x = A * B;
vec[i][j] += x;
}
}
}
return vec;
}
vector<vector<T>> matrixpow(ll k, const vector<vector<T>>& x, const vector<vector<T>>& y){
int n = x.size();
vector<vector<T>> id = identity(n);
vector<vector<T>> c = x;
while(k){
if (k & 1){
id = multiply(id, c);
}
auto d = multiply(c, c);
c = d;
k >>= 1;
}
auto ans = multiply(y, id);
return ans;
}
};
void solve(){
int n, m;
cin >> n >> m;
Matrix<ll> mx;
vector<vector<ll>> id = mx.identity(2);
vector<vector<ll>> c(2, vector<ll> (2, 1));
c[1][1] = 0;
int k = n - 2;
while(k){
if (k & 1){
id = mx.multiply(id, c);
}
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++){
id[i][j] %= m;
}
}
auto d = mx.multiply(c, c);
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++){
d[i][j] %= m;
}
}
c = d;
k >>= 1;
}
vector<vector<ll>> a(1, vector<ll> (2));
a[0][0]++;
auto ans = mx.multiply(a, id);
cout << ans[0][0] << '\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
while(t--){
cout << fixed << setprecision(15);
solve();
}
}