結果

問題 No.6 使いものにならないハッシュ
ユーザー keikei
提出日時 2018-03-29 19:47:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 5,000 ms
コード長 4,237 bytes
コンパイル時間 1,559 ms
コンパイル使用メモリ 170,576 KB
実行使用メモリ 4,480 KB
最終ジャッジ日時 2023-10-14 23:10:56
合計ジャッジ時間 3,233 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:https://yukicoder.me/problems/no/6>
 問題文============================================================
 ハッシュに興味を持ったFrankは、ハッシュアルゴリズムに、このようなアルゴリズムを考えた。
 自然数の各桁を足した合計、それも二桁以上になる場合は、それを繰り返す。
 つまり、
 hash(4)=4,
 hash(17)=hash(1+7)=hash(8)=8,
 hash(119)=hash(1+1+9)=hash(11)=hash(2)=2のようになる。
 
 しかし実際使ってみるとコリジョン(計算値がかぶってしまうこと)が多く、あまり使い物にならなかった。
 
 それでも諦めきれないFrankに対して、あなたは「落ち着け、素数を数えるんだ」と言った。
 やけになったFrankは、自然数 [K,N] (K≤i≤N の範囲ということ) の中の連続した素数列で上記のハッシュアルゴリズムを使用し、
 コリジョンが起こらない(値がかぶらない)最大の範囲を考えようとしている。
 言った手前、申し訳なくなったあなたは、Frankの代わりに求めてあげることにした。
 
 範囲[K,N] (K≤i≤N の範囲)に含まれる連続した素数列で上記のハッシュアルゴリズムを使用した時に、
 すべて異なる値になる最大の長さの素数列を求め、元の素数列の最初の素数を求めてください。
 (複数同じ長さの素数列がある場合は、数が大きい方を選択する)

 =================================================================
 解説=============================================================
 ================================================================
 */

ll Hash(ll x){
    if(x < 10) return x;
    string s = to_string(x);
    ll sum = 0; for(auto c:s) sum += (c-'0');
    return Hash(sum);
}

bool IsPrime(ll num)
{
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false; // 偶数はあらかじめ除く
    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2){
        if (num % i == 0){
            return false;
        }
    }
    return true;
}

ll solve(){
    ll res = 0;
    ll K,N; cin >> K >> N;
    vector<ll> primes;
    vector<ll> h;
    for(ll i = K; i <= N;i++){
        if(IsPrime(i)){
            primes.push_back(i);
            h.push_back(Hash(i));
        }
    }

//    for(auto v:primes) cout << setw(2) << v << " "; cout << endl;
//    for(auto v:h) cout << setw(2) << v << " "; cout << endl;
    
    ll maxlen = 0;
    vector<pll> hl(10,{0,-1});
    ll l = 0,r = 0;
    for(;r < (int)primes.size(); r++){
        if(hl[h[r]].first == 0){
            hl[h[r]].first = 1;
            hl[h[r]].second = r;
            if(r-l+1>=maxlen){
                maxlen = r-l+1;
                res = primes[l];
            }
        }else{
            while(l<=hl[h[r]].second){
                hl[h[l]].first = 0;
                l++;
            }
            hl[h[r]].first = 1;
            hl[h[r]].second = r;
            if(r-l+1>=maxlen){
                maxlen = r-l+1;
                res = primes[l];
            }
        }
    }
    return res;
}

int main(void) {
    cin.tie(0); ios::sync_with_stdio(false);
    cout << solve() << endl;
    return 0;
}
0