結果
| 問題 | No.6 使いものにならないハッシュ | 
| コンテスト | |
| ユーザー |  ぴろず | 
| 提出日時 | 2014-12-21 00:51:03 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 176 ms / 5,000 ms | 
| コード長 | 1,453 bytes | 
| コンパイル時間 | 2,943 ms | 
| コンパイル使用メモリ | 79,308 KB | 
| 実行使用メモリ | 45,428 KB | 
| 最終ジャッジ日時 | 2024-09-16 16:23:12 | 
| 合計ジャッジ時間 | 8,383 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 32 | 
ソースコード
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;
	}
}
            
            
            
        