{"id":1635,"date":"2021-08-15T17:09:14","date_gmt":"2021-08-16T00:09:14","guid":{"rendered":"http:\/\/66.94.114.210\/?p=1635"},"modified":"2021-08-15T17:09:14","modified_gmt":"2021-08-16T00:09:14","slug":"binary-exponentiation-template","status":"publish","type":"post","link":"https:\/\/nanzhou.cc\/index.php\/2021\/08\/15\/binary-exponentiation-template\/","title":{"rendered":"Binary Exponentiation Template"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code lang=\"cpp\" class=\"language-cpp\">constexpr int kMod = 1000'000'007;\n\nusing ll = int64_t;\n  \ninline ll Modular(ll num, bool modular) {\n  return modular ? num % kMod : num;\n}\n\nll FastPow(ll num, ll exponent, bool modular) {\n  ll res = 1;\n  ll base = Modular(num, modular);\n  while (exponent > 1) {\n    if ((exponent &amp; 1) == 1) {\n      res = Modular(res * base, modular);\n    }\n    base = Modular(base * base, modular);\n    exponent >>= 1;\n  }\n  return Modular(res * base, modular);\n}\n\nll FastPowRecursion(ll num, ll exponent, bool modular) {\n  if (exponent == 0) {\n    return 1;\n  }\n  if (exponent == 1) {\n    return Modular(num, modular);\n  }\n  \/\/ without the following line, num * num might overflow\n  num = Modular(num, modular);\n  return Modular(((exponent % 2 == 0) ? 1 : num) * FastPowRecursion(num * num, exponent \/ 2, modular), modular);\n}\n<\/code><\/pre>\n\n\n\n<p>The recursion version is not slower than the while loop version when it is <code>o2<\/code> optimized or above. See https:\/\/godbolt.org\/z\/E9jYefrKG.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The recursion version is not slower than the while loop version when it is o2 optimized or above. See https:\/\/godbolt.org\/z\/E9jYefrKG.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,49],"tags":[],"class_list":["post-1635","post","type-post","status-publish","format-standard","hentry","category-alg","category-math"],"_links":{"self":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/comments?post=1635"}],"version-history":[{"count":2,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1635\/revisions"}],"predecessor-version":[{"id":1637,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/posts\/1635\/revisions\/1637"}],"wp:attachment":[{"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/media?parent=1635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/categories?post=1635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nanzhou.cc\/index.php\/wp-json\/wp\/v2\/tags?post=1635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}