Zoj 2734 Exchange Cards (parent function && DFS)

Source: Internet
Author: User

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 don't always get the money to buy new cards, so sometimes he'll exchange with his friends for car DS He likes. Of course, different cards has 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 both 5$ cards or three 3$ cards plus one 1$ card, depending on the kind S of cards He has and the number of each kind of card. And sometimes he'll involve unfortunately in a bad condition that he had not got the exact value of the card he's Looki Ng for (fans All Exchange cards for equivalent value).

Here comes the problem, given the card value he plans to get and the cards he have, Mike wants to fix how many ways he can Get it. So it's a task to the write a program to figure it out.

Input

The problem consists of multiple test cases, terminated by EOF. There ' s a blank line between and inputs.

The first line of all test case gives n, the value of the card Mike plans to get andm, the number of Di Fferent kinds of Cards Mike has. n would be is an integer number between 1 and 1000. m 'll be is an integer number between 1 and 10.

The next m lines give the information of different kinds of cards of Mike have. Each line contains-integers,val and Num, representing the value of this kind of card, and the number Of this kind of card Mike has.

Note: different kinds of cards'll has different value, eachval and num would be a integer gr Eater 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 is sure that the output would fall into an integer value.

Output a blank line between the test cases.

Sample Input

5 22 13 110 510 27 25 32 21 5

Sample Output

1

7

The main idea is to give m card value and number, select a few cards and the value of N, ask how many ways this question I read the first thought is the mother function, slag slag I did not expect DFS can do. Note Output format

Female function #include <stdio.h> #include <string.h> #define M 1005int 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 several times 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 denotes a number of cards with a value of I and can also be written as k<=cnt[i]*iy[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 (parent function && DFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.