python实现快速幂(支持大整数与取模)。

快速幂算法(也称为“二分求幂”)是一种高效计算大整数幂次的方法,特别是在需要对结果取模时非常有用。它通过将指数分解为二进制形式,利用幂的性质减少乘法次数,从而提高计算效率。

算法原理

快速幂算法的核心思想是将幂次运算分解为一系列平方和乘法操作。对于计算  abmod  m a b mod m,算法步骤如下:

  1. 将指数  b b 表示为二进制形式。
  2. 从最低位到最高位依次处理每一位:
    • 如果当前位是 1,则将当前基数乘到结果中。
    • 无论当前位是 0 还是 1,都将基数平方。
  3. 在每一步中,对结果和基数取模,以防止数值过大。

实现代码

以下是 Python 中实现快速幂算法的示例代码:

pythonCopy Codedef modular_exponentiation(base, exponent, modulus):    """
    计算 (base^exponent) % modulus 使用快速幂算法。
    :param base: 底数
    :param exponent: 指数
    :param modulus: 模数
    :return: (base^exponent) % modulus 的结果
    """
    result = 1
    base = base % modulus  # 处理 base 大于 modulus 的情况
    while exponent > 0:        # 如果 exponent 是奇数,将 base 乘到结果中
        if exponent % 2 == 1:
            result = (result * base) % modulus        
        # 将 exponent 右移一位(相当于整除 2)
        exponent = exponent // 2
        
        # 将 base 平方
        base = (base * base) % modulus    return result# 示例用法base = 2exponent = 10modulus = 1000print(modular_exponentiation(base, exponent, modulus))  # 输出 24,因为 2^10 % 1000 = 1024 % 1000 = 24

代码说明

  • 初始化‌: result 初始化为 1,因为任何数的 0 次幂都是 1。
  • 取模处理‌:在计算之前,先对  base 取模,以防止初始值过大。
  • 循环处理‌:
    • 当  exponent 大于 0 时,继续循环。
    • 如果  exponent 是奇数,将当前的  base 乘到  result 中,并对  result 取模。
    • 将  exponent 右移一位(即整除 2),相当于处理下一位。
    • 将  base 平方,并对  base 取模,准备处理下一位。
  • 返回结果‌:循环结束后, result 即为所求的结果。

优势

  • 时间复杂度‌:快速幂算法的时间复杂度为  O(log⁡b) O ( lo g b ),其中  b b 是指数。这比直接计算  ab a b 的  O(b) O ( b ) 要高效得多。
  • 空间复杂度‌:算法使用常数空间,空间复杂度为  O(1) O ( 1 )

通过这种方法,你可以高效地计算大整数的幂次,并对结果取模,适用于密码学、数论等领域。


http://travel.zgjzs.org

http://hotel.zgjzs.org

http://food.zgjzs.org

http://tech.zgjzs.org

http://health.zgjzs.org

http://edu.zgjzs.org

http://music.zgjzs.org

http://sports.zgjzs.org

http://fashion.zgjzs.org

http://auto.zgjzs.org

http://finance.zgjzs.org

http://law.zgjzs.org

http://art.zgjzs.org

http://book.zgjzs.org

http://movie.zgjzs.org

http://game.zgjzs.org

http://job.zgjzs.org

http://house.zgjzs.org

http://kids.zgjzs.org

http://pet.zgjzs.org

http://beauty.zgjzs.org

http://garden.zgjzs.org

http://science.zgjzs.org

http://history.zgjzs.org

http://culture.zgjzs.org

http://city.zgjzs.org

http://weather.zgjzs.org

http://map.zgjzs.org

http://data.zgjzs.org

http://energy.zgjzs.org

http://eco.zgjzs.org

http://farm.zgjzs.org

http://market.zgjzs.org

http://trade.zgjzs.org

http://service.zgjzs.org

http://club.zgjzs.org

http://event.zgjzs.org

http://festival.zgjzs.org

http://expo.zgjzs.org

http://museum.zgjzs.org

http://library.zgjzs.org

http://school.zgjzs.org

http://hospital.zgjzs.org

http://bank.zgjzs.org

http://insurance.zgjzs.org

http://stock.zgjzs.org

http://invest.zgjzs.org

http://tour.zgjzs.org

http://electronics.shangpin.org

http://clothing.shangpin.org

http://furniture.shangpin.org

http://jewelry.shangpin.org

http://cosmetic.shangpin.org

http://digital.shangpin.org

http://appliance.shangpin.org

http://outdoor.shangpin.org

http://sporting.shangpin.org

http://luxury.shangpin.org

http://grocery.shangpin.org

http://office.shangpin.org

http://tool.shangpin.org

http://craft.shangpin.org

http://antique.shangpin.org

http://collectible.shangpin.org

http://handmade.shangpin.org

http://organic.shangpin.org

http://baby.shangpin.org

http://watch.shangpin.org

http://bag.shangpin.org

http://shoe.shangpin.org

http://glass.shangpin.org

http://ceramic.shangpin.org

http://textile.shangpin.org

http://metal.shangpin.org

http://wood.shangpin.org

http://stone.shangpin.org

http://plastic.shangpin.org

http://paper.shangpin.org

http://ink.shangpin.org

http://paint.shangpin.org

http://lighting.shangpin.org

http://kitchen.shangpin.org

http://bath.shangpin.org

http://bed.shangpin.org

http://sofa.shangpin.org

http://carpet.shangpin.org

http://curtain.shangpin.org

http://lamp.shangpin.org

http://mirror.shangpin.org

http://clock.shangpin.org

http://vase.shangpin.org

http://frame.shangpin.org

http://toy.shangpin.org

http://instrument.shangpin.org

http://plant.shangpin.org

http://gift.shangpin.org

http://business.snjrw.org

http://media.snjrw.org

http://design.snjrw.org

http://photo.snjrw.org

http://video.snjrw.org

http://studio.snjrw.org

http://network.snjrw.org

http://cloud.snjrw.org

http://career.snjrw.org

http://learning.snjrw.org

http://research.snjrw.org

http://innovation.snjrw.org

http://project.snjrw.org

http://solution.snjrw.org

http://global.snjrw.org

http://local.snjrw.org

http://china.snjrw.org

http://asia.snjrw.org

http://europe.snjrw.org

http://america.snjrw.org

http://africa.snjrw.org

http://ocean.snjrw.org

http://space.snjrw.org

http://earth.snjrw.org

http://nature.snjrw.org

http://green.snjrw.org

http://future.snjrw.org

http://ai.snjrw.org

http://robot.snjrw.org

http://vr.snjrw.org

http://ar.snjrw.org

http://iot.snjrw.org

http://blockchain.snjrw.org

http://crypto.snjrw.org

http://nft.snjrw.org

http://metaverse.snjrw.org

http://smart.snjrw.org

http://safe.snjrw.org

http://privacy.snjrw.org

http://security.snjrw.org

http://trust.snjrw.org

http://quality.snjrw.org

http://certified.snjrw.org

http://premium.snjrw.org

http://elite.snjrw.org

http://vip.snjrw.org

http://prime.snjrw.org

http://gold.snjrw.org

请使用浏览器的分享功能分享到微信等