結果

問題 No.891 隣接3項間の漸化式
ユーザー Kome_soudouKome_soudou
提出日時 2021-11-03 14:21:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,206 bytes
コンパイル時間 701 ms
コンパイル使用メモリ 75,184 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-21 05:17:35
合計ジャッジ時間 1,796 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 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 WA -
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 WA -
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 WA -
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 WA -
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 1 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 1 ms
5,376 KB
testcase_41 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

typedef vector<vector<int64_t>> Matrix;
// 行列の乗算(mod)
Matrix ModMultiMatrix(const Matrix &a, const Matrix &b, int64_t mod)
{
  Matrix c(a.size(), vector<int64_t>(b.at(0).size()));
  for(int i = 0; i < a.size(); i++)
  {
    for(int j = 0; j < b.size(); j++)
    {
      for(int k = 0; k < b.at(0).size(); k++)
      {
        c.at(i).at(k) += a.at(i).at(j) * b.at(j).at(k);
        c.at(i).at(k) %= mod;
      }
    }
  }
  return c;
}
// 行列の累乗(mod)
Matrix ModPowerMatrix(Matrix a, int64_t n, int64_t mod)
{
  Matrix b(a.size(), vector<int64_t>(a.size()));
  for(int i = 0; i < a.size(); i++) b.at(i).at(i) = 1;
  while(n > 0)
  {
    if(n & 1) b = ModMultiMatrix(b, a, mod);
    a = ModMultiMatrix(a, a, mod);
    n >>= 1;
  }
  return b;
}

int main()
{
  int64_t A, B, N;
  cin >> A >> B >> N;
  
  int64_t mod = 1000000007;
  Matrix mat(2, vector<int64_t>(2));
  mat.at(0).at(0) = A;
  mat.at(0).at(1) = B;
  mat.at(1).at(0) = 1;
  mat = ModPowerMatrix(mat, N - 1, mod);
  
  Matrix F(2, vector<int64_t>(1));
  F.at(0).at(0) = 1;
  Matrix ans = ModMultiMatrix(mat, F, mod);
  cout << ans.at(0).at(0) << endl;
  return 0;
}
0