My Solution
\n/**\n * Time Complexity: O(n + (max_num + n_queries) * log(max_num))\n * Space Complexity: O(max_num)\n * where `n` is the length of the vector `nums`\n *    
```\n/**\n * Time Complexity: O(n + (max_num + n_queries) * log(max_num))\n * Space Complexity: O(max_num)\n * where `n` is the length of the vector `nums`\n *       `max_num` is the maximum number of the vector `nums`\n *       `n_queries` is the length of the vector `queries`\n */\nclass Solution {\n public:\n  vector<int> gcdValues(const vector<int> &nums, const vector<long long> &queries) {\n    const int max_num = *max_element(nums.begin(), nums.end());\n    int num_to_count[max_num + 1];\n    memset(num_to_count, 0, sizeof(num_to_count));\n    for (const int num : nums) {\n      num_to_count[num] += 1;\n    }\n    \n    long long gcd_to_count[max_num + 1];\n    memset(gcd_to_count, 0, sizeof(gcd_to_count));\n    for (int num = max_num; num > 0; --num) {\n      int count = 0;\n      for (int multiple = num; multiple < max_num + 1; multiple += num) {\n        count += num_to_count[multiple];\n        gcd_to_count[num] -= gcd_to_count[multiple];\n      }\n      gcd_to_count[num] += (static_cast<long long>(count) * (count - 1)) >> 1;\n    }\n    \n    for (int i = 1; i < max_num + 1; ++i) {\n      gcd_to_count[i] += gcd_to_count[i - 1];\n    }\n    \n    vector<int> ret(queries.size());\n    for (size_t i = 0; i < queries.size(); ++i) {\n      ret[i] = static_cast<int>(upper_bound(gcd_to_count,\n                                            gcd_to_count + max_num + 1,\n                                            queries[i]) - gcd_to_count);\n    }\n    return ret;\n  }\n};\n```
[]