結果

問題 No.6 使いものにならないハッシュ
ユーザー imulanimulan
提出日時 2016-06-20 11:52:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,644 bytes
コンパイル時間 1,484 ms
コンパイル使用メモリ 169,108 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-14 22:59:38
合計ジャッジ時間 2,677 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,348 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 4 ms
4,348 KB
testcase_03 AC 3 ms
4,348 KB
testcase_04 AC 3 ms
4,352 KB
testcase_05 AC 3 ms
4,352 KB
testcase_06 AC 3 ms
4,352 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 3 ms
4,356 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 3 ms
4,356 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 3 ms
4,352 KB
testcase_16 AC 3 ms
4,348 KB
testcase_17 AC 3 ms
4,348 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 3 ms
4,352 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 3 ms
4,352 KB
testcase_23 AC 3 ms
4,352 KB
testcase_24 AC 4 ms
4,352 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 3 ms
4,348 KB
testcase_27 AC 3 ms
4,348 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 3 ms
4,348 KB
testcase_31 AC 3 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

//ハッシュ値を計算
int h(int x)
{
    while(1)
    {
        int t=0;
        while(x>0)
        {
            t+=x%10;
            x/=10;
        }
        x=t;

        if(x<10) break;
    }

    return x;
}

int main()
{
    const int N=200000;
    bool p[N+1];
    fill(p,p+N+1,true);
    p[0]=p[1]=false;
    for(int i=2; i<=N; ++i)
    {
        if(p[i]) for(int j=2; i*j<=N; ++j) p[i*j]=false;
    }

    int k,n;
    cin >>k >>n;

    //素数リスト
    vector<int> a;
    for(int i=k; i<=n; ++i) if(p[i]) a.pb(i);

    //ハッシュ値
    vector<int> b(a);
    rep(i,b.size()) b[i]=h(b[i]);

    //rep(i,b.size()) printf("%d: %d\n",a[i], b[i]);

    int len=0;
    int ans=a[0];

    int st=0;
    int ct[10]={0};
    rep(i,b.size())
    {
        if(ct[b[i]]>0)
        {
            int tlen=i-st;
            //printf(" %d - %d : %d\n",a[st],a[i-1],tlen);
            if(tlen>len || (tlen==len&&a[st]>ans))
            {
                len=tlen;
                ans=a[st];
            }

            while(st<i && ct[b[i]]>0)
            {
                --ct[b[st]];
                ++st;
            }
        }

        ++ct[b[i]];
    }

    int tlen=b.size()-st;
    if(tlen>len || (tlen==len&&a[st]>ans))
    {
        len=tlen;
        ans=a[st];
    }

    cout << ans << endl;
    return 0;
}
0