結果
| 問題 |
No.371 ぼく悪いプライムじゃないよ
|
| コンテスト | |
| ユーザー |
ぴろず
|
| 提出日時 | 2016-07-18 23:33:49 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 165 ms / 1,000 ms |
| コード長 | 1,678 bytes |
| コンパイル時間 | 2,656 ms |
| コンパイル使用メモリ | 78,724 KB |
| 実行使用メモリ | 41,840 KB |
| 最終ジャッジ日時 | 2024-12-30 18:25:28 |
| 合計ジャッジ時間 | 11,356 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 42 |
ソースコード
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;
}
}
ぴろず