Cod sursa(job #109901)

Utilizator peanutzAndrei Homorodean peanutz Data 25 noiembrie 2007 12:55:11
Problema Pairs Scor 0
Compilator cpp Status done
Runda preONI 2008, Runda 1, Clasele 11-12 Marime 1.99 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()

#define Dim 500000
char buf[Dim+1];
int poz;
#define cit(x)						\
{							\
	x = 0;						\
	while(buf[poz] < '0' || buf[poz] > '9')		\
		if(++poz == Dim)			\
			fread(buf,1,Dim,stdin), poz = 0;		\
	while(buf[poz] >= '0' && buf[poz] <= '9')	\
	{						\
		x = x*10 + buf[poz] - '0';		\
    	if(++poz == Dim)			\
			fread(buf,1,Dim,stdin), poz = 0;		\
	}						\
}	

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);
    
    fread(buf, 1, Dim, stdin);
    cit(n);
    for(int i = 1; i <= n; ++i)
        cit(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;
}