結果

問題 No.613 Solitude by the window
ユーザー pekempeypekempey
提出日時 2017-12-13 16:05:58
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,226 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 10,896 KB
実行使用メモリ 8,488 KB
最終ジャッジ日時 2023-08-21 02:14:43
合計ジャッジ時間 1,658 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,388 KB
testcase_01 AC 17 ms
8,316 KB
testcase_02 AC 17 ms
8,268 KB
testcase_03 AC 16 ms
8,308 KB
testcase_04 AC 16 ms
8,256 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 16 ms
8,316 KB
testcase_10 AC 17 ms
8,308 KB
testcase_11 AC 16 ms
8,308 KB
testcase_12 AC 16 ms
8,420 KB
testcase_13 AC 16 ms
8,424 KB
testcase_14 AC 16 ms
8,232 KB
testcase_15 AC 16 ms
8,400 KB
testcase_16 AC 16 ms
8,248 KB
testcase_17 AC 16 ms
8,244 KB
testcase_18 AC 16 ms
8,364 KB
testcase_19 AC 16 ms
8,404 KB
testcase_20 AC 16 ms
8,336 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

# OEIS: A003010
# (2 + sqrt(3))^x を展開した際の偶数項目を取り出すと答えが得られるらしい。ほげ
# where x = 2^n
# 
#
#   (2+sqrt(3))^x =   C(x,0) 2^x + C(x,1) 2^(x-1) sqrt(3) +    C(x,2) 2^(x-2) sqrt(3)^2
# + (2-sqrt(3))^x =   C(x,0) 2^x - C(x,1) 2^(x-1) sqrt(3) +    C(x,2) 2^(x-2) sqrt(3)^2
# -------------------------------------------------------------------------------------
#                 =2* C(x,0) 2^x                          + 2* C(x,2) 2^(x-2) sqrt(3)^2
#
# The answer is (2+sqrt(3))^x + (2-sqrt(3))^x

# (a + b sqrt(3)) * (c + d sqrt(3)) = (ac+3bd) + (ad+bc)*sqrt(3)

def mul(a, b, m):
  return (a[0]*b[0] + 3*a[1]*b[1]) % m, (a[0]*b[1] + a[1]*b[0]) % m

def fast_pow(a, n, m):
  ret = (1, 0)
  while n > 0:
    if n % 2 == 1:
      ret = mul(ret, a, m)
    a = mul(a, a, m)
    n //= 2
  return ret

def is_quadratic_residue(a, p):
  return pow(a, (p - 1) // 2, p) == 1

n, m = map(int, input().split())
period = m-1 if is_quadratic_residue(3, m) else m*2-1

x = fast_pow((2, 1),     pow(2, n - 1, period), m)  # The order of F_m(sqrt 3) is either m-1 or m*2-1
y = fast_pow((2, m - 1), pow(2, n - 1, period), m)
z = (x[0] + y[0]) % m
ans = (z**2 - 4) % m

print(ans)
0