This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
set<pair<ll,ll>>A, B;
vector<ll>ans;
map<ll,ll>mp;
ll sum;
bool solve(ll x, ll y) {
if(x<y) swap(x, y);
if(y==0) return true;
auto it=B.end(); --it;
auto p=*it;
it=A.end(); --it;
auto q=*it;
if(p.st>y || q.st>x) return false;
if(p.st==y) {
B.erase(p);
A.erase({p.nd, p.st});
bool xd=solve(x-p.nd, y);
B.insert(p);
A.insert({p.nd, p.st});
return xd;
}
if(q.st==x) {
A.erase(q);
B.erase({q.nd, q.st});
bool xd=solve(x, y-q.nd);
A.insert(q);
B.insert({q.nd, q.st});
return xd;
}
it=A.lower_bound({y, -1});
if(it==A.end()) return false;
auto r=*it;
if(r.st!=y) return false;
A.erase(r);
B.erase({r.nd, r.st});
bool xd=solve(x-r.nd, y);
A.insert(r);
B.insert({r.nd, r.st});
return xd;
}
void check(ll x) {
if(sum%x!=0) return;
x=min(x, sum/x);
if(mp.find(x)!=mp.end()) return;
mp[x]=1;
if(solve(x, sum/x)) ans.pb(x);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
ll k;
cin >> k;
vector<pair<ll,ll>>T(k);
rep(i, k) {
cin >> T[i].nd >> T[i].st;
sum+=T[i].st*T[i].nd;
A.insert({T[i].nd, T[i].st});
B.insert(T[i]);
}
sort(all(T));
check(T[k-1].st);
rep(i, k) if(T[i].nd>T[k-1].st) check(T[i].nd);
if(ans.size()==0) {
cout << 0 << '\n';
return 0;
}
sort(all(ans));
vector<ll>P;
P.pb(ans[0]);
for(auto i : ans) if(P.back()!=i) P.pb(i);
cout << P.size() << '\n';
for(auto i : P) cout << i << '\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... |