----------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1000009;
bool check[maxn];
int prime[maxn], pn = 0, phi[maxn];
ll f[maxn];
void init() {
memset(check, 0, sizeof check);
for(int i = 2; i < maxn; i++) {
if(!check[i]) {
prime[pn++] = i;
f[i] = ll(i) * (phi[i] = i - 1) >> 1;
}
for(int j = 0; j < pn && i * prime[j] < maxn; j++) {
check[i * prime[j]] = 1;
if(i % prime[j]) {
phi[i * prime[j]] = phi[i] * (prime[j] - 1);
f[i * prime[j]] = ll(i) * prime[j] * phi[i * prime[j]] >> 1;
} else {
phi[i * prime[j]] = phi[i] * prime[j];
f[i * prime[j]] = ll(i) * prime[j] * phi[i * prime[j]] >> 1;
break;
}
}
}
}
void work(int n) {
if(n == 1) {
puts("1");
return;
} else if(n == 2) {
puts("4");
return;
} else if(n == 3) {
puts("12");
return;
}
ll ans = 0;
int m = (int)sqrt(n + 0.1);
for(int i = 1; i * i < n; i++) if(n % i == 0)
ans += f[i] + f[n / i];
if(m * m == n) ans += f[m];
printf("%lld\n", ans * n);
}
int main() {
init(); f[1] = 1;
int T; scanf("%d", &T);
while(T--) {
int n; scanf("%d", &n);
work(n);
}
return 0;
}
-----------------------------------------------------------------------------------
2226: [Spoj 5971] LCMSum
Time Limit: 20 Sec Memory Limit: 259 MB Submit: 886 Solved: 404 [ ][ ][ ]Description
Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Least Common Multiple of the integers i and n.
Input
The first line contains T the number of test cases. Each of the next T lines contain an integer n.
Output
Output T lines, one for each test case, containing the required sum.
Sample Input
3
1
2
5
1
2
5
Sample Output
1
4
55
4
55
HINT
Constraints
1 <= T <= 3000001 <= n <= 1000000Source