結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,004 KB
testcase_01 AC 16 ms
8,000 KB
testcase_02 AC 16 ms
8,000 KB
testcase_03 AC 16 ms
7,928 KB
testcase_04 AC 16 ms
7,944 KB
testcase_05 AC 16 ms
8,032 KB
testcase_06 AC 16 ms
8,004 KB
testcase_07 AC 16 ms
7,960 KB
testcase_08 AC 16 ms
7,988 KB
testcase_09 AC 16 ms
7,948 KB
testcase_10 AC 16 ms
7,960 KB
testcase_11 AC 16 ms
8,028 KB
testcase_12 AC 16 ms
7,968 KB
testcase_13 AC 16 ms
7,940 KB
testcase_14 AC 16 ms
8,032 KB
testcase_15 AC 16 ms
8,036 KB
testcase_16 AC 17 ms
8,036 KB
testcase_17 AC 16 ms
8,032 KB
testcase_18 AC 16 ms
7,900 KB
testcase_19 AC 16 ms
8,028 KB
testcase_20 AC 16 ms
8,028 KB
testcase_21 AC 16 ms
7,900 KB
testcase_22 AC 17 ms
8,000 KB
testcase_23 AC 16 ms
7,984 KB
権限があれば一括ダウンロードができます

ソースコード

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