結果

問題 No.816 Beautiful tuples
ユーザー Mister
提出日時 2020-04-18 01:42:07
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 1,500 ms
コード長 691 bytes
コンパイル時間 876 ms
コンパイル使用メモリ 79,344 KB
最終ジャッジ日時 2025-01-09 21:03:14
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>

using lint = long long;

void solve() {
    lint a, b;
    std::cin >> a >> b;

    auto s = a + b;

    std::vector<lint> cs;
    for (lint p = 1; p * p <= s; ++p) {
        if (s % p != 0) continue;
        cs.push_back(p);
        cs.push_back(s / p);
    }

    std::sort(cs.begin(), cs.end());
    for (auto c : cs) {
        if (a != c && b != c &&
            (a + c) % b == 0 && (b + c) % a == 0) {
            std::cout << c << std::endl;
            return;
        }
    }

    std::cout << -1 << std::endl;
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0