結果

問題 No.613 Solitude by the window
ユーザー zimphazimpha
提出日時 2017-12-13 01:33:37
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 857 bytes
コンパイル時間 455 ms
コンパイル使用メモリ 59,572 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 00:05:14
合計ジャッジ時間 1,478 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#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