我一直在纠结换零钱这一类型的题目,今天好好絮叨一下,可以说他是背包的应用,也可以说他是单纯的dp。暂且称他为dp吧。

先上一道模板题目。

sdut2777: 小P的故事——神奇的换零钱

题目描述

已知A国经济很落后,他们只有1、2、3元三种面值的硬币,有一天小P要去A国旅行,想换一些零钱,小P很想知道将钱N兑换成硬币有很多种兑法,但是可惜的是他的数学竟然是体育老师教的,所以他不会啊、、、他只好求助于你,你可以帮他解决吗?

提示:输入数据大于32000组。

输入

 每行只有一个正整数N,N小于32768。

输出

 对应每个输入,输出兑换方法数。

示例输入

100
1500

示例输出

884
188251
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <math.h>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int n,dp[],w[];
int main()
{
w[]=;
w[]=;
w[]=;
memset(dp,,sizeof(dp));
dp[]=;
for(int i=; i<=; i++)//依次添加每类货币
{
for(int j=i; j<=; j++)//枚举可使用第i类货币每一种可能的数和
{
dp[j]=dp[j]+dp[j-w[i]];// (累计前i-1类货币构成j-w[i]的方式数)
//dp[j]=dp[j]+dp[j-w[i]]的含义为:使用前i类货币时的货币组成可能可能的数(dp[j])=当使用前i-1类货币时的货币组成可能的数(dp[j])
//+选择使用一个i类货币+剩余钱数(j-w[i])组成的货币的数目(前i类)---这里有点背包的感觉,所以说他是背包的应用
}
}
while(scanf("%d",&n)!=EOF)
{
printf("%d\n",dp[n]);
}
return ;
}

POJ2229:

Description

Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7:

1) 1+1+1+1+1+1+1 
2) 1+1+1+1+1+2 
3) 1+1+1+2+2 
4) 1+1+1+4 
5) 1+2+2+2 
6) 1+2+4

Help FJ count all possible representations for a given integer N (1 <= N <= 1,000,000).

Input

A single line with a single integer, N.

Output

The number of ways to represent N as the indicated sum. Due to the potential huge size of this number, print only last 9 digits (in base 10 representation).

Sample Input

7

Sample Output

6
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <math.h>
#define inf 0x3f3f3f3f
typedef long long ll;
#define mod 1000000000
using namespace std;
int n,w[],tt,zan,l,dp[];
int main()
{
tt=;
w[tt++]=;
for(int i=;i<=;i++)
{
zan=pow(,i);
if(zan>) break;
w[tt++]=zan;
}
while(scanf("%d",&n)!=EOF)
{
memset(dp,,sizeof(dp));
dp[]=;
for(int i=;i<tt;i++)
{
if(dp[i]>n) break;
for(int j=w[i];j<=n;j++)
{
dp[j]=(dp[j]+dp[j-w[i]])%mod;
}
}
printf("%d\n",dp[n]);
}
return ;
}

子集和问题(应用--换零钱)POJ2229:Sumsets的更多相关文章

  1. DP优化与换零钱问题

    1 当贪心不再起效的时候 对于换零钱问题,最简单也是性能最好的方法就是贪心算法.可是贪心算法一定要满足面值相邻两个零钱至少为二倍关系的前提条件.例如1,2,5,10,20……这样的零钱组应用贪心最简单 ...

  2. SICP 换零钱的迭代版本

    看到换零钱方式统计这里, 书中给出了递归的实现但没有给出迭代版本说要留给读者作为挑战, 既然说是作为挑战那肯定是能解决的,在想了一天无果之后最终在别人博客的帮助下终于实现了迭代的版本...也算是经历坎 ...

  3. 小P的故事——神奇的换零钱&&人活着系列之平方数

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2777&cid=1219 这题不会,看了别人的代码 #include <iostre ...

  4. SDUT3145:Integer division 1(换零钱背包)

    题目:传送门 题目描述 整数划分是一个非常经典的数学问题. 所谓整数划分,是指把一个正整数n写成为n=m1+m2+...+mi的形式,其中mi为正整数,并且1<=mi<=n,此时,{m1, ...

  5. 51nod 1101 换零钱 【完全背包变形/无限件可取】

    1101 换零钱  基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 N元钱换为零钱,有多少不同的换法?币值包括1 2 5分,1 2 5角,1 2 5 ...

  6. 51 Nod 1101 换零钱(动态规划好题)

    1101 换零钱  基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 N元钱换为零钱,有多少不同的换法?币值包括1 2 5分,1 2 5角,1 2 5 ...

  7. 51nod 1101换零钱(背包)

    N元钱换为零钱,有多少不同的换法?币值包括1 2 5分,1 2 5角,1 2 5 10 20 50 100元.   例如:5分钱换为零钱,有以下4种换法: 1.5个1分 2.1个2分3个1分 3.2个 ...

  8. 51nod 1101 换零钱 (完全背包)

    N元钱换为零钱,有多少不同的换法?币值包括1 2 5分,1 2 5角,1 2 5 10 20 50 100元. 例如:5分钱换为零钱,有以下4种换法: 1.5个1分 2.1个2分3个1分 3.2个2分 ...

  9. [51nod1101]换零钱

    题意:给定钱,计算其能换成零钱的分类种数. 解题关键:完全背包计数. $dp[i][j]$表示前i个物品构成j元的种类数,然后优化一维. #include<bits/stdc++.h> u ...

随机推荐

  1. HeadFirst Jsp 09 (JSTL)

    JSTL (jsp standard tag library) 标准标记库 JSTL 安装, 注意你的每一个项目都需要一个 JSTL副本, 并把它放在WEB-INF/lib 目录下, 在 Tomcat ...

  2. imx lcd HV和DE模式转换

    有些时候拿到的lcd手册中关于芯片的时序使用的DE模式的,而imx6内核中使用的参数设置趋势HV模式,应此就需要将DE模式的参数转化为HV模式. 参考链接: https://community.nxp ...

  3. MySQL之explain 的type列 & Extra列

    explain 可以分析 select 语句的执行,即 MySQL 的“执行计划. 一.type 列   MySQL 在表里找到所需行的方式.包括(由左至右,由最差到最好): | All | inde ...

  4. UESTC 1511(差分约束)

    题目链接:http://acm.uestc.edu.cn/problem.php?pid=1511 思路:我们可以等到这样的5个关系式: k=1:dsit[a]-dist[b]>=0&& ...

  5. hdu 2809(状压dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2809 思路:简单的状压dp,看代码会更明白. #include<iostream> #in ...

  6. jquery获取设置input值

    $("#input").val("123"),注意val()这个函数$("#input").val("123"),//给 ...

  7. maven + hessian 简单样例

    项目结构例如以下: pom.xml 内容: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&quo ...

  8. Windows游戏编程大师技巧之三角形填充

    一.三角形的种类 三角形一般可以分为如下的四种类型(这四种类型是对于计算机来说的,不是数学意义上的分类): 平顶三角形:就是在计算机中显示的上面两个顶点的Y坐标相同. 平底三角形:就是在计算机中显示的 ...

  9. c#基础 第五讲

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  10. Assembly中Load, LoadFrom, LoadFile以及AppDomain, Activator类中相应函数的区别

    Assembly和AppDomain的一些关于动态加载程序集的函数有些令人头疼,但细细研究后还是可以将他们区分的. 这些函数大致可以分为四类: 第一类:加载到Load Context内 Load Co ...