結果

問題 No.6 使いものにならないハッシュ
ユーザー fantasiabaeticafantasiabaetica
提出日時 2018-07-12 22:52:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 71 ms / 5,000 ms
コード長 1,596 bytes
コンパイル時間 591 ms
コンパイル使用メモリ 75,368 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 23:12:04
合計ジャッジ時間 2,548 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 71 ms
4,352 KB
testcase_03 AC 5 ms
4,348 KB
testcase_04 AC 9 ms
4,352 KB
testcase_05 AC 12 ms
4,348 KB
testcase_06 AC 27 ms
4,348 KB
testcase_07 AC 12 ms
4,352 KB
testcase_08 AC 21 ms
4,348 KB
testcase_09 AC 6 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 6 ms
4,348 KB
testcase_12 AC 37 ms
4,348 KB
testcase_13 AC 5 ms
4,352 KB
testcase_14 AC 7 ms
4,348 KB
testcase_15 AC 26 ms
4,348 KB
testcase_16 AC 15 ms
4,348 KB
testcase_17 AC 40 ms
4,352 KB
testcase_18 AC 63 ms
4,352 KB
testcase_19 AC 38 ms
4,352 KB
testcase_20 AC 33 ms
4,352 KB
testcase_21 AC 4 ms
4,348 KB
testcase_22 AC 36 ms
4,352 KB
testcase_23 AC 34 ms
4,348 KB
testcase_24 AC 34 ms
4,348 KB
testcase_25 AC 15 ms
4,348 KB
testcase_26 AC 49 ms
4,348 KB
testcase_27 AC 29 ms
4,348 KB
testcase_28 AC 17 ms
4,348 KB
testcase_29 AC 51 ms
4,348 KB
testcase_30 AC 43 ms
4,348 KB
testcase_31 AC 36 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#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');
        }
    }
}

// primesにおけるprimes[i]からの最長部分を求める
int how_long(int i, vector<int> prime_hash){
    set<int> tmp;
    FOR(j, i, prime_hash.size()){
        if (tmp.find(prime_hash[j]) == tmp.end()){
            tmp.insert(prime_hash[j]);                
        } else {
            break;
        }
    }
    return tmp.size();
}

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;
    // 最長部分を求める
    FOR(i, 0, primes.size()){
        int length = how_long(i, prime_hash);
        if (length >= max_length){
            max_length = length;
            max_start = primes[i];
        }
    }
    cout << max_start << endl;
    return 0;
}
0