結果

問題 No.6 使いものにならないハッシュ
ユーザー takeya_okinotakeya_okino
提出日時 2019-08-11 21:47:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 456 ms / 5,000 ms
コード長 1,644 bytes
コンパイル時間 4,696 ms
コンパイル使用メモリ 75,704 KB
実行使用メモリ 56,404 KB
最終ジャッジ日時 2023-10-14 23:18:05
合計ジャッジ時間 13,892 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
55,452 KB
testcase_01 AC 128 ms
55,808 KB
testcase_02 AC 456 ms
55,748 KB
testcase_03 AC 164 ms
56,100 KB
testcase_04 AC 190 ms
55,880 KB
testcase_05 AC 193 ms
55,752 KB
testcase_06 AC 298 ms
55,728 KB
testcase_07 AC 227 ms
55,972 KB
testcase_08 AC 293 ms
55,832 KB
testcase_09 AC 197 ms
55,628 KB
testcase_10 AC 127 ms
55,712 KB
testcase_11 AC 166 ms
55,400 KB
testcase_12 AC 321 ms
55,656 KB
testcase_13 AC 171 ms
55,716 KB
testcase_14 AC 197 ms
55,708 KB
testcase_15 AC 281 ms
55,672 KB
testcase_16 AC 249 ms
55,628 KB
testcase_17 AC 376 ms
55,828 KB
testcase_18 AC 453 ms
56,404 KB
testcase_19 AC 378 ms
55,780 KB
testcase_20 AC 338 ms
55,832 KB
testcase_21 AC 153 ms
55,536 KB
testcase_22 AC 350 ms
55,648 KB
testcase_23 AC 322 ms
55,648 KB
testcase_24 AC 334 ms
56,052 KB
testcase_25 AC 249 ms
55,860 KB
testcase_26 AC 397 ms
55,784 KB
testcase_27 AC 344 ms
55,380 KB
testcase_28 AC 228 ms
55,896 KB
testcase_29 AC 403 ms
55,732 KB
testcase_30 AC 359 ms
55,668 KB
testcase_31 AC 338 ms
55,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

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> prime = new ArrayList<Integer>();
    ArrayList<Integer> F = new ArrayList<Integer>();
    for(int i = k; i <= n; i++) {
      if(isP(i)) prime.add(i);
    }
    Collections.sort(prime);
    for(int i = 0; i < prime.size(); i++) {
      F.add(f(prime.get(i)));
    }
    int right = 0;
    int ans = 0;
    int len = 0;
    int[] kosuu = new int[10];
    kosuu[F.get(0)]++;
    while(right < prime.size() - 1) {
      if(kosuu[F.get(right + 1)] > 0) {
        break;
      } else {
        kosuu[F.get(right + 1)]++;
        right++;
      }
    }
    int c = right + 1;
    if(len <= c) {
      ans = prime.get(0);
      len = c;
    }
    for(int i = 1; i < prime.size(); i++) {
      kosuu[F.get(i - 1)]--;
      while(right < prime.size() - 1) {
        if(kosuu[F.get(right + 1)] > 0) {
          break;
        } else {
          kosuu[F.get(right + 1)]++;
          right++;
        }
      }
      int t = right - i + 1;
      if(len <= t) {
        ans = prime.get(i);
        len = t;
      }
    }
    System.out.println(ans);
  }

  public static boolean isP(int n) {
    int p = 0;
    for(int i = 2; (i * i) <= n; i++) {
      if((n % i) == 0) p++;
    }
    if(n == 1) return false;
    if(p == 0) return true;
    return false;
  }

  public static int f(int n) {
    if(n < 10) return n;
    int q = 0;
    for(int i = 0; i < 10; i++) {
      int t = n / (int)Math.pow(10, i);
      q += (t % 10);
    }
    return f(q);
  } 
}
0