Skip to content

Commit

Permalink
Create 2183.Count-Array-Pairs-Divisible-by-K.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Feb 21, 2022
1 parent cc3ffaa commit ccb314e
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public:
long long countPairs(vector<int>& nums, int k)
{
unordered_set<int>yueshu;
for (int i=1; i*i<=k; i++)
{
if (k%i!=0) continue;
yueshu.insert(i);
if (i*i!=k)
yueshu.insert(k/i);
}

unordered_map<int, vector<int>>Map;
for (int i=0; i<nums.size(); i++)
for (auto x: yueshu)
{
if (nums[i]%x==0)
Map[x].push_back(i);
}

long long ret = 0;
for (int i=0; i<nums.size(); i++)
{
int a = gcd(nums[i], k);
int b = k/a;
auto iter = upper_bound(Map[b].begin(), Map[b].end(), i);
ret += Map[b].end() - iter;
}

return ret;

}
};

0 comments on commit ccb314e

Please sign in to comment.