結果

問題 No.1458 Segment Function
ユーザー lam6er
提出日時 2025-03-20 21:15:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 1,745 bytes
コンパイル時間 222 ms
コンパイル使用メモリ 82,244 KB
実行使用メモリ 77,756 KB
最終ジャッジ日時 2025-03-20 21:15:41
合計ジャッジ時間 3,149 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

def get_segment(c):
    return {
        '0': 6,
        '1': 2,
        '2': 5,
        '3': 5,
        '4': 4,
        '5': 5,
        '6': 6,
        '7': 4,
        '8': 7,
        '9': 6
    }[c]

def calculate_f_positive(x_str):
    x_str = x_str.lstrip('0')
    if not x_str:
        return 6
    total = 0
    for c in x_str:
        total += get_segment(c)
    return total

def f(x_str):
    if not x_str:
        return 0
    if x_str[0] == '-':
        abs_part = x_str[1:]
        stripped = abs_part.lstrip('0')
        if not stripped:
            return 1 + 6  # Handle negative zero case as f(0)
        else:
            return 1 + calculate_f_positive(stripped)
    else:
        return calculate_f_positive(x_str)

def main():
    import sys
    input = sys.stdin.read().split()
    P = input[0]
    N_str = input[1]

    if N_str == '0':
        print(P)
        return

    current = P
    max_steps = 100  # Default to max 100 steps if N is large
    if len(N_str) <= len(str(max_steps)):
        n = int(N_str)
        max_steps = min(n, max_steps)

    for _ in range(max_steps):
        next_val = f(current)
        # Convert current to its normalized form (strip leading zeros and handle negatives)
        is_negative = current.startswith('-')
        abs_part = current[1:] if is_negative else current
        stripped_abs = abs_part.lstrip('0')
        if stripped_abs == '':
            stripped_abs = '0'
        current_normalized = ('-' + stripped_abs) if is_negative and stripped_abs != '0' else stripped_abs
        next_normalized = str(next_val)
        if current_normalized == next_normalized:
            break
        current = next_normalized

    print(current)

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