結果

問題 No.826 連絡網
ユーザー 👑 jupirojupiro
提出日時 2020-01-23 23:58:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 273 ms / 2,000 ms
コード長 2,491 bytes
コンパイル時間 1,174 ms
コンパイル使用メモリ 119,708 KB
実行使用メモリ 55,940 KB
最終ジャッジ日時 2023-09-29 13:25:55
合計ジャッジ時間 8,237 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
53,468 KB
testcase_01 AC 122 ms
53,820 KB
testcase_02 AC 123 ms
52,008 KB
testcase_03 AC 131 ms
53,336 KB
testcase_04 AC 129 ms
52,504 KB
testcase_05 AC 130 ms
53,204 KB
testcase_06 AC 127 ms
53,068 KB
testcase_07 AC 122 ms
53,028 KB
testcase_08 AC 121 ms
52,500 KB
testcase_09 AC 124 ms
52,760 KB
testcase_10 AC 123 ms
53,268 KB
testcase_11 AC 126 ms
52,296 KB
testcase_12 AC 234 ms
53,756 KB
testcase_13 AC 163 ms
52,740 KB
testcase_14 AC 197 ms
52,164 KB
testcase_15 AC 129 ms
52,228 KB
testcase_16 AC 171 ms
53,528 KB
testcase_17 AC 167 ms
53,532 KB
testcase_18 AC 153 ms
52,168 KB
testcase_19 AC 249 ms
54,660 KB
testcase_20 AC 253 ms
54,220 KB
testcase_21 AC 130 ms
53,084 KB
testcase_22 AC 165 ms
52,296 KB
testcase_23 AC 177 ms
52,208 KB
testcase_24 AC 144 ms
52,476 KB
testcase_25 AC 273 ms
55,940 KB
testcase_26 AC 152 ms
52,472 KB
testcase_27 AC 239 ms
53,740 KB
testcase_28 AC 207 ms
52,820 KB
testcase_29 AC 168 ms
53,284 KB
testcase_30 AC 273 ms
55,804 KB
testcase_31 AC 174 ms
53,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

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; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

bool IsPrime[MAX];
ll MinFactor[MAX]; vector<ll> preprocess(ll n = MAX) {
    vector<ll> res;
    for (ll i = 0; i < n; ++i) IsPrime[i] = true, MinFactor[i] = -1;
    IsPrime[0] = false; IsPrime[1] = false;
    MinFactor[0] = 0; MinFactor[1] = 1;
    for (ll i = 2; i < n; ++i) {
        if (IsPrime[i]) {
            MinFactor[i] = i;
            res.push_back(i);
            for (ll j = i * 2; j < n; j += i) {
                IsPrime[j] = false;
                if (MinFactor[j] == -1) MinFactor[j] = i;
            }
        }
    }
    return res;
}

vector<pair<ll, ll> > fast_prime_factor(ll n) {
    vector<pair<ll, ll> > res;
    while (n != 1) {
        ll prime = MinFactor[n];
        ll exp = 0;
        while (MinFactor[n] == prime) {
            ++exp;
            n /= prime;
        }
        res.push_back(make_pair(prime, exp));
    }
    return res;
}

class UnionFind {
public:
	vector<ll> Parent;
	UnionFind(ll N) {
		Parent = vector<ll>(N, -1);
	}
	ll root(ll A) {
		if (Parent[A] < 0) {
			return A;
		}
		return Parent[A] = root(Parent[A]);
	}

	ll size(ll A) {
		return -Parent[root(A)];
	}

	bool connect(ll A, ll B) {
		A = root(A);
		B = root(B);
		if (A == B) {
			return false;
		}
		if (size(A) < size(B)) {
			swap(A, B);
		}

		Parent[A] += Parent[B];
		Parent[B] = A;

		return true;
	}

};

int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

    preprocess();

	ll N, P; cin >> N >> P;
	UnionFind uni(N + 1);

	for (ll i = 2; i <= N; i++) {
		vector<pair<ll, ll>> vp = fast_prime_factor(i);
		for (auto p : vp) {
			uni.connect(p.first, i);
		}
	}
	cout << uni.size(P) << endl;
	return 0;
}
0