Luogu2568 GCD

題目連接:Linkc++

 

### 題意函數

給定整數N,求 $1<=x,y<=N$ 且 $Gcd(x,y)$ 爲素數的數對 $(x,y)$ 有多少對. (這個就是題面233333)spa

 

### 題解code

~~很水的一道數論題~~blog

由於一個有序數對 $(x,y)$ 的 $GCD$ 若要是一個質數,那麼必然的,$x, y$ 中必有一個是質數,不然不存在 $GCD$ 是質數的可能(自行思考,很簡單)。get

那麼對於每個質數 $p$ ,它的答案貢獻就是 $(1$ ~ $\frac{n}{p}) $it

對 $y$ 的取值範圍進行討論,當 $y = x $ 時, 有且僅有 $ y = x = 1$ 時 $x | y$class

​ 當 $y > x$ 時, 答案顯然爲$\varphi (y)$im

最後的答案爲因此有序互質對的個數爲 $(1$ ~ $\frac{n}{p}) $ 的歐拉函數之和乘2減1(要求的是有序互質對,由於有正反因此乘2之後減去(1, 1)多算的一次)di

 

### Code

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 
 6 const int SIZE = 1e7 + 5;
 7 
 8 int n, p, tot;
 9 int phi[SIZE], prime[SIZE], vis[SIZE];
10 LL ans, sum[SIZE];
11 
12 inline void Euler()
13 {
14     phi[1] = 1;
15     for (int i = 2; i <= n; i++)
16     {
17         if (!vis[i]) { phi[i] = i - 1; prime[++tot] = i; }
18         for (int j = 1; j <= tot; j++)
19         {
20             int x = prime[j];
21             if (i * x > n) break;
22             vis[i * x] = 1;
23             if (i % x == 0) { phi[i * x] = phi[i] * x; break; }
24             else phi[i * x] = phi[i] * phi[x];
25         }
26     }
27 }
28 
29 int main()
30 {
31     scanf("%d", &n);
32     Euler();
33     for (int i = 1; i <= n; i++)
34         sum[i] = sum[i - 1] + phi[i];
35     for (int i = 1; i <= tot; i++)
36         ans += sum[n / prime[i]] * 2 - 1;
37     printf("%lld", ans);
38     return 0;
39 }
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息