結果

問題 No.6 使いものにならないハッシュ
ユーザー fantasiabaeticafantasiabaetica
提出日時 2018-07-14 14:21:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 5,000 ms
コード長 1,695 bytes
コンパイル時間 704 ms
コンパイル使用メモリ 78,624 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-14 23:12:11
合計ジャッジ時間 6,146 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>
#include <set>
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
using namespace std;

// 素数かどうか調べる
int if_prime(int n){
    if (n == 1) return 0;
    int root_n = sqrt(n);
    FOR(i, 2, root_n + 1){
        if (n % i == 0) return 0;
    }
    return 1;
}

// hash値を調べる
int to_hash(int n){
    while (true){
        string a;
        a = to_string(n);
        if (a.length() == 1) return n;
        n = 0;
        FOR(i, 0, a.length()){
            n += (a[i] - '0');
        }
    }
}

int main(){
    int k, n;
    cin >> k >> n;
    // 素数のvector
    vector<int> primes;
    FOR(i, k, n + 1){
        if (if_prime(i)) primes.push_back(i);
    }
    // 素数のhash値のvector
    vector<int> prime_hash;
    FOR(i, 0, primes.size()){
        prime_hash.push_back(to_hash(primes[i]));
    }
    // 最長部分を求める
    int max_length = 0;
    int max_start = -1;
    // 最長部分を求める
    int dp[10];
    memset(dp, 0, sizeof(dp));
    // 内側のループを抜けた状態では[start, end)がハッシュ値がすべて異なる最長区間
    int start = 0;
    int end = 0;
    while (start < primes.size()){
        while (end < primes.size()) {
            if (dp[prime_hash[end]] == 0) {
                dp[prime_hash[end]] = 1;
                end += 1;
            } else {
                break;
            }
        }
        if (end - start >= max_length){
            max_start = primes[start];
            max_length = end - start;
        }
        dp[prime_hash[start]] = 0;
        start += 1; 
    }
    cout << max_start << endl;
    return 0;
}
0