結果

問題 No.1936 Rational Approximation
ユーザー 👑 ygussanyygussany
提出日時 2022-05-13 22:21:54
言語 C
(gcc 12.3.0)
結果
TLE  
実行時間 -
コード長 535 bytes
コンパイル時間 703 ms
コンパイル使用メモリ 28,132 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-29 07:47:53
合計ジャッジ時間 9,955 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 0 ms
4,380 KB
testcase_02 AC 0 ms
4,380 KB
testcase_03 AC 1,723 ms
4,376 KB
testcase_04 AC 11 ms
4,380 KB
testcase_05 AC 658 ms
4,376 KB
testcase_06 AC 699 ms
4,380 KB
testcase_07 AC 118 ms
4,376 KB
testcase_08 AC 149 ms
4,376 KB
testcase_09 AC 1,044 ms
4,380 KB
testcase_10 AC 414 ms
4,376 KB
testcase_11 TLE -
testcase_12 AC 57 ms
4,376 KB
testcase_13 AC 872 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

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

int main()
{
	int P, Q, p[2], q[2], g;
	long long tmp;
	scanf("%d %d", &P, &Q);
	for (p[0] = 1; p[0] * 2 <= P; p[0]++) {
		tmp = (long long)p[0] * Q;
		if (tmp % P == 1) {
			q[0] = (tmp - 1) / P;
			break;
		} else if (tmp % P == P - 1) {
			q[0] = (tmp + 1) / P;
			break;
		}
	}
	p[1] = P - p[0];
	q[1] = Q - q[0];
	g = gcd(p[1], q[1]);
	p[1] /= g;
	q[1] /= g;
	printf("%d\n", p[0] + q[0] + p[1] + q[1]);
	fflush(stdout);
	return 0;
}
0