【BZOJ 2818】Gcd

题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2818
离线版题目:http://paste.ubuntu.com/20184097/

这个题目,很容易想到枚举素数,然后求gcd=1的对数
用莫比乌斯函数+分段来搞的话,好像很常规,时间复杂度O(ln(n)*sqrt(n)+n)
然后就是看到这篇博客,发现可以直接用phi函数搞QAQ,时间复杂度O(ln(n)+n)
因为phi前缀和那里必须用long long搞得实际运行时间比莫比乌斯函数还要慢QAQ
莫比乌斯函数版本:

#include<iostream>
#include<cstdio>
#include<bitset>
#define LL long long
using namespace std;

const int MAXN = 10000000+9;

int pri[MAXN],tot,mu[MAXN],n;
bitset<MAXN> tag;

inline void Get_mu(){
	mu[1] = 1;
	for (int i=2;i<=n;i++){
		if (!tag[i]) pri[++tot] = i, mu[i] = -1;
		for (int j=1;j<=tot && pri[j]*i <= n;j++){
			tag[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++) mu[i] += mu[i-1];
}

int main(){ 
	scanf("%d",&n); Get_mu(); LL vout = 0;
	for (int j=1;j<=tot && pri[j] <= n;j++) {
		int k = pri[j], a=n/k;
		for (int i=1,tmp;i<=a;i=tmp+1)
			tmp = a/(a/i),
			vout += (LL)(mu[tmp]-mu[i-1])*(a/i)*(a/i);
	}
	printf("%lld\n",vout);
	return 0;
} 

欧拉函数版本:

#include<iostream>
#include<cstdio>
#include<bitset>
#define LL long long
using namespace std;

const int MAXN = 10000000+9;

int pri[MAXN],tot,n;
LL phi[MAXN],vout=0;
bitset<MAXN> tag; 

inline void Get_Phi(){
	phi[1] = 1;
	for (int i=2;i<=n;i++){
		if (!tag[i]) pri[++tot] = i, phi[i] = i-1;
		for (int j=1;j<=tot && pri[j]*i<=n;j++){
			tag[pri[j]*i] = 1;
			if (i % pri[j]) phi[i*pri[j]] = phi[i]*(pri[j]-1);
			else {phi[i*pri[j]] = phi[i]*pri[j]; break;}
		}
	}
	for (int i=1;i<=n;i++) phi[i] += phi[i-1];
}

int main(){
	scanf("%d",&n); Get_Phi(); 
	for (int i=1;i<=tot && pri[i] <= n;i++) 
		vout += phi[n/pri[i]]*2 - 1;
	printf("%lld\n",vout);
	return 0;
} 

24 thoughts to “【BZOJ 2818】Gcd”

  1. This design is steller! You certainly know how to
    keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well,
    almost…HaHa!) Great job. I really loved what you had to say, and more than that,
    how you presented it. Too cool!

  2. I just like the valuable information you provide for your articles.
    I’ll bookmark your weblog and take a look at again right here frequently.
    I’m quite certain I’ll be told a lot of new stuff right here!
    Good luck for the next!

  3. What i do not realize is in reality how you’re not
    actually much more neatly-preferred than you might be now.
    You’re so intelligent. You already know thus considerably in relation to this matter, made me individually consider it from numerous various angles.
    Its like men and women are not fascinated except it’s one thing to accomplish with Woman gaga!
    Your individual stuffs great. All the time care for it up!

  4. I love what you guys are up too. This kind of clever work and reporting!

    Keep up the terrific works guys I’ve included you guys
    to my own blogroll.

  5. Nice blog here! Also your web site loads up fast! What web host are you using?
    Can I get your affiliate link to your host? I wish my site loaded up as fast as yours lol

  6. Howdy! I could have sworn I’ve been to this site before but after checking
    through some of the post I realized it’s new to me. Nonetheless, I’m definitely delighted I found it
    and I’ll be bookmarking and checking back frequently!

  7. Hey there! I just wish to give you a huge thumbs up for your great information you have got here
    on this post. I am coming back to your web site for more soon.

  8. Hi, I think your site might be having browser compatibility issues.
    When I look at your blog site in Chrome, it looks fine but
    when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up!
    Other then that, terrific blog!

  9. Hey very nice web site!! Man .. Beautiful ..
    Wonderful .. I will bookmark your blog and take the feeds additionally?
    I’m satisfied to find so many helpful information right here
    in the submit, we need develop extra techniques in this regard, thank you for sharing.
    . . . . .

Leave a Reply

Your email address will not be published. Required fields are marked *