Since it is trivial to reduce m/nm/n to co-prime pairs, let's assume m,nm, n are co-prime. Also by definition of rational numbers we can say that "infinite -1" is not a valid output. If prime factors of nn are also prime factors for bb then the representation of m/nm/n is finite. Otherwise, there exists some prime factors of nn that doesn't divide bb. It can be easily shown that 1/n1/n has similar period length as m/nm/n, so we mainly focus on 1/n1/n in base bb for now. Let's further divide the problem in 2 cases as following:

gcd(n,b)=1gcd(n, b) = 1: 1nb=brnbrb=qn+1nbrb=qbrb+1brb1nb\frac{1}{n}_b = \frac{b^r}{nb^r}_b = \frac{qn+1}{nb^r}_b = \frac{q}{b^r}_b + \frac{1}{b^r}_b\frac{1}{n}_b where br=qn+1br1modnb^r = q*n + 1 \Longleftrightarrow b^r \equiv 1 \mod n. Then 1brb1nb=qb2rb+1b2rb1nb\frac{1}{b^r}_b\frac{1}{n}_b = \frac{q}{b^{2r}}_b + \frac{1}{b^{2r}}_b\frac{1}{n}_b. Thus the length of the repetition is rr, this is also called the order of bb modulo nn. And the output will be "infinite 0 r".

gcd(n,b)1gcd(n, b) \neq 1: Let n=stn = st where gcd(s,b)=1gcd(s, b) = 1. Now 1/n=(1/s)(1/t)1/n = (1/s)(1/t) where 1/t1/t has finite representation in base bb and 1/s1/s is solved in previous case. One can verify that the repetition of 1/s1/s gets shifted by the length of the representation of 1/t1/t. Hence output is "infinite l r" where ll is the length of 1tb\frac{1}{t}_b.

At this point there should be some algorithmic task to find s,ts,tas defined above, which can be found by either prime factorizing n,bn, bor repeatedly using gcd to seperate all common prime factors. And to find order one can utilze euler totient theorem and the fact if xuvmodnx^u \equiv v \mod n then xuyvmodnx^{uy} \equiv v \mod n, so take the divisors ddof ϕ(n)\phi(n) and check if bd1modnb^d \equiv 1 \mod n.

Statistics

0% Solution Ratio
Toph uses cookies. By continuing you agree to our Cookie Policy.