結果

問題 No.3398 Accuracy of Integer Division Approximate Function 2
コンテスト
ユーザー 👑 Mizar
提出日時 2025-12-04 23:07:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 328 ms / 2,000 ms
コード長 380 bytes
記録
コンパイル時間 194 ms
コンパイル使用メモリ 82,448 KB
実行使用メモリ 77,260 KB
最終ジャッジ日時 2025-12-04 23:34:10
合計ジャッジ時間 3,856 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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-1)+b*(n:=(c*(n-1)+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