結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-04-08 05:38:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 5,000 ms
コード長 2,788 bytes
コンパイル時間 1,973 ms
コンパイル使用メモリ 173,744 KB
実行使用メモリ 11,076 KB
最終ジャッジ日時 2024-04-19 07:15:01
合計ジャッジ時間 3,401 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<P> vp;
typedef vector<ll> vll;

typedef vector<ll> Vec;
typedef vector<Vec> Mat;

const ll mod = 1e9 + 7;

Mat mulMatMat(Mat A, Mat B) {
    int n = A.size();
    Mat C(n, Vec(n, 0));

    rep(i, n) {
        rep(j, n) {
            rep(k, n) {
                C[i][j] += A[i][k] * B[k][j];
                C[i][j] %= mod;
            }
        }
    }

    return C;
}

Vec mulMatVec(Mat A, Vec vec) {
    int n = A.size();
    Vec res(n, 0);
    reverse(all(vec));  // 添え字の大きい要素を上に持ってくるため

    rep(i, n) {
        rep(j, n) {
            res[i] += A[i][j] * vec[j];
            res[i] %= mod;
        }
    }

    return res;
}

Mat powMat(Mat A, ll p) {
    int n = A.size();
    Mat R(n, Vec(n, 0));
    rep(i, n) R[i][i] = 1;  // 単位行列

    for (; p >= 1; p /= 2) {
        if (p & 1) {
            R = mulMatMat(A, R);
        }
        A = mulMatMat(A, A);
    }

    return R;
}

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

    ll N, K;
    cin >> N >> K;
    vll a(N);
    rep(i, N) cin >> a[i];

    if (N <= (ll)1e4 && K <= (ll)1e6) {
        vll f(K);
        ll t = 0;
        rep(i, N) {
            f[i] = a[i];
            t += f[i];
            t %= mod;
        }

        rep2(i, N, K) {
            f[i] = t;
            t = (t + mod - f[i - N]) % mod;
            t += f[i];
            t %= mod;
        }

        ll s = 0;
        rep(i, K) {
            s += f[i];
            s %= mod;
        }

        cout << f[K - 1] << " " << s << endl;

    } else {
        // Sを行列累乗で計算
        Mat A(N + 1, Vec(N + 1, 0));
        A[0][0] = 2;
        A[0][N] = -1 + mod;
        rep2(i, 1, N + 1) A[i][i-1] = 1;

        // 初期ベクトル s を計算しておく
        Vec f(N + 1, 0);
        rep(i, N) f[i] = a[i];
        rep(i, N) f[N] += f[i];

        Vec s(N + 1, 0);
        rep(i, N + 1) s[i] = f[i];
        rep(i, N) s[i + 1] += s[i];

        A = powMat(A, K - (N + 1));
        s = mulMatVec(A, s);     // s[k], s[k-1], ...

        cout << (s[0] - s[1] + mod) % mod << " " << s[0] << endl;

    }

}


// // Fを直接計算する場合
// Mat A(N, Vec(N, 0));
// rep(i, N) A[0][i] = 1;
// rep2(i, 1, N) A[i][i - 1] = 1;
//
// auto B = powMat(A, K - N);
// auto vec = mulMatVec(B, a);
// ll fk = vec[0];

// for (auto x : vec) {
//     cout << x << " ";
// }
// cout << endl;
//
// cout << fk << endl;
0