南邮 OJ 1128 An Industrial Spy
来源:互联网 发布:图像扫描列数据 编辑:程序博客网 时间:2024/06/09 21:34
An Industrial Spy
总提交 : 38 测试通过 : 20
比赛描述
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 nd out how many prime numbers can be formed using the given digits.
输入
The rst 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.
输出
For each test case, print one line containing the number of di erent primes that can be reconstructed by shuing 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) di er just by leading zeros, are considered identical (see the fourth case of the sample input).
样例输入
4
17
1276543
9999999
011
样例输出
3
1336
0
2
题目来源
NWERC2009
#include<cstdio>#include<string>#include<cstring>#include<algorithm>#define MAXN 10000000char isp[MAXN], visp[MAXN], vis[MAXN];int a[10], t[10], sum;void init(){int i,j;isp[0]=isp[1]=1;isp[2]=isp[3]=0;for(i=4;i<MAXN;){isp[i++]=1;isp[i++]=0;//最大i==N-1,为奇数,不会越界访问}int halfN = MAXN>>1,doubleI;for(i=3;i<=halfN;i+=2){if(!isp[i]){doubleI = i<<1;for(j=i*3;j<MAXN;j+=doubleI){isp[j]=1;}}}}int ToNum(int n){ int sum = 0; for(int i = 0;i < n;i ++) sum = sum*10 + 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] = 1; } return; } for(int i = 0;i < n;i ++){ if(!vis[i]){ vis[i] = 1; t[dep] = a[i]; dfs(n, dep+1); vis[i] = 0; } }}int main(){ char str[10]; int tt, b[10]; init(); scanf("%d", &tt); while(tt--){ memset(str, 0, sizeof(str)); scanf("%s", str); int len = strlen(str); for(int i = 0;i < len;i ++) b[i] = str[i]-'0'; memset(visp, 0, sizeof(visp)); int ans = 0; int UP = (1 << len); for(int i = 1;i < UP;i ++){ int k = 0; for(int j = 0;j < len;j ++) if(i & (1 << j)) a[k++] = b[j]; sum = 0; //memset(vis,0,sizeof(vis); 这句加上就超时。。。 dfs(k, 0); ans += sum; } printf("%d\n", ans); } return 0;}
- 南邮 OJ 1128 An Industrial Spy
- POJ 3842 An Industrial Spy
- LA 4609 ,poj 3842 An Industrial Spy
- POJ - 3842 An Industrial Spy dfs(水)
- poj 3842 -- An Industrial Spy ( 全排列 )
- POJ 3842 An Industrial Spy 快筛质数+STL乱搞
- poj 3842 An Industrial Spy 筛法 全排列
- poj 3842 An Industrial Spy(dfs+乱搞)
- 《An Industrial-Strength Audio Search Algorithm》译文
- spy++
- spy++
- Spy++
- SPY
- SPY
- SPY
- SPY
- Spy
- ROS-Industrial
- hulu面试经验
- 南邮 OJ 1127 Stick
- 函数闭包问题的学习
- Groovy
- linux下fastboot烧录的方法
- 南邮 OJ 1128 An Industrial Spy
- 在ubuntu12.04中安装wine和sourceinsight
- c++中堆、栈内存分配
- proguard 提示Warning: can't find referenced field/method '...' in library class ...等问题
- /sys/class/gpio 文件接口操作IO端口(s3c2440)
- 记录日志的工具类
- iOS开发_常用的正则表达式
- No system images installed for this target
- 当设置RESULT_CACHE_MAX_SIZE参数并且重启过database后,Query Result Cache 还是被禁用的。