結果
問題 |
No.3 ビットすごろく
|
ユーザー |
![]() |
提出日時 | 2020-03-29 09:11:26 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 45 ms / 5,000 ms |
コード長 | 609 bytes |
コンパイル時間 | 197 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 11,136 KB |
最終ジャッジ日時 | 2024-07-01 09:43:55 |
合計ジャッジ時間 | 2,270 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
import sys input=lambda: sys.stdin.readline().rstrip() n=int(input()) inf=float("inf") Dist=[inf]*(n+1) def popcount(x): x=x-((x>>1)&0x5555555555555555) x=(x&0x3333333333333333)+((x>>2)&0x3333333333333333) x=(x+(x>>4))&0x0f0f0f0f0f0f0f0f x=x+(x>>8) x=x+(x>>16) x=x+(x>>32) return x&0x0000007f Dist[1]=1 import collections S=collections.deque() S.append(1) while S: s=S.popleft() ss=popcount(s) if s+ss<=n and Dist[s+ss]==inf: Dist[s+ss]=Dist[s]+1 S.append(s+ss) if s-ss>=1 and Dist[s-ss]==inf: S.append(s-ss) Dist[s-ss]=Dist[s]+1 print(-1 if Dist[n]==inf else Dist[n])