Pagini recente » Cod sursa (job #1956982) | Cod sursa (job #1308483) | Cod sursa (job #1871308) | Cod sursa (job #2946034) | Cod sursa (job #55838)
Cod sursa(job #55838)
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
ifstream in("fractii.in");
ofstream out("fractii.out");
int n;
int pr[78500];
double rapprim[78500];
int cntprime = 1;
void prime()
{
char a[1000000] = {0};
int p = sqrt(n)+1;
pr[0] = 2;
//rapprim[0]=(double)1/(double)pr[0];
for ( int i = 3; i <= n; i += 2 )
{
if ( a[i] == 0 )
{
pr[cntprime] = i;
//rapprim[cntprime]=(double)1/(double)pr[cntprime];
cntprime++;
if ( i <= p )
{
for ( int j = (i<<1); j <= n; j += i )
a[j] = 1;
}
}
}
}
int totv[1000000] = {0, 1, 1, 2, 2};
int tot(int x)
{
int p = x;
int i = 0;
int cnt = 0;
int gasit = 0;
if ( totv[x] != 0 )
return totv[x];
while ( pr[i] < p )
{
if ( p % pr[i] == 0 )
{
gasit = 1;
break;
}
++i;
}
if ( !gasit )
{
totv[x] = x-1;
return totv[x];
}
while ( p % pr[i] == 0 )
{
p /= pr[i];
++cnt;
}
if ( p == 1 )
totv[x] = (pr[i]-1)*(x/pr[i]);
else
{
//int g = x / (int)pow(pr[i], cnt);
//int g = p;
totv[x] = (pr[i]-1)*(x/(pr[i]*p))*totv[p];
}
return totv[x];
}
int totcorina(int nr)
{
double t=nr;
int i = 0;
while ( i<cntprime && pr[i] < nr )
{
if ( nr % pr[i] == 0 )
{
t=t*(1-rapprim[i]);
}
++i;
}
if (pr[i]==nr)
t=t*(1-rapprim[i]);
return (int)t;
}
int main()
{
// 11 -> 83
// 15 -> 143
// 31 -> 615
// 99 -> 6007
// 100 -> 6087
// 1000000 -> 607927104783
//in >> n;
n=1000000;
prime();
long long sol = 0;
for ( int j = 2; j <= n; ++j )
{
sol += tot(j);
// sol+=totcorina(j);
}
out << (1+2*sol) << endl;
cout << (1+2*sol) << endl;
return 0;
}