5

我将一些基础 b 提高到 p 的幂并取其模 m。

让我们假设 b=55170 或 55172 和 m=3043839241(恰好是 55171 的平方)。linux-calculatorbc给出了结果(我们需要这个来控制):

echo "p=5606;b=55171;m=b*b;((b-1)^p)%m;((b+1)^p)%m" | bc
2734550616
309288627

现在计算 55170^5606 给出了一个有点大的数字,但由于我必须进行模运算,我想我可以绕过 BigInt 的使用,因为:

(a*b) % c == ((a%c) * (b%c))%c i.e.
(9*7) % 5 == ((9%5) * (7%5))%5 =>
63 % 5    == (4     *    2) %5 =>
3         == 8 % 5

... 并且 a^d = a^(b+c) = a^b * a^c,因此我可以将 b+c 除以 2,对于偶数或奇数 ds d/2 和 d-(d /2),所以对于 8^5,我可以计算 8^2 * 8^3。

所以我的(有缺陷的)方法总是在运行中切断除数,看起来像这样:

def powMod (b: Long, pot: Int, mod: Long) : Long = { 
      if (pot == 1) b % mod else {
          val pot2 = pot/2
          val pm1 = powMod (b, pot2, mod)             
          val pm2 = powMod (b, pot-pot2, mod)           
          (pm1 * pm2) % mod 
      } 
}

并提供了一些价值,

powMod (55170, 5606, 3043839241L) 
res2: Long = 1885539617
powMod (55172, 5606, 3043839241L) 
res4: Long = 309288627

如我们所见,第二个结果与上面的结果完全相同,但第一个结果看起来完全不同。我正在做很多这样的计算,只要它们保持在 Int 的范围内,它们似乎是准确的,但我看不到任何错误。使用 BigInt 也可以,但是太慢了:

def calc2 (n: Int, pri: Long) = {
    val p: BigInt = pri
    val p3 = p * p
    val p1 = (p-1).pow (n) % (p3)
    val p2 = (p+1).pow (n) % (p3)
    print ("p1: " + p1 + " p2: " + p2)
}

calc2 (5606, 55171) 
p1: 2734550616 p2: 309288627

(结果与 bc 相同)有人可以看到错误powMod吗?

4

3 回答 3

4

我想答案就在这里:

scala> math.sqrt(Long.MaxValue).toLong < 3043839241L
res9: Boolean = true

这意味着即使对于小于该特定模块值的数字,您也可能会出现长时间溢出。让我们试着抓住它:

scala> def powMod (b: Long, pot: Int, mod: Long) : Long = {
     |       if (pot == 1) b % mod else {
     |           val pot2 = pot/2
     |           val pm1 = powMod (b, pot2, mod)
     |           val pm2 = powMod (b, pot-pot2, mod)
     |           val partial = ((pm1 % mod) * (pm2 % mod)).ensuring(res =>
     |             res > pm1 % mod && res > pm2 % mod, "Long overflow multiplying "+pm1+" by "+pm2)
     |           partial % mod
     |       }
     | }
powMod: (b: Long,pot: Int,mod: Long)Long

scala> powMod (55170, 5606, 3043839241L)
java.lang.AssertionError: assertion failed: Long overflow multiplying 3042625480 by 3042625480

你有它。

于 2010-05-17T15:27:15.610 回答
2

不熟悉Scala,但是...

def powMod (b: Long, pot: Int, mod: Long) : Long = {  
      if (pot == 1) b % mod else { 
          val pot2 = pot/2 
          val pm1 = powMod (b, pot, mod)              
          val pm2 = powMod (b, pot-pot2, mod)            
          (pm1 * pm2) % mod  
      }  
} 

你的意思是

          val pm1 = powMod (b, pot2, mod) 

注意 pot2 而不是 pot。

奇怪的是,这似乎应该永远循环/溢出堆栈,但谁知道 Scala 在做什么。

于 2010-05-17T06:39:44.740 回答
1

好吧,伙计们,我花了一些时间,最终破坏了一个长期但从未被证实的假设,即如果你将两个 64 位正整数值相乘(又名:Longs,毕竟实际上只有 63 位),您可能会超出并获得负值,但不会再次超出正值(但错误)。

所以我试图在我的代码中加入一个守卫,用 BigInt 计算我的值,它太大了,但是守卫不够,因为我测试了res < 0. res < pm1 && res < pm2也不够。

为了提高速度,我使用了 mutable.HashMap,现在代码如下所示:

val MVL : Long = Integer.MAX_VALUE
var modPow = new scala.collection.mutable.HashMap [(Long, Int, Long), Long ] () 

def powMod (b: Long, pot: Int, mod: Long) : Long = { 
      if (pot == 1) b % mod else modPow.getOrElseUpdate ((b, pot, mod), {
    val pot2= pot/2
    val pm1 = powMod (b, pot2, mod)             
    val pm2 = powMod (b, pot-pot2, mod)
    val res = (pm1 * pm2) 
    // avoid Long-overrun
    if (pm1 < MVL && pm2 < MVL)
        res % mod else {
            val f1: BigInt = pm1
            val f2: BigInt = pm2
            val erg = (f1 * f2) % mod
            erg.longValue 
        }
      })
}

您可能会问自己,Long-declared MVL 是否真的需要,或者是否真的需要

if (pm1 < Integer.MAX_VALUE && ...

也会奏效的。不,不会。:) 另一个要避免的陷阱。:)

最后它非常快速和正确,我学到了两个关于溢出和 MAX_VALUE - 比较的课程。

于 2010-05-18T02:52:34.557 回答