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的硬币凑出面值j的方案种数是相同的。那么如果使用了面值j,对应方案种数就应该加上dp[i-j][j],如果不使用面值j,对应的方案种数就应该加上dp[i][j-1]。状态转移方程为dp[i][j]=dp[i?j][j]+dp[i][j?1]。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
int n, a, b;
ll dp[305][305];
void init() {
    for (int i = 0; i <= 300; i++) {
        if (!i) dp[i][0] = 1;
        else dp[i][0] = 0;
        for (int j = 1; j <= 300; j++) {
            if (i >= j) {
                dp[i][j] = dp[i - j][j] + dp[i][j - 1];
            } else {
                dp[i][j] = dp[i][j - 1];
            }
        }
    }
}
int main() {
    init();
    while (scanf("%d", &n) != EOF) {
        char temp;
        a = 1, b = n;
        scanf("%c", &temp);
        int flag = 0;
        if (temp == ‘ ‘) {
            flag = 1;
            scanf("%d", &a);
            scanf("%c", &temp);
            if (temp == ‘ ‘) {
                flag = 2;
                scanf("%d", &b);
            }
        }
        if (a > 300) a = 300;
        if (b > 300) b = 300;
        if (!flag) {
            printf("%lld\n", dp[n][n]);
        } else if (flag == 1) {
            printf("%lld\n", dp[n][a]);
        } else if (flag == 2) {
            if (!a) {
                printf("%lld\n", dp[n][b]);
            } else printf("%lld\n", dp[n][b] - dp[n][a - 1]);
        }
    }
    return 0;
}
时间: 2024-10-10 10:50:35

uva 10313 Pay the Price (DP)的相关文章

uva 10635 Prince and Princess(DP)

uva 10635 Prince and Princess(DP) In an n x n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbered 1, 2, 3 ... n*n, as shown below: Prince stands in square 1, make p jumps and finally reach square n*n. He enters a

UVA 10564-Paths through the Hourglass(DP)

题目大意:给出一个沙漏,包含一个倒三角和一个三角,每个方格有一个0到9的数字,从顶上某格走到底下某格,求得到一个特殊值的路径有多少条,并输出字典序最小的.路径用一个起点和一系列'L'和'R'的字符表示. 用a[i][j]表示第i行第j列的数字.由于要求字典序最小的,所以从下往上递推.用d[i][j][u]表示在(i,j)且和为u的路径有多少条,在上半部分是倒三角,可以由d[i+1][j][u-a[i][j]]和d[i+1][j+1][u-a[i][j]]相加递推而来,在下半部分是正三角,可以由d

uva 10401 Injured Queen Problem(DP)

uva 10401 Injured Queen Problem 题目大意:这是一个变形的N皇后问题,皇后不再是占据一行一列以及斜线,她占据的只是她周围的一圈以及她所在的一列.题目给出一个含有问号,数字和字母的字符串.第i个字符是问号代表皇后在第i列的任意一行,若第i个字符是数字或字母X(1-F)代表皇后在第i列的X行.求满足该字符串的摆放方式的方法一共有几种. 解题思路:从第一列开始往后递推.dp[i][j]表示的是结合j - 1列的摆放方式,将皇后放在(i, j)的位置会有几种情况. #inc

Uva 437-The Tower of Babylon(DP)

题目链接:点击打开链接 DAG上最长路. 题意:给出n种长方体,(每种无限),要求能摞的最大高度.连边是大的连小的. #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <string> #include <cctype> #include <vector> #include <cstdio&g

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> #

UVA 10891 Game of Sum(DP)

This is a two player game. Initially there are n integer numbers in an array and players A and B get chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot take from both ends at

uva 507 Jill Rides Again (DP)

uva 507 Jill Rides Again Jill likes to ride her bicycle, but since the pretty city of Greenhills where she lives has grown, Jill often uses the excellent public bus system for part of her journey. She has a folding bicycle which she carries with her

uva 10739 String to Palindrome (dp)

uva 10739 String to Palindrome In this problem you are asked to convert a string into a palindrome with minimum number of operations. The operations are described below: Here you'd have the ultimate freedom. You are allowed to: Add any character at a

uva 11584 Partitioning by Palindromes(dp)

题目链接 题意:给定一个字符串,分解成多个子串,每个子串都是回文串,问最少能分成多少个子串. 题解: dp[i]表示前i个字符串分割成最少回文子串的数量: 0<=j<=i;如果字符串从j到i是回文串,那么dp[i]=min(dp[i],dp[j-1]+1); #include <iostream> using namespace std; int dp[1005]; string s; bool ok(int j,int i) { while(j<=i) { if(s[j]!