# 【BZOJ 2005】【NOI 2010】能量采集 数论+容斥原理

#include<cstdio>
#include<algorithm>
using namespace std;
long long f[100003];
int main(){
int n,m;
long long k=0;
scanf("%d %d\n",&n,&m);
if (n>m)
swap(n,m);
for(int i=n;i>=1;--i){
f[i]=(long long)(n/i)*(m/i);
for(int j=i+i;j<=n;j+=i)
f[i]-=f[j];
k+=f[i]*i*2-f[i];
}
printf("%lld\n",k);
return 0;
}


zky学长讲的$O(n+\sqrt{n})$的快速筛积性函数的方法：

\begin{aligned} ans & = \sum_{i=1}^n \sum_{j=1}^m gcd(i,j) \\ & = \sum_{i=1}^n \sum_{j=1}^m \sum_{k=1}^n k[k|i][k|j][gcd(\frac{i}{k},\frac{j}{k})=1] \\ & = \sum_{k=1}^n k \sum_{i=1}^n \sum_{j=1}^m [k|i][k|j][gcd(\frac{i}{k},\frac{j}{k})=1] \\ & i=ki, j=kj \\ & = \sum_{k=1}^n k \sum_{i=1}^{\left \lfloor \frac{n}{k} \right \rfloor} \sum_{j=1}^{\left \lfloor \frac{m}{k} \right \rfloor} [ gcd(i,j)=1] \\ & = \sum_{k=1}^n k \sum_{i=1}^{\left \lfloor \frac{n}{k} \right \rfloor} \sum_{j=1}^{\left \lfloor \frac{m}{k} \right \rfloor} \sum_{d=1}^{\left \lfloor \frac{n}{k} \right \rfloor} [d|i][d|j] \mu(d) \\ & = \sum_{k=1}^n k \sum_{d=1}^{\left \lfloor \frac{n}{k} \right \rfloor} \mu(d) \sum_{i=1}^{\left \lfloor \frac{n}{k} \right \rfloor} \sum_{j=1}^{\left \lfloor \frac{m}{k} \right \rfloor} [d|i][d|j] \\ & = \sum_{k=1}^n k \sum_{d=1}^{\left \lfloor \frac{n}{k} \right \rfloor} \mu(d) \left \lfloor \frac{n}{dk} \right \rfloor \left \lfloor \frac{m}{dk} \right \rfloor \\ & T=dk \\ & = \sum_{T=1}^n \left \lfloor \frac{n}{T} \right \rfloor \left \lfloor \frac{m}{T} \right \rfloor \sum_{d|T} \mu(d) \frac{T}{d} \\ \end{aligned}

xyx说因为$\sum_{d|T} \mu(d) \frac{T}{d}$(及$id×\mu$)是积性的，所以筛一筛就出来啦

2016-03-30：达神的正解！上面那个看一眼就觉得纯属扯淡(没事莫比乌斯反演干什么)：$(n<m)$

\begin{aligned} ans & = \sum_{i=1}^n \sum_{j=1}^m gcd(i,j) \\ & = \sum_{i=1}^n \sum_{j=1}^m \sum_{d=1}^n [d|i][d|j] \phi(d) \\ & = \sum_{d=1}^n \sum_{i=1}^n \sum_{j=1}^m [d|i][d|j] \phi(d) \\ & = \sum_{d=1}^n \left \lfloor \frac{n}{d} \right \rfloor \left \lfloor \frac{m}{d} \right \rfloor \phi(d) \end{aligned}

posted @ 2016-03-21 14:32  abclzr  阅读(207)  评论(0编辑  收藏  举报