結果
| 問題 |
No.12 限定された素数
|
| コンテスト | |
| ユーザー |
opqopq_
|
| 提出日時 | 2015-05-13 15:07:39 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 45 ms / 5,000 ms |
| コード長 | 1,355 bytes |
| コンパイル時間 | 358 ms |
| コンパイル使用メモリ | 43,904 KB |
| 実行使用メモリ | 11,996 KB |
| 最終ジャッジ日時 | 2024-11-24 08:18:13 |
| 合計ジャッジ時間 | 2,244 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:46:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
46 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
main.cpp:47:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
47 | for (int i = 0; i < N; i++) scanf("%d", A + i);
| ~~~~~^~~~~~~~~~~~~
ソースコード
#include <cstdio>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
#define MIN 1
#define MAX 5000000
#define N_MAX 10
int N;
int A[N_MAX];
bool isnotprime[MAX + 1];
vector<pair<int, int>> prime;
void make_prime() {
isnotprime[0] = isnotprime[1] = 1;
for (int i = 2; i * i <= MAX; i++) {
if (!isnotprime[i]) {
for (int j = i * i; j <= MAX; j += i) {
isnotprime[j] = 1;
}
}
}
prime.emplace_back(MIN - 1, 0);
for (int i = 2; i <= MAX; i++) {
if (isnotprime[i]) continue;
prime.emplace_back(i, 0);
}
prime.emplace_back(MAX + 1, 0);
}
void make_mask() {
for (int i = 1; i + 1 < prime.size(); i++) {
int x = prime[i].first;
int mask = 0;
while (x) {
mask |= 1 << x % 10;
x /= 10;
}
prime[i].second = mask;
}
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) scanf("%d", A + i);
if (N == 10) {
printf("%d\n", MAX - MIN);
return 0;
}
int mask = 0;
for (int i = 0; i < N; i++) mask |= 1 << A[i];
make_prime();
make_mask();
int res = -1;
for (int i = 1; i + 1 < prime.size(); i++) {
int bit = 0;
for (int j = i; j + 1 < prime.size(); j++) {
bit |= prime[j].second;
if ((bit | mask) != mask) break;
if (bit == mask) {
res = max(res, (prime[j + 1].first - 1) - (prime[i - 1].first + 1));
}
}
}
printf("%d\n", res);
return 0;
}
opqopq_