結果

問題 No.3 ビットすごろく
ユーザー fgshunfgshun
提出日時 2020-09-27 00:32:01
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 887 bytes
コンパイル時間 427 ms
コンパイル使用メモリ 10,936 KB
実行使用メモリ 9,264 KB
最終ジャッジ日時 2023-09-12 13:12:45
合計ジャッジ時間 2,683 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,532 KB
testcase_01 AC 19 ms
8,564 KB
testcase_02 AC 18 ms
8,640 KB
testcase_03 AC 22 ms
8,616 KB
testcase_04 WA -
testcase_05 AC 28 ms
8,800 KB
testcase_06 AC 23 ms
8,644 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 28 ms
8,896 KB
testcase_13 AC 21 ms
8,572 KB
testcase_14 AC 33 ms
8,992 KB
testcase_15 AC 38 ms
9,188 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 18 ms
8,648 KB
testcase_22 AC 34 ms
9,072 KB
testcase_23 AC 39 ms
9,100 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 18 ms
8,532 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

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.append((f, cost + 1))
        b = pos - p
        if b > 0:
            costs[b] = min(costs[b], cost + 1)
            q.append((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