結果

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

ソースコード

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

n, m = map(int, input().split())

x = fast_pow((2, 1),     pow(2, n - 1, m**2 - 1), 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, m**2 - 1), m)
z = (x[0] + y[0]) % m
ans = (z**2 - 4) % m

print(ans)
0