50min N^5 的枚举 (N<10) 1秒内没有问题。。
要注意的是for循环里的临时变量,不能直接+= 。。debug花了些时间 ,下次注意
/*
ID: zhangw31
PROG: crypt1
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <set>
using namespace std;
int N, answer;
int num[10];
set<int> s;
bool testOK(int n) {
int a = n % 10;
if (s.count(a) == 0) return false;
n /= 10;
a = n % 10;
if (s.count(a) == 0) return false;
n /= 10;
a = n % 10;
if (s.count(a) == 0) return false;
n /= 10;
if (n > 0) return false;
return true;
}
bool testOK2(int n) {
int a = n % 10;
if (s.count(a) == 0) return false;
n /= 10;
a = n % 10;
if (s.count(a) == 0) return false;
n /= 10;
a = n % 10;
if (s.count(a) == 0) return false;
n /= 10;
a = n % 10;
if (s.count(a) == 0) return false;
n /= 10;
if (n > 0) return false;
return true;
}
void allshow( )
{
for (int i = 0; i < N; i++) {
int tmp = num[i] * 100;
for (int j = 0; j < N; j++) {
int tmp2 = tmp + num[j] * 10;
for (int k = 0; k < N; k++) {
int tmp1 = tmp2 + num[k];
for (int p = 0; p < N; p++) {
int testnum1 = num[p] * tmp1;
if (testOK(testnum1)) {
for (int q = 0; q < N; q++) {
int testnum2 = num[q] * tmp1;
if (testOK(testnum2)) {
if (testOK2(testnum2 * 10 + testnum1)) {
answer++;
}
}
}
}
}
}
}
}
}
int main()
{
ifstream fin("crypt1.in");
ofstream fout("crypt1.out");
fin >> N;
for (int i = 0; i < N; i++) fin >> num[i];
for (int i = 0; i < N; i++) s.insert(num[i]);
allshow();
cout << answer << endl;
fout << answer << endl;
fin.close();
fout.close();
}