結果
問題 | No.194 フィボナッチ数列の理解(1) |
ユーザー | risujiroh |
提出日時 | 2019-03-17 05:55:45 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 152 ms / 5,000 ms |
コード長 | 3,848 bytes |
コンパイル時間 | 1,461 ms |
コンパイル使用メモリ | 176,612 KB |
実行使用メモリ | 7,168 KB |
最終ジャッジ日時 | 2024-07-05 19:02:06 |
合計ジャッジ時間 | 3,144 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
#include <bits/stdc++.h>using namespace std;using lint = long long;template<class T = int> using V = vector<T>;template<class T = int> using VV = V< V<T> >;template<unsigned P> struct ModInt {using M = ModInt;unsigned v;ModInt() : v(0) {}template<class Z> ModInt(Z x) : v(x >= 0 ? x % P : -x % P ? P - -x % P : 0) {}constexpr ModInt(unsigned v, int) : v(v) {}static constexpr unsigned p() { return P; }M operator+() const { return *this; }M operator-() const { return {v ? P - v : 0, 0}; }explicit operator bool() const noexcept { return v; }bool operator!() const noexcept { return !(bool) *this; }M operator*(M r) const { return M(*this) *= r; }M operator/(M r) const { return M(*this) /= r; }M operator+(M r) const { return M(*this) += r; }M operator-(M r) const { return M(*this) -= r; }bool operator==(M r) const { return v == r.v; }bool operator!=(M r) const { return !(*this == r); }M& operator*=(M r) { v = (uint64_t) v * r.v % P; return *this; }M& operator/=(M r) { return *this *= r.inv(); }M& operator+=(M r) { v = r.v < P - v ? v + r.v : v - (P - r.v); return *this; }M& operator-=(M r) { v = r.v <= v ? v - r.v : v + (P - r.v); return *this; }M inv() const {int a = v, b = P, x = 1, u = 0;while (b) {int q = a / b;swap(a -= q * b, b);swap(x -= q * u, u);}assert(a == 1);return x;}template<class Z> M pow(Z n) const {n = n >= 0 ? n % (P - 1) : P - 1 - -n % (P - 1);M res = 1;for (M a = *this; n; a *= a, n >>= 1) if (n & 1) res *= a;return res;}template<class Z> friend M operator*(Z l, M r) { return M(l) *= r; }template<class Z> friend M operator/(Z l, M r) { return M(l) /= r; }template<class Z> friend M operator+(Z l, M r) { return M(l) += r; }template<class Z> friend M operator-(Z l, M r) { return M(l) -= r; }friend ostream& operator<<(ostream& os, M r) { return os << r.v; }friend istream& operator>>(istream& is, M& r) { lint x; is >> x; r = x; return is; }template<class Z> friend bool operator==(Z l, M r) { return M(l) == r; }template<class Z> friend bool operator!=(Z l, M r) { return !(l == r); }};using Mint = ModInt<(unsigned) 1e9 + 7>;template<class T> VV<T> operator*(const VV<T>& A, const VV<T>& B) {assert(A[0].size() == B.size());VV<T> res(A.size(), V<T>(B[0].size()));for (int i = 0; i < A.size(); ++i) for (int k = 0; k < A[0].size(); ++k) for (int j = 0; j < B[0].size(); ++j) {res[i][j] += A[i][k] * B[k][j];}return res;}template<class T> VV<T>& operator*=(VV<T>& A, const VV<T>& B) { return A = A * B; }template<class T> VV<T> I(int n) {VV<T> res(n, V<T>(n));for (int i = 0; i < n; ++i) res[i][i] = 1;return res;}template<class T> VV<T> pow(VV<T> A, lint n) {auto res = I<T>(A.size());while (n) {if (n & 1) res *= A;A *= A;n >>= 1;}return res;}int main() {cin.tie(nullptr); ios::sync_with_stdio(false);lint n, k; cin >> n >> k;V<Mint> a(n); for (auto&& e : a) cin >> e;if (k <= 1e6) {a.resize(k);for (int i = n; i < 2 * n; ++i) {for (int j = 1; j <= n; ++j) {a[i] += a[i - j];}}for (int i = 2 * n; i < k; ++i) {a[i] = 2 * a[i - 1] - (i - n - 1 >= 0 ? a[i - n - 1] : 0);}Mint res = accumulate(begin(a), end(a), Mint(0));cout << a.back() << ' ' << res << '\n';} else {VV<Mint> A(n + 1, V<Mint>(n + 1));for (int j = 0; j < n; ++j) {A[0][j] = 1;if (j + 1 < n) A[j + 1][j] = 1;}A[n][n - 1] = A[n][n] = 1;A = pow(A, k - 1);Mint res;for (int j = 0; j < n; ++j) {res += A[n - 1][j] * a[n - 1 - j];}Mint sum = res;for (int j = 0; j < n; ++j) {sum += A[n][n - 1 - j] * a[j];}cout << res << ' ' << sum << '\n';}}