結果
| 問題 |
No.526 フィボナッチ数列の第N項をMで割った余りを求める
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-12-31 23:56:45 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 2,660 bytes |
| コンパイル時間 | 1,863 ms |
| コンパイル使用メモリ | 176,160 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-10 00:59:16 |
| 合計ジャッジ時間 | 2,676 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 |
ソースコード
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
#define rep(i,n) for(ll i=0; i<n; i++)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rrep(i,n) for(ll i=n-1; i>=0; i--)
#define fi first
#define se second
long long mo = 1000000007;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
typedef pair<ll,Pll> PlP;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<class A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}cout << "\n";}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
string zero_padding(int val, int nf){ ostringstream sout;sout << setfill('0') << setw(nf) << val; return sout.str();};
ld PI=asin(1)*2;
//using namespace atcoder;
// ll m; // 遷移行列のサイズ
// // dpの更新
// vector<ll> matmul(vector<ll> &dp, vector<vector<ll>> &mt){
// vector<ll> ret(m,0);
// rep(i,m)rep(j,m) ret[i] += mt[i][j]*dp[j];
// return ret;
// }
// // 遷移行列の更新
// vector<vector<ll>> update(vector<vector<ll>> &mt){
// vector<vector<ll>> ret(m, vector<ll>(m,0));
// rep(i,m)rep(j,m)rep(k,m) ret[i][j] += mt[i][k] + mt[k][j];
// return ret;
// }
// void matpow(vector<ll> &dp, vector<vector<ll>> &mt, ll k){
// ll m = dp.size();
// while(k){
// if(k&1LL) dp = matmul(dp,mt);
// mt = update(mt);
// k /= 2;
// }
// }
ll M;
ll m; // 遷移行列のサイズ
// dpの更新
vector<ll> matmul(vector<ll> &dp, vector<vector<ll>> &mt){
vector<ll> ret(m,0);
rep(i,m)rep(j,m) {
ret[i] += mt[i][j]*dp[j];
ret[i] %= M;
}
return ret;
}
// 遷移行列の更新
vector<vector<ll>> update(vector<vector<ll>> &mt){
vector<vector<ll>> ret(m, vector<ll>(m,0));
rep(i,m)rep(j,m)rep(k,m) {
ret[i][j] += mt[i][k] * mt[k][j];
ret[i][j] %= M;
}
return ret;
}
void matpow(vector<ll> &dp, vector<vector<ll>> &mt, ll k){
ll m = dp.size();
while(k){
if(k&1LL) dp = matmul(dp,mt);
mt = update(mt);
k /= 2;
}
}
int main(){
ll N;
m = 2;
cin >> N >> M;
vector<ll> dp(2);
vector<vector<ll>> mt(2, vector<ll>(2));
dp[0] = 0; dp[1] = 1;
mt[0][0] = mt[0][1] = mt[1][0] = 1;
mt[1][1] = 0;
matpow(dp,mt,N);
cout << dp[1] << endl;
}
/*
int main(){
ll N,M;
cin >> N >> M;
vector<ll> F(100);
F[0] = 0;
F[1] = 1;
rep(i,N){
F[(i+2)%100] = F[(i+1)%100] + F[i%100];
F[(i+2)%100] %= M;
}
cout << F[(N-1)%100] << endl;
}*/