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

#include <bits/stdc++.h>                 // below comment for pbds
#include <ext/pb_ds/assoc_container.hpp> // to use it make ordered_set
#include <ext/pb_ds/tree_policy.hpp> // all set fnct + = 1. order_of_key(x); // number of
#define ll long long                 // element strictly smaller than it;
#define ld                                                                     \\
  long double // 2. find_by_order(x),pointer el at x pos after sort  use as
              // s.f();
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.flush();
  ll t = 1;
  ll CaseN = 1;
  // cin>>t;
  while (t--) {
    ll a, b, n;
    ll ans = 0;
    cin >> a >> b >> n;
    ll x;
    // ll k = (a*n)/b;

    // ll x = b*k/a ;
    //  bins=ary search in n
    // ax = bk ;      k - k/a;
    ll s = 0;
    ll e = n; // x range

    while (s <= e) {
      // cout<<"--"<<endl;
      ll mid = s + (e - s) / 2;
      ll Ex = (a * mid / b);
      ll p = (mid / b);
      Ex = Ex - (a * p);
      // ans = max(Ex,ans);
      if (mid % b == 0 && (a * mid) % b != 0) {

        s++;
      } else {

        if (Ex >= ans) {
          s = mid + 1;
          ans = Ex;
        } else {
          e = mid - 1;
        }
      }
    }

    // ll Ans =0;
    // for(ll i=0;i<=n;i++)
    //{
    // x =i;
    // ll Ex = (a*x/b);
    // ll p = x/b;
    // Ex = Ex - (a*p) ;
    // cout<<Ex<<endl;
    // Ans = max(Ans,Ex);
    //}

    // cout<<"Case #"<<CaseN<<": ";
    CaseN++;
    // print your ans below;
    cout << ans << endl;
  }

  return 0;
}

// if(ans)
//{ cout<<"YES"<<endl;  }
// else
//{cout<<"NO"<<endl;}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>                 // below comment for pbds
#include <ext/pb_ds/assoc_container.hpp> // to use it make ordered_set
#include <ext/pb_ds/tree_policy.hpp> // all set fnct + = 1. order_of_key(x); // number of
#define ll long long                 // element strictly smaller than it;
#define ld                                                                     \\
  long double // 2. find_by_order(x),pointer el at x pos after sort  use as
              // s.f();
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.flush();
  ll t = 1;
  ll CaseN = 1;
  // cin>>t;
  while (t--) {
    ll a, b, n;
    ll ans = 0;
    cin >> a >> b >> n;
    ll x;
    // ll k = (a*n)/b;

    // ll x = b*k/a ;
    //  bins=ary search in n
    // ax = bk ;      k - k/a;
    ll s = 0;
    ll e = n; // x range

    while (s <= e) {
      // cout<<"--"<<endl;
      ll mid = s + (e - s) / 2;
      ll Ex = (a * mid / b);
      ll p = (mid / b);
      Ex = Ex - (a * p);
      // ans = max(Ex,ans);
      if (mid % b == 0) // && (a*x)%b!=0;
      {

        s++;
      } else {

        if (Ex >= ans) {
          s = mid + 1;
          ans = Ex;
        } else {
          e = mid - 1;
        }
      }
    }

    // ll Ans =0;
    // for(ll i=0;i<=n;i++)
    //{
    // x =i;
    // ll Ex = (a*x/b);
    // ll p = x/b;
    // Ex = Ex - (a*p) ;
    // cout<<Ex<<endl;
    // Ans = max(Ans,Ex);
    //}

    // cout<<"Case #"<<CaseN<<": ";
    CaseN++;
    // print your ans below;
    cout << ans << endl;
  }

  return 0;
}

// if(ans)
//{ cout<<"YES"<<endl;  }
// else
//{cout<<"NO"<<endl;}