結果

問題 No.6 使いものにならないハッシュ
ユーザー kyo1kyo1
提出日時 2020-06-02 11:39:02
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,404 bytes
コンパイル時間 2,235 ms
コンパイル使用メモリ 204,724 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-14 23:24:28
合計ジャッジ時間 3,673 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 4 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 3 ms
4,352 KB
testcase_09 AC 3 ms
4,356 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 3 ms
4,352 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 3 ms
4,352 KB
testcase_18 AC 4 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 3 ms
4,352 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 4 ms
4,348 KB
testcase_23 AC 3 ms
4,348 KB
testcase_24 AC 3 ms
4,348 KB
testcase_25 AC 3 ms
4,352 KB
testcase_26 AC 4 ms
4,352 KB
testcase_27 AC 3 ms
4,352 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 3 ms
4,352 KB
testcase_31 AC 3 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

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