USACO 1.3-Prime Cryptarithm
/* ID: m1590291 TASK: crypt1 LANG: C++ */ #include <iostream> #include <fstream> #include <string.h> using namespace std; /****************************************************************************************************************** 穷举法进行剪枝, 设置一个标志数组 a[],初值为0,当给定数字时赋值为1 对所有数进行判断进行剪枝即可 ******************************************************************************************************************/ int a[15]; bool fuc(int x) { while(x!=0) { if(a[(x-(x/10)*10)] == 0) return false; x/=10; } return true; } int main() { ifstream fin("crypt1.in"); ofstream fout("crypt1.out"); int T,temp; while(fin>>T) { int num=0; memset(a,0,sizeof(a)); for(int i = 1;i <= T;i ++){ fin>>temp; a[temp]=1; } for(int i = 111;i <= 999;i ++){ for(int j = 11;j <= 99;j ++){ if(fuc(i) && fuc(j)){ //剪枝1 int a=i*(j-(j/10)*10); int b=i*(j/10); if(a >= 111 && a<= 999 && b >= 111 && b <= 999 && fuc(a) && fuc(b)){ //剪枝2 int c=b*10+a; if(fuc(c)) //符合所有条件,计数器 num+1 num++; } } } } fout<<num<<endl; } fin.close(); fout.close(); return 0; }