結果

問題 No.6 使いものにならないハッシュ
ユーザー fantasiabaetica
提出日時 2018-07-12 22:52:47
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 73 ms / 5,000 ms
コード長 1,596 bytes
コンパイル時間 749 ms
コンパイル使用メモリ 75,892 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-16 16:44:27
合計ジャッジ時間 2,636 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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