結果

問題 No.613 Solitude by the window
コンテスト
ユーザー zimpha
提出日時 2017-12-13 01:33:37
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 857 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 540 ms
コンパイル使用メモリ 73,856 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-06-06 19:52:53
合計ジャッジ時間 1,812 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <cstdio>
#include <vector>
#include <bitset>

using int64 = long long;

int64 pow_mod(int64 a, int64 n, int64 mod) {
  int64 r = 1;
  for (; n; n >>= 1) {
    if (n & 1) r = r * a % mod;
    a = a * a % mod;
  }
  return r;
}

int64 calc(int64 n, int64 mod) {
  // (2 + sqrt{3}) ^ (2^n) % mod
  if (pow_mod(3, mod / 2, mod) == 1) {
    n = pow_mod(2, n, mod - 1);
  } else {
    n = pow_mod(2, n, mod + 1);
  }
  int64 a = 1, b = 0;
  int64 c = 2, d = 1;
  for (; n; n >>= 1) {
    if (n & 1) {
      int64 u = (a * c + 3 * b * d) % mod;
      int64 v = (a * d + b * c) % mod;
      a = u, b = v;
    }
    int64 u = (c * c + d * d * 3) % mod;
    int64 v = 2 * c * d % mod;
    c = u, d = v;
  }
  return a;
}

int main() {
  int64 n, m;
  scanf("%lld%lld", &n, &m);
  int64 r = calc(n, m) * 2 % m + m - 2;
  printf("%lld\n", r % m);
  return 0;
}
0