結果

問題 No.642 Two Operations No.1
コンテスト
ユーザー LyricalMaestro
提出日時 2026-04-12 02:25:45
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 693 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 186 ms
コンパイル使用メモリ 85,248 KB
実行使用メモリ 54,016 KB
最終ジャッジ日時 2026-04-12 02:25:52
合計ジャッジ時間 7,125 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

## https://yukicoder.me/problems/no/642

from collections import deque

def main():
    N = int(input())
    if N == 1:
        print(0)
        return

    dp = {N: 0}
    queue = deque()
    queue.append(N)
    while len(queue) > 0:
        n = queue.popleft()

        d = dp[n]
        n1 = n + 1
        if n1 not in dp:
            dp[n1] = d + 1
            if n1 == 1:
                break
            queue.append(n1)

        if n % 2 == 0:
            n2 = n // 2

            if n2 not in dp:
                dp[n2] = d + 1
                if n2 == 1:
                    break
                queue.append(n2)
    
    print(dp[1])
    





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