結果

問題 No.3228 Very Large Fibonacci Sum
ユーザー areik
提出日時 2025-08-08 22:11:40
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,891 bytes
コンパイル時間 4,268 ms
コンパイル使用メモリ 262,192 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-08-08 22:11:45
合計ジャッジ時間 4,568 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using isize = size_t;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
using f64 = long double;
using p2 = pair<i64, i64>;
using el = tuple<i64, i64, i64>;
using mint = atcoder::modint1000000007;

void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}

i64 pow(i64 x, i64 n) {
  i64 res = 1;
  i64 t = x;
  while (n > 0) {
    if (n & 1) {
      res = res * t;
    }
    t = t * t;
    n >>= 1;
  }
  return res;
}
i64 pow(i64 x, i64 n, i64 m) {
  i64 res = 1;
  i64 t = x % m;
  while (n > 0) {
    if (n & 1) {
      res = res * t % m;
    }
    t = t * t % m;
    n >>= 1;
  }
  return res;
}

using mat = vector<vector<mint>>;
using v4 = vector<mint>;
i64 K = 1000000007;;
mat mul(mat a, mat b) {
  assert(a[0].size() == b.size());
  mat res(a.size(), v4(b[0].size(), 0));
  for (i64 i = 0; i < a.size(); i++) {
    for (i64 j = 0; j < b[0].size(); j++) {
      for (i64 k = 0; k < a[0].size(); k++) {
        res[i][j] += a[i][k] * b[k][j];
      }
    }
  }
  return res;
}
mat pow(mat a, i64 n) {
  mat t = a;
  assert(a.size() == a[0].size());
  mat res(a.size(), v4(a.size(), 0));
  for (i64 i = 0; i < a.size(); i++) res[i][i] = 1;
  while (n > 0) {
    if (n & 1) res = mul(res, t);
    t = mul(t, t);
    n >>= 1;
  }
  return res;
}
mat e() {
  return {{1, 0}, {0, 1}};
}
void _main() {
  i64 a, b, c, d, e, N;
  cin >> a >> b >> c >> d >> e >> N;
  if (N == 0) {
    cout << mint(a).val() << "\n";
    return;
  }
  mat init(4, v4(1));
  init[0][0] = a, init[1][0] = b, init[2][0] = a + b, init[3][0] = 1;
  mat A = {
    {0, 1, 0, 0},
    {d, c, 0, e},
    {d, c, 1, e},
    {0, 0, 0, 1},
  };
  A = pow(A, N - 1);
  init = mul(A, init);
  cout << init[2][0].val() << "\n";
}
0