fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int totalValidPairs(vector<int> a, vector<int> b){
  5.  
  6. unordered_map<int, int> mp1, mp2;
  7.  
  8. for(int i : a){
  9. mp1[i]++;
  10. }
  11.  
  12. for(int i : b){
  13. mp2[i]++;
  14. }
  15.  
  16. int total_count = 0;
  17.  
  18. for (auto i: mp1){
  19. int curr = i.first, count = 0;
  20.  
  21. for (int j = curr; j <= 1e6+7; j += curr){
  22. count += mp2[j];
  23. }
  24.  
  25. total_count += (count * i.second);
  26.  
  27. }
  28.  
  29. return total_count;
  30. }
  31.  
  32. int main() {
  33.  
  34. vector<int> a = {2, 2, 3, 3, 3, 5, 5};
  35. vector<int> b = {10, 20, 25, 30, 50};
  36. cout << totalValidPairs(a, b);
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0.1s 34392KB
stdin
Standard input is empty
stdout
21