結果

問題 No.3 ビットすごろく
ユーザー takakintakakin
提出日時 2020-03-29 09:11:26
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 34 ms / 5,000 ms
コード長 609 bytes
コンパイル時間 543 ms
コンパイル使用メモリ 10,920 KB
実行使用メモリ 8,948 KB
最終ジャッジ日時 2023-09-14 01:40:28
合計ジャッジ時間 2,230 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,560 KB
testcase_01 AC 20 ms
8,468 KB
testcase_02 AC 19 ms
8,604 KB
testcase_03 AC 23 ms
8,704 KB
testcase_04 AC 20 ms
8,536 KB
testcase_05 AC 25 ms
8,728 KB
testcase_06 AC 22 ms
8,536 KB
testcase_07 AC 21 ms
8,584 KB
testcase_08 AC 25 ms
8,572 KB
testcase_09 AC 29 ms
8,732 KB
testcase_10 AC 30 ms
8,840 KB
testcase_11 AC 28 ms
8,740 KB
testcase_12 AC 27 ms
8,744 KB
testcase_13 AC 21 ms
8,536 KB
testcase_14 AC 30 ms
8,884 KB
testcase_15 AC 33 ms
8,816 KB
testcase_16 AC 32 ms
8,796 KB
testcase_17 AC 33 ms
8,932 KB
testcase_18 AC 22 ms
8,620 KB
testcase_19 AC 32 ms
8,808 KB
testcase_20 AC 19 ms
8,536 KB
testcase_21 AC 19 ms
8,528 KB
testcase_22 AC 31 ms
8,892 KB
testcase_23 AC 34 ms
8,832 KB
testcase_24 AC 33 ms
8,948 KB
testcase_25 AC 33 ms
8,776 KB
testcase_26 AC 19 ms
8,536 KB
testcase_27 AC 22 ms
8,700 KB
testcase_28 AC 32 ms
8,920 KB
testcase_29 AC 28 ms
8,832 KB
testcase_30 AC 19 ms
8,660 KB
testcase_31 AC 19 ms
8,464 KB
testcase_32 AC 27 ms
8,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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])
0