結果

問題 No.6 使いものにならないハッシュ
ユーザー fujisufujisu
提出日時 2015-02-02 23:24:54
言語 Java21
(openjdk 21)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,292 bytes
コンパイル時間 2,566 ms
コンパイル使用メモリ 75,016 KB
実行使用メモリ 53,964 KB
最終ジャッジ日時 2023-10-14 22:48:47
合計ジャッジ時間 7,149 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
50,728 KB
testcase_01 AC 56 ms
50,516 KB
testcase_02 AC 110 ms
53,196 KB
testcase_03 AC 92 ms
53,216 KB
testcase_04 AC 84 ms
52,976 KB
testcase_05 AC 102 ms
52,840 KB
testcase_06 AC 110 ms
52,852 KB
testcase_07 AC 86 ms
52,688 KB
testcase_08 AC 95 ms
52,980 KB
testcase_09 AC 83 ms
52,472 KB
testcase_10 RE -
testcase_11 AC 94 ms
53,780 KB
testcase_12 AC 100 ms
52,892 KB
testcase_13 AC 76 ms
50,656 KB
testcase_14 AC 83 ms
51,064 KB
testcase_15 AC 114 ms
53,332 KB
testcase_16 AC 89 ms
53,376 KB
testcase_17 AC 110 ms
52,972 KB
testcase_18 AC 115 ms
52,936 KB
testcase_19 AC 112 ms
53,504 KB
testcase_20 AC 107 ms
52,384 KB
testcase_21 AC 80 ms
51,256 KB
testcase_22 AC 111 ms
52,552 KB
testcase_23 AC 112 ms
52,984 KB
testcase_24 AC 112 ms
53,964 KB
testcase_25 AC 104 ms
53,088 KB
testcase_26 AC 113 ms
53,600 KB
testcase_27 AC 105 ms
52,916 KB
testcase_28 AC 91 ms
53,120 KB
testcase_29 AC 114 ms
52,888 KB
testcase_30 AC 111 ms
53,052 KB
testcase_31 AC 110 ms
52,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.TreeSet;

public class Main {
	int hash(int k) {
		if (k < 10)
			return k;
		int sum = 0;
		while (0 < k) {
			sum += k % 10;
			k /= 10;
		}
		return hash(sum);
	}

	void run() {
		MyScanner sc = new MyScanner();
		int max = 200001;
		boolean[] prime = new boolean[max];
		Arrays.fill(prime, true);
		prime[0] = prime[1] = false;
		for (int i = 0; i * i <= max; i++) {
			if (prime[i]) {
				for (int j = i * i; j < max; j += i) {
					prime[j] = false;
				}
			}
		}

		int n = sc.nextInt();
		int m = sc.nextInt();

		int count = 0;
		for (int i = n; i <= m; i++) {
			if (prime[i])
				count++;
		}

		int[] list = new int[count];
		int[] hash = new int[count];
		int id = 0;
		for (int i = n; i <= m; i++) {
			if (prime[i]) {
				list[id] = i;
				hash[id] = hash(i);
				id++;
			}
		}

		int left = 0, right = 1;
		TreeSet<Integer> set = new TreeSet<Integer>();
		int maxLength = 0;
		int maxNumber = 0;
		set.add(hash[left]);
		while (left < id) {
			if (id <= right || set.contains(hash[right])) {
				set.remove(hash[left++]);
			} else {
				set.add(hash[right++]);
			}
			if (maxLength <= right - left) {
				maxLength = right - left;
				maxNumber = list[left];
			}
		}

		System.out.println(maxNumber);
	}

	public static void main(String[] args) {
		new Main().run();
	}

	public void mapDebug(int[][] a) {
		System.out.println("--------map display---------");
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < a[i].length; j++) {
				System.out.printf("%3d ", a[i][j]);
			}
			System.out.println();
		}
		System.out.println("----------------------------" + '\n');
	}

	class MyScanner {
		int read() {
			try {
				return System.in.read();
			} catch (IOException e) {
				throw new InputMismatchException();
			}
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		int[] nextIntArray(int n) {
			int[] array = new int[n];
			for (int i = 0; i < n; i++)
				array[i] = nextInt();
			return array;
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		long[] nextLongArray(int n) {
			long[] array = new long[n];
			for (int i = 0; i < n; i++)
				array[i] = nextLong();
			return array;
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		double[] nextDoubleArray(int n) {
			double[] array = new double[n];
			for (int i = 0; i < n; i++)
				array[i] = nextDouble();
			return array;
		}

		String next() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		String[] nextStringArray(int n) {
			String[] array = new String[n];
			for (int i = 0; i < n; i++)
				array[i] = next();

			return array;
		}

		String nextLine() {
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}
	}
}
0