Problem:
Common-Prime Sort
You are now examining a unique method to sort a sequence of numbers in increasing order. The method only allows swapping of two numbers that have a common prime factor. For example, a sequence [6, 4, 2, 3, 7] can be sorted using the following steps.
Step 0: 6 4 2 3 7
(given sequence)
Step 1: 2 4 6 3 7
(elements 6 and 2 swapped)
Step 2: 2 6 4 3 7
(elements 4 and 6 swapped)
Step 3: 2 3 4 6 7
(elements 6 and 3 swapped)
Depending on the nature of the sequence, however, this approach may fail to complete the sorting. You have given a name "Coprime sort" to this approach and are now examining if a given sequence is coprime-sortable.
Make a program to determine if a given sequence can be sorted in increasing order by iterating an arbitrary number of swapping operations of two elements that have a common prime number.
Input
The input is given in the following format.
$N$
$a_1$ $a_2$ $...$ $a_N$
The first line provides the number of elements included in the sequence $N$ ($2 \leq N \leq 10^5$). The second line provides an array of integers $a_i$ ($2 \leq a_i \leq 10^5$) that constitute the sequence.
Output
Output "
1
" if the sequence is coprime-sortable in increasing order, or "
0
" otherwise.
Sample Input 1
5
6 4 2 3 7
Sample Output 1
1
Sample Input 2
7
2 9 6 5 6 7 3
Sample Output 2
0

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std; 
/*#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
using namespace boost::multiprecision;*/
/*#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;*/
//long long型=int64_tという認識で大丈夫です。
//#define int long long
#define ll long long
#define ull unsigned long long
#define vec vector
#define vi vector<int>
#define vll vector<ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define rep(i, l, r) for(ll i=(ll)(l); i<(ll)(r); i++)
#define ld long double

const ll INF = 1e9;
const ll inf = 5e18;
const ld ep = 1e-15;

struct Edge {
    int to;
    ll cost;
};

struct SEdge {
    int to;
    pair<ll, ll> cost;
};

template<class T>
using spq = priority_queue<T, vec<T>, greater<T>>; 

const vector<ll> di = {-1, 0, 1, 0}, dj = {0, -1, 0, 1};

template<class T>
bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool chmax(T& a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T, class U> 
istream &operator>>(istream &is, pair<T, U> &p) {
    is >> p.first >> p.second;
    return is;
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << p.first << " " << p.second;
    return os;
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
    for(auto &x : v) {
        is >> x;
    }
    return is;
}

template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    for(int i = 0; i < (int)v.size(); i++) {
        if(i != (int)v.size() - 1)
            os << v[i] << " ";
        else
            os << v[i];
    }
    return os;
}

string strplus(string a, string b) {
    reverse(a.begin(), a.end()); reverse(b.begin(), b.end());
    bool ch = false;
    if(a.size() < b.size()) {
        swap(a, b);
        ch = true;
    }
    string ret;
    int cnt = 0;
    rep(i, 0, a.size()) {
        if(i >= b.size()) {
            ret += (char)(((int)(a[i] - '0') + cnt) % 10 + '0');
            cnt = (((int)(a[i] - '0') + cnt) / 10);
        }
        else {
            ret += (char)(((int)(a[i] - '0' + b[i] - '0') + cnt) % 10 + '0');
            cnt = ((int)(a[i] - '0' + b[i] - '0') + cnt) / 10;
        }
    }
    if(cnt != 0) ret += "1";
    reverse(ret.begin(), ret.end());
    return ret;
}

string strminus(string a, string b) {
    reverse(a.begin(), a.end()); reverse(b.begin(), b.end());
    bool ch = false;
    if(a.size() < b.size()) {
        swap(a, b);
        ch = true;
    }
    else if(a.size() == b.size() && a < b) {
        swap(a, b);
        ch = true;
    }
    string ret;
    int cnt = 0;
    rep(i, 0, a.size()) {
        if(i >= b.size()) {
            ret += (char)(((int)(a[i] - '0') + cnt + 10) % 10 + '0');
            cnt = ((int)(a[i] - '0') + cnt + 10) / 10 - 1;
        }
        else {
            ret += (char)(((int)(a[i] - b[i]) + cnt + 10) % 10 + '0');
            cnt = ((int)(a[i] - b[i]) + cnt + 10) / 10 - 1;
        }
    }
    while(ret[ret.size() - 1] == '0' && ret.size() >= 1) ret.erase(--ret.end());
    if(ret != "0" && ch) ret += '-';
    reverse(ret.begin(), ret.end());
    return ret;
}

template <class T>
bool jf(pair<T, T> a, pair<T, T> b) {
    if(a.second > b.second) return false;
    else return true;
}

using S = ll;

S op(S a, S b){ return max(a, b); }
S e(){ return 0;}


struct dsu {
  public:
    dsu() : _n(0) {}
    explicit dsu(int n) : _n(n), parent_or_size(n, -1) {}

    int merge(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        int x = leader(a), y = leader(b);
        if (x == y) return x;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        return x;
    }

    bool same(int a, int b) {
        assert(0 <= a && a < _n);
        assert(0 <= b && b < _n);
        return leader(a) == leader(b);
    }

    int leader(int a) {
        assert(0 <= a && a < _n);
        return _leader(a);
    }

    int size(int a) {
        assert(0 <= a && a < _n);
        return -parent_or_size[leader(a)];
    }

    std::vector<std::vector<int>> groups() {
        std::vector<int> leader_buf(_n), group_size(_n);
        for (int i = 0; i < _n; i++) {
            leader_buf[i] = leader(i);
            group_size[leader_buf[i]]++;
        }
        std::vector<std::vector<int>> result(_n);
        for (int i = 0; i < _n; i++) {
            result[i].reserve(group_size[i]);
        }
        for (int i = 0; i < _n; i++) {
            result[leader_buf[i]].push_back(i);
        }
        result.erase(
            std::remove_if(result.begin(), result.end(),
                           [&](const std::vector<int>& v) { return v.empty(); }),
            result.end());
        return result;
    }

  private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size;

    int _leader(int a) {
        if (parent_or_size[a] < 0) return a;
        return parent_or_size[a] = _leader(parent_or_size[a]);
    }
};

signed main(){
    int n; cin >> n;
    vi a(n); rep(i, 0, n) cin >> a[i];
    dsu d(n);
    vec<pair<int, int>> sa(n); rep(i, 0, n) sa[i] = {a[i], i}; sort(sa.begin(), sa.end());
    rep(i, 2, 320) {
        bool ok = true;
        int b = -1;
        rep(j, 0, n) {
            if(a[j] % i == 0) {
                if(b != -1 && !d.same(j, b)) d.merge(j, b);
                while(a[j] % i == 0) a[j] /= i;
                b = j;
            }
        }
    }
    map<int, int> mp;
    rep(i, 0, n) {
        if(a[i] != 1) {
            if(mp.find(a[i]) != mp.end() && !d.same(mp[a[i]], i)) d.merge(mp[a[i]], i);
            else mp[a[i]] = i; 
        }
    }
    rep(i, 0, n) {
        if(!d.same(sa[i].second, i)) {cout << 0 << endl; return 0;}
    }
    cout << 1 << endl;
}
accuracy: 1
time_ms: 220
memory_kb: 5232
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)

struct UnionFind {
	vector<int> par;
	
	UnionFind(int N) : par(N, -1) {}
	
	int root(int x) {
		if (par[x] < 0) return x;
		else return par[x] = root(par[x]);
	}
	
	bool merge(int x, int y) {
		if ((x = root(x)) == (y = root(y))) return false;
		if (par[x] > par[y]) swap(x, y);
		par[x] += par[y];
		par[y] = x;
		return true;
	}
	
	bool same(int x, int y) {
		return root(x) == root(y);
	}
};

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	
	int N;
	cin >> N;
	vector<int> A(N);
	vector<pair<int, int>> sorted(N);
	rep(i, 0, N) {
		cin >> A[i];
		sorted[i] = {A[i], i};
	}
	sort(sorted.begin(), sorted.end());
	
	int maxi = 100010;
	vector<int> isPrime(maxi, 1);
	isPrime[0] = isPrime[1] = -1;
	rep(p, 2, maxi) {
		if (isPrime[p] == -1) continue;
		for (int q = p*2; q < maxi; q += p) isPrime[q] = -1;
	}
	
	int cnt = 0;
	rep(i, 0, maxi) if (isPrime[i] == 1) {
		isPrime[i] = cnt;
		++cnt;
	}
	
	UnionFind uf(N + cnt);
	rep(i, 0, N) {
		for (int p = 2; p*p <= A[i]; ++p) if (A[i] % p == 0) {
			uf.merge(i, isPrime[p] + N);
			while (A[i] % p == 0) A[i] /= p;
		}
		if (A[i] != 1) uf.merge(i, isPrime[A[i]] + N);
	}
	
	bool can = true;
	rep(i, 0, N) {
		if (!uf.same(sorted[i].second, i)) can = false;
	}
	cout << (can ? 1 : 0) << '\n';
}
accuracy: 1
time_ms: 40
memory_kb: 5180
final_rank: 1

code_snippet: #include"bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
typedef long long ll;
typedef vector<ll> vi;
typedef pair<int,int> P;
constexpr ll mod = 998244353;
int par[200005],siz[200005];

int find(int x){
	if(x==par[x])return x;
	return par[x]=find(par[x]);
}
void unite(int x,int y){
	x=find(x),y=find(y);
	if(x==y)return;
	if(siz[x]>siz[y])swap(x,y);
	par[x]=par[y];siz[y]+=siz[x];
}

int a[200005];
vector<int>G[200005];
int main(){
	int n;cin>>n;
	rep(i,n)cin>>a[i];
	rep(i,n+100000){
		par[i]=i;siz[i]=1;
	}

	rep(i,n){
		int x=a[i];
		for(int j=2;j*j<=x;j++){
			if(x%j==0)unite(i,n+j);
			while(x%j==0)x/=j;
		}
		if(x>1)unite(i,n+x);
	}
	rep(i,n){
		G[find(i)].push_back(i);
	}
	vector<P>V;
	rep(i,n)V.push_back(P(a[i],i));
	sort(all(V));
	rep(i,n){
		V[i].first=i;
		swap(V[i].first,V[i].second);
	}
	sort(all(V));
	rep(i,n+100000){
		vector<int>v2;
		rep(j,G[i].size()){
			v2.push_back(V[G[i][j]].second);
		}
		sort(all(v2));sort(all(G[i]));
		if(v2!=G[i]){
			cout<<0<<endl;return 0;
		}
	}
	cout<<1<<endl;
}
accuracy: 1
time_ms: 60
memory_kb: 12236
final_rank: 12

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
#define mod 1000000007
vector<ll> g[100010];
ll n,a[100010];
vector<ll> p[100010];

bool vis[100010];
vector<ll> t;
vector<pair<ll,ll> > v;
void dfs(ll x){
    if(vis[x])return;
    vis[x]=1;
    t.push_back(x);
    v.push_back(make_pair(a[x],x));
    for(auto y:g[x]){
	dfs(y);
    }
}
ll renew[100010];
int main(){
  cin>>n;
  for(int i=0;i<n;i++){
      cin>>a[i];
      ll x=a[i];
      for(ll k=2;k*k<=x;k++){
	  if(x%k==0)p[k].push_back(i);
	  while(x%k==0)x/=k;
      }
      if(x>1)p[x].push_back(i);
  }
  for(ll k=2;k<100010;k++){
      for(int i=1;i<p[k].size();i++){
	  g[p[k][i-1]].push_back(p[k][i]);
	  g[p[k][i]].push_back(p[k][i-1]);
	  //cout<<a[p[k][i]]<<" "<<a[p[k][i-1]]<<endl;
      }
  }
  for(int i=0;i<n;i++)vis[i]=0;
  for(int i=0;i<n;i++){
      v.clear();
      t.clear();
      dfs(i);
      if(v.size()>0){
	  sort(v.begin(),v.end());
	  sort(t.begin(),t.end());
	  for(int j=0;j<v.size();j++){
	      renew[t[j]]=v[j].first;
	      //cout<<t[j]<<" ";
	  }//cout<<endl;
      }
  }
  bool ok=1;
  for(int i=0;i<n-1;i++){
      ok&=(renew[i]<=renew[i+1]);
  }
  cout<<ok<<endl;
  return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 28596
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <bitset>
#include <iomanip>
using namespace std;
using ll = long long;
using VVI = vector<vector<int>>;
using VVL = vector<vector<ll>>;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
using VC = vector<char>;
using VP = vector<pair<int, int>>;
using Graph0 = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define drep(i, a, b) for (int i = (int)(a);i >= (int)(b);i--)
#define urep(i, a, b) for (int i = (int)(a);i <= (int)(b);i++)
#define lrep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ldrep(i, a, b) for (ll i = (ll)(a);i >= (ll)(b);i--)
#define lurep(i, a, b) for (ll i = (ll)(a);i <= (ll)(b);i++)
#define arep(i, v) for (auto i : v)
#define all(a) (a).begin(), (a).end()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define eyes cout << "Yes" << endl;exit(0);
#define eno cout << "No" << endl;exit(0);
template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;  
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b; 
    return true;
  }
  return false;
}
template<typename T>
void excout(T A) {
  cout << A << endl;
  exit(0);
}
constexpr long long INF = (1LL << 60); // INFにちゅういい！


struct Edge
{
  int to;
  int cost;
};
using Graph = std::vector<std::vector<Edge>>;
using Pair = std::pair<long long, int>;
void Dijkstra(const Graph& graph, std::vector<long long>& distances, int startIndex)
{
  std::priority_queue<Pair, std::vector<Pair>, std::greater<Pair>> q;
  q.emplace((distances[startIndex] = 0), startIndex);

  while (!q.empty())
  {
    const long long distance = q.top().first;
    const int from = q.top().second;
    q.pop();
    if (distances[from] < distance)
    {
      continue;
    }
    for (const auto& edge : graph[from])
    {
      const long long d = (distances[from] + edge.cost);
      if (d < distances[edge.to])
      {
        q.emplace((distances[edge.to] = d), edge.to);
      }
    }
  }
}
template<typename T>
T MODS(T a, T mods) {
  return ((((((a + mods) % mods) + mods) % mods)));
}
VVL comb(int n, int r) {
  VVL v(n + 1, VL (n + 1, 0));
  for (int i = 0; i < v.size(); i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (int j = 1; j < v.size(); j++) {
    for (int k = 1; k < j; k++) {
      v[j][k] = (v[j - 1][k - 1] + v[j - 1][k]);
    }
  }
  return v;
}
vector<pair<long long, long long> > prime_factorize(long long N) {
    vector<pair<long long, long long> > res;
    for (long long p = 2; p * p <= N; ++p) {
        if (N % p != 0) {
            continue;
        }
        int e = 0;
        while (N % p == 0) {
            ++e;
            N /= p;
        }
        res.emplace_back(p, e);
    }
    if (N != 1) {
        res.emplace_back(N, 1);
    }
    return res;
}

struct UnionFind {
    vector<int> par, siz;
    UnionFind(int n) : par(n, -1), siz(n, 1) {}
    int root(int x) {
        if (par[x] == -1) return x;
        else return par[x] = root(par[x]);
    }
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    bool unite(int x, int y) {
        x = root(x);y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        par[y] = x;
        siz[x] += siz[y];
        return true;
    }
    int size(int x) {
        return siz[root(x)];
    }
};
template<class Abel> struct WUnionFind {
  vector<int> par;
  vector<int> rank;
  vector<Abel> diff_weight;

  WUnionFind(int n = 1, Abel SUM_UNITY = 0) {
    init(n, SUM_UNITY);
  }

  void init(int n = 1, Abel SUM_UNITY = 0) {
    par.resize(n); rank.resize(n); diff_weight.resize(n);
    for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY;
  }

  int root(int x) {
    if (par[x] == x) {
      return x;
    }
    else {
      int r = root(par[x]);
      diff_weight[x] += diff_weight[par[x]];
      return par[x] = r;
    }
  }

  Abel weight(int x) {
    root(x);
    return diff_weight[x];
  }

  bool issame(int x, int y) {
    return root(x) == root(y);
  }

  bool merge(int x, int y, Abel w) { //W(y)=W(x)+w
    w += weight(x); w -= weight(y);
    x = root(x); y = root(y);
    if (x == y) return false;
    if (rank[x] < rank[y]) swap(x, y), w = -w;
    if (rank[x] == rank[y]) ++rank[x];
    par[y] = x;
    diff_weight[y] = w;
    return true;
  }

  Abel diff(int x, int y)  { // W(y) - W(x)
    return weight(y) - weight(x);
  }
};
VI topo_sort(Graph0& G) {
  int N = G.size();
  VI IND(N, 0);
  rep(v, N) {
    arep(nv, G[v]) {
      IND[nv]++;
    }
  }
  queue<int> que;
  rep(v, N) {
    if (IND[v] == 0) {
      que.push(v);
    }
  }
  VI ANS;
  while (!que.empty()) {
    int v = que.front();
    ANS.push_back(v);
    que.pop();
    arep(nv, G[v]) {
      IND[nv]--;
      if (IND[nv] == 0) {
        que.push(nv);
      }
    }
  }
  return ANS;
}
void ADD(int a, int b, Graph0& G) {
  G[a].push_back(b);
  G[b].push_back(a);
}
VP near(int i, int j, int H, int W) {
  VP ans;
  VP cand = {{i - 1, j}, {i + 1, j}, {i, j - 1}, {i, j + 1}};
  arep(v, cand) {
    if (v.first < 0 or v.first >= H) continue;
    if (v.second < 0 or v.second >= W) continue;
    ans.push_back(v);
  }
  return ans;
}
int cast(int i, int j, int H, int W) {
  return ((W * i) + j);
}
ll pows(ll x, ll n, ll mod) {
    if (!n) return 1;
    x %= mod;
    ll r = pows(x, n / 2, mod);
    (r *= r) %= mod;
    if (n % 2) (r *=x) %= mod;
    return r;
}
struct COMB_MOD {
  ll mod;
  int MAX;
  VL fac, finv, inv;
  COMB_MOD(int max, ll m) {
    fac.assign(max, 0);
    finv.assign(max, 0);
    inv.assign(max, 0);
    mod = m;
    MAX = max;
  }
  void solve() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % mod;

        inv[i] = mod - inv[mod%i] * (mod / i) % mod;
        finv[i] = finv[i - 1] * inv[i] % mod;
    }
    
  }
  ll comb(int n, int k) {
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
  }
};

struct LCA {
    vector<vector<int>> parent;  
    vector<int> dist;           
    LCA(const Graph0 &G, int root = 0) { init(G, root); }
    void init(const Graph0 &G, int root = 0) {
        int V = G.size();
        int K = 1;
        while ((1 << K) < V) K++;
        parent.assign(K, vector<int>(V, -1));
        dist.assign(V, -1);
        dfs(G, root, -1, 0);
        for (int k = 0; k + 1 < K; k++) {
            for (int v = 0; v < V; v++) {
                if (parent[k][v] < 0) {
                    parent[k + 1][v] = -1;
                } else {
                    parent[k + 1][v] = parent[k][parent[k][v]];
                }
            }
        }
    }
    void dfs(const Graph0 &G, int v, int p, int d) {
        parent[0][v] = p;
        dist[v] = d;
        for (auto e : G[v]) {
            if (e != p) dfs(G, e, v, d + 1);
        }
    }
    int query(int u, int v) {
        if (dist[u] < dist[v]) swap(u, v);  
        int K = parent.size();
        for (int k = 0; k < K; k++) {
            if ((dist[u] - dist[v]) >> k & 1) {
                u = parent[k][u];
            }
        }
        if (u == v) return u;
        for (int k = K - 1; k >= 0; k--) {
            if (parent[k][u] != parent[k][v]) {
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }
    int get_dist(int u, int v) { return dist[u] + dist[v] - 2 * dist[query(u, v)]; }
};
int main(void) {
  int N;cin >> N;
  int K = 1e5 + 1;
  VI A(N);
  VI P(K+1, -1);
  rep(i, N){
    cin >> A[i];
    P[A[i]] = i;
  }
  UnionFind uf(K+1);
  urep(k, 2, K) {
    int now = k;
    VI value;
    while (now <= K) {
      if (P[now] >= 0) value.push_back(now);
      now += k;
    }
    rep(i, value.size()-1) uf.unite(value[i],value[i+1]);
  }
  vector<vector<pair<int,int>>> CP(K+1);
  rep(i, N) CP[uf.root(A[i])].push_back({A[i], i});
  rep(i, K+1) {
    sort(all(CP[i]));
    VI ID;
    arep(v, CP[i]) ID.push_back(v.second);
    sort(all(ID));
    rep(t, ID.size()) A[ID[t]] = CP[i][t].first;
  }
  rep(i, N-1) {
    if (A[i] > A[i+1]) {
      cout << 0 << endl;
      return 0;
    }
  }
  cout << 1 << endl;

}
accuracy: 1
time_ms: 20
memory_kb: 9012
final_rank: 3

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll = long long;

struct UnionFind{
    int N;
    vector<int> parent;

    UnionFind(int N):N(N),parent(N,-1){}

    int leader(int x){
        if(parent[x]<0) return x;
        return parent[x] = leader(parent[x]);
    }


    void merge(int x,int y){
        int xp = leader(x);
        int yp = leader(y);
        if(xp==yp) return ;
        if(xp<yp) swap(xp,yp);
        parent[yp] += parent[xp];
        parent[xp] = yp;
    }

    bool same(int x,int y){
        return leader(x)==leader(y);
    }

    int size(int x){
        return -parent[leader(x)];
    }

    vector<vector<int>> groups(){
        vector<vector<int>> a(N);
        rep(i,N) a[leader(i)].push_back(i);
        vector<vector<int>> uni;
        rep(i,N) if(!a[i].empty()) uni.push_back(a[i]);

        return uni;
    }
};




int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N;
    cin >> N;
    vector<int>a(N);
    rep(i,N)cin>>a[i];

    map<int,int> ma;
    
    UnionFind uf(N);

    rep(i,N){
        ll x = a[i];
        for(int j=2;j*j<=x;j++){
            if(x%j==0){
                if(ma.count(j)) uf.merge(ma[j],i);
                ma[j]=i;
                while(x%j==0){
                    x/=j;
                }
            }
        }
        if(x>1){
            if(ma.count(x)) uf.merge(ma[x],i);
            ma[x]=i;
        }
    }

    vector<vector<int>> g = uf.groups();

    vector<int> sorted(N);
    for(vector<int> v:g){
        sort(v.begin(),v.end());
        vector<int> b;
        for(int e: v) b.push_back(a[e]);
        sort(b.begin(),b.end());
        rep(i,v.size()){
            sorted[v[i]] = b[i];
        }
    }
    rep(i,N-1){
        if(sorted[i]>sorted[i+1]){
            cout << 0 << endl;
            return 0;
        }
    }
    cout << 1 << endl;

}
accuracy: 1
time_ms: 150
memory_kb: 8360
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,N) for(ll i=0;i<N;i++)
#define all(A) A.begin(),A.end()
#define fi first
#define se second
const ll mod = 1e9+7;
const ll INF = 1LL<<60;

struct unionfind{
	unionfind() {}
	vector<ll> par,sz;
	unionfind(ll N):par(N),sz(N){
		rep(i,N) par[i]=i,sz[i]=1;
	}
	ll root(ll x){
		if(par[x]==x) return x;
		ll r=root(par[x]);
		par[x]=r;
		return r;
	}
	void unite(ll x,ll y){
		ll rx=root(x);
		ll ry=root(y);
		if(rx==ry) return;
		if(sz[rx]>sz[ry]) swap(rx,ry);
		par[rx]=ry;
		sz[ry]+=sz[rx];
	}
	bool same(ll x,ll y){
		ll rx=root(x);
		ll ry=root(y);
		return rx==ry;
	}
	ll size(ll x){
		return sz[x];
	}
};					

int main(){
	ll N;
	cin >> N;
	vector<ll> A(N);
	rep(i,N) cin >> A[i];
	vector<pair<ll,ll>> P(N);
	rep(i,N) P[i].fi=A[i],P[i].se=i;
	sort(all(P));
	vector<ll> inv(100500,-1);
	rep(i,N) inv[A[i]]=i;
	vector<vector<ll>> G(100500);
	rep(i,N) G[A[i]].push_back(i);
	unionfind tree(N);
	rep(i,100500){
		rep(j,(ll)G[i].size()-1){
			tree.unite(G[i][j],G[i][j+1]);
		}
	}
	for(ll i=2;i<100500;i++){
		vector<ll> V;
		for(ll j=i;j<100500;j+=i){
			if(inv[j]!=-1) V.push_back(inv[j]);
		}
		rep(j,(ll)V.size()-1){
			tree.unite(V[j],V[j+1]);
		}
	}
	bool ok=true;
	rep(i,N){
		if(!tree.same(i,P[i].se)) ok=false;
	}
	if(ok){
		cout << 1 << endl;
	}else{
		cout << 0 << endl;
	}									
}
accuracy: 1
time_ms: 30
memory_kb: 13952
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
vector<int> sosuu(0);
map<int,int> soinsuu;
int check(int i){
    int ans=1;
    if(!soinsuu.count(i)){
        int r=i;
        for(int j=0;j<sosuu.size()&&r>=sosuu.at(j)&&!soinsuu.count(r);j+=1){
            int e=0;
            while(r%sosuu.at(j)==0){
                e=1;
                r/=sosuu.at(j);
            }
            if(e==1){
                ans*=sosuu.at(j);
            }
        }
        soinsuu[i]=ans*soinsuu[r];
        soinsuu[ans*soinsuu[r]]=ans*soinsuu[r];
    }
    return 0;
}
int yakusuu(int a,int b){
    while(b!=0){
        a-=(a/b*b);
        swap(a,b);
    }
    return a;
}
int main(){
    soinsuu[1]=1;
    sosuu.push_back(2);
    for(long i=3,o=1;i<100000;i+=2,o=1){
        for(int j=3;j*j<=i;j+=1){
            if(i%j==0){
                o=0;
                break;
            }
        }
        if(o==1){
            soinsuu[i]=i;
            sosuu.push_back(i);
        }
    }
    int a;
    cin>>a;
    vector<int> pre(a);
    vector<int> srt(a);
    for(int i=0;i<a;i+=1){
        cin>>pre.at(i);
        srt.at(i)=pre.at(i);
        check(pre.at(i));
    }
    sort(srt.begin(),srt.end());
    vector<int> kouho(0);
    for(int i=0;i<a;i+=1){
        if(yakusuu(pre.at(i),srt.at(i))==1){
            kouho.push_back(soinsuu[pre.at(i)]*soinsuu[srt.at(i)]/yakusuu(soinsuu[pre.at(i)],soinsuu[srt.at(i)]));
        }
    }
    for(int i=0;i<kouho.size();i+=1){
        if(!soinsuu.count(kouho.at(i))){
            cout<<0<<endl;
            return 0;
        }
    }
    cout<<1<<endl;
}
accuracy: 0.05
time_ms: 20
memory_kb: 3280
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll gcd(ll a, ll b) {return (b == 0 ? a : gcd(b, a % b));}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
struct UnionFind {
    vector<ll> par,siz;
    UnionFind(ll N) : par(N, -1), siz(N, 1) {}
    ll root(ll v) {
        if (par[v] == -1) return v;
        else return par[v] = root(par[v]);
    }
    bool same(ll u, ll v) {
        return root(u) == root(v);
    }
    bool unite(ll u, ll v) {
        u = root(u), v = root(v);
        if (u == v) return false;
        if (siz[u] < siz[v]) swap(u, v);
        par[v] = u;
        siz[u] += siz[v];
        return true;
    }
};
int main() {
	ll N; cin >> N;
    UnionFind uf(N);
    vector<ll>A(N);
    vector<vector<ll>>B(100001);
    for (ll i = 0; i < N; i++) {
        cin >> A[i];
        B[A[i]].push_back(i);
        for (ll j = 2; j * j <= A[i]; j++) {
            if (A[i] % j == 0) {
                B[j].push_back(i);
                if (j * j != A[i] && A[i] % (A[i] / j) == 0) {
                    B[A[i] / j].push_back(i);
                }
            }
        }
    }
    for (ll i = 0; i <= 100000; i++) {
        for (ll j = 0; j + 1 < B[i].size(); j++) {
            uf.unite(B[i][j], B[i][j + 1]);
        }
    }
    vector<P>C(N);
    for (ll i = 0; i < N; i++) {
        C[i] = make_pair(A[i], i);
    }
    sort(C.begin(), C.end());
    for (ll i = 0; i < N; i++) {
        if (!uf.same(i, C[i].second)) {
            cout << 0 << endl;
            return 0;
        }
    }
    cout << 1 << endl;
}
accuracy: 1
time_ms: 170
memory_kb: 23428
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

struct UnionFind {
	vector<int> par;
	UnionFind(const int n) : par(n, -1) {}
	int find(int u) {
		return par[u] < 0 ? u : par[u] = find(par[u]);
	}
	void merge(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y) return;
		if (par[x] > par[y]) swap(x, y);
		par[x] += par[y];
		par[y] = x;
	}
};

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);


	int N;
	cin >> N;
	vector<int> A(N);
	vector<vector<int>> list(100001);
	for (int i = 0; i < N; ++i) {
		cin >> A[i];
		list[A[i]].push_back(i);
	}

	UnionFind uf(N);
	for (int d = 2; d <= 100000; ++d) {
		int id = -1;
		for (int n = d; n <= 100000; n += d) {
			for (const int i : list[n]) {
				if (id == -1) {
					id = i;
				} else {
					uf.merge(id, i);
				}
			}
		}
	}

	vector<vector<int>> groups(N);
	for (int i = 0; i < N; ++i) {
		groups[uf.find(i)].push_back(i);
	}
	for (const auto& id : groups) {
		vector<int> val;
		for (const int i : id) {
			val.push_back(A[i]);
		}
		sort(begin(val), end(val));
		for (int i = 0; i < (int)size(val); ++i) {
			A[id[i]] = val[i];
		}
	}
	
	cout << is_sorted(begin(A), end(A)) << '\n';

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 12904
final_rank: 8

code_snippet: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define vi vector<int>
using namespace std;

struct UnionFind {
  vi d;
  UnionFind(int n) : d(n, -1){}
  int root(int x){
    if(d[x] < 0) return x;
    return d[x] = root(d[x]);
  }
  bool unite(int x, int y){
    x = root(x);
    y = root(y);
    if(x == y) return false;
    if(d[x] > d[y]) swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
};

int main(){
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  UnionFind tree(n);
  for(int i = 2; i*i <= 100000; i++){
    int last = -1;
    rep(j, n) if(a[j] % i == 0){
      if(last != -1) tree.unite(last, j);
      last = j;
    }
  }
  vector<vi> group(n);
  rep(i, n) group[tree.root(i)].push_back(a[i]);
  rep(i, n) sort(group[i].rbegin(), group[i].rend());
  rep(i, n){
    vi &g = group[tree.root(i)];
    a[i] = g.back();
    g.pop_back();
  }
  rep(i, n - 1) if(a[i] > a[i+1]){
    cout << "0\n";
    return 0;
  }
  cout << "1\n";
}
accuracy: 1
time_ms: 90
memory_kb: 9384
final_rank: 13

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 0397.cc: Common-Prime Sort
 */

#include<cstdio>
#include<vector>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_P = 100000;

/* typedef */

typedef vector<int> vi;
typedef vector<vi> vvi;

struct UFT {
  vector<int> links, ranks, sizes;
  UFT() {}

  void init(int n) {
    links.resize(n);
    for (int i = 0; i < n; i++) links[i] = i;
    ranks.assign(n, 1);
    sizes.assign(n, 1);
  }

  int root(int i) {
    int i0 = i;
    while (links[i0] != i0) i0 = links[i0];
    return (links[i] = i0);
  }

  int rank(int i) { return ranks[root(i)]; }
  int size(int i) { return sizes[root(i)]; }
  bool same(int i, int j) { return root(i) == root(j); }

  int merge(int i0, int i1) {
    int r0 = root(i0), r1 = root(i1), mr;
    if (r0 == r1) return r0;
    if (ranks[r0] == ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      ranks[r0]++;
      mr = r0;
    }
    else if (ranks[r0] > ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      mr = r0;
    }
    else {
      links[r0] = r1;
      sizes[r1] += sizes[r0];
      mr = r1;
    }
    return mr;
  }
};

/* global variables */

int pdivs[MAX_P + 1];
int as[MAX_N], bs[MAX_N], gs[MAX_N], cs[MAX_N], tmp[MAX_N];
vi pds[MAX_N];
UFT uft;

/* subroutines */

int gen_primes(int maxp, vi &pnums) {
  pdivs[0] = 0, pdivs[1] = 1;

  int p;
  for (p = 2; p * p <= maxp; p++)
    if (! pdivs[p]) {
      pnums.push_back(p), pdivs[p] = p;
      for (int q = p * p; q <= maxp; q += p)
	if (! pdivs[q]) pdivs[q] = p;
    }
  for (; p <= maxp; p++)
    if (! pdivs[p]) pnums.push_back(p), pdivs[p] = p;
  return (int)pnums.size();
}

void prime_decomp(int n, vi &pds) {
  pds.clear();
  //printf("%d =",  n);
  while (n > 1) {
    int p = pdivs[n];
    pds.push_back(p);
    while (pdivs[n] == p) n /= p;
  }
  //for (auto p: pds) printf(" %d", p); putchar('\n');
}

/* main */

int main() {
  vi pnums;
  int pn = gen_primes(MAX_P, pnums);
  //printf("pn=%d\n", pn);

  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  copy(as, as + n, bs);
  sort(bs, bs + n);
  //for (int i = 0; i < n; i++) printf("%d,%d ", as[i], bs[i]); putchar('\n');

  vi pdv;
  for (int i = 0; i < n; i++) {
    prime_decomp(as[i], pds[i]);
    for (auto p: pds[i]) pdv.push_back(p);
  }

  sort(pdv.begin(), pdv.end());
  pdv.erase(unique(pdv.begin(), pdv.end()), pdv.end());
  int k = pdv.size();
  //for (auto p: pdv) printf("%d ", p); putchar('\n');
  
  uft.init(n + k);

  for (int i = 0; i < n; i++)
    for (auto p: pds[i]) {
      int j = lower_bound(pdv.begin(), pdv.end(), p) - pdv.begin();
      uft.merge(i, n + j);
    }

  fill(gs, gs + n, -1);
  int gn = 0;
  vvi vvs;
  for (int i = 0; i < n; i++) {
    int ri = uft.root(i);
    if (gs[ri] < 0) gs[ri] = gn++, vvs.push_back(vi());
    gs[i] = gs[ri];
    vvs[gs[i]].push_back(i);
  }
  
  //for (int i = 0; i < gn; i++) {
  //for (auto u: vvs[i]) printf("%d ", u); putchar('\n');
  //}

  for (auto &vs: vvs) {
    int m = 0;
    for (auto j: vs) tmp[m++] = as[j];
    sort(tmp, tmp + m);
    for (int i = 0; i < m; i++) cs[vs[i]] = tmp[i];
  }

  bool ok = true;
  for (int i = 0; ok && i < n; i++) ok = (cs[i] == bs[i]);

  if (ok) puts("1");
  else puts("0");
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 13608
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;

struct UnionFind{
  vector<int> par,siz;
  UnionFind(int n):par(n,-1),siz(n,1){}
  int root(int x){
    if(par[x] == -1) return x;
    else return par[x] = root(par[x]);
  }
  bool issame(int x,int y){
    if(root(x) == root(y)) return true;
    else return false;
  }
  bool unite(int x,int y){
    x = root(x);
    y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) swap(x,y);
    par[y] = x;
    siz[x] += siz[y];
    return true;
  }
  int size(int x){
    return siz[root(x)];
  }
};

int main(){
  int N;
  cin >> N;
  int M = 100000;
  vector<int> A(N);
  for(int i=0;i<N;i++) cin >> A[i];
  UnionFind uf(N);
  vector<vector<int>> pos(M+1);
  for(int i=0;i<N;i++) pos[A[i]].push_back(i);
  vector<int> P(0);
  for(int i=2;i<=M;i++){
    int d = i;
    for(int j=2;j*j<=i;j++){
      if(i%j == 0) d /= j;
    }
    if(d == i) P.push_back(i);
  }
  for(int i=0;i<P.size();i++){
    int p = P[i];
    vector<int> B(0);
    for(int j=p;j<=M;j+=p){
      for(int k=0;k<pos[j].size();k++){
        B.push_back(pos[j][k]);
      }
    }
    for(int j=1;j<B.size();j++) uf.unite(B[0],B[j]);
  }
  vector<int> SA(N);
  for(int i=0;i<N;i++) SA[i] = A[i];
  sort(SA.begin(),SA.end());
  vector<vector<int>> tobe(M+1);
  for(int i=0;i<N;i++) tobe[SA[i]].push_back(i);
  vector<int> POS(N);
  for(int i=0;i<N;i++){
    int a = A[i];
    POS[i] = tobe[a].back();
    tobe[a].pop_back();
  }
  bool flag = true;
  for(int i=0;i<N;i++) if(!uf.issame(i,POS[i])) flag = false;
  if(flag) cout << 1 << endl;
  else cout << 0 << endl;
}
accuracy: 1
time_ms: 80
memory_kb: 15740
final_rank: 14

code_snippet: #include "bits/stdc++.h"

#define REP(i,num) for(ll i=0;i<(num);++i)
#define FOR(i,c,num) for(ll (i)=(c);(i)<(num);++(i))
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;
using ull = unsigned long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=ll>
T in(){ T x; cin >> x; return (x); }
template<typename T=ll,typename C=vector<T>>
C vecin(int N){ C x(N);REP(i,N){ x[i]=in<T>(); }return x; }
template<typename T=ll,size_t N>
array<T,N> arrin(){array<T,N> x;REP(i,N){ x[i]=in<T>(); }return x;}

void vout(){ cout << endl; }
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){ cout << ' ' << h;vout(forward<Tail>(t)...); }
void out(){ cout << endl; }
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){ cout << h;vout(forward<Tail>(t)...); }

template<typename T>
bool chmax(T& a,T b){ if(a<b){ a=b;return true; }return false; }
template<typename T>
bool chmin(T& a,T b){ if(a>b){ a=b;return true; }return false; }

vector<ll> enum_prime(ll N){
	deque<bool> is_prime(N+1,true);
	vector<ll> P;
	for(ll i=2;i<=N;i++){
		if(is_prime[i]){
			for(ll j=i*i;j<=N;j+=i){
				is_prime[j] = false;
			}
			P.emplace_back(i);
		}
	}
	return P;
}

class UnionFind{
	vector<ll> rank;
	vector<ll> diff_weight;
	vector<ll> num;
public:
	vector<ll> par;

	UnionFind(ll N):par(N),num(N),rank(N),diff_weight(N){
		for(ll i=0;i<N;i++){
			par[i]=i;
			num[i]=1;
			rank[i]=0;
			diff_weight[i]=0;
		}
	}

	ll root(ll x){
		if(par[x]==x){
			return x;
		}
		ll r = root(par[x]);
		diff_weight[x]+=diff_weight[par[x]];
		return par[x]=r;
	}

	void unite(ll x,ll y){
		ll rx = root(x);
		ll ry = root(y);
		if(rx == ry) return;
		if(rank[rx]<rank[ry]) swap(rx,ry);
		if(rank[rx]==rank[ry]) ++rank[rx];
		par[ry] = rx;
		num[rx] += num[ry];
	}

	// yはxよりw大きい
	void relate(ll x,ll y,ll w){
		w+=weight(x),w-=weight(y);
		ll rx = root(x);
		ll ry = root(y);
		if(rx == ry) return;
		if(rank[rx]<rank[ry]){
			swap(rx,ry);
			w = -w;
		}
		if(rank[rx]==rank[ry]) ++rank[rx];
		par[ry] = rx;
		diff_weight[ry] = w;
		num[rx] += num[ry];
	}

	bool same(ll x,ll y){
		ll rx = root(x);
		ll ry = root(y);
		return rx == ry;
	}

	ll getsize(ll x){
		return num[root(x)];
	}

	ll weight(ll x){
		root(x);
		return diff_weight[x];
	}

	ll diff(ll x,ll y){
		return weight(y)-weight(x);
	}
};

vector<ll> prime_factor(ll n,vll& prime_list){
	ll sn = sqrt(n)+1;
	vector<ll> res;
	for(int i=0,ei=prime_list.size();i<ei;i++){
		ll v = prime_list[i];
		if(v*v>n) break;
		while(!(n%v)){
			n /= v;
			res.push_back(v);
		}
	}
	if(n>1){
		res.push_back(n);
	}

	return res;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	ll N=in();
	vll A=vecin(N);
	vll B=A;
	sort(ALL(B));
	
	vll P=enum_prime(100010);
	UnionFind uf(100010);
	REP(i,N){
		auto F=prime_factor(A[i],P);
		sort(ALL(F));
		F.erase(unique(ALL(F)),F.end());
		for(auto& x:F) uf.unite(A[i],x);
	}
	REP(i,N){
		if(!uf.same(A[i],B[i])){
			out(0);
			return 0;
		}
	}
	out(1);
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 8040
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vc = vector<T>;
template<class T> using vvc = vc<vc<T>>;
template<class T> using vvvc = vc<vvc<T>>;
template<class T> using vvvvc = vvc<vvc<T>>;
template<class T> using PQ = priority_queue<T>;
template<class T> using invPQ = priority_queue<T, vector<T>, greater<T>>;
using IP = pair<int, int>;
using LP = pair<ll, ll>;
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define rep3(i, m, n) for (int i = (m); (i) < (int)(n); i++)
#define repr(i, n) for (int i = (n) - 1; (i) >= 0; i--)
#define rep3r(i, m, n) for (int i = (n) - 1; (i) >= (int)(m); i--)
template<class T> inline bool chmax(T & a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T & a, T b) { if (a > b) { a = b; return 1; } return 0; }

struct UnionFind {
    vector<int> par, siz;
    UnionFind(int n) : par(n, -1), siz(n, 1) { }
    int root(int x) {
        if (par[x] == -1) return x;
        else return par[x] = root(par[x]);
    }
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (siz[x] < siz[y]) swap(x, y);
        par[y] = x;
        siz[x] += siz[y];
        return true;
    }
    int size(int x) {
        return siz[root(x)];
    }
};

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int N;
    cin >> N;
    vc<int> a(N);
    rep (i, N) cin >> a[i];

    vc<bool> isin(500001);
    rep (i, N) isin[a[i]] = true;
    
    UnionFind uf(1000001);
    rep3 (i, 2, 500001) {
        for (int j = i; j <= 500001; j+=i) {
            if (isin[j]) uf.unite(i + 500000, j);
        }
    }

    vc<int> sorted = a;
    sort(all(sorted));

    bool judge = true;
    rep (i, N) {
        if (!uf.issame(a[i], sorted[i])) judge = false;
    }

    cout << judge << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 11656
final_rank: 7

code_snippet: /*
このコード、と～おれ!
Be accepted!
∧＿∧　
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
		  　　　　　　　　　　(¸.·´ (¸.·'* ☆
*/

#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>
#include <regex>
#include <functional>
#include <complex>
#include <list>
#include <cassert>
#include <iomanip>
#include <set>
#include <stack>
#include <bitset>
#include <array>
#include <chrono>

//#pragma GCC target("arch=skylake-avx512")
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse4")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define repeat(i, n, m) for(int i = n; i < (m); ++i)
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printim(a) printf(a ? "possible\n" : "imposible\n")
#define printdb(a) printf("%.50lf\n", a)
#define printLdb(a) printf("%.50Lf\n", a)
#define printdbd(a) printf("%.16lf\n", a)
#define prints(s) printf("%s\n", s.c_str())
#define all(x) (x).begin(), (x).end()
#define deg_to_rad(deg) (((deg)/360.0L)*2.0L*PI)
#define rad_to_deg(rad) (((rad)/2.0L/PI)*360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d) (abs(a - c) + abs(b - d))

using ll = long long;
using ull = unsigned long long;

constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-6;
const long double PI = acosl(-1.0L);

using namespace std;

void scans(string& str) {
	char c;
	str = "";
	scanf("%c", &c);
	if (c == '\n')scanf("%c", &c);
	while (c != '\n' && c != -1 && c != ' ') {
		str += c;
		scanf("%c", &c);
	}
}

void scanc(char& str) {
	char c;
	scanf("%c", &c);
	if (c == -1)return;
	while (c == '\n') {
		scanf("%c", &c);
	}
	str = c;
}

double acot(double x) {
	return PI / 2 - atan(x);
}

ll LSB(ll n) { return (n & (-n)); }

template<typename T>
inline T chmin(T& a, const T& b) {
	if (a > b)a = b;
	return a;
}

template<typename T>
inline T chmax(T& a, const T& b) {
	if (a < b)a = b;
	return a;
}

//cpp_int
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using namespace boost::multiprecision;
#else
using cpp_int = ll;
#endif

//atcoder library
#if __has_include(<atcoder/all>)
#include <atcoder/all>
//using namespace atcoder;
#endif

/*
	random_device seed_gen;
	mt19937 engine(seed_gen());
	uniform_int_distribution dist(1, 100);
*/


/*----------------------------------------------------------------------------------*/


struct unionfind {
	vector<int> par, siz;

	unionfind(int n) : par(n), siz(n) {
		for (int i = 0; i < n; ++i) {
			//全部根で初期化
			par[i] = i;
			//サイズは1
			siz[i] = 1;
		}
	}

	void init(int n) {
		par.resize(n);
		siz.resize(n);
		for (int i = 0; i < n; ++i) {
			//全部根で初期化
			par[i] = i;
			//サイズは1
			siz[i] = 1;
		}
	}

	//根を返す
	int find(int a) {
		return par[a] == a ? a : par[a] = find(par[a]);
	}

	//くっつける。元から同じだったらfalseを返す
	bool unite(int a, int b) {
		int x = find(a), y = find(b);
		if (x == y)return false;
		else if (siz[x] < siz[y]) {
			par[x] = y;
			siz[y] += siz[x];
		}
		else if (siz[x] > siz[y]) {
			par[y] = x;
			siz[x] += siz[y];
		}
		else {
			par[x] = y;
			siz[y] += siz[x];
		}
		return true;
	}

	//同じ集合か判定する
	bool same(int a, int b) {
		return find(a) == find(b);
	}

	//サイズを返す
	int size(int a) {
		return siz[find(a)];
	}

};

int main() {

	int n;
	scanf("%d", &n);
	vector<int> a(n);
	rep(i, n)scanf("%d", &a[i]);
	unionfind uni(200001);
	vector<pair<int, int>> p(n);
	rep(i, n)p[i] = { a[i], i };
	sort(all(p));
	rep(i, n) {
		for (int j = 2; j * j <= a[i]; ++j) {
			if (a[i] % j == 0) {
				uni.unite(i, 100000 + j);
				while (a[i] % j == 0)a[i] /= j;
			}
		}
		if (a[i] != 1)uni.unite(i, 100000 + a[i]);
	}
	int ok = 1;
	rep(i, n) {
		if(not(uni.same(i, p[i].second)))
			ok = 0;
	}
	printf("%d\n", ok);

	Please AC;
}
accuracy: 1
time_ms: 50
memory_kb: 5752
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++){
    cin >> a[i];
  }
  int V = N + 100000;
  vector<vector<int>> E(V);
  for (int i = 0; i < N; i++){
    for (int j = 1; j * j <= a[i]; j++){
      if (a[i] % j == 0){
        if (j > 1){
          int v = N - 1 + j;
          E[i].push_back(v);
          E[v].push_back(i);
        }
        if (j * j < a[i]){
          int w = N - 1 + a[i] / j;
          E[i].push_back(w);
          E[w].push_back(i);
        }
      }
    }
  }
  vector<bool> used(V, false);
  vector<int> b(N);
  for (int i = 0; i < N; i++){
    if (!used[i]){
      used[i] = true;
      queue<int> Q;
      Q.push(i);
      vector<int> id, x;
      int cnt = 0;
      while (!Q.empty()){
        int v = Q.front();
        Q.pop();
        if (v < N){
          id.push_back(v);
          x.push_back(a[v]);
          cnt++;
        }
        for (int w : E[v]){
          if (!used[w]){
            used[w] = true;
            Q.push(w);
          }
        }
      }
      sort(id.begin(), id.end());
      sort(x.begin(), x.end());
      for (int j = 0; j < cnt; j++){
        b[id[j]] = x[j];
      }
    }
  }
  sort(a.begin(), a.end());
  if (a == b){
    cout << 1 << endl;
  } else {
    cout << 0 << endl;
  }
}
accuracy: 1
time_ms: 100
memory_kb: 25412
final_rank: 17

code_snippet: #include<bits/stdc++.h>

using namespace std;
using ll = long long;
using pint = pair<int,int>;

class UnionFind{
  private:
	vector<int> data;
  public:
	UnionFind(int n) : data(n,-1) {}
	int root(int i) { return data[i] < 0 ? i : data[i] = root(data[i]); }
	int size(int i) { return -data[root(i)]; }
	bool same(int x, int y) {return root(x) == root(y); }
	bool connected() {return size(0) == (int)data.size(); }

	bool unit(int i,int j){
		i = root(i);
		j = root(j);
		if(i != j){
			data[i] += data[j];
			data[j] = i;
		}
		return i != j;
	}
};


int main()
{
	int N; cin >> N;
	vector<int> A(N);
	for(auto &i : A) cin >> i;
	const int M = 1e5 + 1;



	UnionFind uni(N + M);
	for(int i = 0; i < N; i++) {
		int V = A[i];
		for(int j = 2; j * j <= V; j++) {
			if(V % j == 0) {
				uni.unit(i, j + N);
				while(V % j == 0) V /= j;
			}
		}
		uni.unit(i, V + N);
	}

	vector<vector<int>> S(M + 1);
	for(int i = 0; i < N; i++)S[uni.root(i)].push_back(A[i]);
	for(auto &i : S)sort(i.begin(), i.end(), greater{});

	vector<int> B(N);
	for(int i = 0; i < N; i++) {
		int rt = uni.root(i);
		B[i] = S[rt].back();
		S[rt].pop_back();
	}

	cout << is_sorted(B.begin(), B.end()) << endl;


}
accuracy: 1
time_ms: 60
memory_kb: 8104
final_rank: 9

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int lim = 100010;

struct UnionFind{
    vector<int> parent;
    UnionFind(int n){
        parent = vector<int>(n, -1);
    }

    int root(int x){
        if(parent[x] < 0) return x;
        else return parent[x] = root(parent[x]);
    }

    int count(int x){
        return -parent[root(x)];
    }

    bool same(int x, int y){
        return root(x) == root(y);
    }

    void unite(int x, int y){
        if(same(x, y)) return;
        x = root(x);
        y = root(y);
        if(count(x) < count(y)) swap(x, y);
        parent[x] += parent[y];
        parent[y] = x;
    }
};

int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    for(auto &p: a) cin >> p;

    vector<int> fac(lim);
    for(int i = 0; i < lim; i++) fac[i] = i;
    for(int i = 2; i < lim; i++){
        if(fac[i] != i) continue;
        for(int j = 2; i*j < lim; j++){
            fac[i*j] = min(fac[i*j], i);
        }
    }

    vector<int> parv(lim, -1);
    UnionFind data(n);
    for(int i = 0; i < n; i++){
        int p = a[i];
        while(p > 1){
            if(parv[fac[p]] == -1) parv[fac[p]] = i;
            else data.unite(parv[fac[p]], i);
            p /= fac[p];
        }
    }

    vector<vector<int>> nums(n);
    for(int i = 0; i < n; i++){
        nums[data.root(i)].push_back(a[i]);
    }
    for(auto &p: nums) sort(p.begin(), p.end());

    vector<int> newvec(n);
    for(int i = n-1; i >= 0; i--){
        newvec[i] = *nums[data.root(i)].rbegin();
        nums[data.root(i)].pop_back();
    }

    int ans = 1;
    for(int i = 1; i < n; i++){
        if(newvec[i-1] > newvec[i]) ans = 0;
    }
    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 8224
final_rank: 2

code_snippet: #include "bits/stdc++.h"

#define REP(i,num) for(ll i=0;i<(num);++i)
#define FOR(i,c,num) for(ll (i)=(c);(i)<(num);++(i))
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;
using ull = unsigned long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=ll>
T in(){ T x; cin >> x; return (x); }
template<typename T=ll,typename C=vector<T>>
C vecin(int N){ C x(N);REP(i,N){ x[i]=in<T>(); }return x; }
template<typename T=ll,size_t N>
array<T,N> arrin(){array<T,N> x;REP(i,N){ x[i]=in<T>(); }return x;}

void vout(){ cout << endl; }
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){ cout << ' ' << h;vout(forward<Tail>(t)...); }
void out(){ cout << endl; }
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){ cout << h;vout(forward<Tail>(t)...); }

template<typename T>
bool chmax(T& a,T b){ if(a<b){ a=b;return true; }return false; }
template<typename T>
bool chmin(T& a,T b){ if(a>b){ a=b;return true; }return false; }

class UnionFind{
	vector<ll> rank;
	vector<ll> diff_weight;
	vector<ll> num;
public:
	vector<ll> par;

	UnionFind(ll N):par(N),num(N),rank(N),diff_weight(N){
		for(ll i=0;i<N;i++){
			par[i]=i;
			num[i]=1;
			rank[i]=0;
			diff_weight[i]=0;
		}
	}

	ll root(ll x){
		if(par[x]==x){
			return x;
		}
		ll r = root(par[x]);
		diff_weight[x]+=diff_weight[par[x]];
		return par[x]=r;
	}

	void unite(ll x,ll y){
		ll rx = root(x);
		ll ry = root(y);
		if (rx == ry) return;
		if(rank[rx]<rank[ry]) swap(rx,ry);
		if(rank[rx]==rank[ry]) ++rank[rx];
		par[ry] = rx;
		num[rx] += num[ry];
	}

	void relate(ll x,ll y,ll w){
		w+=weight(x),w-=weight(y);
		ll rx = root(x);
		ll ry = root(y);
		if (rx == ry) return;
		if(rank[rx]<rank[ry]){
			swap(rx,ry);
			w = -w;
		}
		if(rank[rx]==rank[ry]) ++rank[rx];
		par[ry] = rx;
		diff_weight[ry] = w;
		num[rx] += num[ry];
	}

	bool same(ll x,ll y){
		ll rx = root(x);
		ll ry = root(y);
		return rx == ry;
	}

	ll getsize(ll x){
		return num[root(x)];
	}

	ll weight(ll x){
		root(x);
		return diff_weight[x];
	}

	ll diff(ll x,ll y){
		return weight(y)-weight(x);
	}
};

vector<ll> enum_prime(ll N){
	deque<bool> is_prime(N+1,true);
	vector<ll> P;
	for(ll i=2;i<=N;i++){
		if(is_prime[i]){
			for(ll j=i*i;j<=N;j+=i){
				is_prime[j] = false;
			}
			P.emplace_back(i);
		}
	}
	return P;
}

vector<ll> prime_factor(ll n,vll& prime_list){
	ll sn = sqrt(n)+1;
	vector<ll> res;
	for(int i=0,ei=prime_list.size();i<ei;i++){
		ll v = prime_list[i];
		if(v*v>n) break;
		while(!(n%v)){
			n /= v;
			res.push_back(v);
		}
	}
	if(n>1){
		res.push_back(n);
	}

	return res;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);

	auto N=in();
	vll S=vecin(N);
	vll T(S);
	sort(ALL(T));

	auto PL = enum_prime(T.back());
	UnionFind uf(T.back()+1);
	for(auto& x:S){
		auto P = prime_factor(x,PL);
		sort(ALL(P));
		P.erase(unique(ALL(P)),P.end());
		for(auto& p:P) uf.unite(x,p);
	}
	
	REP(i,N){
		if(!uf.same(S[i],T[i])){
			out(0);
			return 0;
		}
	}
	out(1);
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 8108
final_rank: 6