結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー mamekinmamekin
提出日時 2015-05-24 23:27:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 2,761 bytes
コンパイル時間 888 ms
コンパイル使用メモリ 102,260 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 12:50:31
合計ジャッジ時間 2,601 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

const int MOD = 1000000007;

// 行列の積
template <class T>
vector<vector<T> > matrixProduct(const vector<vector<T> >& x, const vector<vector<T> >& y)
{
    int a = x.size();
    int b = x[0].size();
    int c = y[0].size();
    vector<vector<T> > z(a, vector<T>(c, 0));
    for(int i=0; i<a; ++i){
        for(int j=0; j<c; ++j){
            for(int k=0; k<b; ++k){
                z[i][j] += x[i][k] * y[k][j];
                z[i][j] %= MOD;
            }
        }
    }
    return z;
}

// 行列の累乗
template <class T>
vector<vector<T> > matrixPower(const vector<vector<T> >& x, long long k)
{
    int n = x.size();
    vector<vector<T> > y(n, vector<T>(n, 0));
    for(int i=0; i<n; ++i)
        y[i][i] = 1; // 積の単位元

    vector<vector<T> > z = x;
    while(k > 0){
        if(k & 1)
            y = matrixProduct(y, z);
        z = matrixProduct(z, z);
        k >>= 1;
    }
    return y;
}

pair<long long, long long> solve1(const vector<int>& a, long long k)
{
    int n = a.size();
    queue<int> q;
    int sum = 0;
    for(int i=0; i<n; ++i){
        sum += a[i];
        sum %= MOD;
        q.push(a[i]);
    }

    pair<long long, long long> ans(-1, sum);
    for(int i=n; i<k; ++i){
        ans.first = sum;
        ans.second += sum;
        ans.second %= MOD;
        q.push(sum);
        sum += sum;
        sum -= q.front();
        sum = (sum % MOD + MOD) % MOD;
        q.pop();
    }
    return ans;
}

pair<long long, long long> solve2(const vector<int>& a, long long k)
{
    int n = a.size();
    vector<vector<long long> > mat(n+1, vector<long long>(n+1, 0));
    for(int i=0; i<n-1; ++i)
        mat[i][i+1] = 1;
    for(int i=0; i<n; ++i)
        mat[n-1][i] = 1;
    mat[n][0] = mat[n][n] = 1;

    mat = matrixPower(mat, k - 1);

    pair<long long, long long> ans(0, 0);
    for(int i=0; i<n; ++i){
        ans.first += mat[0][i] * a[i];
        ans.first %= MOD;
        ans.second += mat[n][i] * a[i];
        ans.second %= MOD;
    }
    ans.second += ans.first;
    ans.second %= MOD;
    return ans;
}

int main()
{
    int n;
    long long k;
    cin >> n >> k;
    vector<int> a(n);
    for(int i=0; i<n; ++i)
        cin >> a[i];

    pair<long long, long long> ans;
    if(n > 30)
        ans = solve1(a, k);
    else
        ans = solve2(a, k);
    cout << ans.first << ' ' << ans.second << endl;

    return 0;
}
0