zoukankan      html  css  js  c++  java
  • [BZOJ2301][HAOI2011]Problem b

    BZOJ
    Luogu
    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

    sol

    如果做了那个题的话就会发现这两道题其实是一道题。
    本题加入了下界的限制,但很显然我们做一个二维的容斥就好了。
    别告诉我你不会二维容斥(就是二维前缀和那种搞法)

    code

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    #define ll long long
    const int N = 50005;
    const int n = 50000;
    int gi()
    {
    	int x=0,w=1;char ch=getchar();
    	while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    	if (ch=='-') w=0,ch=getchar();
    	while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    	return w?x:-x;
    }
    int mu[N],pri[N],tot,s[N];
    bool zhi[N];
    void Mobius()
    {
    	zhi[1]=true;mu[1]=1;
    	for (int i=2;i<=n;i++)
    	{
    		if (!zhi[i]) pri[++tot]=i,mu[i]=-1;
    		for (int j=1;j<=tot&&i*pri[j]<=n;j++)
    		{
    			zhi[i*pri[j]]=1;
    			if (i%pri[j]) mu[i*pri[j]]=-mu[i];
    			else {mu[i*pri[j]]=0;break;}
    		}
    	}
    	for (int i=1;i<=n;i++) s[i]=s[i-1]+mu[i];
    }
    ll calc(int a,int b,int k)
    {
    	a/=k;b/=k;
    	if (a>b) swap(a,b);
    	int i=1,j;ll ans=0;
    	while (i<=a)
    	{
    		j=min(a/(a/i),b/(b/i));
    		ans+=1ll*(s[j]-s[i-1])*(a/i)*(b/i);
    		i=j+1;
    	}
    	return ans;
    }
    int main()
    {
    	int T=gi();
    	Mobius();
    	while (T--)
    	{
    		int a=gi()-1,b=gi(),c=gi()-1,d=gi(),k=gi();
    		printf("%lld
    ",calc(b,d,k)-calc(a,d,k)-calc(b,c,k)+calc(a,c,k));
    	}
    	return 0;
    }
    
  • 相关阅读:
    python-序列化与反序列化(loads、load、dumps、dump)
    STM32命名
    批处理参考
    Delphi通过管道执行外部命令行程序(cmd)并获取返回结果
    ubuntu使用备忘
    ubuntu14.04中安装QuartusII9.1步骤
    删除选中数据
    DBGridEh基本操作
    sqlserver 字符串函数
    使用 Delphi Xe 的 TDictionary
  • 原文地址:https://www.cnblogs.com/zhoushuyu/p/8191361.html
Copyright © 2011-2022 走看看