結果
問題 | No.194 フィボナッチ数列の理解(1) |
ユーザー | tu-sa |
提出日時 | 2018-07-18 04:19:36 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 10 ms / 5,000 ms |
コード長 | 7,533 bytes |
コンパイル時間 | 2,128 ms |
コンパイル使用メモリ | 179,772 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-28 15:20:24 |
合計ジャッジ時間 | 3,567 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 10 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 5 ms
5,248 KB |
testcase_05 | AC | 4 ms
5,248 KB |
testcase_06 | AC | 5 ms
5,248 KB |
testcase_07 | AC | 7 ms
5,248 KB |
testcase_08 | AC | 3 ms
5,248 KB |
testcase_09 | AC | 5 ms
5,248 KB |
testcase_10 | AC | 3 ms
5,248 KB |
testcase_11 | AC | 3 ms
5,248 KB |
testcase_12 | AC | 4 ms
5,248 KB |
testcase_13 | AC | 3 ms
5,248 KB |
testcase_14 | AC | 2 ms
5,248 KB |
testcase_15 | AC | 8 ms
5,248 KB |
testcase_16 | AC | 7 ms
5,248 KB |
testcase_17 | AC | 3 ms
5,248 KB |
testcase_18 | AC | 7 ms
5,248 KB |
testcase_19 | AC | 9 ms
5,248 KB |
testcase_20 | AC | 2 ms
5,248 KB |
testcase_21 | AC | 10 ms
5,248 KB |
testcase_22 | AC | 2 ms
5,248 KB |
testcase_23 | AC | 3 ms
5,248 KB |
testcase_24 | AC | 6 ms
5,248 KB |
testcase_25 | AC | 6 ms
5,248 KB |
testcase_26 | AC | 5 ms
5,248 KB |
testcase_27 | AC | 6 ms
5,248 KB |
testcase_28 | AC | 3 ms
5,248 KB |
testcase_29 | AC | 9 ms
5,248 KB |
testcase_30 | AC | 9 ms
5,248 KB |
testcase_31 | AC | 2 ms
5,248 KB |
testcase_32 | AC | 4 ms
5,248 KB |
testcase_33 | AC | 5 ms
5,248 KB |
testcase_34 | AC | 4 ms
5,248 KB |
testcase_35 | AC | 4 ms
5,248 KB |
testcase_36 | AC | 7 ms
5,248 KB |
testcase_37 | AC | 2 ms
5,248 KB |
testcase_38 | AC | 9 ms
5,248 KB |
testcase_39 | AC | 5 ms
5,248 KB |
ソースコード
//////////////////////////////////////// /// tu3 pro-con template /// //////////////////////////////////////// #include "bits/stdc++.h" using namespace std; // -- loop macros -- // #define REP(i,n) for (int i = 0; i < (n); i++) #define RREP(i,n) for (int i = (n)-1; i >= 0; i--) #define FOR(i,s,n) for (int i = (int)(s); i < (n); i++) #define RFOR(i,s,n) for (int i = (n)-1; i >= (s); i--) #define FOREACH(i,container) for (auto &&i : container) #define allof(c) c.begin(), c.end() #define partof(c,i,n) c.begin() + (i), c.begin() + (i) + (n) // -- functors -- // #define PREDICATE(t,a,exp) [&](const t & a) -> bool { return exp; } #define COMPARISON(t,a,b,exp) [&](const t & a, const t & b) -> bool { return exp; } #define PRED(a,exp) [&](const auto & a) -> bool { return exp; } #define COMP(a,b,exp) [&](const auto & a, const auto & b) -> bool { return exp; } #define CONV1(a,exp) [&](const auto & a) -> auto { return exp; } #define CONV2(a,b,exp) [&](const auto & a, const auto & b) -> auto { return exp; } #define CONV3(a,b,c,exp) [&](const auto & a, const auto & b, const auto & c) -> auto { return exp; } // -- typedefs -- // #define EPS 1e-9 typedef unsigned int uint; typedef long long llong; typedef unsigned long long ullong; // -- I/O Helper -- // struct _Reader { _Reader(istream &cin) :cin(cin) {} istream &cin; template <class T> _Reader operator ,(T &rhs) { cin >> rhs; return *this; } }; struct _Writer { _Writer(ostream &cout) :cout(cout) {} ostream &cout; bool f{ false }; template <class T> _Writer operator ,(const T &rhs) { cout << (f ? " " : "") << rhs; f = true; return *this; } }; #define READ(t,...) t __VA_ARGS__; (_Reader{cin}), __VA_ARGS__ #define WRITE(...) (_Writer{cout}), __VA_ARGS__; cout << '\n' #define DEBUG(...) (_Writer{cerr}), __VA_ARGS__; cerr << '\n' // -- vevector -- // template <class T> struct vevector : public vector<vector<T>> { vevector(size_t n = 0, size_t m = 0, const T &initial = T()) : vector<vector<T>>(n, vector<T>(m, initial)) { } }; template <class T> struct vevevector : public vector<vevector<T>> { vevevector(size_t n = 0, size_t m = 0, size_t l = 0, const T &initial = T()) : vector<vevector<T>>(n, vevector<T>(m, l, initial)) { } }; template <class T> struct vevevevector : public vector<vevevector<T>> { vevevevector(size_t n = 0, size_t m = 0, size_t l = 0, size_t k = 0, const T &initial = T()) : vector<vevevector<T>>(n, vevevector<T>(m, l, k, initial)) { } }; namespace std { template <class T1, class T2> inline istream & operator >> (istream & in, pair<T1, T2> &p) { in >> p.first >> p.second; return in; } template <class T1, class T2> inline ostream & operator << (ostream &out, const pair<T1, T2> &p) { out << p.first << " " << p.second; return out; } } template <class T> T read() { T t; cin >> t; return t; } template <class T> vector<T> read(int n) { vector<T> v; REP(i, n) { v.push_back(read<T>()); } return v; } template <class T> vevector<T> read(int n, int m) { vevector<T> v; REP(i, n) v.push_back(read<T>(m)); return v; } template <class T> vector<T> readjag() { return read<T>(read<int>()); } template <class T> vevector<T> readjag(int n) { vevector<T> v; REP(i, n) v.push_back(readjag<T>()); return v; } template <class T> struct iter_pair_t { T beg, end; }; template <class T> iter_pair_t<T> iter_pair(T beg, T end) { return iter_pair_t<T>{beg, end}; } template <class T> ostream & operator << (ostream &out, iter_pair_t<T> v) { if (v.beg != v.end) { out << *v.beg++; while (v.beg != v.end) { out << " " << *v.beg++; } } return out; } template <class T1> ostream & operator << (ostream &out, const vector<T1> &v) { return out << iter_pair(begin(v), end(v)); } // -- etc -- // template <class T> T infinity_value(); #define DEFINE_INFINITY_VALUE(T, val) template <> constexpr T infinity_value<T>() { return (val); } DEFINE_INFINITY_VALUE(int, 1 << 28); DEFINE_INFINITY_VALUE(uint, 1u << 28); DEFINE_INFINITY_VALUE(llong, 1ll << 60); DEFINE_INFINITY_VALUE(ullong, 1ull << 60); DEFINE_INFINITY_VALUE(double, HUGE_VAL); DEFINE_INFINITY_VALUE(float, HUGE_VAL); #define INF(T) infinity_value<T>() inline int sign_of(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); } template <class TInt> bool in_range(TInt val, TInt min, TInt max) { return val >= min && val < max; } template <> bool in_range<double>(double val, double min, double max) { return val - min > -EPS && val - max < EPS; } template <> bool in_range<float>(float val, float min, float max) { return val - min > -EPS && val - max < EPS; } template <class TInt> bool in_range2d(TInt x, TInt y, TInt w, TInt h) { return x >= 0 && x < w && y >= 0 && y < h; } vector<int> iotavn(int start, int count) { vector<int> r(count); iota(allof(r), start); return r; } //// start up //// void solve(); int main() { //// for local debugging //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); //auto classic_table = ctype<char>::classic_table(); //vector<ctype<char>::mask> ctable(classic_table, classic_table + ctype<char>::table_size); //ctable[':'] |= ctype_base::space; // as delimitor //ctable['/'] |= ctype_base::space; // as delimitor //cin.imbue(locale(cin.getloc(), new ctype<char>(ctable.data()))); cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed; cout << setprecision(std::numeric_limits<double>::max_digits10); solve(); return 0; } // 初項A の n-bonacci 数列の第k項 // first = F[k] // second = ΣF[k] template <llong mod> pair<llong, llong> nbonacci(vector<llong> A, llong k) { int N = static_cast<int>(A.size()); if (k < N) { return pair<llong, llong>(A[k] % mod, accumulate(partof(A, 0, k + 1), 0ll, CONV2(a, b, (a + b) % mod))); } // O(N^3 * log(k)) N=1e10, k=1e20 で約 6e7 if (N <= 100) { struct Matrix { typedef llong T; vevector<T> value; static Matrix Identity(size_t n) { Matrix e(n, n); REP(i, n) e[i][i] = 1; return e; } Matrix(size_t n, size_t m) : value(n, m) { } vector<T> &operator [](int i) { return value[i]; } const vector<T> &operator [](int i) const { return value[i]; } Matrix operator * (const Matrix &m) const { size_t H = value.size(), X = value[0].size(), W = m[0].size(); assert(X == m.value.size()); Matrix mtx(H, W); REP(i, H) REP(j, W) REP(k, X) { mtx[i][j] = (mtx[i][j] + value[i][k] * m[k][j]) % mod; } return mtx; } Matrix power(llong y) const { size_t X = value.size(); assert(X == value[0].size()); Matrix r = Identity(X); Matrix x = *this; for (; y > 0; y >>= 1) { if (y & 1) { r = r * x; } x = x * x; } return r; } }; Matrix m(N + 1, N + 1); // 1 1 1 ... 1 1 // 0 1 1 ... 1 1 // 0 1 0 ... 0 0 // 0 0 1 ... 0 0 // ... // 0 0 0 ... 1 0 REP(i, N + 1) { m[0][i] = 1; } REP(i, N) { m[1][i + 1] = 1; } REP(i, N - 1) { m[i + 2][i + 1] = 1; } m = m.power(k - N + 1); Matrix v(N + 1, 1); v[0][0] = accumulate(allof(A), 0ll, CONV2(a, b, (a + b) % mod)); REP(i, N) { v[N - i][0] = A[i]; } auto c = m * v; return pair<llong, llong>(c[1][0], c[0][0]); } // O(k) { llong f = accumulate(allof(A), 0ll, CONV2(a, b, (a + b) % mod)); llong s = f; FOR(i, N, k) { llong x = A[i % N]; A[i % N] = f; s = (s + f) % mod; f = (f + f - x + mod) % mod; } return pair<llong, llong>(f, (s + f) % mod); } } //////////////////// /// template end /// //////////////////// void solve() { READ(llong, N, K); vector<llong> A = read<llong>(N); auto ans = nbonacci<1000000007LL>(A, K - 1); WRITE(ans); }