結果

問題 No.6 使いものにならないハッシュ
ユーザー ゆうきゆうき
提出日時 2022-10-18 22:14:46
言語 Java21
(openjdk 21)
結果
AC  
実行時間 217 ms / 5,000 ms
コード長 5,716 bytes
コンパイル時間 2,810 ms
コンパイル使用メモリ 92,836 KB
実行使用メモリ 57,508 KB
最終ジャッジ日時 2024-06-29 04:31:27
合計ジャッジ時間 9,902 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
52,676 KB
testcase_01 AC 91 ms
52,372 KB
testcase_02 AC 217 ms
57,208 KB
testcase_03 AC 153 ms
56,548 KB
testcase_04 AC 172 ms
56,784 KB
testcase_05 AC 171 ms
56,488 KB
testcase_06 AC 190 ms
57,256 KB
testcase_07 AC 181 ms
56,976 KB
testcase_08 AC 188 ms
57,164 KB
testcase_09 AC 171 ms
57,508 KB
testcase_10 AC 96 ms
52,696 KB
testcase_11 AC 156 ms
56,492 KB
testcase_12 AC 187 ms
57,136 KB
testcase_13 AC 156 ms
57,092 KB
testcase_14 AC 164 ms
56,960 KB
testcase_15 AC 183 ms
57,236 KB
testcase_16 AC 184 ms
57,172 KB
testcase_17 AC 203 ms
57,328 KB
testcase_18 AC 201 ms
57,296 KB
testcase_19 AC 200 ms
57,264 KB
testcase_20 AC 191 ms
57,240 KB
testcase_21 AC 139 ms
54,384 KB
testcase_22 AC 199 ms
57,108 KB
testcase_23 AC 184 ms
57,136 KB
testcase_24 AC 190 ms
57,204 KB
testcase_25 AC 185 ms
56,968 KB
testcase_26 AC 206 ms
57,216 KB
testcase_27 AC 188 ms
57,308 KB
testcase_28 AC 175 ms
57,184 KB
testcase_29 AC 210 ms
57,176 KB
testcase_30 AC 201 ms
57,220 KB
testcase_31 AC 184 ms
57,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.IntFunction;

class Main{
  boolean isDebug = ManagementFactory.getRuntimeMXBean().getInputArguments().toString()
      .contains("-agentlib:jdwp");
  final MyReader in = new MyReader(System.in);
  final MyWriter out = new MyWriter(System.out);

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

  private void exe(){
    input();
    preCalc();
    solve();
    out.flush();
  }

  int K = in.it();
  int N = in.it();

  private void input(){}

  private void preCalc(){}

  void solve(){

    List<int[]> list = new ArrayList<>();

    for (var p:primes(N))
      if (K <= p)
        list.add(new int[] { p, hash(p) });

    int ans = 0;
    int max = 0;
    Set<Integer> set = new HashSet<>();
    set.add(list.get(0)[1]);
    for (int i = 0,j = 0;i < list.size();i++) {
      var s = list.get(i);

      while (j +1 < list.size() && !set.contains(list.get(j +1)[1])) {
        set.add(list.get(j +1)[1]);
        j++;
      }
      if (max <= j -i) {
        ans = s[0];
        max = j -i;
      }

      set.remove(s[1]);
    }

    out.println(ans);
  }

  private int hash(int p){
    int ret = 0;

    while (0 < p) {
      ret += p %10;
      p /= 10;
    }

    return 9 < ret ? hash(ret) : ret;
  }

  Set<Integer> primes(final int bound){
    Set<Integer> ret = new LinkedHashSet<>();
    for (int i = 2;i <= bound;i++) {
      boolean flg = true;
      for (final int p:ret)
        if (i %p == 0) {
          flg = false;
          break;
        } else if (i < p *p)
          break;
      if (flg)
        ret.add(i);
    }
    return ret;
  }

  /* 定数 */
  final int mod = 998244353;
  final String yes = "Yes";
  final String no = "No";

  /* 入力 */
  static class MyReader{
    byte[] buf = new byte[1 <<16];
    int head = 0;
    int tail = 0;
    InputStream in;

    public MyReader(final InputStream in){ this.in = in; }

    byte read(){
      if (head == tail) {
        try {
          tail = in.read(buf);
        } catch (IOException e) {
          e.printStackTrace();
        }
        head = 0;
      }
      return buf[head++];
    }

    boolean isPrintable(final byte c){ return 32 < c && c < 127; }

    boolean isNum(final byte c){ return 47 < c && c < 58; }

    byte nextPrintable(){
      byte ret = read();
      return isPrintable(ret) ? ret : nextPrintable();
    }

    int it(){ return (int) lg(); }

    int[] it(final int N){
      int[] a = new int[N];
      Arrays.setAll(a,i -> it());
      return a;
    }

    int[][] it(final int H,final int W){ return arr(new int[H][],i -> it(W)); }

    int idx(){ return it() -1; }

    int[] idx(final int N){
      int[] a = new int[N];
      Arrays.setAll(a,i -> idx());
      return a;
    }

    int[][] idx(final int H,final int W){ return arr(new int[H][],i -> idx(W)); }

    long lg(){
      byte i = nextPrintable();
      boolean negative = i == 45;
      long n = negative ? 0 : i -'0';
      while (isPrintable(i = read()))
        n = 10 *n +i -'0';
      return negative ? -n : n;
    }

    long[] lg(final int N){
      long[] a = new long[N];
      Arrays.setAll(a,i -> lg());
      return a;
    }

    long[][] lg(final int H,final int W){ return arr(new long[H][],i -> lg(W)); }

    char[] ch(){ return str().toCharArray(); }

    char[][] ch(final int H){ return arr(new char[H][],i -> ch()); }

    String line(){
      StringBuilder sb = new StringBuilder();

      byte c;
      while (isPrintable(c = read()) || c == ' ')
        sb.append((char) c);
      return sb.toString();
    }

    String str(){
      StringBuilder sb = new StringBuilder();
      sb.append((char) nextPrintable());
      byte c;
      while (isPrintable(c = read()))
        sb.append((char) c);
      return sb.toString();
    }

    String[] str(final int N){ return arr(new String[N],i -> str()); }

    <T> T[] arr(final T[] arr,final IntFunction<T> f){
      Arrays.setAll(arr,f);
      return arr;
    }
  }

  /* 出力 */
  static class MyWriter{
    OutputStream out;

    byte[] buf = new byte[1 <<16];
    byte[] ibuf = new byte[20];

    int tail = 0;

    public MyWriter(final OutputStream out){ this.out = out; }

    void flush(){
      try {
        out.write(buf,0,tail);
        tail = 0;
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    void write(final byte b){
      buf[tail++] = b;
      if (tail == buf.length)
        flush();
    }

    void write(final byte[] b,final int off,final int len){
      for (int i = off;i < off +len;i++)
        write(b[i]);
    }

    void write(final char c){ write((byte) c); }

    void write(long n){
      if (n < 0) {
        n = -n;
        write('-');
      }

      int i = ibuf.length;
      do {
        ibuf[--i] = (byte) (n %10 +'0');
        n /= 10;
      } while (n > 0);

      write(ibuf,i,ibuf.length -i);
    }

    void println(final long n){
      write(n);
      write('\n');
    }

    public void println(final double d){ println(String.valueOf(d)); }

    void println(final String s){
      byte[] b = s.getBytes();
      for (byte bb:b)
        write(bb);
      write('\n');
    }

    public void println(final char[] s){
      for (char bb:s)
        write(bb);
      write('\n');
    }

    void println(final int[] a){
      for (int i = 0;i < a.length;i++) {
        if (0 < i)
          write(' ');
        write(a[i]);
      }
      write('\n');
    }

  }
}
0