ZOJ question 2734 Exchange Cards (DFS deduplication OR primary function)

Source: Internet
Author: User

ZOJ question 2734 Exchange Cards (DFS deduplication OR primary function)

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 your task to write a program to figure it out.

Input

The problem consists of multiple test cases, terminatedEOF. There's a blank line between two inputs.

The first line of each test case givesN, The value of the card Mike plans to get andM, The number of different kinds of cards Mike has.NWill be an integer number between 1 and 1000.MWill be an integer number between 1 and 10.

The nextMLines give the information of different kinds of cards Mike have. Each line contains two integers,ValAndNum, 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, eachValAndNumWill be an integer greater than zero.

Output

For each test case, output in one line the number of different ways Mike cocould 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 22 13 110 510 27 25 32 21 5

Sample Output

17

Various types of water

Ac code

DFS

 

# Include
 
  
# Include
  
   
# Include
   
    
Int n, m, k, ans; int a [100010]; int cmp (const void * a, const void * B) {return * (int *) b-* (int *) a;} void dfs (int pos, int sum) {if (sum = n) {ans ++; return ;} for (int I = pos; I
    
     
Primary function practices
     
#include
      
       #include
       
        int c1[1010],c2[1010],b[15],a[15];int main(){int n,m,flag=0;while(scanf(%d%d,&n,&m)!=EOF){int i,j,k;if(flag)printf();elseflag=1;memset(c1,0,sizeof(c1));memset(c2,0,sizeof(c2));for(i=0;i
        
         

 

 

Author: DAI, WenbinSource: Zhejiang University Local Contest 2006

 

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.