Posts

Showing posts from October, 2016

FRIENDS OF FRIENDS SPOJ SOLUTION

 "FACEFRND" SPOJ SOLUTION #include<bits/stdc++.h> #include<set> #include<vector> using namespace std; int main() {  set<int> s;  vector<int> v;  int n,i;  cin>>n;  while(n--) {  int x;  cin>>x;  v.push_back(x);  int m;  cin>>m;  while(m--)   {   cin>>x;   s.insert(x);   }  } for(i=0;i<v.size();i++) s.erase(v[i]); cout<<s.size(); return 0; }

ADDING REVERSED NUMBERS SPOJ SOLUTION

*/ "ADDREV" SPOJ SOLUTION */ #include<bits/stdc++.h> using namespace std; int rev(int x) {  int y=0,z;  while(x>=1)  {  z=x%10;  y=y*10+z;  x/=10; } return (y); } int main() {int t; cin>>t; while(t--) { int x,y,z; cin>>x>>y; z=rev(x)+rev(y); cout<<rev(z)<<"\n"; } return 0; }

RECTANGLES SPOJ SOLUTION

*/ "AE00" SPOJ SOLUTION */ #include<bits/stdc++.h> using namespace std; int main() { int i,j,c=0; int n; cin>>n; for(i=2;i<=sqrt(n);i++)  {for(j=i;(j*i)<=n;j++)       c++;} cout<<n+c; return 0; }

EULER TOTIENT FUNCTION SPOJ SOLUTION

/*EULER TOTIENT FUNCTION SPOJ SOLUTION*/ #include<bits/stdc++.h> using namespace std; unsigned long long int etf(unsigned long long int n) { if(n==1)  return 1;        unsigned long long int ans=1,i;        for(i=2;i*i<=n;i++)        {          if(!(n%i))           {           ans*=(i-1);           n/=i;           while(!(n%i)){ ans*=i;n/=i;}          if(n==1)           return (ans);            }        }        return(ans*(n-1)); } int main() {  int t;  cin>>t;  while(t--)  {   unsigned long long int n;   cin>>n;   cout<<etf(n)<<"\n";  } return 0; }

TDPRIMES SPOJ SOLUTION

/* PRINTING SOME PRIMES SPOJ SOLUTION*/ #include<bits/stdc++.h> using namespace std; vector <bool> v(100000000,true); int arr[8000000]; int main() {     long long int n = 100000000;     long int i,j ;     v[1] = false;     v[0] = false;     for(i=4;i<=n;i+=2)      v[i]=false;       for(i=3;i*i<=n;i+=2)      {            if(v[i])              {                 for(j=i*i;j<=n;j=j+(2*i))                 v[j] = false;              }      }        j=0;        arr[0] = 2;        for(i=3;i<=n;i+=2)       {         if(v[i])         arr[++j]=i;        }        for(i=1;i<=j;i+=100)       cout<<arr[i-1]<<"\n";       return 0;    }

TDKPRIME SPOJ SOLUTION

/* FINDING THE KTH PRIME SPOJ SOLUTION*/ #include<bits/stdc++.h> using namespace std; vector <bool> v(100000000,true); int arr[8000000]; int main() {     long long int n = 100000000;     long int i,j ;     v[1] = false;     v[0] = false;     for(i=4;i<=n;i+=2)      v[i]=false;     for(i=3;i*i<=n;i+=2)     {         if(v[i])         {             for(j=i*i;j<=n;j=j+(2*i))                 v[j] = false;         }     }          j=0;          arr[0] = 2;          for(i=3;i<=n;i+=2)           {             if(v[i])             arr[++j]=i;           }          long long int t;          cin>>t;          while(t--)           {              long long int n;              cin>>n;               cout<<arr[n-1]<<"\n";           }             return 0;       }

RAMA AND FRIEND SPOJ SOLUTION

 /* RAMA AND FRIEND SOLUTION */ #include<bits/stdc++.h>                                                 using namespace std; int main() { int t,i; cin>>t; while(t--) { int n,s=0,k; cin>>n>>k; int a[n]; for(i=0;i<n;i++) cin>>a[i]; while(k--) { a[0]*=(-1); sort(a,a+n); } for(i=0;i<n;i++) {s+=a[i];} cout<<s<<"\n"; } return 0; }