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 ;
}

 




 

POJ -- 3842的更多相关文章

  1. POJ - 3842 An Industrial Spy dfs(水)

    题意:给你一串数字,最少一个,最多七个,问用这里面的数字能组成多少素数,不重复. 思路:之前还遍历10000000的每一个素数,结果超时,后来发现直接dfs就可以了,只是标记一下做过的数. #prag ...

  2. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  3. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  4. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  5. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  6. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  7. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  8. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  9. POJ 2752 Seek the Name, Seek the Fame [kmp]

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Ac ...

随机推荐

  1. 当linux遇上多网卡时

    我虚拟机有三个网卡,有两个在用,分别是不同的网段 eth4: 192.168.100.6/24 eth6: 192.168.137.131/24 但是默认系统把我的内部网络eth6设置为所有ip地址需 ...

  2. C# 创建XML文档

    有些时候我们需要生成一个xml文档作为数据交换的容器.当然我们用拼接字符串的方法来进行构建xml,但是这种方法虽然简单有效,但是如果xml文档结构过于复杂,拼接字符串会让人眼花缭乱.这时候就需要C#给 ...

  3. svg学习笔记(一)

    SVG——可扩展适量图形,基于XML PC端:IE9+   wap端:表现良好,适合使用 基础图形: line(线段)  <line x1="25" y1="150 ...

  4. 说说对C语言指针的理解

    指针困扰了一些学习编程的人,或许你的老师会告诉你,指针比较难理解. 我当时被老师的话唬住所以学习指针那章的时候都没心情听课.(说得像讲别的内容时我听了似的,开玩笑) 导致了学习链表的时候各种卧槽. * ...

  5. CentOS搭建OpenVPN服务(简易版)

    OpenVPN服务端配置 1. 安装OpenVPN软件包 默认的Centos软件源里面没有OpenVPN的软件包,我们可以添加rpmforge的repo,从而实现yum安装openvpn 针对Cent ...

  6. [转载]C#实现POST提交方式

    网页自动登录和提交POST信息的核心就是分析网页的源代码(HTML),在C#中,可以用来提取网页HTML的组件比较多,常用的用WebBrowser.WebClient.HttpWebRequest这三 ...

  7. 将cocos2dx项目从VS移植到Eclipse

    本文转自:http://www.cnblogs.com/Z-XML/p/3349518.html 引言:我们使用cocos2d-x引擎制作了一款飞行射击游戏,其中创新性地融入了手势识别功能.但是我们在 ...

  8. Mysql的列索引和多列索引(联合索引)

    转自:http://blog.chinaunix.net/uid-29305839-id-4257512.html 创建一个多列索引:CREATE TABLE test (      id       ...

  9. java区分大小写,使用TAB进行缩进,public类名只能有一个,而且文件名与类名保持一致.

    java的类必须大写 java区分大小写,使用TAB进行缩进,public类名只能有一个,而且文件名与类名保持一致. 在dos用上下箭头,调用已用过的命令

  10. grep简单常用的语法介绍

    说明: grep -n 关键字 查询的文档 ->-n表示打印行号 grep -c 关键字 查询的文档1 查询的文档2 ->-c表示输出匹配行的数目,而不是输出匹配的行. grep -rn ...