poj 3842 An Industrial Spy 筛法 全排列


An Industrial Spy
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1537 Accepted: 626

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 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 long
bool 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;
}


 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值