Advertisement
Guest User

Untitled

a guest
Mar 24th, 2021
1,026
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <set>
  5. #include <map>
  6.  
  7. #define all(a) a.begin(), a.end()
  8. #define rall(a) a.rebgin(), a.rend()
  9. #define SOLVE int t; cin >> t; while (t--) solve();
  10.  
  11. using namespace std;
  12.  
  13. int gcd(int a, int b) {
  14.     if (b == 0)
  15.         return a;
  16.     return gcd(b, a % b);
  17. }
  18.  
  19. void solve() {
  20.     int n;
  21.     cin >> n;
  22.     vector<int> a(n);
  23.     for (int i = 0; i < n; ++i)
  24.         cin >> a[i];
  25.     vector<pair<int, int>> next(n);
  26.     for (int i = 0; i < n; ++i)
  27.         next[i] = {(i - 1 + n) % n, (i + 1) % n};
  28.     set<pair<int, int>> q;
  29.     for (int i = 0; i < n; ++i) {
  30.         if (gcd(a[i], a[(i + 1) % n]) == 1) {
  31.             q.insert({0, i});
  32.             ++i;
  33.         }
  34.     }
  35.     vector<int> ans;
  36.     vector<int> used(n);
  37.     while (q.size()) {
  38.         auto z = *q.begin();
  39.         q.erase(z);
  40.         if (used[z.second])
  41.             continue;
  42.         int v = next[z.second].second;
  43.         used[v] = 1;
  44.         ans.push_back(v);
  45.         next[z.second].second = next[v].second;
  46.         next[next[v].second].first = z.second;
  47.         if (gcd(a[z.second], a[next[z.second].second]) == 1) {
  48.             q.insert({z.first + 1, z.second});
  49.         }
  50.     }
  51.     cout << ans.size() << ' ';
  52.     for (int i : ans)
  53.         cout << i + 1 << ' ';
  54.     cout << '\n';
  55. }
  56.  
  57. signed main() {
  58.     ios_base::sync_with_stdio(0);
  59.     cout.tie(0);
  60.     cin.tie(0);
  61.  
  62.     SOLVE
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement