結果

問題 No.718 行列のできるフィボナッチ数列道場 (1)
ユーザー pekempeypekempey
提出日時 2018-07-27 23:56:43
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,132 bytes
コンパイル時間 588 ms
コンパイル使用メモリ 71,888 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-19 03:47:26
合計ジャッジ時間 1,962 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <array>

using namespace std;

const int mod = 1e9 + 7;

struct Modint {
  int n;
  Modint(int n = 0) : n(n) {}
};

Modint operator+(Modint a, Modint b) { return (a.n += b.n) >= mod ? a.n - mod : a.n; }
Modint operator-(Modint a, Modint b) { return (a.n -= b.n) < 0 ? a.n + mod : a.n; }
Modint operator*(Modint a, Modint b) { return 1LL * a.n * b.n % mod; }
Modint &operator+=(Modint &a, Modint b) { return a = a + b; }
Modint &operator-=(Modint &a, Modint b) { return a = a - b; }
Modint &operator*=(Modint &a, Modint b) { return a = a * b; }

// ax + b
using P = pair<Modint, Modint>;

P mul(P p, P q) {
  // p * q = ax^2 + bx + c = a(x + 1) + bx + c
  Modint a = p.first * q.first;
  Modint b = p.first * q.second + p.second * q.first;
  Modint c = p.second * q.second;
  return {a + b, a + c};
}

Modint fib(long long x) {
  P res(0, 1);
  P a(1, 0);
  while (x > 0) {
    if (x & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    x >>= 1;
  }
  return res.first;
}

int main() {
  long long n;
  cin >> n;
  cout << (fib(n) * fib(n + 1)).n << endl;
}
0