fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  5.  
  6. #define int long long
  7. #define pb push_back
  8. #define ff first
  9. #define ss second
  10. #define all(x) (x).begin(), (x).end()
  11. #define rall(x) (x).rbegin(), (x).rend()
  12. #define sz(x) ((int)(x).size())
  13. #define endl '\n'
  14. #define yes cout << "yes\n"
  15. #define no cout << "no\n"
  16.  
  17. #define rep(i,a,b) for(int i=a;i<b;++i)
  18. #define per(i,a,b) for(int i=b-1;i>=a;--i)
  19. #define each(x, a) for (auto& x : a)
  20.  
  21. const int INF = 1e18;
  22. const int MOD = 1e9+7;
  23. const int N = 2e5 + 5;
  24.  
  25. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  26. int lcm(int a, int b) { return (a / gcd(a, b)) * b; }
  27.  
  28. int power(int a, int b, int m = MOD) {
  29. int res = 1;
  30. while (b > 0) {
  31. if (b & 1) res = res * a % m;
  32. a = a * a % m;
  33. b >>= 1;
  34. }
  35. return res;
  36. }
  37.  
  38. int modinv(int a, int m = MOD) {
  39. return power(a, m - 2, m);
  40. }
  41.  
  42. void solve() {
  43. int n, k;
  44. cin >> n >> k;
  45.  
  46. vector<int> q(n), r(n);
  47. map<int, int> q_freq, r_freq;
  48.  
  49. rep(i, 0, n) {
  50. cin >> q[i];
  51. q_freq[q[i]]++;
  52. }
  53. rep(i, 0, n) {
  54. cin >> r[i];
  55. r_freq[r[i]]++;
  56. }
  57.  
  58. int total_operations = 0;
  59.  
  60. // Iterate over all unique q_i and r_j
  61. each(q_pair, q_freq) {
  62. int q_val = q_pair.ff;
  63. int q_count = q_pair.ss;
  64.  
  65. each(r_pair, r_freq) {
  66. int r_val = r_pair.ff;
  67. int r_count = r_pair.ss;
  68.  
  69. // Check the validity condition for the pair (q_val, r_val):
  70. // r + 1 <= floor((k - r) / q)
  71.  
  72. // 1. Check if k - r is non-negative and q is non-zero (since q_val >= 1, it's non-zero)
  73. if (k < r_val) {
  74. // If k < r, then (k - r) / q is negative, which cannot be >= r + 1 (since r >= 1)
  75. continue;
  76. }
  77.  
  78. // 2. Calculate the upper bound for y: floor((k - r) / q)
  79. int y_upper_bound = (k - r_val) / q_val;
  80.  
  81. // 3. Check the condition: r + 1 <= y_upper_bound
  82. if (r_val + 1 <= y_upper_bound) {
  83. // The pair (q_val, r_val) is valid.
  84.  
  85. // The number of possible operations is limited by the minimum frequency.
  86. int matches = min(q_count, r_count);
  87.  
  88. total_operations += matches;
  89. }
  90. }
  91. }
  92.  
  93. cout << total_operations << endl;
  94. }
  95.  
  96. int32_t main() {
  97. fast_io;
  98.  
  99. int t;
  100. cin >> t;
  101. while (t--) {
  102. solve();
  103. }
  104.  
  105. return 0;
  106. }
Success #stdin #stdout 0s 5312KB
stdin
3
1 100
1
27
3 10
5 6 5
7 1 7
5 42
5 4 2 2 1
9 8 9 8 100
stdout
1
0
6