結果

問題 No.1059 素敵な集合
ユーザー tentententen
提出日時 2023-06-21 09:21:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 168 ms / 2,000 ms
コード長 2,642 bytes
コンパイル時間 3,455 ms
コンパイル使用メモリ 87,620 KB
実行使用メモリ 59,096 KB
最終ジャッジ日時 2023-09-11 04:52:17
合計ジャッジ時間 7,589 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,308 KB
testcase_01 AC 111 ms
52,132 KB
testcase_02 AC 89 ms
54,684 KB
testcase_03 AC 45 ms
49,444 KB
testcase_04 AC 45 ms
49,496 KB
testcase_05 AC 44 ms
49,500 KB
testcase_06 AC 90 ms
52,560 KB
testcase_07 AC 90 ms
52,380 KB
testcase_08 AC 92 ms
54,600 KB
testcase_09 AC 80 ms
51,696 KB
testcase_10 AC 97 ms
54,508 KB
testcase_11 AC 92 ms
52,336 KB
testcase_12 AC 85 ms
52,080 KB
testcase_13 AC 113 ms
54,648 KB
testcase_14 AC 52 ms
50,216 KB
testcase_15 AC 138 ms
55,096 KB
testcase_16 AC 94 ms
54,560 KB
testcase_17 AC 95 ms
54,680 KB
testcase_18 AC 111 ms
59,096 KB
testcase_19 AC 168 ms
55,344 KB
testcase_20 AC 167 ms
55,392 KB
testcase_21 AC 145 ms
55,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static final int MOD = 1000000007;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int left = sc.nextInt();
        int right = sc.nextInt();
        UnionFindTree uft = new UnionFindTree(right + 1);
        for (int i = left; i * 2 <= right; i++) {
            for (int j = 2; j * i <= right; j++) {
                uft.unite(i * j, i);
            }
        }
        HashSet<Integer> counts = new HashSet<>();
        for (int i = left; i <= right; i++) {
            counts.add(uft.find(i));
        }
        System.out.println(counts.size() - 1);
    }
    
    static class UnionFindTree {
        int[] parents;
        
        public UnionFindTree(int size) {
            parents = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
            }
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public void unite(int x, int y) {
            if (!same(x, y)) {
                parents[find(x)] = find(y);
            }
        }
    } 
}

class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0