mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-05-12 03:28:29 +08:00
Documentation for c26eea874d
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
|
||||
<meta name="generator" content="Doxygen 1.12.0"/>
|
||||
<meta name="generator" content="Doxygen 1.13.2"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<title>TheAlgorithms/C++: math/modular_inverse_fermat_little_theorem.cpp File Reference</title>
|
||||
<link rel="icon" href="../../favicon.svg" type="image/x-icon" />
|
||||
@@ -60,7 +60,7 @@ window.MathJax = {
|
||||
</table>
|
||||
</div>
|
||||
<!-- end header part -->
|
||||
<!-- Generated by Doxygen 1.12.0 -->
|
||||
<!-- Generated by Doxygen 1.13.2 -->
|
||||
<script type="text/javascript">
|
||||
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
||||
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
|
||||
@@ -174,23 +174,20 @@ Functions</h2></td></tr>
|
||||
\[ϕ(m) = m-1\]
|
||||
</p>
|
||||
<p> where \(m\) is a prime number. </p><p class="formulaDsp">
|
||||
\begin{eqnarray*}
|
||||
a \cdot x &≡& 1 \;\text{mod}\; m\\
|
||||
\begin{eqnarray*} a \cdot x &≡& 1 \;\text{mod}\; m\\
|
||||
x &≡& a^{-1} \;\text{mod}\; m
|
||||
\end{eqnarray*}
|
||||
\end{eqnarray*}
|
||||
</p>
|
||||
<p> Using Euler's theorem we can modify the equation. </p><p class="formulaDsp">
|
||||
\[
|
||||
a^{ϕ(m)} ≡ 1 \;\text{mod}\; m
|
||||
\]
|
||||
\[ a^{ϕ(m)} ≡ 1 \;\text{mod}\; m
|
||||
\]
|
||||
</p>
|
||||
<p> (Where '^' denotes the exponent operator)</p>
|
||||
<p>Here 'ϕ' is Euler's Totient Function. For modular inverse existence 'a' and 'm' must be relatively primes numbers. To apply Fermat's Little Theorem is necessary that 'm' must be a prime number. Generally in many competitive programming competitions 'm' is either 1000000007 (1e9+7) or 998244353.</p>
|
||||
<p>We considered m as large prime (1e9+7). \(a^{ϕ(m)} ≡ 1 \;\text{mod}\; m\) (Using Euler's Theorem) \(ϕ(m) = m-1\) using Fermat's Little Theorem. \(a^{m-1} ≡ 1 \;\text{mod}\; m\) Now multiplying both side by \(a^{-1}\). </p><p class="formulaDsp">
|
||||
\begin{eqnarray*}
|
||||
a^{m-1} \cdot a^{-1} &≡& a^{-1} \;\text{mod}\; m\\
|
||||
\begin{eqnarray*} a^{m-1} \cdot a^{-1} &≡& a^{-1} \;\text{mod}\; m\\
|
||||
a^{m-2} &≡& a^{-1} \;\text{mod}\; m
|
||||
\end{eqnarray*}
|
||||
\end{eqnarray*}
|
||||
</p>
|
||||
<p>We will find the exponent using binary exponentiation such that the algorithm works in \(O(\log n)\) time.</p>
|
||||
<p>Examples: -</p><ul>
|
||||
@@ -358,14 +355,13 @@ false if the number is not prime </dd></dl>
|
||||
<div class="line"><span class="lineno"> 106</span> }</div>
|
||||
<div class="line"><span class="lineno"> 107</span> </div>
|
||||
<div class="line"><span class="lineno"> 108</span> <span class="comment">// Check for invalid cases</span></div>
|
||||
<div class="line"><span class="lineno"> 109</span> <span class="keywordflow">if</span> (!<a class="code hl_function" href="#aba4929409fee35c3cb559e962a544b3e">isPrime</a>(m) || a == 0) {</div>
|
||||
<div class="line"><span class="lineno"> 109</span> <span class="keywordflow">if</span> (!isPrime(m) || a == 0) {</div>
|
||||
<div class="line"><span class="lineno"> 110</span> <span class="keywordflow">return</span> -1; <span class="comment">// Invalid input</span></div>
|
||||
<div class="line"><span class="lineno"> 111</span> }</div>
|
||||
<div class="line"><span class="lineno"> 112</span> </div>
|
||||
<div class="line"><span class="lineno"> 113</span> <span class="keywordflow">return</span> <a class="code hl_function" href="../../de/dcf/binary__exponent_8cpp.html#aeb48dce0725e63d19147944f41843c73">binExpo</a>(a, m - 2, m); <span class="comment">// Fermat's Little Theorem</span></div>
|
||||
<div class="line"><span class="lineno"> 114</span>}</div>
|
||||
<div class="ttc" id="abinary__exponent_8cpp_html_aeb48dce0725e63d19147944f41843c73"><div class="ttname"><a href="../../de/dcf/binary__exponent_8cpp.html#aeb48dce0725e63d19147944f41843c73">binExpo</a></div><div class="ttdeci">int binExpo(int a, int b)</div><div class="ttdef"><b>Definition</b> <a href="../../de/dcf/binary__exponent_8cpp_source.html#l00028">binary_exponent.cpp:28</a></div></div>
|
||||
<div class="ttc" id="amodular__inverse__fermat__little__theorem_8cpp_html_aba4929409fee35c3cb559e962a544b3e"><div class="ttname"><a href="#aba4929409fee35c3cb559e962a544b3e">math::modular_inverse_fermat::isPrime</a></div><div class="ttdeci">bool isPrime(std::int64_t m)</div><div class="ttdoc">Check if an integer is a prime number in time.</div><div class="ttdef"><b>Definition</b> <a href="../../d8/d53/modular__inverse__fermat__little__theorem_8cpp_source.html#l00086">modular_inverse_fermat_little_theorem.cpp:86</a></div></div>
|
||||
</div><!-- fragment -->
|
||||
</div>
|
||||
</div>
|
||||
@@ -387,7 +383,7 @@ false if the number is not prime </dd></dl>
|
||||
</table>
|
||||
</td>
|
||||
<td class="mlabels-right">
|
||||
<span class="mlabels"><span class="mlabel">static</span></span> </td>
|
||||
<span class="mlabels"><span class="mlabel static">static</span></span> </td>
|
||||
</tr>
|
||||
</table>
|
||||
</div><div class="memdoc">
|
||||
@@ -397,15 +393,16 @@ false if the number is not prime </dd></dl>
|
||||
|
||||
<p class="definition">Definition at line <a class="el" href="../../d8/d53/modular__inverse__fermat__little__theorem_8cpp_source.html#l00122">122</a> of file <a class="el" href="../../d8/d53/modular__inverse__fermat__little__theorem_8cpp_source.html">modular_inverse_fermat_little_theorem.cpp</a>.</p>
|
||||
<div class="fragment"><div class="line"><span class="lineno"> 122</span> {</div>
|
||||
<div class="line"><span class="lineno"> 123</span> assert(math::modular_inverse_fermat::modular_inverse(0, 97) == -1);</div>
|
||||
<div class="line"><span class="lineno"> 124</span> assert(math::modular_inverse_fermat::modular_inverse(15, -2) == -1);</div>
|
||||
<div class="line"><span class="lineno"> 125</span> assert(math::modular_inverse_fermat::modular_inverse(3, 10) == -1);</div>
|
||||
<div class="line"><span class="lineno"> 126</span> assert(math::modular_inverse_fermat::modular_inverse(3, 7) == 5);</div>
|
||||
<div class="line"><span class="lineno"> 127</span> assert(math::modular_inverse_fermat::modular_inverse(1, 101) == 1);</div>
|
||||
<div class="line"><span class="lineno"> 128</span> assert(math::modular_inverse_fermat::modular_inverse(-1337, 285179) == 165519);</div>
|
||||
<div class="line"><span class="lineno"> 129</span> assert(math::modular_inverse_fermat::modular_inverse(123456789, 998244353) == 25170271);</div>
|
||||
<div class="line"><span class="lineno"> 130</span> assert(math::modular_inverse_fermat::modular_inverse(-9876543210, 1000000007) == 784794281);</div>
|
||||
<div class="line"><span class="lineno"> 123</span> assert(<a class="code hl_function" href="#ae7e807e02c65c6fffd6162b4c66290c2">math::modular_inverse_fermat::modular_inverse</a>(0, 97) == -1);</div>
|
||||
<div class="line"><span class="lineno"> 124</span> assert(<a class="code hl_function" href="#ae7e807e02c65c6fffd6162b4c66290c2">math::modular_inverse_fermat::modular_inverse</a>(15, -2) == -1);</div>
|
||||
<div class="line"><span class="lineno"> 125</span> assert(<a class="code hl_function" href="#ae7e807e02c65c6fffd6162b4c66290c2">math::modular_inverse_fermat::modular_inverse</a>(3, 10) == -1);</div>
|
||||
<div class="line"><span class="lineno"> 126</span> assert(<a class="code hl_function" href="#ae7e807e02c65c6fffd6162b4c66290c2">math::modular_inverse_fermat::modular_inverse</a>(3, 7) == 5);</div>
|
||||
<div class="line"><span class="lineno"> 127</span> assert(<a class="code hl_function" href="#ae7e807e02c65c6fffd6162b4c66290c2">math::modular_inverse_fermat::modular_inverse</a>(1, 101) == 1);</div>
|
||||
<div class="line"><span class="lineno"> 128</span> assert(<a class="code hl_function" href="#ae7e807e02c65c6fffd6162b4c66290c2">math::modular_inverse_fermat::modular_inverse</a>(-1337, 285179) == 165519);</div>
|
||||
<div class="line"><span class="lineno"> 129</span> assert(<a class="code hl_function" href="#ae7e807e02c65c6fffd6162b4c66290c2">math::modular_inverse_fermat::modular_inverse</a>(123456789, 998244353) == 25170271);</div>
|
||||
<div class="line"><span class="lineno"> 130</span> assert(<a class="code hl_function" href="#ae7e807e02c65c6fffd6162b4c66290c2">math::modular_inverse_fermat::modular_inverse</a>(-9876543210, 1000000007) == 784794281);</div>
|
||||
<div class="line"><span class="lineno"> 131</span>}</div>
|
||||
<div class="ttc" id="amodular__inverse__fermat__little__theorem_8cpp_html_ae7e807e02c65c6fffd6162b4c66290c2"><div class="ttname"><a href="#ae7e807e02c65c6fffd6162b4c66290c2">math::modular_inverse_fermat::modular_inverse</a></div><div class="ttdeci">std::int64_t modular_inverse(std::int64_t a, std::int64_t m)</div><div class="ttdoc">calculates the modular inverse.</div><div class="ttdef"><b>Definition</b> <a href="../../d8/d53/modular__inverse__fermat__little__theorem_8cpp_source.html#l00103">modular_inverse_fermat_little_theorem.cpp:103</a></div></div>
|
||||
</div><!-- fragment -->
|
||||
</div>
|
||||
</div>
|
||||
@@ -415,7 +412,7 @@ false if the number is not prime </dd></dl>
|
||||
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
||||
<ul>
|
||||
<li class="navelem"><a class="el" href="../../dir_296d53ceaeaa7e099814a6def439fe8a.html">math</a></li><li class="navelem"><a class="el" href="../../d8/d53/modular__inverse__fermat__little__theorem_8cpp.html">modular_inverse_fermat_little_theorem.cpp</a></li>
|
||||
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.12.0 </li>
|
||||
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.13.2 </li>
|
||||
</ul>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user