結果

問題 No.826 連絡網
ユーザー ks2m
提出日時 2019-05-03 22:00:41
言語 Java
(openjdk 23)
結果
AC  
実行時間 333 ms / 2,000 ms
コード長 1,017 bytes
コンパイル時間 2,320 ms
コンパイル使用メモリ 77,820 KB
実行使用メモリ 48,064 KB
最終ジャッジ日時 2024-11-24 02:22:18
合計ジャッジ時間 9,684 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

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