結果
| 問題 |
No.3 ビットすごろく
|
| コンテスト | |
| ユーザー |
mediocreRail
|
| 提出日時 | 2023-08-15 08:16:03 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 46 ms / 5,000 ms |
| コード長 | 1,473 bytes |
| コンパイル時間 | 125 ms |
| コンパイル使用メモリ | 12,672 KB |
| 実行使用メモリ | 11,392 KB |
| 最終ジャッジ日時 | 2024-11-23 14:09:38 |
| 合計ジャッジ時間 | 2,486 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
import bisect,collections,itertools,math,functools,heapq
import sys
#sys.setrecursionlimit(10**6)
def I(): return int(sys.stdin.readline().rstrip())
def IN(): return int(input())
def LIN(): return list(map(int, input().split()))
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LF(): return list(map(float,sys.stdin.readline().rstrip().split()))
def SI(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
"""
方針
各門で引き返す or 通過するを足して計算する
"""
def popcount(x):
'''xの立っているビット数をカウントする関数
(xは64bit整数)'''
# 2bitごとの組に分け、立っているビット数を2bitで表現する
x = x - ((x >> 1) & 0x5555555555555555)
# 4bit整数に 上位2bit + 下位2bit を計算した値を入れる
x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f # 8bitごと
x = x + (x >> 8) # 16bitごと
x = x + (x >> 16) # 32bitごと
x = x + (x >> 32) # 64bitごと = 全部の合計
return x & 0x0000007f
N=I()
dp = [-1]*(N+1)
stack = collections.deque()
stack.append(1)
dp[1] = 1
while stack:
v = stack.popleft()
p = popcount(v)
if v+p <= N and dp[v+p] < 0:
dp[v+p] = dp[v]+1
stack.append(v+p)
if v-p >= 0 and dp[v-p] < 0 :
dp[v-p] = dp[v] + 1
stack.append(v-p)
print(dp[N])
mediocreRail