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 (Java/Others)
Total Submission(s): 17917 Accepted Submission(s): 12558
"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!"
10
20
42
627
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)的更多相关文章
- HDU 1028 Ignatius and the Princess III:dp or 母函数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 题意: 给你一个正整数n,将n拆分成若干个正整数之和,问你有多少种方案. 注:"4 = ...
- hdu 1028 Ignatius and the Princess III 简单dp
题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...
- HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)
此题无法用JavaAC,不相信的可以去HD1029题试下! Problem Description "OK, you are not too bad, em- But you can nev ...
- Ignatius and the Princess III(母函数)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu1028(母函数+DP)
题目信息:求分解整数n的个数q(n);能够母函数或者DP http://acm.hdu.edu.cn/showproblem.php?pid=1028 AC代码: /***************** ...
- hdu 1028 Ignatius and the Princess III 母函数
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1028Ignatius and the Princess III(母函数简单题)
Ignatius and the Princess III Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- HDU1028Ignatius and the Princess III(母函数)
http://acm.hdu.edu.cn/showproblem.php?pid=1028 母函数: 例1:若有1克.2克.3克.4克的砝码各一 枚,能称出哪几种重量?各有几种可能方案? 如何解决这 ...
- hdoj 1028 Ignatius and the Princess III(区间dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 思路分析:该问题要求求出某个整数能够被划分为多少个整数之和(如 4 = 2 + 2, 4 = 2 ...
随机推荐
- YAML/Properties配置文件与Spring Boot(转)
多年来,Java开发人员依赖于属性文件或xml文件来指定应用程序配置.在企业应用程序中,人们可以为每个环境(如开发,分段和生产)创建单独的文件,以定义相应环境的属性.但是,通过Spring引导,我们可 ...
- 可恢复的安全rm
我们常常使用rm去删除一些文件.假设不小手一抖,那么就悲剧了.你们都懂的... 在经历过一次这种慘剧后.决定永远杜绝这种情况.重写写了shell函数.运行安全的rm.这个函数会把要删除的文件按日期备份 ...
- 从Linux 2.6.8内核的一个TSO/NAT bug引出的网络问题排查观点(附一个skb的优化点)
梦中没有错与对,梦中没有恨和悔...最好闭上你的嘴.这样才算可爱...我不会说:这不公道,我不能接受.我会用朴素的文字记录点点滴滴,早上4点多起来,一气呵成最近的收获与评价,愤慨与忏悔. 四年多前的一 ...
- nginx-tomcat负载均衡redis-session共享,静态资源分离
nginx-tomcat负载均衡redis-session共享.静态资源分离 基本环境: redis-2.8 apache-tomcat-6.0.41 nginx1.6.2 1.redis配置 1,配 ...
- C++学习之new与delete、malloc与free
在C/C++的面试时,对于new/delete和malloc/free这两对的使用和区别经常被考查到,如果这种基础的问题都答不上来,估计很难过面试了.这篇文章仅仅是浅显的讲一下,仅供参考. 一.new ...
- 【http代理报文】自己开发HTTP代理工具,不限语种。
我们知道通过修改浏览器设置可以使用代理访问网页,其实这个操作就是修改了每次的HTTP头. 工作中,我们难免需要通过TCP/IP协议发送HTTP报文来直接请求网页内容(比如爬虫工具),有同学问如何通过H ...
- 行政区划代码(SQL版本)2018年8月
表结构:(新建好表字段即可直接copy insert SQL语句) SQL语句: INSERT INTO z_regioncode(regioncode,regionname,pcode) VALUE ...
- sql server中数据约束相关的查询
根据表名查找数据约束 SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'CMS_EventLog'; SEL ...
- ubuntu 查看进程,查看服务
jiqing@Ubuntu:~$ sudo netstat -anpl | grep :3306 tcp6 0 0 :::3306 :::* LISTEN 1532/mysqld jiqing@Ubu ...
- SQL 字符串处理函数大全
select语句中只能使用sql函数对字段进行操作(链接sql server),select 字段1 from 表1 where 字段1.IndexOf("云")=1;这条语句不对 ...