結果

問題 No.6 使いものにならないハッシュ
ユーザー arrowsarrows
提出日時 2017-01-20 23:33:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,262 bytes
コンパイル時間 602 ms
コンパイル使用メモリ 73,196 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-14 23:04:04
合計ジャッジ時間 1,800 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;

int h(int x)
{
    if (x < 10) {
        return x;
    }

    int sum = 0;
    while (x > 0) {
        sum += x % 10;
        x /= 10;
    }
    return h(sum);
}

int solve(vector<pair<int, int>>& p)
{
    vector<bool> u(10);
    int s = 0, res = 0, num = -1;
    for (int t = 0; t < (int)p.size(); t++) {
        int n = p[t].first;
        if (u[n]) {
            for (; s < t; s++) {
                if (p[s].first == n) {
                    s++;
                    break;
                }
                u[p[s].first] = 0;
            }
        } else {
            u[n] = 1;
        }
                
        if (t - s + 1 >= res) {
            res = t - s + 1;
            num = p[s].second;
        }
    }
    return num;
}

int main()
{
    constexpr int MAX = 252521;
    vector<bool> prime(MAX, 1);
    prime[0] = prime[1] = 0;
    for (int i = 2; i < MAX; i++) {
        if (!prime[i]) continue;
        for (int j = i * 2; j < MAX; j += i) {
            prime[j] = 0;
        }
    }

    int K, N;
    cin >> K >> N;
    vector<pair<int, int>> p;
    for (int i = K; i <= N; i++) {
        if (prime[i]) p.emplace_back(h(i), i);
    }

    cout << solve(p) << endl;
    return 0;
}
0