結果

問題 No.6 使いものにならないハッシュ
ユーザー H3PO4
提出日時 2023-02-12 12:42:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,406 bytes
コンパイル時間 876 ms
コンパイル使用メモリ 76,076 KB
最終ジャッジ日時 2025-02-10 14:44:10
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:59:18: warning: ‘ans’ may be used uninitialized [-Wmaybe-uninitialized]
   59 |     std::cout << ans << std::endl;
      |                  ^~~
main.cpp:45:9: note: ‘ans’ was declared here
   45 |     int ans;
      |         ^~~

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <vector>
std::vector<int> primes(int n) {
std::vector<bool> is_prime(n + 1, true);
is_prime.at(0) = false;
is_prime.at(1) = false;
std::vector<int> res;
for (int i = 2; i < n + 1; i++) {
if (is_prime.at(i)) {
res.push_back(i);
for (int j = i * 2; j < n + 1; j += i) {
is_prime.at(j) = false;
}
}
}
return res;
}
int calc_hash(int p) {
int hash = p;
while (hash >= 10) {
int new_hash = 0;
while (hash != 0) {
new_hash += hash % 10;
hash /= 10;
}
hash = new_hash;
}
return hash;
}
int main() {
int K, N;
std::cin >> K;
std::cin >> N;
std::vector<int> primes_candidate;
for (auto &p: primes(N)) {
if (p < K) { continue; }
primes_candidate.push_back(p);
}
std::vector<int> exist_indices(10, -1);
int ans;
int l = 0, M = 0;
for (int r = 0; r < primes_candidate.size(); r++) {
int hash = calc_hash(primes_candidate.at(r));
if (exist_indices.at(hash) != -1
&& l < exist_indices.at(hash) + 1) {
l = exist_indices.at(hash) + 1;
}
exist_indices.at(hash) = r;
if (M <= r - l) {
ans = primes_candidate.at(l);
M = r - l;
}
}
std::cout << ans << std::endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0