結果

問題 No.6 使いものにならないハッシュ
ユーザー tottoripaper
提出日時 2014-11-16 02:07:56
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 26 ms / 5,000 ms
コード長 1,208 bytes
コンパイル時間 662 ms
コンパイル使用メモリ 65,996 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-16 16:22:26
合計ジャッジ時間 1,944 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:28:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   28 |     scanf("%d %d", &K, &N);
      |     ~~~~~^~~~~~~~~~~~~~~~~

ソースコード

diff #

#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