結果

問題 No.12 限定された素数
ユーザー ふーらくたるふーらくたる
提出日時 2016-07-28 23:10:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 64 ms / 5,000 ms
コード長 1,552 bytes
コンパイル時間 679 ms
コンパイル使用メモリ 60,212 KB
実行使用メモリ 8,440 KB
最終ジャッジ日時 2023-08-15 23:31:19
合計ジャッジ時間 3,406 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
8,364 KB
testcase_01 AC 60 ms
8,320 KB
testcase_02 AC 59 ms
8,324 KB
testcase_03 AC 62 ms
8,364 KB
testcase_04 AC 57 ms
8,320 KB
testcase_05 AC 60 ms
8,328 KB
testcase_06 AC 59 ms
8,380 KB
testcase_07 AC 63 ms
8,324 KB
testcase_08 AC 59 ms
8,324 KB
testcase_09 AC 62 ms
8,328 KB
testcase_10 AC 62 ms
8,376 KB
testcase_11 AC 64 ms
8,320 KB
testcase_12 AC 61 ms
8,364 KB
testcase_13 AC 63 ms
8,312 KB
testcase_14 AC 62 ms
8,376 KB
testcase_15 AC 60 ms
8,312 KB
testcase_16 AC 63 ms
8,372 KB
testcase_17 AC 63 ms
8,328 KB
testcase_18 AC 60 ms
8,368 KB
testcase_19 AC 61 ms
8,320 KB
testcase_20 AC 59 ms
8,328 KB
testcase_21 AC 60 ms
8,316 KB
testcase_22 AC 62 ms
8,312 KB
testcase_23 AC 62 ms
8,440 KB
testcase_24 AC 62 ms
8,360 KB
testcase_25 AC 62 ms
8,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
using namespace std;

const int kMaxNumber = 5000000;

bool prime_table[kMaxNumber + 1];
bool should_use[10];

int main() {
    // Eratosthenesの篩
    fill(prime_table, prime_table + kMaxNumber + 1, true);
    prime_table[0] = prime_table[1] = false;
    
    for (int n = 2; n * n <= kMaxNumber; n++) {
        if (prime_table[n]) {
            for (int i = 2; i * n <= kMaxNumber; i++) {
                prime_table[i * n] = false;
            }
        }
    }

    int N;
    cin >> N;
    for (int i = 0; i < N; i++) {
        int A;
        cin >> A;
        should_use[A] = true;
    }

    int s = 1, ans = -1;
    while (s <= kMaxNumber) {
        int t = s;
        bool used[10] = {false};

        while (t <= kMaxNumber) {
            if (prime_table[t]) {
                // 使ってもいい素数かどうか
                bool ok = true;
                string str = to_string(t);
                for (int i = 0; i < str.size(); i++) {
                    ok = ok && should_use[str[i] - '0'];
                }
                if (!ok) break;

                for (int i = 0; i < str.size(); i++) {
                    used[str[i] - '0'] = true;
                }
            }
            t++;
        }
        bool valid = true;
        for (int d = 0; d < 10; d++) {
            valid = valid && (!should_use[d] || used[d]);
        }

        if (valid) ans = max(ans, t - s - 1);
        s = t + 1;
    }

    cout << ans << endl;

    return 0;
}
0