bzoj2818

数论题怎么都长得差不多。。。

这里需要对欧拉函数做一下啊前缀和

题解orzlsj:

要求gcd(x, y) = p (1 <= x, y <= n, p为质数 ) 的数对(x, y)个数.我们枚举素数p, 令x' = x / p, y' = y / p, 则只须求  f(p) = gcd(x', y') = 1的数对(x', y')个数(1 <= x', y' <= n / p), 显然f(p) = (∑ phi(x')) * 2 - 1(1 <= x' <= n / p). 所以最后答案为 ∑f(p) 

 1 #include<bits/stdc++.h>
 2 #define clr(a,x) memset(a,x,sizeof(a))
 3 #define rep(i,l,r) for(int i=l;i<r;i++)
 4 typedef long long ll;
 5 using namespace std;
 6 int read()
 7 {
 8     char c=getchar();
 9     int ans=0,f=1;
10     while(!isdigit(c)){
11         if(c=='-') f=-1;
12         c=getchar();
13     }
14     while(isdigit(c)){
15         ans=ans*10+c-'0';
16         c=getchar();
17     }
18     return ans*f;
19 }
20 const int maxn=10000009;
21 bool p[maxn];
22 ll f[maxn];
23 int s[maxn],cnt,n;
24 void getphi()
25 {
26     rep(i,1,n+1) f[i]=i;
27     rep(i,2,n+1){
28         if(f[i]==i){
29             for(int j=i;j<=n;j+=i){
30                 f[j]=f[j]/i*(i-1);
31             }
32         }
33     }
34     rep(i,2,n+1) f[i]+=f[i-1];
35 }
36 void getsushu()
37 {
38     clr(p,-1);
39     p[1]=0;
40     rep(i,2,(n>>1)+1){
41         if(p[i]){
42             for(int j=i<<1;j<=n;j+=i){
43                 p[j]=0;
44             }
45         }
46     }
47     rep(i,2,n+1){
48         if(p[i]){
49             s[cnt++]=i;
50         }
51     }
52 }
53 int main()
54 {    
55     n=read();
56     getphi();
57     getsushu();
58     ll ans=0;
59     rep(i,0,cnt){
60         ans+=f[n/s[i]]*2-1;
61     }
62     printf("%lld
",ans);
63     return 0;
64 }
View Code

2818: Gcd

Time Limit: 10 Sec  Memory Limit: 256 MB
Submit: 2453  Solved: 1087
[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]
原文地址:https://www.cnblogs.com/chensiang/p/4682706.html