Advertisement
Guest User

Stress Test D

a guest
Dec 15th, 2022
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long int ll;
  5.  
  6. vector<int> perm;
  7. int cnt;
  8. ll n;
  9.  
  10. int query(int i, int j) {
  11.     if (++cnt > 2 * n) {
  12.         cout << ("query limit") << '\n';
  13.         exit(0);
  14.     }
  15.     if (i == j)
  16.         exit(130);
  17.     return __gcd(perm[i], perm[j]);
  18. }
  19.  
  20. void answer(vector<int> ans) {
  21.     sort((ans).begin(), (ans).end());
  22.     if (ans.size() != 2)
  23.         exit(131);
  24.  
  25.     if (perm[ans[0]] != 0 && perm[ans[1]] != 0) {
  26.         for (auto i : perm) cout << i << ' ';
  27.         exit(1);
  28.     }
  29. }
  30.  
  31.  
  32.  
  33. int main() {
  34.     n = 10;
  35.     perm = vector<int>(n);
  36.     iota((perm).begin(), (perm).end(), 0);
  37.     do {
  38.  
  39.         cnt = 0;
  40.  
  41.  
  42.  
  43.         vector<int> possible(n), ans;
  44.         iota((possible).begin(), (possible).end(), 0);
  45.  
  46.         while (possible.size() > 2) {
  47.             vector<int> res;
  48.             res.push_back(-1);
  49.  
  50.             for (int i = 1; i < possible.size(); i++) {
  51.                 res.push_back(query(possible[i], possible[0]));
  52.             }
  53.             int mx = *max_element((res).begin(), (res).end());
  54.  
  55.  
  56.             vector<int> next;
  57.             for (int i = 0; i < possible.size(); i++) {
  58.                 if (res[i] == mx)
  59.                     next.push_back(possible[i]);
  60.             }
  61.  
  62.             if (next.size() == 1) {
  63.  
  64.                 next.push_back(possible[0]);
  65.             }
  66.  
  67.             possible = next;
  68.         }
  69.  
  70.  
  71.  
  72.         answer(possible);
  73.  
  74.  
  75.  
  76.     } while (next_permutation((perm).begin(), (perm).end()));
  77.  
  78.  
  79.     return 0;
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement