题目信息:求分解整数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. 百度网络监控实战:NetRadar横空出世(下)

    原文:https://mp.weixin.qq.com/s/CvCs-6rX8Lb5vSTSjYQaBg 转自订阅号「AIOps智能运维」,已授权运维帮转发 作者简介:运小贝,百度高级研发工程师 负责 ...

  2. STL学习笔记(七) 程序中使用STL

    条款43:算法调用优先于手写循环 class Widget { public: bool test(); }; vector<Widget> vec; 算法调用: for_each(vec ...

  3. javascript事件捕获机制,dom tree

    $(document,"a").on("click",function(){alert(2);return false;}); $("<a> ...

  4. linux-3.2.36内核启动2-setup_arch中的内存初始化1(arm平台 分析高端内存和初始化memblock)【转】

    转自:http://blog.csdn.net/tommy_wxie/article/details/17093307 上一篇微博留下了这几个函数,现在我们来分析它们         sanity_c ...

  5. js-classList修改class属性

    classList定义与用法 1)classList属性返回元素的类名,作为DOMTokenList对象 2)该属性用于在元素中添加,移除及切换css类 3)classList属性是只读的,但可以用a ...

  6. 天梯赛 - L2-002 链表去重

    GG思密达,第二个测试点的三分怎么也拿不上,我还是比较熟悉指针,用指针来写~,写完去上概率论 题目链接:https://www.patest.cn/contests/gplt/L2-002 #incl ...

  7. 微信小程序 赋值问题

    通常我们在页面跳转传递过来的参数要用到页面渲染时或是请求接口回来的数据要用到页面渲染时 对page的data赋值可不能用简单的变量赋值,要用微信小微信专有的this.setData方法 Page({ ...

  8. 树讲解——紧急集合(lca)

    大视野    1787: [Ahoi2008]Meet 紧急集合 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3067  Solved: 1365[ ...

  9. 第2章 Spring Boot 文档

    Spring Boot 文档 本节简要介绍了Spring Boot文档,是整个文档的参考指南. 您可以完整阅读本参考指南,或者如果您不感兴趣的话可以跳过该部分. 1. 关于文档 Spring Boot ...

  10. SVG动画基础篇

    参考资料: http://www.w3school.com.cn/svg/index.asp https://msdn.microsoft.com/zh-cn/library/gg193979  gi ...