暴搜+剪枝,在打素数表的时候,要注意一下题目的空间范围是500MB(超大...),可能可以打1e8以内的素数表,打表用的是线性筛,空间是\(\frac{4 * 10^8 * 2}{1024^2}\approx 763\ MB\),然而还是过了
另外还有一个奇坑无比的坑点:1不是素数...
#include<iostream>
#include<string>
#include<vector>
using namespace std;
const int N = 1e8;
vector<int> v;
int cnt;
int st[N + 1], primes[N + 1];
int check(int a){
int y = a / 10000;
int m = (a % 10000) / 100;
int d = a % 100;
if(d == 0) return 0;
if(m == 4 || m == 6 || m == 9 || m == 11) return d <= 30;
else if(m == 2) return d <= 28 + (y % 400 == 0 || (y % 4 == 0 && y % 100));
else return d <= 31;
}
void dfs(string &s, int u){
if(u == 4){
int y = 0;
for(int i = 0; i < 4; i ++) y = y * 10 + v[i];
if(!y) return;
}
if(u == 6){
int m = v[4] * 10 + v[5];
if(m == 0 || m > 12) return;
}
if(u == 8){
int a = 0;
for(int i = 0; i < 8; i ++) a = a * 10 + v[i];
if(!st[a] && !st[a % 10000] && !st[a % 100] && check(a)) cnt ++;
return;
}
if(isdigit(s[u])){
v.push_back(s[u] - ‘0‘);
dfs(s, u + 1);
v.pop_back();
}else{
for(int i = 0; i <= 9; i ++){
v.push_back(i);
dfs(s, u + 1);
v.pop_back();
}
}
}
int main(){
int k = 0;
st[1] = 1;
for(int i = 2; i <= N; i ++){
if(!st[i]) primes[k ++] = i;
for(int j = 0; primes[j] <= N / i; j ++){
st[primes[j] * i] = 1;
if(i % primes[j] == 0) break;
}
}
int t;
cin >> t;
while(t --){
string s;
cin >> s;
cnt = 0;
dfs(s, 0);
cout << cnt << endl;
}
return 0;
}