Sum vs producttime limit: 4000/2000 ms (Java/others) memory limit: 128000/64000 KB (Java/others) submitstatisticnext problemproblem description
Peter has just learned mathematics. he learned how to add, and how to multiply. the fact that 2 + 2 = 2x2 has amazed him greatly. now he wants find more such examples. peters calla 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
25
Sample output
13
Hintthe collections in the last example are: {1, 1, 1, 2, 5}, {1, 1, 1, 3, 3} and {1, 1, 2, 2, 2 }. sourceandrew stankevich contest 23 managermathlover
Question and code:
By typing the first few items in the table, we will find that when n is set to 5, one of the forms is 1 1 1 2 2 2, which is a combination of many numbers. Then we can enumerate the combinations of numbers except 1 to calculate sum [N]. For example, if the combination of numbers is 2 3 4, then we know 2*3*4 = 24, 2 + 3 + 4 = 9 according to the formula, then we need to add 15 1, with the three numbers 2 3 4 and 18 in total, 2 3 4 must belong to the medium situation in sum [18. Then we can use DFS to find all the situations.
The following code is the DFS Code. For the sake of timeout, the AC code is written after the table is created.
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;int sum[510];void init(){ memset(sum,0,sizeof(sum));}void dfs(int nt,int nu,int su,int k){ for(int i=k;i<=500;i++) { if(nu*i>1000) break; sum[nu*i-su-i+nt+1]++; //printf("%d %d %d %d %d\n",nu,su,i,nt+1,nu*i-su-i+nt+1); dfs(nt+1,nu*i,su+i,i); }}int main(){ init(); for(int i=2;i<=500;i++) dfs(1,i,i,i); for(int i=2;i<=500;i++) printf("%d,",sum[i]); return 0;}
Acdream 1431 sum vs Product