首页 > 其他分享 >HAOI2011 Problem b

HAOI2011 Problem b

时间:2023-07-24 19:33:54浏览次数:49  
标签:ch frac limits fw sum HAOI2011 Problem operatorname

Problem b

link

做法:莫比乌斯反演。

思路:

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

\[\operatorname{f}(n)=\sum\limits_{i=1}^x \sum\limits_{j=1}^y [n|\gcd(i,j)] \]

\[ans=\operatorname{g}(n)=\sum\limits_{i=1}^x \sum\limits_{j=1}^y [\gcd(x,y)=k] \]

利用容斥原理,答案为图像上蓝色部分:

image

问题在于如何求 \(\operatorname{g}(x,y)\) 。

我们知道:

\[\operatorname{f}(n)=\sum\limits_{n|d}\operatorname{g}(d) \]

利用莫比乌斯反演可得:

\[\operatorname{g}(n)=\sum\limits_{n|d}\operatorname{\mu}(\frac{d}{n})\operatorname{f}(n) \]

\(\operatorname{\mu}(\frac{d}{n})\) 可以利用线性筛求出, \(\operatorname{f}(d)\) 易得 \(\operatorname{f}(d)=\left\lfloor\frac{x}{n}\right\rfloor \left\lfloor\frac{y}{n}\right\rfloor\)

答案可以推出:

\[ans=\sum\limits_{n|d}\operatorname{\mu}(\frac{d}{n})\operatorname{f}(d)=\sum\limits_{i=1}^{\min(x,y)}\operatorname{\mu}(i)\left\lfloor\frac{x}{i\cdot n}\right\rfloor \left\lfloor\frac{y}{i\cdot n}\right\rfloor \]

再利用数论分块以及前缀和优化一下就不会超时了。

代码:

#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cctype>
#include<algorithm>
typedef long long LL;
typedef unsigned long long ULL;
namespace FastIo{
    typedef __uint128_t ULLL;
    static char buf[100000],*p1=buf,*p2=buf,fw[100000],*pw=fw;
    #define gc p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++
    inline void pc(const char &ch){
    	if(pw-fw==100000)fwrite(fw,1,100000,stdout),pw=fw;
    	*pw++=ch;
	}
    #define fsh fwrite(fw,1,pw-fw,stdout),pw=fw
	struct FastMod{
        FastMod(ULL b):b(b),m(ULL((ULLL(1)<<64)/b)){}
        ULL reduce(ULL a){
            ULL q=(ULL)((ULLL(m)*a)>>64);
            ULL r=a-q*b;
            return r>=b?r-b:r;
        }
        ULL b,m;
    }HPOP(10);
    struct QIO{
    	char ch;
    	int st[40];
    	template<class T>inline void read(T &x){
    		x=0,ch=gc;
    		while(!isdigit(ch))ch=gc;
    		while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=gc;}
		}
		template<class T>inline void write(T a){
			do{st[++st[0]]=HPOP.reduce(a);a/=10;}while(a);
			while(st[0])pc(st[st[0]--]^48);
			pc('\n');
		}
	}qrw;
}
using namespace FastIo;
#define P(A) A=-~A
#define fione(i,a,b) for(register int i=a;i<=b;P(i))
const int NUMBER1=5e4;
int prime[NUMBER1+5],cnt(0);
LL mu[NUMBER1+5];
bool pd[NUMBER1+5];
inline void PRIME(){
	int k;
	mu[1]=1;
	fione(i,2,NUMBER1){
		if(!pd[i])prime[++cnt]=i,mu[i]=-1;
		for(register int j(1);prime[j]*i<=NUMBER1;P(j)){
			k=prime[j]*i;
			pd[k]=true;	
			if(!(i%prime[j]))break;
			mu[k]-=mu[i];
		}
	}
	fione(i,1,NUMBER1)mu[i]+=mu[i-1];
}
inline int fk(int k,int x){return k/(k/x);}
inline LL work(int x,int y,int k){
	x/=k,y/=k;
	LL ans(0);int n=std::min(x,y);
	for(register int l=1,r;l<=n;l=r+1){
		r=std::min(n,std::min(fk(x,l),fk(y,l)));
		ans+=(mu[r]-mu[l-1])*(x/l)*(y/l);
	}
	return ans;
}
signed main(){
	PRIME();
	int T,a,b,c,d,k;
	qrw.read(T);
	while(T--){
		qrw.read(a);
		qrw.read(b);
		qrw.read(c);
		qrw.read(d);
		qrw.read(k);
		qrw.write(work(b,d,k)-work(a-1,d,k)-work(b,c-1,k)+work(a-1,c-1,k));
	}
	fsh;
    exit(0);
    return 0;
}

标签:ch,frac,limits,fw,sum,HAOI2011,Problem,operatorname
From: https://www.cnblogs.com/SHOJYS/p/17578114.html

相关文章

  • 通过docker安装的jira提示We've detected a potential problem with JIRA's Dashboard
    正常通过docker安装jira后,访问是不会出问题的但是如果使用nginx代理后,就是在nginx里配置了proxy_passhttp://localhost:2800再访问后,就会报错We'vedetectedapotentialproblemwithJIRA'sDashboardconfigurationthatyouradministratorcancorrect.Clickhereto......
  • 【优先队列】【堆排序实现优先队列】[1054. 距离相等的条形码](https://leetcode.cn/p
    【优先队列】【堆排序实现优先队列】1054.距离相等的条形码在一个仓库里,有一排条形码,其中第i个条形码为barcodes[i]。请你重新排列这些条形码,使其中任意两个相邻的条形码不能相等。你可以返回任何满足该要求的答案,此题保证存在答案。示例1:输入:barcodes=[1,1,1,2,2,2]......
  • 【大联盟】20230706 Interesting DS Problem(interesting) QOJ2559 【Endless Road】
    题目描述here。题解首先,我们对所有区间离散化,删除一个区间时,我们暴力删除内部还存在的子区间。如果没有区间包含是好做的,因为我们删除一个子区间时,将区间按照左端点排序,可发现包含这个子区间的区间是连续的一个区间。现在考虑有区间包含怎么做。我们考虑维护出当前所有不包含......
  • Could not get list of tables from database. Probably a JDBC driver problem.
     在用myeclipse8.5M1反向生成代码时报错: Aninternalerroroccurredduring:"GeneratingArtifacts".Couldnotgetlistoftablesfromdatabase.ProbablyaJDBCdriverproblem.  =============================  尝试了更换工作空间、重装myeclipse、更换oracle驱动......
  • 【每日一题】Problem 538B. Quasi Binary
    原题解决思路最简单的思路就是贪心了,每次生成不超过目标值的\(quasibinary\),即可使最终数量最少#include<bits/stdc++.h>intquasibinary(intmax){intres=0;intp=0;while(max>0){if(max%10>0){res+=int(pow(10,......
  • poj 1777 Vivian's Problem
    题意:给出K个数,p1,p2,……pk,不一定是素数,给这些数添加指数,0-10之间,最终的乘积为n,他的所有因子和为m,问是否存在一个m为2的幂,如果有多个输出最大的指数,如果没有输出NO。梅森素数 我们把满足E=2^i-1的素数E称作梅森素数。关于梅森素数,有一个重要的定理:“一个数能够写成几个......
  • hdu Polynomial Problem
     有点杂乱无章,考虑各种情况就行了。 #include<iostream>#include<cstdio>#include<cstring>#include<cmath>usingnamespacestd;#defineINF0x3fffffff#defineMAXN100001intmain(){intn,m,x,flag,mul,ans;charstr[MAXN];whil......
  • 题解 Yet Another Minimization Problem
    YetAnotherMinimizationProblem神仙题。第一眼看上去就是DP。定义\(f_{i,j}\)表示当前点\(i\),分\(j\)段的最小费用。\(f_{i,j}=\min(f_{i,j},f_{k,j-1}+val_{k+1,i})\)然后发现复杂度\(O(n^2k)\),直接T飞,需要优化。我们发现\(j\)那一维可以滚掉,也就是只考虑第......
  • 解决报错Cannot connect to the Maven process. Try again later. If the problem per
    故障描述:使用idea下载java某个源文件,idea报错:CannotconnecttotheMavenprocess.Tryagainlater.Iftheproblempersists,checktheMaven解决方案:修改maven的配置文件......
  • Mac环境下 罗技 Logi Options+ Backend Connection Problem
    m1芯片MacBook,安装LogiOptions+。打开后一直提示Backendconnectionproblem-clickheretolaunchbackend。报错图片如下 查询后得知,是一个启动程序没有打开。解决方案如下:下载一个AppCleaner&Uninstaller,软件官网:(AppCleaner&Uninstaller-DownloadfromOffic......