Time limit: 1 second Memory limit: 256 MB We have a binary tree of fractions with…
- User
Time limit: 1 second Memory limit: 256 MB We have a binary tree of fractions with fractions of the form on each vertex a i b i b i
a i
is written The tree is built like this:
a 1 a 1
b 1 b 1
1 1 a 2 i a 2 i = ( a i a i + b i b i ), b 2 i b 2 i
b i b i (child on the left) a 2 i + 1 a 2i+1
a i a i , b 2 i + 1 b 2i+1 = ( a i a i + b i b i ) (child on the right) We define the distance between two vertices in the tree as the number of single edges in the path between them.
to you Q Q request that each include a fraction to the form p i q i q i
p i
is given
For each number in the requests, if there is only one vertex in the tree whose fraction is equal to the given input fraction, print the distance of the vertex of that fraction to vertex 1 in the output, and otherwise
1 Output −1.
input In the first line Q Q and in Q Q next line p i p i and q i q i given to you
1 ≤ Q ≤ 100
000 1≤Q≤100 000
1 ≤ p i , q i ≤ 1 0 18 1≤p i Q i ≤10 18
output on the line i If the fraction is equal to the number p i q i q i
p i
and only this fraction was equal to this number, output its distance from the vertex 1 otherwise
1 Output −1.
Example Sample input 1 2 1 2 3 5
Plain text Sample output 1 1 3 write code
- Assistant