範例程式碼 uva12218

//uva12218
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;

int main(){
	// prime table, take really much time here
	static bool prime[9999999+1];
	fill(prime,prime + 9999999+1, true);
	prime[0] = prime[1] = false;
	double bound = sqrt(9999999.0) + 0.5;
	for(int i=2 ; i*i < 9999999+1 ; ++i){
		for(int j=2 ; j*i < 9999999+1 ; ++j){
			prime[i*j] = false;
		}
	}
	
	int n;
	cin >> n;
	while(n--){
		set<int> ansSet;
		string input;
		cin >> input;
		sort(input.begin(),input.end());
		// test bit by bit ( 2^n )
		for(int i=0 ; i < (1 << input.size()) ; ++i){
			string testCase = "";
			for(int j=0 ; j < input.size() ; ++j)
				if(i & (1 << j)) testCase += input[j];
			do{
				int num = atoi(testCase.c_str());
				if(!prime[num]) continue;
				if(ansSet.count(num)) continue;
				ansSet.insert(num);
			}while(next_permutation(testCase.begin(),testCase.end()));
		}
		cout << ansSet.size() << endl;
	}
	
	return 0;
}