結果

問題 No.2287 ++ -- *=a /=a
ユーザー とりゐとりゐ
提出日時 2023-04-28 22:56:12
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 639 bytes
コンパイル時間 323 ms
コンパイル使用メモリ 82,324 KB
実行使用メモリ 83,996 KB
最終ジャッジ日時 2024-04-29 00:17:21
合計ジャッジ時間 10,555 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,224 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 541 ms
79,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin
input=lambda :stdin.readline()[:-1]

memo={}

def calc(y,x,a):
  if y==0:
    return x
  if y in memo:
    return memo[y]
  ans=abs(y-x)
  tmpL,tmpR=y,y+1
  res=0
  while tmpL<x:
    tmpL*=a
    tmpR*=a
    res+=1
    if tmpL<=x<tmpR:
      ans=min(ans,res)
    else:
      ans=min(ans,res+min(abs(tmpL-x),res+abs(tmpR-x)))
  
  y1=y//a
  if y1<y:
    res1=abs(y1*a-y)+calc(y1,x,a)+1
    ans=min(ans,res1)
  y2=(y+a-1)//a
  if y2<y:
    res2=abs(y2*a-y)+calc(y2,x,a)+1
    ans=min(ans,res2)
  memo[y]=ans
  return ans

for _ in range(int(input())):
  memo.clear()
  x,y,a=map(int,input().split())
  print(calc(y,x,a))
0