結果
| 問題 | No.3527 Minimum Abs Sum |
| コンテスト | |
| ユーザー |
detteiuu
|
| 提出日時 | 2026-05-04 21:55:59 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,635 bytes |
| 記録 | |
| コンパイル時間 | 221 ms |
| コンパイル使用メモリ | 85,504 KB |
| 実行使用メモリ | 143,824 KB |
| 最終ジャッジ日時 | 2026-05-04 21:56:43 |
| 合計ジャッジ時間 | 35,065 ms |
|
ジャッジサーバーID (参考情報) |
judge3_1 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 24 WA * 4 TLE * 2 |
ソースコード
from math import gcd
class Fraction:
def __init__(self, top, bottom):
if bottom == 0:
raise ZeroDivisionError
GCD = gcd(top, bottom)
if bottom < 0:
top = -top
bottom = -bottom
self.top = top//GCD
self.bottom = bottom//GCD
def _to_fraction(self, x):
return x if isinstance(x, Fraction) else Fraction(x, 1)
def __eq__(self, other):
other = self._to_fraction(other)
return self.top == other.top and self.bottom == other.bottom
def __ne__(self, other):
other = self._to_fraction(other)
return self.top != other.top or self.bottom != other.bottom
def __lt__(self, other):
other = self._to_fraction(other)
return self.top*other.bottom < self.bottom*other.top
def __le__(self, other):
other = self._to_fraction(other)
return self.top*other.bottom <= self.bottom*other.top
def __gt__(self, other):
other = self._to_fraction(other)
return self.top*other.bottom > self.bottom*other.top
def __ge__(self, other):
other = self._to_fraction(other)
return self.top*other.bottom >= self.bottom*other.top
def __add__(self, other):
other = self._to_fraction(other)
GCD = gcd(self.bottom, other.bottom)
bottom = self.bottom*other.bottom//GCD
top1 = self.top*(bottom//self.bottom)
top2 = other.top*(bottom//other.bottom)
return Fraction(top1+top2, bottom)
def __sub__(self, other):
other = self._to_fraction(other)
GCD = gcd(self.bottom, other.bottom)
bottom = self.bottom*other.bottom//GCD
top1 = self.top*(bottom//self.bottom)
top2 = other.top*(bottom//other.bottom)
return Fraction(top1-top2, bottom)
def __mul__(self, other):
other = self._to_fraction(other)
return Fraction(self.top*other.top, self.bottom*other.bottom)
def __truediv__(self, other):
other = self._to_fraction(other)
return Fraction(self.top*other.bottom, self.bottom*other.top)
def __radd__(self, other):
return self+other
def __rsub__(self, other):
return self._to_fraction(other)-self
def __rmul__(self, other):
return self*other
def __rtruediv__(self, other):
return self._to_fraction(other)/self
def __int__(self):
return self.top//self.bottom
def __float__(self):
return self.top/self.bottom
def __str__(self):
return f"{self.top}/{self.bottom}"
def __hash__(self):
return hash((self.top, self.bottom))
def __bool__(self):
return self.top != 0
def __neg__(self):
return Fraction(-self.top, self.bottom)
def __abs__(self):
return Fraction(abs(self.top), self.bottom)
def get(self):
return self.top, self.bottom
def inverse(n, d):
return n * pow(d, -1, MOD) % MOD
MOD = 10**9+7
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = []
SUM = 0
for i in range(N):
if A[i] == 0: continue
if A[i] < 0:
A[i] = -A[i]
B[i] = -B[i]
C.append((Fraction(B[i], A[i]), A[i]))
if 1 <= i: SUM += A[i]
C.sort(key=lambda x:x[0])
l, r = C[0][1], SUM
SUM = 0
pre = C[0][0]
for a, b in C:
SUM += (a-pre)*b
X = SUM
ans = a
for a, b in C[1:]:
diff = a-pre
SUM -= diff*r
SUM += diff*l
if SUM < X:
SUM = X
ans = a
r -= b
l += b
pre = a
print(inverse(ans.top, ans.bottom))
detteiuu