您的位置:首页 > 其它

poj 3842 An Industrial Spy 筛法 全排列

2016-08-12 15:38 543 查看
An Industrial Spy
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 1537Accepted: 626
DescriptionIndustrial 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 wasteout 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 outhow many prime numbers can be formed using the given digits.InputThe 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.OutputFor 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 Output
3
1336
0
2
题意:给你一个字符串,用字符串里的数字组合成一个素数,求可组成的素数总数
思路:全排列,判断每个情况是否是素数,判断素数可以筛法打表,也可以打sqrt(10^7)的素数表,再进行素数判断
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<algorithm>using namespace std;#define M 10000000#define LL long longbool visit[M];void iii(){for(int i=0; i<10000000; i++)visit[i]=1;visit[0]=visit[1]=0;for(LL i=2; i<M; ++i){if(visit[i]==true){for(LL  j=i*i; j<M; j+=i){visit[j]=false;}}}}bool jug[10000000];int qw[10000000];char swe[9];int a[9];int main(){iii();int T;int  t,i,d,s,ans;for(i=0; i<10000000; i++)jug[i]=0;scanf("%d",&T);while(T--){scanf("%s",swe);d=strlen(swe);for(i=0; i<d; i++)a[i]=swe[i]-'0';t=ans=0;sort(a,a+d);do{s=0;for(i=0; i<d; i++){s=s*10+a[i];if(jug[s]==0&&visit[s]==1){ans++;jug[s]=-1;qw[t++]=s;}}}while(next_permutation(a,a+d));printf("%d\n",ans);for(i=0; i<t; i++)jug[qw[i]]=0;}return 0;}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数学 数论 筛法 POJ