反模运算符 [英] Reverse Modulus Operator

查看:88
本文介绍了反模运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问这个问题 3 年多后,我找到了解决方案.我已将其作为答案包含在内.

Over 3 years after asking the question I found the solution. I have included it as an answer.

我有一个包含模数的表达式,需要用 x 表示.

I have an expression with modulus in it that needs to be put in terms of x.

(a + x) mod m = b

(a + x) mod m = b

我不知道如何处理模数.有没有办法自己得到 x ,还是我不走运?

I can't figure out what to do with the modulus. Is there a way to get x by itself, or am I out of luck on this one?

编辑:我意识到我可以得到多个答案,但我正在寻找一个落在 m 范围内的答案.

Edit: I realize that I can get multiple answers, but I'm looking for an answer that falls within the range of m.

推荐答案

我正在重新审视这个问题,并意识到根据 @Gorcha 给出的答案,这是可能的.

I was revisiting this question and realized it is possible based off of the answer @Gorcha gave.

(a + x) mod m = b  
a + x = nm + b  
x = nm + b - a for some integer n

我不知道为什么我之前没有意识到,但是可以通过将 n 设置为 0 来导出解决方案.

I don't know why I didn't realize it before but the solution can be derived by setting n to 0.

我的问题的答案似乎是 x = b - a,尽管在​​示例中 (26 + x) mod 29 = 3 结果是 -23,小于 m.要将 -23 恢复到预期范围内,将其修改为 29,得到 6.虽然在问题中未指定,但它给出了一个介于 0 和 m 之间的值.

The answer to my question then appears to be x = b - a, though in the example (26 + x) mod 29 = 3 the result is -23, which is less than m. To get -23 back in the expected range mod it with 29, which gives 6. While not specified in the question this gives a value between 0 and m.

最后的解就变成了:x = (b - a) mod m

I.E.

(26 + x) mod 29 = 3
x = (3 - 26) mod 29
x = -23 mod 29
x = 6

将 x 置于 0 到 m 的范围内.检查将显示 (26 + 6) mod 29 = 3.

Which puts x in the range of 0 to m. Checking will show (26 + 6) mod 29 = 3.

这篇关于反模运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆