poj 3842 An Industrial Spy(dfs+乱搞)

来源:互联网 发布:侵犯网络隐私权的案例 编辑:程序博客网 时间:2024/06/14 05:15

An Industrial Spy

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1733 Accepted: 684
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


给一个数,给数字组合成不同的数,例(011->0,1,10,11,101,110),求有多少个是素数。


超了好几次,刚开始直接筛素数+dfs暴力,TLE,之后dfs有bug,筛素数优化弄了几个版本,最后主要还是map的问题。


#include <iostream>#include <cstdio>#include <map>#include <string>#include <cstring>#include <algorithm>using namespace std;#define N 10#define pm 11111111//map<int,bool>mp;int n, m, sum, len;bool vis[10], prim[4111], mp[pm];int a[10], prm[2111];//void prime()//{//    memset( prim, true, sizeof( prim ));//    prim[0] = prim[1] = false;//    int cnt = 0;//    for( int i = 2 ; i < pm ; i ++ ){//        if( prim[i] ){//            prm[cnt++] = i;//            if( i * i <= pm ){//                for( int j = i*i ; j < pm ; j += i ){//                    prim[j] = false;//                }//            }//            else break;//        }//    }//}int cnt = 0;void prime(){    memset( prim, true, sizeof( prim ));    for( int i = 2 ; i < 4111 ; i ++ ){///        if( prim[i] ){            prm[cnt++] = i;            for( int j = i+i ; j <= 4111 ; j += i ){///这也是因为10^7 < x^2(可以取x=4111),另一个筛素数的是用j=i*i,由此得                prim[j] = false;            }        }    }}///没用上inline int antry( int x ){//cout<<"_"<<x<<"_"<<endl;    if(x == 0 || x == 1) return 0;    for(int i = 0 ; i < cnt && prm[i] < x ; i ++ ){        if( x % prm[i] == 0 )            return 0;    }    return 1;}void dfs( int x, int y ){    if( !mp[x] ){        int flg = 0;        mp[x] = true;        if(x == 0 || x == 1) flg = 1;        else{            for(int i = 0 ; i < cnt && prm[i] < x ; i ++ ){                if( x % prm[i] == 0 ){                    flg = 1;break;                }            }        }        if( !flg ) sum ++;    }//    if( mp[x] != true ){//!use[x]//        mp[x] = true;//        if( antry(x) ) sum ++;//    }    if( y == len ) return;    for( int i = 0 ; i < len ; i ++ ){        if( !vis[i] ){            vis[i] = true;            int s = x * 10 + a[i];            dfs( s, y+1 );            vis[i] = false;        }    }}int main(){    freopen( "in.txt", "r", stdin );    prime();    //for( int i = 0 ; i < 5111 ; i ++ ) if( prm[i] > 10000 ){cout<<i<<" "<<prm[i]<<endl;break;} //cout<<prm[i]<<" ";    scanf( "%d", &n );//    string m;    while( n -- ){//        cin >> m;//        len = m.length();        char m[N];        getchar();        scanf( "%s", m );        len = strlen( m );        memset( a, 0, sizeof( a ));        for( int i = 0 ; i < len ; i ++ ){            a[i] = m[i]-'0';        }        memset( vis, false, sizeof( vis ));        memset( mp, false, sizeof( mp ));        sum = 0;        //mp.clear();        dfs( 0, 0 );        cout<<sum<<endl;        //printf( "%s\n", m );    }    return 0;}

错的dfs(011)


//void dfs( int x, int y )//{//    //!use[x]//    ///if( prim[x] ) sum ++;//    if( antry( x ) ) sum ++;//    if( y == len ) return;//    for( int i = 0 ; i < len ; i ++ ){//cout<<'='<<i<<'=';//        if( !vis[i] ){//            vis[i] = true;//            int s = x * 10 + a[i];//cout<<'/'<<x<<'#'<<s<<" ";//            if( !mp[s] ){/////                mp[s] = true;//                dfs( s, y+1 );//            }//            vis[i] = false;//        }//    }//}
原创粉丝点击