|
|||||||||||||||||||
|
SPOJ time: 2012-02-09 01:54:03 |
Recursive sequencesProblem code: HS09REC
Bernie wishes to impress his math teacher with a new theorem. He observes some sequences which satisfy a recursive relation an+2=2an+1-an+2 Each sequence of his concern starts with number a1=1, but the second numbers differ. Bernie thinks he found a nice rule, which he wants to check. He thinks that no matter what the number a2 is and no matter which n he chooses, one always can find an element of the sequence which equals anan+1. You can help him in his investigations by finding required elements. InputThere is K (1 ≤ K ≤ 1 000) lines of standard input. Each consists of two integer numbers a2, n (2 ≤ a2 ≤ 1 000, 1 ≤ n ≤ 1 000 000 000) separated by spaces. The line K+1 will contain two zeros, which shouldn't be processed. OutputWrite out K lines of output - one for each testcase. For each testcase the line should contain the smallest positive integer m such that am=anan+1 or the number 0 if such an m doesn't exist. ExampleInput: ScoringFor solving this problem you will score 10 points.
|
||||||||||||||||||
| |||||||||||||||||||