結果
問題 | No.3051 Make All Divisible |
ユーザー |
👑 |
提出日時 | 2025-02-03 23:05:14 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,284 ms / 2,000 ms |
コード長 | 1,501 bytes |
コンパイル時間 | 202 ms |
コンパイル使用メモリ | 82,416 KB |
実行使用メモリ | 77,036 KB |
最終ジャッジ日時 | 2025-02-08 15:05:06 |
合計ジャッジ時間 | 9,467 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 31 |
ソースコード
def solve():n, K = map(int, input().split())A = list(map(int, input().split()))A.sort()C = [a % K for a in A]if sum(C) % K != 0:print(-1)returndef ok(x: int) -> bool:tot = 0x_k = x % Kfor a, c in zip(A, C):if a < x:tot += aelse:if x < c:return Falseif c <= x_k:tot += x - x_k + celse:tot += x - x_k + c - Kreturn x <= tot // Krr = A[n - K] - 1 + Kinf = 1 << 62max_c = inffor d in range(K):ma = (rr - d) // K * K + dif not ok(ma):continuel = -1r = ma // Kwhile r - l > 1:mid = (l + r) >> 1if ok(mid * K + d):r = midelse:l = midif r * K + d < max_c:max_c = r * K + dx = max_ctot = 0max_cnt = 0x_k = x % Kfor a, c in zip(A, C):if a < x:tot += aelse:if c <= x_k:tot += x - x_k + cif c == x_k:max_cnt += 1else:tot += x - x_k + c - Ktot //= Kif max_c < K:max_cnt = 0ans = max(max_c, tot - max_cnt)if ans == inf:print(-1)else:print(ans)for _ in range(int(input())):solve()