Problem Description
When Fat Brother is a little child, he just shows his talent in mathematics. He was able to add and multiply at a very young age. Given a set of N integers, Fat Brother would feel happy if the sum of the integers in this set equals to the multiply of these integers. Now given an integer N, your task is to calculate how many sets with N integers would make little Fat Brother happy.
Input
The first line of the date is an integer T (1 <= T <= 20), which is the number of the text cases.
Then T cases follow, each case starts with a number N (2 <= N <= 500) indicated the number of the integers in the set.
Output
For each case, outputs the case number first, then output the number of the sets that would make little Fat Brother happy
See the sample input and output for more details.
Sample Input
2
2
5
Sample Output
Case 1: 1
Case 2: 3
解题思路:刚开始看到题目的时候都虚了,一直再想有没有什么特殊解法,结果只需要暴力DFS + 剪枝就可以。。。
只要积大于和的时候,无论如何枚举和都无法大于积了。
再者。枚举的时候从小到大枚举,因为集合是乱序的,所以这个也可以减去一大段的枝
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int ans;
void dfs(int num, int cur, int Sum, int ji, int Min) {
if(cur == num && Sum == ji) {
ans++;
return ;
}
if(cur > num || (cur == num && Sum != ji))
return ;
for(int i = Min; i <= num; i++) {
Sum += i;
ji *= i;
if(ji > Sum)
break;
dfs(num,cur+1,Sum,ji,i);
Sum -= i;
ji /= i;
}
}
int main() {
int test, cas = 1, n;
scanf("%d", &test);
while(test--) {
scanf("%d", &n);
ans = 0;
dfs(n,0,0,1,1);
printf("Case %d: %d\n",cas++, ans);
}
return 0;
}