For each rational a/b, print the smallest n from 0 to 10 whose stage set misses it, or -1 when all eleven contain it.
Medium5MathSimulationNo attempts yetTime limit3sMemory limit256 MBWhile studying set theory, Seunghyun ran into the following family of sets.
C0=[0,1]
Cn={a+∑i=1n3iai0≤a≤3n1,ai∈{0,2}}(n∈N)
C=⋂n=0∞Cn
C made no sense to him, so he planned two jobs. The first job picks a rational number and decides whether it belongs to C. If it does not, the second job finds the set with the smallest subscript n that fails to contain the number.
After doing this by hand a few times he got tired of it and decided to use a computer. Then he got tired of the coding too, so he handed the work to you, his junior. Stuck with the job, you decided to write an incomplete checker to play a trick on him.
If the answers look too strange he will not fall for it, so the output has to stay plausible. The program checks membership only for C0 through C10, and if all eleven sets contain the number it declares the number to be in C. Write that incomplete program.
The first line contains the number of test cases T.
Each of the next T lines contains the numerator a and the denominator b of a rational number, separated by a space. (0≤a≤100000, 1≤b≤100000)
Print one line per test case.
If some set among C0 through C10 does not contain ba, print the smallest such subscript n. If all eleven sets contain it, print -1.
C1=[0,31]∪[32,1], and C2=[0,91]∪[92,31]∪[32,97]∪[98,1].
21 belongs to C0 but not to C1. 61 belongs through C1 and drops out at C2.
A number with numerator 0 lies in every set: take a=0 and every ai=0 in the definition.
If ba is greater than 1, it already fails at C0, so the answer is 0.