ACM学习历程—HDU1028 Ignatius and the Princess(组合数学)
Ignatius and the Princess
Description
"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
Output
Sample Input
Sample Output
然后考虑到每组解必然有个最大值,于是设了这样一个函数(或者数组)f(n,k),表示和为n且最大数为k的上述元数不定的方程的解的个数。这样一来题目要求求得就是f(n,1)到f(n,n)的和了。
然后我们考虑,对于f(n,k)如果去掉最大值k,那么其子问题就是求和为n-k且最大值小于等于k的上述方程的解的个数。 于是得到递推方程:


代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define inf 0x3fffffff using namespace std; int a[125][125]; int main()
{
//freopen ("test.txt", "r", stdin);
memset (a, 0, sizeof(a));
for (int i = 1; i <= 120; ++i)
{
for (int j = 1; j <= i; ++j)
{
if (i == 1 || i == j)
{
a[i][j] = 1;
}
else
{
for (int k = 1; k <= j && k <= i-j; ++k)
{
a[i][j] += a[i-j][k];
}
}
a[i][0] += a[i][j];
}
}
int n;
while(scanf("%d", &n) != EOF)
{
printf ("%d\n", a[n][0]);
}
return 0;
}
ACM学习历程—HDU1028 Ignatius and the Princess(组合数学)的更多相关文章
- ACM学习历程—HDU1028 Ignatius and the Princess III(递推 || 母函数)
Description "Well, it seems the first problem is too easy. I will let you know how foolish you ...
- ACM学习历程—HDU2068 RPG的错排(组合数学)
Description 今年暑假杭电ACM集训队第一次组成女生队,其中有一队叫RPG,但做为集训队成员之一的野骆驼竟然不知道RPG三个人具体是谁谁.RPG给他机会让他猜猜,第一次猜:R是公主,P是草儿 ...
- hdu acm 1028 数字拆分Ignatius and the Princess III
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU1028 Ignatius and the Princess III 【母函数模板题】
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu1028 Ignatius and the Princess III(递归、DP)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始
以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
- ACM学习历程—HDU5521 Meeting(图论)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是一个人从1开始走,一个人从n开始走.让最 ...
- ACM学习历程—HDU2476 String painter(动态规划)
http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意是给定一个起始串和一个目标串,然后每次可以将某一段区间染成一种字符,问从起始串到目标串最少需要染多 ...
随机推荐
- URL Handle in Swift (一) -- URL 分解
更新时间: 2018-6-6 在程序开发过程之中, 我们总是希望模块化处理某一类相似的事情. 在 ezbuy 开发中, 我接触到了对于 URL 处理的优秀的代码, 学习.改进.记录下来.希望对你有所帮 ...
- 有一个数组a[N]顺序存放0~N-1,要求每隔两个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。以8个数(N=7)为例:{0,1,2,3,4,5,6,7},0->1->2(删除)->3->4->5(删除)->6->7->0(删除),如此循环直到最后一个数被删除。
// ConsoleApplication12.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" // ConsoleApplication1 ...
- OrCAD Capture出现丢失cdn_sfl401as.dll问题
昨天晚上我PCB图的时候还用OrCAD这个组件来着呢.但是还是好好的.但是今天当我再次启动程序的时候就出现了以下的对话框. 当时就吓了好一跳.好好软件怎么突然就不行了呢?先说说我出现这个问题之后的内心 ...
- Visual Studio Code v1.17
Visual Studio Code v1.17发布 欢迎来到2017年9月发行的Visual Studio代码.在这个版本中有一些重要的更新,我们希望你会喜欢,一些关键的亮点包括: macOS To ...
- JS常见事件以及函数
1.js enter键激发事件 document.onkeydown = function (e) { if (!e) e = window.event; ...
- StringBuilder的append、StringBuffer的append和String str = "a"+"b"的区别?
大家都知道String+String会开销额外的系统资源,粗略的原因是String是不可变类,每一步操作都会返回新的String变量,占用空间及时间. 其实我的理解不是这样的,我们来看看String+ ...
- SpringBoot 定时任务升级篇(动态修改cron参数)
需求缘起:在发布了<Spring Boot定时任务升级篇>之后得到不少反馈,其中有一个反馈就是如何动态修改cron参数呢?那么我们一起看看具体怎么实现,先看下本节大纲: ()简单方式:修改 ...
- Linux 批量替换的一种实现方式
替换某目录下所有文件中的某个字符: sed -i 's/origin_str/new_str/g' `grep origin_str -rl ./` origin_str:被替换的字符串: new_s ...
- 膨胀和腐蚀 - cvErode() 和 cvDilate() 函数实现
前言 膨胀就是对图中的每个像素取其核范围内最大的那个值,腐蚀就相反.这两个操作常用来突出显示图的某个高亮部分或者昏暗部分以及去噪.本文展示两个分别对图像进行膨胀和腐蚀的例子. 膨胀和腐蚀函数 cvEr ...
- 高速修复汉澳sinox命令解释程序bash shell漏洞
bash是linux默认命令行管理程序shell.汉澳 sinox也安装有,尽管sinox并没有默认使用bash.可是用户一旦使用就会可能被通过漏洞入侵,所以必须高速修复.尽管sinox使用freeb ...