Feast Coins
InterviewTime limit3sMemory limit256 MB
Count ways to reach total S with owned coins so that every chosen coin value appears the same number of times.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
At the last feast the young princess received far too many coins. She is very young and does not know what each coin is worth. Hand her a coin of value 5 or a coin of value 1 and she counts each one as a single coin, whatever its value.
She does notice that a coin of value 5 does not look like a coin of value 1. She is happy when she holds the same number of coins of every value she has, and unhappy otherwise.
She owns many coins of different values. She wants to pick some of them so that their values add up to exactly , and so that every value present among the picked coins appears the same number of times. Count how many different ways she has to do this.
Input
The first line contains one integer , the number of test cases (). test cases follow.
Each test case starts with a line holding two integers separated by a single space, and (, ), the required total value and the number of different coin values.
Each of the next lines holds two integers separated by a single space, and (), the value of a coin and the number of coins of that value the princess owns. Within one test case all are distinct.
Output
For each test case print one line in the form Case n: X, where is the test case number starting from 1 and is the number of different ways to reach the total described above.
Two ways count as different when some coin value appears a different number of times in them.
The answer always fits in a 64-bit signed integer.
Notes
With , two coins of value 2 and one coin of value 6, the only combination adding up to 10 is (2, 2, 6). It does not count, because value 2 appears twice while value 6 appears once.
With and ten coins of each of the values 1, 2, 3 and 4, there are 5 ways: (1, 1, 1, 1, 1, 1, 1, 1, 1, 1), (2, 2, 2, 2, 2), (2, 2, 3, 3), (1, 1, 4, 4), (1, 2, 3, 4).