結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー yuppe19 😺yuppe19 😺
提出日時 2015-04-27 12:58:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 32 ms / 5,000 ms
コード長 2,021 bytes
コンパイル時間 1,749 ms
コンパイル使用メモリ 159,160 KB
実行使用メモリ 9,828 KB
最終ジャッジ日時 2023-09-18 13:49:06
合計ジャッジ時間 3,779 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 22 ms
4,380 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 9 ms
4,376 KB
testcase_05 AC 8 ms
4,380 KB
testcase_06 AC 9 ms
4,376 KB
testcase_07 AC 14 ms
4,380 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 11 ms
4,376 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 8 ms
4,380 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 17 ms
4,380 KB
testcase_16 AC 14 ms
4,380 KB
testcase_17 AC 5 ms
4,376 KB
testcase_18 AC 15 ms
4,380 KB
testcase_19 AC 20 ms
4,376 KB
testcase_20 AC 30 ms
9,196 KB
testcase_21 AC 32 ms
9,828 KB
testcase_22 AC 31 ms
9,004 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 16 ms
5,184 KB
testcase_25 AC 15 ms
6,420 KB
testcase_26 AC 15 ms
5,560 KB
testcase_27 AC 18 ms
7,356 KB
testcase_28 AC 6 ms
4,376 KB
testcase_29 AC 30 ms
9,804 KB
testcase_30 AC 21 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 7 ms
4,376 KB
testcase_33 AC 10 ms
4,380 KB
testcase_34 AC 8 ms
4,376 KB
testcase_35 AC 7 ms
4,376 KB
testcase_36 AC 16 ms
4,376 KB
testcase_37 AC 3 ms
4,380 KB
testcase_38 AC 18 ms
4,380 KB
testcase_39 AC 8 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int mod = 1e9 + 7;

pair<int, int> solve1(int n, int k, vector<int> a) {
    int y = 0;
    for(int i=0; i<n; i++) { y = (y + a[i]) % mod; }
    int z = y;
    for(int i=n; i<k; i++) {
        a.push_back(z);
        y = (y + z) % mod;
        z = ((z + z) % mod - a[i-n] + mod) % mod;
    }
    return make_pair(a.back(), y);
}

vector<vector<i64>> matmul(vector<vector<i64>> a, vector<vector<i64>> b) {
    vector<vector<i64>> res(a.size(), vector<i64>(b[0].size()));
    for(int i=0; i<a.size(); i++) {
        for(int k=0; k<b.size(); k++) {
            for(int j=0; j<b[0].size(); j++) {
                res[i][j] = (res[i][j] + a[i][k] * b[k][j] + mod) % mod;
            }
        }
    }
    return res;
}

vector<vector<i64>> matpow(vector<vector<i64>> a, i64 n) {
    vector<vector<i64>> res(a.size(), vector<i64>(a.size()));
    for(int i=0; i<a.size(); i++) {
        for(int j=0; j<a.size(); j++) {
            res[i][j] = i==j?1:0;
        }
    }
    while(n > 0) {
        if(n & 1) { res = matmul(res, a); }
        a = matmul(a, a);
        n >>= 1;
    }
    return res;
}

pair<int, int> solve2(int n, i64 k, vector<int> a) {
    vector<vector<i64>> s(n+1, vector<i64>(1));
    s[n][0] = 0;
    s[n-1][0] = a[0];
    for(int i=1; i<n; i++) {
        s[n-1-i][0] = s[n-i][0] + a[i];
    }
    vector<vector<i64>> mat(n+1, vector<i64>(n+1));
    mat[0][0] = 2;
    for(int i=0; i<n; i++) { mat[i+1][i] = 1; }
    mat[0][n] = -1;
    vector<vector<i64>> powered = matpow(mat, k-n);
    vector<vector<i64>> res = matmul(powered, s);
    i64 x = res[0][0], y = res[1][0];
    return make_pair((x-y+mod)%mod, x);
}

int main(void) {
    int n;
    i64 k;
    vector<int> a;
    scanf("%d%lld", &n, &k);
    for(int i=0; i<n; i++) {
        int x;
        scanf("%d", &x);
        a.push_back(x);
    }
    pair<int, int> res = k<=1e6 ? solve1(n, (int)k, a) : solve2(n, k, a);
    printf("%d %d\n", res.first, res.second);
    return 0;
}
0