SalmaYasser

Untitled

Nov 7th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. // you can use includes, for example:
  2. // #include <algorithm>
  3.  
  4. // you can write to stdout for debugging purposes, e.g.
  5. // cout << "this is a debug message" << endl;
  6. #include <vector>
  7. #include <map>
  8.  
  9. int gcd (int a , int b)
  10. {
  11. if ( b == 0)
  12. return a;
  13. return gcd(b,a%b);
  14. }
  15. int find_lcm ( vector <int> &b)
  16. {
  17. long long res = b[0];
  18. int sz = b.size();
  19.  
  20. for (int i = 1 ; i < sz ; i ++)
  21. {
  22. res = (((b[i] * res)) /
  23. ( gcd(b[i], res )));
  24. }
  25. return res;
  26. }
  27. int solution(vector<int> &X, vector<int> &Y) {
  28. // write your code in C++14 (g++ 6.2.0)
  29.  
  30. int sz = X.size();
  31. int sz2 = Y.size();
  32.  
  33. if (sz < 2 || sz2 < 2)
  34. return 0;
  35.  
  36.  
  37. int lcm = find_lcm (Y);
  38.  
  39.  
  40.  
  41. int res = 0;
  42.  
  43. map <int , int> my_map;
  44. for (int i = 0 ; i < sz ; i ++)
  45. {
  46. X[i] = X[i] * (lcm/Y[i]);
  47.  
  48. int c ;
  49. if (my_map.find(lcm - X[i]) == my_map.end())
  50. c = 0;
  51. else
  52. c = my_map[lcm - X[i]];
  53.  
  54. res += c;
  55. res %= 1000000007;
  56.  
  57. if (my_map.find(X[i]) == my_map.end())
  58. c = 0;
  59. else
  60. c = my_map[X[i]];
  61.  
  62. my_map[X[i]] = c + 1;
  63. }
  64. return res;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment