結果

問題 No.6 使いものにならないハッシュ
ユーザー troro_kelptroro_kelp
提出日時 2018-12-30 15:40:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,748 bytes
コンパイル時間 800 ms
コンパイル使用メモリ 96,764 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-14 23:12:52
合計ジャッジ時間 1,950 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <stack>
#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <functional>
#include <numeric>
using ll = long long;
using namespace std;

const ll MOD = 1e9 + 7;
vector<int> prime;

bool sieve[200010];

int key[100010];
int Hash[55];
int main(void)
{
    int K, N;
    cin >> K >> N;

    for (int i = 0; i < 200010; ++i)
        sieve[i] = true;

    sieve[0] = sieve[1] = false;

    for (int i = 2; i * i < 200010; ++i)
    {
        if (sieve[i])
        {
            for (int j = 2; i * j < 200010; ++j)
            {
                sieve[i * j] = false;
            }
        }
    }
    for (int i = K; i <= N; ++i)
        if (sieve[i])
            prime.push_back(i);

    for (int i = 0; i < prime.size(); ++i)
    {
        int s = 0;
        string t = to_string(prime[i]);
        while (1)
        {
            for (int j = 0; j < t.size(); ++j)
            {
                s += t[j] - '0';
            }
            t = to_string(s);
            if (t.size() == 1)
                break;
            s = 0;
        }
        key[i] = s;
        //cout << s << endl;
    }

    int left = 0, right = 0;
    int ans = 0, max_len = 0;
    for (left = 0; left < prime.size(); ++left)
    {
        int t = 0;
        while (right < prime.size() && Hash[key[right]] == 0)
        {
            Hash[key[right]]++;
            right++;
        }

        t = right - left;
        if (t >= max_len)
        {
            max_len = t;
            ans = max(ans, prime[left]);
        }

        if (right == left)
            right++;

        Hash[key[left]]--;
    }

    cout << ans << endl;

    return 0;
}
0