結果

問題 No.1059 素敵な集合
ユーザー tentententen
提出日時 2021-12-07 08:59:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 2,216 bytes
コンパイル時間 1,909 ms
コンパイル使用メモリ 73,788 KB
実行使用メモリ 54,216 KB
最終ジャッジ日時 2023-09-21 16:38:23
合計ジャッジ時間 4,598 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,292 KB
testcase_01 AC 86 ms
51,164 KB
testcase_02 AC 74 ms
51,748 KB
testcase_03 AC 42 ms
49,204 KB
testcase_04 AC 41 ms
49,136 KB
testcase_05 AC 43 ms
49,404 KB
testcase_06 AC 61 ms
51,792 KB
testcase_07 AC 59 ms
51,808 KB
testcase_08 AC 72 ms
51,520 KB
testcase_09 AC 67 ms
50,604 KB
testcase_10 AC 76 ms
54,216 KB
testcase_11 AC 61 ms
51,520 KB
testcase_12 AC 61 ms
51,452 KB
testcase_13 AC 82 ms
51,708 KB
testcase_14 AC 44 ms
49,292 KB
testcase_15 AC 96 ms
52,352 KB
testcase_16 AC 63 ms
51,928 KB
testcase_17 AC 62 ms
52,128 KB
testcase_18 AC 68 ms
51,428 KB
testcase_19 AC 90 ms
52,596 KB
testcase_20 AC 97 ms
50,540 KB
testcase_21 AC 96 ms
52,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    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);
        boolean[] used = new boolean[right + 1];
        for (int i = left; i <= right; i++) {
            if (used[i]) {
                continue;
            }
            for (int j = 2; j * i <= right; j++) {
                uft.unite(i, j * i);
                used[j * i] = true;
            }
        }
        int ans = 0;
        for (int i = left; i < right; i++) {
            if (!uft.same(i, i + 1)) {
                ans++;
                uft.unite(i, i + 1);
            }
        }
        System.out.println(ans);
    }
    
    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(y)] = find(x);
            }
        }
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0