Cod sursa(job #1148944)

Utilizator ELHoriaHoria Cretescu ELHoria Data 21 martie 2014 12:35:15
Problema Sum Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

long long solve(const int& x,vector<int>& primes) {
	long long ret = 0;
	int t = x;
	int c;
	vector<int>  factors;
	for (int i = 0; primes[i] * primes[i] <= t; i++) {
		if (t % primes[i] == 0) {
			t /= primes[i];
			while (t > 1 && t - (c = t / primes[i]) * primes[i] == 0) {
				t = c;
			}
			factors.push_back(primes[i]);
		}
	}

	if (t > 1) factors.push_back(t);

	for (int i = 1; i < (int)(1 << factors.size()); i++) {
		int value = 1;
		int sgn = 1;
		for (int j = 0; j < (int)factors.size(); j++) {
			if ((i >> j) & 1) {
				value *= factors[j];
				sgn *= -1;
			}
		}

		int c = 2 * x / value;
		ret += 1LL * c * (c + 1) / 2 * value * sgn;
	}

	ret =  1LL * x * (2 * x + 1)  + ret;
	return ret;
}

inline bool prime(const int& x) {
	if (x == 2) return 1;
	if (!(x & 1)) return 0; 
	if (x == 1) return 0;
	for (int i = 3; i * i <= x; ++i) {
		if (x % i == 0)
			return 0;
	}

	return 1;
}

int main()
{	
	ifstream cin("sum.in");
	ofstream cout("sum.out");
	vector<int> primes;
	const int Xmax = int(1e5) + int(1e4);
	for (int i = 1; i * i <= Xmax; i++) {
		if (prime(i)) {
			primes.push_back(i);
		}
	}

	int testCount;
	cin >> testCount;
	while (testCount--) {
		int x;
		cin >> x;
		cout << solve(x, primes) << "\n";
	}
	return 0;
}