結果

問題 No.816 Beautiful tuples
ユーザー phsplsphspls
提出日時 2020-04-18 01:31:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,500 ms
コード長 1,173 bytes
コンパイル時間 2,086 ms
コンパイル使用メモリ 176,272 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-07-27 05:45:17
合計ジャッジ時間 2,839 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define llong long long

int main() {
    llong a, b;
    cin >> a >> b;

    vector<llong> divisors;
    llong val = a + b;
    llong limit = (llong)sqrt(val)+1LL;
    for(llong i=2; i<=limit; i++) {
        while(val % i == 0 && val > 1) {
            val /= i;
            divisors.push_back(i);
        }
        if(val == 1) break;
    }
    if(val > 1) divisors.push_back(val);

    vector<llong> cands;
    set<llong> checked;
    cands.push_back(1);
    checked.insert(1);
    for(llong d: divisors) {
        int lim = cands.size();
        rep(i, lim) {
            llong val = cands[i] * d;
            if(checked.count(val)) continue;
            checked.insert(val);
            cands.push_back(val);
        }
    }
    sort(cands.begin(), cands.end());
    for(llong c: cands) {
        if(a == c || b == c) continue;
        if((a+b) % c == 0) {
            if((b+c) % a == 0) {
                if((c+a) % b == 0) {
                    cout << c << "\n";
                    return 0;
                }
            }
        }
    }
    cout << -1 << "\n";
}
0