forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 2183.Count-Array-Pairs-Divisible-by-K.cpp
- Loading branch information
1 parent
cc3ffaa
commit ccb314e
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
Math/2183.Count-Array-Pairs-Divisible-by-K/2183.Count-Array-Pairs-Divisible-by-K.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
}; |