結果

問題 No.910 素数部分列
ユーザー wunderkammer2wunderkammer2
提出日時 2020-03-28 08:04:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,588 bytes
コンパイル時間 1,413 ms
コンパイル使用メモリ 108,664 KB
実行使用メモリ 13,888 KB
最終ジャッジ日時 2024-06-10 17:05:05
合計ジャッジ時間 4,380 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,888 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 3 ms
6,948 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 WA -
testcase_14 AC 2 ms
6,940 KB
testcase_15 WA -
testcase_16 AC 2 ms
6,944 KB
testcase_17 WA -
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 WA -
testcase_21 AC 2 ms
6,944 KB
testcase_22 WA -
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<string>
#include<utility>
#include<vector>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
#define rep(i,n) for(int i=0;i<n;i++)
#define repl(i,s,e) for(int i=s;i<e;i++)
#define reple(i,s,e) for(int i=s;i<=e;i++)
#define revrep(i,n) for(int i=n-1;i>=0;i--)
#define all(x) (x).begin(),(x).end()

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



vector<string> sieve(size_t max)
{
    vector<bool> IsPrime;

    if (max + 1 > IsPrime.size())
    {
        IsPrime.resize(max + 1, true);
    }
    IsPrime[0] = false; // 0は素数ではない
    IsPrime[1] = false; // 1は素数ではない

    vector<string> primes;

    for (size_t i = 2; i * i <= max; ++i)
    {
        if (IsPrime[i]) // iが素数ならば
        {
            //偶数を持つ場合は除外
            string s = to_string(i);
            bool flg = true;
            for (auto c : s)
            {
                if ((c - '0') % 2 == 0)
                {
                    flg = false;
                    break;
                }
            }

            //11はあとから追加
            if (s == "11") flg = false;
            if(flg)primes.push_back(s);

            for (size_t j = 2; i * j <= max; ++j) // (max以下の)iの倍数は
                IsPrime[i * j] = false;      // 素数ではない
        }
    }

    primes.push_back("11");

    return primes;
}

bool canErase(string strP, string S, vector<bool>& used)
{
    vector<int> pos;
    
    //Sの未使用部分がpを部分列として含むことを確認
    int i = 0, j = 0;
    for (; i < strP.size() && j < S.size();)
    {
        for (; j < S.size(); j++)
        {
            if (S[j] == strP[i] && !used[j])
            {
                pos.push_back(j);
                i++;
                j++;
                break;
            }
        }
    }

    if (pos.size() < strP.size()) return false;

    for (int x : pos) used[x] = true;

    return true;
}

int main()
{	
	int N;
	string S;

	cin >> N >> S;

    auto primes = sieve(100000);

    ll count = 0;

    vector<bool> used(N, false);

    for (auto p : primes)
    {
        while(canErase(p, S, used))count++;
    }

    cout << count << endl;

	return 0;
}
0