結果

問題 No.6 使いものにならないハッシュ
ユーザー face4face4
提出日時 2018-09-23 15:13:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,201 bytes
コンパイル時間 678 ms
コンパイル使用メモリ 81,524 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-14 23:12:23
合計ジャッジ時間 1,866 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,356 KB
testcase_02 AC 5 ms
4,352 KB
testcase_03 AC 3 ms
4,352 KB
testcase_04 AC 3 ms
4,352 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,352 KB
testcase_07 AC 3 ms
4,356 KB
testcase_08 AC 3 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 3 ms
4,352 KB
testcase_12 AC 3 ms
4,356 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 3 ms
4,348 KB
testcase_16 AC 3 ms
4,352 KB
testcase_17 AC 4 ms
4,348 KB
testcase_18 AC 5 ms
4,348 KB
testcase_19 AC 4 ms
4,352 KB
testcase_20 AC 4 ms
4,352 KB
testcase_21 AC 3 ms
4,352 KB
testcase_22 AC 4 ms
4,356 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,352 KB
testcase_27 AC 4 ms
4,348 KB
testcase_28 AC 3 ms
4,348 KB
testcase_29 AC 4 ms
4,348 KB
testcase_30 AC 4 ms
4,348 KB
testcase_31 AC 4 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    bool nonprime[200001] = {};
    nonprime[0] = nonprime[1] = true;
    for(int i = 2; i < 200000; i++){
        if(nonprime[i]) continue;
        for(int j = i+i; j < 200001; j+=i){
            nonprime[j] = true;
        }
    }

    int k, n;
    cin >> k >> n;

    vector<int> prime, hash;

    for(int i = k; i <= n; i++){
        if(nonprime[i]) continue;
        int x = i;
        while(x > 9){
            int hash = 0;
            while(x > 0)    hash += x%10, x /= 10;
            x = hash;
        }
        prime.push_back(i);
        hash.push_back(x);
    }

    int siz = hash.size();
    int l = 0, r = 0, anspos = 0, maxlen = 1;
    set<int> s;
    while(l < siz){
        while(r < siz && s.count(hash[r]) == 0){
            s.insert(hash[r]);
            r++;
        }

        if(r-l >= maxlen){
            anspos = l;
            maxlen = r-l;
        }

        if(r == siz)  break;

        while(hash[l] != hash[r]){
            s.erase(hash[l]);
            l++;
        }
        
        l++;
        r++;
    }

    cout << prime[anspos] << endl;

    return 0;
}
0