結果

問題 No.613 Solitude by the window
ユーザー pekempey
提出日時 2017-12-13 16:05:58
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 1,226 bytes
コンパイル時間 130 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-12-14 09:59:10
合計ジャッジ時間 1,636 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14 WA * 7
権限があれば一括ダウンロードができます

ソースコード

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