結果
| 問題 |
No.6 使いものにならないハッシュ
|
| コンテスト | |
| ユーザー |
data9824
|
| 提出日時 | 2015-06-10 22:54:32 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 4 ms / 5,000 ms |
| コード長 | 1,209 bytes |
| コンパイル時間 | 500 ms |
| コンパイル使用メモリ | 63,540 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-09-16 16:26:51 |
| 合計ジャッジ時間 | 1,430 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge6 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 32 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX_PRIME = 200100;
bool isPrime[MAX_PRIME + 1];
vector<int> primes;
int main() {
fill(&isPrime[0], &isPrime[MAX_PRIME] + 1, true);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i <= MAX_PRIME; ++i) {
if (isPrime[i]) {
for (int k = i * 2; k <= MAX_PRIME; k += i) {
isPrime[k] = false;
}
}
}
int k, n;
cin >> k >> n;
for (int i = k; i <= n; ++i) {
if (isPrime[i]) {
primes.push_back(i);
}
}
vector<int> hashes;
for (size_t i = 0; i < primes.size(); ++i) {
int x = primes[i];
int hash;
do {
hash = 0;
for (; x > 0; x /= 10) {
hash += x % 10;
}
x = hash;
} while (hash >= 10);
hashes.push_back(hash);
}
int positions[10];
fill(&positions[0], &positions[9] + 1, -1);
int maxLength = 0;
int maxFirst = 0;
int first = 0;
for (size_t last = 0; last < hashes.size(); ++last) {
int h = hashes[last];
if (positions[h] >= first) {
first = positions[h] + 1;
}
positions[h] = last;
int length = last - first + 1;
if (maxLength <= length) {
maxLength = length;
maxFirst = first;
}
}
cout << primes[maxFirst] << endl;
return 0;
}
data9824