結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー mio_hmio_h
提出日時 2016-11-12 16:13:12
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,364 bytes
コンパイル時間 2,553 ms
コンパイル使用メモリ 154,612 KB
実行使用メモリ 18,868 KB
最終ジャッジ日時 2023-08-17 03:22:22
合計ジャッジ時間 3,928 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

class in{struct It{int it;const bool rev;explicit constexpr It(int it_, bool rev=false):it(it_),rev(rev){}int operator*(){return it;}bool operator!=(It& r){return it!=r.it;}void operator++(){rev?--it:++it;}};const It i,n;public:explicit constexpr in(int n):i(0),n(n<0?0:n){}explicit constexpr in(int i,int n):i(i,n<i),n(n){}const It& begin(){return i;}const It& end(){return n;}};

#include <bits/stdc++.h>
using namespace std;

typedef long long i64;
const i64 MOD = 1e9 + 7;
typedef vector<i64> vec;
typedef vector<vec> mat;
//A * B
mat mat_mul(const mat& A, const mat& B) {
    mat C(A.size(), vec(B.size()));
    for(size_t i = 0; i < A.size(); ++i)
      for(size_t k = 0; k < B.size(); ++k)
        for(size_t j = 0; j < B[0].size(); ++j)
          C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
    return C;
}
//A ^ n
mat mat_pow(mat A, i64 n) {
    mat B(A.size(), vec(A.size()));
    for(size_t i = 0; i < A.size(); ++i) B[i][i] = 1LL;
    while(n > 0) {
        if(n & 1LL) B = mat_mul(B, A);
        A = mat_mul(A, A);
        n >>= 1LL;
    }
    return B;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n; i64 k;
    cin >> n >> k;
    if(n > 30) {
        vector<i64> a(n);
        for(auto& x : a) cin >> x;
        vector<i64> f(k, 0LL), s(k, 0LL);
        for(int i : in(k)) {
            if(i < n) {
                f[i] = a[i];
                s[i] = a[i] + (i == 0 ? 0LL : s[i - 1]);
                continue;
            }
            f[i] = s[i - 1] - (i == n ? 0LL : s[i - n - 1]);
            s[i] = s[i - 1] + f[i];
            f[i] %= MOD;
            s[i] %= MOD;
        }
        cout << f.back() << ' ' << s.back() << endl;
        return 0;
    }
    vector<i64> a(n + 1, 0LL);
    for(int i : in(1, n + 1)) cin >> a[i];
    mat s(n + 1, vec(n + 1, 0LL));
    s[0][0] = 2LL; s[0][n] = -1LL;
    for(int i : in(n)) s[i + 1][i] = 1LL;
    s = mat_pow(s, k - n);
    for(int i : in(n)) a[i + 1] += a[i];
    reverse(a.begin(), a.end());
    i64 sk = [&]() {
        i64 res = 0LL;
        for(int j : in(n + 1)) (res += s[0][j] * a[j] % MOD) %= MOD;
        return res;
    }();
    i64 sk_1 = [&]() {
        i64 res = 0;
        for(int j : in(n + 1)) (res += s[1][j] * a[j] % MOD) %= MOD;
        return res;
    }();
    cout << (sk + MOD - sk_1) % MOD << ' ' << (sk + MOD) % MOD << endl;
    return 0;
}
0