結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー ironairona
提出日時 2020-08-13 12:30:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,277 bytes
コンパイル時間 1,717 ms
コンパイル使用メモリ 172,980 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-18 01:27:56
合計ジャッジ時間 2,252 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
#define ll long long int
#define vec vector<ll>
#define mat vector<vec>

using namespace std;

const ll mod=1000000007;
const ll inf=LONG_LONG_MAX;
ll dx4[4]={1,0,-1,0};
ll dy4[4]={0,-1,0,1};
ll dx8[8]={1,0,-1,1,-1,1,0,-1};
ll dy8[8]={1,1,1,0,0,-1,-1,-1};

mat matmul(mat A,mat B,ll mo){
  ll m=A.size();
  mat C(m,vec(m));
  for(ll i=0;i<m;i++){
    for(ll j=0;j<m;j++){
      for(ll k=0;k<m;k++){
        C[i][j]+=A[i][k]*B[k][j];
        C[i][j]%=mo;
      }
    }
  }
  return C;
}

mat matpow(mat A,ll n,ll mo){
  ll m=A.size();
  if(n==0){
    for(ll i=0;i<m;i++){
      for(ll j=0;j<m;j++){
        if(i==j)A[i][j]=1;
        else A[i][j]=0;
      }
    }
  }
  else if(n&1){
    mat B=matpow(A,n-1,mo);
    A=matmul(A,B,mo);
  }
  else{
    mat B=matpow(A,n/2,mo);
    A=matmul(B,B,mo);
  }
  return A;
}
vec matvec(mat A,vec v,ll mo){
  ll m=v.size();
  vec u(m,0);
  for(ll i=0;i<m;i++){
    for(ll j=0;j<m;j++){
      u[i]+=A[i][j]*v[j];
      u[i]%=mo;
    }
  }
  return u;
}
vec solve(mat A,vec v,ll n,ll mo){
  mat B=matpow(A,n,mo);
  return matvec(B,v,mo);
}

int main(){

  mat A(2,vec(2));
  A[0][0]=1;
  A[0][1]=1;
  A[1][0]=1;
  A[1][1]=0;
  ll n,m;
  cin >> n >> m;
  vec v={1,0};
  v=solve(A,v,n-1,m);

  cout << v[1] << endl;



}
0