[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的最大值,最后答案是 ...
随机推荐
- JQuery高级(二)
3. 事件绑定 1. jquery标准的绑定方式 * jq对象.事件方法(回调函数): * 注:如果调用事件方法,不传递回调函数,则会触发浏览器默认行为. * 表单对象.submit();//让表单提 ...
- Python知识点总结篇(五)
软件目录结构规范 目标: 提高可读性: 提高可维护性: 常见结构 Demo/ |-- bin/ #存放项目的一些可执行文件 | |-- demo #可执行程序,启动demo调main.py | |-- ...
- zbar android sdk源码编译
zbar,解析条码和二维码的又一利器,zbar代码是用c语言编写的,如果想在Android下使用zbar类库,就需要使用NDK将zbar编译成.so加载使用,zbar编译好的Android SDK可以 ...
- Scala 系列(一)—— Scala 简介及开发环境配置
一.Scala简介 1.1 概念 Scala 全称为 Scalable Language,即"可伸缩的语言",之所以这样命名,是因为它的设计目标是希望伴随着用户的需求一起成长.Sc ...
- Java单元测试 Http Server Mock框架选型
背景动机 某期优化需要针对通用的HttpClient封装组件--HttpExecutor在保证上层暴露API不动的前提做较多改动,大致包括以下几点: apache http client 版本升级 H ...
- 全栈项目|小书架|微信小程序-登录回调及获取点赞列表功能
效果图 这一节介绍,登录回调 以及 喜欢列表 的实现. 登录回调:这里是指在获取登录完成之后,再进行下一步的操作. 比如效果图中我的页面,默认是未登录状态,积分和喜欢列表的数量都没有获取到. 而登录成 ...
- spring Boot 学习(四、Spring Boot与任务)
一.异步任务 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在 处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用 多线程来完成此类任务,其实,在Spri ...
- Go defer 会有性能损耗,尽量不要用?
上个月在 @polaris @轩脉刃 的全栈技术群里看到一个小伙伴问 “说 defer 在栈退出时执行,会有性能损耗,尽量不要用,这个怎么解?”. 恰好前段时间写了一篇 <深入理解 Go def ...
- 执行kubectl create-f replicaset.yaml后k8s是如何工作的
参考:提高 kubectl 使用生产力[译] 英文原文:https://learnk8s.io/blog/kubectl-productivity/ Kubernetes 架构 Kubernetes ...
- Spring Security 解析(二) —— 认证过程
Spring Security 解析(二) -- 认证过程 在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把Spring Security .S ...