結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー y_mazuny_mazun
提出日時 2015-04-28 20:51:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 1,666 bytes
コンパイル時間 1,402 ms
コンパイル使用メモリ 72,260 KB
実行使用メモリ 11,064 KB
最終ジャッジ日時 2023-09-18 14:09:11
合計ジャッジ時間 3,086 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <numeric>
typedef long long ll;

#define REP(i,n) for(int i=0; i<(int)(n); i++)

#include <queue>
#include <cstdio>
inline int getInt(){ int s; scanf("%d", &s); return s; }
inline ll getLL(){ ll s; scanf("%lld", &s); return s; }

#include <set>

using namespace std;

const ll mod = 1000000007;

vector<vector<ll> > mult(const vector<vector<ll> > &lhs, const vector<vector<ll> > &rhs){
  const int n = lhs.size();
  vector<vector<ll> > ret(n, vector<ll>(n));
  REP(k,n) REP(i,n) REP(j,n)
    ret[i][j] = (ret[i][j] + lhs[i][k] * rhs[k][j]) % mod;
  return ret;
}

int main(){
  const int n = getInt();
  const ll k = getLL();
  vector<ll> a(n);
  REP(i,n) a[i] = getInt();

  if(k <= 1000000){
    a.resize(k);
    a[n] = accumulate(a.begin(), a.end(), 0ll) % mod;
    for(int i = n + 1; i < k; i++){
      a[i] = (2 * a[i - 1] - a[i - n - 1] + mod) % mod;
    }
    ll s = 0;
    REP(i,k) s = (s + a[i]) % mod;
    printf("%lld %lld\n", a[k - 1], s);
  }else{
    vector<ll> v(n + 1);
    vector<vector<ll> > m(n + 1, vector<ll>(n + 1));

    REP(i,n) v[i] = a[i];
    v[n] = accumulate(a.begin(), a.end(), 0ll) % mod;

    REP(i,n+1) m[n][i] = 1;
    REP(i,n) REP(j,n){
      if(i == n - 1) m[i][j] = 1;
      else if(j - 1 == i) m[i][j] = 1;
      else m[i][j] = 0;
    }

    ll kk = k - n;

    vector<vector<ll> > e(n + 1, vector<ll>(n + 1));
    REP(i,n+1) e[i][i] = 1;

    while(kk){
      if(kk % 2) e = mult(m, e);
      m = mult(m, m);
      kk /= 2;
    }

    vector<ll> ans(n + 1);
    REP(i,n+1) REP(j,n+1)
      ans[i] = (ans[i] + v[j] * e[i][j]) % mod;

    printf("%lld %lld\n", ans[n - 1], ans[n]);
  }

  return 0;
}
0