結果
| 問題 |
No.910 素数部分列
|
| コンテスト | |
| ユーザー |
wunderkammer2
|
| 提出日時 | 2020-03-28 08:04:25 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,588 bytes |
| コンパイル時間 | 1,177 ms |
| コンパイル使用メモリ | 105,832 KB |
| 実行使用メモリ | 17,360 KB |
| 最終ジャッジ日時 | 2025-01-02 10:43:30 |
| 合計ジャッジ時間 | 60,891 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 16 WA * 5 TLE * 29 |
ソースコード
#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;
}
wunderkammer2