如何计算“模乘逆”当分母不与 m 互质时?

发布于 2024-08-06 23:32:50 字数 941 浏览 9 评论 0原文

我需要计算 (a/b) mod m,其中 ab< /code>是非常大的数字。

我想做的是计算 (a mod m) * (x mod m),其中 x > 是 b模逆

我尝试使用 扩展欧几里得算法,但是当 b 和 m 不互质时该怎么办? 特别提到 b 和 m 需要互质。

我尝试使用代码此处,并意识到例如: 3 * x mod 12对于x的任何值都是不可能的,它不存在!

我应该做什么?可以以某种方式修改算法吗?

I need to calculate (a/b) mod m where a and b are very large numbers.

What I am trying to do is to calculate (a mod m) * (x mod m), where x is the modular inverse of b.

I tried using Extended Euclidean algorithm, but what to do when b and m are not co-prime?
It is specifically mentioned that b and m need to be co-prime.

I tried using the code here, and realized that for example:
3 * x mod 12 is not at all possible for any value of x, it does not exist!

What should I do? Can the algorithm be modified somehow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

執念 2024-08-13 23:32:50

是的,你有麻烦了。如果 b 和 m 有公约数,则 x 在 b*x = 1 mod m 中无解。同样,在原始问题 a/b = y mod m 中,您正在寻找 y 使得 a=by mod m。如果 a 可以被 gcd(b,m) 整除,那么您可以除以该因子并求解 y。如果不是,则没有 y 可以解方程(即未定义 a/b mod m)。

Yep, you are in trouble. x has no solution in b*x = 1 mod m if b and m have a common divisor. Similarly, in your original problem a/b = y mod m, you are looking for y such that a=by mod m. If a is divisible by gcd(b,m), then you can divide out by that factor and solve for y. If not, then there is no y that can solve the equation (i.e. a/b mod m is not defined).

白昼 2024-08-13 23:32:50

b 和 m 必须互质的原因是中国剩余定理。基本上问题是:

3 * x mod 12

可以被认为是涉及

3*x mod 33*x mod 4 = 2^2 的 复合问题

现在,如果 b 不与 12 互质,这就像尝试除以零一样。因此答案不存在!

这是由于抽象代数中的场论。域基本上是一个具有明确定义的加法、减法、乘法和除法的集合。有限域的形式始终为 GF(p^n),其中 p 为素数,n 为正整数,运算为对 p^n 取模的加法和乘法。现在,12 不是质数幂,因此您的不是一个字段。因此,对于任何不与 m 互质的 b,这个问题都无法解决。

The reason that b and m have to be coprime is because of the Chinese Remainder Theorem. Basically the problem:

3 * x mod 12

Can be thought of as a compound problem involving

3*x mod 3 and 3*x mod 4 = 2^2

Now if b is not coprime to 12, this is like trying to divide by zero. Thus the answer doesn't exist!

This is due to field theory in abstract algebra. A field is basically a set which has addition, subtraction, multiplication, and division well-defined. A finite field is always of the form GF(p^n), where p is prime and n is a positive integer, and the operations are addition and multiplication modulo p^n. Now, 12 is not a prime power, so your ring is not a field. Thus this problem can't be solved for any b which is not coprime to m.

胡渣熟男 2024-08-13 23:32:50

检查这个:http://www.math.harvard.edu/~sarah /magic/topics/division
这可能有帮助。
它解释了模块划分的方法。

Check this: http://www.math.harvard.edu/~sarah/magic/topics/division
It might help.
It explains methods of modular division.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文