<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>https://wiki.xhsr.org.cn/index.php?action=history&amp;feed=atom&amp;title=%E7%AE%97%E6%B3%95%3A_%E6%B1%82%E6%A8%A1%E6%84%8F%E4%B9%89%E4%B8%8B%E7%9A%84%E4%B9%98%E6%B3%95%E9%80%86%E5%85%83</id>
	<title>算法: 求模意义下的乘法逆元 - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.xhsr.org.cn/index.php?action=history&amp;feed=atom&amp;title=%E7%AE%97%E6%B3%95%3A_%E6%B1%82%E6%A8%A1%E6%84%8F%E4%B9%89%E4%B8%8B%E7%9A%84%E4%B9%98%E6%B3%95%E9%80%86%E5%85%83"/>
	<link rel="alternate" type="text/html" href="https://wiki.xhsr.org.cn/index.php?title=%E7%AE%97%E6%B3%95:_%E6%B1%82%E6%A8%A1%E6%84%8F%E4%B9%89%E4%B8%8B%E7%9A%84%E4%B9%98%E6%B3%95%E9%80%86%E5%85%83&amp;action=history"/>
	<updated>2026-06-02T21:54:55Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.39.12</generator>
	<entry>
		<id>https://wiki.xhsr.org.cn/index.php?title=%E7%AE%97%E6%B3%95:_%E6%B1%82%E6%A8%A1%E6%84%8F%E4%B9%89%E4%B8%8B%E7%9A%84%E4%B9%98%E6%B3%95%E9%80%86%E5%85%83&amp;diff=3896&amp;oldid=prev</id>
		<title>2025年1月9日 (四) 12:15 Luke li</title>
		<link rel="alternate" type="text/html" href="https://wiki.xhsr.org.cn/index.php?title=%E7%AE%97%E6%B3%95:_%E6%B1%82%E6%A8%A1%E6%84%8F%E4%B9%89%E4%B8%8B%E7%9A%84%E4%B9%98%E6%B3%95%E9%80%86%E5%85%83&amp;diff=3896&amp;oldid=prev"/>
		<updated>2025-01-09T12:15:25Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2025年1月9日 (四) 20:15的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l16&quot;&gt;第16行：&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第16行：&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;如何求取乘法逆元呢？这里介绍两种简单的方法。&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;如何求取乘法逆元呢？这里介绍两种简单的方法。&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=== 费马小定理 ===&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=== 费马小定理 ===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;根据[[费马小定理]]，我们有 &amp;lt;math&amp;gt;a^p\equiv a(mod\ p),\  p&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;\ &lt;/del&gt;\ is\ &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;\ prime&lt;/del&gt;&amp;lt;/math&amp;gt; 。因此对于 &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; 是质数的情形，我们可以使用 &amp;lt;math&amp;gt;a^{p-2}&amp;lt;/math&amp;gt; 作为 &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; 在模 &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; 意义下的乘法逆元。&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;根据[[费马小定理]]，我们有 &amp;lt;math&amp;gt;a^p\equiv a(mod\ p),\  p \is\&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;mathbb{Prime}&lt;/ins&gt;&amp;lt;/math&amp;gt; 。因此对于 &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; 是质数的情形，我们可以使用 &amp;lt;math&amp;gt;a^{p-2}&amp;lt;/math&amp;gt; 作为 &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; 在模 &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; 意义下的乘法逆元。&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;这可以使用[[快速幂]]在时间复杂度 &amp;lt;math&amp;gt;O(\log p)&amp;lt;/math&amp;gt; 内计算出来。&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;这可以使用[[快速幂]]在时间复杂度 &amp;lt;math&amp;gt;O(\log p)&amp;lt;/math&amp;gt; 内计算出来。&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wumoe:diff::1.12:old-3893:rev-3896 --&gt;
&lt;/table&gt;</summary>
		<author><name>Luke li</name></author>
	</entry>
	<entry>
		<id>https://wiki.xhsr.org.cn/index.php?title=%E7%AE%97%E6%B3%95:_%E6%B1%82%E6%A8%A1%E6%84%8F%E4%B9%89%E4%B8%8B%E7%9A%84%E4%B9%98%E6%B3%95%E9%80%86%E5%85%83&amp;diff=3893&amp;oldid=prev</id>
		<title>Luke li：​模意义下的乘法逆元是一种重要的数论算法，可用于密码加密等领域。有主要的两种求乘法逆元的方法：费马小定理法，和扩展欧几里得法。</title>
		<link rel="alternate" type="text/html" href="https://wiki.xhsr.org.cn/index.php?title=%E7%AE%97%E6%B3%95:_%E6%B1%82%E6%A8%A1%E6%84%8F%E4%B9%89%E4%B8%8B%E7%9A%84%E4%B9%98%E6%B3%95%E9%80%86%E5%85%83&amp;diff=3893&amp;oldid=prev"/>
		<updated>2024-10-24T01:01:31Z</updated>

		<summary type="html">&lt;p&gt;模意义下的乘法逆元是一种重要的数论算法，可用于密码加密等领域。有主要的两种求乘法逆元的方法：费马小定理法，和扩展欧几里得法。&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==预备知识==&lt;br /&gt;
[[模运算]]&lt;br /&gt;
&lt;br /&gt;
==定义==&lt;br /&gt;
这里我们只讨论整数中的乘法逆元。&amp;lt;ref&amp;gt;OI-wiki中更详细的介绍&amp;lt;/ref&amp;gt;&lt;br /&gt;
具体而言，如果在模 &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; 意义下，&amp;lt;math&amp;gt;ax\equiv 1(mod\ p)&amp;lt;/math&amp;gt; ，我们就称 &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt; 是 &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; 的乘法逆元。记作 &amp;lt;math&amp;gt;a^{-1}=x&amp;lt;/math&amp;gt; 。&lt;br /&gt;
&lt;br /&gt;
如果 &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; 是质数，那么对于任意的 &amp;lt;math&amp;gt;a\ne 0&amp;lt;/math&amp;gt; 都一定存在乘法逆元；如果 &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; 不是质数，那么可能存在 &amp;lt;math&amp;gt;a\ne 0&amp;lt;/math&amp;gt; 不存在乘法逆元。&lt;br /&gt;
&lt;br /&gt;
== 用途 ==&lt;br /&gt;
在处理过程中需要取模的问题时，可能会遇到除法。我们都知道，把取模过的数字相除无法得出原来的结果（即 &amp;lt;math&amp;gt;\frac{a\mod c}{b\mod c}\ne \frac{a}{b}&amp;lt;/math&amp;gt; ），此时我们就需要用 &amp;lt;math&amp;gt;\times a^{-1}&amp;lt;/math&amp;gt; 代替 &amp;lt;math&amp;gt;\div a&amp;lt;/math&amp;gt; 。&lt;br /&gt;
&lt;br /&gt;
另外，乘法逆元在加密算法（如[[RSA]]），[[扩展中国剩余定理算法]]等算法中有着重要意义。&lt;br /&gt;
&lt;br /&gt;
== 做法 ==&lt;br /&gt;
如何求取乘法逆元呢？这里介绍两种简单的方法。&lt;br /&gt;
=== 费马小定理 ===&lt;br /&gt;
根据[[费马小定理]]，我们有 &amp;lt;math&amp;gt;a^p\equiv a(mod\ p),\  p\ \ is\ \ prime&amp;lt;/math&amp;gt; 。因此对于 &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; 是质数的情形，我们可以使用 &amp;lt;math&amp;gt;a^{p-2}&amp;lt;/math&amp;gt; 作为 &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; 在模 &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; 意义下的乘法逆元。&lt;br /&gt;
&lt;br /&gt;
这可以使用[[快速幂]]在时间复杂度 &amp;lt;math&amp;gt;O(\log p)&amp;lt;/math&amp;gt; 内计算出来。&lt;br /&gt;
=== 扩展欧几里得 ===&lt;br /&gt;
我们都知道欧几里得的[[辗转相除法]]，有人从这一算法中得到启发发明了[[扩展欧几里得]]算法。&lt;br /&gt;
&lt;br /&gt;
这一算法与[[裴蜀定理]]息息相关。裴蜀定理是说：&amp;lt;math&amp;gt;ax+by=c&amp;lt;/math&amp;gt; 有解，当且仅当 &amp;lt;math&amp;gt;\gcd(a,b)|c&amp;lt;/math&amp;gt; 。因此我们可以先解出 &amp;lt;math&amp;gt;ax+by=\gcd(a,b)&amp;lt;/math&amp;gt; 的解集，然后再把 &amp;lt;math&amp;gt;x,y&amp;lt;/math&amp;gt; 乘 &amp;lt;math&amp;gt;\frac{c}{\gcd(a,b)}&amp;lt;/math&amp;gt; ，即可得出最后的解。 &amp;lt;ref&amp;gt;详情见 oi-wiki贝祖定理&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
在辗转相除法中，我们知道 &amp;lt;math&amp;gt;\gcd(a,b)=\gcd(b,a\ mod\ b) , b\ne 0&amp;lt;/math&amp;gt; 。运用[[递归]]的思想，先求出 &amp;lt;math&amp;gt;bx&amp;#039;+(a\ mod\ b)y&amp;#039;=\gcd(b,a\ mod\ b)=\gcd(a,b)&amp;lt;/math&amp;gt; 的解，然后再转化为原方程的解即可。&lt;br /&gt;
&lt;br /&gt;
可以列出方程式 &amp;lt;math&amp;gt;ax+by=bx&amp;#039;+(a\ mod\ b)y&amp;#039;&amp;lt;/math&amp;gt; ，其中 &amp;lt;math&amp;gt;x,y&amp;lt;/math&amp;gt; 是未知数，其余的字母都是已知数。因为 &amp;lt;math&amp;gt;a\ mod\ b=a-a\lfloor \frac{a}{b} \rfloor&amp;lt;/math&amp;gt; ，移项得到 &amp;lt;math&amp;gt;ax+by=a(y&amp;#039;-\lfloor \frac{a}{b}\rfloor y&amp;#039;)+bx&amp;#039;&amp;lt;/math&amp;gt; ，所以 &amp;lt;math&amp;gt;x=y&amp;#039;-\lfloor \frac{a}{b}\rfloor y&amp;#039;,\  y=x&amp;#039;&amp;lt;/math&amp;gt; 是原方程一组可行的解。&lt;br /&gt;
&lt;br /&gt;
这样我们就得到了解方程 &amp;lt;math&amp;gt;ax+by=c&amp;lt;/math&amp;gt; 的方法。&lt;br /&gt;
&lt;br /&gt;
将“求模 &amp;lt;math&amp;gt;p&amp;lt;/math&amp;gt; 意义下 &amp;lt;math&amp;gt;a&amp;lt;/math&amp;gt; 的乘法逆元”这个问题转化一下，就可以改为求 &amp;lt;math&amp;gt;ax+py=1&amp;lt;/math&amp;gt; 这一问题中 &amp;lt;math&amp;gt;x&amp;lt;/math&amp;gt;的解。显然可以使用裴蜀定理判断是否有解，然后使用exgcd（扩展欧几里得）解决。&lt;/div&gt;</summary>
		<author><name>Luke li</name></author>
	</entry>
</feed>