結果

問題 No.1936 Rational Approximation
ユーザー 👑 ygussanyygussany
提出日時 2022-05-13 22:24:22
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 538 bytes
コンパイル時間 402 ms
コンパイル使用メモリ 67,604 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-29 07:51:50
合計ジャッジ時間 9,608 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1,779 ms
4,376 KB
testcase_04 AC 12 ms
4,376 KB
testcase_05 AC 680 ms
4,376 KB
testcase_06 AC 725 ms
4,376 KB
testcase_07 AC 123 ms
4,380 KB
testcase_08 AC 153 ms
4,376 KB
testcase_09 AC 1,076 ms
4,380 KB
testcase_10 AC 428 ms
4,376 KB
testcase_11 TLE -
testcase_12 AC 59 ms
4,380 KB
testcase_13 AC 901 ms
4,380 KB
testcase_14 AC 2 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;
		g = tmp % P;
		if (g == 1) {
			q[0] = (tmp - 1) / P;
			break;
		} else if (g == 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