結果

問題 No.534 フィボナッチフィボナッチ数
ユーザー kimiyukikimiyuki
提出日時 2016-12-18 05:00:11
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 168 ms / 2,000 ms
コード長 1,880 bytes
コンパイル時間 903 ms
コンパイル使用メモリ 77,092 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 00:33:53
合計ジャッジ時間 9,351 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
typedef long long ll;
using namespace std;
template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); }

vector<vector<ll> > mul(vector<vector<ll> > const & a, vector<vector<ll> > const & b, ll m) {
    int n = a.size();
    vector<vector<ll> > c = vectors(n, n, ll());
    repeat (y,n) repeat (z,n) repeat (x,n) c[y][x] = (c[y][x] + a[y][z] * b[z][x] % m) % m;
    return c;
}
vector<ll> mul(vector<vector<ll> > const & a, vector<ll> const & b, ll m) {
    int n = a.size();
    vector<ll> c(n);
    repeat (y,n) repeat (z,n) c[y] = (c[y] + a[y][z] * b[z] % m) % m;
    return c;
}
vector<vector<ll> > unit_matrix(int n) {
    vector<vector<ll> > e = vectors(n, n, ll());
    repeat (i,n) e[i][i] = 1;
    return e;
}
template <typename T, typename F>
T powt(T x, ll y, T unit, F f) {
    T z = unit;
    for (ll i = 1; i <= y; i <<= 1) {
        if (y & i) z = f(z, x);
        x = f(x, x);
    }
    return z;
}
ll fib(ll n, ll m) {
    vector<vector<ll> > f { { 1, 1 }, { 1, 0 } };
    vector<ll> x { 0, 1 };
    return mul(powt(f, n, unit_matrix(2), [&](auto a, auto b) { return mul(a, b, m); }), x, m)[0];
}

const ll mod   = 1000000007;
const ll cycle = 2000000016;

void unittest() {
    for (int i = 0, a = 0, b = 1; i < 10000; swap(a, b), a = (a + b) % mod, ++ i) assert (fib(i, mod) == a);
    for (ll i = cycle - 1000; i < cycle + 1000; ++ i) assert (fib(i, mod) == fib(i % cycle, mod));
}
int main() {
    unittest();
    ll n; cin >> n;
    assert (0 <= n and n <= 1000000000000000000ll);
    cout << fib(fib(n, cycle), mod) << endl;
    return 0;
}
0