結果

問題 No.12 限定された素数
ユーザー yukitfyukitf
提出日時 2020-08-17 18:50:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,788 bytes
コンパイル時間 1,552 ms
コンパイル使用メモリ 171,016 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-19 19:01:34
合計ジャッジ時間 3,848 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 54 ms
6,084 KB
testcase_02 WA -
testcase_03 AC 46 ms
5,956 KB
testcase_04 AC 51 ms
5,832 KB
testcase_05 AC 51 ms
5,956 KB
testcase_06 AC 53 ms
6,080 KB
testcase_07 AC 52 ms
6,084 KB
testcase_08 AC 52 ms
6,084 KB
testcase_09 AC 54 ms
6,084 KB
testcase_10 AC 54 ms
5,960 KB
testcase_11 AC 55 ms
6,088 KB
testcase_12 AC 54 ms
5,960 KB
testcase_13 AC 53 ms
5,956 KB
testcase_14 AC 53 ms
5,980 KB
testcase_15 AC 53 ms
6,080 KB
testcase_16 AC 56 ms
6,084 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 54 ms
6,084 KB
testcase_21 AC 57 ms
5,952 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 53 ms
6,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n)   for(int i = 0; i < (n); i++)
#define REPS(i, n)  for(int i = 1; i <= (n); i++)
#define RREP(i, n)  for(int i = (n)-1; i >= 0; i--)
#define RREPS(i, n) for(int i = (n); i > 0; i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define mt make_tuple
#define pb push_back

using ll  = long long;
using pi  = pair<int, int>;
using pl  = pair<ll, ll>;
using vi  = vector<int>;
using vl  = vector<ll>;
using vs  = vector<string>;
using vb  = vector<bool>;
using vvi = vector<vi>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const ll  INFL = 1e18;
const double PI = 3.141592653589793;
const double EPS = 1e-9;

template<class T> bool chmax(T &a, const T &b) { if(a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if(a > b) { a = b; return true; } return false; }

const int MAXN = 5000000;

signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    int N; cin >> N;
    vb A(10, false); 
    REP(i, N)
    {
        int a; cin >> a;
        A[a] = true;
    }

    //REP(i, 10) if(A[i]) cout << i << endl;

    vb sieve(MAXN+1, true);
    sieve[0] = sieve[1] = false;
    for(int i = 2; i * i <= MAXN; i++)
    {
        if(sieve[i])
        {
            for(int j = i * 2; j <= MAXN; j += i) sieve[j] = false;
        }
    }

    vi prime;
    REP(i, MAXN+1) if(sieve[i]) prime.pb(i);

    //REP(i, (int)prime.size()) cout << prime[i] << " ";
    //cout << endl;

    int n = (int)prime.size();
    vi nums(10, 0);
    int l = 0, r = 0;
    int ans = -1;
    while(l < n && r < n)
    {
        bool p = false;
        bool q = false;
        REP(i, 10)
        {
            if(!A[i] && nums[i] > 0) p = true; // 超過
            if(A[i] && nums[i] == 0) q = true; // 不足
        }

        if(p)
        {
            int x = prime[l];
            while(x > 0)
            {
                nums[x % 10]--;
                x /= 10;
            }
            l++;
        }
        else if(q)
        {
            int x = prime[r];
            while(x > 0)
            {
                nums[x % 10]++;
                x /= 10;
            }
            r++;
        }
        else
        {
            int len = prime[r]-1;
            if(l > 0) len -= (prime[l-1]+1);
            else len--;
            chmax(ans, len);

            int x = prime[r];
            while(x > 0)
            {
                nums[x % 10]++;
                x /= 10;
            }
            r++;
        }
    }
    if(l > 0) chmax(ans, MAXN - prime[l-1] - 1);
    else chmax(ans, MAXN - 1);
    cout << ans << endl;
}
0