結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー kenjiiiHkenjiiiH
提出日時 2015-11-28 22:58:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 12 ms / 5,000 ms
コード長 2,457 bytes
コンパイル時間 650 ms
コンパイル使用メモリ 69,420 KB
実行使用メモリ 11,204 KB
最終ジャッジ日時 2023-10-12 05:33:46
合計ジャッジ時間 2,621 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

const long long MOD = 1e9 + 7;
int N;
long long K;
long long a[10000];

template<typename T>
class _matrix {
  int _r, _c;
  vector<vector<T> > _container;
public:
  _matrix(int _r, int _c) :  _r(_r), _c(_c), _container(_r, vector<T>(_c)) {}    
  _matrix(int n) :  _r(n), _c(n), _container(n, vector<T>(n)) {}
  _matrix(vector<vector<T> > vec) : _r(vec.size()), _c(vec[0].size()), _container(vec.begin(), vec.end()) {}
  vector<T>& operator[] (int _r) { return this->_container[_r]; }
  int r() { return this->_r; }
  int c() { return this->_c; }
  
  /*
   * return an identity matrix of the size n
   */
  static _matrix<T> eye(int n) {
    _matrix ret(n);
    for (int i = 0; i < n; i++)
      ret[i][i] = 1;
    return ret;
  }
  
  /*
   * matrix multiplication
   */ 
  _matrix<T> operator*(const _matrix<T> &mtx) const {
    _matrix<T> ret(this->_r, mtx._c);
    for (int i = 0; i < this->_r; i++) {
      for (int k = 0; k < this->_c; k++) {
	for (int j = 0; j < mtx._c; j++) {
	  ret[i][j] += this->_container[i][k] * mtx._container[k][j];
	  ret[i][j] %= MOD;
	}
      }
    }
    return ret;
  }
  
  /*
   * matrix power
   */
  _matrix<T> operator^(long long p) const {
    _matrix<T> ret = eye(this->_r);
    _matrix<T> x(this->_container);
    while (p > 0) {
      if (p & 1)
	ret = ret * x;
      x = x * x;
      p >>= 1;
    }
    return ret;
  }
};
typedef _matrix<long long> matrix;

void solve1() {
  matrix A(N+1);
  A[0][0] = 1;
  A[0][N-1] = 1;
  for (int i = 1; i <= N; i++)
    A[1][i] = 1;
  for (int i = 2; i <= N; i++)
    A[i][i-1] = 1;

  matrix B = A ^ (K-1);

  vector<long long> v(N+1);
  for (int i = 0; i < N; i++)
    v[N-i] = a[i];
  v[0] = a[0];

  long long x = 0;
  long long y = 0;

  for (int i = 0; i <= N; i++) {
    x = (x + B[0][i] * v[i]) % MOD;
    y = (y + B[N][i] * v[i]) % MOD;
  }

  cout << y << " " << x << endl;
}

long long dp[1000000 + 1];
void solve2() {
  long long sum = 0;
  long long acc = 0;
  for (int i = 0; i < N; i++)
    dp[i] = a[i], acc = (acc + dp[i]) % MOD;

  sum = acc;
  for (int i = N; i < K; i++) {
    dp[i] = acc;
    acc = (acc - dp[i-N] + dp[i] + MOD) % MOD;
    sum = (sum + dp[i]) % MOD;
  }

  cout << dp[K-1] << " " << sum << endl;

}

void solve() {
  if (K > 1e6)
    solve1();
  else
    solve2();
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> a[i];

  solve();

  return 0;
}
0