Equal digit sums in an arithmetic sequence

Scan n = 1, 2, 3, ... and stop when some digit sum of Cn + D in base B reaches M occurrences, then print those n values.

Medium5SimulationImplementationMathNo attempts yetTime limit2sMemory limit64 MB

Problem

The infinite arithmetic sequence A(n)=Cn+DA(n) = Cn + D is defined for every natural number nn. Find MM distinct natural numbers n1,n2,,nMn_1, n_2, \dots, n_M, each at most 101510^{15}, such that A(n1),A(n2),,A(nM)A(n_1), A(n_2), \dots, A(n_M) all have the same sum of digits in base BB.

Every positive integer NN has exactly one representation in base BB: the digit string xkxk1x1x0x_k x_{k-1} \dots x_1 x_0 with 0xi<B0 \le x_i < B for each ii and xkBk+xk1Bk1++x1B+x0=Nx_k B^k + x_{k-1} B^{k-1} + \dots + x_1 B + x_0 = N. Its sum of digits is xk++x0x_k + \dots + x_0.

Several answers usually satisfy the condition, so exactly one of them is selected by the following rule. For a natural number nn, let f(n)f(n) be the sum of the digits of A(n)A(n) in base BB. Compute f(n)f(n) for n=1,2,3,n = 1, 2, 3, \dots in that order and count how many times each value has appeared. Stop at the moment some value reaches MM occurrences and call that value ss. Exactly MM of the numbers nn examined so far satisfy f(n)=sf(n) = s, and those MM numbers are the answer.

Input

The first line contains the integers CC, DD, BB and MM, separated by spaces (1C,D100001 \le C, D \le 10000, 2B50002 \le B \le 5000, 1M2500001 \le M \le 250000).

The input is such that some digit sum reaches MM occurrences while nn runs from 11 to 10710^7.

Output

On one line, print the MM numbers selected by the rule above in increasing order, separated by single spaces.

Print the numbers nin_i, not the numbers A(ni)A(n_i). Every printed number is at most 101510^{15}.

Note

In the first example A(2)=5×2+3=13A(2) = 5 \times 2 + 3 = 13 and A(5)=5×5+3=28A(5) = 5 \times 5 + 3 = 28. In base 2 the number 1313 is written 11011101 and the number 2828 is written 1110011100, so both digit sums are 33. The value 33 reaches two occurrences at n=5n = 5, and no other value reaches two occurrences earlier.

In the second example A(2)=5A(2) = 5, A(11)=23A(11) = 23 and A(20)=41A(20) = 41. All three have digit sum 55 in base 10, and no value reaches three occurrences before n=20n = 20.