您的位置:首页 > 其它

南邮 OJ 1128 An Industrial Spy

2015-08-05 09:14 344 查看


An Industrial Spy

时间限制(普通/Java) : 10000 MS/ 30000 MS          运行内存限制 : 65536 KByte
总提交 : 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 10000000
char 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息