結果

問題 No.12 限定された素数
ユーザー koyoprokoyopro
提出日時 2015-09-28 12:37:11
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 208 ms / 5,000 ms
コード長 2,348 bytes
コンパイル時間 745 ms
コンパイル使用メモリ 81,068 KB
実行使用メモリ 8,376 KB
最終ジャッジ日時 2023-08-15 23:07:51
合計ジャッジ時間 7,142 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 197 ms
8,288 KB
testcase_01 AC 201 ms
8,316 KB
testcase_02 AC 197 ms
8,376 KB
testcase_03 AC 207 ms
8,224 KB
testcase_04 AC 198 ms
8,196 KB
testcase_05 AC 203 ms
8,296 KB
testcase_06 AC 201 ms
8,288 KB
testcase_07 AC 205 ms
8,356 KB
testcase_08 AC 200 ms
8,216 KB
testcase_09 AC 197 ms
8,288 KB
testcase_10 AC 200 ms
8,244 KB
testcase_11 AC 206 ms
8,296 KB
testcase_12 AC 207 ms
8,236 KB
testcase_13 AC 203 ms
8,244 KB
testcase_14 AC 201 ms
8,220 KB
testcase_15 AC 203 ms
8,240 KB
testcase_16 AC 208 ms
8,232 KB
testcase_17 AC 199 ms
8,244 KB
testcase_18 AC 196 ms
8,304 KB
testcase_19 AC 195 ms
8,208 KB
testcase_20 AC 198 ms
8,364 KB
testcase_21 AC 199 ms
8,252 KB
testcase_22 AC 196 ms
8,200 KB
testcase_23 AC 194 ms
8,248 KB
testcase_24 AC 196 ms
8,372 KB
testcase_25 AC 198 ms
8,252 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0