結果

問題 No.6 使いものにならないハッシュ
コンテスト
ユーザー tottoripaper
提出日時 2014-11-16 02:07:56
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 16 ms / 5,000 ms
コード長 1,208 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 508 ms
コンパイル使用メモリ 84,332 KB
実行使用メモリ 7,972 KB
最終ジャッジ日時 2026-04-05 21:01:24
合計ジャッジ時間 1,580 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:61:11: warning: 'first' may be used uninitialized [-Wmaybe-uninitialized]
   61 |     printf("%d\n", first);
      |     ~~~~~~^~~~~~~~~~~~~~~
main.cpp:42:24: note: 'first' was declared here
   42 |     int maxLength = 0, first;
      |                        ^~~~~

ソースコード

diff #
raw source code

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

int isPrime(int x){
    if(x == 1){return false;}

    for(int i=2;i*i<=x;i++){
        if(x % i == 0){return false;}
    }
    return true;
}

int hash(int x){
    std::string s = std::to_string(x);
    while(s.size() > 1){
        int sum = 0;
        for(auto c : s){sum += c - '0';}
        s = std::to_string(sum);
    }

    return s[0] - '0';
}

int main(){
    int K, N;
    scanf("%d %d", &K, &N);

    std::vector<int> primes;
    for(int i=K;i<=N;i++){
        if(isPrime(i)){primes.push_back(i);}
    }
    
    int p_length = primes.size();

    std::vector<int> hs;
    for(int i=0;i<p_length;i++){
        hs.push_back(hash(primes[i]));
    }

    int maxLength = 0, first;
    int table[10];
    std::fill(table, table+10, 0);

    for(int i=0,j=0;i<p_length;){
        while(j<p_length && table[hs[j]] == 0){
            table[hs[j]] += 1;
            j += 1;
        }

        if(j-i >= maxLength){
            maxLength = j-i;
            first = primes[i];
        }

        table[hs[i]] -= 1;
        i += 1;
    }

    printf("%d\n", first);
}
0