Ignatius and the Princess III

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16191    Accepted Submission(s): 11407

Problem Description
"Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.

"The second problem is, given an positive integer N, we define an equation like this:
  N=a[1]+a[2]+a[3]+...+a[m];
  a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
  4 = 4;
  4 = 3 + 1;
  4 = 2 + 2;
  4 = 2 + 1 + 1;
  4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"

 
Input
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.
 
Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.
 
Sample Input
4
10
20
 
Sample Output
5
42
627
 
Author
Ignatius.L
 
整数拆分问题
试着用数形结合的思想理解dfs
每个节点维护两个值,当前结点的值和剩下可传递给儿子的值,为了避免重复,所以每个节点的值都大于等于儿子节点的值
 
如果每次询问都来一遍dfs会进行很多重复运算,所以可用记忆化搜索
 
/*
ID: LinKArftc
PROG: 1028.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ;
int dp[maxn][maxn]; int dfs(int last, int res) {
if (res <= ) return ;
if (dp[last][res]) return dp[last][res];
int ret = ;
if (last >= res) {
for (int i = res; i >= ; i --) {
ret += dfs(i, res - i);
}
} else {
for (int i = last; i >= ; i --) {
ret += dfs(i, res - i);
}
}
dp[last][res] = ret;
return ret;
} int main() {
int n;
while (~scanf("%d", &n)) {
int ans = ;
memset(dp, , sizeof(dp));
for (int i = n; i >= ; i --) ans += dfs(i, n - i);
printf("%d\n", ans);
} return ;
}

HDU1028 (整数拆分)的更多相关文章

  1. [hdu1028]整数拆分,生成函数

    题意:给一个正整数n,求n的拆分方法数(不考虑顺序) 思路:不妨考虑用1~n来构成n.用多项式表示单个数所有能构成的数,用多项式表示,就相当于卷积运算了. 1 2 3 4 5 6 7 8 9 10 1 ...

  2. HDU 4651 Partition(整数拆分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4651 题意:给出n.求其整数拆分的方案数. i64 f[N]; void init(){    f[0 ...

  3. LightOJ 1336 Sigma Function(数论 整数拆分推论)

    --->题意:给一个函数的定义,F(n)代表n的所有约数之和,并且给出了整数拆分公式以及F(n)的计算方法,对于一个给出的N让我们求1 - N之间有多少个数满足F(x)为偶数的情况,输出这个数. ...

  4. LightOJ 1341 Aladdin and the Flying Carpet(整数拆分定理)

    分析:题目并不难理解,就是一些细节上的优化需要我们注意,我在没有优化前跑了2000多MS,优化了一些细节后就是400多MS了,之前还TLE了好几次. 方法:将整数拆分为质因子以后,表达为这样的形式,e ...

  5. 整数拆分问题_C++

    一.问题背景  整数拆分,指把一个整数分解成若干个整数的和 如 3=2+1=1+1+1  共2种拆分 我们认为2+1与1+2为同一种拆分 二.定义 在整数n的拆分中,最大的拆分数为m,我们记它的方案数 ...

  6. Pollard-Rho大整数拆分模板

    随机拆分,简直机智. 关于过程可以看http://wenku.baidu.com/link?url=JPlP8watmyGVDdjgiLpcytC0lazh4Leg3s53WIx1_Pp_Y6DJTC ...

  7. poj3181【完全背包+整数拆分】

    题意: 给你一个数n,在给你一个数K,问你这个n用1-k的数去组合,有多少种组合方式. 思路: 背包重量就是n: 那么可以看出 1-k就是重物,价值是数值,重量是数值. 每个重物可以无限取,问题转化为 ...

  8. HDU 1028 Ignatius and the Princess III(母函数整数拆分)

    链接:传送门 题意:一个数n有多少种拆分方法 思路:典型母函数在整数拆分上的应用 /********************************************************** ...

  9. LeetCode 343. 整数拆分(Integer Break) 25

    343. 整数拆分 343. Integer Break 题目描述 给定一个正整数 n,将其拆分为至少两个正整数的和,并使这些整数的乘积最大化. 返回你可以获得的最大乘积. 每日一算法2019/5/2 ...

随机推荐

  1. 10-Mysql数据库----数据的增删改

    本节重点: 插入数据 INSERT 更新数据 UPDATE 删除数据 DELETE 再来回顾一下之前我们练过的一些操作,相信大家都对插入数据.更新数据.删除数据有了全面的认识.那么在mysql中其实最 ...

  2. Spring Boot 学习随记

    微架构的思想在各大互联网公司越来越普及,特此记录Spring Boot的一些细节问题! 网上spring-boot的教程一堆一堆,就没有必要再详细记录了 1:建议通过Idea 来创建spring-bo ...

  3. [USACO19JAN]Cow Poetry

    题面 Solution: 这是一道很好的dp题. 一开始看不懂题面没有一点思路,看了好久题解才看懂题目... \(y[i]\) 为第 \(i\) 个词结尾,\(l[i]\) 为第 \(i\) 个词长度 ...

  4. 图的同构 (Graph Isomorphism)

    整理摘自:https://www.jianshu.com/p/c33b5d1b4cd9 同构是在数学对象之间定义的一类映射,它能揭示出在这些对象的属性或者操作之间存在的关系.若这两个数学结构之间存在同 ...

  5. tensorflow学习笔记(2)-反向传播

    tensorflow学习笔记(2)-反向传播 反向传播是为了训练模型参数,在所有参数上使用梯度下降,让NN模型在的损失函数最小 损失函数:学过机器学习logistic回归都知道损失函数-就是预测值和真 ...

  6. JavaScript - Standard built-in objects

    标准对象分类 Value Properties 以下全局属性返回一个简单的值:它们没有属性或者方法: Infinity NaN undefined null literal Function Prop ...

  7. HDU 4722 Good Numbers(位数DP)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description If we sum up every digit of a number and the result can be exactly divided by 10, we say ...

  8. 修改freemarker的ftl时,不重启tomcat的办法(使用了springMVC)

    一.在使用Freemarker 时,需要在spring-mvc.xml 配置文件中作如下配置: <!-- 配置freeMarker的模板路径 --> <bean id="f ...

  9. Asp.Net生命周期系列一

    Asp.Net生命周期对于初级甚至中级程序员来说,一直都是一个难题,很多程序员不了解生命周期,导致使用Asp.Net做开发感觉很不灵活,感觉太多东西被微软封装好了,我们不能改变,其实只要你稍微了解一下 ...

  10. fork开源代码后如何基于某个tag建立自己的branch

    应用场景: 在github上fork一个自己想看的开源项目,想基于某个tag来写一些测试demo,然后可以做到版本控制. 方法: //克隆 git clone xxxxx.git //查看tag gi ...