結果

問題 No.3398 Accuracy of Integer Division Approximate Function 2
コンテスト
ユーザー 👑 Mizar
提出日時 2025-12-05 17:17:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 388 ms / 2,000 ms
コード長 376 bytes
記録
コンパイル時間 3,966 ms
コンパイル使用メモリ 82,516 KB
実行使用メモリ 77,060 KB
最終ジャッジ日時 2025-12-05 17:20:08
合計ジャッジ時間 4,946 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import math
for _ in[0]*int(input()):
 D,A,B,K=map(int,input().split());g=math.gcd(D,A);D//=g;A//=g;l,h=0,A*B*K+2
 if A*B%D<1 and D*K+2>A:print(-1);continue
 while l+1<h:
  x=n=(l+h)//2;a,b,c,m=B,-(A*B//D),D,A;s=v=d=0
  while(n):a+=c//m*b;c%=m;s+=d//m*b;d%=m;v=max(v,s,s+a*~-n+b*(n:=(c*~-n+d)//m));s+=(a+b)*(a<0);a,b,c,d,m=b,a,m,m+~d,c
  if v<=B*K:l=x
  else:h=x
 print(g*D*l)
0