10年福州赛区的题目,暴力搜索即可。
#include <iostream> #include <string> #include <cstring> #include <cstdio> #include <algorithm> #include <memory> #include <cmath> #include <bitset> #include <queue> #include <vector> #include <stack> using namespace std; #define CLR(x,y) memset(x,y,sizeof(x)) #define MIN(m,v) (m)<(v)?(m):(v) #define MAX(m,v) (m)>(v)?(m):(v) #define ABS(x) ((x)>0?(x):-(x)) #define rep(i,x,y) for(i=x;i<y;++i) char s1[10],s2[10],s3[10]; char dir[10]; bool bd[300]; int bk[300]; bool vb[100]; long long a,b,c; int len1,len2,len3; int ans; int kk; int init() { return 0; } int ai( char* str, int len) { int i,tmp = 0; if( bk[str[0]] == 0 && len > 1) return -1; rep(i,0,len) tmp = tmp * 10 + bk[str[i]]; return tmp; } int dfs(int k) { if(k == kk) { a = ai(s1,len1); b = ai(s2,len2); c = ai(s3,len3); if( a != -1 && b != -1 && c != -1) { //printf("[%lld %lld %lld]",a,b,c); if( a + b - c == 0) ++ans; if( a - b - c == 0) ++ans; if( a * b - c == 0) ++ans; if( a - b * c == 0 && b != 0) ++ans; } return 0; } for( int i = 0; i < 10; ++i) { if( vb[i] ) continue; bk[dir[k]] = i; vb[i] = true; dfs(k+1); vb[i] = false; } return 0; } int work() { int cnt; int i; scanf("%d",&cnt); while( cnt -- ){ CLR(vb,0); CLR(bd,0); cin>>s1>>s2>>s3; len1 = strlen(s1); rep(i,0,len1) bd[s1[i]] = true; len2 = strlen(s2); rep(i,0,len2) bd[s2[i]] = true; len3 = strlen(s3); rep(i,0,len3) bd[s3[i]] = true; kk = 0; if( bd['A'] ) { dir[kk] = 'A'; ++kk; } if ( bd['B']) { dir[kk] = 'B'; ++kk; } if ( bd['C']) { dir[kk] = 'C'; ++kk; } if ( bd['D']) { dir[kk] = 'D'; ++kk; } if ( bd['E']) { dir[kk] = 'E'; ++kk; } ans = 0; dfs(0); printf("%d\n",ans); } return 0; } int main() { init(); work(); return 0; }