結果

問題 No.2122 黄金比で擬似乱数生成
ユーザー lam6er
提出日時 2025-04-15 22:02:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,672 bytes
コンパイル時間 424 ms
コンパイル使用メモリ 81,812 KB
実行使用メモリ 54,852 KB
最終ジャッジ日時 2025-04-15 22:03:26
合計ジャッジ時間 2,418 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

def compute_V(n, exponent, mod):
    if exponent == 0:
        return 0
    if exponent == 1:
        return 1 % mod
    
    def multiply(a, b):
        return [
            [(a[0][0]*b[0][0] + a[0][1]*b[1][0]) % mod,
             (a[0][0]*b[0][1] + a[0][1]*b[1][1]) % mod],
            [(a[1][0]*b[0][0] + a[1][1]*b[1][0]) % mod,
             (a[1][0]*b[0][1] + a[1][1]*b[1][1]) % mod]
        ]
    
    def matrix_power(matrix, power):
        result = [[1, 0], [0, 1]]
        while power > 0:
            if power % 2 == 1:
                result = multiply(result, matrix)
            matrix = multiply(matrix, matrix)
            power //= 2
        return result
    
    matrix = [[n % mod, 1], [1, 0]]
    power = exponent - 1
    mat = matrix_power(matrix, power)
    V = (mat[0][0] * 1 + mat[0][1] * 0) % mod
    return V

def solve():
    S = input().strip()
    M = int(input())
    L = int(input())
    
    current_s = S
    memo = {}
    step = 0
    remaining = L
    
    while remaining > 0:
        if current_s in memo:
            prev_step = memo[current_s]
            cycle_len = step - prev_step
            if cycle_len == 0:
                break
            remaining %= cycle_len
            if remaining == 0:
                break
        memo[current_s] = step
        n = int(current_s)
        mod = 10000
        if M == 0:
            v = 0
        else:
            v = compute_V(n, M, mod)
        integer_part = (v - (M % 2)) % mod
        integer_part = (integer_part + mod) % mod
        current_s = f"{integer_part:04d}"
        remaining -= 1
        step += 1
    
    print(current_s)

if __name__ == "__main__":
    solve()
0