結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,836 KB
testcase_01 AC 16 ms
7,836 KB
testcase_02 AC 16 ms
7,792 KB
testcase_03 AC 67 ms
7,852 KB
testcase_04 AC 21 ms
7,860 KB
testcase_05 AC 253 ms
8,312 KB
testcase_06 AC 77 ms
8,328 KB
testcase_07 AC 37 ms
7,904 KB
testcase_08 AC 167 ms
8,320 KB
testcase_09 AC 410 ms
8,308 KB
testcase_10 AC 582 ms
8,484 KB
testcase_11 AC 347 ms
8,444 KB
testcase_12 AC 240 ms
8,284 KB
testcase_13 AC 52 ms
7,844 KB
testcase_14 AC 544 ms
8,492 KB
testcase_15 AC 823 ms
8,472 KB
testcase_16 AC 714 ms
8,424 KB
testcase_17 AC 805 ms
8,528 KB
testcase_18 AC 42 ms
7,840 KB
testcase_19 AC 842 ms
8,424 KB
testcase_20 AC 19 ms
7,788 KB
testcase_21 AC 16 ms
7,792 KB
testcase_22 AC 559 ms
8,476 KB
testcase_23 AC 843 ms
8,448 KB
testcase_24 AC 842 ms
8,512 KB
testcase_25 AC 827 ms
8,424 KB
testcase_26 AC 15 ms
7,792 KB
testcase_27 AC 59 ms
7,764 KB
testcase_28 AC 685 ms
8,508 KB
testcase_29 AC 358 ms
8,356 KB
testcase_30 AC 16 ms
7,784 KB
testcase_31 AC 16 ms
7,864 KB
testcase_32 AC 304 ms
8,200 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