結果

問題 No.816 Beautiful tuples
ユーザー betweensbetweens
提出日時 2019-05-19 02:01:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,500 ms
コード長 2,680 bytes
コンパイル時間 1,044 ms
コンパイル使用メモリ 106,332 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 07:51:41
合計ジャッジ時間 1,819 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <numeric>
#include <fstream>
#include <iomanip>
#include <iterator>

typedef long double ld;
typedef long long ll;
const ll INF = (ll)1e18 + 1;
const ll MOD = 1e9 + 7;

// Split
namespace util {

std::vector< std::string > split(std::string s, char delimiter)
{
  std::vector< std::string > vs;
  std::string sub;
  for (auto c : s) {
    if (c == delimiter) vs.push_back(sub), sub.clear();
    else sub += c;
  }
  vs.push_back(sub);
  return vs;
}

} // namespace util

// Minimum, Maximum
template<class T> T minimum(T head, T tail) { return std::min(head, tail); }
template<class H, class... T> H minimum(H head, T... tail) { return std::min(head, minimum(tail...)); }
template<class T> T maximum(T head, T tail) { return std::max(head, tail); }
template<class H, class... T> H maximum(H head, T... tail) { return std::max(head, maximum(tail...)); }

// Output
template<class T, class S> std::ostream& operator << (std::ostream& os, std::pair<T, S> p)
{
  return os << p.first << " " << p.second;
}

template<class T> std::ostream& operator << (std::ostream& os, std::vector< T > v)
{
  for (ll i = 0; i < (ll)v.size(); i++){ os << " [" << i << "]" << v[i]; if (i % 10 == 9) os << std::endl; }
  return os;
}

template<class T, class S> std::ostream& operator << (std::ostream& os, std::vector< std::pair<T, S> > vp)
{
  ll i = 0;
  for (auto p : vp){os << " [" << i++ << "]" << p.first << " " << p.second; if (i % 10 == 0) os << std::endl;}
  return os;
}

void print(){ std::cout << std::endl; }
template<typename H> void print(H head) { std::cout << head << std::endl; }
template<typename H, typename... T> void print(H head, T... tail){ std::cout << head << " ", print(tail...); }

std::map<ll, ll> mc;
std::vector<ll> vnum;

void dfs(std::map<ll, ll>::iterator it, ll num)
{
  if (it == mc.end()) {
    vnum.push_back(num);
    return;
  }

  for (ll i = 0; i <= (ll)it->second; i++) {
    ll next_num = num * std::pow(it->first, i);
    dfs(std::next(it), next_num);
  }

  return;
}

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

  ll A, B;
  std::cin >> A >> B;

  if (A == B) {
    print(-1);
    return 0;
  }
  ll N = A + B;

  for (ll i = 2; i * i <= N; i++) {
    while (N % i == 0) {
      mc[i]++;
      N /= i;
    }
    if (N != 1) mc[N] = 1;
  }

  //print("mc", mc[2], mc[3]);
  dfs(mc.begin(), 1LL);
  //print(vnum);

  ll ans = -1;
  for (auto C : vnum) {
    if (A != C && B != C &&
	(A+B) % C == 0 &&
	(B+C) % A == 0 &&
	(C+A) % B == 0) {
      ans = C;
      break;
    }
  }

  print(ans);
  return 0;
}
0