题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3419
思路:注意减枝就行,不然会TLE
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> const int inf = 0x3f3f3f3f;//1061109567 typedef long long ll; const int maxn = 100010; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; int visit[10]; int a[10]; int sum; int total = 0; int one,two,three; void dfs(int cur) { if(cur == sum) { int sum1 = 0,sum2 = 0,sum3 = 0; for(int i=0; i<one; i++) sum1 = sum1 * 10 + a[i]; for(int i=one; i<one + two ;i++) sum2 = sum2 * 10 + a[i]; for(int i=one + two; i<one + two + three; i++) sum3 = sum3 * 10 + a[i]; if(sum1 * sum2 == sum3) total++; return; } for(int i=1; i<=9; i++) { if(!visit[i]) { visit[i] = 1; a[cur] = i; dfs(cur+1); visit[i] = 0; } } } int main() { while(scanf("%d%d%d",&one,&two,&three)) { sum = one + two + three; if(sum == 0) break; if(three > one + two)//第三个数的位数大于前2个数的位数值和,直接减掉 { printf("0\n"); continue; } if(three < one || three < two)//小于其中一个因子的位数,直接减掉 { printf("0\n"); continue; } if(one == 0 || two == 0 || three == 0)//有一个数的位数为0,直接减掉 { printf("0\n"); continue; } total = 0; memset(visit,0,sizeof(visit)); dfs(0); printf("%d\n",total); } return 0; }
时间: 2024-10-07 05:25:23