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 ...
随机推荐
- Java EE (2) -- Java EE 6 Enterprise JavaBeans Developer Certified Expert(1z0-895)
Introduction to Java EE Gain an understanding of the Java Platform, Enterprise Edition (Java EE) Exa ...
- 0x00000000该内存不能为read
0X000000存储器不能read解决方案 有这种现象方面,首先,在硬件,这有问题的内存,二,软件,其中有许多问题. 一:先说说硬件: 一般来说,电脑硬件不easy生病.内存故障的可能性并不大(非你的 ...
- easyui LinkButton
http://www.zi-han.net/case/easyui/menu&button.html
- java中途强制跳出递归
有些时候我们需要在中途强制跳出递归,而且还是需要一步跳出,而不一层一层的跳出,这时,我们可以采用抛异常的方法来实现. class Test { static class StopMsgExceptio ...
- Javascript 进阶 作用域 作用域链
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/25076713 一直觉得Js很强大,由于长期不写js代码,最近刚好温故温故. 1. ...
- fork与vfork详解
一.fork函数 要创建一个进程,最基本的系统调用是fork,系统调用fork用于派生一个进程,函数原型如下: pid_t fork(void) 若成功,父进程中返回子进程ID,子进程中返回0,若出 ...
- 用golang写的生成文件md5sum,检验文件md5sum
源代码地址: https://github.com/sndnvaps/md5sum-golang
- linux、hdfs、hive、hbase经常使用的命令
linux经常使用命令 pwd 查看当前工作文件夹的绝对路径 cat input.txt 查看input.txt文件的内容 ls 显示当前文件夹下全部的文件及子文件夹 rm recommender-d ...
- Unreal Engine 4 C++ 能够创建角色Zoom摄像头(资源)
游戏摄像头可以观察到的距离越近,作用和拉远是一个比较普遍的要求,UE4它也实现比较简单. 在这篇文章中TopDown模板案例,解释如何,分步实施能Zoom摄像头. 创建TopDown模板C++项目达产 ...
- 【rman,1】经典案例增量备份
一.备份策略: 1.星期天晚上 -level 0 backup performed(全备份) 2.星期一晚上 -level 2 backup performed 3.星期二晚上 ...