結果
| 問題 | No.3 ビットすごろく | 
| コンテスト | |
| ユーザー |  lllllll88938494 | 
| 提出日時 | 2022-01-21 11:21:46 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 97 ms / 5,000 ms | 
| コード長 | 530 bytes | 
| コンパイル時間 | 128 ms | 
| コンパイル使用メモリ | 12,544 KB | 
| 実行使用メモリ | 11,264 KB | 
| 最終ジャッジ日時 | 2024-11-25 05:28:15 | 
| 合計ジャッジ時間 | 3,058 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 33 | 
ソースコード
import sys
sys.setrecursionlimit(10**6)
n=int(input())
ns=[]
for i in range(n):
    ns.append(str(bin(i+1)).count('1'))
inf=10**7
dist=[inf]*n
ans=[10**10]
def  dfs(x,cnt):
    dist[x] = cnt
    if x == n-1:
        ans[0] = min(ans[0],cnt)
        return
    
    if x+ns[x] < n:
        if dist[x+ns[x]] > cnt+1:
            dfs(x+ns[x],cnt+1)
            
    if x-ns[x] >= 0:
        if dist[x-ns[x]] > cnt+1:
            dfs(x-ns[x],cnt+1)
dfs(0,0)
if ans[0] == 10**10:
    print(-1)
else:
    print(ans[0]+1)
            
            
            
            
        