結果

問題 No.3 ビットすごろく
ユーザー m4tsum4tsu
提出日時 2018-06-15 21:12:51
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 846 ms / 5,000 ms
コード長 1,192 bytes
コンパイル時間 109 ms
コンパイル使用メモリ 10,952 KB
実行使用メモリ 8,548 KB
最終ジャッジ日時 2023-09-14 01:03:17
合計ジャッジ時間 12,055 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
7,912 KB
testcase_01 AC 16 ms
7,808 KB
testcase_02 AC 16 ms
7,860 KB
testcase_03 AC 66 ms
7,764 KB
testcase_04 AC 22 ms
7,804 KB
testcase_05 AC 252 ms
8,320 KB
testcase_06 AC 78 ms
8,308 KB
testcase_07 AC 37 ms
7,792 KB
testcase_08 AC 167 ms
8,276 KB
testcase_09 AC 412 ms
8,372 KB
testcase_10 AC 582 ms
8,392 KB
testcase_11 AC 347 ms
8,340 KB
testcase_12 AC 240 ms
8,308 KB
testcase_13 AC 52 ms
7,840 KB
testcase_14 AC 543 ms
8,404 KB
testcase_15 AC 827 ms
8,548 KB
testcase_16 AC 714 ms
8,448 KB
testcase_17 AC 803 ms
8,528 KB
testcase_18 AC 43 ms
7,844 KB
testcase_19 AC 844 ms
8,448 KB
testcase_20 AC 19 ms
7,836 KB
testcase_21 AC 16 ms
7,860 KB
testcase_22 AC 557 ms
8,476 KB
testcase_23 AC 845 ms
8,512 KB
testcase_24 AC 846 ms
8,508 KB
testcase_25 AC 829 ms
8,444 KB
testcase_26 AC 16 ms
7,804 KB
testcase_27 AC 59 ms
7,764 KB
testcase_28 AC 687 ms
8,504 KB
testcase_29 AC 358 ms
8,288 KB
testcase_30 AC 16 ms
7,760 KB
testcase_31 AC 17 ms
7,800 KB
testcase_32 AC 307 ms
8,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def bit_count(n): #自然数nを二進数にしたときの1のbit数
    bit = 0
    while 1:
        if n == 1:
            return 1
            
        q, mod = divmod(n, 2)
        if mod == 1:
            bit += 1
            
        if q == 1:
            bit += 1
            break
        else:
            n = q
        
        
    
    return bit


#処理
def sugoroku(N):
    if N == 1:
        return 1
    step = 1 #求める移動数
    pos = 1 #今の場所
    visited =[1] #今までにたどりつけた地点
    frontier =[1]
    
    while frontier:
        new_frontier = []
        step += 1
        for pos in frontier:
            d = bit_count(pos) #移動距離
            if pos + d == N or pos - d == N: #Nに到達したら終わり
                return step
            p1 = pos + d
            p2 = pos - d
            
            if p1 < N and p1 not in visited:
                visited.append(p1)
                new_frontier.append(p1)
            if p2 > 1 and p2 not in visited:
                visited.append(p2)
                new_frontier.append(p2)
        frontier = new_frontier #更新
    return -1

N = int(input())

print(sugoroku(N))
0