結果

問題 No.826 連絡網
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-05-07 12:00:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 384 ms / 2,000 ms
コード長 1,427 bytes
コンパイル時間 2,322 ms
コンパイル使用メモリ 77,956 KB
実行使用メモリ 68,452 KB
最終ジャッジ日時 2024-05-03 05:21:44
合計ジャッジ時間 10,265 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 118 ms
40,292 KB
testcase_01 AC 132 ms
41,100 KB
testcase_02 AC 136 ms
41,432 KB
testcase_03 AC 143 ms
41,296 KB
testcase_04 AC 144 ms
41,492 KB
testcase_05 AC 141 ms
41,184 KB
testcase_06 AC 139 ms
41,412 KB
testcase_07 AC 144 ms
41,392 KB
testcase_08 AC 142 ms
41,112 KB
testcase_09 AC 146 ms
41,572 KB
testcase_10 AC 141 ms
41,312 KB
testcase_11 AC 142 ms
41,232 KB
testcase_12 AC 309 ms
61,976 KB
testcase_13 AC 230 ms
48,880 KB
testcase_14 AC 278 ms
56,796 KB
testcase_15 AC 169 ms
43,144 KB
testcase_16 AC 246 ms
51,292 KB
testcase_17 AC 229 ms
48,796 KB
testcase_18 AC 207 ms
47,248 KB
testcase_19 AC 335 ms
64,208 KB
testcase_20 AC 337 ms
63,644 KB
testcase_21 AC 151 ms
41,344 KB
testcase_22 AC 229 ms
48,952 KB
testcase_23 AC 248 ms
52,056 KB
testcase_24 AC 201 ms
45,912 KB
testcase_25 AC 368 ms
68,032 KB
testcase_26 AC 200 ms
46,680 KB
testcase_27 AC 306 ms
61,404 KB
testcase_28 AC 273 ms
57,464 KB
testcase_29 AC 227 ms
48,720 KB
testcase_30 AC 384 ms
68,452 KB
testcase_31 AC 239 ms
50,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        int n = stdin.nextInt();
        int p = stdin.nextInt();
        Main main = new Main(n, p);
        System.out.println(main.solve());
    }
    
    private int n;
    private int p;
    
    public Main(int n, int p) {
        this.n = n;
        this.p = p;
    }
    
    public int solve() {
        UnionFind uf = new UnionFind(n);
        for (int x = 2; x <= n; x++) {
            for (int y = x + x; y <= n; y += x) {
                uf.union(x - 1, y - 1);
            }
        }
        return uf.size(p - 1);
    }
}

class UnionFind {
    
    private int[] parents;
    private int[] sizes;
    
    public UnionFind(int n) {
        parents = IntStream.range(0, n).toArray();
        sizes = new int[n];
        Arrays.fill(sizes, 1);
    }
    
    public void union(int x, int y) {
        x = find(x);
        y = find(y);
        
        if (x != y) {
            sizes[y] += sizes[x];
            parents[x] = y;
        }
    }
    
    public int find(int x) {
        if (parents[x] == x) {
            return x;
        } else {
            parents[x] = find(parents[x]);
            return parents[x];
        }
    }
    
    public int size(int x) {
        return sizes[find(x)];
    }
}
0