結果

問題 No.6 使いものにならないハッシュ
ユーザー tokkakatokkaka
提出日時 2016-07-17 08:39:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 1,887 bytes
コンパイル時間 1,045 ms
コンパイル使用メモリ 108,956 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-14 22:59:51
合計ジャッジ時間 2,317 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <functional>
#include <tuple>
#include <climits>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <iomanip>

using namespace std;

class Sieve
{
    std::vector<bool> prime_;
public:
    Sieve(int max)
    {
        prime_.resize(max, true);
        prime_[0] = prime_[1] = false;
        for(int i=2; i<max/2;i++) {
            for(int j=2; i*j<max;j++)
                prime_[i*j] = false;
        }
    }

    bool isPrime(int n)
    {
        return prime_[n];
    }
};

int h(int n)
{
    int s = 0;
    if(n < 10) return n;
    while(n >= 10) {
        s += h(n % 10);
        n /= 10;
    }
    s += n;
    return s>=10?h(s):s;
}

int main()
{
    Sieve s(200000);
    int K, N;
    cin >> K >> N;
    std::vector<int> S;
    std::vector<int> Sh;
    for(int i=0;i<N-K+1;i++) {
        if(s.isPrime(K+i)) {
            S.push_back(K+i);
            Sh.push_back(h(K+i));
        }
    }

    int first=S[0];
    int length=0;
    int max_first = S[0];
    int max_length = 0;
    unordered_map<int, int> seq;
    for(int i=0; i<S.size(); i++) {
        if(seq.find(Sh[i]) == seq.end()) {
            length++;
            seq[Sh[i]] = i;
        } else {
            if(length >= max_length) {
                max_first = first;
                max_length = length;
            }
            length = i - seq[Sh[i]];
            first = S[seq[Sh[i]]+1];
            
            for(auto it=seq.begin();it!=seq.end();) {
                if(it->second < seq[Sh[i]]) {
                    it = seq.erase(it);
                    continue;
                }
                ++it;
            }

            seq[Sh[i]] = i;
        }
    }
    if(length >= max_length) {
        max_first = first;
    }
    cout << max_first << endl;
}
0