結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー Taiki0715Taiki0715
提出日時 2023-03-23 15:42:50
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,843 bytes
コンパイル時間 1,412 ms
コンパイル使用メモリ 143,896 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-18 19:33:32
合計ジャッジ時間 2,111 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 <atcoder/modint>
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;
}
#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());
    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]=1;
  mat[0][1]=1;
  mat[1][0]=1;
  Matrix<modint>s(2,1);
  s[0][0]=0;
  s[1][0]=1;
  auto ans=(mat^(n-1))*s;
  cout<<ans[0][0].val()<<endl;
}
0