{"id":8328,"date":"2021-04-06T19:34:22","date_gmt":"2021-04-07T02:34:22","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8328"},"modified":"2021-04-06T23:02:13","modified_gmt":"2021-04-07T06:02:13","slug":"leetcode-1819-number-of-different-subsequences-gcds","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1819-number-of-different-subsequences-gcds\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1819. Number of Different Subsequences GCDs"},"content":{"rendered":"\n<p>You are given an array&nbsp;<code>nums<\/code>&nbsp;that consists of positive integers.<\/p>\n\n\n\n<p>The&nbsp;<strong>GCD<\/strong>&nbsp;of a sequence of numbers is defined as the greatest integer that divides&nbsp;<strong>all<\/strong>&nbsp;the numbers in the sequence evenly.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, the GCD of the sequence&nbsp;<code>[4,6,16]<\/code>&nbsp;is&nbsp;<code>2<\/code>.<\/li><\/ul>\n\n\n\n<p>A&nbsp;<strong>subsequence<\/strong>&nbsp;of an array is a sequence that can be formed by removing some elements (possibly none) of the array.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example,&nbsp;<code>[2,5,10]<\/code>&nbsp;is a subsequence of&nbsp;<code>[1,2,1,<strong><u>2<\/u><\/strong>,4,1,<u><strong>5<\/strong><\/u>,<u><strong>10<\/strong><\/u>]<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>number<\/strong>&nbsp;of&nbsp;<strong>different<\/strong>&nbsp;GCDs among all&nbsp;<strong>non-empty<\/strong>&nbsp;subsequences of<\/em>&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/03\/17\/image-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [6,10,3]\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> The figure shows all the non-empty subsequences and their GCDs.\nThe different GCDs are 6, 10, 3, 2, and 1.\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> nums = [5,15,40,5,6]\n<strong>Output:<\/strong> 7\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 2 * 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>Enumerate all possible gcds (1 to max(nums)), and check whether there is a subset of the numbers that can form a given gcd i.<br>If we want to check whether 10 is a valid gcd, we found all multipliers of 10 in the array and compute their gcd.<br>ex1  gcd(10, 20, 30) = 10, true<br>ex2 gcd(20, 40, 80) = 20, false<\/p>\n\n\n\n<p>Time complexity: O(mlogm)<br>Space complexity: O(m)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countDifferentSubsequenceGCDs(vector<int>& nums) {\n    const int kMax = *max_element(begin(nums), end(nums));\n    vector<int> s(kMax + 1);\n    for (int x : nums) s[x] = 1;    \n    int ans = 0;\n    for (int i = 1; i <= kMax; ++i) {\n      int g = 0;\n      for (int j = i; j <= kMax; j += i)\n        if (s[j]) g = gcd(g, j);\n      ans += g == i;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array&nbsp;nums&nbsp;that consists of positive integers. The&nbsp;GCD&nbsp;of a sequence of numbers is defined as the greatest integer that divides&nbsp;all&nbsp;the numbers in the&#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":[359,217,31],"class_list":["post-8328","post","type-post","status-publish","format-standard","hentry","category-math","tag-gcd","tag-hard","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8328","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=8328"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8328\/revisions"}],"predecessor-version":[{"id":8332,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8328\/revisions\/8332"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}