[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 Server 系统库的备份与恢复
master数据库 master作为数据库的主要数据库,记录着SQL Server系统的所有系统级信息,例如登录用户.系统配置设置.端点和凭证以及访问其他数据服务器所需要的信息.master数据库还记 ...
- go switch 和java C#有不同
1 switch 后的语句可以有简单的赋值语句 2 case :后的语句结束后不需要break;默认自动结束 除非以 fallthrough 语句结束,否则分支会自动终止 没有条件的 switch 有 ...
- VB.NET 读写XML
Public Class CSysXML Dim mXmlDoc As New System.Xml.XmlDocument Public XmlFile As String Public Sub N ...
- .net Aop 实现原理
本文实现所有继承BaseModel的类都通过代理拦截 using System; using System.Reflection; using System.Collections.Generic; ...
- [golang]按图片中心旋转后的新图左顶点和原图左顶点的偏移量计算
1 前言 略,作为记录使用 2 代码 /** * @Author: FB * @Description: * @File: RotateSample.go * @Version: 1.0.0 * @D ...
- RabbitMQ知识梳理
RabbitMQ 基本概念 交换机类型: RabbitMQ 运转流程: AMQP协议 入门使用 安装环境: 交换机和队列: 进阶使用 消息去从 消息确认投递 消息防止丢失 过期时间 (TTL) 消息分 ...
- Java自学-类和对象 方法重载
什么是Java 方法重载 方法的重载指的是方法名一样,但是参数类型不一样 步骤 1 : attack方法的重载 有一种英雄,叫做物理攻击英雄 ADHero 为ADHero 提供三种方法 public ...
- Java部分目录
一.Java基础 1.访问权限控制 2.重载和覆盖 3.面向对象的特征 4.接口和抽象类 5.Java环境变量配置 6.Java英文缩写详解 7.如何在Maven项目中引入自己的jar包 8.使用ba ...
- day 01 预科
目录 作业 二,Markdown基本语法 一级标题 二级标题 三级标题 作业 二,Markdown基本语法 标题 一级标题 二级标题 三级标题 四级标题 加粗 哦,更粗了 斜体 咦,我歪了 高亮 == ...
- gitlab中clone项目时,IP地址是一串数字
问题:docker安装后,clone显示ip是一串地址 解决(如果是非docker启动的,自然就是进入gitlab下): 1.进入docker 后台: docker exec -it gitlab / ...