Sum vs Product

Time Limit: 4000/2000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)

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}.

Source

Andrew Stankevich Contest 23

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的更多相关文章

  1. ACdream 1431——Sum vs Product——————【dfs+剪枝】

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

  2. 【概率证明】—— 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 的 ...

  3. acdream Divide Sum

    Divide Sum Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitSta ...

  4. ACdream: Sum

    Sum Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticN ...

  5. ACDream - Power Sum

    先上题目: Power Sum Time Limit: 20000/10000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) S ...

  6. ACDream - Lowbit Sum

    先上题目: C - Lowbit Sum Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others ...

  7. [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 ...

  8. mysql 中sum (if()) 用法

    原表: id    fenlei     time 1      分类1      20130316 2      分类2      20130316 3      分类3      20130317 ...

  9. [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 ...

随机推荐

  1. Swift基础--使用TableViewController自己定义列表

    首先建立一个swift项目,把storyboard的内容删掉,加入一个Navigation Controller.然后设置storyboard相应界面的class,在Navigation Contro ...

  2. 谈论高并发(二十二)解决java.util.concurrent各种组件(四) 深入了解AQS(二)

    上一页介绍AQS其基本设计思路以及两个内部类Node和ConditionObject实现 聊聊高并发(二十一)解析java.util.concurrent各个组件(三) 深入理解AQS(一) 这篇说一 ...

  3. doc-remote-debugging.html

    https://studio.zerobrane.com/doc-remote-debugging.html

  4. iOS 删除黑色边框线导航栏&amp;删除搜索框的阴影边界线和中黑色文本输入框 - 解

    删除黑色边框线导航栏 in viewDidload: [self.navigationController.navigationBar setBackgroundImage:[[UIImage all ...

  5. AC自己主动机 总结

    模板--参考六如家培训指南 /*===============================*\ 依照训练指南写的 \*===============================*/ #incl ...

  6. quartz.net持久化和集群

    首先你应该使用的是持久化的quartz,所有定时任务的情况都是保存在数据库表总的,每次启动时,scheduler容器都是按照qrtz_triggers等表内存储的信息来执行定时任务(主要包括cron表 ...

  7. 【转】求质数算法的N种境界

    原文地址:http://blog.csdn.net/program_think/article/details/7032600/ ★引子 前天,俺在<俺的招聘经验[4]:通过笔试答题能看出啥?& ...

  8. AutoFac使用方法总结:Part I

    注册部分 使用RegisterType进行注册 [Fact] public void can_resolve_myclass() { var builder = new ContainerBuilde ...

  9. zoj 2874 &amp; poj 3308 Paratroopers (最小割)

    意甲冠军: 一m*n该网络的规模格.详细地点称为伞兵着陆(行和列). 现在,在一排(或列) 安装激光枪,激光枪可以杀死线(或塔)所有伞兵.在第一i安装一排 费用是Ri.在第i列安装的费用是Ci. 要安 ...

  10. effective c++ 条款23 perfer nonmember nonfreind function to member function

    主要的理由还是封装.nonmember nonfreind function 不能访问类private 成员变量. 这个场景是有一个类提供了一些基本功能,比如 class WebBrowser { p ...