結果

問題 No.826 連絡網
ユーザー rlangevinrlangevin
提出日時 2023-02-08 08:59:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 346 ms / 2,000 ms
コード長 1,438 bytes
コンパイル時間 361 ms
コンパイル使用メモリ 87,104 KB
実行使用メモリ 159,700 KB
最終ジャッジ日時 2023-09-20 02:34:55
合計ジャッジ時間 7,668 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,580 KB
testcase_01 AC 95 ms
71,628 KB
testcase_02 AC 102 ms
76,848 KB
testcase_03 AC 133 ms
78,416 KB
testcase_04 AC 131 ms
78,784 KB
testcase_05 AC 128 ms
78,776 KB
testcase_06 AC 132 ms
78,976 KB
testcase_07 AC 134 ms
78,716 KB
testcase_08 AC 133 ms
78,852 KB
testcase_09 AC 135 ms
78,672 KB
testcase_10 AC 115 ms
77,912 KB
testcase_11 AC 130 ms
78,892 KB
testcase_12 AC 276 ms
137,876 KB
testcase_13 AC 196 ms
101,672 KB
testcase_14 AC 238 ms
122,408 KB
testcase_15 AC 147 ms
84,404 KB
testcase_16 AC 205 ms
106,452 KB
testcase_17 AC 190 ms
101,936 KB
testcase_18 AC 176 ms
96,272 KB
testcase_19 AC 312 ms
147,964 KB
testcase_20 AC 315 ms
146,752 KB
testcase_21 AC 134 ms
78,800 KB
testcase_22 AC 196 ms
102,132 KB
testcase_23 AC 207 ms
108,312 KB
testcase_24 AC 164 ms
91,068 KB
testcase_25 AC 330 ms
159,460 KB
testcase_26 AC 173 ms
94,400 KB
testcase_27 AC 280 ms
139,152 KB
testcase_28 AC 248 ms
125,472 KB
testcase_29 AC 193 ms
101,220 KB
testcase_30 AC 346 ms
159,700 KB
testcase_31 AC 206 ms
106,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]


from math import ceil, sqrt
def Sieve(n):
    lst = [True] * (n + 1)
    S = set()
    for i in range(2, ceil(sqrt(n)) + 1):
        if lst[i]:
            for j in range(2 * i, n + 1, i):
                lst[j] = False
    for i in range(2, n + 1):
        if lst[i]:
            S.add(i)
    return S


N, P = map(int, input().split())
G = [[] for i in range(N + 1)]
U = UnionFind(N + 1)
for i in Sieve(N):
    for j in range(i, N + 1, i):
        if j + i <= N:
            U.union(i, i + j)

ans = 0
for i in range(N + 1):
    if U.is_same(U.find(P), i):
        ans += 1
print(ans)
0