結果

問題 No.816 Beautiful tuples
ユーザー NoiriNoiri
提出日時 2019-07-23 13:08:16
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,500 ms
コード長 858 bytes
コンパイル時間 1,728 ms
コンパイル使用メモリ 171,044 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-07 18:31:06
合計ジャッジ時間 2,625 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for(long long i = 0; i < (long long)(n); i++)
using namespace std;
using ll = long long;
const ll INF = 1e18;

// 約数列挙
vector<long long> calc_divisor(long long n) {
    vector<long long> res;
    for (long long i = 1LL; i*i <= n; ++i) {
        if (n % i == 0) {
            res.push_back(i);
            long long j = n / i;
            if (j != i) res.push_back(j);
        }
    }
    sort(res.begin(), res.end());
    //sort(res.rbegin(), res.rend());
    return res;
}


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

    auto d = calc_divisor(a+b);

    ll ans = INF;
    rep(i, d.size()){
        ll c = (a + b) / d[i];
        if(a != c && b != c && (a + c) % b == 0 && (b + c) % a == 0) ans = min(ans, c);
    }

    if(ans == INF) cout << "-1" << endl;
    else cout << ans << endl;

    return 0;
}
0