HDU - Statistics Coin

Statistics coins


Problem Description

Suppose a pile of n coins consisting of 1 cent, 2 cents, and 5 cents has a total face value of m cents. Find the total number of possible combinations (the number of coins of a certain face value can be 0).
 

Input

The first line of the input data has a positive integer T, indicating that there are T groups of test data; in the next T lines, each line has two numbers n, m, and the meanings of n and m are the same as above.
 

Output

For each set of test data, output the number of possible combinations; one line per set of output.
 

Sample Input

 
  
2
3 5
4 8
 

Sample Output

 
  
1
2
 

Triple loop doesn't time out

#include<bits/stdc++.h>
using namespace std;
intmain()
{
    int t,sum,n,m;
    scanf("%d",&t);
    while(t--)
    {
        sum=0;
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                if(i+j>n||1*i+2*j>m)break;
                for(int k=0;k<=n;k++)
                {
                    if(i+j+k>n||1*i+2*j+5*k>m)break;
                    if(i+j+k==n&&1*i+2*j+5*k==m)
                    {
                        sum++;
                    }
                }
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324817413&siteId=291194637