poj 3842 An Industrial Spy 筛法 全排列
2016-08-12 15:38
543 查看
An Industrial Spy
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
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1537 | Accepted: 626 |
4 17 1276543 9999999 011Sample 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_3842_An Industrial Spy_筛法、素数表
- poj 3842 An Industrial Spy
- LA 4609 ,poj 3842 An Industrial Spy
- LA 4609 ,poj 3842 An Industrial Spy
- HUST team contest #2 A An Industrial Spy ||poj 3842 (筛)
- LA 4609 ,poj 3842 An Industrial Spy
- POJ 3842 An Industrial Spy
- POJ 3842 An Industrial Spy 快筛质数+STL乱搞
- poj 3842 全排列+筛素数+暴力
- 南邮 OJ 1128 An Industrial Spy
- poj1731(dfs实现全排列)
- poj 2826 An Easy Problem?!
- POJ 1152 An Easy Problem!(进位制)
- POJ 3126 Prime Path(筛法,双向搜索)
- POJ-3292-Semi-prime H-numbers-数筛法
- POJ 1738 An old Stone Game 笔记
- POJ 2689 Prime Distance【大区间素数筛选】【埃氏筛法】【经典题】
- POJ2200+全排列模拟
- poj 3358 Period of an Infinite Binary Expansion
- poj Backward Digit Sums 3187 (全排列)