結果
| 問題 |
No.526 フィボナッチ数列の第N項をMで割った余りを求める
|
| コンテスト | |
| ユーザー |
Taiki0715
|
| 提出日時 | 2023-05-04 15:29:05 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,909 bytes |
| コンパイル時間 | 5,882 ms |
| コンパイル使用メモリ | 163,944 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-22 08:00:40 |
| 合計ジャッジ時間 | 4,622 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 |
ソースコード
#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <math.h>
#include <cassert>
#include <bitset>
#include <map>
#include <algorithm>
#include <iterator>
#include <string>
#include <utility>
#include <numeric>
#include <regex>
#include <complex>
#include <atcoder/all>
using namespace atcoder;
using namespace std;
using ll=long long;
using ull=unsigned long long;
using P=pair<ll,ll>;
template<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;
template<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}
template<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}
template<typename T1,typename T2>
ostream &operator<<(ostream &os,const pair<T1,T2>&p){
os<<p.first<<" "<<p.second;
return os;
}
template<typename T1,typename T2>
istream &operator>>(istream &is,pair<T1,T2>&p){
is>>p.first>>p.second;
return is;
}
template<typename T>
istream &operator>>(istream &is,vector<T> &a){
for(auto &i:a)is>>i;
return is;
}
#define reps(i,a,n) for(int i=(a);i<(n);i++)
#define rep(i,n) reps(i,0,n)
#define all(x) x.begin(),x.end()
ll myceil(ll a,ll b){return (a+b-1)/b;}
template<class T>
struct Matrix{
vector<vector<T>>A;
Matrix(int h,int w):A(h,vector<T>(w,0)){}
Matrix(int n):A(n,vector<T>(n,0)){}
size_t height()const{return A.size();}
size_t width()const{return A[0].size();}
const vector<T> &operator[](int k)const{return (A.at(k));}
vector<T> &operator[](int k){return (A.at(k));}
Matrix I(size_t n){
Matrix m(n);
rep(i,n)m[i][i]=1;
return m;
}
Matrix &operator+=(const Matrix &B){
size_t h=height(),w=width();
assert(h==B.height());
assert(w==B.width());
rep(i,h)rep(j,w)(*this)[i][j]+=B[i][j];
return *this;
}
Matrix &operator-=(const Matrix &B){
size_t h=height(),w=width();
assert(h==B.height());
assert(w==B.width());
rep(i,h)rep(j,w)(*this)[i][j]-=B[i][j];
return *this;
}
Matrix &operator*=(const Matrix &B){
size_t h=height(),w=B.width(),p=width();
assert(B.height()==p);
vector<vector<T>>C(h,vector<T>(w,0));
rep(i,h)rep(j,w)rep(k,p)C[i][j]=(C[i][j]+(*this)[i][k]*B[k][j]);
A.swap(C);
return *this;
}
Matrix &operator^=(ll k){
assert(height()==width());
assert(k>=0);
Matrix B=I(height());
while(k){
if(k&1)B*=*this;
*this *= *this;
k>>=1ll;
}
A.swap(B.A);
return *this;
}
Matrix operator+(const Matrix &B)const{
return (Matrix(*this)+=B);
}
Matrix operator-(const Matrix &B)const{
return (Matrix(*this)-=B);
}
Matrix operator*(const Matrix &B)const{
return (Matrix(*this)*=B);
}
Matrix operator^(const ll k)const{
return (Matrix(*this)^=k);
}
};
int main(){
int n,m;
cin>>n>>m;
modint::set_mod(m);
Matrix<modint>mat(2);
mat[0][0]=mat[0][1]=mat[1][0]=1;
cout<<(mat^(n-1))[1][0].val()<<endl;
}
Taiki0715