不知道为啥会 RE,写的线性筛,数论分块,求挑错。
# include <bits/stdc++.h>
#define int long long
using namespace std;
int N, cnt;
int phi[2000007], pri[2000007];
bool vis[2000007];
int euler_phi () {
phi[1] = 1;
for (int i = 2; i <= 1000000; ++ i) {
if (!vis[i]) {
pri[++ cnt] = i;
phi[i] = i - 1;
}
for (int j = 1; j <= cnt && i * pri[j] <= 1000000; ++ j) {
vis[i * pri[j]] = 1;
if (i % pri[j] == 0) {
phi[i * pri[j]] = pri[j] * phi[i];
break;
}
else
phi[i * pri[j]] = phi[i] * phi[pri[j]];
}
}
for (int i = 1; i <= 1000000; ++ i)
phi[i] += phi[i - 1];
}
signed main () {
euler_phi ();
while (cin >> N) {
if (N == 0) return 0;
long long res = 0;
int l = 1;
while (l <= N) {
int r = N / (N / l);
res += (phi[r] - phi[l - 1]) * (N / l) * (N / l);
l = r + 1;
}
cout << (res - (N + 1) * N / 2) / 2 << "\n";
}
}