Manasa and Stones

Manasa and Stones

Problem Description


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 test cases follow; each has 3 lines. The first line contains n (the number of stones). The second line contains a, and the third line contains b.


Constraints

1<=T<=10

1<=n,a,b<=103


Output Format

Space-separated list of numbers which are the possible values of the last stone in increasing order.


Logic Test Case 1


Input (stdin)

2


3


1


2


4


10


100


Expected Output


2 3 4 


30 120 210 300 

Logic Test Case 2


Input (stdin)

2


6


4


8


11


3


10


Expected Output


20 24 28 32 36 40 


30 37 44 51 58 65 72 79 86 93 100 




Code Area


import java.util.*;

public class TestClass {

  public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int T = input.nextInt();

        for(int i = 0; i < T; i++)

        {

            int n = input.nextInt()-1;

            int a = input.nextInt();

            int b = input.nextInt();

            if(a == b)

            {

                System.out.println(a*n + " ");

                continue;

            }

            int tmp = a;

            a = Math.min(a,b);

            b = (a == b) ? tmp : b;

            int min = a*n;

            int max = b*n;

            for(int finalSteps = min; finalSteps <= max; finalSteps += (b-a))

            {

                System.out.print(finalSteps + " ");

            }

            

            System.out.println();

            

        }

    }

} 

Post a Comment

0 Comments