結果

問題 No.344 ある無理数の累乗
ユーザー maspy
提出日時 2020-01-01 14:50:02
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 847 bytes
コンパイル時間 150 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-11-22 04:49:21
合計ジャッジ時間 2,216 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

MOD = 1000

class Qint():
    def __init__(self,a,b):
        self.x = a
        self.y = b
    def __mul__(self,other):
        x = self.x * other.x + 3 * self.y * other.y
        y = self.x * other.y + self.y * other.x
        return Qint(x%MOD,y%MOD)
    def __repr__(self):
        return '({}) + ({})sqrt 3'.format(self.x, self.y)
    def __pow__(self,n):
        if n == 0:
            return Qint(1,0)
        x = self ** (n//2)
        x *= x
        return self * x if n & 1 else x

def f(N):
    q = Qint(1,1) ** N
    x = q.x * 2 % MOD
    if N == 0:
        return 1
    elif N & 1:
        return x
    else:
        return (x - 1) % MOD

# f(0), f(2), f(12), f(123456789)

N = int(read())
answer = f(N)
print(answer)
0