Inclusion-Exclusion Principle || Binary Search
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int[] gcdValues(int[] nums, long[] queries) {\n        int max = 0;\n        for (int e : nums) max = Math.max(max, e);\n        int[] freq = new int[max+1];\n        for (int e : nums) ++freq[e];\n        long[] cnt = new long[max+1];\n        for (int i = max; i >= 1; --i) {\n            int x = 0;\n            for (int j = i; j <= max; j += i) {\n                x += freq[j];\n                cnt[i] -= cnt[j];\n            }\n            cnt[i] += x * (long) (x - 1) / 2;\n        }\n        for (int i = 1; i <= max; ++i) cnt[i] += cnt[i-1];\n        int[] ans = new int[queries.length];\n        for (int i = 0; i < queries.length; ++i) {\n            ans[i] = binarySearch(cnt, queries[i] + 1);\n        }\n        return ans;\n    }\n\n    private int binarySearch(long[] a, long t) {\n        int low = 0, high = a.length - 1;\n        while (low <= high) {\n            int mid = low + ((high - low) >>> 1);\n            if (t <= a[mid]) high = mid - 1;\n            else low = mid + 1;\n        }\n        return low;\n    }\n}\n```
['Java']