bzoj 1101 zap 莫比乌斯

1101: [POI2007]Zap

Time Limit: 10 Sec  Memory Limit: 162 MB

Description

  FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a
,y<=b,并且gcd(x,y)=d。作为FGD的同学,FGD希望得到你的帮助。

Input

  第一行包含一个正整数n,表示一共有n组询问。(1<=n<= 50000)接下来n行,每行表示一个询问,每行三个
正整数,分别为a,b,d。(1<=d<=a,b<=50000)

Output

  对于每组询问,输出到输出文件zap.out一个正整数,表示满足条件的整数对数。

Sample Input

2
4 5 2
6 4 3

Sample Output

3
2
//对于第一组询问,满足条件的整数对有(2,2),(2,4),(4,2)。对于第二组询问,满足条件的整数对有(
6,3),(3,3)。
 
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define esp 0.00000000001
#define pi 4*atan(1)
const int N=1e5+10,M=1e7+10,inf=1e9+10,mod=1e9+7;
int mu[N], p[N], np[N], cnt, sum[N];
void init() {
    mu[1]=1;
    for(int i=2; i<N; ++i) {
        if(!np[i]) p[++cnt]=i, mu[i]=-1;
        for(int j=1; j<=cnt && i*p[j]<N; ++j) {
            int t=i*p[j];
            np[t]=1;
            if(i%p[j]==0) { mu[t]=0; break; }
            mu[t]=-mu[i];
        }
    }
    for(int i=1;i<N;i++)
    sum[i]=sum[i-1]+mu[i];
}
ll getans(int b,int d)
{
    ll ans=0;
    for(int L=1,R=0;L<=b;L=R+1)
    {
        R=min(b/(b/L),d/(d/L));
        ans+=(ll)(sum[R]-sum[L-1])*(b/L)*(d/L);
    }
    return ans;
}
int main()
{
    int T;
    init();
    scanf("%d",&T);
    while(T--)
    {
        int b,d,k;
        scanf("%d%d%d",&b,&d,&k);
        if(b>d)swap(b,d);
        ll ans=0;
        printf("%lld
",getans(b/k,d/k));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jhz033/p/5789689.html