結果

問題 No.6 使いものにならないハッシュ
ユーザー kyunakyuna
提出日時 2020-04-24 08:12:16
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,041 bytes
コンパイル時間 835 ms
コンパイル使用メモリ 75,160 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-14 23:23:36
合計ジャッジ時間 2,227 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;

int digital_root(int n, int base = 10) {
    if (n == 0) return 0;
    if (n % (base - 1) == 0) return base - 1;
    return n % (base - 1);
}

vector<int> prime_table(int n) {
    vector<int> is_prime(n + 1);
    if (n) iota(is_prime.begin() + 2, is_prime.end(), 2);
    for (int i = 2; i * i <= n; i++) if (is_prime[i]) {
        for (int j = i + i; j <= n; j += i) is_prime[j] = 0;
    }
    return is_prime;
}

int main() {
    int k; cin >> k;
    int n; cin >> n;
    auto is_prime = prime_table(n);
    vector<int> ps, ds;
    for (int x = k; x <= n; x++) if (is_prime[x]) ps.emplace_back(x);
    for (auto &p: ps) ds.emplace_back(digital_root(p));
    pair<int, int> ma{-1, -1};
    for (int i = 0, sz = ps.size(); i < sz; i++) {
        int cur = i, mask = 0;
        while (cur < sz && !(mask >> ds[cur] & 1)) mask ^= 1 << ds[cur], cur++;
        ma = max(ma, {cur - i, ps[i]});
    }
    cout << ma.second << endl;
    return 0;
}
0