zoj Exchange Cards
来源:互联网 发布:宁夏中卫云计算基地 编辑:程序博客网 时间:2024/05/22 07:40
As a basketball fan, Mike is also fond of collecting basketball player cards. But as a student, he can not always get the money to buy new cards, so sometimes he will exchange with his friends for cards he likes. Of course, different cards have different value, and Mike must use cards he owns to get the new one. For example, to get a card of value 10$, he can use two 5$ cards or three 3$ cards plus one 1$ card, depending on the kinds of cards he have and the number of each kind of card. And Sometimes he will involve unfortunately in a bad condition that he has not got the exact value of the card he is looking for (fans always exchange cards for equivalent value).
Here comes the problem, given the card value he plans to get and the cards he has, Mike wants to fix how many ways he can get it. So it's you task to write a program to figure it out.
Input
The problem consists of multiple test cases, terminated by EOF. There's a blank line between two inputs.
The first line of each test case gives n, the value of the card Mike plans to get andm, the number of different kinds of cards Mike has. n will be an integer number between 1 and 1000.m will be an integer number between 1 and 10.
The next m lines give the information of different kinds of cards Mike have. Each line contains two integers,val and num, representing the value of this kind of card, and the number of this kind of card Mike have.
Note: different kinds of cards will have different value, eachval and num will be an integer greater than zero.
Output
For each test case, output in one line the number of different ways Mike could exchange for the card he wants. You can be sure that the output will fall into an integer value.
Output a blank line between two test cases.
Sample Input
5 22 13 110 510 27 25 32 21 5
Sample Output
17
多重背包 dfs解法~
数据比较小~~~~~
在寻找方向的时候纠结了一下,因为多重背包是有多个相同的值的,为了能再次访问,需要定义一个m 来作为向下递归的起点~
code:
#include<iostream>#include<string.h>using namespace std;int countn[10000];int SUM,n;int ans;int sum;void dfs(int m,int sum){if(sum==SUM){ans++;return;}for(int i=m;i<=SUM;i++){if(countn[i]&&i+sum<=SUM){countn[i]--;sum+=i;dfs(i,sum);//这里并没有把当前值定为sum而是定为了 i 因为可能会有多个相同值,需要重复访问~(多重背包你懂得~) sum-=i;countn[i]++;}}}int main(){int a,b;int T=0;while(cin>>SUM>>n){ if(T) cout<<endl; T++;memset(countn,0,sizeof(countn));for(int i=0;i<n;i++) { cin>>a>>b; countn[a]=b; } ans=0; dfs(0,0); cout<<ans<<endl; }return 0;}
- ZOJ 2734 Exchange Cards
- zoj - 2734 - Exchange Cards
- zoj 2734 Exchange Cards
- zoj Exchange Cards
- zoj 2734 Exchange Cards
- zoj 2734 Exchange Cards
- zoj 2734 Exchange Cards
- zoj Exchange Cards 2734
- ZOJ 2734(Exchange Cards)
- zoj 2734 Exchange Cards(DFS~)
- ZOJ 2734 Exchange Cards(DFS)
- ZOJ 2734--Exchange Cards【DFS经典】
- ZOJ Exchange Cards 2734【母函数】
- zoj 2734 Exchange Cards 【DFS or 母函数】
- zoj 2734 Exchange Cards(母函数 && DFS)
- ZOJ-2734-Exchange Cards(DFS全排列+去重)
- 【练习赛2补题】zoj 2734 Exchange Cards 【DFS】
- zoj2734-Exchange Cards
- 不引用服务而使用WCF,手动编写客户端代理类
- 在Spring环境下存取properties文件中的值
- nonatomic retain copy assign区别
- android系统如何获得外置卡路径
- SQLite foreign key examples
- zoj Exchange Cards
- 模板引擎的原理
- 反射技术
- 关系操作符运算规则
- 要求java调用http协议生成的结果集为一个xml文件!
- Dalvik 初探 (一)
- 错误 1 “ResolveAssemblyReference”任务意外失败。
- js倒计时汇总(源码)
- 有关二叉树操作的代码实现