Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#define DivertaPC2019_D
#ifdef DivertaPC2019_D

#pragma comment(linker, "/STACK:256000000")
#include "bits/stdc++.h"
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a), i##_len = (b); i < i##_len; ++i)
#define BUGAVOID(x) x
#define rep(...)                                                               \
  BUGAVOID(_overload3(__VA_ARGS__, repi, _rep, _rep)(__VA_ARGS__))
#define all(c) c.begin(), c.end()
#define mp make_pair
#define write(x) cout << (x) << "\n"
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
template <class T> using vv = vector<vector<T>>;
template <class T> vv<T> vvec(size_t n, size_t m, T v) {
  return vv<T>(n, vector<T>(m, v));
}
template <class T> bool chmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template <class T> bool chmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
constexpr int INF = 1 << 28, MOD = 1e9 + 7;
constexpr ll LINF = 1ll << 60;
constexpr double EPS = 1e-6;
struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  };
} aaaa;

ll N;
ll ans = 0;

int main() {
  cin >> N;

  if (N == 2) {
    write(1);
    return 0;
  }

  ll sqrtN = sqrt(N);
  if ((sqrtN + 1) * (sqrtN + 1) == N) {
    sqrtN++;
  } else if ((sqrtN - 1) * (sqrtN - 1) == N) {
    sqrtN--;
  }

  ll lim = sqrtN + 1;
  for (ll k = 1; k < lim; ++k) {
    ll n = N - k;
    if (n % k == 0) {
      ll m = n / k;
      if (k % m != 0) {
        if (N % m == k && N / m == k) {
          ans += m;
        }
      }
    }
  }

  write(ans);
}

#endif

Fixed submission:
#define DivertaPC2019_D
#ifdef DivertaPC2019_D

#pragma comment(linker, "/STACK:256000000")
#include "bits/stdc++.h"
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a), i##_len = (b); i < i##_len; ++i)
#define BUGAVOID(x) x
#define rep(...)                                                               \
  BUGAVOID(_overload3(__VA_ARGS__, repi, _rep, _rep)(__VA_ARGS__))
#define all(c) c.begin(), c.end()
#define mp make_pair
#define write(x) cout << (x) << "\n"
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
template <class T> using vv = vector<vector<T>>;
template <class T> vv<T> vvec(size_t n, size_t m, T v) {
  return vv<T>(n, vector<T>(m, v));
}
template <class T> bool chmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template <class T> bool chmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
constexpr int INF = 1 << 28, MOD = 1e9 + 7;
constexpr ll LINF = 1ll << 60;
constexpr double EPS = 1e-6;
struct aaa {
  aaa() {
    cin.tie(0);
    ios::sync_with_stdio(0);
  };
} aaaa;

ll N;
ll ans = 0;

int main() {
  cin >> N;

  if (N == 1) {
    write(0);
    return 0;
  } else if (N == 2) {
    write(0);
    return 0;
  }

  ll sqrtN = sqrt(N);
  if ((sqrtN + 1) * (sqrtN + 1) == N) {
    sqrtN++;
  } else if ((sqrtN - 1) * (sqrtN - 1) == N) {
    sqrtN--;
  }

  ll lim = sqrtN + 1;
  for (ll k = 1; k < lim; ++k) {
    ll n = N - k;
    if (n % k == 0) {
      ll m = n / k;
      if (k % m != 0) {
        if (N % m == k && N / m == k) {
          ans += m;
        }
      }
    }
  }

  write(ans);
}

#endif