fork download
  1. #include <bits/stdc++.h>
  2. typedef long long ll;
  3. using namespace std;
  4.  
  5. void fastIO() {
  6. ios::sync_with_stdio(false);
  7. cin.tie(nullptr);
  8. }
  9.  
  10. int main() {
  11. fastIO();
  12. ll t;
  13. cin >> t;
  14. while (t--) {
  15. ll n, m;
  16. cin >> n >> m;
  17. // compute maximum g such that g(g-1) <= n:
  18. // g <= (1 + sqrt(1+4n)) / 2
  19. long double D = 1.0L + 4.0L * n;
  20. ll root = (ll)((1.0L + sqrtl(D)) / 2.0L);
  21. // clamp to [0..m]
  22. ll G = min(root, m);
  23. // we need g >= 2 so that a = g*(g-1) >= 1
  24. // count is number of g in [2..G] => max(0, G-1)
  25. ll ans = G >= 2 ? (G - 1) : 0;
  26. cout << ans << "\n";
  27. }
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0.01s 5320KB
stdin
6
1 1
2 3
3 5
10 8
100 1233
1000000 1145141
stdout
0
1
1
2
9
999