标签:
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
17
大意就是给m种卡片的值和个数,选几张卡片的值的和为n,问有多少种方法 这题我读完第一想到的就是母函数,渣渣的我没想到DFS也可以做.注意输出格式
//母函数 #include<stdio.h> #include<string.h> #define M 1005 int x[M],y[M]; int cnt[15]; int main(){ int n,m,Case=0; while(~scanf("%d%d",&n,&m)){ if(Case++) printf("\n");//pe好几次 int a,b; memset(cnt,0,sizeof(cnt)); memset(x,0,sizeof(x)); for(int i=0;i<m;i++){ scanf("%d%d",&a,&b); cnt[a]=b; } for(int i=0;i<=cnt[1];i++){ x[i]=1; } for(int i=2;i<11;i++){ for(int j=0;j<=n;j++){ for(int k=0,s=0;k+j<=n && s<=cnt[i];k+=i,s++) //s表示价值为i的卡片有几个,也可以写成k<=cnt[i]*i y[k+j]+=x[j]; } for(int j=1;j<=n;j++){ x[j]=y[j]; y[j]=0; } } printf("%d\n",x[n]); } return 0; } //DFS #include <stdio.h> #include <string.h> int sum,ways; int num[1010]; int value,n; void DFS(int x) { if(sum == value) { ways++; return ; } for(int i=x; i<=value; i++) { if(num[i] && sum+i<=value){ num[i]--; sum+=i; DFS(i); num[i]++; sum-=i; } } } int main(){ int x,y; int Case = 0; while(~scanf("%d%d",&value,&n)) { if(Case++) printf("\n"); memset(num,0,sizeof(num)); sum=ways=0; for(int i=0; i<n; i++) { scanf("%d%d",&x,&y); num[x] = y; } DFS(1); printf("%d\n",ways); } return 0; }
zoj 2734 Exchange Cards(母函数 && DFS)
标签:
原文地址:http://blog.csdn.net/ling_du/article/details/47401611