結果

問題 No.2067 ±2^k operations
ユーザー とりゐとりゐ
提出日時 2022-08-26 01:32:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,346 ms / 2,000 ms
コード長 686 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 294,896 KB
最終ジャッジ日時 2024-04-23 19:16:16
合計ジャッジ時間 18,125 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
51,712 KB
testcase_01 AC 653 ms
108,332 KB
testcase_02 AC 478 ms
105,232 KB
testcase_03 AC 376 ms
101,252 KB
testcase_04 AC 365 ms
101,056 KB
testcase_05 AC 371 ms
101,856 KB
testcase_06 AC 165 ms
79,388 KB
testcase_07 AC 1,346 ms
293,476 KB
testcase_08 AC 1,287 ms
275,496 KB
testcase_09 AC 1,315 ms
294,896 KB
testcase_10 AC 1,345 ms
294,564 KB
testcase_11 AC 1,278 ms
287,032 KB
testcase_12 AC 1,315 ms
294,784 KB
testcase_13 AC 1,304 ms
294,816 KB
testcase_14 AC 1,326 ms
294,448 KB
testcase_15 AC 1,287 ms
294,444 KB
testcase_16 AC 1,315 ms
292,764 KB
testcase_17 AC 173 ms
80,700 KB
testcase_18 AC 112 ms
76,416 KB
testcase_19 AC 108 ms
76,288 KB
testcase_20 AC 109 ms
76,160 KB
testcase_21 AC 110 ms
76,032 KB
testcase_22 AC 112 ms
76,160 KB
testcase_23 AC 112 ms
76,032 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

memo2={}
def calc(n):
  if n==1:
    return 0,1,0,0
  if n in memo2:
    return memo2[n]
  if n%4==0:
    res,a,b,c=calc(n//4)
    res2=4*res+4*a+3*b+c
    a2=2*a+b+c
    b2=a+2*b+c
    c2=a+b+2*c
    memo2[n]=(res2,a2,b2,c2)
    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
  
  memo2[n]=res,a,b,c
  return res,a,b,c

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