zoj 2734 Exchange Cards

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 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 2
2 1
3 1

10 5
10 2
7 2
5 3
2 2
1 5

Sample Output

1

7


嗯,题目大一就是说,不同价值的卡片要换其他价值的卡片要求价值要相等。v和n,v代表要换的价值,n代表你所有的卡片的总张数。然后是va和nu,代表你有nu张价值为va的卡片,现在问有多少种换的方式,这里大神讲的要去重.。。。这题还可以用母函数,奈何昨天看了一下午母函数了解它是什么东西但还是不会啊啊啊啊,等我会了在加母函数的方法

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int v,n,k,cnt;
int val[1010];
int cmp(int a,int b)
{
	return a>b;
}
void dfs(int pos,int tot)
{
	if(tot==v)
	{
		cnt++;
		return ;
	}
	for(int i=pos;i<k;++i)
	{
		if(tot+val[i]<=v)
		{
			dfs(i+1,tot+val[i]);
			while(val[i]==val[i+1]&&i<k-1)//就是在这里去重,这张已经用过,那下一张价值相同的情况也一样
				i++;
		}
	}
}
int main()
{
	int va,nu,m=0;
	while(scanf("%d%d",&v,&n)!=EOF)
	{
		k=0;
		cnt=0;
		while(n--)
		{
			scanf("%d%d",&va,&nu);
			while(nu--)
				val[k++]=va;
		}
		sort(val,val+k,cmp);
		if(m)
			printf("\n");
		dfs(0,0);
		printf("%d\n",cnt);
		m++;
	}
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值