結果

問題 No.6 使いものにならないハッシュ
ユーザー arrowsarrows
提出日時 2017-01-20 23:17:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,219 bytes
コンパイル時間 676 ms
コンパイル使用メモリ 74,580 KB
実行使用メモリ 4,572 KB
最終ジャッジ日時 2023-08-24 18:22:51
合計ジャッジ時間 2,500 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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;
                }
            }
        } 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