結果

問題 No.371 ぼく悪いプライムじゃないよ
ユーザー ぴろずぴろず
提出日時 2016-07-18 23:33:49
言語 Java21
(openjdk 21)
結果
AC  
実行時間 139 ms / 1,000 ms
コード長 1,678 bytes
コンパイル時間 2,448 ms
コンパイル使用メモリ 75,756 KB
実行使用メモリ 56,048 KB
最終ジャッジ日時 2023-08-29 01:09:43
合計ジャッジ時間 10,302 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,432 KB
testcase_01 AC 135 ms
55,672 KB
testcase_02 AC 127 ms
55,948 KB
testcase_03 AC 128 ms
55,708 KB
testcase_04 AC 136 ms
55,684 KB
testcase_05 AC 134 ms
55,716 KB
testcase_06 AC 136 ms
55,692 KB
testcase_07 AC 128 ms
55,484 KB
testcase_08 AC 134 ms
55,980 KB
testcase_09 AC 135 ms
55,676 KB
testcase_10 AC 139 ms
55,816 KB
testcase_11 AC 135 ms
55,824 KB
testcase_12 AC 136 ms
55,832 KB
testcase_13 AC 132 ms
55,812 KB
testcase_14 AC 136 ms
55,760 KB
testcase_15 AC 133 ms
55,640 KB
testcase_16 AC 137 ms
55,748 KB
testcase_17 AC 131 ms
55,680 KB
testcase_18 AC 132 ms
55,680 KB
testcase_19 AC 131 ms
55,736 KB
testcase_20 AC 128 ms
55,696 KB
testcase_21 AC 129 ms
53,580 KB
testcase_22 AC 126 ms
55,668 KB
testcase_23 AC 126 ms
55,520 KB
testcase_24 AC 137 ms
55,764 KB
testcase_25 AC 134 ms
55,560 KB
testcase_26 AC 130 ms
55,824 KB
testcase_27 AC 135 ms
55,768 KB
testcase_28 AC 135 ms
55,644 KB
testcase_29 AC 134 ms
55,796 KB
testcase_30 AC 134 ms
55,940 KB
testcase_31 AC 130 ms
55,644 KB
testcase_32 AC 134 ms
55,620 KB
testcase_33 AC 132 ms
55,760 KB
testcase_34 AC 133 ms
55,800 KB
testcase_35 AC 134 ms
55,548 KB
testcase_36 AC 134 ms
55,972 KB
testcase_37 AC 133 ms
55,720 KB
testcase_38 AC 132 ms
55,496 KB
testcase_39 AC 138 ms
55,804 KB
testcase_40 AC 135 ms
56,048 KB
testcase_41 AC 136 ms
55,768 KB
testcase_42 AC 139 ms
55,712 KB
testcase_43 AC 137 ms
55,972 KB
testcase_44 AC 133 ms
55,952 KB
testcase_45 AC 134 ms
55,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no371;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static ArrayList<Integer> primes = primeList(100000);
	
	//参考 http://kmjp.hatenablog.jp/entry/2016/05/14/0900
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long l = sc.nextLong();
		long h = sc.nextLong();
		System.out.println(solve(l,h));
	}
	
	public static long solve(long l,long h) {
		for(int i=primes.size()-1;i>=0;i--) {
			long p = primes.get(i);
			if (p * p > h) continue;
			
			for(long x = h / p * p; x >= l; x -= p) {
				boolean flag = true;
				for(long q:primes) {
					if (q >= p) break;
					if (x % q == 0) {
						flag = false;
						break;
					}
				}
				if (flag) {
					return x;
				}
			}
		}
		
		return -1; //???
	}

	public static boolean[] isPrimeArray(int max) {
		boolean[] isPrime = new boolean[max+1];
		Arrays.fill(isPrime, true);
		isPrime[0] = isPrime[1] = false;
		for(int i=2;i*i<=max;i++) {
			if (isPrime[i]) {
				int j = i * 2;
				while(j<=max) {
					isPrime[j] = false;
					j += i;
				}
			}
		}
		return isPrime;
	}
	public static ArrayList<Integer> primeList(int max) {
		boolean[] isPrime = isPrimeArray(max);
		ArrayList<Integer> primeList = new ArrayList<Integer>();
		for(int i=2;i<=max;i++) {
			if (isPrime[i]) {
				primeList.add(i);
			}
		}
		return primeList;
	}
	
	public static ArrayList<Long> primeFactorL(ArrayList<Integer> primeList,long num) {
		ArrayList<Long> ret = new ArrayList<Long>();
		for(int p:primeList) {
			while(num % p == 0) {
				num /= p;
				ret.add((long) p);
			}
		}
		if (num > 1) {
			ret.add(num);
		}
		return ret;
	}
}
0