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

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;
// for loop
#define F(i, L, R) for (long i = L; i < R; i++)
#define FE(i, L, R) for (long i = L; i <= R; i++)
// for (reversed)
#define FR(i, L, R) for (long i = L; i > R; i--)
#define FRE(i, L, R) for (long i = L; i >= R; i--)

// data types
#define Int long
#define ul unsigned long
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned shoft

// vectors
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> ii;
#define ALL(c) (c).begin(), (c).end()
#define last(vec) vec.size() - 1
#define lastEle(vec) vec[last(vec)]
#define pb push_back
#define InputV(a, n)                                                           \\
  F(i, 0, n) {                                                                 \\
    long l;                                                                    \\
    cin >> l;                                                                  \\
    a.pb(l);                                                                   \\
  }

// debug
#define DBG(vari) cerr << #vari << " = " << (vari) << endl;
#define printA(a, L, R) FE(i, L, R) cout << a[i] << (i == R ? '\\n' : ' ')
#define printAF(a, L) printA(a, 0, L - 1)
#define printV(a) printA(a, 0, a.size() - 1)

// utils
#define char2int(c) (c - '0')
#define bit(x, i) (x & (1 << i))
#define space ' '
#define PI 3.14159265358979323

void printM(vector<vector<long>> &vec, long height, long width) {
  F(i, 0, height) {
    F(j, 0, width) { cout << vec[i][j] << space; }
    cout << endl;
  }
}

ll flo(ull a, ull b, ull x) {
  ull first = a * x;
  first = first / b;
  ull second = x / b;
  second = second * a;
  ll hoge = first - second;
  return hoge;
}

#define TST 0
int main() {
#if TST == 1
  ifstream in("input.txt");
  cin.rdbuf(in.rdbuf());
#endif
  ull a, b, n;
  cin >> a >> b >> n;
  ll ans = -1 * a - 1;
  ull first;
  ull second;
  ull x;
  if (n >= b) {
    ull mo = n % b;
    x = n - mo - 1;
    ans = max(ans, flo(a, b, x));
    // long l = x % a;
    // for(long i = x; i > 0 && i % a != l;i = i - b ){
    //     ans = max(ans,flo(a, b, i) );
    // }
  }
  // while(b*k -1 < n){
  //     //x = b*k - 1;
  //     //first =  a*x/b;
  //     //second = x/b;
  //     //second = second * a;
  //     //ans = max(ans, first-second);
  //     k++;
  // }
  // if(k > 1){
  // x = b*(k-1) -1;
  // first = a*x/b;
  // second = x/b;
  // second = second * a;
  // ans = max(ans, first- second);
  // }
  ans = max(ans, flo(a, b, n));
  cout << ans << endl;

#if TST == 1

#endif
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;
// for loop
#define F(i, L, R) for (long i = L; i < R; i++)
#define FE(i, L, R) for (long i = L; i <= R; i++)
// for (reversed)
#define FR(i, L, R) for (long i = L; i > R; i--)
#define FRE(i, L, R) for (long i = L; i >= R; i--)

// data types
#define Int long
#define ul unsigned long
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned shoft

// vectors
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> ii;
#define ALL(c) (c).begin(), (c).end()
#define last(vec) vec.size() - 1
#define lastEle(vec) vec[last(vec)]
#define pb push_back
#define InputV(a, n)                                                           \\
  F(i, 0, n) {                                                                 \\
    long l;                                                                    \\
    cin >> l;                                                                  \\
    a.pb(l);                                                                   \\
  }

// debug
#define DBG(vari) cerr << #vari << " = " << (vari) << endl;
#define printA(a, L, R) FE(i, L, R) cout << a[i] << (i == R ? '\\n' : ' ')
#define printAF(a, L) printA(a, 0, L - 1)
#define printV(a) printA(a, 0, a.size() - 1)

// utils
#define char2int(c) (c - '0')
#define bit(x, i) (x & (1 << i))
#define space ' '
#define PI 3.14159265358979323

void printM(vector<vector<long>> &vec, long height, long width) {
  F(i, 0, height) {
    F(j, 0, width) { cout << vec[i][j] << space; }
    cout << endl;
  }
}

#define TST 0
int main() {
#if TST == 1
  ifstream in("input.txt");
  cin.rdbuf(in.rdbuf());
#endif
  ull a, b, n;
  cin >> a >> b >> n;
  long k = 1;
  ull ans = 0;
  ull first;
  ull second;
  ull x;
  while (b * k - 1 < n) {
    // x = b*k - 1;
    // first =  a*x/b;
    // second = x/b;
    // second = second * a;
    // ans = max(ans, first-second);
    k++;
  }
  if (k > 1) {
    x = b * (k - 1) - 1;
    first = a * x / b;
    second = x / b;
    second = second * a;
    ans = max(ans, first - second);
  }
  x = n;
  first = a * x / b;
  second = x / b;
  second = second * a;
  ans = max(ans, first - second);
  cout << ans << endl;

#if TST == 1

#endif
}