Advertisement
Guest User

Co-Prime Enemy Pair

a guest
Nov 20th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef unsigned long long ull;
  5. #define INF 9223372036854775806
  6. #define pb push_back
  7. #define mp make_pair
  8. #define MOD 1000000007
  9. #define PI 2*acos(0.0)
  10. ll mod(ll B,ll M){ll X=B%M; if(X>=0) return X; else return M+X;}
  11. ///cin.ignore();
  12. ll max(ll a,ll b) {if(a>b) return a; else return b;} ll min(ll a,ll b) {if(a<b) return a; else return b;}
  13. ll fx4[]={1,-1,0,0}; ll fy4[]={0,0,1,-1};
  14. const ll maxx=10000000;
  15. int status[(maxx>>5)+2];
  16.  
  17. bool check(int N,int pos)
  18. {
  19.     return (bool)(N & (1<<pos));
  20. }
  21. int Set(int N, int pos)
  22. {
  23.     return N=(N|(1<<pos));
  24. }
  25. vector<ll>Primes;
  26. void sieve()
  27. {
  28.     ll i,j,sqrtN;
  29.     sqrtN=ll(sqrt(maxx))+1;
  30.     Primes.pb(2);
  31.     for(int i=3;i<=maxx;i+=2)
  32.     {
  33.         if(check(status[i>>5],i&31)==0)
  34.         {
  35.             Primes.pb(i);
  36.  
  37.             if(i<=sqrtN)
  38.             for(j=i*i;j<=maxx;j+=(i<<1))
  39.             {
  40.                 status[j>>5]=Set(status[j>>5],j & 31);
  41.             }
  42.         }
  43.     }
  44. }
  45. ll one[1000006],cnt[1000006];
  46. ll lim=1000000;
  47. ll A[1000006],cA[1000006];
  48. void phi()
  49. {
  50.     A[0]=0;
  51.     for(ll i=1;i<=lim;i++) A[i]=i;
  52.     for(ll x:Primes){
  53.         for(ll i=x;i<=lim;i+=x){
  54.             A[i]=(A[i]*(x-1))/x;
  55.         }
  56.     }
  57. }
  58. int main()
  59. {
  60.     //freopen("C:/Users/Md Faizul Haque/Desktop/Solving/input.txt","r",stdin);
  61.     //freopen("C:/Users/Md Faizul Haque/Desktop/Solving/output.txt","w",stdout);
  62.     sieve();
  63.     phi();
  64.     ll t,n,m,x,y,w,k,q,r,p,cs;
  65.     for(auto x:Primes){
  66.         y=x;
  67.         while(y<=lim){
  68.             one[y]=1;
  69.             y=y*x;
  70.         }
  71.     }
  72.     for(int i=1;i<=lim;i++){
  73.         cnt[i]=cnt[i-1]+one[i];
  74.     }
  75.     for(int i=1;i<=lim;i++){
  76.         if(one[i]==0){
  77.             cA[i]=cA[i-1];
  78.         }
  79.         else{
  80.             cA[i]=cA[i-1]+A[i];
  81.         }
  82.     }
  83.  
  84.     cin>>t;
  85.     for(cs=1;cs<=t;cs++){
  86.         cin>>n;
  87.         ll ans=cA[n]-cnt[n];
  88.         printf("Case %lld: %lld\n",cs,ans);
  89.     }
  90.  
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement