Coin Combinations and Queries
Time limit3sMemory limit512 MB
For each query, count the multisets of at most d_i coins of denomination c_i (i=1..4) that sum to exactly v; answers fit in 64-bit.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Combinatorics, Math, Implementation
- Solved
- No attempts yet
Problem
A country issues coins of exactly four denominations, worth , , and won. Your wallet holds coins worth won, coins worth won, coins worth won and coins worth won. Write a program that counts the ways to pay exactly won with those coins.
Coins of the same denomination are not distinguished. Two ways are the same when they use the same number of coins of every denomination.
For example, with three 1 won coins, two 2 won coins, three 5 won coins and one 10 won coin, there are four ways to pay 10 won.
Input
The first line contains the number of test cases . ()
The first line of each test case contains the integers , , , , separated by spaces. (, )
Each of the next lines contains one query, made of the integers , , , , . ()
Output
For each query, print on its own line the number of ways to pay exactly won using at most coins of won, at most coins of won, at most coins of won and at most coins of won. Print 0 when no way exists. The answer can exceed the range of a 32-bit integer.