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
题解:求种类数,i*card.v代表可以加上的价值;
代码:
 #include<stdio.h>
struct Node {
int v,num;
};
Node card[];
int N,M,tot;
void dfs(int x,int now){
if(now==N){
tot++;return;
}if(now>N||x>=M)return;
for(int i=;i<=card[x].num;i++){
dfs(x+,now+i*card[x].v);
}
}
int main(){int flot=;
while(~scanf("%d%d",&N,&M)){
if(flot++)puts("");
for(int i=;i<M;i++)scanf("%d%d",&card[i].v,&card[i].num);
tot=;
dfs(,);
printf("%d\n",tot);
}
return ;
}

Exchange Cards(dfs)的更多相关文章

  1. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  2. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  3. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  4. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  5. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

  6. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  7. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  8. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  9. 搜索——深度优先搜索(DFS)

    设想我们现在身处一个巨大的迷宫中,我们只能自己想办法走出去,下面是一种看上去很盲目但实际上会很有效的方法. 以当前所在位置为起点,沿着一条路向前走,当碰到岔道口时,选择其中一个岔路前进.如果选择的这个 ...

随机推荐

  1. UESTC_秋实大哥の恋爱物语 2015 UESTC Training for Search Algorithm & String<Problem K>

    K - 秋实大哥の恋爱物语 Time Limit: 5000/2000MS (Java/Others)     Memory Limit: 32000/32000KB (Java/Others) Su ...

  2. Isomorphic Strings 解答

    Question Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...

  3. 【转】10个你必须掌握的超酷VI命令技巧

    摘要:大部分Linux开发者对vi命令相当熟悉,可是遗憾的是,大部分开发者都只能掌握一些最常用的Linux vi命令,下面介绍的10个vi命令虽然很多不为人知,但是在实际应用中又能让你大大提高效率. ...

  4. iOS 系统架构 && 常用 framework

    整理自互联网,感谢原文作者! 1.iOS基于UNIX系统,因此从系统的稳定性上来说它要比其他操作系统的产品好很多 2.iOS的系统架构分为四层,由上到下一次为:可触摸层(Cocoa Touch lay ...

  5. SQL Server2008R2安装失败问题之语言包问题

           今天安装SQL Server2008 的时候出现了,如下的的问题,安装过程在ExcuteStandardTimingsWorkflow时候报错,结束安装.       提示:       ...

  6. js库开发

    <!DOCTYPE html><html>    <head>        <meta charset="UTF-8">      ...

  7. dir_colors linux颜色配置

    在控制台下,用ls,就会发现,shell将不同类型的文件项目显示为不同的颜色.者可以提高效率,不用ls -l便能大概的把各个文件的类型情况了解一下. 你有没有想过更改这个着色配置呢? 其实,在/etc ...

  8. Eclipse导入jre方法

    处理步骤:引入本机安装的jre1.8的步骤如下:

  9. ThinkPHP 类似Yii的Gii生成Model的功能。

    ThinkPHP 类似Yii的Gii生成Model的功能.自动生成ThinkPhp 3.1 的基础模型.. #!/usr/bin/env php <?php /** * * THINKPHP 基 ...

  10. HttpClient 请求WebApi

    HttpClient client = new HttpClient(); client.BaseAddress = new Uri(ConfigurationManager.AppSettings[ ...