Cod sursa(job #342355)

Utilizator Alexa_ioana_14Antoche Ioana Alexandra Alexa_ioana_14 Data 21 august 2009 13:27:00
Problema Sum Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include<cstdio>
#define N 100001
bool b[N];
char a[N*2][18];
struct sum{bool b; long long v;}b1[N];
void ciur()
{
	int d=2,n=N*2;
	while (d*d<=n)
	{
		if (!b[d])
		{
			for (int i=d+d; i*i<=n; i+=d)
			{
				a[i][++a[i][0]]=d;
				b[i]=true;
			}
			a[d][++a[d][0]]=d;
		}
		++d;
	}
}
void afis()
{
	for (int i=1; i<=12; ++i)
	{
		for (int j=1; j<=a[i][0]; ++j)
			printf("%hd ",a[i][j]);
		printf("\n");
	}
}
bool verific(int i,int x)
{
	for (int j=1; j<=a[x][0]; ++j)
		if (i%a[x][j]==0)
			return false;
	return true;
}
void calcul(int x)
{
	int g=2*x;
	long long s=1;
	for (int i=2; i<g; ++i)
		if (verific(i,x)&&i!=x)
			s+=i;
	b1[x].b=true;
	b1[x].v=s;
	printf("%lld\n",s);
}
void citire()
{
	freopen("sum.in","r",stdin);
	freopen("sum.out","w",stdout);
	int n;
	scanf("%d",&n);
	int x;
	while(n)
	{
		scanf("%d",&x);
		if (b1[x].b)
			printf("lld",b1[x].v);
		else
			if (!b[x])
				printf("%lld",((x*2)*(x*2+1)/2-x-2*x));
			else
				calcul(x);
		--n;
	}
}
int main()
{
	ciur();
	citire();
	//afis();
	return 0;
}