码迷,mamicode.com
首页 > 其他好文 > 详细

DFS acdrean 1431

时间:2015-04-17 09:41:05      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

C - Sum vs Product
Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu
Appoint description: 

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;
}

  

DFS acdrean 1431

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4433959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!