結果
| 問題 |
No.6 使いものにならないハッシュ
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-07-14 14:11:09 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 1,615 bytes |
| コンパイル時間 | 788 ms |
| コンパイル使用メモリ | 77,928 KB |
| 実行使用メモリ | 10,148 KB |
| 最終ジャッジ日時 | 2024-10-09 20:14:52 |
| 合計ジャッジ時間 | 8,807 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | RE * 10 TLE * 1 -- * 21 |
コンパイルメッセージ
main.cpp: In function 'int to_hash(int)':
main.cpp:27:13: warning: control reaches end of non-void function [-Wreturn-type]
27 | tmp += to_hash(n / 10);
| ~~~~^~~~~~~~~~~~~~~~~~
ソースコード
#include <iostream>
#include <string.h>
#include <math.h>
#include <vector>
#include <set>
#define FOR(i,a,b) for(int i=(a); i<(b); i++)
using namespace std;
// 素数かどうか調べる
int if_prime(int n){
if (n == 1) return 0;
int root_n = sqrt(n);
FOR(i, 2, root_n + 1){
if (n % i == 0) return 0;
}
return 1;
}
// hash値を調べる
int to_hash(int n){
int tmp = 0;
if (n < 10){
tmp += n;
return tmp;
} else {
tmp += n % 10;
tmp += to_hash(n / 10);
}
}
int main(){
int k, n;
cin >> k >> n;
// 素数のvector
vector<int> primes;
FOR(i, k, n + 1){
if (if_prime(i)) primes.push_back(i);
}
// 素数のhash値のvector
vector<int> prime_hash;
FOR(i, 0, primes.size()){
prime_hash.push_back(to_hash(primes[i]));
}
// 最長部分を求める
int max_length = 0;
int max_start = -1;
// 最長部分を求める
int dp[10];
memset(dp, 0, sizeof(dp));
// 内側のループを抜けた状態では[start, end)がハッシュ値がすべて異なる最長区間
int start = 0;
int end = 0;
while (start < primes.size()){
while (end < primes.size()) {
if (dp[prime_hash[end]] == 0) {
dp[prime_hash[end]] = 1;
end += 1;
} else {
break;
}
}
if (end - start >= max_length){
max_start = primes[start];
max_length = end - start;
}
dp[start] = 0;
}
cout << max_start << endl;
return 0;
}