atcoder#ARC149F. [ARC149F] Rational Number System
[ARC149F] Rational Number System
Score : points
Problem Statement
Let be a rational number, and and be the numerator and denominator of , respectively. That is, and are positive integers such that and .
Let the base-\boldsymbol{r} expansion of a positive integer be the integer sequence that satisfies all of the following conditions.
- is an integer between and .
- .
- .
It can be proved that any positive integer has a unique base- expansion.
You are given positive integers , , , , and . Here, and satisfy .
Consider sorting all positive integers not greater than in ascending lexicographical order of their base- expansions. Find the -th, -th, , -th positive integers in this list.
As usual, decimal notations are used in the input and output of positive integers.
What is lexicographical order on sequences?
A sequence is said to be lexicographically smaller than when 1. or 2. below is satisfied. Here, and denote the lengths of and , respectively.
- and .
- There is an integer that satisfies both of the following.
- .
- .
Constraints
Input
The input is given from Standard Input in the following format:
Output
Print lines. The -th line should contain the -th positive integer in the list of all positive integers not greater than sorted in ascending lexicographical order of their base- expansions.
Use decimal notations to print positive integers.
3 1 9 1 9
1
3
9
4
5
2
6
7
8
The base- expansions of the positive integers not greater than are as follows.
1: (1), 2: (2), 3: (1, 0),
4: (1, 1), 5: (1, 2), 6: (2, 0),
7: (2, 1), 8: (2, 2), 9: (1, 0, 0).
3 2 9 1 9
1
2
3
4
6
9
7
8
5
The base- expansions of the positive integers not greater than are as follows.
1: (1), 2: (2), 3: (2, 0),
4: (2, 1), 5: (2, 2), 6: (2, 1, 0),
7: (2, 1, 1), 8: (2, 1, 2), 9: (2, 1, 0, 0).
One can see that the base- expansion of is , for instance, from $2\cdot \bigl(\frac32\bigr)^2 + 1\cdot \frac32 + 0\cdot 1 = 6$.
3 2 9 3 8
3
4
6
9
7
8
This is the -rd through -th lines of the output to Sample Input 2.
10 9 1000000000000000000 123456789123456789 123456789123456799
806324968563249278
806324968563249279
725692471706924344
725692471706924345
725692471706924346
725692471706924347
725692471706924348
725692471706924349
653123224536231907
653123224536231908
653123224536231909