You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1020 B
46 lines
1020 B
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
|
|
#define int long long
|
|
#define endl "\n"
|
|
const int N = 1e7 + 10; // 这家伙好大啊
|
|
int s[N];
|
|
|
|
// 筛法求欧拉函数值
|
|
int primes[N], cnt;
|
|
bool st[N];
|
|
int phi[N];
|
|
void euler(int n) {
|
|
phi[1] = 1;
|
|
for (int i = 2; i <= n; i++) {
|
|
if (!st[i]) {
|
|
primes[cnt++] = i;
|
|
phi[i] = i - 1;
|
|
}
|
|
for (int j = 0; primes[j] * i <= n; j++) {
|
|
st[primes[j] * i] = true;
|
|
if (i % primes[j] == 0) {
|
|
phi[i * primes[j]] = phi[i] * primes[j];
|
|
break;
|
|
}
|
|
phi[i * primes[j]] = phi[i] * (primes[j] - 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
signed main() {
|
|
int n;
|
|
cin >> n;
|
|
|
|
// 欧拉函数筛法
|
|
euler(n);
|
|
|
|
// 前缀和
|
|
for (int i = 1; i <= n; i++) s[i] = s[i - 1] + phi[i];
|
|
|
|
int res = 0;
|
|
for (int i = 0; i < cnt; i++)
|
|
res += s[n / primes[i]] * 2 - 1; // 推导的公式
|
|
|
|
cout << res << endl;
|
|
} |