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. jquery checkbox 选中 全选 插件

    checkbox  选中 全选 在项目中经常用到,但是不同的程序员写出的东西各有差异,在此整合了jquery checkbox插件,用起来很方便,也总结了我们项目中通常会出现问题的地方,一行代码搞定. ...

  2. jquery鼠标样式

    浏览器是有自带的鼠标样式的,如果某些时候为了保持鼠标样式的统一,或者想指定特定的鼠标样式该怎么办呢?那就要使用自定义了,下面有个不错的示例,喜欢的朋友可以参考下   1.浏览器自带的鼠标样式:  2. ...

  3. MEF学习笔记

    之前公司里用到了一个叫MEF的东西,说来惭愧一直只管写代码却不曾理解MEF框架为何物,今天就来学习一下,这是一篇迟到了不知多久的博客. -------------------------------- ...

  4. PHP — 用PHP实现一个双向队列

    1.简介 deque,全名double-ended queue,是一种具有队列和栈的性质的数据结构.双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行.双向队列(双端队列)就像是一个队 ...

  5. 免费使用的图表控件XML/SWF Charts 5.08

    免费使用的图表控件XML/SWF Charts 5.08 http://www.pin5i.com/showtopic-26053.html 10个免费的在线统计图表工具 http://paranim ...

  6. Python设计模式——设计原则

    1.单一职责原则:每个类都只有一个职责,修改一个类的理由只有一个 2.开放-封闭远程(OCP):开放是指可拓展性好,封闭是指一旦一个类写好了,就尽量不要修改里面的代码,通过拓展(继承,重写等)来使旧的 ...

  7. Sublime Text 3插件之Emmet:HTML/CSS代码快速编写神器

    一.快速编写HTML代码 1.  初始化 HTML文档需要包含一些固定的标签,比如<html>.<head>.<body>等,现在你只需要1秒钟就可以输入这些标签. ...

  8. Network boot from AMD Am79C970A

    用虚拟机安装winxp系统,出现错误信息如下: Network boot from AMD Am79C970A Copright (c)2003-2005 Vmware, Inc. Copright ...

  9. 微信开发之开发环境搭建( visual studio 2015we + IIS express + ngrok)

    1. 申请个人测试使用的微信订阅号 https://mp.weixin.qq.com 可注册微信订阅号. 不会?请自行百度. 2. 安装 ngrok 微信开发首先要解决如何让微信链接到本地开发环境.有 ...

  10. Xcode 调试技巧-b

    随着Xcode 5的发布,LLDB调试器已经取代了GDB,成为了Xcode工程中默认的调试器.它与LLVM编译器一起,带给我们更丰富的流程控制和数据检测的调试功能.LLDB为Xcode提供了底层调试环 ...