莫比乌斯入门:bzoj 1101 Zap(Mobius)

传送门
题解:
莫比乌斯入门:bzoj 1101 Zap(Mobius)_第1张图片

#include
using namespace std;
const int MAXN=50002,INF=0x3f3f3f3f;
int mu[MAXN],prime[MAXN],tot=0;
bool vis[MAXN];
inline void linear_shaker() {
    memset(vis,false,sizeof(vis));
    mu[1]=1;
    for (register int i=2;iif (!vis[i]) prime[++tot]=i,mu[i]=-1;
        for (int j=1;j<=tot&&i*prime[j]true;
            if (i%prime[j]==0) {mu[i*prime[j]]=0;break;}
            mu[i*prime[j]]=-mu[i];
        }
    }
    for (register int i=2;i1];
}
int main() {
    linear_shaker();
    int kase;
    scanf("%d",&kase);
    while (kase--) {
        int n,m,d;
        scanf("%d%d%d",&n,&m,&d);
        n/=d,m/=d;
        int ans=0,last=0,t=min(n,m);
        for (int i=1;i<=t;i=last+1) {
            last=min(n/(n/i),m/(m/i));
            ans+=(mu[last]-mu[i-1])*(n/i)*(m/i);
        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(OI-数学)