hdu 2069 普通母函数 +dp

2023-10-23 15:38
文章标签 函数 dp hdu 普通 2069

本文主要是介绍hdu 2069 普通母函数 +dp,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目传送门:点击打开链接

一开始做题并没有想到Your program should be able to handle up to 100 coins.这句话什么意思,也没有当回事。。但是wa几次,我就感觉只是套模版应该是不对的,但是又找不到,后来我在论坛上看到写着 总的硬币个数不能超过100,才知道有条件限制。但是接下来该怎么利用这条件呢?母函数模版只是能得出最后的结果,并不能求出m个硬币凑成的这个n价值的方法数,所以我们利用母函数的求解问题的思路,用二维数组c1[][]来代替c1[],c1[i][j]  i其实与模版中的i含义一样(表示凑成i的价值),那么j 代表用了j个硬币凑成i  所以c1[i][j]代表 j个硬币凑成i的方法个数。所以在求的价值为n的时候,我们只要判断硬币个数是否大与100就可以了。这不懂的话可以看一下代码。。(注释的不用看)后面的注释为个别数据。

#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
int f[]={0,1,5,10,25,50};
int c1[300][110],c2[300][110];
int n;
void solve()
{memset(c1,0,sizeof(c1));memcpy(&c2,&c1,sizeof(c1));c1[0][0]=1;for(int i=1;i<=n&&i<=100;++i)c1[i][i]=1;for(int i=2;i<=5;++i){for(int j=0;j<=n;++j){//if(c1[j][0])//{for(int k=0;k+j<=n;k+=f[i]){for(int kk=0;k/f[i]+kk<=100;++kk)//判断条件{if(c1[j][kk]){c2[j+k][kk+k/f[i]]+=c1[j][kk];//if(j+k==115)//  cout<<j<<" "<<kk<<' '<<c1[j][kk]<<endl;}}}}memcpy(&c1,&c2,sizeof(c2));memset(c2,0,sizeof(c2));}int ans=0;for(int i=0;i<=100;++i)ans+=c1[n][i];cout<<ans<<endl;// cout<<c1[n][0]<<endl;
}
int main(int argc, const char * argv[])
{while(cin>>n){solve();}return 0;
}
//n      ans
//0      1
//250   3830//110   380//115   430//100   292//101   291

Coin Change

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18203    Accepted Submission(s): 6268


Problem Description
Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.

For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, or two 5-cent coins and one 1-cent coin, or one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.

Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 100 coins.

Input
The input file contains any number of lines, each one consisting of a number ( ≤250 ) for the amount of money in cents.

Output
For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.

Sample Input
  
11 26

Sample Output
  
4 13


这篇关于hdu 2069 普通母函数 +dp的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/268772

相关文章

hdu4826(三维DP)

这是一个百度之星的资格赛第四题 题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1004&cid=500 题意:从左上角的点到右上角的点,每个点只能走一遍,走的方向有三个:向上,向下,向右,求最大值。 咋一看像搜索题,先暴搜,TLE,然后剪枝,还是TLE.然后我就改方法,用DP来做,这题和普通dp相比,多个个向上

hdu1011(背包树形DP)

没有完全理解这题, m个人,攻打一个map,map的入口是1,在攻打某个结点之前要先攻打其他一个结点 dp[i][j]表示m个人攻打以第i个结点为根节点的子树得到的最优解 状态转移dp[i][ j ] = max(dp[i][j], dp[i][k]+dp[t][j-k]),其中t是i结点的子节点 代码如下: #include<iostream>#include<algorithm

hdu1171(母函数或多重背包)

题意:把物品分成两份,使得价值最接近 可以用背包,或者是母函数来解,母函数(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v) 其中指数为价值,每一项的数目为(该物品数+1)个 代码如下: #include<iostream>#include<algorithm>

hdu4865(概率DP)

题意:已知前一天和今天的天气概率,某天的天气概率和叶子的潮湿程度的概率,n天叶子的湿度,求n天最有可能的天气情况。 思路:概率DP,dp[i][j]表示第i天天气为j的概率,状态转移如下:dp[i][j] = max(dp[i][j, dp[i-1][k]*table2[k][j]*table1[j][col] )  代码如下: #include <stdio.h>#include

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

usaco 1.1 Broken Necklace(DP)

直接上代码 接触的第一道dp ps.大概的思路就是 先从左往右用一个数组在每个点记下蓝或黑的个数 再从右到左算一遍 最后取出最大的即可 核心语句在于: 如果 str[i] = 'r'  ,   rl[i]=rl[i-1]+1, bl[i]=0 如果 str[i] = 'b' ,  bl[i]=bl[i-1]+1, rl[i]=0 如果 str[i] = 'w',  bl[i]=b

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :