結果
| 問題 | 
                            No.613 Solitude by the window
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2017-12-13 16:03:28 | 
| 言語 | Python3  (3.13.1 + numpy 2.2.1 + scipy 1.14.1)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,103 bytes | 
| コンパイル時間 | 282 ms | 
| コンパイル使用メモリ | 12,544 KB | 
| 実行使用メモリ | 11,008 KB | 
| 最終ジャッジ日時 | 2024-12-14 09:59:07 | 
| 合計ジャッジ時間 | 1,780 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 WA * 1 | 
| other | AC * 4 WA * 17 | 
ソースコード
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)