2018-12-29 19:30:21 +08:00
|
|
|
#coding: utf-8
|
|
|
|
''' mbinary
|
|
|
|
#######################################################################
|
|
|
|
# File : fastPow.py
|
|
|
|
# Author: mbinary
|
|
|
|
# Mail: zhuheqin1@gmail.com
|
2019-01-31 12:09:46 +08:00
|
|
|
# Blog: https://mbinary.xyz
|
2018-12-29 19:30:21 +08:00
|
|
|
# Github: https://github.com/mbinary
|
|
|
|
# Created Time: 2018-12-17 21:39
|
|
|
|
# Description: fast power
|
|
|
|
#######################################################################
|
|
|
|
'''
|
|
|
|
|
2020-04-15 12:28:20 +08:00
|
|
|
|
|
|
|
def fastPow(a, n):
|
2018-12-29 19:30:21 +08:00
|
|
|
'''a^n'''
|
|
|
|
rst = 1
|
|
|
|
while n:
|
2020-04-15 12:28:20 +08:00
|
|
|
if n % 2:
|
|
|
|
rst *= a
|
|
|
|
n >>= 1
|
|
|
|
a *= a
|
2018-12-29 19:30:21 +08:00
|
|
|
return rst
|
|
|
|
|
2020-04-15 12:28:20 +08:00
|
|
|
|
|
|
|
def fastMul(a, b):
|
2018-12-29 19:30:21 +08:00
|
|
|
'''a*b'''
|
|
|
|
rst = 0
|
|
|
|
while b:
|
2020-04-15 12:28:20 +08:00
|
|
|
if b & 1:
|
|
|
|
rst += a
|
|
|
|
b >>= 1
|
|
|
|
a *= 2
|