結果

問題 No.6 使いものにならないハッシュ
ユーザー togari_takamototogari_takamoto
提出日時 2015-12-13 04:38:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,597 bytes
コンパイル時間 767 ms
コンパイル使用メモリ 96,044 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 22:55:54
合計ジャッジ時間 2,135 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <iomanip>
#include <numeric>
#include <memory>
#include <limits.h>
#include <set>
#include <cassert>
#include <cmath>
#include <bitset>
#include <functional>
#include <limits>
using namespace std;

typedef long long ll;

#define REP(i,n) for(ll i=0; i<(n); ++i)
#define TEN(x) ((ll)1e##x)
#define ALL(v) (v).begin(), (v).end()

// [a,b)の整数に対して篩 O((b-a) log log b): is_prime[i-a] = true ⇔ iが素数
vector<int> segment_sieve(ll a, ll b){
	vector<int> t_small(sqrt(b)+1, true);
	vector<int> t(b-a, true);
	
	for(ll i = 2; i*i < b; ++i) if(t_small[i]){
		for(ll j = 2*i; (ll)j*j < b; j+=i) t_small[j] = false;  // [2, √b)の篩
		for(ll j = max(2LL, (a+i-1)/i) * i; j < b; j+=i) t[j - a] = false; // [a,b)の篩
	}
	
	return t;
}

// 数字根
// 345 -> 3+4+5 = 12 -> 1+2 = 3 ≡ 345 (mod 9)
ll numroot(ll a){
	if(a!=0 && a%9==0) return 9;
	return a%9;
}

int main(){
	ll k, n;
	cin >> k >> n;
	auto t = segment_sieve(k, n + 1); 
	vector<ll> v; v.reserve(t.size());
	REP(i, t.size()) if (t[i] && i+k!=1) v.push_back(i + k);
	vector<ll> r(v.size());
	REP(i, v.size()) r[i] = numroot(v[i]);
	
	vector<bool> used(10, false);
	vector<ll> max_l(11, -1);
	ll end = v.size();
	for (ll i = v.size() - 1; i >= 0; --i) {
		while (used[r[i]]) {
			end--;
			used[r[end]] = false;
		}
		used[r[i]] = true;
		if (max_l[end - i] == -1) max_l[end - i] = v[i];
	}
	reverse(ALL(max_l));
	for (auto x : max_l) if (x != -1) {
		cout << x << endl;
		return 0;
	}
}
0