[Ignatius and the Princess III] 整数的无序拆分(DP + 生成函数)
整数的有序拆分就是隔板法,无序拆分则有两种处理方法
DP递推
我们假设P(n,m)P(n,m)P(n,m)是正整数nnn无序拆分为mmm个正整数的方案数
对于某一种拆分,不妨将拆分出来的mmm个数从小到大排序,分类讨论
- 最小的数等于111,那么去掉这个111,相当于把剩下的n−1n-1n−1拆分成m−1m-1m−1个数,方案数就为P(n−1,m−1)P(n-1,m-1)P(n−1,m−1)
- 最下的数大于111,那么将所有的数减去111,相当于把剩下的n−mn-mn−m拆分成mmm个数,方案数就为P(n−m,m)P(n-m,m)P(n−m,m)
则最终答案为∑i=1nP(n,i)\large\sum_{i=1}^nP(n,i)∑i=1nP(n,i),时间复杂度为Θ(n2)\large \Theta(n^2)Θ(n2)
AC code
#include <bits/stdc++.h>
using namespace std;
int P[121][121];
int main ()
{
for(int i = 1; i <= 120; ++i)
{
P[i][1] = P[i][i] = 1;
for(int j = 2; j < i; ++j)
P[i][j] = P[i-1][j-1] + P[i-j][j];
}
for(int i = 1; i <= 120; ++i)
for(int j = 1; j <= i; ++j) //做前缀和
P[i][j] += P[i][j-1];
int n;
while(~scanf("%d", &n)) printf("%d\n", P[n][n]);
}
生成函数/卷积
- 想一想,显然可得答案为
(1+x1+x2+...)∗(1+x2+x4+...)∗(1+x3+x6+...)∗...\large (1+x^1+x^2+...)\\*(1+x^2+x^4+...)\\*(1+x^3+x^6+...)\\*...(1+x1+x2+...)∗(1+x2+x4+...)∗(1+x3+x6+...)∗...所得多项式中次数为nnn的系数 - 因为是多项式的乘积,就是在每个多项式中选111项,最后再加起来。在第iii个多项式中,111表示数iii不选,xkix^{ki}xki表示选了kkk个iii
- 这实际上就是把加法运算,转化为多项式的次数来做乘法/卷积。思想类似于(分治)FFT等
- 时间复杂度为Θ(n3)\large \Theta(n^3)Θ(n3)
AC code
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 121;
int n, ans[MAXN], tmp[MAXN];
int main ()
{
while(~scanf("%d", &n))
{
for(int i = 0; i <= n; ++i)
ans[i] = 1, tmp[i] = 0;
for(int i = 2; i <= n; ++i)
{
for(int j = 0; j <= n; j+=i)
for(int k = 0; k + j <= n; ++k)
tmp[j+k] += ans[k];
for(int j = 0; j <= n; ++j)
ans[j] = tmp[j], tmp[j] = 0;
}
printf("%d\n", ans[n]);
}
}
[Ignatius and the Princess III] 整数的无序拆分(DP + 生成函数)的更多相关文章
- HDU 1028 Ignatius and the Princess III 整数的划分问题(打表或者记忆化搜索)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1028 Ignatius and the Princess III Time Limit: 2000/1 ...
- 题解报告:hdu 1028 Ignatius and the Princess III(母函数or计数DP)
Problem Description "Well, it seems the first problem is too easy. I will let you know how fool ...
- Ignatius and the Princess III HDU - 1028 || 整数拆分,母函数
Ignatius and the Princess III HDU - 1028 整数划分问题 假的dp(复杂度不对) #include<cstdio> #include<cstri ...
- HDU 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 ...
- hdu 1028 Ignatius and the Princess III 母函数
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 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 ...
- hdu 1028 Ignatius and the Princess III(DP)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu 1028 Ignatius and the Princess III 简单dp
题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...
随机推荐
- jmeter Dashboard Report
说明:详情参考:https://jmeter.apache.org/usermanual/generating-dashboard.html JMeter3.0以后引入了Dashboard Repor ...
- 《算法图解》全本PDF下载附百度云链接
作者使用Python和图画来解释算法,找了好久才找到PDF版本,末尾附百度云链接~ 作者[美]Aditya Bhargava 译者袁国忠 类别 出版 / 非虚构 出版社人民邮电出版社 / 2017-0 ...
- Go语言【数据结构】字符串
字符串 简介 一个字符串是一个不可改变的字节序列,字符串通常是用来包含人类可读的文本数据.和数组不同的是,字符串的元素不可修改,是一个只读的字节数组.每个字符串的长度虽然也是固定的,但是字符串的长度并 ...
- golang 之 sql
golang提供了sql包查询数据 建立连接 导入第三方包 import( "database/sql" _"github.com/go-sql-driver/mysql ...
- centos 7安装jdk并封装service服务
一.概述 有一个Spring Cloud的jar包,文件名为:RDS.jar.必须要jdk1.8版本,需要部署在 Centos 7.5的服务器上面,最好能设置开机自启动! 二.安装jdk 关闭防火墙 ...
- Golang ---testing包
golang自带了testing测试包,使用该包可以进行自动化的单元测试,输出结果验证,并且可以测试性能. 建议安装gotests插件自动生成测试代码: go get -u -v github.com ...
- 1.ASP.NET Core 中向 Razor Pages 应用添加模型
右键单击“RazorPagesMovie”项目 >“添加” > “新建文件夹”. 将文件夹命名为“Models”.右键单击“Models”文件夹. 选择“添加” > “类”. 将类命 ...
- 如何在一个Docker中同时运行多个程序进程?
我们都知道Docker容器的哲学是一个Docker容器只运行一个进程,但是有时候我们就是需要在一个Docker容器中运行多个进程 那么基本思路是在Dockerfile 的CMD 或者 ENTRYPOI ...
- modbus协议使用小记
下载了libmodbus库,交叉编译后运行,总是接收回复时不正确.原因不明. 由于使用到modbus的需求比较简单,所以选择直接拼出modbus的请求报文,然后用串口直接发送和接收的方式, 拼modb ...
- NMS的实现代码详解
NMS代码说明(来自Fast-RCNN) 个人觉得NMS包含很多框,其坐标为(x1,y1,x2,y2),每个框对应了一个score,我们将按照score得分降序,并将第一个最高的score的框(我们叫 ...