結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー hangin-svghangin-svg
提出日時 2021-05-24 01:37:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 866 bytes
コンパイル時間 1,758 ms
コンパイル使用メモリ 170,560 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-20 10:50:12
合計ジャッジ時間 2,572 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

unsigned long long n,m;

vector<vector<unsigned long long>> a = {{1,1},{1,0}};

vector<vector<unsigned long long>> mul(vector<vector<unsigned long long>> const &b,vector<vector<unsigned long long>> const &c){
  vector<vector<unsigned long long>> d(2,vector<unsigned long long>(2,0));
  for(int i=0;i<2;i++)
    for(int j=0;j<2;j++)
      for(int k=0;k<2;k++){
	d[i][k] += b[i][j] * c[j][k] % m;
	d[i][k] %= m;
      }
  return d;
}

vector<vector<unsigned long long>>  rep_mul(vector<vector<unsigned long long>> const &b,unsigned long long i){
  if(i==1) return b;
  if(i%2==1){
    vector<vector<unsigned long long>> temp = b;
    return mul(rep_mul(mul(b,b),i/2),temp);
  }else{
    return rep_mul(mul(b,b),i/2);
  }
}

int main(){

  cin >> n >> m;
  auto ans = rep_mul(a,n-2);
  
  cout << ans[0][0] << endl;
  
}

0