# [bzoj2310][HAOI2011]Problem b

102 篇文章 0 订阅
4 篇文章 0 订阅

2301: [HAOI2011]Problem b

Time Limit: 50 Sec Memory Limit: 256 MB

Input

Output

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

badcgcd(x,y)=k $\sum_a^b\sum_c^dgcd(x,y)=k$

= b/ka/kd/kc/k[gcd(x,y)=1] $\sum_{a/k}^{b/k}\sum_{c/k}^{d/k}[gcd(x,y)=1]$

= b/ka/kd/kc/kz|gcd(x,y)μ(z) $\sum_{a/k}^{b/k}\sum_{c/k}^{d/k}\sum_{z|gcd(x,y)}\mu(z)$

= min(n,m)z(bzaz)(dzcz)μ(z) $\sum_z^{min(n,m)}(\lfloor \frac{b}{z} \rfloor-\lfloor \frac{a}{z} \rfloor)*(\lfloor \frac{d}{z} \rfloor-\lfloor \frac{c}{z} \rfloor)*\mu(z)$

#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#define ll long long
using namespace std;
const int N=51000;
int n,m,a,b,c,d,k,j;
int mu[N],prime[N];
bool is[N];
ll ans;
{
char c;
int res,flag=0;
while((c=getchar())>'9'||c<'0') if(c=='-')flag=1;
res=c-'0';
while((c=getchar())>='0'&&c<='9') res=(res<<3)+(res<<1)+c-'0';
return flag?-res:res;
}
inline int get(int x,int y)
{
return !(x%y)?x/y:x/y+1;
}
const int inf=2147483646;
inline void solve()
{
ans=0;
a=get(a,k)-1;b=b/k;c=get(c,k)-1;d=d/k;
if(b>d) swap(b,d),swap(a,c);
for(int i=1;i<=b;i=j+1)
{
j=min(b/(b/i),min((a/i)?a/(a/i):inf,min((c/i)?c/(c/i):inf,d/(d/i))));
ans+=(ll)(b/i-a/i)*(d/i-c/i)*(mu[j]-mu[i-1]);
}

/*  for(int i=1;i<=b;++i)
ans+=(ll)(b/i-a/i)*(d/i-c/i)*(mu[i]-mu[i-1]);*/
printf("%lld\n",ans);
}
int main()
{
//  freopen("2301.txt","r",stdin);
mu[1]=1;
for(int i=2;i<N;++i)
{
if(!is[i]) mu[i]=-1,prime[++prime[0]]=i;
for(j=1;j<=prime[0]&&prime[j]*i<N;++j)
{
is[prime[j]*i]=1;
if(!(i%prime[j]))
{
mu[i*prime[j]]=0;
break;
}
mu[i*prime[j]]=-mu[i];
}
mu[i]+=mu[i-1];
}
while(T--)
solve();
}
• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
12-06 572
06-17 215
02-22 94
04-20 229
02-25 887
01-17 228
06-13 304
01-24 135
12-23 230
04-21 871
04-22 157

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