[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的最大值,最后答案是 ...
随机推荐
- sql特殊日期
--a. 本月的第一天 select dateadd(mm, datediff(mm,0,getdate()), 0) as 本月的第一天 --b. 本月的最后一天 select dateadd(ms ...
- Actions require unique method/path combination for Swagger
原文:Actions require unique method/path combination for Swagger services.AddSwaggerGen (c => { c.Re ...
- springcolud 的学习(二).SpringCloud微服务框架
为什么选择SpringCloud因为SpringCloud出现,对微服务技术提供了非常大的帮助,因为SpringCloud 提供了一套完整的微服务解决方案,不像其他框架只是解决了微服务中某个问题. 服 ...
- java之hibernate之hibernate查询
这篇主要简单间接 hibernate查询 1.数据库操作中最重要的是查询,Hibernate提供了多种查询方式来帮助程序员快速实现查询功能. 有hql,本地sql查询,Criteria查询,examp ...
- MVC学习笔记(三)—用EF向数据库中添加数据
1.在EFDemo文件夹中添加Controllers文件夹(用的是上一篇MVC学习笔记(二)—用EF创建数据库中的项目) 2.在Controllers文件夹下添加一个空的控制器(StudentsCon ...
- Hibernate的关联映射--一对多、
这是我 1 单向一对多: 实体类:(课程类)Grade与(学生类)Student的一对多关系 学生类: public class Student implements java.io.Serializ ...
- Weak Session IDs
工具的使用 首先github上下载火狐插件(正版收费),按F12调用 服务器生成sessionID通过response返回给浏览器,sessionID存放在浏览器cookie中,然后再通过cookie ...
- MySQL数据库基本规范整理
此篇文章是学习MySQL技术整理的,不足之处还望指教,不胜感激. 数据库基本规范涉及数据库命名规范.数据库索引设计规范.数据库基本设计规范.数据库字段设计规范.数据库SQL开发规范.数据库操作行为规范 ...
- 【面试突击】-Redis常见面试题(二)
1.什么是Redis?简述它的优缺点? Redis本质上是一个Key-Value类型的内存数据库,很像memcached,整个数据库统统加载在内存当中进行操作,定期通过异步操作把数据库数据flush到 ...
- Typora-yes:typora最舒适的使用-优化主题+图床服务+自动上传图片插件
转载注明出处:https://www.cnblogs.com/nreg/p/11992678.html,谢谢 开源项目下载:https://github.com/nreg/typora-yes 云盘: ...