結果
| 問題 | No.6 使いものにならないハッシュ | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2022-12-31 16:11:22 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 12 ms / 5,000 ms | 
| コード長 | 1,023 bytes | 
| コンパイル時間 | 1,966 ms | 
| コンパイル使用メモリ | 203,660 KB | 
| 最終ジャッジ日時 | 2025-02-09 22:23:40 | 
| ジャッジサーバーID (参考情報) | judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 32 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
const int M = 200000;
int myhash(int a) {
    if (a < 10) return a;
    int ret = 0;
    while (a) {
        ret += a % 10;
        a /= 10;
    }
    return myhash(ret);
}
int main() {
    vector<bool> isP(M, true);
    isP[0] = isP[1] = false;
    for (int i = 2; i <= M; i++) {
        if (isP[i]) {
            for (int j = 2*i; j <= M; j += i) {
                isP[j] = false;
            }
        }
    }
    int K, N;
    cin >> K >> N;
    vector<int> ps;
    for (int i = K; i <= N; i++) if (isP[i]) ps.push_back(i);
    set<int> st;
    int ri = 0;
    int id = -1;
    int M_len = -1;
    for (int le = 0; le < ps.size(); le++) {
        while (ri < ps.size() && st.count(myhash(ps[ri])) == 0) {
            st.insert(myhash(ps[ri]));
            ri++;
        }
        if (M_len <= ri - le) {
            M_len = ri - le;
            id = le;
        }
        if (ri == le) ri++;
        else st.erase(myhash(ps[le]));
    }
    cout << ps[id] << endl;
}
            
            
            
        