BZOJ2818 Gcd

题意

<body> <center> <div style="width:90%; text-align:left"> <img src="image/logo.png"> </div> <table width="96%"> <tbody><tr align="center" class="hd" valign="top"> <th><a href="faqs.php">F.A.Qs</a></th> <th><a href="./">Home</a></th> <th><a href="./bbs.php">Discuss</a></th> <th><a href="./problemset.php">ProblemSet</a></th> <th><a href="./status.php">Status</a></th> <th><a href="./ranklist.php">Ranklist</a></th> <th><a href="./contest.php">Contest</a></th> <th><a href="http://begin.lydsy.com/JudgeOnline">入门OJ</a></th> <th><a href="./modifypage.php"><b>ModifyUser</b></a>&nbsp;&nbsp;<a href="userinfo.php?user=autoint"> <font color="red">autoint</font></a></th><th><a href="logout.php">Logout</a></th> <th><a href="./donation.php"><font color="red">捐赠本站</font></a></th> </tr> </tbody></table> </center> <title>Problem 2818. -- Gcd</title><center><h2>2818: Gcd</h2><span class="green">Time Limit: </span>10 Sec&nbsp;&nbsp;<span class="green">Memory Limit: </span>256 MB<br><span class="green">Submit: </span>9590&nbsp;&nbsp;<span class="green">Solved: </span>4263<br>[<a href="submitpage.php?id=2818">Submit</a>][<a href="problemstatus.php?id=2818">Status</a>][<a href="bbs.php?id=2818">Discuss</a>]</center><h2>Description</h2><div class="content"><p>给定整数N,求1&lt;=x,y&lt;=N且Gcd(x,y)为素数的<br> 数对(x,y)有多少对.</p> <p></p></div><h2>Input</h2><div class="content"><p>一个整数N</p></div><h2>Output</h2><div class="content"><p>如题</p></div><h2>Sample Input</h2> <div class="content"><span class="sampledata">4<br> <br> </span></div><h2>Sample Output</h2> <div class="content"><span class="sampledata">4</span></div><h2>HINT</h2> <div class="content"><p></p><p>hint<br><br> 对于样例(2,2),(2,4),(3,3),(4,2)</p><br> <p>1&lt;=N&lt;=10^7<br><br> </p><p></p></div><h2>Source</h2> <div class="content"><p><a href="problemset.php?search=湖北省队互测">湖北省队互测</a></p></div><center>[<a href="submitpage.php?id=2818">Submit</a>][<a href="problemstatus.php?id=2818">Status</a>][<a href="bbs.php?id=2818">Discuss</a>]</center><br> <a href="./"><span class="red">HOME</span></a> <a href="javascript:history.go(-1)"><span class="red">Back</span></a>
</body>

分析

参照hzwer的题解。

枚举每个素数,然后每个素数p对于答案的贡献就是(1 ~ n / p) 中有序互质对的个数
而求1~m中有序互质对x,y的个数,可以令y >= x, 当y = x时,有且只有y = x = 1互质,当y > x时,确定y以后符合条件的个数x就是phiy
所以有序互质对的个数为(1 ~ n/p)的欧拉函数之和乘2减1(要求的是有序互质对,乘2以后减去(1, 1)多算的一次)
那么就只需要先筛出欧拉函数再求个前缀和就可以了

时间复杂度\(O(n)\)

代码

#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
    rg T data=0,w=1;rg char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
    while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
    return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;

co int N=1e7+1;
int n,p[N];
ll phi[N],ans;
int main(){
//	freopen(".in","r",stdin),freopen(".out","w",stdout);
	read(n);
	phi[1]=1;
	for(int i=2;i<=n;++i){
		if(!p[i]) p[++p[0]]=i,phi[i]=i-1;
		for(int j=1;j<=p[0]&&i*p[j]<=n;++j){
			p[i*p[j]]=1;
			if(i%p[j]==0) {phi[i*p[j]]=phi[i]*p[j];break;}
			phi[i*p[j]]=phi[i]*phi[p[j]];
		}
	}
	for(int i=2;i<=n;++i) phi[i]+=phi[i-1];
	for(int i=1;i<=p[0];++i) ans+=phi[n/p[i]]*2-1;
	printf("%lld\n",ans);
	return 0;
}

posted on 2019-04-02 18:14  autoint  阅读(87)  评论(0编辑  收藏  举报

导航