ACdream1431-Sum vs Product

Sum vs Product

Time Limit: 2000/1000MS (Java/Others)  Memory Limit: 128000/64000KB (Java/Others)
Problem 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}.
Source
Andrew Stankevich Contest 23
Manager

题意:让你求1~n中任意选n个数字,保证这n个数字加和与乘积相等,问有多少种方法。
解题思路:暴力搜索,加上剪枝。从2开始枚举,乘积总是比加和的上升速度快。那么如果前m个数的和加上剩下的n-m个1的值还是小于前m个数的乘积的话,那么就不可能有让最后的和跟积相等的情况了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>

using namespace std;

int n,sum;

void dfs(int cur,int sum1,int mul,int k)
{
    if(sum1+n-k<mul) return ;
    else if(sum1+n-k==mul)
    {
        sum++;
        return ;
    }
    for(int i=cur; i<=n; i++)
        dfs(i,sum1+i,mul*i,k+1);
}

int main()
{
    while(~scanf("%d",&n))
    {
        sum=0;
        dfs(2,0,1,0);
        printf("%d\n",sum);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值