結果

問題 No.12 限定された素数
ユーザー h_nosonh_noson
提出日時 2016-06-17 12:07:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 60 ms / 5,000 ms
コード長 1,680 bytes
コンパイル時間 1,238 ms
コンパイル使用メモリ 66,600 KB
実行使用メモリ 8,516 KB
最終ジャッジ日時 2023-08-15 23:30:03
合計ジャッジ時間 3,699 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
8,300 KB
testcase_01 AC 53 ms
8,516 KB
testcase_02 AC 52 ms
8,184 KB
testcase_03 AC 39 ms
8,248 KB
testcase_04 AC 54 ms
8,260 KB
testcase_05 AC 56 ms
8,248 KB
testcase_06 AC 56 ms
8,296 KB
testcase_07 AC 53 ms
8,244 KB
testcase_08 AC 56 ms
8,196 KB
testcase_09 AC 55 ms
8,376 KB
testcase_10 AC 55 ms
8,236 KB
testcase_11 AC 60 ms
8,248 KB
testcase_12 AC 54 ms
8,248 KB
testcase_13 AC 56 ms
8,200 KB
testcase_14 AC 56 ms
8,376 KB
testcase_15 AC 56 ms
8,364 KB
testcase_16 AC 59 ms
8,244 KB
testcase_17 AC 57 ms
8,180 KB
testcase_18 AC 54 ms
8,260 KB
testcase_19 AC 52 ms
8,240 KB
testcase_20 AC 54 ms
8,308 KB
testcase_21 AC 54 ms
8,372 KB
testcase_22 AC 56 ms
8,240 KB
testcase_23 AC 54 ms
8,248 KB
testcase_24 AC 60 ms
8,240 KB
testcase_25 AC 56 ms
8,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

#define REP(i,s,e) for (i = s; i <= e; i++)
#define rep(i,n) REP (i,0,(int)(n)-1)
#define RREP(i,s,e) for (i = s; i >= e; i--)
#define rrep(i,n) RREP (i,(int)(n)-1,0)
#define INF (int)1e8
#define MOD (int)(1e9+7)

typedef long long ll;

bool prime[5000001];

int main(void) {
    int i, j;
    for (i = 2; i <= 5000000; i++) prime[i] = true;
    for (i = 2; i * i <= 5000000; i++) if (prime[i]) {
        for (j = i*2; j <= 5000000; j+=i)
            prime[j] = false;
    }

    int n;
    int req = 0;
    cin >> n;
    rep (i,n) {
        int a;
        cin >> a;
        req |= 1<<a;
    }

    int nums[10] {};
    int l, r;
    int ans = -1;
    l = 1; r = 2;
    while (r <= 5000000) {
        int x = r;
        while (x) {
            nums[x%10]++;
            x /= 10;
        }
        do {
            r++;
        } while (!prime[r] && r <= 5000000);
        r--;
        while (l <= r) {
            bool ok = true;
            rep (i,10) {
                if ((req & 1<<i) && nums[i] == 0)
                    break;
                else if ((req & 1 << i) == 0 && nums[i] > 0)
                    ok = false;
            }
            if (i < 10) break;
            if (!ok) {
                while (!prime[l] && l <= 5000001) l++;
                int x = l;
                while (x) {
                    nums[x%10]--;
                    x /= 10;
                }
                l++;
            }
            else {
                ans = max(r - l, ans);
                break;
            }
        }
        r++;
    }
    cout << ans << endl;
    return 0;
}
0