Fractal Origami (Codeforces 1924C)
Tags: Math, matrices

You have a square piece of paper with a side length equal to $$$1$$$ unit. In one operation, you fold each corner of the square to the center of the paper, thus forming another square with a side length equal to $$$\dfrac{1}{\sqrt{2}}$$$ units. By taking this square as a new square, you do the operation again and repeat this process a total of $$$N$$$ times.  Performing operations for $$$N = 2$$$. After performing the set of operations, you open the paper with the same side up you started with and see some crease lines on it. Every crease line is one of two types: a mountain or a valley. A mountain is when the paper folds outward, and a valley is when the paper folds inward.You calculate the sum of the length of all mountain crease lines on the paper and call it $$$M$$$. Similarly, you calculate for valley crease lines and call it $$$V$$$. You want to find the value of $$$\dfrac{M}{V}$$$.It can be proved that this value can be represented in the form of $$$A + B\sqrt{2}$$$, where $$$A$$$ and $$$B$$$ are rational numbers. Let this $$$B$$$ be represented as an irreducible fraction $$$\dfrac{p}{q}$$$, your task is to print $$$p*inv(q)$$$ modulo $$$999\,999\,893$$$ (note the unusual modulo), where $$$inv(q)$$$ is the modular inverse of $$$q$$$.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). Description of the test cases follows.The only line of each test case contains an integer $$$N$$$ ($$$1 \leq N \leq 10^9$$$), the number of operations you perform on the square paper.

Output: For each test case, print on a new line the required answer.

Note: The blue lines in the given figures represent mountain crease lines, and the green lines represent valley crease lines. Crease lines after $$$1$$$ operation $$$(\dfrac{M}{V} = 0)$$$.Crease lines after $$$2$$$ operations $$$(\dfrac{M}{V} = \sqrt{2} - 1)$$$. 

Sample Inputs:
3
1
2
3

Sample Outputs:
0
1
714285638

Solution (Python):
def II():
    return int(input())

def LMI():
    return list(map(int, input().split()))

def I():
    return input()

def MII():
    return map(int, input().split())
import sys
input = sys.stdin.readline
mod = 999999893
import math
for _ in range(II()):
    n = II()
    if n % 2:
        beta = pow(2, (n - 1) // 2, mod) - 1
        alp = beta + 2
        print(beta * pow((alp ** 2 - 2) % mod, -1, mod) % mod)
    else:
        alp = pow(2, (n - 2) // 2, mod) - 1
        bet = alp + 1
        print(-(alp + bet) % mod * pow((2 * alp ** 2 - 1) % mod, -1, mod) % mod)

Solution (Python):
M = 999999893
for t in range(int(input())):
    n = int(input())
    tot = (0, 1)
    if n > 1:
        tot = (2 * (pow(2, n // 2, M) - 1), pow(2, (n + 1) // 2, M) - 1)
    (a, b) = tot
    print(2 * a * pow(2 * (b + 1) * (b + 1) - a * a, -1, M) % M)

Solution (Python):
M = 999999893
for t in range(int(input())):
    n = int(input())
    tot = (0, 1)
    if n > 1:
        tot = (2 * (pow(2, n // 2, M) - 1), pow(2, (n + 1) // 2, M) - 1)
    (x, y) = tot
    print(2 * x * pow(2 * (y + 1) * (y + 1) - x * x, -1, M) % M)