BZOJ 2005 [Noi2010]能量采集(严格来说应该叫欧拉反演)

练习一发LaTeX
ans=x=1ny=1m2gcd(x,y)1=x=1ny=1m(1+2pgcd(x,y)φ(p))=2x=1ny=1mpgcd(x,y)φ(p)nm=2p=1min(n,m)φ(p)x=1ny=1m[px][py]nm=2p=1min(n,m)φ(p)npmpnm\begin{aligned}ans &= \sum_{x=1}^n\sum_{y=1}^m2gcd(x,y)-1\\ &=\sum_{x=1}^n\sum_{y=1}^m\left(-1+2\sum_{p|gcd(x,y)}\varphi(p)\right)\\ &=2\sum_{x=1}^n\sum_{y=1}^m\sum_{p|gcd(x,y)}\varphi(p) - n*m\\ &=2\sum_{p=1}^{min(n,m)}\varphi(p)\sum_{x=1}^n\sum_{y=1}^m[p|x][p|y] - n*m\\ &=2\sum_{p=1}^{min(n,m)}\varphi(p)\lfloor\frac np\rfloor \lfloor\frac mp\rfloor - n*m\end{aligned}
这个搁今天得整除分块后杜教筛1e10+多组数据

AC Code:

#include<bits/stdc++.h>
#define maxn 100005
#define LL long long
using namespace std;

int n,m,phi[maxn],pr[maxn/8],cnt_pr;
bool vis[maxn];

int main()
{
	scanf("%d%d",&n,&m); if(n>m) swap(n,m);
	phi[1] = 1;
	LL ans = 1ll*n*m;
	for(int i=2;i<=n;i++)
	{
		if(!vis[i]) pr[cnt_pr++] = i , phi[i]=i-1;
		for(int j=0;pr[j]*i<=n;j++)
		{
			vis[pr[j] * i] = 1;
			if(i % pr[j] == 0){ phi[i*pr[j]]=phi[i]*pr[j];break; }
			phi[i*pr[j]] = phi[i] * phi[pr[j]];
		}
		ans += 1ll*(2*phi[i])*(n/i)*(m/i);
	}
	printf("%lld\n",ans);
}

upd:自己杠精了。
i=1nj=1njφ(i)=j=1ni=1njφ(i)=j=1nΦ(nj)=i=1ni=n(n+1)2\begin{aligned} \sum_{i=1}^n\sum_{j=1}^{\frac nj} \varphi(i) = \sum_{j=1}^n\sum_{i=1}^{\frac nj} \varphi(i) = \sum_{j=1}^n \Phi(\frac nj) = \sum_{i=1}^n i = \frac {n(n+1)}2 \end{aligned}
AC Code:

#include<bits/stdc++.h>
#define maxn 100005
#define LL long long
using namespace std;

int n,m,phi[maxn],pr[maxn/8],cnt_pr;
bool vis[maxn];
map<int,LL>mp;

LL solve(int now)
{
	if(now <= 2137) return phi[now];
	if(mp.count(now)) return mp[now];
	LL &ret=mp[now]=1ll*now*(now+1)/2;
	for(int i=2,nxt;i<=now;i=nxt+1)
	{
		int tmp = now/i;
		nxt = now / tmp;
		ret -= solve(tmp) * (nxt-i+1);
	}
	return ret;
}

int main()
{
	scanf("%d%d",&n,&m); if(n>m) swap(n,m);
	phi[1] = 1;
	LL ans = -1ll*n*m;
	for(int i=2;i<=min(n,2137);i++)
	{
		if(!vis[i]) pr[cnt_pr++] = i , phi[i]=i-1;
		for(int j=0;pr[j]*i<=n;j++)
		{
			vis[pr[j] * i] = 1;
			if(i % pr[j] == 0){ phi[i*pr[j]]=phi[i]*pr[j];break; }
			phi[i*pr[j]] = phi[i] * (pr[j]-1);
		}
		phi[i] += phi[i-1];
	}
	for(int i=1,nxt,psolve=0,tmp,div1,div2;i<=n;i=nxt+1)
	{
		div1 = n / i , div2 = m / i;
		nxt = min(n/div1,m/div2);
		ans += 2ll * ((tmp=solve(nxt))-psolve) * div1 * div2;
		psolve = tmp;
	}
	printf("%lld\n",ans);
}
发布了639 篇原创文章 · 获赞 94 · 访问量 9万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览