結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー utouto97utouto97
提出日時 2019-03-15 12:56:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,164 bytes
コンパイル時間 1,390 ms
コンパイル使用メモリ 151,308 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-11 04:11:29
合計ジャッジ時間 2,264 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

#define int long long
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define mp make_pair
#define mt make_tuple

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int inf = 1LL<<60;
const int mod = 1e9 + 7;
const double eps = 1e-9;

/*{
}*/

int n, m;

typedef vector<int> vec;
typedef vector<vec> mat;

mat mul(mat &a, mat &b)
{
  mat c(a.size(), vec(b[0].size()));
  for(int i = 0; i < a.size(); i++){
    for(int k = 0; k < b.size(); k++){
      for(int j = 0; j < b[0].size(); j++){
        c[i][j] = (c[i][j] + (a[i][k] * b[k][j])) % m;
      }
    }
  }
  return c;
}

mat pow(mat a, int n)
{
  mat b(a.size(), vec(a.size()));
  for(int i = 0; i < b.size(); i++) b[i][i] = 1;
  while(n > 0){
    if(n & 1) b = mul(b, a);
    a = mul(a, a);
    n >>= 1;
  }
  return b;
}

signed main()
{
  cin >> n >> m;

  mat a(1, vec(2));
  a[0][0] = 1;

  mat b(2, vec(2));
  b[0][0] = b[0][1] = b[1][0] = 1;

  b = pow(b, n-2);
  cout << mul(b, a)[0][0] << endl;

  return 0;
}
0