Problem p02729 (C++)

Buggy submission (status: Time Limit Exceeded):
//??????????SM??????????

//  -------------Sourav Mondal----------
//        _____________________________________"GUN IS ON FIRE~SM"
//..._|.._|___________________, ,) ----)           "KILL YOUR headache Shoot iT"
//....../ `---___________----_____|]_____)     ********* 2eZ4@SM
//...../_==o;;;;;;;;_______.:/
//.....), ---.(_(__) /                                   ask2sm*********
//....// (.||_.) ), ----"
//...//___//
//../|___/|
//.||___||
//----------------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
#define llt long long int
#define pb(x) push_back(x)

int 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;

  return p;
}

int main() {
  int n, m;
  cin >> n >> m;
  if (n < 2 && m >= 2) {
    cout << printNcR(m, 2) << endl;
    return 0;
  } else if (m < 2 && n >= 2) {
    cout << printNcR(n, 2) << endl;
    return 0;
  }
  cout << printNcR(n, 2) + printNcR(m, 2) << endl;
  int count = 0;
}

Fixed submission:
//??????????SM??????????

//  -------------Sourav Mondal----------
//        _____________________________________"GUN IS ON FIRE~SM"
//..._|.._|___________________, ,) ----)           "KILL YOUR headache Shoot iT"
//....../ `---___________----_____|]_____)     ********* 2eZ4@SM
//...../_==o;;;;;;;;_______.:/
//.....), ---.(_(__) /                                   ask2sm*********
//....// (.||_.) ), ----"
//...//___//
//../|___/|
//.||___||
//----------------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
#define llt long long int
#define pb(x) push_back(x)

int 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;

  return p;
}

int main() {
  int n, m;
  cin >> n >> m;
  if (n == 1 && m == 1) {
    cout << 0 << endl;
    return 0;
  }
  if (n < 2 && m >= 2) {
    cout << printNcR(m, 2) << endl;
    return 0;
  } else if (m < 2 && n >= 2) {
    cout << printNcR(n, 2) << endl;
    return 0;
  }
  cout << printNcR(n, 2) + printNcR(m, 2) << endl;
  int count = 0;
}