UVa 10313 - Pay the Price

题目:求一个整数的重复拆分,限制拆分数的个数。

分析:dp,二维多重背包。整数拆分就用背包。

状态:设f(i,j)为j拆分成i个元素的拆法;

转移:f(i,j)= sum(f(i-1,j-k),f(i-1,j-2k),...,f(i-1,j-mk)){ 其中,1 ≤ k ≤ j };

因为输入格式WA好多次,强大的sscanf( ⊙ o ⊙ )啊!

说明:注意数据范围,使用long long防止溢出。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

long long F[303][303],S[303][303];

int main()
{
	for (int i = 0 ; i <= 300 ; ++ i)
	for (int j = 0 ; j <= 300 ; ++ j)
		F[i][j] = S[i][j] = 0LL;
	F[0][0] = 1LL;
	for (int i = 1 ; i <= 300 ; ++ i)
	for (int j = 1 ; j <= 300 ; ++ j)
	for (int k = i ; k <= 300 ; ++ k)
		F[j][k] += F[j-1][k-i];
	S[0][0] = 1LL;
	for (int i = 1 ; i <= 300 ; ++ i)
	for (int j = 0 ; j <= 300 ; ++ j)
		S[i][j] = S[i-1][j]+F[i][j];

	int  N,L1,L2;
	char buf[256];
	while (gets(buf)) {
		int n = sscanf(buf,"%d%d%d",&N,&L1,&L2);
		if (n > 1) {
			if (L1 > 300) L1 = 300;
			if (n > 2) {
				if (L2 > 300) L2 = 300;
				if (L1 > L2) cout << 0 << endl;
				else if (L1)
					cout << S[L2][N] - S[L1-1][N] << endl;
				else cout << S[L2][N] << endl;
			}else cout << S[L1][N] << endl;
		}else cout << S[N][N] << endl;
	}
	return 0;
}
时间: 2024-10-11 11:42:22

UVa 10313 - Pay the Price的相关文章

uva 10313 Pay the Price (DP)

uva 10313 Pay the Price 题目大意:现在有300种面额的硬币(1~300),给出一个金额数,问这300种面额的硬币组成该金额数的方式有多少种.注意:该题的输入有三种模式,1个数n:n为金额数:2个数n, a:n为金额数,a为硬币个数上限:3个数n, a,b:n为金额数,a b为硬币个数的下限和上限. 解题思路:dp[i][j]表示面额i的金额,在硬币数不超过j的情况下,有几种组成方式.这个题目涉及到一个结论,用不超过i个硬币凑出面值j的方案种数,是和用面值不超过i的硬币凑出

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

算法入门经典大赛 Dynamic Programming

111 - History Grading LCS 103 - Stacking Boxes 最多能叠多少个box DAG最长路 10405 - Longest Common Subsequence LCS 674 - Coin Change 全然背包求方案数 10003  - Cutting Sticks 区间DP dp[l][r]代表分割l到r的最小费用 116 - Unidirectional TSP 简单递推 输出字典序最小解 从后往前推 10131 - Is Bigger Smarte

小白书关于动态规划

10192 最长公共子序列 http://uva.onlinejudge.org/index.php?option=com_onlinejudge& Itemid=8&page=show_problem&category=114&problem=1133&mosmsg= Submission+received+with+ID+13297616 */ #include <cstdio> #include <string.h> #include&

Understand the Qt containers(有对应表)

Container classes are one of the cornerstones of object-oriented programming, invaluable tools that free us from having to permanently think about memory management. Qt comes with its own set of container classes, closely modeled after those in the S

Columbus’s bargain (hdu 3268 最短路)

Columbus's bargain Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1836    Accepted Submission(s): 467 Problem Description On the evening of 3 August 1492, Christopher Columbus departed from Pa

Charlie&#39;s Change(完全背包+路径记忆)

Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3176   Accepted: 913 Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motores

Columbus’s bargain

Columbus’s bargain Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1721    Accepted Submission(s): 431 Problem Description On the evening of 3 August 1492, Christopher Columbus departed from Pal

聊聊Javascript中的AOP编程

Duck punch 我们先不谈AOP编程,先从duck punch编程谈起. 如果你去wikipedia中查找duck punch,你查阅到的应该是monkey patch这个词条.根据解释,Monkey patch这个词来源于 guerrilla patch,意为在运行中悄悄的改变代码,而 guerrilla 这个词与 gorilla 同音,而后者意又与monkey相近(前者为“猩猩”的意思),最后就演变为了monkey patch. 如果你没有听说过duck punch,但你或许听说过du