HYSBZ 2301 Problem b 莫比乌斯 分块优化

本文提供了一种高效方法来解决在给定范围内寻找满足特定最大公约数条件的数对数量的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


#include<iostream>
#include<cstring>
#include<cstdio>
#include<ostream>
#include<istream>
#include<algorithm>
#include<queue>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<vector>
#define fi first
#define se second
#define ll long long
#define pii pair<int,int>
#define inf (1<<30)
#define eps 1e-8
#define pb push_back
using namespace std;
const int maxn=100010;
int sum[maxn];
int mu[maxn],prime[maxn],tot;
bool check[maxn];
void getmu()
{
    int N=100000;
    memset(check,0,sizeof(check));
    mu[1]=1,tot=0;
    for(int i=2;i<=N;i++){
        if(!check[i])
            prime[tot++]=i,mu[i]=-1;
        for(int j=0;j<tot;j++){
            if(i*prime[j]>N)
                break;
            check[i*prime[j]]=true;
            if(i%prime[j]==0){
                mu[i*prime[j]]=0;
                break;
            }
            else
                mu[i*prime[j]]=-mu[i];
        }
    }
}
int main()
{
    getmu();
    int n,m;
    while(~scanf("%d%d",&n,&m)) {
        if(n>m) swap(n,m);
        ll ans=0;
        for(int i=1;i<=n;i++) {
            ll u=0;
            for(int j=i;j<=n;j+=i) {
                u=u+(ll)mu[j/i]*(n/j)*(m/j);
            }
            ans+=u*(2*(i-1)+1);
        }
        printf("%lld\n",ans);
    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值