{"id":3359,"date":"2018-07-28T22:15:21","date_gmt":"2018-07-29T05:15:21","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3359"},"modified":"2018-07-28T22:26:43","modified_gmt":"2018-07-29T05:26:43","slug":"leetcode-878-nth-magical-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-878-nth-magical-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 878. Nth Magical Number"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>A positive integer\u00a0is\u00a0<em>magical<\/em>\u00a0if it is divisible by either\u00a0<span style=\"font-family: monospace;\">A<\/span>\u00a0or\u00a0<span style=\"font-family: monospace;\">B<\/span>.<\/p>\n<p>Return the\u00a0<span style=\"font-family: monospace;\">N<\/span>-th magical number.\u00a0 Since the answer may be very large,\u00a0<strong>return it modulo\u00a0<\/strong><code>10^9 + 7<\/code>.<\/p>\n<div>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>N = <span id=\"example-input-1-1\">1<\/span>, A = <span id=\"example-input-1-2\">2<\/span>, B = <span id=\"example-input-1-3\">3<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">2<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>N = <span id=\"example-input-2-1\">4<\/span>, A = <span id=\"example-input-2-2\">2<\/span>, B = <span id=\"example-input-2-3\">3<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">6<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>N = <span id=\"example-input-3-1\">5<\/span>, A = <span id=\"example-input-3-2\">2<\/span>, B = <span id=\"example-input-3-3\">4<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">10<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 4:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>N = <span id=\"example-input-4-1\">3<\/span>, A = <span id=\"example-input-4-2\">6<\/span>, B = <span id=\"example-input-4-3\">4<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-4\">8<\/span>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= N\u00a0&lt;= 10^9<\/code><\/li>\n<li><code>2 &lt;= A\u00a0&lt;= 40000<\/code><\/li>\n<li><code>2 &lt;= B\u00a0&lt;= 40000<\/code><\/li>\n<\/ol>\n<h1><strong>Solution: Math + Binary Search<\/strong><\/h1>\n<p>Let n denote the number of numbers &lt;= X that are divisible by either\u00a0<span style=\"font-family: monospace;\">A<\/span>\u00a0or\u00a0<span style=\"font-family: monospace;\">B.<\/span><\/p>\n<p>n = X \/ A + X \/ B &#8211; X \/ lcm(A, B) = X \/ A + X \/ B &#8211; X \/ (A * B \/ gcd(A, B))<\/p>\n<p>Binary search for the smallest X such that n &gt;= N<\/p>\n<p>Time complexity: O(log(1e9*4e5)<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 0 ms\r\nclass Solution {\r\npublic:\r\n  int nthMagicalNumber(int N, int A, int B) {\r\n    const int kMod = 1000000007;\r\n    long l = 2;\r\n    long r = static_cast&lt;long&gt;(1e9 * 4e5);\r\n    int d = gcd(A, B);\r\n    while (l &lt; r) {\r\n      long m = (r - l) \/ 2 + l;\r\n      if (m \/ A + m \/ B - m \/ (A * B \/ d) &lt; N)\r\n        l = m + 1;\r\n      else\r\n        r = m;\r\n    }\r\n    return l % kMod;\r\n  }\r\nprivate:\r\n  int gcd(int a, int b) {\r\n    if (a % b == 0) return b;\r\n    return gcd(b, a % b);\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem A positive integer\u00a0is\u00a0magical\u00a0if it is divisible by either\u00a0A\u00a0or\u00a0B. Return the\u00a0N-th magical number.\u00a0 Since the answer may be very large,\u00a0return it modulo\u00a010^9 + 7. Example&#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":[52,359,217,360,31],"class_list":["post-3359","post","type-post","status-publish","format-standard","hentry","category-math","tag-binary-search","tag-gcd","tag-hard","tag-lcm","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3359","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=3359"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3359\/revisions"}],"predecessor-version":[{"id":3363,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3359\/revisions\/3363"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}