Sum vs Product

Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Others)
Submit Status

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}.
 
题目大意:让你求1---n中任意选n个数字,保证这n个数字加和与乘积相等,问有多少种方法。
 
 
解题思路:暴力搜索,加上剪枝。我们从2开始枚举,因为从2开始,乘积总是比加和的上升速度快。那么如果前m个数的和加上剩下的n-m个1的值还是小于前m个数的乘积的话,那么继续枚举就不可能有让最后的和跟积相等的情况了。所以这个剪枝会很剪掉很多情况。
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,sum;
void dfs(int cur,int sum1,int sum2,int dep){
if(sum1+(n-dep)<sum2){
return ;
}
if(sum1+(n-dep)==sum2){
sum++;
return ;
}
for(int i=cur;i<=n;i++){
dfs(i,sum1+i,sum2*i,dep+1);
}
}
int main(){
while(scanf("%d",&n)!=EOF){
sum=0;
dfs(2,0,1,0);
printf("%d\n",sum);
}
return 0;
}

  

ACdream 1431——Sum vs Product——————【dfs+剪枝】的更多相关文章

  1. acdream 1431 Sum vs Product

    Sum vs Product Time Limit: 4000/2000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Submi ...

  2. POJ 1564 Sum It Up (DFS+剪枝)

                                                                                                       ...

  3. poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)

    Sum It Up Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...

  4. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  6. LA 6476 Outpost Navigation (DFS+剪枝)

    题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...

  7. poj 1011 Sticks (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 127771   Accepted: 29926 Descrip ...

  8. NYOJ 1249 物资调度(DFS+剪枝)

    题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=1249 描述 某地区发生了地震,灾区已经非常困难,灾民急需一些帐篷.衣物.食品和血浆等物 ...

  9. poj1011 && uva307 DFS + 剪枝

    将木棒从大到小排列,保证每次的选择都是最长可选的木棒. 剪枝: 1 . 如果第 i 根木棒被选择却无法成功拼接,那么后面与其长度相同的也不能选择. 2 . 如果第 cnt + 1 根木棒无法成功拼接, ...

随机推荐

  1. logback 相关

    %logger{36} 表示logger名字最长36个字符,否则按照句点分割. %X{key} to get the value that are stored in the MDC map ${lo ...

  2. Spring 学习十四 Spring security安全

    Spring security: 我用过的安全机制:   oauth2, filter,  secured方法保护 9.2  保护web请求: 9.2.1  代理Servlet过滤器: Delegat ...

  3. File 类 操作实例

    File 操作 <介绍> 尽管java.io定义的大多数类是实行流式操作的,File类不是.它直接处理文件和文件系统.也就是说,File类没有指定信息怎样从文件读取或向文件存储:它描述了文 ...

  4. JDBC编程之程序优化

    -----------------siwuxie095 首先下载 MySQL 的 JDBC 驱动,下载链接: https://dev.mysql.com/downloads/connector/j/ ...

  5. nfs(Network FileSystem)的简单配置

    如果想要在window和linux之间共享文件,那么用samba.如果仅仅想在Unix like系统之间共享文件,那么可以用NFS. 这篇文章分为两个部分,第一部分只是简单的用:照着写的步骤一步步来就 ...

  6. JavaScript中的构造函数和工厂函数说明

    在cnblog上看到一篇文章,讲解JS中的构造函数和工厂函数,觉得讲的真好 JavaScript中的工厂函数和构造函数都能用来创建一个对象,我们可以来看看下面的例子 构造函数 function cre ...

  7. UVa 11468 Substring (AC自动机+概率DP)

    题意:给出一个字母表以及每个字母出现的概率.再给出一些模板串S.从字母表中每次随机拿出一个字母,一共拿L次组成一个产度为L的串, 问这个串不包含S中任何一个串的概率为多少? 析:先构造一个AC自动机, ...

  8. asp.net刷新本页面的六种方法总结

    第一: private void Button1_Click( object sender, System.EventArgs e )   {     Response.Redirect( Reque ...

  9. MVC下使用ajax后台查询值赋值到前端控件

    初学MVC,今天做个简单的功能,就是输入BeginDate和EndDate,从后台计算后赋值给另外一个文本框Amount 界面很简单,方法也很简单,今天就使用jquery的post方法,先准备后台代码 ...

  10. html5表单及新增的改良元素

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...