1. 程式人生 > >HDU 2566 統計硬幣

HDU 2566 統計硬幣

統計硬幣

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9482    Accepted Submission(s): 6413  

Problem Description

假設一堆由1分、2分、5分組成的n個硬幣總面值為m分,求一共有多少種可能的組合方式(某種面值的硬幣可以數量可以為0)。

Input

輸入資料第一行有一個正整數T,表示有T組測試資料; 接下來的T行,每行有兩個數n,m,n和m的含義同上。

Output

對於每組測試資料,請輸出可能的組合方式數; 每組輸出佔一行。

Sample Input

2

3 5

4 8

Sample Output

1 2

// 直接消元,然後列舉
#include<cstdio>

int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        int n, m;
        scanf("%d%d", &n, &m);
        int a, b, c = 0;
        int ans = 0;
        while (1)
        {
            b = -4 * c + m - n;
            a = 3 * c+ 2 * n - m;
            if (b>=0&&a>=0)ans++;
            else if (b < 0) break;
            c++;
        }
        printf("%d\n", ans);
    }
    return 0;
}