/*
Manasa and stones in Picat.
From https://www.hackerrank.com/challenges/manasa-and-stones
"""
Manasa is out on a hike with friends. She finds a trail of stones with numbers on them.
She starts following the trail and notices that two consecutive stones have a difference
of either a or b. Legend has it that there is a treasure trove at the end of the trail
and if Manasa can guess the value of the last stone, the treasure would be hers.
Given that the number on the first stone was 0, find all the possible values for the
number on the last stone.
Note : The numbers on the stones are in increasing order
Input Format
The first line contains an integer T i.e. the number of Test cases. T testcases follow.
Each testcase has 3 lines. The first line contains n ( the number of stones ) The second line contains a.
The third line contains b.
Output Format
Space separated list of numbers which are the possible values of the last stone in increasing order.
Constraints
1 <= T <= 10
1 <= n, a, b <= 103
Sample Input 00
2
3
1
2
4
10
100
Sample Output 00
2 3 4
30 120 210 300
Explanation
All possible series for first test cases are given below.
0,1,_2_
0,1,_3_
0,2,_3_
0,2,_4_
Hence the answer 2 3 4.
Series with different number of final step for second test cases are
0, 10, 20, 30
0, 10, 20, 120
0, 10, 110, 120
0, 10, 110, 210
0, 100, 110, 120
0, 100, 110, 210
0, 100, 200, 210
0, 100, 200, 300
hence the answer 30 120 210 300
"""
This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
import cp.
main => go.
go =>
foreach(Data in 1..2)
data(Data, N, A, B),
Res = stones(N,A,B),
println(Data=Res)
end,
nl.
stones(N,A,B) = Res =>
X = new_list(N),
X :: 0..N*max(A,B),
X[1] #= 0,
foreach(I in 2..N)
(X[I] #= X[I-1] + A)
#\/
(X[I] #= X[I-1] + B)
end,
increasing(X), % redundant constraint
All = solve_all([ff,split], X),
% the distinct last steps
Res = [last(E) : E in All].sort_remove_dups().
% The two examples above
data(1, N, A, B) => [N,A,B] = [3,1,2].
data(2, N, A, B) => [N,A,B] = [4,10,100].
increasing(List) =>
foreach(I in 2..List.length) List[I-1] #=< List[I] end.