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

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

int main(void) {
  long long a, b, n;
  cin >> a >> b >> n;
  cout << (a * min(b - 1, n)) / b - a * (min(b - 1, n) / b) << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

/*
 * Welcome to my code!
 *---------------------------------------------*
 * author : lynmisakura(twitter : @andoreiji11)
 */

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define repn for (int i = 0; i < n; i++)
#define ain(a)                                                                 \\
  for (auto &i : a)                                                            \\
    cin >> i;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define cont continue

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using pi = pair<int, int>;
using vpi = vector<pi>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;

template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> void print(std::vector<T> a) {
  int sz = a.size();
  for (int i = 0; i < a.size(); i++)
    cout << a[i] << (i < sz - 1 ? ' ' : '\\n');
}
void ioboost() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
}
long long gcd(long long a, long long b) { return b != 0 ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
// a x + b y = gcd(a, b)
long long extgcd(long long a, long long b, long long &x, long long &y) {
  long long g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}
// 素因数分解 O(√n)
map<ll, ll> prime_factor(ll n) {
  map<ll, ll> res;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ++res[i];
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1LL;
  return res;
}
template <class T> T qp(T a, long long b) {
  T ans = 1;
  do {
    if (b & 1)
      ans = 1 * ans * a;
    a *= a;
  } while (b >>= 1);
  return ans;
}

///////////////////////////////////////////////////////////
int main(int argc, char const *argv[]) {
  ioboost();
  ll a, b, n;
  cin >> a >> b >> n;

  auto pfa = prime_factor(a);
  auto pfb = prime_factor(b);

  ll g = 1;
  for (auto i : pfa) {
    g *= qp(i.first, min(i.second, pfb[i.first]));
  }
  ll y = b / g;
  ll z = gcd(y, b);
  y /= z;

  if (y >= (n + b - 1) / b) {
    ll ans = 0;
    for (ll x = n - 1e8; x <= n; x++) {
      if (0 <= x and x <= n) {
        chmax(ans, (a * x / b) - a * (x / b));
      }
    }
    cout << ans << '\\n';
  } else {
    ll x = y * b - 1;
    ll ans = 0;
    for (ll y = x - 5e7; y <= x + 5e7; x++) {
      if (0 <= y and y <= n) {
        chmax(ans, (a * y / b) - a * (y / b));
      }
    }
    cout << ans << '\\n';
  }
  return 0;
}