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

#include <cmath>
#include <iostream>
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) {
  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);
    printf("%lld\\n", fun(min(n, b - 1)));
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
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) {
  ll ans = floor(((ld)a * x / b)) - a * floor((ld)x / b);
  return ans;
}

signed main() {
  fast;

  ll t = 1;
  // cin>>t;
  while (t--) {
    scanf("%lld %lld %lld", &a, &b, &n);
    ll ans = 0;
    for (int k = 1;; k++) {
      ll x = (k * b + a - 1) / a;
      if (k > llinf / b) {
        break;
      }
      if (x > n or x < 0) {
        break;
      }
      ll res = fun(x);
      if (res < ans) {
        break;
      }
      ans = res;
    }
    printf("%lld\\n", ans);
  }
  return 0;
}