結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー mahala_codermahala_coder
提出日時 2020-03-16 21:52:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 839 bytes
コンパイル時間 1,405 ms
コンパイル使用メモリ 170,480 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-18 19:28:38
合計ジャッジ時間 2,110 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
ll mod=1e9+7;

// size of matrix
ll m;
vec matmul(vec &dp, mat &mt) {
  vec ret(m);
  for(ll i=0;i<m;i++) {
    for(ll j=0;j<m;j++) {
      ret[i]=(ret[i]+mt[i][j]*dp[j])%mod;
    }
  }
  return ret;
}

mat update(mat &mt) {
  mat ret(m,vec(m));
  for(ll i=0;i<m;i++) {
    for(ll j=0;j<m;j++) {
      for(ll k=0;k<m;k++) {
        ret[i][j]=(ret[i][j]+mt[i][k]*mt[k][j])%mod;
      }
    }
  }
  return ret;
}

void matpow(vec &dp,mat &mt, ll k) {
  ll m=dp.size();
  while(k)  {
    if(k&1) dp=matmul(dp,mt);
    mt=update(mt);
    k/=2;
  }
}

int main() {
  m=2;
  vec dp(2);
  mat mt(2,vec(2));
  ll k;
  cin >> k >> mod;
  dp[0]=dp[1]=1;
  mt[0][0]=mt[0][1]=mt[1][0]=1;
  matpow(dp,mt,k+2);
  cout << dp[1] << endl;
}
0