結果

問題 No.194 フィボナッチ数列の理解(1)
ユーザー PachicobuePachicobue
提出日時 2018-09-15 03:04:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 11 ms / 5,000 ms
コード長 2,336 bytes
コンパイル時間 5,296 ms
コンパイル使用メモリ 207,636 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-21 11:41:01
合計ジャッジ時間 4,097 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define show(x) std::cerr << #x << " = " << x << std::endl
using ll = long long;
constexpr ll MOD = 1000000007LL;
struct Matrix
{
    Matrix(const std::size_t n) : Matrix{n, n} {}
    Matrix(const std::size_t r, const std::size_t c) : R{r}, C{c}, table(r, std::vector<ll>(c, (ll)0)) {}
    std::vector<ll>& operator[](const std::size_t n) { return table[n]; }
    const std::vector<ll>& operator[](const std::size_t n) const { return table[n]; }
    Matrix operator*(const Matrix& mat) const
    {
        assert(C == mat.R);
        Matrix result(R, mat.C);
        for (std::size_t i = 0; i < R; i++) {
            for (std::size_t j = 0; j < mat.C; j++) {
                for (std::size_t k = 0; k < C; k++) { (result[i][j] += table[i][k] * mat[k][j]) %= MOD; }
            }
        }
        return result;
    }
    static Matrix Unit(const std::size_t n)
    {
        Matrix ans(n, n);
        for (std::size_t i = 0; i < n; i++) { ans[i][i] = 1; }
        return ans;
    }
    std::size_t R, C;
    std::vector<std::vector<ll>> table;
};
Matrix power(const Matrix& mat, const ll n) { return n == 0 ? Matrix::Unit(mat.R) : n % 2 == 1 ? power(mat, n - 1) * mat : power(mat * mat, n / 2); }

int main()
{
    int N;
    ll K;
    std::cin >> N >> K;
    if (N <= 30) {
        std::vector<ll> A(N);
        for (int i = 0; i < N; i++) { std::cin >> A[i]; }
        const ll B = std::accumulate(A.begin(), A.end(), 0LL);
        Matrix mat(N + 1);
        for (int j = 0; j <= N; j++) { mat[0][j] = 1; }
        for (int j = 1; j <= N; j++) { mat[1][j] = 1; }
        for (int i = 2; i <= N; i++) { mat[i][i - 1] = 1; }
        const auto M = power(mat, K - N);
        ll F = 0, S = M[0][0] * B % MOD;
        for (int i = 1; i <= N; i++) { (F += M[1][i] * A[N - i]) %= MOD, (S += M[0][i] * A[N - i]) %= MOD; }
        std::cout << F << " " << S << std::endl;
    } else {
        std::queue<ll> q;
        ll F = 0;
        for (int i = 0; i < N; i++) {
            ll A;
            std::cin >> A, (F += A) %= MOD, q.push(A);
        }
        ll S = F;
        for (ll i = N + 1; i < K; i++) {
            const ll p = q.front();
            q.pop(), q.push(F), (S += F) %= MOD, (F += (MOD - p) + F) %= MOD;
        }
        std::cout << F << " " << (F + S) % MOD << std::endl;
    }
    return 0;
}
0