poj 3842 An Industrial Spy(dfs+亂搞)

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;
//        }
//    }
//}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章