結果

問題 No.6 使いものにならないハッシュ
ユーザー kyo1
提出日時 2020-06-02 11:39:02
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 1,404 bytes
コンパイル時間 2,393 ms
コンパイル使用メモリ 197,304 KB
最終ジャッジ日時 2025-01-10 20:32:10
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template <typename T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

class Prime {
 public:
  static vector<int> sieve_of_eratosthenes(int n) {
    vector<bool> flags(n + 1, true);
    flags[0] = flags[1] = false;
    for (int i = 0; i * i < n + 1; i++) {
      if (!flags[i]) continue;
      for (int k = 2; i * k < n + 1; k++) {
        flags[i * k] = false;
      }
    }
    vector<int> res;
    for (int i = 0; i < n + 1; i++) {
      if (flags[i]) res.emplace_back(i);
    }
    return res;
  }
};

int f(int x) {
  int res = 0;
  while (x > 0) {
    res += x % 10;
    x /= 10;
  }
  return (res < 10 ? res : f(res));
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int K, N;
  cin >> K >> N;
  auto primes = Prime::sieve_of_eratosthenes(N);
  vector<int> hashed;
  int base = 0;
  for (int prime : primes) {
    if (prime < K) {
      base++;
      continue;
    }
    hashed.emplace_back(f(prime));
  }
  int res = 0, mlen = 0;
  vector<bool> used(N + 2, false);
  int j = 0;
  for (int i = 0; i < (int)hashed.size(); i++) {
    while (j < (int)hashed.size() && !used[hashed[j]]) {
      used[hashed[j]] = true;
      j++;
    }
    used[hashed[i]] = false;
    if (chmax(mlen, j - i) || mlen == j - i) {
      res = primes[base + i];
    }
  }
  cout << res << '\n';
  return 0;
}
0