acdream 1431 Sum vs Product
Sum vs Product
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
Output
Sample Input
2
5
Sample Output
1
3
Hint
Source
Manager
题解及代码:
通过打表前几项我们会发现构成n。比方n=5时。其形式之中的一个是1 1 2 2 2,都是这样的非常多1,然后其它数字组合的形式。那么我们就能够枚举除了1以外的数字的组合,来计算sum[n]。比方数字组合为2 3 4,那么依据公式我们知道2*3*4=24,2+3+4=9,那么我们还须要补上15个1,加上2 3 4 这三个数字,总共是18个数字,那么2 3 4必定属于sum[18]里面的一中情况。得到验证。这样我们就能用dfs来求出全部的情况数了。
以下的代码是dfs的代码,由于怕超时的缘故,题目AC的代码是打表之后交的。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
int sum[510];
void init()
{
memset(sum,0,sizeof(sum));
}
void dfs(int nt,int nu,int su,int k)
{
for(int i=k;i<=500;i++)
{
if(nu*i>1000) break;
sum[nu*i-su-i+nt+1]++;
//printf("%d %d %d %d %d\n",nu,su,i,nt+1,nu*i-su-i+nt+1);
dfs(nt+1,nu*i,su+i,i);
}
} int main()
{
init();
for(int i=2;i<=500;i++)
dfs(1,i,i,i);
for(int i=2;i<=500;i++)
printf("%d,",sum[i]);
return 0;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
acdream 1431 Sum vs Product的更多相关文章
- ACdream 1431——Sum vs Product——————【dfs+剪枝】
Sum vs Product Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) S ...
- 【概率证明】—— sum and product rules of probability
1. sum and product rules of probability ⎧⎩⎨p(x)=∫p(x,y)dyp(x,y)=p(x|y)p(y) sum rule of probability 的 ...
- acdream Divide Sum
Divide Sum Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitSta ...
- ACdream: Sum
Sum Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticN ...
- ACDream - Power Sum
先上题目: Power Sum Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) S ...
- ACDream - Lowbit Sum
先上题目: C - Lowbit Sum Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others ...
- [LeetCode] Subarray Sum Equals K 子数组和为K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- mysql 中sum (if()) 用法
原表: id fenlei time 1 分类1 20130316 2 分类2 20130316 3 分类3 20130317 ...
- [LeetCode] 560. Subarray Sum Equals K 子数组和为K
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
随机推荐
- MySQL初步研究数据库
我用的是环境Win7.开始学习PHP和MySQL,而买了这<Head First PHP & MySQL>,从能Head First Labs官网获得HeadFirst系列书籍的相 ...
- Java 并发专题 : CyclicBarrier 打造一个安全的门禁系统
继续并发专题~ 这次介绍CyclicBarrier:看一眼API的注释: /** * A synchronization aid that allows a set of threads to all ...
- MessageBox()功能
MessageBox()功能.这是一个非常频繁使用的Win32 API,在屏幕上显示一个窗体,提出问题,并等待用户输入.它的原型是 int MessageBox(HWND hwnd,LPCTSTR l ...
- 中文/英文双语言版本TWRP for Nexus5 -hammerheadcaf
编译及作者:laser杨万荣 编译时间: 2015-05-17 编译目的:用于刷cm-12.0 和cm-12.1 ROM 因为现在Nexus5 最新的 CM是 hammerheadcaf ,即和 ao ...
- WPF绘制党徽(立体效果,Cool)
原文:WPF绘制党徽(立体效果,Cool) 前面用WPF方式绘制了党旗(WPF制作的党旗) ,去年3月份利用C# 及GDI+绘制过党徽,这次使用WPF来绘制党徽. ------------------ ...
- windows下搭建Apache+Mysql+PHP开发环境
原文:windows下搭建Apache+Mysql+PHP开发环境 要求 必备知识 熟悉基本编程环境搭建. 运行环境 windows 7(64位); Apache2.2;MySQL Server 5. ...
- Windows10微软在线账户与本地账户的切换方法
Win10里面存在着两个账户,除了本地账户外,还有着一个微软在线账户,这个账户可以同步设置.日历等数据.不过对于大部分用户来说,本地账户已经足够我们使用了,那么这两个账户之间该如何切换呢? Win10 ...
- JAVA card 应用程序开发(七) JAVA 卡数据(永久数据/)时间数据
JAVA 卡对象 JAVA CARD 存储器装置: a. ROM: 永久保存程序和数据,虚拟机,API等待:(Applets它也可以在这里放) b. RAM: 栈数据,暂时对象. ...
- 开源通讯组件ec
跨平台开源通讯组件elastic communication elastic communication是基于c#开发支持.net和mono的通讯组件(简称EC),EC的主要目的简化mono和.net ...
- 最新jhost免费jsp云空间会员邀请码
jhost支持jsp.php的免费云空间,邀请码用于激活空间服务: 邀请码:20141003104317_149661 有效期:2014-10-03 http://w ...