bzoj1101 [POI2007]Zap

Description


 FGD is cracking a cipher and he needs to answer many similar questions: for given integers a, b and d, how many positive integer pairs x, y are there such that x<=a
, y<=b, and gcd(x, y)=d. As a classmate of FGD, FGD hopes to get your help.

1<=n<= 50000
1<=d<=a,b<=50000

Solution


sb question, now I have also written the title written by 66wei last year, and I am full of emotions

a n s = i = 1 n d j = 1 m d [ g c d ( i , j ) = 1 ] = x = 1 n d μ ( x ) n d x m d x

Code


#include <stdio.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)

const int N=500005;

int prime[N],mu[N];

bool not_prime[N];

void pre_work(int n) {
    mu[1]=1;
    for (int i=2;i<=n;i++) {
        if (!not_prime[i]) {
            prime[++prime[0]]=i;
            mu[i]=-1;
        }
        for (int j=1;i*prime[j]<=n&&j<=prime[0];j++) {
            not_prime[i*prime[j]]=1;
            if (i%prime[j]==0) {
                mu[i*prime[j]]=0;
                break;
            }
            mu[i*prime[j]]=-mu[i];
        }
    }
    for (int i=1;i<=n;i++) mu[i]+=mu[i-1];
}

void solve(int n,int m) {
    if (n>m) std:: swap(n,m);
    int ans=0;
    for (int i=1,j;i<=n;i=j+1) {
        j=std:: min(n/(n/i),m/(m/i));
        ans+=(mu[j]-mu[i-1])*(n/i)*(m/i);
    }
    printf("%d\n", ans);
}

int main(void) {
    pre_work(N-5);
    int T; scanf("%d",&T);
    while (T--) {
        int n,m,d; scanf("%d%d%d",&n,&m,&d);
        solve(n/d,m/d);
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324737616&siteId=291194637