<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>powmod Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/powmod/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/powmod/</link>
	<description></description>
	<lastBuildDate>Sun, 26 Dec 2021 03:13:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>powmod Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/powmod/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1922. Count Good Numbers</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-1922-count-good-numbers/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-1922-count-good-numbers/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 26 Dec 2021 03:12:43 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[power]]></category>
		<category><![CDATA[powmod]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9241</guid>

					<description><![CDATA[<p>A digit string is&#160;good&#160;if the digits&#160;(0-indexed)&#160;at&#160;even&#160;indices are&#160;even&#160;and the digits at&#160;odd&#160;indices are&#160;prime&#160;(2,&#160;3,&#160;5, or&#160;7). For example,&#160;"2582"&#160;is good because the digits (2&#160;and&#160;8) at even positions are even and&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1922-count-good-numbers/">花花酱 LeetCode 1922. Count Good Numbers</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>A digit string is&nbsp;<strong>good</strong>&nbsp;if the digits&nbsp;<strong>(0-indexed)</strong>&nbsp;at&nbsp;<strong>even</strong>&nbsp;indices are&nbsp;<strong>even</strong>&nbsp;and the digits at&nbsp;<strong>odd</strong>&nbsp;indices are&nbsp;<strong>prime</strong>&nbsp;(<code>2</code>,&nbsp;<code>3</code>,&nbsp;<code>5</code>, or&nbsp;<code>7</code>).</p>



<ul><li>For example,&nbsp;<code>"2582"</code>&nbsp;is good because the digits (<code>2</code>&nbsp;and&nbsp;<code>8</code>) at even positions are even and the digits (<code>5</code>&nbsp;and&nbsp;<code>2</code>) at odd positions are prime. However,&nbsp;<code>"3245"</code>&nbsp;is&nbsp;<strong>not</strong>&nbsp;good because&nbsp;<code>3</code>&nbsp;is at an even index but is not even.</li></ul>



<p>Given an integer&nbsp;<code>n</code>, return&nbsp;<em>the&nbsp;<strong>total</strong>&nbsp;number of good digit strings of length&nbsp;</em><code>n</code>. Since the answer may be large,&nbsp;<strong>return it modulo&nbsp;</strong><code>10<sup>9</sup>&nbsp;+ 7</code>.</p>



<p>A&nbsp;<strong>digit string</strong>&nbsp;is a string consisting of digits&nbsp;<code>0</code>&nbsp;through&nbsp;<code>9</code>&nbsp;that may contain leading zeros.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 1
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good numbers of length 1 are "0", "2", "4", "6", "8".
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4
<strong>Output:</strong> 400
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 50
<strong>Output:</strong> 564908303
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= n &lt;= 10<sup>15</sup></code></li></ul>



<h2><strong>Solution: Fast Power</strong></h2>



<p>Easy to see that f(n) = (4 + (n &amp; 1)) * f(n &#8211; 1), f(1) = 5</p>



<p>However, since n is huge, we need to rewrite f(n) as 4<sup>n/2</sup> * 5<sup>(n+1)/2</sup> and use fast power to compute it.</p>



<p>Time complexity: O(logn)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
constexpr int kMod = 1e9 + 7;
long long modPow(long long base, long long n) {
  long long ans = 1;
  while (n) {
    if (n &amp; 1) ans = (ans * base) % kMod;
    base = (base * base) % kMod;
    n &gt;&gt;= 1;
  }
  return ans;
}
class Solution {
public:
  int countGoodNumbers(long long n) {
    return (modPow(4, n / 2) * modPow(5, (n + 1) / 2)) % kMod;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-1922-count-good-numbers/">花花酱 LeetCode 1922. Count Good Numbers</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-1922-count-good-numbers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
