結果

問題 No.6 使いものにならないハッシュ
ユーザー H3PO4H3PO4
提出日時 2023-02-12 12:42:47
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,406 bytes
コンパイル時間 1,576 ms
コンパイル使用メモリ 77,688 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-23 04:49:00
合計ジャッジ時間 2,768 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 5 ms
4,380 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 4 ms
4,376 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 4 ms
4,376 KB
testcase_27 AC 4 ms
4,380 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 4 ms
4,380 KB
testcase_30 AC 3 ms
4,376 KB
testcase_31 AC 4 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:59:18: 警告: ‘ans’ may be used uninitialized [-Wmaybe-uninitialized]
   59 |     std::cout << ans << std::endl;
      |                  ^~~
main.cpp:45:9: 備考: ‘ans’ はここで定義されています
   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;
}
0