結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー nvt4s
提出日時 2019-06-13 12:11:13
言語 C++14
(gcc 8.2.0)
結果
AC  
実行時間 330 ms
コード長 3,619 Byte
コンパイル時間 1,497 ms
使用メモリ 9,340 KB
最終ジャッジ日時 2019-06-13 12:11:20

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
sample1.txt AC 3 ms
6,872 KB
sample2.txt AC 2 ms
6,872 KB
sample3.txt AC 327 ms
6,868 KB
system_test1.txt AC 28 ms
6,872 KB
system_test2.txt AC 118 ms
6,868 KB
system_test3.txt AC 93 ms
6,872 KB
system_test4.txt AC 117 ms
6,868 KB
system_test5.txt AC 198 ms
6,868 KB
system_test6.txt AC 19 ms
6,868 KB
system_test7.txt AC 166 ms
6,868 KB
system_test8.txt AC 55 ms
6,872 KB
system_test9.txt AC 58 ms
6,868 KB
system_test10.txt AC 97 ms
8,916 KB
system_test11.txt AC 35 ms
6,868 KB
system_test12.txt AC 9 ms
6,872 KB
system_test13.txt AC 250 ms
6,872 KB
system_test14.txt AC 203 ms
6,868 KB
system_test15.txt AC 51 ms
6,868 KB
system_test16.txt AC 217 ms
6,872 KB
system_test17.txt AC 330 ms
6,872 KB
testcase01.txt AC 10 ms
6,868 KB
testcase02.txt AC 42 ms
9,340 KB
testcase03.txt AC 3 ms
6,872 KB
testcase04.txt AC 8 ms
6,868 KB
testcase05.txt AC 23 ms
6,872 KB
testcase06.txt AC 21 ms
8,916 KB
testcase07.txt AC 21 ms
6,868 KB
testcase08.txt AC 24 ms
6,872 KB
testcase09.txt AC 9 ms
6,868 KB
testcase10.txt AC 37 ms
8,480 KB
testcase11.txt AC 313 ms
6,868 KB
testcase12.txt AC 3 ms
6,872 KB
testcase13.txt AC 85 ms
6,872 KB
testcase14.txt AC 130 ms
6,872 KB
testcase15.txt AC 105 ms
8,920 KB
testcase16.txt AC 92 ms
6,868 KB
testcase17.txt AC 249 ms
6,868 KB
testcase18.txt AC 19 ms
6,872 KB
testcase19.txt AC 268 ms
6,868 KB
testcase20.txt AC 98 ms
6,872 KB
テストケース一括ダウンロード

ソースコード

diff #
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
#define revrep(i, n) for(int i = n-1; i >= 0; i--)
typedef long long ll;
typedef pair<int,int> Pint;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
//typedef pair<int, pair<int, int>> P;
//typedef tuple<int,int,int> T;
ll INFL = 1000000000000000010;//10^18 = 2^60
int INF = 1000000000;//10^9
ll MOD  = 1000000007;
//vector<int> dy = {0,0,1,-1};
//vector<int> dx = {1,-1,0,0};

mat mul_mat(mat &A, mat &B){
  mat res(A.size(), vec(B[0].size()));
  rep(i, A.size())rep(k, B.size())rep(j, B[0].size()){
    res[i][j] = (res[i][j] + (A[i][k] * B[k][j]) % MOD) % MOD;
  }
  return res;
}
mat pow_mat(mat &A, ll n){
  mat res(A.size(), vec(A.size()));
  rep(i, A.size()) res[i][i] = 1;
  while(n > 0){
    if(n&1) res = mul_mat(res, A);
    A = mul_mat(A, A);
    n >>= 1;
  }
  return res;
}

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

  if(K <= N){
    cout << A[K] << " ";
    ll S = 0;
    rep(i, K){
      S = (S + A[i]) % MOD;
    }
    cout << S << endl;
    return 0;
  }else if(N <= 50){
    mat a(N, vec(N, 0));
    rep(i, N) a[i][0] = 1;
    rep(i, N-1) a[i][i+1] = 1;
    mat a_ = a;
    a = pow_mat(a, K-N);
    ll F = 0;
    rep(i, N){
      F = (F + A[N-1-i] * a[i][0]) % MOD;
    }
    mat b(2*N, vec(2*N, 0));
    rep(i, N)rep(j, N){
      b[i][j] = a_[i][j];
      if(i == j){
        b[i+N][i] = 1;
        b[i+N][i+N] = 1;
      }
    }
    b = pow_mat(b, K-N+1);
    b[N][0]--;

    ll S = 0;
    rep(i, N){
      S = (S + A[N-1-i] * (b[i+N][0])) % MOD;
      S = (S + A[i]) % MOD;
    }
    S = (S + MOD) % MOD;
    cout << F << " " << S << endl;
  }else{
    vec F(K, 0);
    rep(i, N) F[i] = A[i];
    for(int i = 0; i < N; i++){
      F[N] += A[i];
      F[N] %= MOD;
    }
    for(int i = N+1; i < K; i++){
      F[i] = 2 * F[i-1] - F[i-N-1];
      F[i] %= MOD;
    }
    ll S = 0;
    rep(i, K){
      S += F[i];
      S %= MOD;
    }
    cout << (F[K-1] + MOD) % MOD << " " << (S + MOD) % MOD << endl;

  }
}
0