Under construction

Still being built. Things may change

m-ary Partitions

Interview

Time limit2sMemory limit512 MB

Summary
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 nn is a collection of positive integers whose sum is nn, 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 mm. 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 nn.

Input

The first line contains the number of data sets PP (1≤P≤10001 \le P \le 1000). 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 KK (1≤K≤10001 \le K \le 1000), the base of the powers mm (3≤m≤1003 \le m \le 100), and the integer nn (3≤n≤100003 \le n \le 10000) 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 nn 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 KK read from the input, one space, and the number of m-ary partitions of nn.

Examples2

  1. Example 1

    Input
    5
    1 3 9
    2 3 47
    3 5 123
    4 7 4321
    5 97 9999
    
    Expected output
    1 5
    2 63
    3 75
    4 144236
    5 111
    
  2. Example 2

    Input
    1
    1 3 3
    
    Expected output
    1 2