結果

問題 No.3 ビットすごろく
ユーザー mediocreRailmediocreRail
提出日時 2023-08-15 08:16:03
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 36 ms / 5,000 ms
コード長 1,473 bytes
コンパイル時間 91 ms
コンパイル使用メモリ 10,988 KB
実行使用メモリ 9,308 KB
最終ジャッジ日時 2023-08-15 08:16:06
合計ジャッジ時間 2,415 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
8,848 KB
testcase_01 AC 22 ms
8,844 KB
testcase_02 AC 21 ms
8,976 KB
testcase_03 AC 24 ms
8,828 KB
testcase_04 AC 22 ms
8,784 KB
testcase_05 AC 28 ms
8,984 KB
testcase_06 AC 25 ms
9,036 KB
testcase_07 AC 24 ms
9,044 KB
testcase_08 AC 27 ms
8,924 KB
testcase_09 AC 31 ms
9,112 KB
testcase_10 AC 33 ms
9,164 KB
testcase_11 AC 29 ms
9,124 KB
testcase_12 AC 28 ms
8,964 KB
testcase_13 AC 24 ms
9,040 KB
testcase_14 AC 32 ms
9,232 KB
testcase_15 AC 35 ms
9,200 KB
testcase_16 AC 34 ms
9,228 KB
testcase_17 AC 34 ms
9,064 KB
testcase_18 AC 23 ms
8,944 KB
testcase_19 AC 36 ms
9,264 KB
testcase_20 AC 22 ms
8,940 KB
testcase_21 AC 21 ms
8,840 KB
testcase_22 AC 32 ms
9,160 KB
testcase_23 AC 35 ms
9,276 KB
testcase_24 AC 35 ms
9,268 KB
testcase_25 AC 35 ms
9,308 KB
testcase_26 AC 21 ms
8,932 KB
testcase_27 AC 26 ms
8,960 KB
testcase_28 AC 33 ms
9,144 KB
testcase_29 AC 29 ms
9,140 KB
testcase_30 AC 21 ms
8,912 KB
testcase_31 AC 22 ms
8,968 KB
testcase_32 AC 30 ms
8,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect,collections,itertools,math,functools,heapq
import sys
#sys.setrecursionlimit(10**6)
def I(): return int(sys.stdin.readline().rstrip())
def IN(): return int(input())
def LIN(): return list(map(int, input().split()))
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LF(): return list(map(float,sys.stdin.readline().rstrip().split()))
def SI(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

"""
方針
各門で引き返す or 通過するを足して計算する
"""

def popcount(x):
    '''xの立っているビット数をカウントする関数
    (xは64bit整数)'''

    # 2bitごとの組に分け、立っているビット数を2bitで表現する
    x = x - ((x >> 1) & 0x5555555555555555)

    # 4bit整数に 上位2bit + 下位2bit を計算した値を入れる
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)

    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f # 8bitごと
    x = x + (x >> 8) # 16bitごと
    x = x + (x >> 16) # 32bitごと
    x = x + (x >> 32) # 64bitごと = 全部の合計
    return x & 0x0000007f

N=I()
dp = [-1]*(N+1)

stack = collections.deque()
stack.append(1)
dp[1] = 1
while stack:
    v = stack.popleft()
    p = popcount(v)
    if v+p <= N and dp[v+p] < 0:
        dp[v+p] = dp[v]+1
        stack.append(v+p)
    if v-p >= 0 and dp[v-p] < 0 :
        dp[v-p] = dp[v] + 1
        stack.append(v-p)
    
print(dp[N])
0