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

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. Mysql 索引基础

    [1]什么是索引?为什么要建立索引? 索引,其实就是目录. 索引,用于快速找出在某个列中有某个特定值的行. 不使用索引,MySQL必须从第一条记录开始查找整张表,直到找出相关的行,那么表越大,查询数据 ...

  2. CLRS最大子数组问题

    今天我们一起来看一下关于最大子数组的一些问题.最大子数组的应用场景可以是这样的:有一天,你搞了一场投资开始炒股,这时你就会想,我怎样才能获得最大的利润呢,最简单的想法就是我在股票的最低价时买入,然后在 ...

  3. golang ---JSON-ITERATOR 使用

    jsoniter ( json-iterator )是一款快且灵活的 JSON 解析器 Jsoniter 是最快的 JSON 解析器.它最多能比普通的解析器快 10 倍之多, 独特的 iterator ...

  4. Visual Studio 2019 安装

    目录 写在前面 官网下载 安装 等待安装 启动 写在前面 目前工作的开发环境还是旧版本的Visual Studio 2013版.个人感觉还是有点跟不上时代更新迭代的节奏了.毕竟,技术在进步.如果我们也 ...

  5. python3 语法 数据类型

     python3中 有6种标准数据类型 数字,字符串,列表,元祖,集合,字典

  6. Excel 97-2003版本内部结构与查看方法

    MS-XLS内部结构:BIFF格式 https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/cd03cb5f-ca0 ...

  7. docker实战 (3) 常规配置

    本节会持续更新,在项目实战中遇到的docker配置都会更新进来 docker常用命令: docker 介绍: what: 是什么 why: 为什么用 how: 怎么用 docker 特点: 轻量级,可 ...

  8. composer的用法笔记

    一.到compose官网下载 composer.exe 的安装的文件,直接打开安装,在安装的目录的要选择到,你的开发环境中的 php.exe 的所在目录里..例如:D:\phpStudy\php\ph ...

  9. 【HICP Gauss】数据库 数据库管理(连接方式 会话模式 存储表空间)-6

    数据库连接方式:驱动连接和客户端连接 驱动连接 : JDBC GSC ODBC 客户端连接 zsql工具 zsql / as sysdba -q #管理员身份登陆 zsql omm/ - #普通身份登 ...

  10. Python Image库简单处理图像

    直接列举几个常用的函数,可在 http://effbot.org/imagingbook/image.htm 中查看更多相关函数. from PIL import Image import numpy ...