#include <bits/stdc++.h>
using namespace std;
using ll = long long;
set<pair<int, int>> A, B;
bool check(ll x, ll y) {
// cout << x << " " << y << endl;
if (x > y) swap(x, y);
if (x == 0) return true;
auto p = *prev(A.end());
auto q = *prev(B.end());
// cout << "P: " << p.first << " " << p.second << " ? " << x << endl;
// cout << "Q: " << q.first << " " << q.second << " ? " << y << endl;
if (p.first > x && q.first > y) return false;
bool res = false;
if (p.first == x) {
A.erase(A.find(p));
B.erase(B.find({p.second, p.first}));
// cout << "in\n";
res = max(res, check(x, y - p.second));
// cout << "out\n";
A.insert(p);
B.insert({p.second, p.first});
}
if (q.first == y) {
B.erase(B.find(q));
A.erase(A.find({q.second, q.first}));
res = max(res, check(x - q.second, y));
B.insert(q);
A.insert({q.second, q.first});
}
if (p.second == x) {
A.erase(A.find(p));
B.erase(B.find({p.second, p.first}));
// cout << "in\n";
res = max(res, check(x, y - p.first));
// cout << "out\n";
A.insert(p);
B.insert({p.second, p.first});
}
if (q.second == y) {
B.erase(B.find(q));
A.erase(A.find({q.second, q.first}));
res = max(res, check(x - q.first, y));
B.insert(q);
A.insert({q.second, q.first});
}
return res;
}
int main() {
int n;
cin >> n;
ll tot = 0;
vector<int> a(n), b(n);
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i];
tot += 1LL * a[i] * b[i];
A.insert({a[i], b[i]});
B.insert({b[i], a[i]});
}
set<ll> p;
for (int i = 0; i < n; ++i) {
if (tot % a[i] != 0) continue;
ll o = tot / a[i];
if (p.count(min((ll)a[i], o))) continue;
// cout << a[i] << " " << o << endl << endl;
if (check(a[i], o)) {
p.insert(min((ll)a[i], o));
}
if (check(o, a[i])) {
p.insert(min((ll)a[i], o));
}
// cout << endl << endl;
}
cout << p.size() << endl;
for (auto& u : p) {
cout << u << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |