Advertisement
El_GEMMY

array reordering

Mar 28th, 2022
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. // Those who cannot remember the past are
  2. // condemned to repeat it (use DP -_-)
  3. // - George Santayana
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8.  
  9. using namespace std;
  10. using namespace __gnu_pbds;
  11.  
  12. #define all(v) v.begin(),v.end()
  13. #define rall(v) v.rbegin(),v.rend()
  14. #define ll long long
  15. #define ull unsigned long long
  16. #define MOD 1000000007
  17. #define PI 3.14159265
  18. #define ceil(a, b) ((a / b) + (a % b ? 1 : 0))
  19. #define imin INT_MIN
  20. #define imax INT_MAX
  21. #define inf 2000000000
  22. #define nl '\n'
  23. #define modulo(a, b, mod) ((((a) % mod) * ((b) % mod)) % mod)
  24. #define debug(x) cout << "x: " << x << nl;
  25. #define debug2(x, y) cout << "x: " << x << " y: " << y << nl;
  26. #define ordered_set tree<pair<int, int>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>
  27. #define ordered_map tree<int, int, less<>, rb_tree_tag, tree_order_statistics_node_update>
  28.  
  29. //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
  30. //vector<int> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
  31.  
  32. template<typename T = int> istream& operator>>(istream& in, vector<pair<int, int>>& v){
  33. for (auto& [x, y] : v) in >> x >> y;
  34. return in;
  35. }
  36.  
  37. template<typename T = int> istream& operator>>(istream& in, vector<T>& v){
  38. for (T& i : v) in >> i;
  39. return in;
  40. }
  41.  
  42. template<typename T = int> ostream& operator<<(ostream& out, const vector<T>& v){
  43. for (const T& x : v)
  44. out << x << ' ';
  45. return out;
  46. }
  47.  
  48. template<typename T = pair<int, int>> ostream& operator << (ostream& out, const vector<pair<int, int>>& v){
  49. for(auto& [x, y] : v){
  50. out << x << ' ' << y << nl;
  51. }
  52. return out;
  53. }
  54.  
  55. void Start_Crushing() {
  56. ios_base::sync_with_stdio(false);
  57. cin.tie(nullptr);
  58. cout.tie(nullptr);
  59. #ifndef ONLINE_JUDGE
  60. freopen("input.txt", "r", stdin);
  61. freopen("output.txt", "w", stdout);
  62. #endif
  63. }
  64.  
  65. void solve(){
  66. int n; cin >> n;
  67. vector<int> v(n);
  68.  
  69. map<int, int> odd;
  70. for(auto& i : v){
  71. cin >> i;
  72. if(i & 1)
  73. odd[i]++;
  74. }
  75.  
  76. sort(all(v), [&](int a, int b){
  77. return (a & 1) < (b & 1);
  78. });
  79.  
  80. int ans = 0;
  81. for(int i = 0; i < n; i++){
  82. if(not(v[i] & 1)) {
  83. ans += n - i - 1;
  84. }
  85. else{
  86. odd[v[i]]--;
  87. for(auto& [x, y] : odd){
  88. if(y and gcd(2 * x, v[i]) > 1)
  89. ans += y;
  90. }
  91. }
  92. }
  93. cout << ans;
  94. }
  95.  
  96. int main(){
  97. Start_Crushing();
  98.  
  99. int t = 1;
  100. /*Multiple test cases?*/ cin >> t;
  101. while (t--) {
  102. solve();
  103. if(!t) break;
  104. cout << nl;
  105. }
  106.  
  107. // for(int tc = 1; tc <= t; tc++){
  108. // cout << "Case #" << tc << ": ";
  109. // solve();
  110. // if(tc != t)
  111. // cout << nl;
  112. // }
  113.  
  114. return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement