BZOJ2818 Gcd

Topic link: https://vjudge.net/problem/HYSBZ-2818

Knowledge points: Euler function, integral function

Problem solving ideas:

  For the ordered number pair \((x,y)\), if it satisfies \(gcd(x,y)=p\) (\(p\) is a prime number), we can use \(x\) and \( y\) Divide by \(p\) at the same time, the above formula becomes \(gcd(x',y')=1\), then for a \(x\), satisfying the condition\(y\) The number is \(\varphi (x)\) . For each prime number\(p\) in \([1,N]\), we can find it by summing the Euler functions in the range \([1,N/p]\)\( The number of unordered pairs of gcd(x,y) = p\), considering that the pairs in the question are ordered, so it is necessary to multiply the unordered pairs by two. Another point is that special handling is required when both \(x\) and \(y\) are the same prime number.

  The algorithm for finding the value of the Euler function of the first \(n\) numbers is mainly to use the properties of the Euler function as an integral function and a classical calculation formula of the Euler function:

  \(\varphi(n) = \prod_{i=1}^{k} (p_i-1)p_i^{c_i-1}\).

AC code:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 typedef long long LL;
 5 const int maxn = 1e7+5;
 6 bool check[maxn];
 7 int phi[maxn];
 8 int prime[maxn>>1],tot;
 9 
10 void init(int N){
11     phi[1]=1;
12     tot=0;
13     for(int i=2;i<=N;i++){
14         if(!check[i]){
15             phi[i]=i-1;
16             prime[tot++]=i;
17         }
18         for(int j=0;j<tot&&(LL)i*prime[j]<=N;j++){
19             check[i*prime[j]]=true;
20             if(i%prime[j]==0){
21                 phi[i*prime[j]]=phi[i]*prime[j];
22                 break;
23             } else{
24                 phi[i*prime[j]]=phi[i]*(prime[j]-1);
25             }
26         }
27     }
28 }
29 
30 int main(){
31     LL ans=0;
32     int N;
33     scanf("%d",&N);
34     init(N);
35 
36     for(int i=0;i<tot;i++){
37         for(int j=1;j<=N/prime[i];j++)
38             ans+=phi[j]*2;
39         ans--;
40     }
41     printf("%lld\n",ans);
42     return 0;
43 }

 

Guess you like

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