# BZOJ 2818: Gcd( 欧拉函数 )

-------------------------------------------------------------------

#include<bits/stdc++.h>

#define clr(x, c) memset(x, c, sizeof(x))
#define rep(i, n) for(int i = 0; i < n; ++i)
#define foreach(i, x) for(__typeof(x.begin()) i = x.begin(); i != x.end(); i++)

using namespace std;

typedef long long ll;

const int maxn = 10000009;

int prime[maxn], N = 0, n;
ll phi[maxn], ans = 0;
bool check[maxn];

void init() {
clr(check, 0);
phi[1] = 1;
for(int i = 2; i <= n; i++) {
if(!check[i]) {
prime[N++] = i;
phi[i] = i - 1;
}
for(int j = 0; j < N && prime[j] * i <= n; j++) {
check[prime[j] * i] = true;
if(i % prime[j])
phi[i * prime[j]] = phi[i] * (prime[j] - 1);
else {
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
}
}
}

int main() {
freopen("test.in", "r", stdin);
cin >> n;
init();
for(int i = 2; i <= n; i++)
phi[i] += phi[i - 1];
rep(i, N) ans += phi[n / prime[i]] * 2 - 1;
cout << ans << "\n";
return 0;
}

-------------------------------------------------------------------

## 2818: Gcd

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 2450  Solved: 1086
[Submit][Status][Discuss]

4

4

hint

1<=N<=10^7

## Source

posted @ 2015-07-28 12:39  JSZX11556  阅读(124)  评论(0编辑  收藏