BZOJ2818 Gcd

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2818

Description

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

Input

一个整数N

Output

如题

懒得写题解就看黄学长的吧:http://hzwer.com/3466.html

这题不难

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cmath>
 6 #define rep(i,l,r) for(int i=l; i<=r; i++)
 7 #define clr(x,y) memset(x,y,sizeof(x))
 8 using namespace std;
 9 typedef long long ll;
10 const int maxn = 10000010;
11 int n,cnt=0,pri[maxn],phi[maxn];
12 ll sum[maxn];
13 bool flag[maxn];
14 void get_phi(){
15     clr(flag,0); phi[1] = 1;
16     rep(i,2,n){
17         if (!flag[i]) pri[++cnt] = i, phi[i] = i - 1;
18         for(int j = 1; j <= cnt && i*pri[j] <= n; j++){
19             flag[i*pri[j]] = 1;
20             if (i % pri[j]) phi[i*pri[j]] = phi[i] * (pri[j] - 1);
21             else{
22                 phi[i*pri[j]] = phi[i] * pri[j];
23                 break;
24             }
25         }
26     }
27     sum[1] = phi[1]; rep(i,2,n) sum[i] = sum[i-1] + phi[i];
28 }
29 int main(){
30     scanf("%d",&n);
31     get_phi();
32     ll ans = 0;
33     rep(i,1,cnt) ans += (sum[n/pri[i]] << 1) - 1;
34     printf("%lld\n",ans);
35     return 0;
36 }
View Code

 

posted on 2016-01-24 20:29  ACMICPC  阅读(198)  评论(0编辑  收藏  举报

导航