题目信息:求分解整数n的个数q(n);能够母函数或者DP

http://acm.hdu.edu.cn/showproblem.php?pid=1028

AC代码:

/******************************

题目大意:求分解整数n的个数q(n)

例:

5 = 5;

5 = 4 + 1;

5 = 3 + 1 + 1;

5 = 3 + 2;

5 = 2 + 2 + 1;

5 = 2 + 1 + 1 + 1;

5 = 1 + 1 + 1 + 1 + 1;

sum(5) = 7;不区分顺序,

(3+2)与(2+3)为同一个

*******************************/

/**

 *DP

 */

#include<iostream>

using namespace std;

int a[121][121];//储存数组

long long q(int n,int m)

{//递归分析,m为分解的最大值

    if((n<1)||(m<1)) return 0;

    if((n==1)||(m==1)) return 1;

    if(n<m) return q(n,n);

    if(n==m) return q(n,m-1)+1;

    return q(n,m-1)+q(n-m,m);

}

/**

int main()

{

    for(int i=1;i<=120;i++){

        for(int j=1;j<=120;j++){//由递归式转存到数组中。降低计算量

            if((i==1)||(j==1)) a[i][j]=1;

            else if(i<j) a[i][j]=a[i][i];

            else if(i==j) a[i][j]=a[i][j-1]+1;

            else a[i][j]=a[i][j-1]+a[i-j][j];

        }

    }

    int n;

    while(cin>>n){

        cout<<a[n][n]<<endl;

    }

    return 0;

}**/

/**

 *母函数解法

 */

int main()

{

    int a[350],b[350],i,j,k,n;

    while(cin>>n&&n)

    {

        for(i=0;i<=n;i++){

            a[i]=1;

            b[i]=0;

        }

        for(i=2;i<=n;i++){

            for(j=0;j<=n;j++)

                for(k=0;k+j<=n;k+=i)

                    b[k+j]+=a[j];

            for(j=0;j<=n;j++){

                a[j]=b[j];

                b[j]=0;

            }

        }

        cout<<a[n]<<endl;

    }

    return 0;

}

hdu1028(母函数+DP)的更多相关文章

  1. hdu刷题2

    hdu1021 给n,看费波纳列数能否被3整除 算是找规律吧,以后碰到这种题就打打表找找规律吧 #include <stdio.h> int main(void) { int n; whi ...

  2. HDU 1398 Square Coins(母函数或dp)

    Square Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  3. HDU 1028 Ignatius and the Princess III:dp or 母函数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 题意: 给你一个正整数n,将n拆分成若干个正整数之和,问你有多少种方案. 注:"4 = ...

  4. hdu 1284 钱币兑换问题 (递推 || DP || 母函数)

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. E比昨天更多的棒棒糖(Easy+Hrad)(华师网络赛)(DP||母函数||背包优化)

    Time limit per test: 2.0 seconds Memory limit: 512 megabytes 唐纳德先生的某女性朋友最近与唐纳德先生同居.该女性朋友携带一 baby.该 b ...

  6. HihoCoder1339 Dice Possibility(概率DP+母函数)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 What is possibility of rolling N dice and the sum of the numb ...

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

  8. 题解报告:hdu 1398 Square Coins(母函数或dp)

    Problem Description People in Silverland use square coins. Not only they have square shapes but also ...

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

随机推荐

  1. java面试题之Thread类中的start()和run()方法有什么区别

    start()方法被用来启动新创建的线程,而且start()内部调用了run()方法, 区别: 当你调用run()方法的时候,只会是在原来的线程中调用,没有新的线程启动: start()方法才会启动新 ...

  2. bzoj[Usaco2008 Nov]mixup2 混乱的奶牛 状压dp

    [Usaco2008 Nov]mixup2 混乱的奶牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1204  Solved: 698[Submit ...

  3. 标准C程序设计七---112

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  4. mysql 新增用户并授权

    grant all privileges on *.* to 'root'@‘%’ identified by '123456'; *.* 表示所有资源. 刷新权限 flush privileges;

  5. js-随机生成16进制颜色

    <body onload="color()"></body> <script> function color(){ 方法一: document. ...

  6. 作列表排列时div的table属性应用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. [原创][SW]一些实用软件的小tips(长期更新)

    0. 简介 生活中我们经常使用许多的小工具或软件,来提高我们的工作效率,比如UltraEdit.Notepad++等.本文主要做一些记录,目的呢就是防止自己遗忘或者是快速的查询,来源是自己的摸索和网络 ...

  8. [转载][FPGA]有限状态机FSM学习笔记(二)

    1. Mealy和Moore状态机的互换 对于给定的时序逻辑功能,可以用Mealy机实现,也可以用Moore机实现.根据Moore机比Mealy机输出落后一个周期的特性,可以实现两种状态机之间的转换. ...

  9. F#周报2019年第21期

    新闻 F#在GitHub上的开发仓库现在变为dotnet/fsharp Ionide 4.0路线图 Fable的五月公告 Visual Studio 2019版本16.1 WinUI 3.0路线图 欢 ...

  10. IDEA连接linux服务器

    idea连接linux(完成了xshell和xftp连接linux的功能,可以直接卸载这俩了..) File->settings->Deployment左侧加号添加 选择传输类型ftp或者 ...