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. strdup与strndup

    strdup()函数是c语言中常用的一种字符串拷贝库函数,一般和free()函数成对出现. extern char *strdup(char *s); 头文件:string.h 功 能: 将串拷贝到新 ...

  2. 开源一个windows消息队列查看器

    windows消息简单易用,在异步消息发送场景的使用还是比较适合.为了方便查看队列中的消息和删除一些异常消息,开发了一个简单的小工具拿出来分享下. 源码地址:https://gitee.com/eab ...

  3. navicat自动备份

    http://blog.csdn.net/eastmount/article/details/70239244

  4. webStorage

    1.HTML5中的本地存储概念是什么? 很多时候我们会存储用户本地信息到电脑上,例如:比方说用户有一个填充了一半的长表格,然后突然网络连接断开了,这样用户希望你能存储这些信息到本地,当网络恢复的时候, ...

  5. Linux下部署MySQL,大小写敏感踩坑记录

    今天在将开发环境中的门户数据库复制到新环境后,使用SqlSugar的ORM框架进行数据库操作的时候,出现了主键找不到的现象.排查了很久终于发现了关键点.特此记录. 1.开发环境:    操作系统:CE ...

  6. [51nod1384]全排列

    法一:next_permutation函数,两个参数分别为起始指针和末尾指针. #include<bits/stdc++.h> using namespace std; typedef l ...

  7. 点云视窗类CloudViewer

    博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=149 点云视窗类CloudViewer是简单显示点云的可视化工具类,可以让用 ...

  8. Entity Framework Code-First(16):Move Configurations

    Move Configurations to Separate Class in Code-First: By now, we have configured all the domain class ...

  9. 1.2 xss原理分析与剖析(3)

    0×01 第三方劫持 (外调J/C): 本方法是我看长短短贴代码时知晓的,这篇文章我只是把这个攻击手法整理了出来,来说明这个漏洞,这个攻击手法并不是我发现的,我也不是太清楚是谁.“第三方劫持”就是把资 ...

  10. javascript 数组中出现的次数最多的元素

    javascript 数组中出现的次数最多的元素 var arr = [1,-1,2,4,5,5,6,7,5,8,6]; var maxVal = arr[0]; // 数组中的最大值 var min ...