Cod sursa(job #1412011)

Utilizator BabutaRaresBabuta Rares Mihai BabutaRares Data 1 aprilie 2015 03:04:48
Problema Fractii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#define _CRT_SECURE_NO_WARNINGS
#include<fstream>
#include<cmath>
#include<climits>
using namespace std;

unsigned long long a[1000001], totient[1000001], produs[1000001],i, j, s, k, n,  e;
unsigned long long sol = 1;

int gcd(int a, int b)
{
	if (b == 0) return a;
	return gcd(b, a%b);
}
int coprimes(int n)
{
	int sol2 = 1;
	for (i = 2; i <= n;i++)
	for (j = 1; j < i;j++)
	if (gcd(i, j) == 1)
		sol2++;
	return sol2*2-1;
}
int main()
{
	int t;
	ifstream f("fractii.in"); ofstream g ("fractii.out");
	f >> n;
	totient[1] = 1;
	for (i = 2; i <= n; i++) {
		produs[i] = i; totient[i] = 1;
	}
		for (i = 2; i <= n; i++)
		{
			if (a[i] == 0)
			{
				totient[i] = i - 1;
				produs[i] = 1;
				for (j = i + i; j <= n; j = j + i)
				{
					totient[j] = totient[j] * totient[i];
					produs[j] = produs[j] / i;
					a[j] = 1;
				}
			}
		}
		for (i = 2; i <= n; i++)
			sol += (totient[i] * produs[i]*2);
			
		g << sol;
			//fprintf(out, "%d", coprimes(n));
	}