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. http请求中的get和post的区别

    1.标准答案 GET在浏览器回退时是无害的,而POST会再次提交请求. GET产生的URL地址可以被Bookmark,而POST不可以. GET请求会被浏览器主动cache,而POST不会,除非手动设 ...

  2. 2017清北学堂(提高组精英班)集训笔记——动态规划Part3

    现在是晚上十二点半,好累(无奈脸),接着给各位——也是给自己,更新笔记吧~ 序列型状态划分: 经典例题:乘积最大(Luogu 1018) * 设有一个长度为 N 的数字串,要求选手使用 K 个乘号将它 ...

  3. nodejs 操作文件系统读取写入文件

    我们通过fs这个模块来对文件系统进行操作,对于文件系统操作一般都有同步.异步方法,两者区别,同步等有返回结果时候,在继续执行后面的代码,异步是不等返回结果,直接执行后面的代码,待有返回结果时候,通过回 ...

  4. SpringSecurity01 SpringSecurity环境搭建

    版本说明: JDK -> java version "1.8.0_101" MAVEN -> Apache Maven 3.5.0 IDEA -> 2017.2. ...

  5. oracle 新增字段

    alter table shop_procategory add (category_ORDER_FIELD number(10) default '0' not null);comment on c ...

  6. 服务器环境迁移,Linux centos7 64位 基础环境部署 jdk+tomcat+mysql+nginx

    最近阿里云服务器到期,这个周末连夜将服务器迁移到美国去了,为什么迁移到美国去呢?主要是因为阿里云服务器费用高,另外网站的访问量不大,对网速要求也不高,主要是宣传和信息传递的作用,加上本人之前在***上 ...

  7. Luogu 3320 [SDOI2015]寻宝游戏

    一开始还真没想到. 发现从所有有宝藏的点出发绕一圈只要不刻意绕路答案都是一样的,即我们呢要求的最后答案$ans = dis(x_1, x_2) + dis(x_2, x_3) +... + dis(x ...

  8. ubuntu判断系统是32位还是64位

    1. 打开终端. 2. 输入命令  sudo uname -m 3. 如果输出为 x86_64, 则说明是64位操作系统:如果输出为 i686,说明是32位操作系统.

  9. Razor中的 内容标记块语法

    在C#中,有两种方法来进行内容块标记 第一种方式 用@: 来标识 @if (true) { @: 测试内容标记块 @DateTime.Now.ToString() } <hr /> 第2种 ...

  10. SQL Server中通过设置非聚集索引(Non-Clustered index)来达到性能优化的目的

    首先我们一下,在SQL Server 2014 Management Studio中,如何为一张表设置Non-Clustered index 具体可以参考  https://docs.microsof ...