結果

問題 No.12 限定された素数
ユーザー なおなお
提出日時 2014-10-06 23:56:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 165 ms / 5,000 ms
コード長 2,673 bytes
コンパイル時間 683 ms
コンパイル使用メモリ 70,528 KB
実行使用メモリ 8,584 KB
最終ジャッジ日時 2023-08-15 22:26:50
合計ジャッジ時間 2,989 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,304 KB
testcase_01 AC 34 ms
8,296 KB
testcase_02 AC 22 ms
8,244 KB
testcase_03 AC 165 ms
8,256 KB
testcase_04 AC 26 ms
8,248 KB
testcase_05 AC 51 ms
8,244 KB
testcase_06 AC 97 ms
8,252 KB
testcase_07 AC 107 ms
8,312 KB
testcase_08 AC 40 ms
8,292 KB
testcase_09 AC 41 ms
8,296 KB
testcase_10 AC 35 ms
8,584 KB
testcase_11 AC 148 ms
8,360 KB
testcase_12 AC 105 ms
8,248 KB
testcase_13 AC 79 ms
8,244 KB
testcase_14 AC 42 ms
8,300 KB
testcase_15 AC 85 ms
8,360 KB
testcase_16 AC 124 ms
8,308 KB
testcase_17 AC 22 ms
8,428 KB
testcase_18 AC 18 ms
8,292 KB
testcase_19 AC 18 ms
8,292 KB
testcase_20 AC 23 ms
8,324 KB
testcase_21 AC 34 ms
8,256 KB
testcase_22 AC 23 ms
8,236 KB
testcase_23 AC 22 ms
8,436 KB
testcase_24 AC 21 ms
8,236 KB
testcase_25 AC 49 ms
8,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cmath>
#include <set>
using namespace std;
#define REP(i, n)           for(int(i)=0;(i)<(n);++(i))

const int LIM = 5000000;

bool isprime[LIM+1];
void  sieve_of_atkin(int N) {
    int n;
    int sqrtN = (int)sqrt((double)N);
    for(int z = 1; z <= 5; z += 4){
        for(int y = z; y <= sqrtN; y += 6){
            for(int x = 1; x <= sqrtN && (n = 4*x*x+y*y) <= N; ++x)      isprime[n] = !isprime[n];
            for(int x = y+1; x <= sqrtN && (n = 3*x*x-y*y) <= N; x += 2) isprime[n] = !isprime[n];
        }
    }
    for(int z = 2; z <= 4; z += 2){
        for(int y = z; y <= sqrtN; y += 6){
            for(int x = 1; x <= sqrtN && (n = 3*x*x+y*y) <= N; x += 2)   isprime[n] = !isprime[n];
            for(int x = y+1; x <= sqrtN && (n = 3*x*x-y*y) <= N; x += 2) isprime[n] = !isprime[n];
        }
    }
    for(int y = 3; y <= sqrtN; y += 6){
        for(int z = 1; z <= 2; ++z){
            for(int x = z; x <= sqrtN && (n = 4*x*x+y*y) <= N; x += 3)   isprime[n] = !isprime[n];
        }
    }
    for(int n = 5; n <= sqrtN; ++n)
        if(isprime[n]) for(int k = n*n; k <= N; k+=n*n) isprime[k] = false;
    isprime[2] = isprime[3] = true;
}

int main(){
    int N, n;
    set<int> setN;
    cin >> N;
    REP(i,N){
        cin >> n;
        setN.insert(n);
    }
    sieve_of_atkin(LIM);
    set<int> setK;
    int low = 1;
    int maxv = -1;
    for(int i = 1; i <= LIM; i++){
        if(!isprime[i]) continue;
        int j = i;
        bool flag = true;
        set<int> add;
        while(j){
            if(setN.count(j%10)){
                add.insert(j%10);
                j /= 10;
            } else {
                flag = false;
                break;
            }
        }
        if(!flag){
            if(low && setK.size() == N){
                maxv = max(maxv, (i-1)-low);
#ifdef _DEBUG
                if(maxv == (i-1)-low){
                    cout << low << ":" << i-1 << ":" << ((i-1)-low) << endl;
                    for(int j = low; j <= i-1; j++){
                        if(isprime[j]) cout << j << " ";
                    }
                    cout << endl;
                }
#endif
            }
            low = i+1;
            setK.clear();
        } else {
            setK.insert(add.begin(), add.end());
        }
    }
    if(setK.size() == N){
        maxv = max(maxv, LIM-low);
#ifdef _DEBUG
        if(maxv == (LIM-low)){
        	cout << low << ":" << LIM << ":" << (LIM-low) << endl;
            for(int j = low; j <= LIM; j++){
                if(isprime[j]) cout << j << " ";
            }
            cout << endl;
        }
#endif
    }
    cout << maxv << endl;
    return 0;
}
0