Yet Another Array Restoration (Codeforces 1409C)
Tags: Math, number theory

We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array:  The array consists of $$$n$$$ distinct positive (greater than $$$0$$$) integers.  The array contains two elements $$$x$$$ and $$$y$$$ (these elements are known for you) such that $$$x < y$$$.  If you sort the array in increasing order (such that $$$a_1 < a_2 < \ldots < a_n$$$), differences between all adjacent (consecutive) elements are equal (i.e. $$$a_2 - a_1 = a_3 - a_2 = \ldots = a_n - a_{n-1})$$$. It can be proven that such an array always exists under the constraints given below.Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize $$$\max(a_1, a_2, \dots, a_n)$$$.You have to answer $$$t$$$ independent test cases.

Input: The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow.The only line of the test case contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$2 \le n \le 50$$$; $$$1 \le x < y \le 50$$$) — the length of the array and two elements that are present in the array, respectively.

Output: For each test case, print the answer: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter).It can be proven that such an array always exists under the given constraints.


Sample Inputs:
5
2 1 49
5 20 50
6 20 50
5 3 8
9 13 22

Sample Outputs:
1 49 
20 40 30 50 10
26 32 20 38 44 50 
8 23 18 13 3 
1 10 13 4 19 22 25 16 7

Solution (Python):
for i in range(int(input())):
    (n, x, y) = map(int, input().split())
    if (y - x) % (n - 1) == 0:
        k = int((y - x) / (n - 1))
        print(*range(x, x + n * k, k))
    else:
        k = int((y - x) / (n - 1)) + 1
        while k <= y - x:
            if (y - x) % k == 0:
                break
            k = k + 1
        z = int((x - 1) / k)
        if n - 1 - (y - x) / k < z:
            print(*range(y - (n - 1) * k, y + k, k))
        else:
            print(*range(x - z * k, x + (n - z) * k, k))

Solution (Python):
t = int(input())
for _ in range(t):
    lst = list(map(int, input().split(' ')))
    n = lst[0]
    x = lst[1]
    y = lst[2]
    diff = y - x
    temp = n - 1
    while temp > 0:
        if (diff / temp).is_integer():
            dec = diff // temp
            break
        else:
            temp -= 1
    ans = []
    for i in range(x, y + 1, dec):
        ans.append(i)
    l = len(ans)
    while l < n:
        if ans[0] - dec > 0:
            ans.insert(0, ans[0] - dec)
        else:
            ans.append(ans[-1] + dec)
        l += 1
    print(*ans)

Solution (Python):
def primes(n):
    if n <= 2:
        return [1]
    sieve = [True] * (n + 1)
    for x in range(3, int(n ** 0.5) + 1, 2):
        for y in range(3, n // x + 1, 2):
            sieve[x * y] = False
    return [2] + [i for i in range(3, n, 2) if sieve[i]]
t = int(input())
for i in range(t):
    (n, x, y) = map(int, input().split())
    if y >= n and x > y - n:
        for i in range(y - n + 1, y + 1):
            print(i, end=' ')
        print()
        continue
    elif y < n:
        for i in range(1, n + 1):
            print(i, end=' ')
        print()
        continue
    v = y - x
    p1 = primes(v)
    r = False
    for x1 in p1:
        if v % x1 == 0:
            r = True
            break
    if r == False:
        x2 = x % v
        if x2 != 0:
            x3 = (n - 1) * v + x2
        else:
            x3 = n * v
    else:
        x2 = x % x1
        if x2 != 0:
            x3 = (n - 1) * x1 + x2
        else:
            x3 = n * x1
    x4 = max(x3, y)
    x6 = v if r == False else x1
    if x3 == x4:
        if x2 == 0:
            x2 += x6
        for x5 in range(x2, x4 + 1, x6):
            print(x5, end=' ')
        print()
    else:
        q1 = n - 1
        while True:
            if v % q1 == 0:
                x7 = v // q1
                x8 = y - x7 * (n - 1)
                x8 = x8 if x8 > 0 else y % x7
                break
            q1 -= 1
        for x5 in range(x8, x8 + x7 * (n - 1) + 1, x7):
            print(x5, end=' ')
        print()