Description
Input
一共T+1行
第1行为数据组数T(T<=10)
第2~T+1行每行一个非负整数N,代表一组询问
Output
一共T行,每行两个用空格分隔的数ans1,ans2
Sample Input
6
1
2
8
13
30
2333
Sample Output
1 1
2 0
22 -2
58 -3
278 -3
1655470 2
题解
#include<stdio.h>
#include<map>
using namespace std;
typedef long long LL;
const int MAXN = 2e6 + 5;
int p[MAXN], prm[MAXN], phi[MAXN], miu[MAXN], SM[MAXN], sz;
LL SP[MAXN];
void init() {
SM[1] = miu[1] = SP[1] = phi[1] = 1;
for(int i = 2; i < MAXN; ++i) {
if(!p[i]) {
phi[i] = i - 1;
miu[i] = -1;
prm[sz++] = i;
}
SP[i] = phi[i] + SP[i - 1];
SM[i] = miu[i] + SM[i - 1];
for(int j = 0 ; j < sz; ++j) {
int t = i * prm[j];
if(t >= MAXN) break;
p[t] = true;
if(i%prm[j]) {
phi[t] = phi[i] * (prm[j] - 1);
miu[t] = -miu[i];
} else {
phi[t] = phi[i] * prm[j];
miu[t] = 0;
break;
}
}
}
}
map<LL, LL> mphi;
inline LL PHI(int n) {
if(n < MAXN) return SP[n];
if(mphi[n]) return mphi[n];
LL res = ((LL)n + 1) * n / 2; int l = 2, r = 2;
while(true) {
res -= PHI(n / r) * (r - l + 1);
if(r == n) break;
l = r + 1;
r = n / (n / l);
}
return mphi[n] = res;
}
map<LL, int> mmiu;
inline int MIU(int n) {
if(n < MAXN) return SM[n];
if(mmiu[n]) return mmiu[n];
int res = 1, l = 2, r = 2;
while(true) {
res -= MIU(n / r) * (r - l + 1);
if(r == n) break;
l = r + 1;
r = n / (n / l);
}
return mmiu[n] = res;
}
int main()
{
init();
int T, n;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
LL ans1 = PHI(n);
int ans2 = MIU(n);
printf("%lld %d\n", ans1, ans2);
}
return 0;
}