結果

問題 No.6 使いものにならないハッシュ
ユーザー ぴろずぴろず
提出日時 2014-12-21 00:51:03
言語 Java21
(openjdk 21)
結果
AC  
実行時間 191 ms / 5,000 ms
コード長 1,453 bytes
コンパイル時間 2,343 ms
コンパイル使用メモリ 81,680 KB
実行使用メモリ 60,440 KB
最終ジャッジ日時 2023-10-14 22:48:03
合計ジャッジ時間 9,155 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
55,692 KB
testcase_01 AC 132 ms
55,552 KB
testcase_02 AC 188 ms
58,420 KB
testcase_03 AC 144 ms
57,736 KB
testcase_04 AC 159 ms
57,976 KB
testcase_05 AC 159 ms
57,940 KB
testcase_06 AC 172 ms
57,700 KB
testcase_07 AC 176 ms
57,928 KB
testcase_08 AC 171 ms
57,520 KB
testcase_09 AC 175 ms
57,916 KB
testcase_10 AC 132 ms
55,492 KB
testcase_11 AC 144 ms
57,684 KB
testcase_12 AC 174 ms
57,704 KB
testcase_13 AC 176 ms
58,116 KB
testcase_14 AC 175 ms
58,136 KB
testcase_15 AC 180 ms
57,780 KB
testcase_16 AC 178 ms
58,248 KB
testcase_17 AC 177 ms
57,752 KB
testcase_18 AC 191 ms
60,440 KB
testcase_19 AC 176 ms
57,792 KB
testcase_20 AC 176 ms
57,944 KB
testcase_21 AC 143 ms
55,848 KB
testcase_22 AC 177 ms
57,620 KB
testcase_23 AC 176 ms
57,816 KB
testcase_24 AC 175 ms
57,772 KB
testcase_25 AC 176 ms
58,100 KB
testcase_26 AC 175 ms
57,720 KB
testcase_27 AC 175 ms
57,888 KB
testcase_28 AC 161 ms
57,728 KB
testcase_29 AC 187 ms
58,096 KB
testcase_30 AC 176 ms
57,740 KB
testcase_31 AC 177 ms
57,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no006;

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

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int k = sc.nextInt();
		int n = sc.nextInt();
		ArrayList<Integer> primes = Sieve.primeList(n);
		int ans = 0;
		int max = 0;
		int i = 0;
		while(i < primes.size() && primes.get(i) < k) {
			i++;
		}
		for(;i<=n;i++) {
			boolean[] used = new boolean[10];
			for(int j=i;j<primes.size();j++) {
				int h = uselessHash(primes.get(j));
				if (used[h]) {
					break;
				}
				used[h] = true;
				if (j-i+1 >= max) {
					max = j - i + 1;
					ans = primes.get(i);
				}
			}
		}
		System.out.println(ans);
	}

	static int uselessHash(int n) {
		while(n >= 10) {
			int sum = 0;
			while(n > 0) {
				sum += n % 10;
				n /= 10;
			}
			n = sum;
		}
		return n;
	}

}
class Sieve {
	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<>();
		for(int i=2;i<=max;i++) {
			if (isPrime[i]) {
				primeList.add(i);
			}
		}
		return primeList;
	}
}
0