有效的除法求幂
((a ** p - 1) // (a - 1)) % m == ((pow(a, p, x) - 1) // (a - 1)) % m
在给定 a
、p
和 m
的情况下,我怎样才能找到一个可以工作的 x
呢?难道只是x = ((a - 1) * m)
吗?它们都是整数,并且 m
是素数。只是 a ** p
太大了,计算起来很慢,而且没有必要。
我正在尝试计算线性变换的组成。假设有两个函数:
A = lambda x: (a * x + b) % m
B = lambda x: (c * x + d) % m
组合它们很简单。但计算 A
的重复应用正是我想要做的。将函数 C
定义为 A(x) == C(A, 1)(x)
, A(A(x)) == C(A 、 2)(x)
、A(A(A(x))) == C(A, 3)(x)
等等。 B
也可以。
C(A, n)(x) == (pow(a, n, m) * x + b * ((a ** n - 1) // (a - 1))) % m
C(B, n)(x) == (pow(c, n, m) * x + d * ((c ** n - 1) // (c - 1))) % m
我并不是想反算A
。我正在尝试转发计算C
。
I need to calculate to compute a repeated application of a linear transform under a modulus.
((a ** p - 1) // (a - 1)) % m == ((pow(a, p, x) - 1) // (a - 1)) % m
How could I figure an x
that would work given a
, p
, and m
? Could it just be x = ((a - 1) * m)
? They're all integers and m
is prime. It's just that a ** p
is simply far to large and for to slow to calculate, and it's unnecessary.
I'm trying to compute the composition of linear transforms. Say there are two functions:
A = lambda x: (a * x + b) % m
B = lambda x: (c * x + d) % m
Composing them is simple. But calculating the repeated application of A
is what I'm trying to do. Define the function C
as A(x) == C(A, 1)(x)
, A(A(x)) == C(A, 2)(x)
, A(A(A(x))) == C(A, 3)(x)
, and so on. B
works too.
C(A, n)(x) == (pow(a, n, m) * x + b * ((a ** n - 1) // (a - 1))) % m
C(B, n)(x) == (pow(c, n, m) * x + d * ((c ** n - 1) // (c - 1))) % m
I'm not trying to back-calculate A
. I'm trying to forward calculate C
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论