luogu2158 [SDOI2008]仪仗队 欧拉函数

点 $ (i,j) $ 会看不见当有 $ k|i $ 且 $ k|j$ 时。
然后就成了求欧拉函数了。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int n, phi[40005], pri[40005], cnt;
bool isp[40005];
long long ans=0;
void shai(){
	memset(isp, true, sizeof(isp));
	isp[0] = isp[1] = false;
	for(int i=2; i<=n; i++){
		if(isp[i])	pri[++cnt] = i, phi[i] = i - 1;
		for(int j=1; j<=cnt; j++){
			if(i*pri[j]>n)	break;
			isp[i*pri[j]] = false;
			if(i%pri[j]==0){
				phi[i*pri[j]] = phi[i] * pri[j];
				break;
			}
			else	phi[i*pri[j]] = phi[i] * (pri[j] - 1);
		}
	}
}
int main(){
	cin>>n;
	shai();
	for(int i=2; i<=n-1; i++)	ans += phi[i];
	ans = ans * 2 + 3;
	cout<<ans<<endl;
	return 0;
}
原文地址:https://www.cnblogs.com/poorpool/p/8067483.html