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 MBThe infinite arithmetic sequence A(n)=Cn+D is defined for every natural number n. Find M distinct natural numbers n1,n2,…,nM, each at most 1015, such that A(n1),A(n2),…,A(nM) all have the same sum of digits in base B.
Every positive integer N has exactly one representation in base B: the digit string xkxk−1…x1x0 with 0≤xi<B for each i and xkBk+xk−1Bk−1+⋯+x1B+x0=N. Its sum of digits is xk+⋯+x0.
Several answers usually satisfy the condition, so exactly one of them is selected by the following rule. For a natural number n, let f(n) be the sum of the digits of A(n) in base B. Compute f(n) for n=1,2,3,… in that order and count how many times each value has appeared. Stop at the moment some value reaches M occurrences and call that value s. Exactly M of the numbers n examined so far satisfy f(n)=s, and those M numbers are the answer.
The first line contains the integers C, D, B and M, separated by spaces (1≤C,D≤10000, 2≤B≤5000, 1≤M≤250000).
The input is such that some digit sum reaches M occurrences while n runs from 1 to 107.
On one line, print the M numbers selected by the rule above in increasing order, separated by single spaces.
Print the numbers ni, not the numbers A(ni). Every printed number is at most 1015.
In the first example A(2)=5×2+3=13 and A(5)=5×5+3=28. In base 2 the number 13 is written 1101 and the number 28 is written 11100, so both digit sums are 3. The value 3 reaches two occurrences at n=5, and no other value reaches two occurrences earlier.
In the second example A(2)=5, A(11)=23 and A(20)=41. All three have digit sum 5 in base 10, and no value reaches three occurrences before n=20.