ZOJ 2734--Exchange Cards【DFS经典】
来源:互联网 发布:淘宝3c认证必须要吗 编辑:程序博客网 时间:2024/05/29 11:00
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
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;int a[11000];int n, m, k;int ans;void dfs(int pos, int sum){ if(sum == n){ ans++; return ; } for(int i = pos; i < k; ++i){ if(sum + a[i] <= n){ dfs(i + 1, sum + a[i]); while(i + 1 < k && a[i] == a[i + 1])//精髓 i++; } }}int main (){ int T = 0; while(scanf("%d%d", &n, &m) != EOF){ k = 0; while(m--){ int val, num; scanf("%d%d", &val, &num); while(num--){ a[k++] = val; } } sort(a, a + k); ans = 0; dfs(0, 0); if(T > 0) printf("\n"); printf("%d\n",ans); T++; } return 0;}
- ZOJ 2734--Exchange Cards【DFS经典】
- zoj 2734 Exchange Cards(DFS~)
- ZOJ 2734 Exchange Cards(DFS)
- ZOJ 2734 Exchange Cards
- zoj - 2734 - Exchange Cards
- zoj 2734 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 or 母函数】
- zoj 2734 Exchange Cards(母函数 && DFS)
- ZOJ-2734-Exchange Cards(DFS全排列+去重)
- 【练习赛2补题】zoj 2734 Exchange Cards 【DFS】
- ZOJ 题目2734 Exchange Cards(DFS 去重OR 母函数)
- ZOJ Exchange Cards 2734【母函数】
- zoj Exchange Cards
- input text表单中嵌入select选项的方法
- Chapter 5:collection view - iOS 8 Swift Programming cookBook 读书笔记
- hdoj2191多重背包化为01背包
- Java基础-常用API之String类
- js深入理解(二)
- ZOJ 2734--Exchange Cards【DFS经典】
- 82. Remove Duplicates from Sorted List II
- DPDK 内存管理(一)(内存初始化)
- hbase无法启动,The node /hbase is not in ZooKeeper和Can't get master address from ZooKeeper;
- 快速排序
- Static修饰成员变量
- Arbitrage (Uva 104 dp+floyd)
- HAProxy+Keepalived实现Web服务器负载均衡
- 黑马程序员——Java 面向对象_多态