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

#include <bits/stdc++.h>
using namespace std;

/// File input & output /////
#define fRead(x) freopen("x.txt", "r", stdin)
#define fWrite(x) freopen("x.txt", "w", stdout)

#define mt make_tuple
#define ld long double
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define INF 2e16
#define PI acos(-1.0)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<LL, LL>

#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define min4(a, b, c, d) min(a, min(b, min(c, d)))
#define max4(a, b, c, d) max(a, max(b, max(c, d)))
#define SQR(a) ((a) * (a))
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, b) for (int i = 0; i < b; i++)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ABS(x) ((x) < 0 ? -(x) : (x))

#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())

#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(nullptr);

///..................Sort by second element in a pair in decending
///order............. ///
bool SortbySecDesc(const pair<long long, long long> &a,
                   const pair<long long, long long> &b) {
  return a.second > b.second;
}

///.........................first element of pair in descending
///order..........................//////
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.first > b.first);
}

///.............................................................TEMPLATE................................................./////

ll printNcR(int n, int r) {

  // p holds the value of n*(n-1)*(n-2)...,
  // k holds the value of r*(r-1)...

  long long p = 1, k = 1;

  if (n - r < 0)
    return 0;

  // C(n, r) == C(n, n-r),
  // choosing the smaller value
  if (n - r < r)
    r = n - r;

  if (r != 0) {
    while (r) {
      p *= n;
      k *= r;

      // gcd of p, k
      long long m = __gcd(p, k);

      // dividing by gcd, to simplify product
      // division by their gcd saves from the overflow
      p /= m;
      k /= m;

      n--;
      r--;
    }

    // k should be simplified to 1
    // as C(n, r) is a natural number
    // (denominator should be 1 ) .
  }

  else
    p = 1;

  // if our approach is correct p = ans and k =1
  return p;
}

int main() {
  FASTIO;

  ll n, m;

  cin >> n >> m;

  cout << printNcR(n, 2) + printNcR(m, 2) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

/// File input & output /////
#define fRead(x) freopen("x.txt", "r", stdin)
#define fWrite(x) freopen("x.txt", "w", stdout)

#define mt make_tuple
#define ld long double
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define INF 2e16
#define PI acos(-1.0)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<LL, LL>

#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define min4(a, b, c, d) min(a, min(b, min(c, d)))
#define max4(a, b, c, d) max(a, max(b, max(c, d)))
#define SQR(a) ((a) * (a))
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define ROF(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, b) for (int i = 0; i < b; i++)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ABS(x) ((x) < 0 ? -(x) : (x))

#define SORT(v) sort(v.begin(), v.end())
#define REV(v) reverse(v.begin(), v.end())

#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(nullptr);

///..................Sort by second element in a pair in decending
///order............. ///
bool SortbySecDesc(const pair<long long, long long> &a,
                   const pair<long long, long long> &b) {
  return a.second > b.second;
}

///.........................first element of pair in descending
///order..........................//////
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.first > b.first);
}

///.............................................................TEMPLATE................................................./////

ll printNcR(int n, int r) {

  // p holds the value of n*(n-1)*(n-2)...,
  // k holds the value of r*(r-1)...
  long long p = 1, k = 1;

  // C(n, r) == C(n, n-r),
  // choosing the smaller value
  if (n - r < r)
    r = n - r;

  if (r != 0) {
    while (r) {
      p *= n;
      k *= r;

      // gcd of p, k
      long long m = __gcd(p, k);

      // dividing by gcd, to simplify product
      // division by their gcd saves from the overflow
      p /= m;
      k /= m;

      n--;
      r--;
    }

    // k should be simplified to 1
    // as C(n, r) is a natural number
    // (denominator should be 1 ) .
  }

  else
    p = 1;

  // if our approach is correct p = ans and k =1
  return p;
}

int main() {
  FASTIO;

  ll n, m;

  cin >> n >> m;

  cout << printNcR(n, 2) + printNcR(m, 2) << endl;

  return 0;
}