Cod sursa(job #349)

Utilizator ZweisteinAdrian VELICU Zweistein Data 11 decembrie 2006 08:04:23
Problema Fractii Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.88 kb
#include <stdio.h>
#include <memory.h>
#include <math.h>
#define PRCOUNT 1000000
#define PRCOUNT2 100
#define PRCOUNTCOUNT 80000
#define PRCOUNTCOUNT2 800
long phi[PRCOUNT];
long long prime[PRCOUNTCOUNT];
long long primes=0;
char ciur[PRCOUNT];	
long long tot (long long x) {
     long long temp,put,temp2,i,indices;
     if (x==1) { return 1; }
     else if (ciur[x]) { phi[x]=x-1;}
     else {
          temp=0;
	   for (i=1; (prime[i]<=x) && (temp==0); i++) {
                     if (x%prime[i]==0) { temp=i; };
	       };
	   temp2=x;
           put=0;
           while (temp2%prime[temp]==0) {
                  temp2/=prime[temp];
                  put++;
		 };
		 indices=(long long)(powl(prime[temp],put));
		 if (phi[indices]==0) phi[indices]=(prime[temp]-1)*(long long)(powl(prime[temp],put-1));
		 phi[x]=phi[indices]*tot(temp2);
//		 if (temp2!=1) phi[x]*=tot(temp2);
          };
     return phi[x];
};
long long mal_tot (long long x) {
     long long i,temp,put;
     if (phi[x]!=0) return phi[x];
		for (i=1; (prime[i]<=x); i++) {
			if (x%prime[i]==0) {
                temp=x;
                put=0;
                while (temp%prime[i]==0) { temp=temp/prime[i]; put++;};
                if (temp==0) {
//                              phi[x]=(prime[i]-1)*pow(prime[i],put-1);
                             }
                             else
                             {
//                              phi[x]=(prime[i]-1)*pow(prime[i],put-1)*tot(temp);
                             };
			};
		};
        return phi[x];
};
int main (void) {
	memset(ciur,1,PRCOUNT*sizeof(char));
	memset(phi,0,PRCOUNT*sizeof(int));
	ciur[2]=1;
	long long i; long long k;
	for (i=2; i<=PRCOUNT; i++) {
		if (ciur[i]) {
		       k=i;
		       primes++;
		       prime[primes]=i;
		       while (k<PRCOUNT-i) {
				k+=i;
				ciur[k]=0;
		       };
		};
	};
//	for (i=2; i<=PRCOUNT; i++) {
//		printf("%c",ciur[i]+1);
//	};
//	printf("\n");
	long long n;         long long j;
	FILE * fi = fopen("fractii.in","rt");
	FILE * fo = fopen("fractii.out","wt");
	fscanf(fi,"%lld",&n);
	/*char relc[PRCOUNT];*/ long long sum; long long supersum=0;
	for (i=2; i<=n; i++) {
//        if (i%10000==0) printf("%lld\n",i);
/*		memset(relc,1,PRCOUNT*sizeof(char));
		for (j=2; j<=n; j++) {
			if (ciur[j] && (i%j==0)) {
				k=j;
				relc[k]=0;
				while (k<n) {
					k+=j;
					relc[k]=0;
				};
			};
		};*/
/*		sum=i;
		for (j=1; (prime[j]<=i); j++) {
			if (i%prime[j]==0) {
                temp=i;
                put=0;
                while (temp%prime[j]==0) { temp=temp/prime[j]; put++;};
                if (
                sum=sum-sum/prime[j];
			};
		};*/
//		sum+=tot(i);
		supersum+=tot(i);
//		printf("for %ld the sum is %ld\n",i,long long(sum));
	};
	supersum=supersum*2+1;
	fprintf(fo,"%lld",supersum);
	fclose(fi); fclose(fo);
	return 0;
};