Sort by

recency

|

9 Discussions

|

  • + 0 comments

    Python 3 Solution :

    M = 1000000007
    
    
    def power(x, y):
        x %= M
        ans = 1
        while y > 0:
            if y & 1 == 1:
                ans = ans*x % M
            x = x*x % M
            y >>= 1
        return(ans)
    
    
    def tan(b, n):
        if n == 1:
            return b
        if n % 2 == 0:
            a = tan(b, n//2)
            return(2*a % M*power((1-a**2 % M+M) % M, M-2) % M)
        else:
            a = tan(b, n-1)
            soorat = (a+b) % M
            makhraj = (1-a*b % M+M) % M
            return(soorat*power(makhraj, M-2) % M)
    
    
    def solve(p, q, n):
        return(tan(p*power(q, M-2) % M, n))
    
    
    for _ in range(int(input())):
        p, q, n = map(int, input().split())
        print(solve(p, q, n))
    
  • + 1 comment

    I got the values of final p and q but I didn't get the modulo thing..can anyone help me in this.............

    • + 1 comment

      Consider without modulo. a = bx, so to solve for x, x = a/b. Unfortunately we can't really divide under modulo, but we can write this equation as a * b^-1 = x or x is a times the multiplicative inverse of b.

      Luckily we can find the mulitplicative inverse of b under modulo M (because M=10^9 + 7 is prime, so b | b < M is coprime to M). We can calculate it by (b^(M-2))%M

      • + 0 comments

        Actually, the euclidean extended algorithm is faster than the fast exponentiation of b^(M-2)%m https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm because b and M are corpime (statted in the problem and M is prime) there exists a bezout identity X*b + Y*M = 1, so in modulo M, the inverse of b is X.

  • + 0 comments

    In deriving tan(n * alpha) we can use tangent identity recursively until it reaches tan(alpha).

    tan(alpha + beta)
    = [tan(alpha) + tan(beta)] / [1 - tan(alpha) * tan(beta)]
    

    The right hand side expanded recursively overflows. To avoid overflow we can ues modular division.

    (a / b) mod n
    = [(a mod n)(b^(−1) mod n)] mod n
    
    when the right hand side is defined
    (that is when b and n are coprime).
    

    To find modular multiplicative inverse of b modulo n, i.e. x below,

    bx ≡ 1 mod n
    
    or
    
    bx + ny = gcd(b, n) = 1
    

    use Extended Eucleadean Algorithm that finds quotients x and y in addition to gcd(b, n).

  • + 0 comments

    The first and most straightforward approach is to use the addition formula for tangents: This gives us a way to compute given similarly to fast exponentiation: def tan_add(t,u): [// compute tan(a+b) given tan(a) = t and tan(b) = u return (a + b) / (1 - a*b)

    def tan(n,t): // compute tan(n*a) given tan(a) = t if n == 1:

            return t
    else if n % 2 == 0:
            result = tan(n/2,t)
            return tan_add(result, result)
    else:
            result = tan(n-1,t)
         return tan_add(result, t)](http://)
    
  • + 0 comments

    def tan_add(t,u): // compute tan(a+b) given tan(a) = t and tan(b) = u return (a + b) / (1 - a*b)

    def tan(n,t): // compute tan(n*a) given tan(a) = t if n == 1: return t else if n % 2 == 0: result = tan(n/2,t) return tan_add(result, result) else: result = tan(n-1,t) return tan_add(result, t)