POJ -- 3842

时间:2021-04-29 05:48:33
An Industrial Spy
 

Description

Industrial spying is very common for modern research labs. I am such an industrial spy - don't tell anybody! My recent job was to steal the latest inventions from a famous math research lab. It was hard to obtain some of their results but I got their waste out of a document shredder. 
I have already reconstructed that their research topic is fast factorization. But the remaining paper snippets only have single digits on it and I cannot imagine what they are for. Could it be that those digits form prime numbers? Please help me to find out how many prime numbers can be formed using the given digits.

Input

The first line of the input holds the number of test cases c (1 <= c <= 200). Each test case consists of a single line. This line contains the digits (at least one, at most seven) that are on the paper snippets.

Output

For each test case, print one line containing the number of different primes that can be reconstructed by shuffling the digits. You may ignore digits while reconstructing the primes (e.g., if you get the digits 7 and 1, you can reconstruct three primes 7, 17, and 71). Reconstructed numbers that (regarded as strings) differ just by leading zeros, are considered identical (see the fourth case of the sample input).

Sample Input

4
17
1276543
9999999
011

Sample Output3

1336
0
2
题意:给你一串数字,问你他们能组成多少个不同的素数。 思路:枚举这些数字的组合的排列,打一张素数表直接判断。这题时间限制很紧啊(1000ms),在无限TLE之后,我把memset(vis,0,sizeof(vis))这句删除之后跑了844ms,险过,memset怎么会那么坑,不是说内部实现是调用批处理吗,为什么还那么慢???
另外,打素数表的时候,注意姿势。。。,不然必然超时。
Ps:按照刚进实验室时某大三学长教我的那个写法打素数表时超时,当时他还说两种方法都一样,现在才发现坑了我们。
下面是三个版本的: 1.dfs+二进制枚举优化 (844ms)
 #include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define MAXN 10000000
char isp[MAXN], visp[MAXN], vis[MAXN];
int a[], t[], sum;
void init(){
memset(isp, , sizeof(isp));
isp[] = isp[] = ;
for(int i = ;i*i<MAXN;i ++)
if(!isp[i])
for(int j = i*i;j < MAXN;j += i)
isp[j] = ;
return;
}
int ToNum(int n){
int sum = ;
for(int i = ;i < n;i ++) sum = sum* + t[i];
return sum;
}
void dfs(int n, int dep){
if(dep == n){
int tmp = ToNum(n);
if(!isp[tmp] && !visp[tmp]){
sum++;
visp[tmp] = ;
}
return;
}
for(int i = ;i < n;i ++){
if(!vis[i]){
vis[i] = ;
t[dep] = a[i];
dfs(n, dep+);
vis[i] = ;
}
}
}
int main(){
char str[];
int tt, n, b[];
init();
scanf("%d", &tt);
while(tt--){
memset(str, , sizeof(str));
scanf("%s", str);
int len = strlen(str);
for(int i = ;i < len;i ++) b[i] = str[i]-'';
memset(visp, , sizeof(visp));
int ans = ;
int UP = ( << len);
for(int i = ;i < UP;i ++){
int k = ;
for(int j = ;j < len;j ++)
if(i & ( << j)) a[k++] = b[j];
sum = ;
//memset(vis,0,sizeof(vis); 这句加上就超时。。。
dfs(k, );
ans += sum;
}
printf("%d\n", ans);
}
return ;
}

2. 普通dfs不加优化(969ms。。。)


 #include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define MAXN 10000000
char isp[MAXN], visp[MAXN], vis[MAXN];
int a[], t[], sum;
void init(){
memset(isp, , sizeof(isp));
isp[] = isp[] = ;
for(int i = ;i*i<MAXN;i ++)
if(!isp[i])
for(int j = i*i;j < MAXN;j += i)
isp[j] = ;
return;
}
int ToNum(int n){
int sum = ;
for(int i = ;i < n;i ++) sum = sum* + t[i];
return sum;
}
void dfs(int n, int dep, int cnt){
if(dep == cnt){
int tmp = ToNum(cnt);
if(!isp[tmp] && !visp[tmp]){
sum++;
visp[tmp] = ;
}
}
for(int i = ;i < n;i ++){
if(!vis[i]){
vis[i] = ;
t[dep] = a[i];
dfs(n, dep+, cnt);
vis[i] = ;
}
}
}
int main(){
char str[];
int tt, n;
init();
scanf("%d", &tt);
while(tt--){
memset(str, , sizeof(str));
scanf("%s", str);
int len = strlen(str);
for(int i = ;i < len;i ++) a[i] = str[i]-'';
memset(visp, , sizeof(visp));
int ans = ;
for(int i = ;i <= len;i ++){
//memset(vis, 0, sizeof(vis)); 加上就超时。。。
sum = ;
dfs(len, , i);
ans += sum;
}
printf("%d\n", ans);
}
return ;
}

3.C++ STL库 next_permutation(a,a+k)函数 + 二进制优化(813ms),可以看出并不比dfs快多少,况且没有调用ToNum函数,减少了程序运行时跳转的时间
 #include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define MAXN 10000000
using namespace std;
char isp[MAXN], visp[MAXN], vis[MAXN];
int a[], t[], sum;
void init(){
memset(isp, , sizeof(isp));
isp[] = isp[] = ;
for(int i = ;i*i<MAXN;i ++)
if(!isp[i])
for(int j = i*i;j < MAXN;j += i)
isp[j] = ;
return;
}
int ToNum(int n){
int sum = ;
for(int i = ;i < n;i ++) sum = sum* + a[i];
return sum;
}
void dfs(int n, int dep){
if(dep == n){
int tmp = ToNum(n);
if(!isp[tmp] && !visp[tmp]){
sum++;
visp[tmp] = ;
}
return;
}
for(int i = ;i < n;i ++){
if(!vis[i]){
vis[i] = ;
t[dep] = a[i];
dfs(n, dep+);
vis[i] = ;
}
}
}
int main(){
char str[];
int tt;
init();
scanf("%d", &tt);
while(tt--){
memset(str, , sizeof(str));
scanf("%s", str);
int len = strlen(str);
sort(str, str+len);
memset(visp, , sizeof(visp));
int ans = , UP = ( << len);
for(int i = ;i < UP;i ++){
int k = ;
for(int j = ;j < len;j ++)
if((i >> j) & ) a[k++] = str[j]-'';
sum = ;
//memset(vis,0,sizeof(vis)) 加上同样超时。。。
do{
int tmp = ;
for(int i = ;i < k;i ++) tmp = tmp*+a[i];
if(!isp[tmp] && !visp[tmp]){
sum++;
visp[tmp] = ;
}
}while(next_permutation(a, a+k));
ans += sum;
}
printf("%d\n", ans);
}
return ;
}