知识卡片

普通读书笔记卡 · 3701

重复平方法递归减半求模幂

理解等级:中级

在模数n≥2、指数b≥0时,

  • 偶数指数先求a^{b/2}再平方取模;
  • 奇数指数先降为b−1再乘a取模;
  • b=0返回1。

每一步都在模n下运算,避免先构造巨大的a^b;n=1时基例需改为1 mod n。

这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • 第31章 数论算法 · 查看原书依据

      The method of repeated squaring solves this problem efficiently. Repeated squaring is based on the following formula to compute ab for nonnegative integers a and b: The last case, where b is odd, reduces to the one of the first two cases, since if b is odd, then b − 1 is even. The recursive procedure MODULAR-EXPONENTIATION on the next page computes ab mod n using equation (31.34), but performing all

    参考来源

    - 位置:《算法导论(第4版)》第31章《数论算法》(OEBPS/chapter031.xhtml,文本字符[55000,60000))。 - 结论依据:公式图 Art_P1208 的式(31.34)为 a^b=1(b=0)、(a^{b/2})²(b>0且偶)、a·a^{b−1}(b>0且奇);MODULAR-EXPONENTIATION 对各分支取模。其 b=0 分支直接返回1,因此作为规范余数时须限定 n≥2;n=1 应返回0。 - 原始内容:The method of repeated squaring solves this problem efficiently. Repeated squaring is based on the following formula to compute a b for nonnegative integers a and b : The last case, where b is odd, reduces to the one of the first two cases, since if b is odd, then b − 1 is even. The recursive procedure M ODULAR -E XPONENTIATION on the next page computes a b mod n using equation (31.34), but performing all