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+剪枝】的更多相关文章

  1. 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 ...

  2. ZOJ Seven-Segment Display 暴力dfs + 剪枝

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3954 0 = on     1 = off A seven segment ...

  3. Exchange Cards(dfs)

    Exchange Cards Time Limit: 2 Seconds      Memory Limit: 65536 KB As a basketball fan, Mike is also f ...

  4. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  6. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  7. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  8. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  9. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. Codevs 1065 01字符串

    1065 01字符串 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 输出仅有0和1组成的长度为n的字符串,并且其中不能含有 ...

  2. css字体大小设置em与rem的区别

    em 单位em是相对于父元素的,如果父元素没有设置字体大小,那就会追溯到body. 比如  如果我在box_text的父元素box加了一个字体大小   那么body的8px就会被box_text的父元 ...

  3. sql知识

    SQL 基本知识 SQL Server 是Microsoft 公司推出的关系型数据库管理系统.具有使用方便可伸缩性好与相关软件集成程度高等优点,可跨越从运行Microsoft Windows 98 的 ...

  4. Android 简单的FC

    直接贴log 01-02 08:17:56.589 I/ActivityManager( 312): Start proc com.android.providers.calendar for con ...

  5. sql如果存在就修改不存在就新增

    FROM 表名 WHERE 条件) UPDATE 表名 SET 字段=值 WHERE 条件 ELSE INSERT INTO 表名(字段) VALUES(值) 真实使用举例: from [UserRu ...

  6. Android Studio HelloWorld

    开发第一应用 可以开发属于自己的应用,是否有点小激动?好吧!让我们开始,首先点击Start a new Android Studio Project创建工程: 接下来需要输入应用名称(第一个字母要大写 ...

  7. tomcat发布静态网页

  8. 有关collection中的一些数据结构

    Collection接口 Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同的元素 ...

  9. PI数据库的使用-PI System Management Tools

    1.PI连接管理器 2.标记搜索 3.当前值

  10. Jsp中获得集合List或Set的长度

    首先要引入<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> ...