Digit Sums

Time limit1sMemory limit128 MB

Summary
Count positive integers up to A whose base-B digits add up to C for each query line.
Level

Medium4 of 10

Topics
Dynamic programming, Math
Solved
No attempts yet

Problem

You are given three positive integers AA, BB, and CC. How many positive integers not greater than AA have digits that sum to CC when the number is written in base BB?

Input

The input is a series of lines. Each line holds three integers AA, BB, and CC separated by one or more blanks, with 2≤B≤1002 \le B \le 100 and 1≤A,C≤1091 \le A, C \le 10^9. All three numbers are given in base 10. A line containing three zeros ends the input and is not processed.

Output

For each input line, print the number of such integers in base 10, one per line.

Examples1

  1. Example 1

    Input
    100 10 9
    100 10 1
    750000 2 2
    1000000000 10 40
    100000000 100 200
    0 0 0
    
    Expected output
    10
    3
    189
    45433800
    666303