結果
| 問題 |
No.194 フィボナッチ数列の理解(1)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-09-15 03:04:31 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 23 ms / 5,000 ms |
| コード長 | 2,336 bytes |
| コンパイル時間 | 1,888 ms |
| コンパイル使用メモリ | 203,976 KB |
| 最終ジャッジ日時 | 2025-01-06 13:24:04 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
ソースコード
#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;
}