結果

問題 No.3 ビットすごろく
ユーザー fgshunfgshun
提出日時 2020-09-27 08:10:57
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 38 ms / 5,000 ms
コード長 895 bytes
コンパイル時間 94 ms
コンパイル使用メモリ 10,884 KB
実行使用メモリ 8,968 KB
最終ジャッジ日時 2023-09-14 01:51:49
合計ジャッジ時間 2,411 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,524 KB
testcase_01 AC 20 ms
8,608 KB
testcase_02 AC 21 ms
8,500 KB
testcase_03 AC 25 ms
8,672 KB
testcase_04 AC 20 ms
8,564 KB
testcase_05 AC 29 ms
8,712 KB
testcase_06 AC 24 ms
8,508 KB
testcase_07 AC 21 ms
8,528 KB
testcase_08 AC 26 ms
8,776 KB
testcase_09 AC 32 ms
8,740 KB
testcase_10 AC 34 ms
8,928 KB
testcase_11 AC 31 ms
8,740 KB
testcase_12 AC 28 ms
8,612 KB
testcase_13 AC 22 ms
8,588 KB
testcase_14 AC 34 ms
8,944 KB
testcase_15 AC 38 ms
8,928 KB
testcase_16 AC 36 ms
8,960 KB
testcase_17 AC 37 ms
8,868 KB
testcase_18 AC 21 ms
8,628 KB
testcase_19 AC 38 ms
8,908 KB
testcase_20 AC 20 ms
8,520 KB
testcase_21 AC 19 ms
8,600 KB
testcase_22 AC 34 ms
8,780 KB
testcase_23 AC 38 ms
8,864 KB
testcase_24 AC 38 ms
8,880 KB
testcase_25 AC 37 ms
8,880 KB
testcase_26 AC 19 ms
8,456 KB
testcase_27 AC 23 ms
8,700 KB
testcase_28 AC 35 ms
8,968 KB
testcase_29 AC 31 ms
8,800 KB
testcase_30 AC 19 ms
8,624 KB
testcase_31 AC 19 ms
8,496 KB
testcase_32 AC 30 ms
8,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import itertools
import collections

def popcount(i):
    c = 0
    while i > 0:
        i, m = divmod(i, 2)
        if m:
            c += 1

    return c


def resolve(in_):
    N = int(next(in_))
    INF = 10 ** 9
    costs = [INF] * (N + 1)
    costs[1] = 1
    used = [False] * (N + 1)
    q = collections.deque()
    q.append((1, 1))

    while q:
        pos, cost = q.pop()
        if used[pos]:
            continue
        used[pos] = True
        p = popcount(pos)
        f = pos + p
        if f <= N:
            costs[f] = min(costs[f], cost + 1)
            q.appendleft((f, cost + 1))
        b = pos - p
        if b > 0:
            costs[b] = min(costs[b], cost + 1)
            q.appendleft((b, cost + 1))
    
    return costs[N] if costs[N] != INF else -1


def main():
    ans = resolve(sys.stdin.buffer)
    print(ans)


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