m-ary Partitions
InterviewTime limit2sMemory limit512 MB
Count the partitions of n into powers of m, for up to 1000 queries with n up to 10000.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Math, Implementation, Combinatorics
- Solved
- No attempts yet
Problem
A partition of an integer is a collection of positive integers whose sum is , usually written in descending order. For example:
10 = 4+3+2+1
A partition is m-ary if every term in it is a power of . The 3-ary partitions of 9 are these five:
9
3+3+3
3+3+1+1+1
3+1+1+1+1+1+1
1+1+1+1+1+1+1+1+1
Write a program that counts the m-ary partitions of an integer .
Input
The first line contains the number of data sets (). The data sets are independent and are all processed the same way.
Each data set is one line holding three integers separated by spaces: the data set number (), the base of the powers (), and the integer () whose m-ary partitions are to be counted. The data set numbers come from the input and are not guaranteed to be sorted.
The number of m-ary partitions of always fits in a 32-bit unsigned integer.
Output
Print one line per data set, in the order the data sets appear in the input. Each line holds the data set number read from the input, one space, and the number of m-ary partitions of .