Fractal Origami (Codeforces 1925F)
Tags: Math, geometry

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):
MOD = 999999893

def inverse(x):
    return pow(x, MOD - 2, MOD)
t = int(input())
for i in range(t):
    n = int(input())
    a = c = (pow(2, n // 2 + 1, MOD) - 2) % MOD
    b = d = (pow(2, (n - 1) // 2 + 1, MOD) - 2) % MOD
    b = (b + 2) % MOD
    ans = (a * d - b * c) * inverse(a * a - 2 * b * b) % MOD
    print(ans)

Solution (Python):
""""""
mod = 999999893

def inv(x):
    return pow(x, mod - 2, mod)

def mul(a_normal_coef, a_z_coef, b_normal_coef, b_z_coef):
    normal_coef = a_normal_coef * b_normal_coef % mod + 2 * a_z_coef * b_z_coef % mod
    normal_coef %= mod
    z_coef = a_normal_coef * b_z_coef % mod + a_z_coef * b_normal_coef % mod
    z_coef %= mod
    return (normal_coef, z_coef)

def div(num_normal_coef, num_z_coef, den_normal_coef, den_z_coef):
    (den2_normal_coef, den2_z_coef) = mul(den_normal_coef, den_z_coef, den_normal_coef, -den_z_coef)
    assert den2_z_coef == 0
    (num2_normal_coef, num2_z_coef) = mul(num_normal_coef, num_z_coef, den_normal_coef, -den_z_coef)
    D = inv(den2_normal_coef)
    return (num2_normal_coef * D % mod, num2_z_coef * D % mod)

def solve(N):
    a = 2
    n = N - 1
    if n % 2 == 0:
        b_normal_coef = (pow(2, n // 2, mod) - 1) % mod
        b_z_coef = 0
    else:
        b_normal_coef = -1
        b_z_coef = pow(2, n // 2, mod)
    a_normal_coef = 2
    a_z_coef = 0
    c_normal_coef = -1
    c_z_coef = 1
    (M_normal_coef, M_z_coef) = mul(a_normal_coef, a_z_coef, b_normal_coef, b_z_coef)
    (M_normal_coef, M_z_coef) = div(M_normal_coef, M_z_coef, c_normal_coef, c_z_coef)
    V_normal_coef = M_normal_coef
    V_z_coef = (2 + M_z_coef) % mod
    (ans_normal_coef, ans_z_coef) = div(M_normal_coef, M_z_coef, V_normal_coef, V_z_coef)
    return ans_z_coef

def main():
    t = int(input())
    allans = []
    for _ in range(t):
        N = int(input())
        ans = solve(N)
        allans.append(ans)
    multiLineArrayPrint(allans)
    return
import sys
input = sys.stdin.buffer.readline

def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
    return [int(x) for x in input().split()]

def makeArr(defaultValFactory, dimensionArr):
    dv = defaultValFactory
    da = dimensionArr
    if len(da) == 1:
        return [dv() for _ in range(da[0])]
    else:
        return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(a, b, c):
    print('? {} {} {}'.format(a, b, c))
    sys.stdout.flush()
    return int(input())

def answerInteractive(x1, x2):
    print('! {} {}'.format(x1, x2))
    sys.stdout.flush()
inf = float('inf')
from math import gcd, floor, ceil
import math
for _abc in range(1):
    main()

Solution (Python):
import sys
import random
input = sys.stdin.readline
rd = random.randint(10 ** 9, 2 * 10 ** 9)
Mod = 999999893
t = int(input())
for _ in range(t):
    n = int(input())
    if n % 2:
        (a, b) = (1 - pow(2, n // 2, Mod), 0)
        (c, d) = (-1 - pow(2, n // 2, Mod), 1)
    else:
        (a, b) = (1, -pow(2, n // 2 - 1, Mod))
        (c, d) = (-1, -pow(2, n // 2 - 1, Mod) + 1)
    q = (c * c - 2 * d * d) % Mod
    p = -a * d + b * c
    print(p * pow(q, Mod - 2, Mod) % Mod)