Problem p02696 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;

#define endl '\\n'
#define ff first
#define ss second
#define int long long
#define pb push_back
#define f(i, l, r) for (ll i = l; i <= r; i++)
#define rf(i, r, l) for (ll i = r; i >= l; i--)
#define Max 5000001
#define inf 2147483647
#define llinf 9223372036854775807
#define all(v) v.begin(), v.end()
#define bp(n) __builtin_popcountll(n)
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

#define d0(a) cout << a << ' ';
#define d1(a) cout << a << endl;
#define d2(a, b) cout << a << ' ' << b << endl;
#define d3(a, b, c) cout << a << ' ' << b << ' ' << c << endl;
#define d4(a, b, c, d) cout << a << ' ' << b << ' ' << c << ' ' << d << endl;
#define d5(a, b, c, d, e)                                                      \\
  cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << endl;
#define d6(a, b, c, d, e, f)                                                   \\
  cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << ' ' << f << endl;

const ll N = 100, mod = 1e9 + 7, bit = 61;
const ld PI = acos(-1.00);

ll a, b, n;

ll fun(ll x) {
  assert(x <= n);
  ll ans = ((a * x) / b) - a * (x / b);
  return ans;
}

signed main() {
  fast;

  ll t = 1;
  // cin>>t;
  while (t--) {
    scanf("%lld %lld %lld", &a, &b, &n);
    ll mx = (a * n) / b;
    ll l = 0, r = n, x = n;
    while (l <= r) {
      ll mid = (l + r) >> 1;
      ll nw = (a * mid) / b;
      if (nw == mx) {
        x = min(x, mid);
        r = mid - 1;
      } else {
        l = mid + 1;
      }
    }
    ll ans = fun(x);
    f(i, 0, min(5000000ll, n)) {
      ans = max(ans, fun(i));
      ans = max(ans, fun(n / max(1ll, i)));
    }
    //        f(i,1,5000000)
    //        {
    //            ll x=i;
    //            ll k=min(b*x+b-1,n);
    //            ans=max(ans,fun(k));
    //            x=b/i;
    //            k=min(b*x+b-1,n);
    //            ans=max(ans,fun(k));
    //        }
    ans = max(ans, fun(min(n, b - 1)));
    ans = max(ans, fun(min(n, a)));
    ans = max(ans, fun(n));
    // assert(ans<=fun(min(a,b-1)));
    printf("%lld\\n", ans);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;

#define endl '\\n'
#define ff first
#define ss second
#define int long long
#define pb push_back
#define f(i, l, r) for (ll i = l; i <= r; i++)
#define rf(i, r, l) for (ll i = r; i >= l; i--)
#define Max 5000001
#define inf 2147483647
#define llinf 9223372036854775807
#define all(v) v.begin(), v.end()
#define bp(n) __builtin_popcountll(n)
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

#define d0(a) cout << a << ' ';
#define d1(a) cout << a << endl;
#define d2(a, b) cout << a << ' ' << b << endl;
#define d3(a, b, c) cout << a << ' ' << b << ' ' << c << endl;
#define d4(a, b, c, d) cout << a << ' ' << b << ' ' << c << ' ' << d << endl;
#define d5(a, b, c, d, e)                                                      \\
  cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << endl;
#define d6(a, b, c, d, e, f)                                                   \\
  cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << ' ' << f << endl;

const ll N = 100, mod = 1e9 + 7, bit = 61;
const ld PI = acos(-1.00);

ll a, b, n;

ll fun(ll x) {
  assert(x >= 0 and x <= n);
  ll ans = ((a * x) / b) - a * (x / b);
  return ans;
}

signed main() {
  fast;

  ll t = 1;
  // cin>>t;
  while (t--) {
    scanf("%lld %lld %lld", &a, &b, &n);
    ll mx = (a * n) / b;
    ll l = 0, r = n, x = n;
    while (l <= r) {
      ll mid = (l + r) >> 1;
      ll nw = (a * mid) / b;
      if (nw == mx) {
        x = min(x, mid);
        r = mid - 1;
      } else {
        l = mid + 1;
      }
    }
    ll ans = fun(x);
    f(i, 0, min(5000000ll, n)) {
      ans = max(ans, fun(i));
      ans = max(ans, fun(n / max(1ll, i)));
    }
    f(i, 1, 5000000) {
      ll x = i;
      ll k = min(b * x + b - 1, n);
      ans = max(ans, fun(k));
      x = b / i;
      k = min(b * x + b - 1, n);
      ans = max(ans, fun(k));
    }
    ans = max(ans, fun(min(n, b - 1)));
    ans = max(ans, fun(min(n, a)));
    ans = max(ans, fun(n));
    // assert(ans<=fun(min(a,b-1)));
    printf("%lld\\n", ans);
  }
  return 0;
}