Ignatius and the Princess III

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17917    Accepted Submission(s): 12558

Problem Description
"Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.

"The second problem is, given an positive integer N, we define an equation like this:
  N=a[1]+a[2]+a[3]+...+a[m];
  a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
  4 = 4;
  4 = 3 + 1;
  4 = 2 + 2;
  4 = 2 + 1 + 1;
  4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"

 
Input
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.
 
Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.
 
Sample Input
4
10
20
 
Sample Output
5
42
627
 
看了题解,学了两种方法。一种是母函数,一种是dp。
母函数:组合数学方法,第一次接触。
  此题构造的母函数(1+x^1+x^2+x^3...+x^n)(1+x^2+x^4+x^6...+x^2n).....
  第一项表示(0个1,1个1,2个1,3个1...),第二项表示(0个2,1个2,2个2,3个2,4个2...)以此类推。
  展开后,每一项的指数表示划分的这个数,系数表示该数的划分数。
import java.util.*;
import java.io.*; public class Main { public static int cal(int n)
{
int c1[]=new int [n+1];
int c2[]=new int [n+1];
for(int i=0;i<=n;i++)
{
c1[i]=1;
c2[i]=0;
}
for(int i=2;i<=n;i++)
{
for(int j=0;j<=n;j++)
for(int k=0;k+j<=n;k+=i)
c2[j+k]+=c1[j];
for(int j=0;j<=n;j++)
{
c1[j]=c2[j];
c2[j]=0;
}
}
return c1[n];
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n;
while(in.hasNext())
{
n=in.nextInt();
System.out.println(cal(n));
}
} }

dp:

dp[i][j]表示i这个数划分为最大加数不超过j的划分数。

if(i>j)  dp[i][j]=dp[i][j-1]+dp[i-j][j];

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

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

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std; int dp[][]; int main()
{
int n,m;
//dp[1][1]=1;
for(int i=; i<=; i++)
for(int j=; j<=; j++)
{
if(j==)
dp[i][j]=;
else if(i==j)
dp[i][j]=dp[i][j-]+;
else if(i>j)
dp[i][j]=dp[i][j-]+dp[i-j][j];
else if(i<j)
dp[i][j]=dp[i][i];
}
while(scanf("%d",&n)!=EOF)
{
printf("%d\n",dp[n][n]);
} return ;
}

HDU_1028_Ignatius and the Princess III_(母函数,dp)的更多相关文章

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

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

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

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

  3. HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)

    此题无法用JavaAC,不相信的可以去HD1029题试下! Problem Description "OK, you are not too bad, em- But you can nev ...

  4. Ignatius and the Princess III(母函数)

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

  5. hdu1028(母函数+DP)

    题目信息:求分解整数n的个数q(n);能够母函数或者DP http://acm.hdu.edu.cn/showproblem.php?pid=1028 AC代码: /***************** ...

  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 1028Ignatius and the Princess III(母函数简单题)

     Ignatius and the Princess III Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  8. HDU1028Ignatius and the Princess III(母函数)

    http://acm.hdu.edu.cn/showproblem.php?pid=1028 母函数: 例1:若有1克.2克.3克.4克的砝码各一 枚,能称出哪几种重量?各有几种可能方案? 如何解决这 ...

  9. hdoj 1028 Ignatius and the Princess III(区间dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 思路分析:该问题要求求出某个整数能够被划分为多少个整数之和(如 4 = 2 + 2, 4 = 2 ...

随机推荐

  1. DELPHI新版本WEBSERVICE的变化

    DELPHI新版本WEBSERVICE,不仅可以编译成ISAPI DLL,依靠IIS部署, 并且还可以编译成单独的EXE,不再依赖IIS就可以独立运行,这一点未尝不可以说是非常方便的改进.

  2. 开源软件Review Board

    开源软件, Review Board 代码审查的. https://www.reviewboard.org/

  3. Android MTP 文件浏览Demo

    本apk实现了MTP文件浏览的简单功能. 通过Demo apk能够浏览连接到当前设备上的MTP设备上的文件. Demo路径:http://download.csdn.net/detail/sailin ...

  4. STL源代码剖析(二) - 迭代器与traits技法

    提要 先看一段用迭代器的代码: int a[] = {1, 2, 3, 4, 5}; vector<int> v1( a, a+5); vector<int>::iterato ...

  5. 到底什么是RPC?

    RPC:远程过程调用,是一种同意分布式应用程序调用网络上不同计算机的可用服务的机制.RPC服务会在注冊表中给自己注冊一个UUID,成为通用唯一标识符.这个UUID针对每一项服务都是一个唯一的值,且在全 ...

  6. JMeter—丰富报表功能PerfMon插件

    可能有童鞋不知道PerfMon插件是干啥的.这里简要说一下: 在做负载測试时,我们要时刻关注server的CPU.MEM--的使用情况,可是JMeter本身对这些信息是不做收集的,这个时候PerfMo ...

  7. Python图像处理库:PIL中Image,ImageDraw等基本模块介绍

    Python图像处理库:PIL中Image,ImageDraw等基本模块介绍 标签: 图像处理PILPYTHON 2016-08-19 10:58 461人阅读 评论(0) 收藏 举报  分类: 其他 ...

  8. Linux对外连接port数限制

    左右时,開始大量抛例如以下异常: java.net.BindException:Cannot assign requested address atsun.nio.ch.Net.connect0(Na ...

  9. go语言笔记——go环境变量goroot是安装了路径和gopath是三方包路径

    Go 环境变量 Go 开发环境依赖于一些操作系统环境变量,你最好在安装 Go 之间就已经设置好他们.如果你使用的是 Windows 的话,你完全不用进行手动设置,Go 将被默认安装在目录 c:/go  ...

  10. openstack封装待调试