Advertisement
hemel18681

How many pair gcd greater than 1

Nov 30th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define maxx 100009
  4. using namespace std;
  5.  
  6. ll phi[maxx];
  7. ll mark[maxx];
  8. ll cu_phi[maxx];
  9.  
  10.  
  11. void phi_sum(){
  12.     cu_phi[1]=1;
  13.     cu_phi[2]=1;
  14.     for(int i=2;i<=maxx;i++) {cu_phi[i]=phi[i]+cu_phi[i-1];}
  15. }
  16.  
  17. void sieve_phi(){
  18.     for(int i=1;i<=maxx;i++) phi[i]=i;
  19.     phi[1]=1;
  20.     mark[1]=1;
  21.     for(int i=2;i<=maxx;i++){
  22.         if(!mark[i]){
  23.             for(int j=i;j<=maxx;j+=i){
  24.                 mark[j]=1;
  25.                 phi[j]=phi[j] / i*(i-1);
  26.             }
  27.         }
  28.     }
  29. }
  30.  
  31. int main()
  32. {
  33.     sieve_phi();
  34.     phi_sum();
  35.     int t,cas=0;
  36.     cin>>t;
  37.     while(t--){
  38.         ll x;
  39.         cin>>x;
  40.         cout<<"Case "<<++cas<<": "<<((x*(x+1))/2)-cu_phi[x]<<endl;
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement