Algorithms for repeated squaring(重复乘方)

重复乘方(repeated squaring)算法是分治法的一种,其时间复杂度为Θ(lgn) (T(n)=T(n/2)+Θ(1)


We've learned two algorithms for the modular exponentiation problem(模块化乘方问题), one we call the naive algorithm and one we call the repeated squaring algorithm. The naive algorithm simply starts with b and multiplies by b over and over, calculating the result mod m each time. After k-1 multiplications, the result is tex2html_wrap_inline93.

The repeated squaring algorithm consists of two parts. 

  1. In the first part, the algorithm starts with b, then multiplies it by itself (''squares'' it) mod m, then squares the result mod m, and then squares that mod m, etc. 
  2. In the second part, the algorithm combines together some of these results, multiplying them together mod m. In order to make the algorithm more precise, we need the notion of binary expansion.
介绍十进制和二进制

We ordinarily write numbers in base 10 (``decimal'') notation. The rightmost digit is in the ones place, the second-to-rightmost digit is in the tens place, the third-to-rightmost digit is in the hundreds place, and so on. Thus 765 represents 7 times tex2html_wrap_inline105 plus 6 times tex2html_wrap_inline107 plus 5 times tex2html_wrap_inline109 .

Another system for writing numbers is base 2 (``binary''), where the rightmost digit is in the ones place, the second-to-rightmost digit is in the twos place, the third-to-rightmost digit is in the fours place, the fourth-to-rightmost digit is in the eights place, and so on. Thus 100111 represents 1 times tex2html_wrap_inline113 plus 0 times tex2html_wrap_inline115 plus 0 times tex2html_wrap_inline117 plus 1 times tex2html_wrap_inline119 plus 1 times tex2html_wrap_inline121 plus 1 times tex2html_wrap_inline123 , which is 39. The digits in binary notation are called ``bits'' (short for ``binary digits''). By convention, the leftmost bit must be a 1 (just as ordinarily we don't write the number 7 as 007).


Back to the repeated-squaring algorithm. To calculate tex2html_wrap_inline93 , we write k in binary. The position of the leftmost bit tells us how many squarings need to take place in the first part of the algorithm. For example, if the leftmost bit is in the tex2html_wrap_inline129 s place, then we need to calculate tex2html_wrap_inline131 . Since the first number is just b, we need 11 squarings. The ones in the binary representation of k tell us which powers of b that we have calculated we need to combine to get the final result. 


Here is an example, the multiplications needed to calculate tex2html_wrap_inline139 . Next to each multiplication row, I have indicated an expression for the value calculated in that row.

NAMECOMMANDvalue
result0btex2html_wrap_inline143
result1result0 tex2html_wrap_inline145 result0tex2html_wrap_inline147
result2result1 tex2html_wrap_inline145 result1tex2html_wrap_inline151
result3result2 tex2html_wrap_inline145 result2tex2html_wrap_inline155
result4result3 tex2html_wrap_inline145 result3tex2html_wrap_inline159
result5result4 tex2html_wrap_inline145 result4tex2html_wrap_inline163
result6result5 tex2html_wrap_inline145 result5tex2html_wrap_inline167
result7result6 tex2html_wrap_inline145 result6tex2html_wrap_inline171
result8result7 tex2html_wrap_inline145 result7tex2html_wrap_inline175
result9result8 tex2html_wrap_inline145 result8tex2html_wrap_inline179
result10result9 tex2html_wrap_inline145 result9tex2html_wrap_inline183
result11result10 tex2html_wrap_inline145 result10tex2html_wrap_inline187
finalresultresult3 tex2html_wrap_inline145 result7 tex2html_wrap_inline145 result11tex2html_wrap_inline139

Say the exponent k takes L bits to represent. Then the number of multiplications for the first part of the algorithm is L-1. In the second part of the algorithm, we need to multiply together some of the L results we obtained in the first part. Since we need to multiply together at most L values, we need at most L-1 multiplications in the second part of the algorithm.

The number of bits needed to represent a positive integer k is 1 plus the rounded-down value of tex2html_wrap_inline209 , (the base-2 logarithm of k). For example,tex2html_wrap_inline213 , so the rounded-down value of the logarithm is 11. Thus this formula predicts that the number of bits needed to represent 2184 is 12. (For most purposes, it is good enough to neglect the ''1 plus'' and estimate the number of bits by tex2html_wrap_inline209 .)

Similarly, the number of decimal digits needed to represent a number, say m, is 1 plus the rounded down value of the base-10 logarithm. for example, tex2html_wrap_inline219 is 2.8836614..., so the rounded-down value is 2. Thus this formula predicts that the number of decimal digits to represent 765 is 3.

It is easy to convert between the base-10 log of a number and its base-2 log using the following formula. Memorize it!

displaymath221

(The symbol tex2html_wrap_inline223 denotes ''is approximately equal to''. For those who are curious about where the 3.3 comes from, this is the base-2 logarithm of 10.)


http://cs.brown.edu/courses/cs007/comp/node2.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值