C - Sum vs Product
Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu
Submit Status Practice ACdream 1431
Appoint description:
System Crawler (2015-04-16)
Description
Peter has just learned mathematics. He learned how to add, and how to multiply. The fact that 2 + 2 = 2 × 2 has amazed him greatly. Now he wants find more such examples. Peters calls a collection of numbers beautiful if the product of the numbers in it is equal to their sum.
For example, the collections {2, 2}, {5}, {1, 2, 3} are beautiful, but {2, 3} is not.
Given n, Peter wants to find the number of beautiful collections with n numbers. Help him!
Input
The first line of the input file contains n (2 ≤ n ≤ 500)
Output
Output one number — the number of the beautiful collections with n numbers.
Sample Input
2 5
Sample Output
1 3
Hint
The collections in the last example are: {1, 1, 1, 2, 5}, {1, 1, 1, 3, 3} and {1, 1, 2, 2, 2}.
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> #include<queue> #include<vector> #include<stack> using namespace std; int n,ans; void dfs(int x,int k,int product,int sum) { for(int i=x;i>=2;i--) { if(product*i>sum+i+n-k) continue; else if(product*i==sum+i+n-k) ans++; else dfs(i,k+1,product*i,sum+i); } } int main() { scanf("%d",&n); dfs(n,1,1,0); printf("%d\n",ans); return 0; }