P2568 GCD

\(\color{#0066ff}{ 题目描述 }\)

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

\(\color{#0066ff}{输入格式}\)

一个整数N

\(\color{#0066ff}{输出格式}\)

答案

\(\color{#0066ff}{输入样例}\)

4

\(\color{#0066ff}{输出样例}\)

4

\(\color{#0066ff}{数据范围与提示}\)

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

1<=N<=10^7

\(\color{#0066ff}{ 题解 }\)

\[\sum_{p\in prime} \sum_{i=1}^n \sum_{j=1}^n [gcd(i,j)==p] \]

\[\sum_{p\in prime} \sum_{i=1}^{\lfloor\frac n p \rfloor} \sum_{j=1}^{\lfloor\frac n p \rfloor} [gcd(i,j)==1] \]

\(\varphi\) xjb统计一下就行了

#include<bits/stdc++.h>
#define LL long long
LL in() {
	char ch; LL x = 0, f = 1;
	while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
	for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
	return x * f;
}
const int maxn = 1e7 + 10;
int pri[maxn], tot, n;
LL phi[maxn];
bool vis[maxn];
signed main() {
	n = in();
	phi[1] = 1;
	for(int i = 2; i <= n; i++) {
		if(!vis[i]) pri[++tot] = i, phi[i] = i - 1;
		for(int j = 1; j <= tot && (LL)i * pri[j] <= n; j++) {
			vis[i * pri[j]] = true;
			if(i % pri[j] == 0) {
				phi[i * pri[j]] = phi[i] * pri[j];
				break;
			}
			else phi[i * pri[j]] = phi[i] * (pri[j] - 1);
		}
	}
	for(int i = 2; i <= n; i++) phi[i] += phi[i - 1];
	LL ans = 0;
	for(int i = tot; i >= 1; i--) {
		ans += (phi[n / pri[i]] << 1LL) - 1;
	}

	printf("%lld\n", ans);
	return 0;
}
posted @ 2019-01-21 17:06  olinr  阅读(205)  评论(0编辑  收藏  举报