Sequences
InterviewTime limit1sMemory limit128 MB
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, is an arithmetic sequence whose first term is and whose common difference is : each term after the first is formed by adding to the previous one. Here is term and is term .
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 and .
Hint
For the first query 3 2 11, the sequence is , so is term .
For the second query -1 -3 -8, the sequence is , so does not appear and the answer is X.