結果

問題 No.826 連絡網
ユーザー finefine
提出日時 2019-05-03 22:03:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 3,217 bytes
コンパイル時間 2,177 ms
コンパイル使用メモリ 179,204 KB
実行使用メモリ 7,476 KB
最終ジャッジ日時 2023-08-15 18:18:52
合計ジャッジ時間 3,236 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 15 ms
6,436 KB
testcase_13 AC 7 ms
4,516 KB
testcase_14 AC 12 ms
5,704 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 8 ms
4,636 KB
testcase_17 AC 6 ms
4,404 KB
testcase_18 AC 5 ms
4,376 KB
testcase_19 AC 17 ms
6,856 KB
testcase_20 AC 17 ms
6,812 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 7 ms
4,400 KB
testcase_23 AC 8 ms
4,724 KB
testcase_24 AC 4 ms
4,380 KB
testcase_25 AC 20 ms
7,476 KB
testcase_26 AC 5 ms
4,376 KB
testcase_27 AC 14 ms
6,392 KB
testcase_28 AC 11 ms
5,772 KB
testcase_29 AC 6 ms
4,468 KB
testcase_30 AC 20 ms
7,476 KB
testcase_31 AC 7 ms
4,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

bool is_prime(int n) {
    if (n % 2 == 0) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return n != 1;
}

vector<int> divisor(int n) {
    vector<int> res;
    for (int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            res.emplace_back(i);
            if (i != n / i) res.emplace_back(n / i);
        }
    }
    return res;
}

map<int, int> prime_factor(int n) {
    map<int, int> res;
    for (int i = 2; i * i <= n; i++) {
        while (n % i == 0) {
            ++res[i];
            n /= i;
        }
    }
    if (n != 1) res[n] = 1;
    return res;
}

vector<int> sieve(int n) {
    vector<int> res;
    vector<bool> is_prime(n + 1, true);
    is_prime[0] = false;
    is_prime[1] = false;
    for (int i = 2; i <= n; i++) {
        if (!is_prime[i]) continue;
        res.emplace_back(i);
        for (int j = 2 * i; j <= n; j += i) is_prime[j] = false;
    }
    return res;
}

struct UnionFind {
    //各要素が属する集合の代表(根)を管理する
    //もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しい
    vector<int> data;

    UnionFind(int sz) : data(sz, -1) {}

    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        bool is_union = (x != y);
        if (is_union) {
            if (data[x] > data[y]) swap(x, y);
            data[x] += data[y];
            data[y] = x;
        }
        return is_union;
    }

    int find(int x) {
        if (data[x] < 0) { //要素xが根である
            return x;
        } else {
            data[x] = find(data[x]); //data[x]がxの属する集合の根でない場合、根になるよう更新される
            return data[x];
        }
    }

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

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

int solve(int n, int p, vector<int>& ok) {
    UnionFind uf(n);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j < i; j++) {
            if (__gcd(i, j) != 1) uf.unite(i-1, j-1);
        }
    }

    for (int i = 1; i <= n; i++) {
        if (uf.same(i-1,p-1)) {
            ok[i] = true;
        }
    }
    return uf.size(p-1);
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, p;
    cin >> n >> p;
    if (p == 1) {
        cout << 1 << endl;
        return 0;
    }
    vector<int> s = sieve(n);
    map<int, int> pf = prime_factor(p);
    ll hoge = pf.begin()->first;
    if (hoge != 2 && hoge * 2 <= n) {
        hoge = 2;
    }
    
    vector<int> ok(n + 1, false);
    for (auto& p : pf) ok[p.first] = true;
    ok[hoge] = true;
    for (int x : s) {
        if (!ok[x] && x * hoge > n) continue;
        ok[x] = true;
        for (int i = 2 * x; i <= n; i += x) {
            ok[i] = true;
        }
    }

    //vector<int> ok2(n, false);
    //solve(n, p, ok2);

    int ans = 0;
    for (int i = 1; i <= n; i++) {
        ans += ok[i];
        /*
        if (ok[i] != ok2[i]) {
            cerr << i << endl;
        }*/
    }
    cout << ans << endl;

    return 0;
}
0