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

#include <bits/stdc++.h>
#define rep(i, cc, n) for (int i = cc; i <= n; ++i)
#define drep(i, cc, n) for (int i = cc; i >= n; --i)
#define sz(s) (int)(s.size())
#define vecprint(v)                                                            \\
  rep(i, 0, v.size() - 1) cout << v[i] << " ";                                 \\
  cout << endl;
#define arrprint(v, n)                                                         \\
  rep(i, 0, n - 1) cout << v[i] << " ";                                        \\
  cout << endl;
#define matprint(v, n, m)                                                      \\
  rep(i, 0, n - 1) {                                                           \\
    rep(j, 0, m - 1) { cout << v[i][j] << " "; }                               \\
    cout << endl;                                                              \\
  }
#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
using namespace std;
const int mod = 1000000007;
const int INF = 1001001001;
typedef long long ll;
typedef pair<int, int> P;
template <typename T>
using priority_queue_rev =
    priority_queue<T, vector<T>, greater<T>>; // 小さいものから取り出す

ll f(ll a, ll b, ll x) { return (a * x) / b - a * (x / b); }

int main() {
  ll a, b, n;
  cin >> a >> b >> n;

  if (n >= b - 1) {
    cout << a * (b - 1) / b << endl;
    return 0;
  }
  cout << f(a, b, n) << endl;

  // ll l = 0,r = n;
  // int cnt = 1000000;
  // while(cnt--){
  //     ll c1,c2;
  //     c1 = (l*2+r)/3;
  //     c2 = (l+r*2)/3;
  //     if(f(a,b,c1)<f(a,b,c2)){
  //         l = c1;
  //     }else{
  //         r = c2;
  //     }
  // }
  // ll ans = 0;
  // rep(i,l,r)ans = max(ans,f(a,b,i));
  // cout << ans << endl;

  // cout << max(max(f(a,b,l),f(a,b,r)),f(a,b,(r+l)/2)) << endl;
  // cout << f(a,b,l) << " " << f(a,b,r) << endl;
  // cout << l << " " << r << endl;

  //  printf("%.10f\\n",ans);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, cc, n) for (int i = cc; i <= n; ++i)
#define drep(i, cc, n) for (int i = cc; i >= n; --i)
#define sz(s) (int)(s.size())
#define vecprint(v)                                                            \\
  rep(i, 0, v.size() - 1) cout << v[i] << " ";                                 \\
  cout << endl;
#define arrprint(v, n)                                                         \\
  rep(i, 0, n - 1) cout << v[i] << " ";                                        \\
  cout << endl;
#define matprint(v, n, m)                                                      \\
  rep(i, 0, n - 1) {                                                           \\
    rep(j, 0, m - 1) { cout << v[i][j] << " "; }                               \\
    cout << endl;                                                              \\
  }
#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())
using namespace std;
const int mod = 1000000007;
const int INF = 1001001001;
typedef long long ll;
typedef pair<int, int> P;
template <typename T>
using priority_queue_rev =
    priority_queue<T, vector<T>, greater<T>>; // 小さいものから取り出す

ll f(ll a, ll b, ll x) { return (a * x) / b - a * (x / b); }

int main() {
  ll a, b, n;
  cin >> a >> b >> n;

  ll l = 0, r = n;
  int cnt = 1000000;
  while (cnt--) {
    ll c1, c2;
    c1 = (l * 2 + r) / 3;
    c2 = (l + r * 2) / 3;
    if (f(a, b, c1) < f(a, b, c2))
      l = c1;
    else
      r = c2;
  }
  ll ans = 0;
  rep(i, l, r) ans = max(ans, f(a, b, i));
  cout << ans << endl;

  // cout << max(max(f(a,b,l),f(a,b,r)),f(a,b,(r+l)/2)) << endl;
  // cout << f(a,b,l) << " " << f(a,b,r) << endl;
  // cout << l << " " << r << endl;

  //  printf("%.10f\\n",ans);

  return 0;
}