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. ExtJs--09--javascript对象的方法的3种写法 prototype通过原型设置方法效率最好

    /** * javascript对象的方法的3种写法 推荐第三种 运行效率最好 */ function P(name , age){ this.name = name ; this.age = age ...

  2. 猫猫学iOS 之CoreLocation反地理编码小Demo输入经纬度得到城市

    猫猫分享,必须精品 原创文章,欢迎转载.转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 输入经纬度,能够得到相应的地名 二:思路 跟地里编码差 ...

  3. 传智播客JDBC视频教程

    视频介绍: 一些视频教程通过浅显案例来让刚開始学习的人感到轻松,可是课程中编写的代码不能直接应用于项目中:而本套视频教程正好相反,视频解说者李勇老师以技术见长.性格朴实无华.不善于幽默搞笑.李勇老师编 ...

  4. Python3基础(十一) 类的拓展

    在类的初印象中,我们已经简单的介绍了类,包括类的定义.类对象和实例对象.本文将进一步学习类的继承.迭代器.发生器等等. 一.类的继承 单继承 派生类的定义如下: class DerivedClassN ...

  5. Linux C 网络编程——多线程的聊天室实现(server端)

    server端的主要功能: 实现多用户群体聊天功能(此程序最多设定为10人.可进行更改),每一个人所发送的消息其它用户均能够收到.用户能够任意的增加或退出(推出以字符串"bye"实 ...

  6. 递归,迭代,堆栈三种方式实现单链表反转(C++)

    #author by changingivan# 2016.04.12#include <iostream> #include <stack> using namespace ...

  7. 【转】Android Fragment中使用SurfaceView切换时闪一下黑屏的解决办法

    重构了下之前自己的一个新闻客户端,全部使用了Fragment来进行页面切换,只有一个入口Activity作为程序的启动Activity,其中有一个界面需要调用摄像头识别二维码, 于是就会用到Surfa ...

  8. SQL SERVER 语句大全

    ·SQL的简单查询实例教程关键词:SQL语句大全 中文网 整理编辑,经典SQL语句大全(SQL语句大总结),欢迎网友投稿 下列语句部分是Mssql语句,不可以在access中使用.SQL分类:DDL— ...

  9. SQL 字符串处理函数大全

    select语句中只能使用sql函数对字段进行操作(链接sql server),select 字段1 from 表1 where 字段1.IndexOf("云")=1;这条语句不对 ...

  10. struts2里result类型Stream的参数配置

    转自:https://blog.csdn.net/q714699280/article/details/51756126 contentType 内容类型,和互联网MIME标准中的规定类型一致,例如t ...