{"id":9241,"date":"2021-12-25T19:12:43","date_gmt":"2021-12-26T03:12:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9241"},"modified":"2021-12-25T19:13:27","modified_gmt":"2021-12-26T03:13:27","slug":"leetcode-1922-count-good-numbers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1922-count-good-numbers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1922. Count Good Numbers"},"content":{"rendered":"\n<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>\n\n\n\n<ul class=\"wp-block-list\"><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>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 1\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> The good numbers of length 1 are \"0\", \"2\", \"4\", \"6\", \"8\".\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4\n<strong>Output:<\/strong> 400\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 50\n<strong>Output:<\/strong> 564908303\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 10<sup>15<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Fast Power<\/strong><\/h2>\n\n\n\n<p>Easy to see that f(n) = (4 + (n &amp; 1)) * f(n &#8211; 1), f(1) = 5<\/p>\n\n\n\n<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>\n\n\n\n<p>Time complexity: O(logn)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua\nconstexpr int kMod = 1e9 + 7;\nlong long modPow(long long base, long long n) {\n  long long ans = 1;\n  while (n) {\n    if (n & 1) ans = (ans * base) % kMod;\n    base = (base * base) % kMod;\n    n >>= 1;\n  }\n  return ans;\n}\nclass Solution {\npublic:\n  int countGoodNumbers(long long n) {\n    return (modPow(4, n \/ 2) * modPow(5, (n + 1) \/ 2)) % kMod;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A digit string is&nbsp;good&nbsp;if the digits&nbsp;(0-indexed)&nbsp;at&nbsp;even&nbsp;indices are&nbsp;even&nbsp;and the digits at&nbsp;odd&nbsp;indices are&nbsp;prime&nbsp;(2,&nbsp;3,&nbsp;5, or&nbsp;7). For example,&nbsp;&#8220;2582&#8221;&nbsp;is good because the digits (2&nbsp;and&nbsp;8) at even positions are even and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,751,750],"class_list":["post-9241","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-power","tag-powmod","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9241"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9241\/revisions"}],"predecessor-version":[{"id":9243,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9241\/revisions\/9243"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}