Files
C-Plus-Plus/de/dab/ncr__modulo__p_8cpp.html
2025-07-09 10:53:16 +00:00

408 lines
31 KiB
HTML

<!-- HTML header for doxygen 1.12.0-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<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.14.0"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>TheAlgorithms/C++: math/ncr_modulo_p.cpp File Reference</title>
<link rel="icon" href="../../favicon.svg" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@xpack-3rd-party/doxygen-awesome-css@2.2.0-1/doxygen-awesome-darkmode-toggle.js"></script>
<script type="text/javascript">
DoxygenAwesomeDarkModeToggle.init()
</script>
<script type="text/javascript" src="../../clipboard.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<script type="text/javascript" src="../../cookie.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/javascript">
window.MathJax = {
options: {
ignoreHtmlClass: 'tex2jax_ignore',
processHtmlClass: 'tex2jax_process'
},
loader: {
load: ['[tex]/ams']
},
tex: {
macros: {},
packages: ['base','configmacros','ams']
}
};
</script>
<script type="text/javascript" id="MathJax-script" async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../doxygen-awesome.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectlogo"><img alt="Logo" src="../../project_logo.png"/></td>
<td id="projectalign">
<div id="projectname">TheAlgorithms/C++<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">All the algorithms implemented in C++</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.14.0 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
</script>
<script type="text/javascript">
$(function() { codefold.init(); });
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
initMenu('../../',true,false,'search.php','Search',true);
$(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(function(){initNavTree('de/dab/ncr__modulo__p_8cpp.html','../../',''); });
</script>
<div id="container">
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div class="header">
<div class="headertitle"><div class="title">ncr_modulo_p.cpp File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>This program aims at calculating <a href="https://cp-algorithms.com/combinatorics/binomial-coefficients.html" target="_blank">nCr modulo p</a>.
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;cassert&gt;</code><br />
<code>#include &lt;iostream&gt;</code><br />
<code>#include &lt;vector&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for ncr_modulo_p.cpp:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" loading="lazy" frameborder="0" src="../../d4/d25/ncr__modulo__p_8cpp__incl.svg" width="252" height="111"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</div>
</div>
<p><a href="../../de/dab/ncr__modulo__p_8cpp_source.html">Go to the source code of this file.</a></p>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 id="header-nested-classes" class="groupheader"><a id="nested-classes" name="nested-classes"></a>
Classes</h2></td></tr>
<tr class="memitem:math_3A_3Ancr_5Fmodulo_5Fp_3A_3ANCRModuloP" id="r_math_3A_3Ancr_5Fmodulo_5Fp_3A_3ANCRModuloP"><td class="memItemLeft" align="right" valign="top">class &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d6/dc1/classmath_1_1ncr__modulo__p_1_1_n_c_r_modulo_p.html">math::ncr_modulo_p::NCRModuloP</a></td></tr>
<tr class="memdesc:"><td class="mdescLeft">&#160;</td><td class="mdescRight">Class which contains all methods required for calculating nCr mod p. <a href="../../d6/dc1/classmath_1_1ncr__modulo__p_1_1_n_c_r_modulo_p.html#details">More...</a><br /></td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 id="header-namespaces" class="groupheader"><a id="namespaces" name="namespaces"></a>
Namespaces</h2></td></tr>
<tr class="memitem:math" id="r_math"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d47/namespacemath.html">math</a></td></tr>
<tr class="memdesc:dd/d47/namespacemath"><td class="mdescLeft">&#160;</td><td class="mdescRight">for assert <br /></td></tr>
<tr class="memitem:ncr_5Fmodulo_5Fp" id="r_ncr_5Fmodulo_5Fp"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/db9/namespacencr__modulo__p.html">ncr_modulo_p</a></td></tr>
<tr class="memdesc:d4/db9/namespacencr__modulo__p"><td class="mdescLeft">&#160;</td><td class="mdescRight">Functions for <a href="https://cp-algorithms.com/combinatorics/binomial-coefficients.html" target="_blank">nCr modulo p</a> implementation. <br /></td></tr>
<tr class="memitem:utils" id="r_utils"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d6/d84/namespaceutils.html">utils</a></td></tr>
<tr class="memdesc:d6/d84/namespaceutils"><td class="mdescLeft">&#160;</td><td class="mdescRight">this namespace contains the definitions of the functions called from the class <a class="el" href="../../d6/dc1/classmath_1_1ncr__modulo__p_1_1_n_c_r_modulo_p.html" title="Class which contains all methods required for calculating nCr mod p.">math::ncr_modulo_p::NCRModuloP</a> <br /></td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 id="header-func-members" class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a9010ad5669d31449c3bf3271ab5ebc86" id="r_a9010ad5669d31449c3bf3271ab5ebc86"><td class="memItemLeft" align="right" valign="top">int64_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a9010ad5669d31449c3bf3271ab5ebc86">math::ncr_modulo_p::utils::gcdExtended</a> (const int64_t &amp;a, const int64_t &amp;b, int64_t &amp;x, int64_t &amp;y)</td></tr>
<tr class="memdesc:a9010ad5669d31449c3bf3271ab5ebc86"><td class="mdescLeft">&#160;</td><td class="mdescRight">finds the values x and y such that a*x + b*y = gcd(a,b) <br /></td></tr>
<tr class="memitem:a6c7bfe3bdaa086d32261a5c5584d0fa9" id="r_a6c7bfe3bdaa086d32261a5c5584d0fa9"><td class="memItemLeft" align="right" valign="top">int64_t&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a6c7bfe3bdaa086d32261a5c5584d0fa9">math::ncr_modulo_p::utils::modInverse</a> (const int64_t &amp;a, const int64_t &amp;m)</td></tr>
<tr class="memitem:a483bb8ccf42aaf7375a83e91490eda1e" id="r_a483bb8ccf42aaf7375a83e91490eda1e"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#a483bb8ccf42aaf7375a83e91490eda1e">tests</a> ()</td></tr>
<tr class="memdesc:a483bb8ccf42aaf7375a83e91490eda1e"><td class="mdescLeft">&#160;</td><td class="mdescRight">tests <a class="el" href="../../d6/dc1/classmath_1_1ncr__modulo__p_1_1_n_c_r_modulo_p.html" title="Class which contains all methods required for calculating nCr mod p.">math::ncr_modulo_p::NCRModuloP</a> <br /></td></tr>
<tr class="memitem:afa2b50f4716fc3b42221a72e676e1422" id="r_afa2b50f4716fc3b42221a72e676e1422"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#afa2b50f4716fc3b42221a72e676e1422">example</a> ()</td></tr>
<tr class="memdesc:afa2b50f4716fc3b42221a72e676e1422"><td class="mdescLeft">&#160;</td><td class="mdescRight">example showing the usage of the <a class="el" href="../../d6/dc1/classmath_1_1ncr__modulo__p_1_1_n_c_r_modulo_p.html" title="Class which contains all methods required for calculating nCr mod p.">math::ncr_modulo_p::NCRModuloP</a> class <br /></td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4" id="r_ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
</table>
<a name="details" id="details"></a><h2 id="header-details" class="groupheader">Detailed Description</h2>
<div class="textblock"><p>This program aims at calculating <a href="https://cp-algorithms.com/combinatorics/binomial-coefficients.html" target="_blank">nCr modulo p</a>. </p>
<p>nCr is defined as n! / (r! * (n-r)!) where n! represents factorial of n. In many cases, the value of nCr is too large to fit in a 64 bit integer. Hence, in competitive programming, there are many problems or subproblems to compute nCr modulo p where p is a given number. </p><dl class="section author"><dt>Author</dt><dd><a href="https://github.com/KaustubhDamania" target="_blank">Kaustubh Damania</a> </dd></dl>
<p class="definition">Definition in file <a class="el" href="../../de/dab/ncr__modulo__p_8cpp_source.html">ncr_modulo_p.cpp</a>.</p>
</div><a name="doc-func-members" id="doc-func-members"></a><h2 id="header-doc-func-members" class="groupheader">Function Documentation</h2>
<a id="afa2b50f4716fc3b42221a72e676e1422" name="afa2b50f4716fc3b42221a72e676e1422"></a>
<h2 class="memtitle"><span class="permalink"><a href="#afa2b50f4716fc3b42221a72e676e1422">&#9670;&#160;</a></span>example()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void example </td>
<td>(</td>
<td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>example showing the usage of the <a class="el" href="../../d6/dc1/classmath_1_1ncr__modulo__p_1_1_n_c_r_modulo_p.html" title="Class which contains all methods required for calculating nCr mod p.">math::ncr_modulo_p::NCRModuloP</a> class </p>
<p class="definition">Definition at line <a class="el" href="../../de/dab/ncr__modulo__p_8cpp_source.html#l00175">175</a> of file <a class="el" href="../../de/dab/ncr__modulo__p_8cpp_source.html">ncr_modulo_p.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 175</span> {</div>
<div class="line"><span class="lineno"> 176</span> <span class="keyword">const</span> int64_t size = 1e6 + 1;</div>
<div class="line"><span class="lineno"> 177</span> <span class="keyword">const</span> int64_t p = 1e9 + 7;</div>
<div class="line"><span class="lineno"> 178</span> </div>
<div class="line"><span class="lineno"> 179</span> <span class="comment">// the ncrObj contains the precomputed values of factorials modulo p for</span></div>
<div class="line"><span class="lineno"> 180</span> <span class="comment">// values from 0 to size</span></div>
<div class="line"><span class="lineno"> 181</span> <span class="keyword">const</span> <span class="keyword">auto</span> ncrObj = <a class="code hl_class" href="../../d6/dc1/classmath_1_1ncr__modulo__p_1_1_n_c_r_modulo_p.html">math::ncr_modulo_p::NCRModuloP</a>(size, p);</div>
<div class="line"><span class="lineno"> 182</span> </div>
<div class="line"><span class="lineno"> 183</span> <span class="comment">// having the ncrObj we can efficiently query the values of (n C r)%p</span></div>
<div class="line"><span class="lineno"> 184</span> <span class="comment">// note that time of the computation does not depend on size</span></div>
<div class="line"><span class="lineno"> 185</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt;= 7; i++) {</div>
<div class="line"><span class="lineno"> 186</span> std::cout &lt;&lt; 6 &lt;&lt; <span class="stringliteral">&quot;C&quot;</span> &lt;&lt; i &lt;&lt; <span class="stringliteral">&quot; mod &quot;</span> &lt;&lt; p &lt;&lt; <span class="stringliteral">&quot; = &quot;</span> &lt;&lt; ncrObj.ncr(6, i)</div>
<div class="line"><span class="lineno"> 187</span> &lt;&lt; <span class="stringliteral">&quot;\n&quot;</span>;</div>
<div class="line"><span class="lineno"> 188</span> }</div>
<div class="line"><span class="lineno"> 189</span>}</div>
<div class="ttc" id="aclassmath_1_1ncr__modulo__p_1_1_n_c_r_modulo_p_html"><div class="ttname"><a href="../../d6/dc1/classmath_1_1ncr__modulo__p_1_1_n_c_r_modulo_p.html">math::ncr_modulo_p::NCRModuloP</a></div><div class="ttdoc">Class which contains all methods required for calculating nCr mod p.</div><div class="ttdef"><b>Definition</b> <a href="../../de/dab/ncr__modulo__p_8cpp_source.html#l00079">ncr_modulo_p.cpp:79</a></div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a9010ad5669d31449c3bf3271ab5ebc86" name="a9010ad5669d31449c3bf3271ab5ebc86"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a9010ad5669d31449c3bf3271ab5ebc86">&#9670;&#160;</a></span>gcdExtended()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int64_t math::ncr_modulo_p::utils::gcdExtended </td>
<td>(</td>
<td class="paramtype">const int64_t &amp;</td> <td class="paramname"><span class="paramname"><em>a</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const int64_t &amp;</td> <td class="paramname"><span class="paramname"><em>b</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int64_t &amp;</td> <td class="paramname"><span class="paramname"><em>x</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">int64_t &amp;</td> <td class="paramname"><span class="paramname"><em>y</em></span>&#160;)</td>
</tr>
</table>
</div><div class="memdoc">
<p>finds the values x and y such that a*x + b*y = gcd(a,b) </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">a</td><td>the first input of the gcd </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">a</td><td>the second input of the gcd </td></tr>
<tr><td class="paramdir">[out]</td><td class="paramname">x</td><td>the Bézout coefficient of a </td></tr>
<tr><td class="paramdir">[out]</td><td class="paramname">y</td><td>the Bézout coefficient of b </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>the gcd of a and b </dd></dl>
<p class="definition">Definition at line <a class="el" href="../../de/dab/ncr__modulo__p_8cpp_source.html#l00044">44</a> of file <a class="el" href="../../de/dab/ncr__modulo__p_8cpp_source.html">ncr_modulo_p.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 45</span> {</div>
<div class="line"><span class="lineno"> 46</span> <span class="keywordflow">if</span> (a == 0) {</div>
<div class="line"><span class="lineno"> 47</span> x = 0;</div>
<div class="line"><span class="lineno"> 48</span> y = 1;</div>
<div class="line"><span class="lineno"> 49</span> <span class="keywordflow">return</span> b;</div>
<div class="line"><span class="lineno"> 50</span> }</div>
<div class="line"><span class="lineno"> 51</span> </div>
<div class="line"><span class="lineno"> 52</span> int64_t x1 = 0, y1 = 0;</div>
<div class="line"><span class="lineno"> 53</span> <span class="keyword">const</span> int64_t <a class="code hl_function" href="../../d4/da0/gcd__iterative__euclidean_8cpp.html#ae48807fa2b7000afae599e67f327545e">gcd</a> = gcdExtended(b % a, a, x1, y1);</div>
<div class="line"><span class="lineno"> 54</span> </div>
<div class="line"><span class="lineno"> 55</span> x = y1 - (b / a) * x1;</div>
<div class="line"><span class="lineno"> 56</span> y = x1;</div>
<div class="line"><span class="lineno"> 57</span> <span class="keywordflow">return</span> <a class="code hl_function" href="../../d4/da0/gcd__iterative__euclidean_8cpp.html#ae48807fa2b7000afae599e67f327545e">gcd</a>;</div>
<div class="line"><span class="lineno"> 58</span>}</div>
<div class="ttc" id="agcd__iterative__euclidean_8cpp_html_ae48807fa2b7000afae599e67f327545e"><div class="ttname"><a href="../../d4/da0/gcd__iterative__euclidean_8cpp.html#ae48807fa2b7000afae599e67f327545e">gcd</a></div><div class="ttdeci">int gcd(int num1, int num2)</div><div class="ttdef"><b>Definition</b> <a href="../../d4/da0/gcd__iterative__euclidean_8cpp_source.html#l00015">gcd_iterative_euclidean.cpp:15</a></div></div>
</div><!-- fragment -->
</div>
</div>
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">&#9670;&#160;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">void</td> <td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p class="definition">Definition at line <a class="el" href="../../de/dab/ncr__modulo__p_8cpp_source.html#l00191">191</a> of file <a class="el" href="../../de/dab/ncr__modulo__p_8cpp_source.html">ncr_modulo_p.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 191</span> {</div>
<div class="line"><span class="lineno"> 192</span> <a class="code hl_function" href="#a483bb8ccf42aaf7375a83e91490eda1e">tests</a>();</div>
<div class="line"><span class="lineno"> 193</span> <a class="code hl_function" href="#afa2b50f4716fc3b42221a72e676e1422">example</a>();</div>
<div class="line"><span class="lineno"> 194</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><span class="lineno"> 195</span>}</div>
<div class="ttc" id="ancr__modulo__p_8cpp_html_a483bb8ccf42aaf7375a83e91490eda1e"><div class="ttname"><a href="#a483bb8ccf42aaf7375a83e91490eda1e">tests</a></div><div class="ttdeci">static void tests()</div><div class="ttdoc">tests math::ncr_modulo_p::NCRModuloP</div><div class="ttdef"><b>Definition</b> <a href="../../de/dab/ncr__modulo__p_8cpp_source.html#l00142">ncr_modulo_p.cpp:142</a></div></div>
<div class="ttc" id="ancr__modulo__p_8cpp_html_afa2b50f4716fc3b42221a72e676e1422"><div class="ttname"><a href="#afa2b50f4716fc3b42221a72e676e1422">example</a></div><div class="ttdeci">void example()</div><div class="ttdoc">example showing the usage of the math::ncr_modulo_p::NCRModuloP class</div><div class="ttdef"><b>Definition</b> <a href="../../de/dab/ncr__modulo__p_8cpp_source.html#l00175">ncr_modulo_p.cpp:175</a></div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a6c7bfe3bdaa086d32261a5c5584d0fa9" name="a6c7bfe3bdaa086d32261a5c5584d0fa9"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a6c7bfe3bdaa086d32261a5c5584d0fa9">&#9670;&#160;</a></span>modInverse()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int64_t math::ncr_modulo_p::utils::modInverse </td>
<td>(</td>
<td class="paramtype">const int64_t &amp;</td> <td class="paramname"><span class="paramname"><em>a</em></span>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const int64_t &amp;</td> <td class="paramname"><span class="paramname"><em>m</em></span>&#160;)</td>
</tr>
</table>
</div><div class="memdoc">
<p>Find modular inverse of a modulo m i.e. a number x such that (a*x)m = 1</p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">a</td><td>the number for which the modular inverse is queried </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">m</td><td>the modulus </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>the inverce of a modulo m, if it exists, -1 otherwise </dd></dl>
<p class="definition">Definition at line <a class="el" href="../../de/dab/ncr__modulo__p_8cpp_source.html#l00066">66</a> of file <a class="el" href="../../de/dab/ncr__modulo__p_8cpp_source.html">ncr_modulo_p.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 66</span> {</div>
<div class="line"><span class="lineno"> 67</span> int64_t x = 0, y = 0;</div>
<div class="line"><span class="lineno"> 68</span> <span class="keyword">const</span> int64_t g = <a class="code hl_function" href="#a9010ad5669d31449c3bf3271ab5ebc86">gcdExtended</a>(a, m, x, y);</div>
<div class="line"><span class="lineno"> 69</span> <span class="keywordflow">if</span> (g != 1) { <span class="comment">// modular inverse doesn&#39;t exist</span></div>
<div class="line"><span class="lineno"> 70</span> <span class="keywordflow">return</span> -1;</div>
<div class="line"><span class="lineno"> 71</span> } <span class="keywordflow">else</span> {</div>
<div class="line"><span class="lineno"> 72</span> <span class="keywordflow">return</span> ((x + m) % m);</div>
<div class="line"><span class="lineno"> 73</span> }</div>
<div class="line"><span class="lineno"> 74</span>}</div>
<div class="ttc" id="ancr__modulo__p_8cpp_html_a9010ad5669d31449c3bf3271ab5ebc86"><div class="ttname"><a href="#a9010ad5669d31449c3bf3271ab5ebc86">math::ncr_modulo_p::utils::gcdExtended</a></div><div class="ttdeci">int64_t gcdExtended(const int64_t &amp;a, const int64_t &amp;b, int64_t &amp;x, int64_t &amp;y)</div><div class="ttdoc">finds the values x and y such that a*x + b*y = gcd(a,b)</div><div class="ttdef"><b>Definition</b> <a href="../../de/dab/ncr__modulo__p_8cpp_source.html#l00044">ncr_modulo_p.cpp:44</a></div></div>
</div><!-- fragment -->
</div>
</div>
<a id="a483bb8ccf42aaf7375a83e91490eda1e" name="a483bb8ccf42aaf7375a83e91490eda1e"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a483bb8ccf42aaf7375a83e91490eda1e">&#9670;&#160;</a></span>tests()</h2>
<div class="memitem">
<div class="memproto">
<table class="mlabels">
<tr>
<td class="mlabels-left">
<table class="memname">
<tr>
<td class="memname">void tests </td>
<td>(</td>
<td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
<td></td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel static">static</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>tests <a class="el" href="../../d6/dc1/classmath_1_1ncr__modulo__p_1_1_n_c_r_modulo_p.html" title="Class which contains all methods required for calculating nCr mod p.">math::ncr_modulo_p::NCRModuloP</a> </p>
<p class="definition">Definition at line <a class="el" href="../../de/dab/ncr__modulo__p_8cpp_source.html#l00142">142</a> of file <a class="el" href="../../de/dab/ncr__modulo__p_8cpp_source.html">ncr_modulo_p.cpp</a>.</p>
<div class="fragment"><div class="line"><span class="lineno"> 142</span> {</div>
<div class="line"><span class="lineno"> 143</span> <span class="keyword">struct </span><a class="code hl_struct" href="../../da/d86/struct_test_case.html">TestCase</a> {</div>
<div class="line"><span class="lineno"> 144</span> <span class="keyword">const</span> int64_t size;</div>
<div class="line"><span class="lineno"> 145</span> <span class="keyword">const</span> int64_t p;</div>
<div class="line"><span class="lineno"> 146</span> <span class="keyword">const</span> int64_t n;</div>
<div class="line"><span class="lineno"> 147</span> <span class="keyword">const</span> int64_t r;</div>
<div class="line"><span class="lineno"> 148</span> <span class="keyword">const</span> int64_t expected;</div>
<div class="line"><span class="lineno"> 149</span> </div>
<div class="line"><span class="lineno"> 150</span> TestCase(<span class="keyword">const</span> int64_t size, <span class="keyword">const</span> int64_t p, <span class="keyword">const</span> int64_t n,</div>
<div class="line"><span class="lineno"> 151</span> <span class="keyword">const</span> int64_t r, <span class="keyword">const</span> int64_t expected)</div>
<div class="line"><span class="lineno"> 152</span> : size(size), p(p), n(n), r(r), expected(expected) {}</div>
<div class="line"><span class="lineno"> 153</span> };</div>
<div class="line"><span class="lineno"> 154</span> <span class="keyword">const</span> std::vector&lt;TestCase&gt; test_cases = {</div>
<div class="line"><span class="lineno"> 155</span> <a class="code hl_struct" href="../../da/d86/struct_test_case.html">TestCase</a>(60000, 1000000007, 52323, 26161, 224944353),</div>
<div class="line"><span class="lineno"> 156</span> <a class="code hl_struct" href="../../da/d86/struct_test_case.html">TestCase</a>(20, 5, 6, 2, 30 % 5),</div>
<div class="line"><span class="lineno"> 157</span> <a class="code hl_struct" href="../../da/d86/struct_test_case.html">TestCase</a>(100, 29, 7, 3, 35 % 29),</div>
<div class="line"><span class="lineno"> 158</span> <a class="code hl_struct" href="../../da/d86/struct_test_case.html">TestCase</a>(1000, 13, 10, 3, 120 % 13),</div>
<div class="line"><span class="lineno"> 159</span> <a class="code hl_struct" href="../../da/d86/struct_test_case.html">TestCase</a>(20, 17, 1, 10, 0),</div>
<div class="line"><span class="lineno"> 160</span> <a class="code hl_struct" href="../../da/d86/struct_test_case.html">TestCase</a>(45, 19, 23, 1, 23 % 19),</div>
<div class="line"><span class="lineno"> 161</span> <a class="code hl_struct" href="../../da/d86/struct_test_case.html">TestCase</a>(45, 19, 23, 0, 1),</div>
<div class="line"><span class="lineno"> 162</span> <a class="code hl_struct" href="../../da/d86/struct_test_case.html">TestCase</a>(45, 19, 23, 23, 1),</div>
<div class="line"><span class="lineno"> 163</span> <a class="code hl_struct" href="../../da/d86/struct_test_case.html">TestCase</a>(20, 9, 10, 2, -1)};</div>
<div class="line"><span class="lineno"> 164</span> <span class="keywordflow">for</span> (<span class="keyword">const</span> <span class="keyword">auto</span>&amp; tc : test_cases) {</div>
<div class="line"><span class="lineno"> 165</span> assert(<a class="code hl_class" href="../../d6/dc1/classmath_1_1ncr__modulo__p_1_1_n_c_r_modulo_p.html">math::ncr_modulo_p::NCRModuloP</a>(tc.size, tc.p).<a class="code hl_function" href="../../d6/dc1/classmath_1_1ncr__modulo__p_1_1_n_c_r_modulo_p.html#a499df7a01bcd348345dc5a0ab79bb0e9">ncr</a>(tc.n, tc.r) ==</div>
<div class="line"><span class="lineno"> 166</span> tc.expected);</div>
<div class="line"><span class="lineno"> 167</span> }</div>
<div class="line"><span class="lineno"> 168</span> </div>
<div class="line"><span class="lineno"> 169</span> std::cout &lt;&lt; <span class="stringliteral">&quot;\n\nAll tests have successfully passed!\n&quot;</span>;</div>
<div class="line"><span class="lineno"> 170</span>}</div>
<div class="ttc" id="aclassmath_1_1ncr__modulo__p_1_1_n_c_r_modulo_p_html_a499df7a01bcd348345dc5a0ab79bb0e9"><div class="ttname"><a href="../../d6/dc1/classmath_1_1ncr__modulo__p_1_1_n_c_r_modulo_p.html#a499df7a01bcd348345dc5a0ab79bb0e9">math::ncr_modulo_p::NCRModuloP::ncr</a></div><div class="ttdeci">int64_t ncr(const int64_t &amp;n, const int64_t &amp;r) const</div><div class="ttdoc">computes nCr % p</div><div class="ttdef"><b>Definition</b> <a href="../../de/dab/ncr__modulo__p_8cpp_source.html#l00116">ncr_modulo_p.cpp:116</a></div></div>
<div class="ttc" id="astruct_test_case_html"><div class="ttname"><a href="../../da/d86/struct_test_case.html">TestCase</a></div><div class="ttdoc">represents single example inputs and expected output of the function longest_common_string_length</div><div class="ttdef"><b>Definition</b> <a href="../../da/d0d/longest__common__string_8cpp_source.html#l00054">longest_common_string.cpp:54</a></div></div>
</div><!-- fragment -->
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<div id="page-nav" class="page-nav-panel">
<div id="page-nav-resize-handle"></div>
<div id="page-nav-tree">
<div id="page-nav-contents">
</div><!-- page-nav-contents -->
</div><!-- page-nav-tree -->
</div><!-- page-nav -->
</div><!-- container -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a href="../../dir_296d53ceaeaa7e099814a6def439fe8a.html">math</a></li><li class="navelem"><a href="../../de/dab/ncr__modulo__p_8cpp.html">ncr_modulo_p.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.14.0 </li>
</ul>
</div>
</body>
</html>