【题目链接】

点击打开链接

【算法】

不难看出,ans = phi(2) + phi(3) + .... + phi(n-1) + phi(n)

线性筛筛出欧拉函数,预处理前缀和,即可

【代码】

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
typedef long long ll; const ll MAXN = 1e6; ll i,n,tmp,j,tot;
ll prime[MAXN+10],phi[MAXN+10],sum[MAXN+10],f[MAXN+10]; template <typename T> inline void read(T &x) {
ll f = 1; x = 0;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
x *= f;
}
template <typename T> inline void write(T x) {
if (x < 0) { x = -x; putchar('-'); }
if (x > 9) write(x/10);
putchar(x%10+'0');
}
template <typename T> inline void writeln(T x) {
write(x);
puts("");
} int main() { for (i = 2; i <= MAXN; i++) {
if (!f[i]) {
prime[++tot] = f[i] = i;
phi[i] = i - 1;
}
for (j = 1; j <= tot; j++) {
tmp = i * prime[j];
if (tmp > MAXN) break;
f[tmp] = prime[j];
if (prime[j] == f[i]) {
phi[tmp] = prime[j] * phi[i];
break;
}
phi[tmp] = phi[i] * phi[prime[j]];
}
}
for (i = 2; i <= MAXN; i++) sum[i] = sum[i-1] + phi[i]; while (true) {
read(n);
if (!n) return 0;
writeln(sum[n]);
} return 0;
}
05-26 09:02