Problem p03050 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define TR(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(x, c) memset(x, c, sizeof(x))

using namespace std;

const double eps = 1e-8;

#define PB push_back
#define MP make_pair

typedef map<int, int> MII;
typedef map<string, int> MSI;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<long double> VD;
typedef pair<int, int> PII;
typedef long long int64;
typedef long long LL;
typedef unsigned int UI;
typedef long double LD;
typedef unsigned long long ULL;

int main() {
  LL n;
  cin >> n;

  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }

  set<LL> res;
  for (int i = 1; i <= 2000000 && i <= n; ++i) {
    if ((n - i) % i == 0) {
      res.insert((n - i) / i);
    }
  }

  for (int i = 1; i <= 2000000; ++i) {
    if (n % (i + 1) == 0) {
      LL val = n / (i + 1);
      if (val < n) {
        res.insert(i);
      }
    }
  }

  LL ans = 0;
  TR(it, res) if (n / *it == n % *it) ans += *it;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define TR(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define CLEAR(x) memset(x, 0, sizeof(x))
#define FILL(x, c) memset(x, c, sizeof(x))

using namespace std;

const double eps = 1e-8;

#define PB push_back
#define MP make_pair

typedef map<int, int> MII;
typedef map<string, int> MSI;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<long double> VD;
typedef pair<int, int> PII;
typedef long long int64;
typedef long long LL;
typedef unsigned int UI;
typedef long double LD;
typedef unsigned long long ULL;

int main() {
  LL n;
  cin >> n;

  if (n == 1) {
    cout << 0 << endl;
    return 0;
  }

  set<LL> res;
  for (int i = 1; i <= 2000000 && i < n; ++i) {
    if ((n - i) % i == 0) {
      res.insert((n - i) / i);
    }
  }

  for (int i = 1; i <= 2000000; ++i) {
    if (n % (i + 1) == 0) {
      LL val = n / (i + 1);
      if (val < n) {
        res.insert(i);
      }
    }
  }

  LL ans = 0;
  TR(it, res) if (n / *it == n % *it) ans += *it;
  cout << ans << endl;
  return 0;
}