結果

問題 No.826 連絡網
ユーザー face4face4
提出日時 2019-05-03 21:47:21
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,530 bytes
コンパイル時間 709 ms
コンパイル使用メモリ 73,780 KB
実行使用メモリ 12,276 KB
最終ジャッジ日時 2023-08-15 18:11:36
合計ジャッジ時間 2,275 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
4,600 KB
testcase_01 AC 7 ms
4,560 KB
testcase_02 AC 7 ms
4,600 KB
testcase_03 AC 7 ms
4,604 KB
testcase_04 AC 7 ms
4,648 KB
testcase_05 AC 7 ms
4,764 KB
testcase_06 AC 7 ms
4,596 KB
testcase_07 AC 7 ms
4,604 KB
testcase_08 AC 7 ms
4,764 KB
testcase_09 AC 7 ms
4,624 KB
testcase_10 AC 7 ms
4,788 KB
testcase_11 AC 7 ms
4,600 KB
testcase_12 AC 29 ms
9,956 KB
testcase_13 AC 16 ms
6,736 KB
testcase_14 AC 24 ms
8,644 KB
testcase_15 AC 8 ms
4,992 KB
testcase_16 AC 18 ms
7,264 KB
testcase_17 AC 16 ms
6,576 KB
testcase_18 AC 14 ms
6,004 KB
testcase_19 AC 34 ms
11,096 KB
testcase_20 AC 34 ms
10,688 KB
testcase_21 AC 7 ms
4,756 KB
testcase_22 AC 16 ms
6,624 KB
testcase_23 AC 18 ms
7,368 KB
testcase_24 AC 12 ms
5,844 KB
testcase_25 AC 40 ms
12,004 KB
testcase_26 AC 13 ms
6,072 KB
testcase_27 AC 30 ms
10,372 KB
testcase_28 AC 24 ms
8,968 KB
testcase_29 AC 15 ms
6,588 KB
testcase_30 AC 39 ms
12,276 KB
testcase_31 AC 17 ms
7,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;

class DisjointSet{
  public:
    vector<int> rank, p;

    DisjointSet(){}
    DisjointSet(int size){
        rank.resize(size, 0);
        p.resize(size, 0);
        for(int i = 0; i < size; i++) makeSet(i);
    }

    void makeSet(int x){
        p[x] = x;
        rank[x] = 0;
    }

    bool same(int x, int y){
        return findSet(x) == findSet(y);
    }
    
    void unite(int x, int y){
        link(findSet(x), findSet(y));
    }

    void link(int x, int y){
        if(rank[x] > rank[y]){
            p[y] = x;
        }else{
            p[x] = y;
            if(rank[x] == rank[y]){
                rank[y]++;
            }
        }
    }

    int findSet(int x){
        if(x != p[x]){
            // path compression
            p[x] = findSet(p[x]);
        }
        return p[x];
    }
};

int main(){
    int n, p;
    cin >> n >> p;
    vector<int> prime;
    bool nonp[1000001] = {};
    for(int i = 2; i < 1000001; i++){
        if(nonp[i]) continue;
        prime.push_back(i);
        for(int j = i+i; j < 1000001; j+= i)    nonp[j] = true;
    }
    int pnum = prime.size();
    DisjointSet uf(n);

    // ここが重いかもしれない
    for(int j = 0; j < pnum; j++){
        if(prime[j] > n)    break;
        for(int i = prime[j]+prime[j]; i <= n; i+=prime[j]){
            uf.unite(i-1, i-prime[j]-1);
        }
    }

    int ans = 0;
    for(int i = 0; i < n; i++)  if(uf.same(p-1,i))  ans++;
    cout << ans << endl;
    return 0;
}
0