【bzoj2301】[HAOI2011]Problem b 莫比乌斯反演

来源:互联网 发布:软件供应商 编辑:程序博客网 时间:2024/05/22 16:58

Description

对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数。

Input

第一行一个整数n,接下来n行每行五个整数,分别表示a、b、c、d、k

Output

共n行,每行一个整数表示满足要求的数对(x,y)的个数

Sample Input

22 5 1 5 11 5 1 5 2

Sample Output

143

HINT

100%的数据满足:1≤n≤50000,1≤a≤b≤50000,1≤c≤d≤50000,1≤k≤50000

Source


同【bzoj1101】[POI2007]Zap 莫比乌斯反演

注意,输入后要先减再除。

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const int SZ = 1000010;const int MAXN = 50010;bool vis[SZ];int pri[SZ],mu[SZ];int sum[SZ];void shai(){    mu[1] = 1;    for(int i = 2,tot = 0;i <= MAXN;i ++)    {        if(!vis[i]) pri[++ tot] = i,mu[i] = -1;        for(int j = 1,m;j <= tot && (m = i * pri[j]) <= MAXN;j ++)        {            vis[m] = 1;            if(i % pri[j] == 0) { mu[m] = 0; break; }            else mu[m] = - mu[i];        }    }    for(int i = 1;i <= MAXN;i ++)        sum[i] = sum[i - 1] + mu[i];}LL ask(int n,int m){    LL ans = 0;     for(int i = 1,r;i <= min(n,m);i = r + 1)    {        r = min(n / (n / i),m / (m / i));        ans += ((LL)sum[r] - sum[i - 1]) * (n / i) * (m / i);    }    return ans;}int main(){    int T;    scanf("%d",&T);    shai();//  for(int i = 1;i <= 100;i ++) printf("%d ",sum[i]);    while(T --)    {        int a,b,c,d,k;        scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);        a --; c --;        a /= k; b /= k; c /= k;d /= k;        printf("%lld\n",ask(b,d) - ask(a,d) - ask(b,c) + ask(a,c));    }    return 0;}
0 0
原创粉丝点击