結果

問題 No.2066 Simple Math !
ユーザー square1001square1001
提出日時 2022-09-02 21:56:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 501 ms / 2,000 ms
コード長 1,233 bytes
コンパイル時間 580 ms
コンパイル使用メモリ 72,800 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-10 03:56:35
合計ジャッジ時間 11,524 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 5 ms
4,388 KB
testcase_02 AC 21 ms
4,376 KB
testcase_03 AC 6 ms
4,380 KB
testcase_04 AC 486 ms
4,376 KB
testcase_05 AC 481 ms
4,380 KB
testcase_06 AC 477 ms
4,380 KB
testcase_07 AC 481 ms
4,376 KB
testcase_08 AC 475 ms
4,380 KB
testcase_09 AC 500 ms
4,380 KB
testcase_10 AC 500 ms
4,376 KB
testcase_11 AC 496 ms
4,380 KB
testcase_12 AC 498 ms
4,380 KB
testcase_13 AC 501 ms
4,380 KB
testcase_14 AC 475 ms
4,380 KB
testcase_15 AC 472 ms
4,380 KB
testcase_16 AC 474 ms
4,380 KB
testcase_17 AC 475 ms
4,380 KB
testcase_18 AC 473 ms
4,376 KB
testcase_19 AC 130 ms
4,380 KB
testcase_20 AC 129 ms
4,376 KB
testcase_21 AC 129 ms
4,380 KB
testcase_22 AC 132 ms
4,376 KB
testcase_23 AC 132 ms
4,380 KB
testcase_24 AC 233 ms
4,380 KB
testcase_25 AC 232 ms
4,380 KB
testcase_26 AC 230 ms
4,380 KB
testcase_27 AC 232 ms
4,376 KB
testcase_28 AC 234 ms
4,384 KB
testcase_29 AC 94 ms
4,380 KB
testcase_30 AC 6 ms
4,380 KB
testcase_31 AC 6 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0