2818: Gcd
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 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
#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 DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> 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