Pagini recente » Cod sursa (job #2243558) | Cod sursa (job #3247425) | Cod sursa (job #2868310) | Cod sursa (job #1323668) | Cod sursa (job #509114)
Cod sursa(job #509114)
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <map>
#include <set>
#include <bitset>
using namespace std;
set<int> doPrimes(const int n) {
set<int> p;
bitset < 1000001 > bs;
for (int i = 2; i <= n; i++) {
if (!bs[i]) {
bs[i] = true;
int j = 2 * i;
while (j < n + 1) {
bs[j] = true;
j += i;
}
p.insert(i);
// cout << i << " ";
}
}
return p;
}
int totient(set<int> primes, int n) {
bitset < 1000001 > bs;
int m = n;
set<int>::iterator it = primes.begin();
int t = 1;
while (it != primes.end()) {
if (*it > m) break;
if (m % (*it) == 0) {
if (!bs[*it]) {
bs[*it] = true;
t *= (*it) - 1;
} else t *= (*it);
m /= (*it);
continue;
}
it++;
}
if (m != 1) {
if (!bs[m]) t*=(m-1);
else t*=m;
}
return t;
}
int main() {
ifstream in("fractii.in");
ofstream out("fractii.out");
int n;
in >> n;
set<int> primes = doPrimes(n);
int t = 1;
for (int i = 2; i <= n; i++) {
// for (int i = 2; i <= 10; i++) {
int tot = totient(primes, i);
t += tot * 2;
// cout << i << "->" << tot << " ," << t << endl;
}
cout << t;
out << t;
return 0;
}