結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
双六
|
| 提出日時 | 2020-07-22 09:20:59 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 60 ms / 5,000 ms |
| コード長 | 1,104 bytes |
| コンパイル時間 | 176 ms |
| コンパイル使用メモリ | 81,988 KB |
| 実行使用メモリ | 70,676 KB |
| 最終ジャッジ日時 | 2024-07-01 09:52:55 |
| 合計ジャッジ時間 | 2,982 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
from collections import deque
INF = float("inf")
def getlist():
return list(map(int, input().split()))
class Graph(object):
def __init__(self):
self.graph = defaultdict(list)
def __len__(self):
return len(self.graph)
def add_edge(self, a, b):
self.graph[a].append(b)
class BFS(object):
def __init__(self, graph, s, N):
self.g = graph.graph
self.Q = deque(); self.Q.append(s)
self.dist = [INF] * N; self.dist[s] = 0
while self.Q:
v = self.Q.popleft()
for i in self.g[v]:
if self.dist[i] == INF:
self.dist[i] = self.dist[v] + 1
self.Q.append(i)
#処理内容
def main():
N = int(input())
G = Graph()
for i in range(1, N + 1):
n = i
cnt = 0
while True:
if n % 2 == 1:
cnt += 1
n >>= 1
if n == 0:
break
if i - cnt >= 1:
G.add_edge(i, i - cnt)
G.add_edge(i, i + cnt)
BF = BFS(G, 1, N + 100)
# print(BF.dist)
ans = BF.dist[N]
if ans == INF:
print(-1)
else:
print(ans + 1)
if __name__ == '__main__':
main()
双六