zoj 2734 Exchange Cards【dfs+剪枝】
Exchange Cards
Time Limit: 2 Seconds Memory Limit: 65536 KB
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 and m, 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, each val 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 2
2 1
3 1 10 5
10 2
7 2
5 3
2 2
1 5
Sample Output
1 7
注意输出格式
#include<stdio.h>
#include<string.h>
int n,m,sum,k;
int val[10000010];
void dfs(int cur,int tot)
{
int i,j;
if(tot==n)
{
sum++;
return ;
}
for(i=cur;i<k;i++)
{
if(tot+val[i]<=n)//剪枝
dfs(i+1,tot+val[i]);
while(val[i]==val[i+1]&&i<k-1)//去重
++i;
}
}
int main()
{
int i,t=0;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(t>0) printf("\n");
k=0;
int x,y;
for(i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
while(y--)
val[k++]=x;
}
sum=0;
dfs(0,0);
printf("%d\n",sum);
t++;
}
return 0;
}
zoj 2734 Exchange Cards【dfs+剪枝】的更多相关文章
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
Sum It Up Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- ZOJ Seven-Segment Display 暴力dfs + 剪枝
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3954 0 = on 1 = off A seven segment ...
- Exchange Cards(dfs)
Exchange Cards Time Limit: 2 Seconds Memory Limit: 65536 KB As a basketball fan, Mike is also f ...
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- POJ 3009 DFS+剪枝
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- DFS+剪枝 HDOJ 5323 Solve this interesting problem
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- c、c++混编实现查询本地IP地址
一.思路 1.要想得到本地IP地址,可以通过本机名来查询,所以首先得得到本机名. 2.牵涉到IP地址,首先想到牵涉到网络协议,因此得加载套接字协议,所以先使用WSAStartup函数完成对Winsoc ...
- Linux下彻底卸载LibreOffice方法
Linux下彻底卸载LibreOffice方法 终端中输入命令: 对所有基于 Debian 的发行版(Debian.Ubuntu.Kubuntu.Xubuntu.*buntu.Sidux 等): su ...
- 使用Express搭建服务器
Express是基于Node.js平台,快速.开放.极简的web开发框架.所以,使用Express之前,请确保已安装Node.js. 1.创建一个目录作为当前工作目录: $ mkdir myapp $ ...
- PHP 关于 $GLOBALS['HTTP_RAW_POST_DATA']
PHP 关于 $GLOBALS['HTTP_RAW_POST_DATA'] 最近用微信api写接口时用到了这个,记录,下面转载开始: —————————— 这是手册里写的 总是产生变量包含有原始的 P ...
- ng-form
form提供的属性都是用来表示表单的验证状态的,包括:$pristine(表单没有填写记录).$dirty(表单有填写记录).$valid(通过验证).$invalid(未通过验证).$error(验 ...
- Mac Pro的HDMI接口与WI-FI可能存在冲突的解决方法
当我将Mac Pro通过HDMI->DVI转接头接上一台显示器时,正在使用的WI-FI网络立马不能使用,重新连接网络也不行,但断开HDMI连接后,WI-FI立马恢复正常. 在网上查,在apple ...
- WPF学习之路初识
WPF学习之路初识 WPF 介绍 .NET Framework 4 .NET Framework 3.5 .NET Framework 3.0 Windows Presentation Found ...
- java 正则操作之获取
// 正则操作 获取import java.util.regex.*;class Demo{ public static void main(String[] args){ String str=& ...
- 2016022609 - redis哈希命令集合
参考:http://www.yiibai.com/redis/redis_hashes.html Redis的哈希值是字符串字段和字符串值之间的映射,所以他们是表示对象的完美数据类型 在Redis中的 ...
- 应用Java(环境变量)
工作中,不一定非要设置Java环境变量 因为,IDE自身环境的设置,代替了系统环境变量 环境变量 系统的环境变量,相当于软件工作的环境.工作中,经常需要设置以下变量: Path ClassPath 自 ...