結果

問題 No.6 使いものにならないハッシュ
ユーザー Yang33Yang33
提出日時 2018-04-07 00:32:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 3,672 bytes
コンパイル時間 1,421 ms
コンパイル使用メモリ 167,580 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 23:11:02
合計ジャッジ時間 2,647 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/04/06  Problem: yukicoder 006  / Link: http://yukicoder.me/problems/no/006  ----- */
/* ------問題------

ハッシュに興味を持った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 の範囲)に含まれる連続した素数列で上記のハッシュアルゴリズムを使用した時に、
すべて異なる値になる最大の長さの素数列を求め、元の素数列の最初の素数を求めてください。
(複数同じ長さの素数列がある場合は、数が大きい方を選択する)

-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */

#define primeN 200002 //78493個
// 78493番目までの素数のリストを返す O(NloglogN)
vector<int > make_prime() {
	vector<int >a;
	bool prime[primeN + 1];// 外部だとハッシュ表みたいになる
	FOR(i, 0, primeN + 1) {
		prime[i] = 1;
	}prime[0] = prime[1] = 0;

	for (int i = 2; i <= primeN; i++) {
		if (prime[i]) {
			a.push_back((long long)i);
			for (int j = i * 2; j <= primeN; j += i)
				prime[j] = 0;
		}
	}
	return a;
}
#undef primeN 

LL K, N;

int ans = 0;

int f(int x) {
	int sum = x;
	int ret = sum;
	do {
		sum = ret;
		ret = 0;
		while (sum) {
			ret += sum % 10;
			sum /= 10;
		}
	} while (ret > 9);
	return ret;
}
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	cin >> K >> N;
	vector<int> a = make_prime();
	int i = 0, j = 0;
	while (a[i] < K)i++;
	j = i;
	int ansid = a[i];
	ans = 1;
	VI used(10, 0);
	while (a[i] <= N) {

		for (;a[j]<=N && !used[f(a[j])] ;j++ ) { // [i,j)が正しい区間
			used[f(a[j])] = 1;
		}
		if (ans <= j - i) {
			ans = j - i;
			ansid = a[i];
		}
		used[f(a[i])] = 0;
		i++;
	}
	//cout << ans << "\n";
	cout << ansid << endl;
	return 0;
}
0