結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー roiti46roiti46
提出日時 2015-10-11 04:04:15
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,614 bytes
コンパイル時間 1,365 ms
コンパイル使用メモリ 153,992 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-28 11:37:33
合計ジャッジ時間 4,817 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 AC 5 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 3 ms
4,376 KB
testcase_33 AC 3 ms
4,376 KB
testcase_34 AC 3 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 4 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 5 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) REP(i, 0, a)
#define EACH(i, a) for (auto i: a)
#define ITR(x, a) for (__typeof(a.begin()) x = a.begin(); x != a.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define HAS(a, x) (a.find(x) != a.end())

int N;
ll K;
const ll mod = 1000000007;
int A[10005];

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]) % mod;
  return C;
}

mat mat_pow(mat A, ll n) {
  mat B(A.size(), vec(A.size()));
  for (int i = 0; i < A.size(); i++) B[i][i] = 1;
  while (n) {
    if (n & 1) B = mat_mul(B, A);
    A = mat_mul(A, A);
    n >>= 1;
  }
  return B;
}

void solve1() {
  ll F = 0, S = 0;
  rep(i, N) A[N] += A[i], S += A[i];
  REP(i, N + 1, K + 1) {
    S = (S + A[i - 1]) % mod;
    A[i] = (2 * A[i - 1] - A[i - N - 1]) % mod;
  }
  cout << A[K - 1] << " " << S << endl;
}

void solve2() {
  mat MB(N + 1, vec(1));
  rep(i, N) MB[N - i][0] = A[i];
  rep(i, N) MB[0][0] += A[i];
  mat MA(N + 1, vec(N + 1));
  rep(i, N + 1) MA[0][i] = 1;
  rep(i, N) MA[1][i + 1] = 1;
  rep(i, N - 1) MA[i + 2][i + 1] = 1;
  MA = mat_pow(MA, K - N);
  MB = mat_mul(MA, MB);
  cout << MB[1][0] << " " << MB[0][0] << endl;
}

int main(void) {
  cin >> N >> K;
  rep(i, N) cin >> A[i];

  if (K <= 1000000)
    solve1();
  else
    solve2();

  return 0;
}
0