2818: Gcd

来源:互联网 发布:凡科互动 游戏源码 编辑:程序博客网 时间:2024/06/07 02:00

2818: Gcd

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 4048  Solved: 1784
[Submit][Status][Discuss]

Description

给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
数对(x,y)有多少对.

Input

一个整数N

Output

如题

Sample Input

4

Sample Output

4

HINT

hint

对于样例(2,2),(2,4),(3,3),(4,2)


1<=N<=10^7

Source

湖北省队互测

[Submit][Status][Discuss]

莫比乌斯反演一波
ans = ∑F[i] (i∈prime)
F[i]:以i为最大公约数的对数
F[I] =  ∑u[d/i]*[n/d]*[n/d]  (i|d)
ans = ∑[n/d]^2∑u[d/i]
对于第二个∑,维护一个前缀和就好
(强行用所有素数来筛,复杂度近似O(n)的,n以内的素数个数约为(n/logn))

#include<iostream>#include<cstdio>#include<queue>#include<vector>#include<bitset>#include<algorithm>#include<cstring>#include<map>#include<stack>#include<set>#include<cmath>#include<ext/pb_ds/priority_queue.hpp>using namespace std;const int maxn = 1E7 + 10;typedef long long LL;int tot,prime[maxn],mu[maxn],sum[maxn];bool not_prime[maxn];LL ans,n;int main(){#ifdef DMCfreopen("DMC.txt","r",stdin);#endifcin >> n; mu[1] = 1;for (int i = 2; i <= n; i++) {if (!not_prime[i]) {mu[i] = -1; prime[++tot] = i;}for (int j = 1; j <= tot; j++) {int Nex = i*prime[j];if (Nex > n) break;not_prime[Nex] = 1;mu[Nex] = mu[prime[j]]*mu[i];if (i % prime[j] == 0) {mu[Nex] = 0; break;}} }for (int i = 1; i <= tot; i++)for (int j = 1; ; j++) {int Nex = j*prime[i];if (Nex > n) break;sum[Nex] += mu[Nex/prime[i]];}for (LL i = 1; i <= n; i++) ans += 1LL*sum[i]*(n/i)*(n/i);cout << ans;return 0;}

讲道理这个式子单词计算复杂度可以降成根号来回答多组询问。。bulabulabula

0 0