結果

問題 No.2756 GCD Teleporter
ユーザー NAMIDAIRONAMIDAIRO
提出日時 2024-05-11 14:13:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 210 ms / 2,000 ms
コード長 6,902 bytes
コンパイル時間 1,711 ms
コンパイル使用メモリ 125,940 KB
実行使用メモリ 13,512 KB
最終ジャッジ日時 2024-12-20 08:34:45
合計ジャッジ時間 7,368 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,844 KB
testcase_01 AC 5 ms
8,048 KB
testcase_02 AC 4 ms
7,896 KB
testcase_03 AC 5 ms
7,844 KB
testcase_04 AC 15 ms
8,204 KB
testcase_05 AC 57 ms
9,248 KB
testcase_06 AC 156 ms
11,248 KB
testcase_07 AC 126 ms
10,664 KB
testcase_08 AC 119 ms
10,420 KB
testcase_09 AC 103 ms
10,144 KB
testcase_10 AC 146 ms
10,956 KB
testcase_11 AC 182 ms
11,732 KB
testcase_12 AC 39 ms
8,772 KB
testcase_13 AC 187 ms
11,596 KB
testcase_14 AC 174 ms
11,348 KB
testcase_15 AC 41 ms
8,844 KB
testcase_16 AC 97 ms
9,972 KB
testcase_17 AC 115 ms
10,192 KB
testcase_18 AC 38 ms
8,736 KB
testcase_19 AC 191 ms
11,732 KB
testcase_20 AC 178 ms
11,108 KB
testcase_21 AC 83 ms
9,400 KB
testcase_22 AC 180 ms
11,128 KB
testcase_23 AC 25 ms
8,404 KB
testcase_24 AC 146 ms
10,320 KB
testcase_25 AC 150 ms
10,612 KB
testcase_26 AC 172 ms
11,084 KB
testcase_27 AC 182 ms
11,396 KB
testcase_28 AC 95 ms
9,812 KB
testcase_29 AC 142 ms
10,580 KB
testcase_30 AC 183 ms
11,336 KB
testcase_31 AC 79 ms
9,296 KB
testcase_32 AC 177 ms
10,280 KB
testcase_33 AC 70 ms
8,780 KB
testcase_34 AC 28 ms
8,296 KB
testcase_35 AC 169 ms
10,660 KB
testcase_36 AC 164 ms
10,732 KB
testcase_37 AC 5 ms
7,908 KB
testcase_38 AC 209 ms
11,196 KB
testcase_39 AC 210 ms
13,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <random>
#include <iomanip>
#include <string>
#include <cmath>
#include <complex>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)

template<class T>
using vi = vector<T>;

template<class T>
using vii = vector<vi<T>>;

template<class T>
using viii = vector<vii<T>>;

template<class T>
using viiii = vector<viii<T>>;

using P = pair<ll, int>;

void chmin(ll & x, ll y) { x = min(x, y); }

void chmax(ll& x, ll y) { x = max(x, y); }

struct mint {
    const long long mod = 998244353;
    long long x;
    mint(long long x_ = 0) : x((x_% mod + mod) % mod) {}

    mint& operator+=(const mint& other) {
        x += other.x;
        if (x >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint& other) {
        x -= other.x;
        if (x < 0) x += mod;
        return *this;
    }
    mint& operator*=(const mint& other) {
        x *= other.x;
        x %= mod;
        return *this;
    }

    mint& operator+=(const long long n) {
        return *this += mint(n);
    }
    mint& operator-=(const long long n) {
        return *this -= mint(n);
    }
    mint& operator*=(const long long n) {
        return *this *= mint(n);
    }

    mint& operator=(const mint& other) {
        x = other.x;
        return *this;
    }
    mint& operator=(const long long n) {
        x = n % mod;
        return *this;
    }

    bool operator==(const mint& other) const {
        return x == other.x;
    }
    bool operator!=(const mint& other) const {
        return x != other.x;
    }

    mint operator-() const {
        mint res(mod - x);
        return res;
    }

    mint operator+(const mint& other) const {
        mint res(x);
        return res += other;
    }
    mint operator-(const mint& other) const {
        mint res(x);
        return res -= other;
    }
    mint operator*(const mint& other) const {
        mint res(x);
        return res *= other;
    }

    mint operator+(const long long n) const {
        mint res(x);
        mint other(n);
        return res += other;
    }
    mint operator-(const long long n) const {
        mint res(x);
        mint other(n);
        return res -= other;
    }
    mint operator*(const long long n) const {
        mint res(x);
        mint other(n);
        return res *= other;
    }

    mint pow(long long n) const {
        if (n == 0) return mint(1);
        mint res = pow(n / 2);
        res *= res;
        if (n % 2) res *= *this;
        return res;
    }
    mint inv() const {
        return pow(mod - 2);
    }
    mint& operator/=(const mint& other) {
        *this *= other.inv();
        return *this;
    }
    mint operator/(const mint& other) const {
        mint res(x);
        return res /= other;
    }
};


struct combination {
    vector<mint> fact, ifact;
    combination(int m) :fact(m + 1), ifact(m + 1) {
        fact[0] = 1;
        for (int i = 1; i <= m; i++) fact[i] = fact[i - 1] * mint(i);
        ifact[m] = fact[m].inv();
        for (int i = m; i >= 1; i--) ifact[i - 1] = ifact[i] * mint(i);
    }
    mint operator()(int n, int k) {//for n<=m, calc nck
        if (k < 0 || k > n) return mint(0);
        return fact[n] * ifact[k] * ifact[n - k];
    }
};


template<class T>
struct NTT {
    const int divlim = 23; //when mod is 998244353
    vector<mint> root, invroot;
    const mint primitive = 3;

    NTT() : root(divlim + 1), invroot(divlim + 1) {
        root[divlim] = primitive.pow((primitive.mod - 1) >> divlim);
        invroot[divlim] = root[divlim].inv();
        for (int i = divlim - 1; i >= 0; i--) {
            root[i] = root[i + 1] * root[i + 1];
            invroot[i] = invroot[i + 1] * invroot[i + 1];
        }
    }

    void dft(vector<mint>& d, const int log, const bool inv = false) {
        int n = (int)d.size();
        if (n == 1 || log == 0) return;

        vector<mint> d0, d1;
        for (int i = 0; i < n / 2; i++) {
            d0.push_back(d[2 * i]);
            d1.push_back(d[2 * i + 1]);
        }

        dft(d0, log - 1, inv);
        dft(d1, log - 1, inv);

        mint pow = 1, z = (inv ? invroot[log] : root[log]);
        for (int i = 0; i < n / 2; i++) {
            d[i] = d0[i] + d1[i] * pow;
            pow *= z;
        }
        for (int i = n / 2; i < n; i++) {
            d[i] = d0[i - n / 2] + d1[i - n / 2] * pow;
            pow *= z;
        }
        return;
    }

    void idft(vector<mint>& d, const int log) {
        dft(d, log, true);
        return;
    }

    vector<mint> convolution(vector<T>& f, vector<T>& g) {
        int n = 1, log = 0, lenf = (int)f.size(), leng = (int)g.size();
        while (n < lenf + leng) {
            n <<= 1;
            log++;
        }

        vector<mint> df(n), dg(n);
        for (int i = 0; i < lenf; i++) df[i] = f[i];
        for (int i = 0; i < leng; i++) dg[i] = g[i];

        dft(df, log);
        dft(dg, log);
        for (int i = 0; i < n; i++) df[i] *= dg[i];
        idft(df, log);

        mint ninv = mint(n).inv();
        for (int i = 0; i < n; i++) df[i] *= ninv;
        return df;
    }
};

int gcd(int& x, int y) { return y ? gcd(y, x % y) : x; }


struct unionfind {
    vector<int> d;
    unionfind(int n = 0) {
        d = vector<int>(n, -1);
    }

    int find(int x) {
        if (d[x] < 0) return x;
        return d[x] = find(d[x]);
    }

    bool unite(int x, int y) {
        int rx = find(x);
        int ry = find(y);
        if (rx == ry) return false;
        if (d[rx] > d[ry]) swap(rx, ry);
        d[rx] += d[ry];
        d[ry] = rx;
        return true;
    }

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

    int size(int x) { return -d[find(x)]; }
};


int main()
{
    int n;
    cin >> n;
    vi<int> a(n);
    rep(i, n) cin >> a[i];

    int mx = 2e5 + 10;
    int mxx = 1000;
    vi<int> check(mxx);
    vi<int> prime;
    for (int i = 2; i < mxx; i++) {
        if (check[i]) continue;
        prime.push_back(i);
        for (ll j = (ll)i * i; j < mxx; j += i) check[j] = true;
    }

    vii<int> mp(mx);
    rep(i, n) {
        for (int p : prime) {
            if (a[i] % p) continue;
            mp[p].push_back(i);
            while (a[i] % p == 0) a[i] /= p;
        }
        if (a[i] > 1) mp[a[i]].push_back(i);
    }

    unionfind uf(n);
    ll mnp = mx;
    rep(i, mx) {
        int sz = (int)mp[i].size();
        if (sz == 0) continue;
        mnp = min<ll>(mnp, i);
        for (int j = 1; j < sz; j++) uf.unite(mp[i][0], mp[i][j]);
    }

    int cnt = 0;
    vi<bool> visited(mx);
    rep(i, n) {
        int rx = uf.find(i);
        if (visited[rx]) continue;
        visited[rx] = true;
        cnt++;
    }

    ll ans = mnp * (cnt - 1);
    if (mnp != 2) ans = min<ll>(ans, cnt * 2);
    cout << ans << endl;
    return 0;
}

0