Sequences

Interview

Time limit1sMemory limit128 MB

Summary
For each query with a first term, non-zero common difference, and value, print the term index if the value is in the arithmetic sequence, otherwise X.
Level

Easy3 of 10

Topics
Math, Implementation, Number theory
Solved
No attempts yet

Problem

An arithmetic sequence is a sequence of numbers in which every term after the first is obtained by adding a fixed number, called the common difference, to the previous term.

For example, 3,5,7,93, 5, 7, 9 is an arithmetic sequence whose first term is 33 and whose common difference is 22: each term after the first is formed by adding 22 to the previous one. Here 33 is term 11 and 99 is term 44.

Given a first term, a common difference, and a value, determine whether the value can appear in the sequence. If it can, output which term it is; otherwise output the letter X.

Input

The input consists of several lines. Each line contains three integers separated by spaces:

  • the first term of the sequence,
  • the common difference (a non-zero integer),
  • the value to test.

The input is terminated by a line containing 0 0 0, which must not be processed.

Output

For each input line except the terminating line, print a single line containing the term number of the value, or X if the value is not part of the sequence.

Constraints

  • The first term, the common difference, and the value are each between −6000000-6000000 and 60000006000000.

Hint

For the first query 3 2 11, the sequence is 3,5,7,9,11,…3, 5, 7, 9, 11, \dots, so 1111 is term 55.

For the second query -1 -3 -8, the sequence is −1,−4,−7,−10,…-1, -4, -7, -10, \dots, so −8-8 does not appear and the answer is X.

Examples3

  1. Example 1

    Input
    3 2 11
    -1 -3 -8
    0 0 0
    
    Expected output
    5
    X
    
  2. Example 2

    Input
    3 2 3
    0 0 0
    
    Expected output
    1
    
  3. Example 3

    Input
    3 2 1
    0 0 0
    
    Expected output
    X