結果

問題 No.6 使いものにならないハッシュ
ユーザー motimoti
提出日時 2015-08-01 20:26:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 1,765 bytes
コンパイル時間 702 ms
コンパイル使用メモリ 97,956 KB
実行使用メモリ 7,508 KB
最終ジャッジ日時 2023-10-14 22:53:59
合計ジャッジ時間 1,923 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,712 KB
testcase_01 AC 2 ms
5,824 KB
testcase_02 AC 9 ms
7,508 KB
testcase_03 AC 3 ms
5,968 KB
testcase_04 AC 3 ms
6,020 KB
testcase_05 AC 4 ms
6,440 KB
testcase_06 AC 5 ms
6,612 KB
testcase_07 AC 3 ms
6,188 KB
testcase_08 AC 5 ms
6,432 KB
testcase_09 AC 3 ms
6,100 KB
testcase_10 AC 2 ms
5,744 KB
testcase_11 AC 3 ms
6,012 KB
testcase_12 AC 6 ms
6,884 KB
testcase_13 AC 3 ms
5,952 KB
testcase_14 AC 3 ms
5,948 KB
testcase_15 AC 5 ms
6,632 KB
testcase_16 AC 4 ms
6,236 KB
testcase_17 AC 7 ms
6,856 KB
testcase_18 AC 8 ms
7,280 KB
testcase_19 AC 7 ms
6,864 KB
testcase_20 AC 6 ms
6,752 KB
testcase_21 AC 2 ms
5,968 KB
testcase_22 AC 6 ms
6,772 KB
testcase_23 AC 6 ms
6,812 KB
testcase_24 AC 6 ms
6,728 KB
testcase_25 AC 4 ms
6,272 KB
testcase_26 AC 7 ms
7,104 KB
testcase_27 AC 6 ms
6,640 KB
testcase_28 AC 4 ms
6,356 KB
testcase_29 AC 7 ms
7,060 KB
testcase_30 AC 6 ms
6,996 KB
testcase_31 AC 6 ms
6,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>

#define int ll

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

bool pr[200001];
int PSIZE;
int vpr[200001];
int phash[200001];

int memo[200001];

int get_hash(int x) {
  if(memo[x] > 0) { return memo[x]; }
  if(x < 10) { return x; }
  string s = to_string(x);
  int sum = 0;
  int size = s.size();
  rep(i, size) {
    sum += s[i]-'0';
  }
  return memo[x] = get_hash(sum);
}

signed main() {

  int K, N; cin >> K >> N;

  fill(pr, pr+200001, true);

  pr[0] = pr[1] = 0;
  rep(i, N+1) {
    if(pr[i]) {
      pr[i] = 1;
      if(K <= i) {
        vpr[PSIZE++] = i;
      }
      for(int j=i*i; j<=N+1; j+=i) {
        pr[j] = 0;
      }
    }
  }

  if(PSIZE == 0) { cout << 0 << endl; return 0; }

  rep(i, PSIZE) {
    phash[i] = get_hash(vpr[i]);
  }

  int maxlen = 0, ans = 0;
  int l = 0, r = 0;

  unordered_set<int> st;
//  deque<int> dq;
  while(1) {
    if(r >= PSIZE) { break; }
    if(!st.count(phash[r])) {
      st.insert(phash[r]);//  dq.push_back(vpr[r]);
      if(maxlen <= (int)st.size()) {
        maxlen = st.size();
        ans = vpr[l];
        /*
        for(auto const& ii: dq) {
          cout << ii << ' ';
        }
        cout << endl;
        */
      }
      r++;
    }
    else {
      while(1) {
        st.erase(phash[l++]);
        if(l == r) { break; }
        if(!st.count(phash[r])) {
          break;
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}
0