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

#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define ff first
#define ss second
#define file_se_input                                                          \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define pp long long int
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define zip(i, a, b) for (int i = a; i < b; i++)
#define ll unsigned long long int
#define test                                                                   \\
  long int t;                                                                  \\
  cin >> t;                                                                    \\
  while (t--)
#define um unordered_map
#define en '\\n'
#define us unordered_set
#define mm 1000000007

bool check_prime(long long n) {
  int flag = 0;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      flag = 1;
      break;
    }
  }
  if (n == 1)
    return false;
  else if (flag == 0 || n == 2 || n == 3) {
    return true;
  } else {
    return false;
  }
}

int binaryExponentiation(int x,
                         int n) // function to calculate x raise to power n
{
  int result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = result * x;
    x = x * x;
    n = n / 2;
  }
  return result;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
#endif
  //_______________________________-code
  //starts-______________________________________________________________________________

  ll a, b, n;
  cin >> a >> b >> n;
  ll x = min(n, b - 1);
  cout << (a * x) / b - a * (x / b) << en;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds; // to use policy based data structures like
                            // indexed_set  "never used yet !! "
#define vi vector<int>
#define ff first
#define ss second
#define file_se_input                                                          \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define pp long long int
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define zip(i, a, b) for (int i = a; i < b; i++)
#define ll unsigned long long int
#define test                                                                   \\
  long int t;                                                                  \\
  cin >> t;                                                                    \\
  while (t--)
#define um unordered_map
#define en '\\n'
#define us unordered_set
#define mm 1000000007

bool check_prime(long long n) {
  int flag = 0;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      flag = 1;
      break;
    }
  }
  if (n == 1)
    return false;
  else if (flag == 0 || n == 2 || n == 3) {
    return true;
  } else {
    return false;
  }
}

int binaryExponentiation(int x,
                         int n) // function to calculate x raise to power n
{
  int result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = result * x;
    x = x * x;
    n = n / 2;
  }
  return result;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  if (fopen("INPUT.txt", "r")) {
    freopen("INPUT.txt", "r", stdin);
    freopen("OUTPUT.txt", "w", stdout);
  }
#endif
  //_______________________________-code
  //starts-______________________________________________________________________________

  ll a, b, n;
  cin >> a >> b >> n;
  ll x = pow(10, 9);
  if (n < x) {
    ll ans = 0;
    for (ll i = 0; i <= n; i++) {
      ll y = (a * i) / b - a * (i / b);
      if (y >= ans) {
        ans = y;
      }
    }
    cout << ans << en;
  } else {
    ll ans = 0;
    for (ll i = 0; i <= x; i++) {
      ll y = (a * i) / b - a * (i / b);
      if (y >= ans) {
        ans = y;
      }
    }
    cout << ans << en;
  }

  return 0;
}