[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的最大值,最后答案是 ...
随机推荐
- NFS挂载参数
mount -t nfs -o rw,bg,hard,nointr,rsize=32768,wsize=32768,tcp,actimeo=0,vers=3,timeo=6 192.168.12.50 ...
- U9单据打印模板自定义扩展字段显示名称
UBF打印模板中,单据自定义扩展字段显示均为扩展字段值集值编码,而在实际运用过程中打印时需要显示扩展字段名称,具体实现方法如下 方式一:采用SQL系统定义函数[dbo].[fn_GetSegName] ...
- Gradle 翻译 tips and recipes 使用技巧 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 在win10上安装FFmpeg
1.下载:https://ffmpeg.zeranoe.com/builds/ 2.解压缩到,并改名为ffmpeg,放到如:D:\ffmpeg文件夹. 3.添加系统环境变量. 4.打开cmd,输入:f ...
- NETRemoting学习笔记
1..NET Remoting概念 1.一种分布式处理方式.从字面意义上看出,他是基于.net平台的一种远程对象开发技术,该技术是将远程计算机中的数据视为分布式对象来进行开发. 2.一种网络通信技术. ...
- spring Boot 学习(一、Spring Boot与缓存)
JSR-107.Spring缓存抽象.整合Redis Java Caching定义了5个核心接口,分别是CachingProvider, CacheManager, Cache, Entry 和 Ex ...
- java之mybatis之helloworld
1. MyBatis 是一款一流的支持自定义SQL.存储过程和高级映射的持久化框架. MyBatis几乎消除了所有的 JDBC 代码,也基本不需要手工去设置参数和获取检索结果. MyBatis几乎能够 ...
- github上传本地项目代码
进入github首页,点击新项目new repository,如下图所示: 然后进入如下页面,填写信息: 最后点击Create repository,生成如下页面: 红色圈圈画的步骤,先点击Clone ...
- 长期作业:web框架源码剖析
Tornado框架 1.1. 手动安装 1.2. 从简单的开始:分析红框部分的源码 Django框架
- Flask 和 Django 框架的区别
1)Flask Flask确实很“轻”,不愧是Micro Framework,从Django转向Flask的开发者一定会如此感慨,除非二者均为深入使用过 Flask自由.灵活,可扩展性强,第三方库的选 ...