fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define int long long int
  5. #define ld long double
  6. #define all(x) x.begin(), x.end()
  7. #define sortall(x) sort(all(x))
  8. #define endl '\n'
  9. #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  10. template<class T>
  11. void printC (T Collection)
  12. {
  13. for (auto&i:Collection)
  14. cout << i << " \n";
  15. cout << '\n';
  16. }
  17.  
  18. /*
  19.  * Think twice, code once
  20.  * Think of different approaches to tackle a problem: write them down.
  21.  * Think of different views of the problem. don't look from only one side.
  22.  * don't get stuck in one approach.
  23.  * common mistakes: - over_flow
  24.  * - out_of_bound index
  25.  * - infinite loop
  26.  * - corner cases
  27.  * - duplication counting.
  28. */
  29.  
  30. void solve()
  31. {
  32. int n, k; cin >> n >> k;
  33. int l = 2, r = k, ans = -1;
  34. if (n == 1)
  35. {
  36. cout << 0;
  37. return;
  38. }
  39. while (l <= r)
  40. {
  41. int mid = (l+r)>>1;
  42. if ([&](int)->bool
  43. {
  44. int have = k*(k+1)/2 - (mid-1)*mid/2 - (k-mid);
  45. return have >= n;
  46. }(mid))
  47. {
  48. ans = k-mid+1;
  49. l = mid + 1;
  50. }else
  51. r = mid - 1;
  52. }
  53. cout << ans;
  54. }
  55.  
  56. int32_t main()
  57. {
  58. // #ifndef ONLINE_JUDGE
  59. // freopen("input.txt", "r", stdin);
  60. // freopen("output.txt", "w", stdout);
  61. // freopen("Errors.txt", "w", stderr);
  62. // #endif
  63. fast
  64. int t = 1;
  65. // cin >> t;
  66. while (t--)
  67. {
  68. solve();
  69. if (t) cout << '\n';
  70. }
  71. cout << '\n';
  72. return 0;
  73. }
Success #stdin #stdout 0.01s 5296KB
stdin
Standard input is empty
stdout
-1