結果
| 問題 | No.2066 Simple Math ! |
| コンテスト | |
| ユーザー |
square1001
|
| 提出日時 | 2022-09-02 21:56:48 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 510 ms / 2,000 ms |
| コード長 | 1,233 bytes |
| コンパイル時間 | 694 ms |
| コンパイル使用メモリ | 71,260 KB |
| 最終ジャッジ日時 | 2025-02-07 01:12:33 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 31 |
ソースコード
#include <numeric>
#include <iostream>
#include <algorithm>
using namespace std;
// floor sum from: https://atcoder.jp/contests/practice2/submissions/16928474
long long floor_sum(long long n, long long m, long long a, long long b) {
if (a >= m || b >= m) {
return n * (n - 1) * (a / m) / 2 + n * (b / m) + floor_sum(n, m, a % m, b % m);
}
long long y = (n * a + b) / m, x = m * y - b;
if (y == 0) {
return 0;
}
return (n - (x + a - 1) / a) * y + floor_sum(y, a, m, (a - x % a) % a);
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int T;
cin >> T;
for (int testcase = 1; testcase <= T; testcase++) {
long long P, Q, K;
cin >> P >> Q >> K;
long long G = gcd(P, Q);
P /= G;
Q /= G;
if (K >= (P - 1) * (Q - 1) / 2) {
cout << (K + (P - 1) * (Q - 1) / 2) * G << '\n';
}
else {
long long l = 0, r = P * Q;
while (r - l > 1) {
long long m = (l + r) / 2;
// floor sum: floor(m / Q + 1) + floor((m - P) / Q + 1) + floor((m - 2 * P) / Q + 1) + ... + floor((m - k * P) / Q + 1) where k = floor(m / P)
long long res = floor_sum(m / P + 1, Q, P, Q + m % P);
if (res >= K + 1) {
r = m;
}
else {
l = m;
}
}
cout << r * G << '\n';
}
}
return 0;
}
square1001