Cod sursa(job #37088)

Utilizator RobytzzaIonescu Robert Marius Robytzza Data 24 martie 2007 16:42:55
Problema Fractii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.6 kb
#include<fstream.h>
int main(){
ifstream fin("fractii.in");
ofstream fout("fractii.out");
long int N;
fin>>N;
long int x;
short ok=1;
long long nr=N*N;
for (long i=2;i<=N;i++){
	  x=i;
	  ok=1;
	  if (x%2==0){
	     nr-=N-N/2;
	     ok=0;}
      while (x%2==0){
	     x/=2;}
      for (long int d=3;d<=x;d+=2){
       if (ok==1){
	   if (x%d==0){
	       nr-=N/d;
	       ok=0;
	   while (x%d==0)
	       x/=d;   }}
	  else {
	      int S=0;
	      if (x%d==0){
	     while (x%d==0){
		   x/=d;
		   S--;}
	       nr-=(N/d+S);}} } }
fout<<nr;
fin.close();
fout.close();
return 0;
}