def mod_exp(base, exp, mod):
"""Function to perform modular exponentiation: (base^exp) % mod"""
result = 1
base = base % mod # Handle base greater than mod
while exp > 0:
if exp % 2 == 1: # If exponent is odd
result = (result * base) % mod
exp = exp // 2 # Divide the exponent by 2
base = (base * base) % mod # Square the base
return result
def encrypt_code(S, N, M):
“””Encrypt the secret code S using the keys N and M”””
# Step 1: Calculate (S^N % 10)
S_mod_10 = mod_exp(S, N, 10)
# Step 2: Calculate (S_mod_10^M % 1000000007)
result = mod_exp(S_mod_10, M, 1000000007)
return result
# Example usage
S = 123456789
N = 10000000000
M = 10000000000
print(encrypt_code(S, N, M)) # Outputs the encrypted codemod_exp
):mod_exp
function with a modulus of 10.mod_exp
function with a modulus of 100000000710000000071000000007.