Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define endl "\n"
#define all(x) x.begin(), x.end()
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define vc vector<char>
#define vii vector<pii>
#define vvi vector<vi>
#define fori(i, n) for (int i = 1; i <= n; ++i)
#define forn(i, n) for (int i = 0; i < n; ++i)
#define sz(x) int(x.size())
#define error(x) cout << #x << " " << x << endl
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)

const int N = 100 * 1000;
const int mod = 1e9 + 7;

int min3(int a, int b, int c) { return min(a, min(b, c)); }
int max3(int a, int b, int c) { return max(a, max(b, c)); }
int min4(int a, int b, int c, int d) { return min(min(a, b), min(c, d)); }
int max4(int a, int b, int c, int d) { return max(max(a, b), max(c, d)); }

#define int int64_t
// int a[N+2];

int32_t main() {
  IOS;
  int n;
  cin >> n;
  int ans = 0;
  for (int i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      if (n / i != i) {
        // different divisor
        { // for i

          int j = i - 1;
          // cout<<j<<" j\n";
          if ((n / j) == (n % j))
            ans += j;
        }
        { // i=n/i
          int j = n / i;
          j--;
          // cout<<j<<" j\n";
          if ((n / j) == (n % j))
            ans += j;
        }

      } else {
        // same divisor
        int j = i - 1;
        // cout<<j<<" j\n";
        if ((n / j) == (n % j))
          ans += j;
      }
    }
  }
  if ((n / (n - 1)) == (n % (n - 1)))
    ans += n - 1;
  cout << ans << endl;
  return 0;
}


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

#define endl "\n"
#define all(x) x.begin(), x.end()
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define vc vector<char>
#define vii vector<pii>
#define vvi vector<vi>
#define fori(i, n) for (int i = 1; i <= n; ++i)
#define forn(i, n) for (int i = 0; i < n; ++i)
#define sz(x) int(x.size())
#define error(x) cout << #x << " " << x << endl
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0)

const int N = 100 * 1000;
const int mod = 1e9 + 7;

int min3(int a, int b, int c) { return min(a, min(b, c)); }
int max3(int a, int b, int c) { return max(a, max(b, c)); }
int min4(int a, int b, int c, int d) { return min(min(a, b), min(c, d)); }
int max4(int a, int b, int c, int d) { return max(max(a, b), max(c, d)); }

#define int int64_t
// int a[N+2];

int32_t main() {
  IOS;
  int n;
  cin >> n;
  int ans = 0;
  if (n == 1) {
    cout << "0\n";
    return 0;
  }
  for (int i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      if (n / i != i) {
        // different divisor
        { // for i

          int j = i - 1;
          // cout<<j<<" j\n";
          if ((n / j) == (n % j))
            ans += j;
        }
        { // i=n/i
          int j = n / i;
          j--;
          // cout<<j<<" j\n";
          if ((n / j) == (n % j))
            ans += j;
        }

      } else {
        // same divisor
        int j = i - 1;
        // cout<<j<<" j\n";
        if ((n / j) == (n % j))
          ans += j;
      }
    }
  }
  if ((n / (n - 1)) == (n % (n - 1)))
    ans += n - 1;
  cout << ans << endl;
  return 0;
}