結果

問題 No.2244 Integer Complete
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-03-10 22:05:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 3,295 bytes
コンパイル時間 1,137 ms
コンパイル使用メモリ 108,220 KB
実行使用メモリ 7,000 KB
最終ジャッジ日時 2023-10-18 07:50:28
合計ジャッジ時間 3,615 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 3 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 3 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 8 ms
4,348 KB
testcase_18 AC 9 ms
4,348 KB
testcase_19 AC 21 ms
7,000 KB
testcase_20 AC 16 ms
6,208 KB
testcase_21 AC 18 ms
6,736 KB
testcase_22 AC 17 ms
7,000 KB
testcase_23 AC 14 ms
6,208 KB
testcase_24 AC 15 ms
6,208 KB
testcase_25 AC 15 ms
6,208 KB
testcase_26 AC 18 ms
7,000 KB
testcase_27 AC 18 ms
6,208 KB
testcase_28 AC 19 ms
6,736 KB
testcase_29 AC 18 ms
6,208 KB
testcase_30 AC 20 ms
7,000 KB
testcase_31 AC 15 ms
6,208 KB
testcase_32 AC 12 ms
5,680 KB
testcase_33 AC 4 ms
4,348 KB
testcase_34 AC 13 ms
5,944 KB
testcase_35 AC 18 ms
6,736 KB
testcase_36 AC 16 ms
6,472 KB
testcase_37 AC 16 ms
6,472 KB
testcase_38 AC 18 ms
6,736 KB
testcase_39 AC 10 ms
4,716 KB
testcase_40 AC 11 ms
5,152 KB
testcase_41 AC 7 ms
4,348 KB
testcase_42 AC 10 ms
4,628 KB
testcase_43 AC 12 ms
5,152 KB
testcase_44 AC 12 ms
5,152 KB
testcase_45 AC 16 ms
6,472 KB
testcase_46 AC 11 ms
5,152 KB
testcase_47 AC 11 ms
5,152 KB
testcase_48 AC 6 ms
4,348 KB
testcase_49 AC 7 ms
4,348 KB
testcase_50 AC 6 ms
4,348 KB
testcase_51 AC 13 ms
5,152 KB
testcase_52 AC 10 ms
4,408 KB
testcase_53 AC 8 ms
4,348 KB
testcase_54 AC 8 ms
4,348 KB
testcase_55 AC 9 ms
4,888 KB
testcase_56 AC 8 ms
4,888 KB
testcase_57 AC 3 ms
4,348 KB
testcase_58 AC 17 ms
6,736 KB
testcase_59 AC 17 ms
6,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


constexpr int LIM = 30010;
int lpf[LIM];

int M, N;
vector<int> A, B;

bitset<LIM> hasA, hasB;

int len;
vector<int> ps, es;

bool dfs(int i, int x, int y) {
  if (i == len) {
    if (hasA[(int)sqrt(y)] && hasB[(int)sqrt(x / y)]) {
      return true;
    }
    return false;
  } else {
    for (int e = 0; ; ++e) {
      if (dfs(i + 1, x, y)) return true;
      if (e == es[i]) break;
      y *= ps[i];
    }
    return false;
  }
}

int main() {
  iota(lpf, lpf + LIM, 0);
  for (int p = 2; p < LIM; ++p) if (lpf[p] == p) {
    for (int n = 2 * p; n < LIM; n += p) chmin(lpf[n], p);
  }
  
  for (; ~scanf("%d%d", &M, &N); ) {
    A.resize(M); for (int i = 0; i < M; ++i) scanf("%d", &A[i]);
    B.resize(N); for (int i = 0; i < N; ++i) scanf("%d", &B[i]);
    
    hasA.reset(); for (int i = 0; i < M; ++i) hasA[A[i]] = true;
    hasB.reset(); for (int i = 0; i < N; ++i) hasB[B[i]] = true;
    
    int ans = 0;
    if (!(A[0] == 1 && B[0] == 1)) {
      ans = 1;
    } else {
      for (int c = 1; ; ++c) if (!hasA[c] && !hasB[c]) {
        // [c^2, (c+1)^2)
        const int lb = c * c;
        const int ub = (c + 1) * (c + 1);
        vector<vector<int>> sieve(ub - lb);
        for (int p = 2; p < LIM; ++p) if (lpf[p] == p) {
          for (int x = (lb + p - 1) / p * p; x < ub; x += p) {
            const int pos = x - lb;
            sieve[pos].push_back(p);
          }
        }
        for (int x = lb; x < ub; ++x) {
          const int pos = x - lb;
          int xx = x;
          ps.clear();
          es.clear();
          for (const int p : sieve[pos]) {
            int e = 0;
            do {
              ++e;
              xx /= p;
            } while (xx % p == 0);
            ps.push_back(p);
            es.push_back(e);
          }
          if (xx > 1) {
            ps.push_back(xx);
            es.push_back(1);
          }
          len = ps.size();
// cerr<<x<<": "<<ps<<" "<<es<<endl;
          if (!dfs(0, x, 1)) {
            ans = x;
            goto found;
          }
        }
        assert(false);
      }
     found:{}
    }
    printf("%d\n", ans);
  }
  return 0;
}
0