結果

問題 No.826 連絡網
コンテスト
ユーザー ks2m
提出日時 2019-05-03 22:00:41
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 1,017 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,750 ms
コンパイル使用メモリ 83,808 KB
実行使用メモリ 51,568 KB
最終ジャッジ日時 2026-05-17 22:52:49
合計ジャッジ時間 6,332 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int p= sc.nextInt();
		sc.close();

		if (p == 1) {
			System.out.println(1);
			return;
		}

		List<Integer> sosuu = sosuuList(n);
		Set<Integer> set = new HashSet<Integer>();
		int n2 = n / 2;
		for (Integer o : sosuu) {
			if (o > n2) {
				set.add(o);
			}
		}

		if (set.contains(p)) {
			System.out.println(1);
		} else {
			System.out.println(n - set.size() - 1);
		}
	}

	static List<Integer> sosuuList(int n) {
		List<Integer> sosuu = new ArrayList<Integer>();
		for (int i = 2; i <= n; i++) {
			int r = (int) Math.sqrt(i);
			boolean flg = false;
			for (Integer o : sosuu) {
				if (r < o) {
					break;
				}
				if (i % o == 0) {
					flg = true;
					break;
				}
			}
			if (!flg) {
				sosuu.add(i);
			}
		}
		return sosuu;
	}
}
0