did modular exponentiation with high level of ignorance (mooooooooo tablet, see problem F)
This commit is contained in:
parent
f25d961f9c
commit
e8251c2aad
1 changed files with 18 additions and 4 deletions
|
@ -3,10 +3,24 @@ import sys
|
||||||
|
|
||||||
def mod_exp(x, y, m):
|
def mod_exp(x, y, m):
|
||||||
k = x % m
|
k = x % m
|
||||||
for i in range(y):
|
results = {}
|
||||||
print(i)
|
return exp_m(results, k, y, m)
|
||||||
k = (k * x) % m
|
|
||||||
return k
|
def exp_m(results, k, y, m):
|
||||||
|
if y in results:
|
||||||
|
return results[y]
|
||||||
|
else:
|
||||||
|
if k == 1:
|
||||||
|
ret = 1
|
||||||
|
elif y == 0:
|
||||||
|
ret = 1
|
||||||
|
elif y == 1:
|
||||||
|
ret = k
|
||||||
|
else:
|
||||||
|
h = y // 2
|
||||||
|
ret = (exp_m(results, k, h, m) % m * exp_m(results, k, y - h, m) % m) % m
|
||||||
|
results[y] = ret
|
||||||
|
return ret
|
||||||
|
|
||||||
line = sys.stdin.readline().split()
|
line = sys.stdin.readline().split()
|
||||||
x = int(line[0])
|
x = int(line[0])
|
||||||
|
|
Reference in a new issue