{"id":1529,"date":"2014-12-24T10:09:47","date_gmt":"2014-12-24T10:09:47","guid":{"rendered":"http:\/\/theoryofprogramming.wordpress.com\/?p=10"},"modified":"2023-12-12T02:53:09","modified_gmt":"2023-12-12T02:53:09","slug":"modular-arithmetic-properties","status":"publish","type":"post","link":"https:\/\/theoryofcoding.com\/index.php\/2014\/12\/24\/modular-arithmetic-properties\/","title":{"rendered":"Modular Arithmetic Properties"},"content":{"rendered":"\n<p>In competitive programming, Modular Arithmetic Properties are essential tools in solving big number problems. In the problem statement, whenever they say, &#8220;print the answer&nbsp;<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-ea90e38558f150ff7c70f4edde83404e_l3.png?resize=104%2C20&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#40;&#49;&#48;&#94;&#123;&#57;&#125;&#32;&#43;&#32;&#55;&#41;&#32;\" title=\"Rendered by QuickLaTeX.com\" height=\"20\" width=\"104\" style=\"vertical-align: -5px;\" data-recalc-dims=\"1\"\/>&nbsp;&#8220;, it&#8217;s not that simple. You may have worked a lot to get the logic, but the output must be given as they say. You may get your answer stored in a single variable and just print the value of \u2192&nbsp;<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-7753691056a29636505a75009031abf9_l3.png?resize=177%2C20&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#32;&#118;&#97;&#114;&#105;&#97;&#98;&#108;&#101;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#40;&#49;&#48;&#94;&#123;&#57;&#125;&#32;&#43;&#32;&#55;&#41;&#32;\" title=\"Rendered by QuickLaTeX.com\" height=\"20\" width=\"177\" style=\"vertical-align: -5px;\" data-recalc-dims=\"1\"\/>&nbsp;. But what if you can&#8217;t get it into a single variable, what if it gets out of the range of long long int or so&#8230;? It is then that you have to use modular arithmetic&#8230;!<\/p>\n\n\n\n<p>Let&#8217;s go systematically, by stating the principles one-by-one. There are two fundamental formula for modular arithmetic, and the third one ins&#8217;t exactly fundamental as it needs a lot of derivation &#8211;<\/p>\n\n\n\n<p>1.&nbsp; &nbsp;<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-a1c7ee2a0e6c016a39715e288690eaa6_l3.png?resize=109%2C19&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#32;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#97;&#43;&#98;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;\" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"109\" style=\"vertical-align: -5px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p class=\"has-text-align-left\">If we were to print the modulo &#8216;PRIME&#8217; of the sum of two big numbers stored in variables &#8216;a&#8217; and &#8216;b&#8217;, we apply the following formula &#8211;<\/p>\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-8f4724b612b5fffa58d13667450e0433_l3.png?resize=495%2C19&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\" &#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#97;&#43;&#98;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#61;&#32;&#92;&#108;&#101;&#102;&#116;&#40;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#97;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#43;&#32;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#98;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80; &#92;&#113;&#113;&#117;&#97;&#100;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#97;&#114;&#114;&#111;&#119;&#32;&#101;&#113;&#46;&#32;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#49;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41; \" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"495\" style=\"vertical-align: -5px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>2.&nbsp; <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-06be97f6e58ebc052f5b52a13462844d_l3.png?resize=109%2C19&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#32;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#97;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#98;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;\" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"109\" style=\"vertical-align: -5px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>Now, if we were to print the modulo &#8216;PRIME&#8217; of the product of two big numbers stored in variables &#8216;a&#8217; and &#8216;b&#8217;, we apply the following formula &#8211;<\/p>\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-3ad6dd15dfb13205acbc17cabd388ccf_l3.png?resize=495%2C19&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\" &#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#97;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#98;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#61;&#32;&#92;&#108;&#101;&#102;&#116;&#40;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#97;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#98;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80; &#92;&#113;&#113;&#117;&#97;&#100;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#97;&#114;&#114;&#111;&#119;&#32;&#101;&#113;&#46;&#32;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#50;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41; \" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"495\" style=\"vertical-align: -5px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>As we have just gained a little knowledge on the fundamentals of modular arithmetic, it is good time for a quick practice! Let&#8217;s try to solve a very straight-forward problem. We will take two inputs from the user, a variable &#8216;x&#8217; and a variable &#8216;y&#8217;, and we are supposed to print the value of <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-1504dbd27432c8bdcc17c21f4727061c_l3.png?resize=177%2C20&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#32;&#40;&#120;&#33;&#32;&#43;&#32;&#121;&#33;&#41;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#40;&#49;&#48;&#94;&#123;&#57;&#125;&#32;&#43;&#32;&#55;&#41;&#32;\" title=\"Rendered by QuickLaTeX.com\" height=\"20\" width=\"177\" style=\"vertical-align: -5px;\" data-recalc-dims=\"1\"\/>.&nbsp;It&#8217;s a super simple problem, you can work out on the paper and solve. Don&#8217;t worry if it is taking time, remember, things worth having don&#8217;t come easy! Knowledge is one such thing. Once you have coded, check your output by putting a few test cases and verifying it on a calculator. If you are stuck, you can refer to my code below!<\/p>\n\n\n\n<p>3.&nbsp; &nbsp;<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-d0ab2ea12157fed24fb5e98cfc132feb_l3.png?resize=104%2C54&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#32;&#32;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#92;&#99;&#102;&#114;&#97;&#99;&#123;&#97;&#125;&#123;&#98;&#125;&#92;&#114;&#105;&#103;&#104;&#116;&#41;&#32;&#92;&#109;&#111;&#100;&#32;&#80;&#32;\" title=\"Rendered by QuickLaTeX.com\" height=\"54\" width=\"104\" style=\"vertical-align: -23px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>This equation is the bid bad guy! Now, if we were to print the modulo &#8216;PRIME&#8217; of the quotient of two big numbers stored in variables &#8216;a&#8217; and &#8216;b&#8217;, we don&#8217;t have a specific formula. This is what troubles a lot of programmers. It took me a month to learn this. I had to query in a many web sites, but I will explain it all here! So in order to calculate this, we need to learn two things the <strong>Modular Inverse<\/strong>, <strong>Fermat&#8217;s Little Theorem<\/strong> and <strong>Binary Exponentiation Technique<\/strong>.<\/p>\n\n\n\n<p><strong>Modular <\/strong><b>Inverse <\/b>&#8211; Modular Inverse of an integer &#8216;a&#8217; modulo &#8216;m&#8217; is an integer &#8216;x&#8217; such that,<\/p>\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-f7e9268043b4eda7880899b8c9b14825_l3.png?resize=361%2C20&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\" &#97;&#32;&#120;&#32;&#92;&#101;&#113;&#117;&#105;&#118;&#32;&#49;&#32;&#92;&#109;&#111;&#100;&#32;&#80;&#32;&#92;&#113;&#113;&#117;&#97;&#100;&#32;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#111;&#114;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#113;&#113;&#117;&#97;&#100;&#32;&#97;&#32;&#97;&#94;&#123;&#45;&#49;&#125;&#32;&#92;&#101;&#113;&#117;&#105;&#118;&#32;&#49;&#32;&#92;&#109;&#111;&#100;&#32;&#80; \" title=\"Rendered by QuickLaTeX.com\" height=\"20\" width=\"361\" style=\"vertical-align: -5px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>Every non-zero integer &#8216;a&#8217; has an inverse (modulo p) for a prime &#8216;p&#8217; and &#8216;a&#8217; not a multiple of &#8216;p&#8217;. Example,<\/p>\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-738f8ea962889ae10bbab2c36fb5ab71_l3.png?resize=113%2C81&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\" &#49;&#94;&#123;&#45;&#49;&#125;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#53;&#32;&#61;&#32;&#49;  &#50;&#94;&#123;&#45;&#49;&#125;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#53;&#32;&#61;&#32;&#51;  &#51;&#94;&#123;&#45;&#49;&#125;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#53;&#32;&#61;&#32;&#50;  &#52;&#94;&#123;&#45;&#49;&#125;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#53;&#32;&#61;&#32;&#52; \" title=\"Rendered by QuickLaTeX.com\" height=\"81\" width=\"113\" style=\"vertical-align: 0px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p><strong>Fermat&#8217;s Little Theorem <\/strong> &#8211; Fermat&#8217;s Little Theorem states that if &#8216;p&#8217; is a prime number, then for any integer a, the number <strong>a<sup>p<\/sup> &#8211; a<\/strong> is an integer multiple of &#8216;p&#8217;. In the notation of modular arithmetic, this is expressed as, <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-a2475ede79cdf36a4bf49ea503600bbd_l3.png?resize=151%2C15&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#32;&#97;&#94;&#123;&#80;&#125;&#32;&#92;&#101;&#113;&#117;&#105;&#118;&#32;&#97;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#38;&#115;&#61;&#49;&#32;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"151\" style=\"vertical-align: 0px;\" data-recalc-dims=\"1\"\/> &nbsp;or, simply,&nbsp; <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-bee8381f84b2c8c555f36bfe409921a7_l3.png?resize=169%2C15&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#32;&#97;&#94;&#123;&#80;&#125;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#61;&#32;&#97;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;\" title=\"Rendered by QuickLaTeX.com\" height=\"15\" width=\"169\" style=\"vertical-align: 0px;\" data-recalc-dims=\"1\"\/>. Now, in this formula if we multiply on both sides by <strong>a<sup>-2<\/sup><\/strong>, we get,<\/p>\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-5bbfac9be7756bbf94772add354e88e6_l3.png?resize=517%2C20&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\" &#97;&#94;&#123;&#80;&#32;&#45;&#32;&#50;&#125;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#61;&#32;&#97;&#94;&#123;&#45;&#49;&#125;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#92;&#113;&#117;&#97;&#100;&#32;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#111;&#114;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#113;&#117;&#97;&#100;&#32;&#97;&#94;&#123;&#45;&#49;&#125;&#32;&#61;&#32;&#97;&#94;&#123;&#80;&#32;&#45;&#32;&#50;&#125;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#92;&#113;&#117;&#97;&#100;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#97;&#114;&#114;&#111;&#119;&#32;&#101;&#113;&#46;&#32;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#51;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41; \" title=\"Rendered by QuickLaTeX.com\" height=\"20\" width=\"517\" style=\"vertical-align: -5px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>Now, we will combine all the equations to get our end formula. Now, <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-e7e0b3edd960f451073192fbe9f2abf5_l3.png?resize=104%2C54&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#32;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#92;&#99;&#102;&#114;&#97;&#99;&#123;&#97;&#125;&#123;&#98;&#125;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#109;&#111;&#100;&#32;&#80;&#32;\" title=\"Rendered by QuickLaTeX.com\" height=\"54\" width=\"104\" style=\"vertical-align: -23px;\" data-recalc-dims=\"1\"\/> can be written as,<\/p>\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-511cabe1de82aad5cbaaa73dcde773c2_l3.png?resize=129%2C22&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\" &#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#97;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#98;&#94;&#123;&#45;&#49;&#125;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80; \" title=\"Rendered by QuickLaTeX.com\" height=\"22\" width=\"129\" style=\"vertical-align: -7px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>Applying eq. (2) on this, we get,<\/p>\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-528c58ae49534fa5b42af179abd2d174_l3.png?resize=429%2C22&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\" &#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#97;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#98;&#94;&#123;&#45;&#49;&#125;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#61;&#32;&#92;&#108;&#101;&#102;&#116;&#40;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#97;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#98;&#94;&#123;&#45;&#49;&#125;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80; \" title=\"Rendered by QuickLaTeX.com\" height=\"22\" width=\"429\" style=\"vertical-align: -7px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>Now, applying our Fermat&#8217;s Little Theorem formula, i.e., eq.(3), we get,<\/p>\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-02798564b9e3e28167818ff2eba6e710_l3.png?resize=450%2C23&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\" &#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#97;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#98;&#94;&#123;&#45;&#49;&#125;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#61;&#32;&#92;&#108;&#101;&#102;&#116;&#40;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#97;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#98;&#94;&#123;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#80;&#32;&#45;&#32;&#50;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#125;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#80; \" title=\"Rendered by QuickLaTeX.com\" height=\"23\" width=\"450\" style=\"vertical-align: -7px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>Now, calculating <strong>b<sup>(p &#8211; 2)<\/sup> % p<\/strong> is a challenge here. As the online judges generally keep the prime number pretty big, we cannot afford the naive exponentiation technique. So, we use the <strong>Binary Exponentiation<\/strong> for this. It is very simple, for a positive integer &#8216;n&#8217;, we have (assuming integer division),<\/p>\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-3c8b60a284c3f6e059ae1501df62792b_l3.png?resize=291%2C44&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\" &#120;&#94;&#123;&#110;&#125;&#32;&#61; &#92;&#108;&#101;&#102;&#116;&#92;&#123;&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#97;&#114;&#114;&#97;&#121;&#125;&#123;&#108;&#125; &#120;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#120;&#94;&#123;&#110;&#32;&#47;&#32;&#50;&#125;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#120;&#94;&#123;&#110;&#32;&#47;&#32;&#50;&#125;&#32;&#92;&#113;&#113;&#117;&#97;&#100;&#32;&#92;&#44;&#32;&#92;&#116;&#101;&#120;&#116;&#123;&#110;&#32;&#105;&#115;&#32;&#111;&#100;&#100;&#125;&#32;&#92;&#92; &#120;&#94;&#123;&#110;&#32;&#47;&#32;&#50;&#125;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#120;&#94;&#123;&#110;&#32;&#47;&#32;&#50;&#125;&#32;&#92;&#113;&#113;&#117;&#97;&#100;&#32;&#92;&#113;&#113;&#117;&#97;&#100;&#32;&#92;&#116;&#101;&#120;&#116;&#123;&#110;&#32;&#105;&#115;&#32;&#101;&#118;&#101;&#110;&#125; &#92;&#101;&#110;&#100;&#123;&#97;&#114;&#114;&#97;&#121;&#125;&#92;&#114;&#105;&#103;&#104;&#116;&#46; \" title=\"Rendered by QuickLaTeX.com\" height=\"44\" width=\"291\" style=\"vertical-align: -17px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>This might seem like a very ordinary exponentiation technique that can be easily implemented by recursion. But if we use a little Dynamic Programming sense, by storing the value of <strong>a<sup>(n \/ 2)<\/sup><\/strong> in a variable temp, we can reduce the complexity to O(log n), which is pretty fast.<\/p>\n\n\n\n<p>So the equation 4 that we derived is the final formula of calculating&nbsp;&#8211; <\/p>\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-93be41fc55c67b18122d6b41d1508689_l3.png?resize=104%2C54&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\" &#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#92;&#99;&#102;&#114;&#97;&#99;&#123;&#97;&#125;&#123;&#98;&#125;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#92;&#109;&#111;&#100;&#32;&#80; \" title=\"Rendered by QuickLaTeX.com\" height=\"54\" width=\"104\" style=\"vertical-align: -23px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>So the overall process of computing will have a complexity of O(log n).<\/p>\n\n\n\n<p>Now, as we have the sufficient knowledge, let&#8217;s try practice. For the sake of practice, I take up a problem of HackerRank, <a href=\"https:\/\/www.hackerrank.com\/challenges\/sherlock-and-permutations\"><strong>Sherlock and Permutations<\/strong><\/a>. That&#8217;s because, I do all my coding practice in HackerRank and this problem will fit for the situation perfectly. On reading the problem statement carefully, one can easily come up with the formula &#8211;<\/p>\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-adeb82df5d5489ca1cb5a8ea9331f458_l3.png?resize=221%2C45&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\" &#92;&#99;&#102;&#114;&#97;&#99;&#123;&#32;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#32;&#110;&#32;&#43;&#32;&#109;&#32;&#45;&#32;&#49;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#33;&#125;&#123;&#92;&#108;&#101;&#102;&#116;&#32;&#40;&#109;&#32;&#45;&#32;&#49;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#32;&#41;&#32;&#33;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#110;&#33;&#125;&#32;&#92;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#40;&#32;&#49;&#48;&#94;&#123;&#57;&#125;&#32;&#43;&#32;&#55;&#41; \" title=\"Rendered by QuickLaTeX.com\" height=\"45\" width=\"221\" style=\"vertical-align: -17px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>Now, applying our equation 4, we get,<\/p>\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-c9f717bc4a558551468c75b42dc00ac3_l3.png?resize=582%2C45&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\" &#40;&#40;&#40;&#110;&#32;&#43;&#32;&#109;&#32;&#45;&#32;&#49;&#41;&#33;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#40;&#49;&#48;&#94;&#123;&#57;&#125;&#32;&#43;&#32;&#55;&#41;&#41;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#40;&#40;&#40;&#109;&#32;&#45;&#32;&#49;&#41;&#33;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#110;&#33;&#41;&#94;&#123;&#49;&#48;&#94;&#123;&#57;&#125;&#32;&#43;&#32;&#55;&#32;&#45;&#32;&#50;&#125;&#41;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#40;&#49;&#48;&#94;&#123;&#57;&#125;&#32;&#43;&#32;&#55;&#41;&#41;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#40;&#49;&#48;&#94;&#123;&#57;&#125;&#32;&#43;&#32;&#55;&#41; \" title=\"Rendered by QuickLaTeX.com\" height=\"45\" width=\"582\" style=\"vertical-align: -5px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>Don&#8217;t get scared looking at the big formula, trust me, you&#8217;ll get it easily if you work it out on paper. If it gets too heavy on you refer to my <a href=\"https:\/\/www.hackerrank.com\/challenges\/sherlock-and-permutations\/forum\"><strong>discussion thread<\/strong><\/a> to the same HackerRank problem. Now, the<\/p>\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-ce8f57b433175dc3e89ff7fed737e41e_l3.png?resize=209%2C20&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\" &#40;&#110;&#32;&#43;&#32;&#109;&#32;&#45;&#32;&#49;&#41;&#33;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#40;&#49;&#48;&#94;&#123;&#57;&#125;&#32;&#43;&#32;&#55;&#41; \" title=\"Rendered by QuickLaTeX.com\" height=\"20\" width=\"209\" style=\"vertical-align: -5px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>part can be calculated by the above described example. Now, to calculate the<\/p>\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-1be711132cb58bdaec160ed22bf17cba_l3.png?resize=171%2C23&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\" &#40;&#40;&#109;&#32;&#45;&#32;&#49;&#41;&#33;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#110;&#33;&#41;&#94;&#123;&#49;&#48;&#94;&#123;&#57;&#125;&#32;&#43;&#32;&#55;&#32;&#45;&#32;&#50;&#125; \" title=\"Rendered by QuickLaTeX.com\" height=\"23\" width=\"171\" style=\"vertical-align: -5px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>part, firstly, compute the value of<\/p>\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-2a9b28ff72e6d3735c06988520804a2e_l3.png?resize=469%2C20&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\" &#40;&#40;&#109;&#32;&#45;&#32;&#49;&#41;&#33;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#40;&#49;&#48;&#94;&#123;&#57;&#125;&#32;&#43;&#32;&#55;&#41;&#41;&#32;&#92;&#116;&#105;&#109;&#101;&#115;&#32;&#40;&#110;&#33;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#40;&#49;&#48;&#94;&#123;&#57;&#125;&#32;&#43;&#32;&#55;&#41;&#41;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#40;&#49;&#48;&#94;&#123;&#57;&#125;&#32;&#43;&#32;&#55;&#41; \" title=\"Rendered by QuickLaTeX.com\" height=\"20\" width=\"469\" style=\"vertical-align: -5px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>and let&#8217;s say we store it in a variable &#8216;temp&#8217;. Now all we have to do is to calculate<\/p>\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-2442a3c67d3cfff06d042b66f51c1988_l3.png?resize=219%2C23&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\" &#116;&#101;&#109;&#112;&#94;&#123;&#40;&#49;&#48;&#94;&#123;&#57;&#125;&#32;&#43;&#32;&#55;&#32;&#45;&#32;&#50;&#41;&#125;&#32;&#92;&#98;&#109;&#111;&#100;&#32;&#40;&#49;&#48;&#94;&#123;&#57;&#125;&#32;&#43;&#32;&#55;&#41; \" title=\"Rendered by QuickLaTeX.com\" height=\"23\" width=\"219\" style=\"vertical-align: -5px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>we do this by Binary Exponentiation Technique. To help you with coding I have put my code of the Binary Exponentiation Function here.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/3c8366ae96b54ccabebed91014b9dba4.js\"><\/script>\n\n\n\n<p>Don&#8217;t hurry things, you won&#8217;t understand them. This is a new level of difficulty, so try working out everything step-by-step on paper, you will understand them. If you are not able to get it even after trying on paper, I will gladly help you, but it&#8217;s a humble request to do your part too.<\/p>\n\n\n\n<p>This is all you have to know about solving problems related to Modular Arithmetic. Though the problems related to this subject can become exceedingly complex, these are the fundamentals of the subject. If you have any doubts, how tiny ever, feel free to comment them. If you find this post helpful, please appreciate my efforts by recommending or tweet or liking. Happy Coding&#8230;! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In competitive programming, Modular Arithmetic Properties are essential tools in solving big number problems. In the problem statement, whenever they say, &#8220;print [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10],"tags":[27,48,80,81,83],"class_list":["post-1529","post","type-post","status-publish","format-standard","hentry","category-math","tag-binary-exponentiation-technique","tag-fermats-little-theorem","tag-modular-arithmetic","tag-modular-inverse","tag-number-theory"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/1529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/comments?post=1529"}],"version-history":[{"count":14,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/1529\/revisions"}],"predecessor-version":[{"id":3930,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/1529\/revisions\/3930"}],"wp:attachment":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/media?parent=1529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/categories?post=1529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/tags?post=1529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}