結果

問題 No.2067 ±2^k operations
ユーザー とりゐとりゐ
提出日時 2022-08-25 21:51:39
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,133 ms / 2,000 ms
コード長 590 bytes
コンパイル時間 381 ms
コンパイル使用メモリ 86,876 KB
実行使用メモリ 259,812 KB
最終ジャッジ日時 2023-08-05 22:56:00
合計ジャッジ時間 16,113 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,280 KB
testcase_01 AC 408 ms
94,700 KB
testcase_02 AC 409 ms
93,108 KB
testcase_03 AC 573 ms
99,100 KB
testcase_04 AC 546 ms
96,652 KB
testcase_05 AC 495 ms
94,800 KB
testcase_06 AC 263 ms
80,244 KB
testcase_07 AC 940 ms
256,404 KB
testcase_08 AC 962 ms
250,680 KB
testcase_09 AC 1,013 ms
249,464 KB
testcase_10 AC 995 ms
250,904 KB
testcase_11 AC 950 ms
254,260 KB
testcase_12 AC 925 ms
253,744 KB
testcase_13 AC 1,016 ms
250,088 KB
testcase_14 AC 996 ms
249,408 KB
testcase_15 AC 1,133 ms
259,812 KB
testcase_16 AC 927 ms
249,460 KB
testcase_17 AC 346 ms
84,460 KB
testcase_18 AC 248 ms
79,960 KB
testcase_19 AC 130 ms
78,060 KB
testcase_20 AC 152 ms
78,464 KB
testcase_21 AC 165 ms
78,752 KB
testcase_22 AC 162 ms
79,388 KB
testcase_23 AC 156 ms
79,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

memo={}
def f(n):
  if n<=1:
    return n
  if n in memo:
    return memo[n]
  if n%2==0:
    res=f(n//2)
    memo[n]=res
    return res
  else:
    res=1+min(f(n//2),f(n//2+1))
    memo[n]=res
    return res

def calc(n):
  if n==1:
    return 0,1,0,0
  if n%4==0:
    res,a,b,c=calc(n//4)
    res2=res*4+4*a+3*b+c
    a2=2*a+b+c
    b2=a+2*b+c
    c2=a+b+2*c
    return res2,a2,b2,c2
  
  res,a,b,c=calc(n-1)
  p=f(n-1)
  q=f(n)
  res+=p

  if p<q:
    a+=1
  if p==q:
    b+=1
  if p>q:
    c+=1
  
  return res,a,b,c

for _ in range(int(input())):
  n=int(input())
  print(calc(n+1)[0])
0