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

#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const long long int INF = 1e18;
const long long int mod = 1e9 + 7;

typedef pair<ll, ll> pairs;
typedef vector<pairs> p;

struct Edge {
  ll to, weight;
  Edge(ll t, ll w) : to(t), weight(w) {}
};
using graph = vector<vector<ll>>;
using Graph = vector<vector<Edge>>;

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

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

ll keta(ll N) {
  int tmp{};
  while (N > 0) {
    tmp += (N % 10);
    N /= 10;
  }
  N = tmp;
  return N;
}

vector<vector<ll>> combination(ll n, ll r) {
  vector<vector<ll>> v(n + 1, vector<ll>(n + 1, 0));
  for (ll i = 0; i < v.size(); ++i) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (ll i = 1; i < v.size(); ++i) {
    for (ll j = 0; j < i; ++j) {
      v[i][j] = (v[i - 1][j - 1] + v[i - 1][j]);
    }
  }
  return v;
}

// 回文
bool kai(string S) {
  bool flag = true;
  for (ll i = 0; i < S.size() / 2; ++i) {
    if (S[i] != S[S.size() - i - 1]) {
      flag = false;
      break;
    }
  }
  return flag;
}
// ---------------------------------------------

int main() {
  ll a, b, n;
  cin >> a >> b >> n;
  ll ans = 0;
  ll count = 0;
  count = 0;
  for (ll i = n; i >= 0; i -= b) {
    ll flag = (a * i / b) - a * (i / b);
    if (ans < flag) {
      ans = flag;
      count = 0;
    } else {
      count++;
    }
    if (count > 10000000) {
      break;
    }
  }
  count = 0;
  for (ll i = n; i >= 0; --i) {
    ll flag = (a * i / b) - a * (i / b);
    if (ans < flag) {
      ans = flag;
      count = 0;
    } else {
      count++;
    }
    if (count > 10000000) {
      break;
    }
  }
  ans = max(ans, (a * min(b - 1, n) / b) - a * (min(b - 1, n) / b));
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const long long int INF = 1e18;
const long long int mod = 1e9 + 7;

typedef pair<ll, ll> pairs;
typedef vector<pairs> p;

struct Edge {
  ll to, weight;
  Edge(ll t, ll w) : to(t), weight(w) {}
};
using graph = vector<vector<ll>>;
using Graph = vector<vector<Edge>>;

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

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

ll keta(ll N) {
  int tmp{};
  while (N > 0) {
    tmp += (N % 10);
    N /= 10;
  }
  N = tmp;
  return N;
}

vector<vector<ll>> combination(ll n, ll r) {
  vector<vector<ll>> v(n + 1, vector<ll>(n + 1, 0));
  for (ll i = 0; i < v.size(); ++i) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (ll i = 1; i < v.size(); ++i) {
    for (ll j = 0; j < i; ++j) {
      v[i][j] = (v[i - 1][j - 1] + v[i - 1][j]);
    }
  }
  return v;
}

// 回文
bool kai(string S) {
  bool flag = true;
  for (ll i = 0; i < S.size() / 2; ++i) {
    if (S[i] != S[S.size() - i - 1]) {
      flag = false;
      break;
    }
  }
  return flag;
}
// ---------------------------------------------

int main() {
  ll a, b, n;
  cin >> a >> b >> n;
  ll ans = 0;
  ll count = 0;
  count = 0;
  for (ll i = n - b - 1; i >= 0; --i) {
    ll flag = (a * i / b) - a * (i / b);
    if (ans < flag) {
      ans = flag;
      count = 0;
    } else {
      count++;
    }
    if (count > 100000000) {
      break;
    }
  }
  count = 0;
  for (ll i = n; i >= 0; --i) {
    ll flag = (a * i / b) - a * (i / b);
    if (ans < flag) {
      ans = flag;
      count = 0;
    } else {
      count++;
    }
    if (count > 100000000) {
      break;
    }
  }
  cout << ans << endl;
}