結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー ctyl_0ctyl_0
提出日時 2015-10-04 17:27:30
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,028 bytes
コンパイル時間 835 ms
コンパイル使用メモリ 95,984 KB
実行使用メモリ 11,280 KB
最終ジャッジ日時 2023-09-27 01:59:36
合計ジャッジ時間 3,327 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>

#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define mod 1000000009
typedef long long ll;

using namespace std;


int inputValue(){
    int a;
    cin >> a;
    return a;
};

template <typename T>
void output(T a, int precision) {
    if(precision > 0){
        cout << setprecision(precision)  << a << "\n";
    }
    else{
        cout << a << "\n";
    }
}

vector<vector<ll>> multi_mat(vector<vector<ll>> a, vector<vector<ll>> b, ll m){
    vector<vector<ll>> ret(a.size(), vector<ll>(a.size(), 0));
    
    rep(i, a.size()){
        rep(j, a.size()){
            rep(p, a.size()){
                ret[i][j] = (ret[i][j] + a[i][p] * b[p][j]) % m;
            }
        }
    }
    
    return ret;
}

vector<vector<ll>> pow_mat(vector<vector<ll>> mat, ll pow, ll m){
    vector<vector<ll>> ret(mat.size(), vector<ll>(mat.size(), 0));
    rep(i, mat.size()){
        ret[i][i] = 1;
    }
    while (pow > 0) {
        if (pow & 1) {
            ret = multi_mat(ret, mat, m);
        }
        mat = multi_mat(mat, mat, m);
        pow >>= 1;
    }
    return ret;
}

int main() {
    
    // source code
    int N = inputValue(); // 10^4, 30
    ll K;
    cin >> K; // 10^6, 10^12
    
    vector<int> A(N); // 9
    rep(i, N){
        A[i] = inputValue();
    }
    
    if (N > 30) { // #1 N > 30
        vector<ll> S(K); // 10^6
        S[0] = A[0];
        repd(i, 1, N){
            S[i] = (A[i] + S[i - 1]) % mod;
        }
        repd(i, N, K){
            if (i == N) {
                S[i] = (2 * S[i - 1]) % mod;
            }
            else{
                S[i] = (2 * S[i - 1] - S[i - (N + 1)]) % mod;
            }
        }
        cout << (S[K - 1] - S[K - 2] + mod) % mod << " ";
        cout << S[K - 1] << endl;// F(K), S(K)
    }
    else{ // #2 K large
        vector<ll> S(N + 1); // 30
        S[0] = A[0];
        repd(i, 1, N){
            S[i] = (A[i] + S[i - 1]) % mod;
        }
        
        
        S[N] = (2 * S[N - 1]) % mod;
        
        if (N >= K) {
            cout << (S[K - 1] - S[K - 2] + mod) % mod << " ";
            cout << S[K - 1] << endl;// F(K), S(K)
            return 0;
        }
        
        // コンパニオン行列の作成
        vector<vector<ll>> mat(N + 1, vector<ll>(N + 1));
        mat[0][0] = 2;
        mat[0][N] = mod - 1;
        repd(i, 1, N + 1){
            mat[i][i - 1] = 1;
        }
        
        vector<vector<ll>> ret = pow_mat(mat, K - (N + 1), mod);
        
        ll Sn = 0;
        ll Sp = 0;
        rep(i, N + 1){
            Sn = (Sn + ret[0][i] * S[N - i]) % mod;
        }
        rep(i, N + 1){
            Sp = (Sp + ret[1][i] * S[N - i]) % mod;
        }
        
        cout << (Sn - Sp + mod) % mod << " ";
        cout << Sn << endl;
    }
    
    
    
    
    
    return 0;
}
0