Problem p02696 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, a, b) for ((i) = a; i < (int)(b); i++)
#define max(a, b) ((a) < (b) ? (b) : (a))
#define min(a, b) ((a) > (b) ? (b) : (a))
#define AAA (1000000007)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll A, B;
int score(ll x) { return (A * x) / B - A * (x / B); }
double Score(ll x) { return ((double)A * x) / B - A * ((double)x / B); }

void BS(ll a, ll b) {
  if (a + 1 == b || a == b) {
    ll ans = max(score(a), score(b));
    cout << ans;
    return;
  }
  if (b - a < 10000) {
    ll ans = 0;
    for (ll i = 0; a + i <= b; i++)
      ans = max(ans, score(a + i));
    cout << ans;
    return;
  }
  ll c = (a + b) / 2;
  ll p = score(a);
  ll q = score(b);
  ll r = score(c);
  //	cout << p << " " << r << " " << q << endl;
  if (p > q) {
    BS(a, c);
  } else if (p < q) {
    BS(c, b);
  } else {
    int d = (a + c) / 2;
    int e = (c + b) / 2;
    if (Score(d) < Score(e)) {
      BS(a, c);
    } else {
      BS(c, b);
    }
  }
  return;
}

int main() {
  string S;
  int i, j, k;
  // ll N, M, K;
  // int N, M, K;

  ll N, x;
  cin >> A >> B >> N;
  if (A == 1 || B == 1) {
    cout << 0;
    return 0;
  }
  cout << score(min(B - 1, N));

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, a, b) for ((i) = a; i < (int)(b); i++)
#define max(a, b) ((a) < (b) ? (b) : (a))
#define min(a, b) ((a) > (b) ? (b) : (a))
#define AAA (1000000007)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll A, B;
int score(ll x) { return (A * x) / B - A * (x / B); }

void BS(ll a, ll b) {
  if (a + 1 == b || a == b) {
    ll ans = max(score(a), score(b));
    cout << ans;
    return;
  }
  if (b - a < 10000) {
    ll ans = 0;
    for (ll i = 0; a + i <= b; i++)
      ans = max(ans, score(a + i));
    cout << ans;
    return;
  }
  ll c = (a + b) / 2;
  ll p = score(a);
  ll q = score(b);
  ll r = score(c);
  //	cout << p << " " << r << " " << q << endl;
  if (p > q) {
    BS(a, c);
  } else if (p < q) {
    BS(c, b);
  } else {
    int d = (a + c) / 2;
    if (score(d) < score(c)) {
      BS(d, b);
    } else {
      BS(a, c);
    }
  }
  return;
}

int main() {
  string S;
  int i, j, k;
  // ll N, M, K;
  // int N, M, K;

  ll N, x;
  cin >> A >> B >> N;
  if (A == 1 || B == 1) {
    cout << 0;
    return 0;
  }
  BS(0, N);

  return 0;
}