結果

問題 No.1936 Rational Approximation
ユーザー 👑 ygussanyygussany
提出日時 2022-05-13 22:27:28
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 577 ms / 2,000 ms
コード長 589 bytes
コンパイル時間 389 ms
コンパイル使用メモリ 28,108 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-29 07:55:49
合計ジャッジ時間 2,872 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 375 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 143 ms
4,376 KB
testcase_06 AC 153 ms
4,380 KB
testcase_07 AC 27 ms
4,380 KB
testcase_08 AC 33 ms
4,376 KB
testcase_09 AC 227 ms
4,376 KB
testcase_10 AC 91 ms
4,376 KB
testcase_11 AC 577 ms
4,380 KB
testcase_12 AC 13 ms
4,380 KB
testcase_13 AC 190 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, rem, add;
	long long tmp;
	scanf("%d %d", &P, &Q);
	for (p[0] = 1, tmp = Q, rem = Q % P, add = rem; p[0] * 2 <= P; p[0]++, tmp += Q, rem += add) {
		if (rem >= P) rem -= P;
		if (rem == 1) {
			q[0] = (tmp - 1) / P;
			break;
		} else if (rem == 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