Problem23

#include <iostream>
#include <vector>
#include <set>
#include <cmath>
using namespace std;

#define LIMIT 28123

void
aliquot(int n, set<int> *pset_pf)
{
    pset_pf->insert(1);
    if(n == 2)
    {
        return;
    }

    bool *t = new bool [n];
    memset(t, false, n);
    t[1] = true;

    for (int i = 2; i < n; i++)
    {
        if (n%i == 0 && !t[i])
        {
            t[i] = true;
            t[n/i] = true;
        }
        if (t[i])
        {
            pset_pf->insert(i);
            pset_pf->insert(n/i);
        }
    }

    delete [] t;
}

bool
isAbundant(int n)
{
    set<int> set_pf;
    aliquot(n, &set_pf);

    int sum = 0;
    for (set<int>::iterator it = set_pf.begin(), end = set_pf.end(); it != end; it++)
    {
        sum += *it;
    }
    
    return sum > n ? true : false;
}

int
main(int argc, char ** argv)
{
    int ans = 0;
    bool t_is[LIMIT];
    bool t_sum[LIMIT];
    memset(t_is, false, LIMIT);
    memset(t_sum, false, LIMIT);

    for (int n = 2; n < LIMIT; n++)
    {
        if (isAbundant(n))
        {
            t_is[n] = true;
        }
    }

    for (int i = 0; i < LIMIT; i++)
    {
        if (t_is[i])
        {
            for (int j = i; j+i < LIMIT; j++)
            {
                if (t_is[j])
                {
                    t_sum[i+j] = true;
                }
            }
        }
    }

    for(int i = 0; i < LIMIT; i++)
    {
        if (!t_sum[i])
        {
            ans += i;
        }
    }
    cout << ans << endl;

    return 0;
}