結果

問題 No.262 面白くないビットすごろく
ユーザー maspymaspy
提出日時 2020-04-08 20:45:54
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,107 bytes
コンパイル時間 227 ms
コンパイル使用メモリ 11,004 KB
実行使用メモリ 9,776 KB
最終ジャッジ日時 2023-09-26 11:01:50
合計ジャッジ時間 1,001 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,832 KB
testcase_01 AC 26 ms
9,680 KB
testcase_02 AC 26 ms
9,656 KB
testcase_03 AC 26 ms
9,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from functools import lru_cache

popcount = [0]
for _ in range(10):
    popcount = popcount + [x + 1 for x in popcount]


@lru_cache(None)
def simu(B, n, add):
    assert n < (1 << B)
    if B < 10:
        step = 0
        while n < (1 << B):
            n += popcount[n] + add
            step += 1
        return step, n
    step, n = simu(B - 1, n, add)
    m = n - (1 << (B - 1))
    step1, n = simu(B - 1, m, add + 1)
    n += 1 << (B - 1)
    return step + step1, n


def simu_naive(B, n, add):
    step = 0
    while n < 1 << B:
        n += bin(n).count('1') + add
        step += 1
    return step, n


N = int(read())
add = 0
total_step = 1
n = 1
for B in range(60, 10, -1):
    if N - n < (1 << B):
        continue
    q, r = divmod(n, 1 << B)
    step, next_n = simu(B, r, bin(q).count('1'))
    n = (q << B) + next_n
    total_step += step

while n < N:
    total_step += 1
    n += bin(n).count('1')

answer = -1 if n != N else total_step
print(answer)
0