整数的有序拆分就是隔板法,无序拆分则有两种处理方法

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=1n​P(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 + 生成函数)的更多相关文章

  1. 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 ...

  2. 题解报告: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 ...

  3. Ignatius and the Princess III HDU - 1028 || 整数拆分,母函数

    Ignatius and the Princess III HDU - 1028 整数划分问题 假的dp(复杂度不对) #include<cstdio> #include<cstri ...

  4. HDU 1028 整数拆分问题 Ignatius and the Princess III

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  5. HDU1028 Ignatius and the Princess III 【母函数模板题】

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  6. hdu 1028 Ignatius and the Princess III 母函数

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  7. 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 ...

  8. 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 ...

  9. hdu 1028 Ignatius and the Princess III 简单dp

    题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...

随机推荐

  1. java之mybatis之字段映射及多对一

    1. 数据库中表的列名和实体类的属性名称不一致. 可以使用 resultMap来解决. <select id="findAll" resultMap="UserMa ...

  2. Java——简单实现学生管理系统

    import java.io.*;import java.util.ArrayList;import java.util.Scanner;class MyObjectOutputStream exte ...

  3. c# 基于WebApi的快速开发框架FastFramework

    一.框架简介 此框架是针对于webapi进行开发,项目分层是基于ABP框架的分层,更好的抽离业务逻辑关系,ABP是基于EF做数据访问层,本人个人比较喜欢Dapper,就把数据访问层封装成了Dapper ...

  4. 换个语言学一下 Golang (8)——指针

    定义 所谓指针其实你可以把它想像成一个箭头,这个箭头指向(存储)一个变量的地址. 因为这个箭头本身也需要变量来存储,所以也叫做指针变量. Go的指针不支持那些乱七八糟的指针移位.它就表示一个变量的地址 ...

  5. 基于SVM的道路简单分割

    折腾了几天了,这个看似简单的东西,怎么做起来那么费劲啊? 任重而道远,光玩,光去幻想,是什么也做不出来的,要一点一点儿大量时间与精力的投入,才能出结果的. (点击下图,可选择原图观看,清晰的效果) 2 ...

  6. Oracle大表改为分区表及表空间切换方案

    Oracle大表改为分区表及表空间切换方案 一.            背景 由于之前数据库表和索引放在一个表空间导致表空间数据文件增长太快,文件数量即将达到Oracle表空间的限制,需要对表(没有分 ...

  7. Python3链接Oracle

    1. 说明 本篇主要参见与cx_Oracle安装 全部操作均在root用户下完成 2. 下载Oracle Instant Client客户端 依据系统,在Oracle Instant Client下载 ...

  8. Class.getDeclaredFields()和Class.getFields()的区别。 Class.getMethods()和Class.getDeclaredMethods()的区别。

    package www.cn.extend; /** Animal * 2019/07/04 * @author Administrator * */ public class Animal { pu ...

  9. 【Mybatis异常】Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

    一.错误原因分析 从错误提示可以看出:实际传入的参数大于sql中待设置的参数,也就是sql中的?少于参数或?根本没有产生原因:  ?号被单引号包围 如: sql += " and artic ...

  10. 如何临时修改 macOS 应用的界面语言?

    一般情况下,应用程序的界面语言会和系统语言保持一致.但有些时候,我们也会希望临时换用一种不同的界面语言.例如,一些程序的中文翻译词不达意,有必要参考英文界面来确定功能的准确含义:又如,一些网页会强制按 ...