結果

問題 No.2066 Simple Math !
ユーザー 👑 ygussanyygussany
提出日時 2022-08-30 23:03:03
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 527 ms / 2,000 ms
コード長 963 bytes
コンパイル時間 1,031 ms
コンパイル使用メモリ 29,824 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-27 15:42:50
合計ジャッジ時間 12,245 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 31 ms
6,944 KB
testcase_02 AC 57 ms
6,944 KB
testcase_03 AC 6 ms
6,944 KB
testcase_04 AC 517 ms
6,944 KB
testcase_05 AC 514 ms
6,944 KB
testcase_06 AC 511 ms
6,944 KB
testcase_07 AC 513 ms
6,940 KB
testcase_08 AC 510 ms
6,940 KB
testcase_09 AC 522 ms
6,940 KB
testcase_10 AC 523 ms
6,940 KB
testcase_11 AC 520 ms
6,944 KB
testcase_12 AC 522 ms
6,940 KB
testcase_13 AC 527 ms
6,940 KB
testcase_14 AC 510 ms
6,944 KB
testcase_15 AC 502 ms
6,944 KB
testcase_16 AC 506 ms
6,940 KB
testcase_17 AC 502 ms
6,944 KB
testcase_18 AC 505 ms
6,940 KB
testcase_19 AC 148 ms
6,944 KB
testcase_20 AC 146 ms
6,944 KB
testcase_21 AC 147 ms
6,940 KB
testcase_22 AC 152 ms
6,944 KB
testcase_23 AC 149 ms
6,944 KB
testcase_24 AC 344 ms
6,940 KB
testcase_25 AC 339 ms
6,940 KB
testcase_26 AC 342 ms
6,944 KB
testcase_27 AC 341 ms
6,940 KB
testcase_28 AC 338 ms
6,940 KB
testcase_29 AC 78 ms
6,940 KB
testcase_30 AC 6 ms
6,940 KB
testcase_31 AC 30 ms
6,944 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