acm-模运算

新浪博客 发表时间 -- 2009-07-26 20:19:43

很多地方用到模运算,这里说明模运算的一些规律,并加以证明。 后续会对这些理论实际的应用加以记录和说明。

1. 模运算是取余运算(记做 % 或者 mod),具有周期性的特点。 m%n的意思是n除m后的余数, 当m递增时m%n呈现周期性特点,并且n越大,周期越长,周期等于n。
<wbr><wbr><wbr><wbr> 例如<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> 0 % 20 = 0,1 % 20 = 1, 2 % 20 = 2, 3 % 20 = 3, ..., 19 % 20 = 19<br><wbr><wbr><wbr><wbr><wbr><wbr> 20 % 20 = 0,21 % 20 = 1,22 % 20 = 2,23 % 20 = 3, ...,39 % 20 = 19<br> 2. 如果 m % n = r,那么可以推出如下等式<br><wbr><wbr><wbr><wbr> m = k * n + r (k为大于等于0的整数, r &lt;= m)<br> 3. 同余式, 表示正整数a,b对n取模,它们的余数相同,记做 a ≡ b mod n或者a = b (mod n)。<br><wbr><wbr><wbr> 根据2的等式可以推出 a = kn + b 或者 a - b = kn<br><wbr><wbr><wbr> 证明:<wbr><wbr> ∵ a = k1 * n + r1<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> b = k2 * n + r2<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> ∴ a - b = (k1 - k2) * n + (r1 - r2)<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> a = k * n + (r1 - r2) + b<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> ∵ a, b对n取模同余,r1 = r2<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> ∴ a = k * n + b (k = k1 - k2)<br> 4. 模运算规则, 模运算与基本四则运算有些相似,但是除法例外。其规则如下<br><wbr><wbr><wbr> (a + b) % n = (a % n + b % n) % n<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> (1)<br><wbr><wbr><wbr> (a - b) % n = (a % n - b % n) % n<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> (2)<br><wbr><wbr><wbr> (a * b) % n = (a % n * b % n) % n<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> (3)<br><wbr><wbr><wbr> a<sup>b</sup> % n = ((a % n)<sup>b</sup>) % n<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> (4)<br><br> (1)式证明<br> ∵ a = k1*n + r1</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr> b = k2*n + r2</wbr></wbr>

a % n = r1

b % n = r2

∴(a+b) % n = ((k1+k2)*n + (r1+r2)) % n = (r1+r2) % n = (a % n + b % n)% n
<wbr><wbr><wbr><wbr> 得证<br> (2)式证明同上<br> (3)式证明<br><wbr><wbr><wbr><wbr> a = k1*n + r1<br><wbr><wbr><wbr><wbr><wbr> b = k2*n + r2<br><wbr><wbr><wbr><wbr> (a*b) % n = (k1k2<span style="font-family:Tahoma">n<sup>2</sup></span> + (k1r2+k2r1)n + r1r2) % n = r1r2 % n = (a %n * b %n ) % n<br> (4)式证明<br><wbr><wbr><wbr><wbr><wbr><wbr> 设 a % n = r<br><wbr><wbr><wbr><wbr><wbr> a<sup>b</sup> %n= (a * a * a * a…*a) %n = (a %n * a %n * a %n * … * a %n) %n = r<sup>b</sup> % n = ((a % n) <sup>b</sup>) % n<br><br> 模运算看起来不是很直观,但是可以用来推导出一些有用的东西。 例如(4)式可以用来降幂运算,例如计算62<sup>65</sup> % 133,直接计算的话需要算出62<sup>65</sup> 利用(4)式可以进行降幂运算。<br><wbr><wbr> 62<sup>65</sup> % 133<br> = 62 * 62<sup>64</sup> % 133<br> = 62 * (62<sup>2</sup>)<sup>32</sup> % 133<br> = 62 * 3844<sup>32</sup> % 133<br> = 62 * (3844 % 133)<sup>32</sup> % 133<br> = 62 * 120<sup>32</sup> % 133</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

= 62 * 3616 % 133
= 62 * 998 % 133
= 62 * 924 % 133
= 62 * 852 % 133
= 62 * 43 % 133
= 2666 % 133
= 6

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值