算术 HDU - 6715 (莫比乌斯反演)
大意: 给定$n,m$, 求$\sum\limits_{i=1}^n\sum\limits_{j=1}^m\mu(lcm(i,j))$
首先有$\mu(lcm(i,j))=\mu(i)\mu(j)\mu(gcd(i,j))$
枚举$gcd$可以得到$\sum\limits_{d=1}^{min(n,m)}\mu(d)\sum\limits_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum\limits_{j=1}^{\lfloor\frac{m}{d}\rfloor}\mu(id)\mu(jd)[gcd(i,j)=1]$
再反演一下就有$\sum\limits_{d_1=1}^{min(n,m)}\mu(d_1)\sum\limits_{d_2=1}^{\lfloor\frac{min(n,m)}{d_1}\rfloor}\mu(d_2)\sum\limits_{i=1}^{\lfloor\frac{n}{d_1d_2}\rfloor}\sum\limits_{j=1}^{\lfloor\frac{m}{d_1d_2}\rfloor}\mu(id_1d_2)\mu(jd_1d_2)$
枚举$d_1d_2$就有$\sum\limits_{T=1}^{min(n,m)}\sum\limits_{d\mid T}\mu(d)\mu(\frac{T}{d})\sum\limits_{i=1}^{\lfloor\frac{n}{T}\rfloor}\mu(iT)\sum\limits_{j=1}^{\lfloor\frac{m}{T}\rfloor}\mu(jT)$
预处理一下$\mu * \mu$即可
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <cmath> #include <set> #include <map> #include <queue> #include <string> #include <cstring> #include <bitset> #include <functional> #include <random> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl '\n' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head const int N = 1e6+50; int cnt, p[N], mu[N], mu2[N], vis[N]; void init() { mu[1] = mu2[1] = 1; REP(i,2,N-1) { if (!vis[i]) p[++cnt]=i,mu[i]=-1,mu2[i]=-2; for (int j=1,t; j<=cnt&&i*p[j]<N; ++j) { vis[t=i*p[j]] = 1; if (i%p[j]==0) { mu[t] = 0; if (i/p[j]%p[j]==0) mu2[t] = 0; else mu2[t] = 1; break; } mu[t] = -mu[i]; mu2[t] = -2*mu2[i]; } } } void work() { int n, m; scanf("%d%d", &n, &m); int mx = min(n,m), ans = 0; REP(i,1,mx) { int r1 = 0, r2 = 0; for (int j=i; j<=n; j+=i) r1+=mu[j]; for (int j=i; j<=m; j+=i) r2+=mu[j]; ans += mu2[i]*r1*r2; } printf("%d\n", ans); } int main() { init(); int t; scanf("%d", &t); while (t--) work(); }