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

#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fore(i, l, r) for (int i = int(l); i < int(r); i++)
#define pb push_back
#define deb(x) cout << #x << " " << x << endl;
#define deb2(x, y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define deb3(x, y, z)                                                          \\
  cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z     \\
       << endl;
#define all(x) x.begin(), x.end()
#define sz(a) int((a).size())
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;
template <class T, class U> inline void add_self(T &a, U b) {
  a += b;
  if (a >= mod)
    a -= mod;
  if (a < 0)
    a += mod;
}
template <class T, class U> inline void min_self(T &x, U y) {
  if (y < x)
    x = y;
}
template <class T, class U> inline void max_self(T &x, U y) {
  if (y > x)
    x = y;
}

template <typename T> void print(T v, bool show_index = false) {
  int w = 2;
  if (show_index) {
    for (int i = 0; i < v.size(); i++)
      cout << setw(w) << i << " ";
    cout << endl;
  }
  for (auto i = v.begin(); i != v.end(); i++)
    cout << setw(w) << *i << " ";
  cout << endl;
}
template <typename T> void print_vv(T v) {
  if (v.size() == 0) {
    cout << "Empty" << endl;
    return;
  }
  int w = 3;
  cout << setw(w) << " ";
  for (int j = 0; j < v[0].size(); j++)
    cout << setw(w) << j << " ";
  cout << endl;
  for (auto i = 0; i < v.size(); i++) {
    cout << i << " {";
    for (auto j = 0; j != v[i].size(); j++) {
      cout << setw(w) << v[i][j] << ",";
    }
    cout << "}," << endl;
  }
  cout << endl;
}
template <class T, class U> void print_m(map<T, U> m, int w = 3) {
  if (m.empty()) {
    cout << "Empty" << endl;
    return;
  }
  for (auto x : m)
    cout << "(" << x.first << ": " << x.second << ")," << endl;
  cout << endl;
}

// Since the answer is always 0 for multiples take sth max less then
// multiple, so min(b-1,n)
int main() {
  ll a, b, n;
  while (cin >> a >> b >> n) {
    ll x = min(n, b - 1);
    ll res = (a * x) / b;
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define fore(i, l, r) for (int i = int(l); i < int(r); i++)
#define pb push_back
#define deb(x) cout << #x << " " << x << endl;
#define deb2(x, y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define deb3(x, y, z)                                                          \\
  cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z     \\
       << endl;
#define all(x) x.begin(), x.end()
#define sz(a) int((a).size())
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;
template <class T, class U> inline void add_self(T &a, U b) {
  a += b;
  if (a >= mod)
    a -= mod;
  if (a < 0)
    a += mod;
}
template <class T, class U> inline void min_self(T &x, U y) {
  if (y < x)
    x = y;
}
template <class T, class U> inline void max_self(T &x, U y) {
  if (y > x)
    x = y;
}

template <typename T> void print(T v, bool show_index = false) {
  int w = 2;
  if (show_index) {
    for (int i = 0; i < v.size(); i++)
      cout << setw(w) << i << " ";
    cout << endl;
  }
  for (auto i = v.begin(); i != v.end(); i++)
    cout << setw(w) << *i << " ";
  cout << endl;
}
template <typename T> void print_vv(T v) {
  if (v.size() == 0) {
    cout << "Empty" << endl;
    return;
  }
  int w = 3;
  cout << setw(w) << " ";
  for (int j = 0; j < v[0].size(); j++)
    cout << setw(w) << j << " ";
  cout << endl;
  for (auto i = 0; i < v.size(); i++) {
    cout << i << " {";
    for (auto j = 0; j != v[i].size(); j++) {
      cout << setw(w) << v[i][j] << ",";
    }
    cout << "}," << endl;
  }
  cout << endl;
}
template <class T, class U> void print_m(map<T, U> m, int w = 3) {
  if (m.empty()) {
    cout << "Empty" << endl;
    return;
  }
  for (auto x : m)
    cout << "(" << x.first << ": " << x.second << ")," << endl;
  cout << endl;
}

int main() {
  ll a, b, n;
  while (cin >> a >> b >> n) {
    ll prev = -323;
    set<ll> seen;
    for (ll i = 1; i <= n; ++i) {
      ll ans = (a * i) / b - a * (i / b);
      if (ans != prev && seen.count(ans)) {
        cout << *max_element(all(seen)) << endl;
        break;
      }
      seen.insert(ans);
      prev = ans;
    }
    cout << *max_element(all(seen)) << endl;
  }
  return 0;
}