Cod sursa(job #109846)

Utilizator peanutzAndrei Homorodean peanutz Data 25 noiembrie 2007 12:49:48
Problema Pairs Scor 0
Compilator cpp Status done
Runda preONI 2008, Runda 1, Clasele 11-12 Marime 1.6 kb
#include <stdio.h>
#include <math.h>
#include <vector>
#include <string.h>

using namespace std;

#define NMAX 100005
#define SQR 1010
#define pb push_back
#define sz size()

int n;
int v[NMAX];
int res;
vector<int> list[NMAX];
vector<int> p;
vector<int> _div[NMAX];
int uz[NMAX];

short prim(int x)
{
    int d = 2, until = (int)sqrt(x)+10;
    while(d <= until && d < x)
    {
        if(!(x % d))
            return 0;
        ++d;
    }
    return 1;
}

void find()
{
    for(int i = 2; i < SQR; ++i)
        if(prim(i))
            p.pb(i);
}

int main()
{
    freopen("pairs.in", "r", stdin);
    freopen("pairs.out", "w", stdout);
    
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &v[i]);
        
    find();

    for(int i = 1, j, rad, until = p.sz; i <= n; ++i)
        for(j = 0; j < until && p[j] <= v[i]; ++j)
            if(!(v[i]%p[j]))
                list[j].pb(i), _div[i].pb(j);
                
    vector<int> :: iterator it;
    for(int i = 1, j, until, aux; i <= n; ++i)
    {
            aux = n-i;
            for(j = 0, until = _div[i].sz; j < until; ++j)
                for(it = list[_div[i][j]].begin(); it != list[_div[i][j]].end(); ++it)
                    if(uz[*it] != i && *it > i)
                        uz[*it] = i, --aux;
            res += aux;
            
//            printf("%d: ", v[i]);
//            for(j = 1; j <= n; ++j)
//                if(!uz[j] && j > i)
//                    printf("%d ", v[j]);
//            printf("\n");
    }

    printf("%d\n", res);
    return 0;
}