結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー y_mazuny_mazun
提出日時 2015-04-28 20:51:12
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 20 ms / 5,000 ms
コード長 1,666 bytes
コンパイル時間 628 ms
コンパイル使用メモリ 70,840 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-07-05 05:04:18
合計ジャッジ時間 1,922 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘ll getLL()’:
main.cpp:10:31: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   10 | inline ll getLL(){ ll s; scanf("%lld", &s); return s; }
      |                          ~~~~~^~~~~~~~~~~~
main.cpp: In function ‘int getInt()’:
main.cpp:9:34: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    9 | inline int getInt(){ int s; scanf("%d", &s); return s; }
      |                             ~~~~~^~~~~~~~~~

ソースコード

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