結果
問題 | No.2244 Integer Complete |
ユーザー |
👑 |
提出日時 | 2023-03-10 22:05:54 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 18 ms / 2,000 ms |
コード長 | 3,295 bytes |
コンパイル時間 | 1,097 ms |
コンパイル使用メモリ | 107,912 KB |
実行使用メモリ | 7,040 KB |
最終ジャッジ日時 | 2024-09-18 04:19:50 |
合計ジャッジ時間 | 3,162 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 60 |
ソースコード
#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; }