結果

問題 No.2066 Simple Math !
ユーザー 👑 ygussanyygussany
提出日時 2022-08-30 23:03:03
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 517 ms / 2,000 ms
コード長 963 bytes
コンパイル時間 1,179 ms
コンパイル使用メモリ 28,504 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-09 21:14:52
合計ジャッジ時間 12,869 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
4,384 KB
testcase_01 AC 31 ms
4,380 KB
testcase_02 AC 52 ms
4,384 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 502 ms
4,380 KB
testcase_05 AC 505 ms
4,380 KB
testcase_06 AC 502 ms
4,380 KB
testcase_07 AC 507 ms
4,384 KB
testcase_08 AC 505 ms
4,380 KB
testcase_09 AC 516 ms
4,380 KB
testcase_10 AC 516 ms
4,388 KB
testcase_11 AC 512 ms
4,380 KB
testcase_12 AC 517 ms
4,384 KB
testcase_13 AC 516 ms
4,380 KB
testcase_14 AC 500 ms
4,380 KB
testcase_15 AC 494 ms
4,384 KB
testcase_16 AC 497 ms
4,380 KB
testcase_17 AC 498 ms
4,380 KB
testcase_18 AC 494 ms
4,380 KB
testcase_19 AC 145 ms
4,384 KB
testcase_20 AC 144 ms
4,384 KB
testcase_21 AC 143 ms
4,384 KB
testcase_22 AC 144 ms
4,380 KB
testcase_23 AC 146 ms
4,380 KB
testcase_24 AC 338 ms
4,384 KB
testcase_25 AC 338 ms
4,380 KB
testcase_26 AC 335 ms
4,384 KB
testcase_27 AC 333 ms
4,380 KB
testcase_28 AC 331 ms
4,380 KB
testcase_29 AC 76 ms
4,380 KB
testcase_30 AC 6 ms
4,384 KB
testcase_31 AC 29 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

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;
	else return (n - (x + a - 1) / a) * y + floor_sum(y, a, m, (a - x % a) % a);
}

long long solve(int P, int Q, int K)
{
	if (P == Q) return K;
	
	long long l = 1, r = (long long)P * K, m, n;
	while (l < r) {
		m = (l + r) / 2;
		n = m / P + 1;
		if (n > Q) n = Q;
		if (floor_sum(n, Q, P, m + Q - P * (n - 1)) <= K) l = m + 1;
		else r = m;
	}
	return l;
}

int gcd(int a, int b)
{
	if (a == 0) return b;
	else return gcd(b % a, a);
}

int main()
{
	int T, P, Q, K, G;
	scanf("%d", &T);
	while (T--) {
		scanf("%d %d %d", &P, &Q, &K);
		G = gcd(P, Q);
		P /= G;
		Q /= G;
		if (P > Q) {
			P ^= Q;
			Q ^= P;
			P ^= Q;
		}
		printf("%lld\n", solve(P, Q, K) * G);
	}
	fflush(stdout);
	return 0;
}
0