Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void solve() {
- int n;
- read(n);
- vector<int> a(n);
- read(a);
- if (n == 1) {
- if (a[0] == 1) {
- cout << "1 1\n";
- } else {
- cout << "0\n";
- }
- return;
- }
- set<int> indexes;
- for (int i = 0; i < n; i++) {
- indexes.insert(i);
- }
- set<pair<int,int>> arr;
- for (int i = 0; i < n; i++) {
- arr.insert({i, a[i]});
- }
- int pre = -1;
- int cur = 0;
- vector<int> ans;
- int itr = 0;
- int pre_itr = -2;
- while (indexes.size()) {
- itr++;
- auto it = indexes.lower_bound(cur);
- if (it == indexes.end()) {
- it = indexes.lower_bound(0);
- }
- cur = *it;
- int nxt = cur;
- auto it2 = arr.lower_bound({cur + 1, 0});
- if (it2 != arr.end()) {
- nxt = it2->first;
- } else {
- nxt = arr.begin()->first;
- }
- if (__gcd(a[nxt], a[cur]) == 1 && (pre_itr + 1 != itr || pre != cur)) {
- indexes.erase(nxt);
- arr.erase({nxt, a[nxt]});
- ans.push_back(nxt);
- pre = cur;
- cur += 2;
- } else {
- pre_itr = itr;
- pre = cur;
- indexes.erase(cur);
- cur++;
- }
- }
- cout << ans.size() << ' ';
- for (int val : ans) {
- cout << val + 1 << ' ';
- }
- cout << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement