【BZOJ1101】[POI2007]Zap

来源:互联网 发布:知乎 了不起的盖茨比 编辑:程序博客网 时间:2024/06/14 19:51

题解:
莫比乌斯反演
这里写图片描述
这里将N和M分别替换为N/d向下取整和M/d向下取整即可

//by sdfzchy#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;const int inf=(1<<30),N=100010;int n,m;inline int in(){    char ch=getchar();    int f=1,tmp=0;    while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}    while(ch>='0'&&ch<='9') {tmp=(tmp<<1)+(tmp<<3)+(ch-'0');ch=getchar();}    return tmp*f;}int pri[N],pcnt,miu[N+10],sum[N+10];bool ok[N+10];void init(){    miu[1]=1;    for(int i=2;i<=N;i++)    {        if(!ok[i]) pri[++pcnt]=i,miu[i]=-1;        for(int j=1;j<=pcnt&&(LL)i*pri[j]<=N;j++)        {            ok[i*pri[j]]=1;            if(i%pri[j]==0) {miu[i*pri[j]]=0;break;}            miu[i*pri[j]]=-miu[i];        }    }    for(int i=1;i<=N;i++) sum[i]=sum[i-1]+miu[i];}LL cal(int x,int y){    if(x>y) swap(x,y);    LL ans=0;    for(int i=1,p;i<=x;i=p+1)    {        p=min(x/(x/i),y/(y/i));        ans+=((LL )sum[p]-sum[i-1])*(x/i)*(y/i);    }    return ans;}int a,b,k;int main(){    init();    int T=in();    while(T--)    {        a=in(),b=in(),k=in();        printf("%lld\n",cal(a/k,b/k));    }    return 0;}
原创粉丝点击