[bzoj2005] [NOI2010]能量采集

Sample Input

【样例输入1】

5 4


【样例输入2】

3 4


Sample Output

【样例输出1】

 36


【样例输出2】

20


Solution

$\sum_{i=1}^{n}\sum_{j=1}^m2(i,j)-1=2\cdot \sum_{i=1}^n\sum_{j=1}^m(i,j)-nm$

\begin{align} &\sum_{i=1}^{n}\sum_{j=1}^m(i,j)\\ =&\sum_{d=1}^{min(n,m)}d\sum_{i=1}^n\sum_{j=1}^m[(i,j)=d]\\ =&\sum_{d=1}^{min(n,m)}d\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{d}\rfloor}[(i,j)=1]\\ =&\sum_{d=1}^{min(n,m)}d\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{d}\rfloor}\sum_{t|i\&t|j}\mu(t)\\ =&\sum_{d=1}^{min(n,m)}d\sum_{t=1}^{min(\lfloor\frac{n}{d}\rfloor,\lfloor\frac{m}{d}\rfloor)}\mu(t)\lfloor\frac{n}{dt}\rfloor\lfloor\frac{m}{dt}\rfloor\\ =&\sum_{T=1}^{min(n,m)}\sum_{d|T}\frac{T}{d}\mu(d)\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor\\ \end{align}

#include<bits/stdc++.h>
using namespace std;

#define ONLINE_JUDGE

#define int long long

#ifdef ONLINE_JUDGE
#endif

namespace fast_IO {
char buf[1<<21],*p1=buf,*p2=buf;

template <typename T> inline void read(T &x) {
x=0;T f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
template <typename T,typename... Args> inline void read(T& x,Args& ...args) {
}

char buf2[1<<21],a[80];int p,p3=-1;

inline void flush() {fwrite(buf2,1,p3+1,stdout),p3=-1;}
template <typename T> inline void write(T x) {
if(p3>(1<<20)) flush();
if(x<0) buf2[++p3]='-',x=-x;
do {a[++p]=x%10+48;} while(x/=10);
do {buf2[++p3]=a[p];} while(--p);
buf2[++p3]='\n';
}
template <typename T,typename... Args> inline void write(T x,Args ...args) {
write(x),write(args...);
}
}

using fast_IO :: write;
using fast_IO :: flush;

const int maxn = 1e5+10;

int n,m,pri[maxn],mu[maxn],tot,vis[maxn],f[maxn];

void sieve() {
mu[1]=1;
for(int i=2;i<maxn;i++) {
if(!vis[i]) pri[++tot]=i,mu[i]=-1;
for(int j=1;j<=tot&&i*pri[j]<maxn;j++) {
vis[i*pri[j]]=1;
if(i%pri[j]==0) {mu[i*pri[j]]=0;break;}
mu[i*pri[j]]=-mu[i];
}
}
for(int i=1;i<maxn;i++)
for(int j=i;j<maxn;j+=i)
f[j]+=mu[i]*(j/i);
for(int i=1;i<maxn;i++) f[i]=f[i]+f[i-1];
}

signed main() {