結果
| 問題 | No.816 Beautiful tuples | 
| コンテスト | |
| ユーザー |  bal4u | 
| 提出日時 | 2019-06-16 09:23:46 | 
| 言語 | C (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 1,500 ms | 
| コード長 | 732 bytes | 
| コンパイル時間 | 589 ms | 
| コンパイル使用メモリ | 31,744 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-11-22 11:31:05 | 
| 合計ジャッジ時間 | 1,421 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 15 | 
ソースコード
// yukicoder: No.816 Beautiful tuples
// 2019.6.16 bal4u
#include <stdio.h>
#include <math.h>
int gcd(int a, int b) {
	int r;
	while (b != 0) r = a % b, a = b, b = r;
	return a;
}
int q[100], sz;
int A, B, C;
int check(int x) {
	if (x == A) return 0;
	if (x == B) return 0;
	if ((A+x) % B) return 0;
	return (B+x) % A == 0;
}
int main()
{
	int g, S, s;
	scanf("%d%d", &A, &B);
	g = gcd(A, B);
	if (g > 1) A /= g, B /= g;
	S = A + B, s = (int)sqrt((double)S);
	for (C = 1; C <= s; C++) {
		if (S % C == 0) {
			if (check(C)) break;
			q[sz++] = S/C;
		}
	}
	if (C <= s) C *= g;
	else {
		for (s = sz-1; ; s--) {
			if (s < 0) { C = -1; break; }
			if (check(q[s])) { C = g*q[s]; break; }
		}
	}
	printf("%d\n", C);
	return 0;
}
            
            
            
        