結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー gotetengoteten
提出日時 2021-10-30 10:58:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 996 bytes
コンパイル時間 1,930 ms
コンパイル使用メモリ 170,660 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 16:14:53
合計ジャッジ時間 2,484 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(x) (x).begin(),(x).end()
const int mod = 1000000007;
const long long INF = 1LL << 60;
using ll = long long;
#define v2d(type,H,W,name,value) vector<vector<type>> name(H,vector<type>(W,value));

vector<vector<ll>> mul(vector<vector<ll>> a,vector<vector<ll>> b,ll M){
  v2d(ll,a.size(),b[0].size(),c,0);
  rep(i,a.size()){
    rep(k,b.size()){
      rep(j,b[0].size()){
        c[i][j]=(c[i][j]+a[i][k]*b[k][j])%M;
      }
    }
  }
  return c;
}
//a^b mod M
vector<vector<ll>> power(vector<vector<ll>> a,ll b,ll M){
  ll bb=b;
  auto a_pow=a;
  v2d(ll,a.size(),a.size(),res,0);
  rep(i,a.size()){
    res[i][i]=1;
  }
  while(bb>0){
    if(bb&1) res=mul(res,a_pow,M);
    a_pow=mul(a_pow,a_pow,M);
    bb>>=1;
  }
  return res;
}

int main()
{
  ll N,M;
  cin >> N >> M;

  v2d(ll,2,2,a,0);
  a[0][0]=1;
  a[0][1]=1;
  a[1][0]=1;
  a[1][1]=0;

  a=power(a,N,M);
  
  cout << a[1][1] << endl;
}
0