結果

問題 No.6 使いものにならないハッシュ
ユーザー mayoko_mayoko_
提出日時 2014-12-11 10:29:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 16 ms / 5,000 ms
コード長 2,175 bytes
コンパイル時間 584 ms
コンパイル使用メモリ 76,732 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 22:47:44
合計ジャッジ時間 2,127 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>

#define INF 1000000000

using namespace std;
typedef long long ll;

const int MAXN = 400200;
bool isPrime[MAXN];
vector<int> prime;
bool flag[10];

int getHash(int n) {
    if (n < 10) return n;
    string s = to_string(n);
    int ret = 0;
    for (int i = 0; i < s.size(); i++) {
        ret += s[i] - '0';
    }
    return getHash(ret);
}

void createPrime() {
    for (int i = 2; i < MAXN; i++) isPrime[i] = true;
    for (int i = 2; i * i < MAXN; i++) {
        if (isPrime[i]) {
            for (int j = 2; i * j < MAXN; j++) {
                isPrime[i*j] = false;
            }
        }
    }
    for (int i = 2; i < MAXN; i++) {
        if (isPrime[i]) prime.push_back(i);
    }
}

int main(void) {
    createPrime();
    int K, N;
    cin >> K;
    cin >> N;
    int startIndex = 0;
    int endIndex = 0;
    for (int i = 0; ; i++) {
        if (prime[i] >= K) {
            startIndex = i;
            break;
        }
    }
    for (int i = startIndex; ; i++) {
        if (prime[i] > N) {
            endIndex = i;
            break;
        }
    }
//    cout << startIndex << "  " << endIndex << endl;
    // しゃくとり法
    int ansIndex = -1;
    int maxLength = 0;
    int s = startIndex, t = startIndex;
    while (1) {
        // collisionするまで長さを増やす
//        cout << "start " << s << endl;
        while (t < endIndex) {
            int tmp = getHash(prime[t]);
            if (flag[tmp]) break;
            else {
                flag[tmp] = true;
                t++;
            }
        }
//        cout << "end " << t << endl;
        if (maxLength <= t - s) {
            ansIndex = s;
            maxLength = t-s;
        }
        flag[getHash(prime[s])] = false;
        s++;
        if (t >= endIndex) break;
    }
    cout << prime[ansIndex] << endl;
    return 0;
}
0