結果

問題 No.6 使いものにならないハッシュ
ユーザー cormorancormoran
提出日時 2016-11-14 13:35:16
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,712 bytes
コンパイル時間 1,605 ms
コンパイル使用メモリ 174,400 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 23:03:44
合計ジャッジ時間 2,908 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 4 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 4 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 3 ms
4,352 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 4 ms
4,352 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 3 ms
4,352 KB
testcase_23 AC 3 ms
4,352 KB
testcase_24 AC 3 ms
4,348 KB
testcase_25 AC 3 ms
4,352 KB
testcase_26 AC 4 ms
4,348 KB
testcase_27 AC 3 ms
4,348 KB
testcase_28 AC 3 ms
4,348 KB
testcase_29 AC 4 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;

#define rep(i, j) for(int i=0; i < (int)(j); i++)
#define repeat(i, j, k) for(int i = (j); i < (int)(k); i++)

template<class T> bool set_max(T &a, const T &b) { return a <= b  ? a = b, true : false; }

class Solver {
  public:
    vector<uint8_t> sieve_of_eratosthenes(int n) {
        vector<uint8_t> ret(n + 1, 1);
        ret[0] = ret[1] = 0;
        repeat(i, 2, n + 1) {
            if(ret[i]) {
                for(int j = i * i; j < ret.size(); j += i) ret[j] = 0;
            }
        }
        return ret;
    }
    vector<int> prime_production(int k, int n) { // [k, n]
        auto is_p = sieve_of_eratosthenes(n);
        vector<int> ret;
        repeat(i,k, n + 1) if(is_p[i]) ret.push_back(i);
        return ret;
    }
    int hash(int n) {
        while(n >= 10) {
            int nxt = 0;
            while(n) {
                nxt += n % 10;
                n /= 10;
            }
            n = nxt;
        }
        return n;
    }
    bool solve() {
        int K, N; cin >> K >> N;        
        vector<int> primes = prime_production(K, N);

        int ans_len = -1, ans;
        queue<int> list;
        vector<bool> used(10);
        for(int p : primes) {
            int h = hash(p);
            while(used[h]) {
                used[hash(list.front())] = false;
                list.pop();
            }
            list.push(p);
            used[h] = true;
            if(set_max(ans_len, (int)list.size())) ans = list.front();
        }
        assert(ans_len > 0);
        cout << ans << endl;
        return 0;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    Solver s;
    s.solve();
    return 0;
}
0