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

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

2



2 5 1 5 1



1 5 1 5 2

Sample Output

14



3

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值