結果
| 問題 |
No.534 フィボナッチフィボナッチ数
|
| コンテスト | |
| ユーザー |
Min_25
|
| 提出日時 | 2017-07-01 19:04:33 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 3,682 bytes |
| コンパイル時間 | 1,025 ms |
| コンパイル使用メモリ | 92,492 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-10-04 23:59:34 |
| 合計ジャッジ時間 | 2,194 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 42 |
ソースコード
#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <stack>
#include <queue>
#include <tuple>
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#define _rep(_1, _2, _3, _4, name, ...) name
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define rep4(i, a, b, c) for (int i = int(a); i < int(b); i += int(c))
#define rep(...) _rep(__VA_ARGS__, rep4, rep3, rep2, _)(__VA_ARGS__)
using namespace std;
using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f80 = long double;
int get_int() {
int c, n;
while ((c = getchar()) < '0');
n = c - '0';
while ((c = getchar()) >= '0') n = n * 10 + (c - '0');
return n;
}
template <u64 Modulus>
struct Mod64 {
using u128 = __uint128_t;
static constexpr u64 mul_inv(u64 x, int e=6, u64 inv=1) {
return (e == 0) ? inv : mul_inv(x, e - 1, inv * (2 - x * inv));
}
static constexpr int shift = __builtin_ctzll(Modulus);
static constexpr u64 mask = (u64(1) << shift) - 1;
static constexpr u64 one = u64(1) << shift | 1;
static constexpr u64 mod = Modulus >> shift;
static constexpr u64 r2 = -u128(mod) % mod;
static constexpr u64 inv = mul_inv(mod);
Mod64() : n_(0) {}
Mod64(u64 n) : n_(init(n)) {}
static u64 modulus() { return Modulus; }
static u64 init(u64 x) {
return reduce_odd(u128(x) * r2) << shift | (x & mask);
}
static u64 reduce_odd(u128 x) {
u64 y = u64(x >> 64) - u64((u128(u64(x) * inv) * mod) >> 64);
return i64(y) < 0 ? y + mod : y;
}
static u64 reduce(u64 x0, u64 x1) {
u64 y = reduce_odd(u128(x0 >> shift) * (x1 >> shift));
return y << shift | ((x0 * x1) & mask);
}
Mod64& operator += (Mod64 rhs) {
u64 hi = (n_ >> shift) + (rhs.n_ >> shift) - mod;
if (i64(hi) < 0) hi += mod;
n_ = hi << shift | ((n_ + rhs.n_) & mask);
return *this;
}
Mod64& operator -= (Mod64 rhs) {
u64 hi = (n_ >> shift) - (rhs.n_ >> shift);
if (i64(hi) < 0) hi += mod;
n_ = hi << shift | ((n_ - rhs.n_) & mask);
return *this;
}
Mod64& operator *= (Mod64 rhs) { n_ = reduce(n_, rhs.n_); return *this; }
Mod64 operator + (Mod64 rhs) const { return Mod64(*this) += rhs; }
Mod64 operator - (Mod64 rhs) const { return Mod64(*this) -= rhs; }
Mod64 operator * (Mod64 rhs) const { return Mod64(*this) *= rhs; }
u64 get() const {
u64 ret = reduce(n_, one);
u64 r1 = ret >> shift;
return mod * (((ret - r1) * inv) & mask) + r1;
}
void set(u64 n) { n_ = n; }
Mod64 pow(u64 exp) const {
Mod64 ret = one, base = *this;
for (; exp; exp >>= 1, base *= base) {
if (exp & 1) ret *= base;
}
return ret;
}
Mod64 inverse() const { return pow(mod - 2); }
friend ostream& operator << (ostream& os, const Mod64& m) { return os << m.get(); }
u64 n_;
};
template <typename Mod>
Mod fib(u64 n) {
if (n <= 2) return Mod((n + 1) >> 1);
Mod a = Mod(1), b = a;
for (u64 mask = u64(1) << __lg(n >> 1); mask; mask >>= 1) {
Mod t = a * a + b * b;
if (n & mask) {
b *= a + a + b, a = t;
} else {
a *= b + b - a, b = t;
}
}
return a;
}
void solve() {
const u64 mod = 1e9 + 7;
using Mod1 = Mod64<mod * mod>;
using Mod2 = Mod64<mod * mod - 1>;
u64 n;
while (~scanf("%llu", &n)) {
u64 ans = fib<Mod1>(fib<Mod2>(n).get()).get();
printf("%llu\n", ans % mod);
}
}
int main() {
clock_t beg = clock();
solve();
clock_t end = clock();
fprintf(stderr, "%.3f sec\n", double(end - beg) / CLOCKS_PER_SEC);
return 0;
}
Min_25