bzoj 2818 Gcd

2818: Gcd

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 7381  Solved: 3281
[Submit][Status][Discuss]

Description

Given an integer N, find the
number of pairs (x,y) for which 1<=x,y<=N and Gcd(x,y) is prime.

 

Input

an integer N

Output

as the title

Sample Input

4

Sample Output

4

HINT

 

hint

for samples (2,2),(2,4),(3,3),(4,2)


1<=N<=10^7

 

Source

总结:gcd(a, b) == c >>>>gcd(a / c, b / c) == 1;
Then thought of enumerating each prime number c, prefixing the Euler function,
Multiply by 2 and subtract repeats when counting
Two cases: a / c > b / c, or a / c < b / c;
Repeating part: a / c == b / c (should be subtracted)
 
#include<bits/stdc++.h>

using namespace std;

#define ll long long
const int maxn = 1e7 + 5;
int n, pri[maxn];
ll f[maxn];
bool Notpri[maxn]; int tot = 0;

void pre(int x) {
	f[1] = 1;
	for (int i = 2; i <= n; ++i) {
		if(!Notpri[i]) pri[++tot] = i, f[i] = i - 1;
		for (int j = 1; j <= tot && i * pri[j] <= n; ++j) {
			Notpri[i * pri[j]] = true;
			if(i % pri[j] == 0) {
				f[i * pri[j]] = f[i] * pri[j];
				break;
			} f[i * pri[j]] = f[i] * (pri[j] - 1);
		}
	}
	for (int i = 1; i <= n; ++i) f[i] += f[i - 1];
}

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

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325073481&siteId=291194637
gcd