結果

問題 No.6 使いものにならないハッシュ
ユーザー mh72012246mh72012246
提出日時 2016-11-07 15:45:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,792 bytes
コンパイル時間 1,578 ms
コンパイル使用メモリ 77,780 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-14 23:03:34
合計ジャッジ時間 2,146 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cmath>
#include <vector>
#include <array>
#include <queue>
#include <set>
#include <map>
#include <sstream>   // istringstream
#include <algorithm> // sort
#include <utility>   // pair
#include <cfloat>    // DBL_MAX

typedef long long ll;
using namespace std;

const int maxN = 200000;
bool ps[maxN-1];

int myhash(int p){
  while(p>9){
    int sum=0, tmp=p;
    while(tmp>0){
      sum += tmp%10;
      tmp /= 10;
    }
    p = sum;
  }
  return p;
}

int main(){
  ll K,N; // [2, 200 000]
  cin >> K >> N;

  // prime (K ~ N)
  vector<int> primes;
  for(int i=0; i<N-1; i++){ ps[i] = true; }
  for(int i=2; i<sqrt(N)+1; i++){
    if(ps[i-2]){
      for(int j=i*i; j<=N; j+=i){
        ps[j-2] = false;
      }
    }
  }
  for(int i=K; i<=N; i++){
    if(ps[i-2]){
      primes.push_back(i);
    }
  }

  int np = primes.size();

  // main
  vector<int> hashed(np);
  for(int i=0; i<np; i++){
    hashed[i] = myhash(primes[i]);
  }

  vector<int> maxlen(np, 10);
  for(int n=1; n<10; n++){
    int c,tmp=0,nn;
    for(nn=0; nn<(int)np; nn++){
      if(hashed[nn]==n){
        if(tmp>0){ break; }
        tmp++;
      }
    }
    c=nn;

    for(int i=0; i<(int)np; i++){
      maxlen[i] = min(maxlen[i], c-i);
      if(hashed[i]==n){
        for(nn=c+1; nn<(int)np; nn++){
          if(hashed[nn]==n) { break; }
        }
        c=nn;
      }
    }
  }

  // for(int i=0; i<np; i++){
  //   cout << primes[i] << " ";
  // } cout << endl;
  // for(int i=0; i<np; i++){
  //   cout << hashed[i] << " ";
  // } cout << endl;
  // for(int i=0; i<np; i++){
  //   cout << maxlen[i] << " ";
  // } cout << endl;

  int maxid=0;
  for(int i=0; i<np; i++){
    if(maxlen[i] >= maxlen[maxid]){
      maxid = i;
    }
  }
  cout << primes[maxid] << endl;

  return 0;
}
0