結果
| 問題 | No.12 限定された素数 |
| コンテスト | |
| ユーザー |
koyopro
|
| 提出日時 | 2015-09-28 12:37:11 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 194 ms / 5,000 ms |
| コード長 | 2,348 bytes |
| 記録 | |
| コンパイル時間 | 614 ms |
| コンパイル使用メモリ | 86,824 KB |
| 実行使用メモリ | 8,368 KB |
| 最終ジャッジ日時 | 2024-11-24 08:29:47 |
| 合計ジャッジ時間 | 6,260 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include <iostream>
#include <sstream>
#include <math.h>
#include <vector>
#include <array>
#include <algorithm>
#include <queue>
#include <numeric>
#include <map>
#include <set>
#include <bitset>
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define SUM(a) accumulate(ALL(a), 0)
#define array2(type, x, y) array<array<type, y>, x>
#define vector2(type) vector<vector<type> >
#define out(...) printf(__VA_ARGS__)
typedef pair<int, int> pos;
int pos::*x = &pos::first;
int pos::*y = &pos::second;
int dxy[] = {0, 1, 0, -1, 0};
/*================================*/
// 5000000
bool notPrime[5000010];
bool ok[10];
int N;
set<int> separate(int n) {
set<int> s;
while (n > 0) {
s.insert(n % 10);
n /= 10;
}
return s;
}
bool isok(int icnt[10]) {
REP(i, 10) {
if (ok[i] && !icnt[i]) return false;
if (!ok[i] && icnt[i]) return false;
}
return true;
}
bool isover(set<int> s) {
for (int t : s) {
if (!ok[t]) return true;
}
return false;
}
signed main()
{
notPrime[0] = notPrime[1] = true;
FOR(i,2,5000001) {
if (notPrime[i] == false) {
int k = 2;
while (i*k <= 5000000) {
notPrime[i*k] = true;
k++;
}
}
}
cin >> N;
int c;
REP(i,N) {
cin >> c;
ok[c] = true;
}
int maxl = -1;
int icnt[10] = {0};
int i = 1;
int j = 1;
while (j <= 5000000) {
if (!notPrime[j]) {
set<int> s = separate(j);
if (isover(s)) {
if (isok(icnt)) {
maxl = max(maxl, j - i - 1);
}
REP(k,10) icnt[k] = 0;
j++;
i = j;
continue;
} else {
for (auto& t : s) icnt[t]++;
}
}
j++;
}
if (isok(icnt)) {
maxl = max(maxl, j - i - 1);
}
cout << maxl << endl;
return 0;
}
koyopro