結果

問題 No.6 使いものにならないハッシュ
ユーザー recososo
提出日時 2020-05-31 13:37:37
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,419 bytes
コンパイル時間 1,567 ms
コンパイル使用メモリ 174,268 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-16 16:56:40
合計ジャッジ時間 2,528 ms
ジャッジサーバーID
(参考情報)
judge6 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:64:13: warning: 'ans' may be used uninitialized [-Wmaybe-uninitialized]
   64 |     cout << ans << endl;
      |             ^~~
main.cpp:51:9: note: 'ans' was declared here
   51 |     int ans;
      |         ^~~

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i,n) for (int i = 0; i < (n); ++i)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=1e9+7;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
vector<int> mem(200001,-1);
int f(int x){
    if(mem[x]!=-1){
        return mem[x];
    }
    if(x<10){
        return mem[x]=x;
    }
    int sum=0;
    while(x){
        sum+=x%10;
        x/=10;
    }
    return mem[x]=f(sum);
}
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int k,n;cin >> k >> n;
    vector<bool> p(n+1,true);
    p[0]=p[1]=false;
    for(int i=2;i*i<=n;i++){
        if(p[i]){
            for(int j=2;j*i<=n;j++){
                p[i*j]=false;
            }
        }
    }
    vector<int> v,h;
    for(int i=k;i<=n;i++){
        if(p[i]){
            v.push_back(i);
            h.push_back(f(i));
        }
    }
    int r=0;
    int x=h.size();
    int len=0;
    int ans;
    vector<int> c(10);
    for(int i=0;i<x;i++){
        while(r<x&&!c[h[r]]){
            c[h[r]]++;
            r++;
        }
        if(len<=r-i){
            ans=v[i];
            len=r-i;
        }
        c[h[i]]--;
    }
    cout << ans << endl;
}
0