Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE
Showing posts with label implementation. Show all posts
Showing posts with label implementation. Show all posts

Thursday, October 22, 2015

BSHEEP-Build the Fence

Build the Fence 

Given below code is for bsheep spoj or build the fence spoj.

Hint:- Simple implementation of any O(N log N) algorithm for  Convex Hull. 





/*
===================================================
Name :- Nishant Raj
Email :- raj.nishant360@gmail.com
College :- Indian School of Mines
Branch :- Computer Science and Engineering
Time :- 22 October 2015 (Thursday) 09:33
===================================================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define mod 1000000009
class point{
public:
    int x , y , pos;
    point(){};
    point(int a , int b , int c){x = a , y = b , pos = c;}
    double distance(point &a){
        int dx = a.x - x;
        int dy = a.y - y;
        return sqrt(dx*dx + dy*dy);
    }
    bool operator != (point &a){
        if(a.x != x || a.y != y)
            return true;
        return false;
    }
};
bool cmp(const point &a , const point &b){
    return a.y == b.y ? (a.x == b.x ? a.pos < b.pos : a.x < b.x ): a.y < b.y;
}
int orientation( point P , point Q , point R ){
    return  ( Q.x - P.x ) * ( R.y - P.y ) - ( R.x - P.x ) * ( Q.y - P.y );
}
void convex_hull(vector<point> &p , vector<point> &upper , vector<point> &lower){
    sort(p.begin() , p.end() , cmp);
    int k = 1;
    for(int i = 1 ; i < p.size() ; i++)
        if(p[i-1] != p[i])
            p[k++] = p[i];
    p.resize(k);
    int sz = 2*k;
    upper.resize(sz);
    lower.resize(sz);
    int j = 0 ;
    k = 0;
    for(int i = 0 ; i < p.size() ; i++){
        while(j >= 2 && orientation(lower[j-2] , lower[j-1] , p[i]) <= 0)
            j--;
        while(k >=2 && orientation(upper[k-2] , upper[k-1] , p[i]) >= 0)
            k--;
        lower[j++] = p[i];
        upper[k++] = p[i];
    }
    lower.resize(j);
    upper.resize(k);
}
int main(){
    
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        vector<point> v;
        int x , y;
        for(int i = 0 ; i < n ; i++){
            scanf("%d%d",&x , &y);
            v.pb(point(x , y , i+1));
        }
        vector<point> lower , upper;
        convex_hull(v , upper , lower);
        for(int i = upper.size() - 2 ; i > 0 ; i--)
            lower.pb(upper[i]);
        double dist = 0.0;
        for(int i = 1 ; i < lower.size() ; i++)
            dist += lower[i-1].distance(lower[i]);
        dist += lower[lower.size() -1].distance(lower[0]);
        printf("%.2lf\n", dist);
        for(int i = 0 ; i < lower.size() ; i++)
            printf("%d ", lower[i].pos);
        printf("\n");
    }
    
    return 0;
}

Monday, October 19, 2015

INS14A-BSTRING

BSTRING

Given below c++ code is for ins14a spoj or bstring spoj.

Hint :- Think of bringing all one's to it median in windows of 'm' ones.








/*
===================================================
Name :- Nishant Raj
Email :- raj.nishant360@gmail.com
College :- Indian School of Mines
Branch :- Computer Science and Engineering
Time :- 18 October 2015 (Sunday) 22:05
===================================================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define mod 1000000009

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        char s[50009];
        ll m;
        scanf("%lld %s" , &m ,s);
        int n = strlen(s);
        vector<ll> position;
        position.pb(0);
        for(int i = 0 ; i < n ; i++)
            if(s[i] == 49)
                position.pb(i+1);
        ll median = m/2 + 1;
        ll left = 0 , right = 0;
        for(int i = 0 ; i < median ; i++)
            left += position[i];
        for(int i = median + 1 ; i<=m ; i++)
            right += position[i];
        ll ans = right - left + (2*median - m - 1)*position[median];
        int var_median = median;
        for(int i = m+1 , j = 1 ; i < position.size() ;j++, i++){
            left -= position[j];
            left += position[var_median];
            var_median++;
            right -= position[var_median];
            right += position[i];
            ll temp = right - left + (2*median - m - 1)*position[var_median];
            ans = min(ans , temp);
        }
        ans -= (m*m + 2*median*(median - 1) - 2*m*median + m)/2;
        printf("%lld\n", ans);
    }
    return 0;
}

Sunday, June 14, 2015

HASHIT-Hash it!

Hash it!

Given below c++ code for HASHIT spoj or Hash it! spoj.



#include <bits/stdc++.h>
using namespace std;
long long hash_value(string a)
{
    long long h=0;
    for(long long i=0; i<a.size(); i++)
    {
        h+=(i+1)*(long long)a[i];
    }
    h=h*19;
    return h%101;
}
vector<string> hash_table[101];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int q;
        scanf("%d",&q);
        START:
        while(q--)
        {
            string a;
            cin>>a;
            string x(a.begin()+4,a.end());
            if(a[0]=='A')
            {
                for(int i = 0 ; i < 101 ; i++)
                {
                    if(!hash_table[i].empty()&&hash_table[i][0] == x)
                        goto START;
                }
                int h=hash_value(x);
                for(int j=0; j<20; j++)
                {
                    int idx=(h+j*j+23*j)%101;
                    if(hash_table[idx].empty())
                    {
                        hash_table[idx].push_back(x);
                        break;
                    }
                }
            }
            else
            {
                int h=hash_value(x);
                for(int j=0; j<20; j++)
                {
                    int idx=(h+j*j+23*j)%101;
                    if(!hash_table[idx].empty()&&hash_table[idx][0]==x)
                    {
                        hash_table[idx].pop_back();
                        break;
                    }
                }
            }
        }
        int c=0;
        for(int i=0; i<101; i++)
            c+=hash_table[i].size();
        printf("%d\n",c);
        for(int i=0; i<101; i++)
        {
            if(!hash_table[i].empty())
                printf("%d:%s\n",i,hash_table[i][0].c_str());
        }
        for(int i=0; i<101; i++)
            hash_table[i].clear();
    }
    return 0;
}

Wednesday, January 14, 2015

DONALDO-DONALDO

DONALDO

Given below c/c++ code for donaldo spoj .


An easy question . Here you can use queue for solving the question . In queue start pushing time till difference b/w first and last element is less then given time I (in question ) . After that calculate the size of queue that will give you the number of girlfriend possible in that time interval. Now after that remove first element from the queue. and repeat this process till all time stamp are not consider .
For the case if there are more time stamp with same value then remove all element from queue at a time.




#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    for(int test = 1 ; test<= t; test++){
        int hh , mm , tt , I;
        int n;
        scanf("%d",&n);
        vector<long long > v;
        long long temp;
        char a;
        for(int i = 0 ; i < n ; i++){
            scanf("%d%c%d%c%d",&hh,&a ,&mm,&a,&tt);
            temp = hh*3600 + mm*60 + tt;
            v.push_back(temp);
        }
        scanf("%d",&I);
        if(n==0){
            printf("Case %d: 0\n",test);
            continue;
        }
        sort(v.begin() , v.end());
        queue<long long> q;
        int j = 0 , len = -1;
        q.push(v[j]);
        while(j<n){
            while(j< n && v[j] - q.front() < I){
                j++;
                q.push(v[j]);
            }
            int size = q.size() - 1;
            len = max(len , size);
            int temp = q.front();
            q.pop();
            while(!q.empty() && temp == q.front())
                q.pop();
        }
        printf("Case %d: %d\n",test ,len);
    }
}

Tuesday, December 30, 2014

OPSL-Operation Searchlight

Operation Searchlight

Given below c++ code is for OPSL spoj or Operation Searchlight spoj .

This is an easy problem & should be in tutorial class , here you should know sieve and how to calculate LCM of numbers.



Thursday, December 25, 2014

DEADFR-Dead Fraction

Dead Fraction

Below given code is for deadfr spoj or dead fraction spoj.

Source : - http://www.basic-mathematics.com/converting-repeating-decimals-to-fractions.html

How this problem can be solved :- 

Step 1:

Let x equal the repeating decimal you are trying to convert to a fraction

Step 2:

Examine the repeating decimal to find the repeating digit(s)

Step 3:

Place the repeating digit(s) to the left of the decimal point

Step 4:

Place the repeating digit(s) to the right of the decimal point

Step 5:

Subtract the left sides of the two equations.Then, subtract the right sides of the two equations

As you subtract, just make sure that the difference is positive for both sides

Now let's practice converting repeating decimals to fractions with two good examples

Example #1:

What rational number or fraction is equal to 0.55555555555

Step 1:

x = 0.5555555555 

Step 2:

After examination, the repeating digit is 5

Step 3:

To place the repeating digit ( 5 ) to the left of the decimal point, you need to move the decimal point 1 place to the right

repeating-decimals-image


Technically, moving a decimal point one place to the right is done by multiplying the decimal number by 10.

When you multiply one side by a number, you have to multiply the other side by the same number to keep the equation balanced

Thus, 10x = 5.555555555

Step 4:

Place the repeating digit(s) to the right of the decimal point

Look at the equation in step 1 again. In this example, the repeating digit is already to the right, so there is nothing else to do.

x = 0.5555555555

Step 5:

Your two equations are:

10x = 5.555555555

   x = 0.5555555555

10x - x = 5.555555555 − 0.555555555555

9x = 5

Divide both sides by 9

x = 5/9


What you have to consider in problem ?

1. If given fraction is 0.2154... then
        Case 1-  '4' can be repeating 
        Case 2- '54' can be repeating 
        Case 3- '154' can be repeating 
        Case 4- '2154' can also be repeating  
So you have to check for all case and print the one with lowest denominator.

2. If ans is '1' then print "1/1" .

Saturday, November 29, 2014

TETRASUM-Sum of Tetranacci numbers

Sum of Tetranacci numbers

Given below code is for TETRASUM spoj sum of tetranacci numbers spoj.


#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define gc getchar_unlocked
class matrix
{
public:
    long long m[4][4];
};
matrix multiply(matrix a , matrix b)
{
    matrix temp;
    for(int i = 0 ; i < 4 ; i++)
        for(int j = 0 ; j < 4 ; j++)
        {
            temp.m[i][j] = 0;
            for(int k = 0 ; k < 4 ; k++){
                temp.m[i][j] = (temp.m[i][j] + a.m[i][k] *b.m[k][j]);
                if(temp.m[i][j] >= MOD) temp.m[i][j] %= MOD;
            }
        }
    return temp;
}
matrix arr[32];
void pre()
{
    arr[0].m[0][0] = 0 ; arr[0].m[0][1] = 0 ; arr[0].m[0][2]  = 0 ; arr[0].m[0][3] = 1;
    arr[0].m[1][0] = 1 ; arr[0].m[1][1] = 0 ; arr[0].m[1][2]  = 0 ; arr[0].m[1][3] = 1;
    arr[0].m[2][0] = 0 ; arr[0].m[2][1] = 1 ; arr[0].m[2][2]  = 0 ; arr[0].m[2][3] = 1;
    arr[0].m[3][0] = 0 ; arr[0].m[3][1] = 0 ; arr[0].m[3][2]  = 1 ; arr[0].m[3][3] = 1;
    matrix temp;
    for(int pos = 1 ; pos <= 29 ; pos++){
        for(int i = 0 ; i < 4 ; i++)
            for(int j = 0 ; j < 4 ; j++)
            {
                arr[pos].m[i][j] = 0;
                for(int k = 0 ; k < 4 ; k++){
                    arr[pos].m[i][j] = (arr[pos].m[i][j] + arr[pos-1].m[i][k] *arr[pos-1].m[k][j]);
                    if(arr[pos].m[i][j] >= MOD) arr[pos].m[i][j] %= MOD;
                }
            }
    }
}
matrix nth_Term(int n)
{
    int count = 0 ;
    matrix a;
    a.m[0][0] = 1 ; a.m[0][1] = 0 ; a.m[0][2] = 0 ; a.m[0][3]= 0;
    a.m[1][0] = 0 ; a.m[1][1] = 1 ; a.m[1][2] = 0 ; a.m[1][3]= 0;
    a.m[2][0] = 0 ; a.m[2][1] = 0 ; a.m[2][2] = 1 ; a.m[2][3]= 0;
    a.m[3][0] = 0 ; a.m[3][1] = 0 ; a.m[3][2] = 0 ; a.m[3][3]= 1;
    if(n<=2)
        return a;
    n-=2;
    while(n)
    {
        if(n&1){
            a = multiply(a,arr[count]);
        }
        count++;
        n>>=1;
    }
    return a;
}
int main()
{
    int t ;
    pre();
    scanf("%d",&t);
    matrix temp;
    long long res1 = 0 , res2 , n_1 , n_t , n_2 ;
    int m , n , res;
    long long inv = 333333336;
    while(t--)
    {
        scanf("%d%d", &m , &n);
        if(n == m)
        {
            temp = nth_Term(n);
            printf("%lld\n" , temp.m[3][2]);
            continue;
        }
        if(n<=2)
            res1 = 0;
        else
        {
            temp = nth_Term(n-1);
            n_1 = temp.m[3][2];
            temp = multiply(temp,arr[0]);
            n_t = temp.m[3][2];
            temp = multiply(temp,arr[1] );
            n_2 = temp.m[3][2];
            res1 = (n_2 + (n_t<<1) + n_1 -1);
            res1 = (res1*inv)%MOD;
        }
        if(m<=3)
            res2 = 0;
        else
        {
            m--;
            temp = nth_Term(m-1);
            n_1 = temp.m[3][2];
            temp = multiply(temp,arr[0]);
            n_t = temp.m[3][2];
            temp = multiply(temp,arr[1]);
            n_2 = temp.m[3][2];
            res2 = (n_2 +  (n_t<<1) + n_1 - 1 );
            res2 = (res2*inv)%MOD;
        }
        res = (res1 - res2 + MOD)%MOD;
        printf("%d\n",res);
    }
    return 0;
}

Tuesday, June 3, 2014

WAYS-PATHS

PATHS

Below given 114 byte c code is for ways spoj or paths spoj.
Here first time i was wondering that this code worked in c . I was thinking this will even not pass compilation but it was running.



f(j){return j<2?2:f(j-1)*(4*j-2)/j;}l="%d\n";main(t,m){for(scanf(l,&t);t--;scanf(l,&m),printf(l,f(m)));return 0;}

Wednesday, April 2, 2014

WPC5B-Crypto

Crypto

given below code is for wpc5b spoj or crypto spoj.
Here no need of explanation every number multiplied with itself get repeated after some interval .So here just calculate the interval.
.. Happy Coding ..
#include <bits/stdc++.h>
using namespace std;
#define MOD 10
int main()
{
 long long t;
 int temp,m;
 int res[10][5]={{0,0,0,0,1},
                 {1,1,1,1,1},
                 {6,2,4,8,4},
                 {1,3,9,7,4},
                 {6,4,0,0,2},
                 {5,0,0,0,1},
                 {6,0,0,0,1},
                 {1,7,9,3,4},
                 {6,8,4,2,4},
                 {1,9,0,0,2},
    };
 scanf("%lld",&t);
 while(t--)
 {
  long long a,b;
  scanf("%lld%lld",&a,&b);
  if(b==0){
   printf("1\n");
   continue;}
  temp = a % MOD;
  m = b%res[temp][4];
  printf("%d\n",res[temp][m]);
 }
 return 0;
}

Tuesday, April 1, 2014

INS14I-Infinite Sequence

Infinite Sequence

below given code is for ins14i spoj or infinite sequence spoj.
if you want proof of solution please mail me @ raj.nishant360@gmail.com.
#include <bits/stdc++.h>
using namespace std;
#define X (3+2*sqrt(2))
int main()
{
 int t;
 scanf("%d",&t);
 while(t--)
 {
  int n;
  scanf("%d",&n);
  if(n==0){
   printf("4\n");continue;}
  double temp,k;
  temp= ceil(n/X);
  k=floor(temp*X);
  if(k==n)
   printf("4\n");
  else
   printf("5\n");
 }
 return 0;
}

Sunday, March 30, 2014

SIRNUMS-SIR CHIRAG AND MAGIC NUMBERS

SIR CHIRAG AND MAGIC NUMBERS

given below code is for sirnums spoj or sir chirag and magic numbers spoj.
if you want logic mail me. email :- raj.nishant360@gmail.com
#include <bits/stdc++.h>
using namespace std;
int main()
{
 int t;
 scanf("%d",&t);
 while(t--){
 int k,x;
 scanf("%d%d",&k,&x);
 char a[1000009];
 int temp,carry,temp1;
 int flag=0;
 for(int i=x;i<=9;i++)
 {
  a[k-1]=i+48;
  carry=0;
  for(int j=k-2;j>=0;j--)
  {
   a[j]=((a[j+1]-48)*x + carry)%10 + 48;
   carry = ((a[j+1]-48)*x + carry)/10;
  }
  a[k]=0;
  temp=a[0]-48;
  temp1= (temp*x + carry)%10;
  carry=(temp*x + carry)/10;
  if(temp1 == i && carry==0){
   printf("%s\n",a);
   flag=1;
   break;
  }
 }
 if(!flag)
  printf("Impossible\n");
 }
 return 0;
}

Thursday, March 27, 2014

FERT21_0-Matches

Matches

below given code is for fert21_0 spoj or matches spoj.
you can make this code more faster using character array ans fast i/o;
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cfloat>
#include <map>
#include <fstream>
#include <sstream>
#include <bits/stdc++.h>
#include <climits>
using namespace std;
string arr[1009];
string multiply(string s,int a)
{
 int i,carry=0,j;
 string temp;
 char c;
 for(i=s.size() - 1;i>=0;i--)
 {
  j= s[i] - 48;
  c = (j*a + carry)%10 + 48;
  carry = (j*a + carry)/10;
  temp = c + temp;
 }
 if(carry!=0)
  c=carry+48,temp=c+temp;
 return temp;
}
void pre()
{
 arr[0] =49;
 for(int i=1;i<=1000;i++)
  arr[i] = multiply(arr[i-1],5);
}
int main()
{
 int t;
 scanf("%d",&t);
 pre();
 while(t--)
 {
  int n,l,p;
  ios_base::sync_with_stdio(false);
  scanf("%d",&n);
  if(n==1)
   cout<<"1"<<endl;
  else
  {
   cout<<"0.";
   p=arr[n-1].size();
   l= abs((n-1) - p);
   for(int i=0;i<l;i++)
    cout<<"0";
   cout<<arr[n-1]<<endl;
  }
 }
 return 0;
}

RMID-Running Median

Running Median

given below code is for rmid spoj or running median spoj.
in this code if you will use advance() function then it will give TLE so use increment/ decrement operator for iterator.
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cfloat>
#include <map>
#include <fstream>
#include <sstream>
#include <bits/stdc++.h>
#include <climits>
using namespace std;
int main()
{
 int n,count=0,pos=0,check=1,i=0;
 list<int>l;
 list<int>::iterator t,t1,t2,temp;
 t=l.begin();
 while(scanf("%d",&n)!=EOF)
 {
  if(n== -1)
  {
   printf("%d\n",(*t));
   temp=t;
   t++;
   t1=t;
   t--;
   if(t!=l.begin())
    t--;
   t2=t;
   l.erase(temp);
   if(count&1)
    t=t2;
   else
    t=t1;
   count--;
  }
  else if(n==0)
  {
   count=0;
   l.clear();
   t=l.begin();
   printf("\n");
  }
  else
  {
   l.push_back(n);
   count++;
   //cout<<count<<endl;
   if(count&1)
    t++;
  }
 }
 return 0;
}

GSSQUNCE-Sequence

Sequence

below given code is for gssqunce spoj or sequence spoj.
easy one ... no need of explanation here only check if number is repeated more then twice then it can not form sequence .
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <cstring>
#include <sstream>
#include <fstream>
#include <climits>
#include <ctime>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int main()
{
 int t;
 scanf("%d",&t);;
 while(t--)
 {
  int n;
  scanf("%d",&n);
  int a[60000],flag=1;
  map<int ,int>mp;
  map<int ,int >::iterator t;
  for(int i=0;i<n;i++)
   scanf("%d",&a[i]),mp[a[i]]+=1;
  for(t=mp.begin();t!=mp.end();t++)
   if((*t).second >2){
    flag=0;
    break;}
  if(flag==0 || n==1)
   printf("NO\n");
  else
   printf("YES\n");
 }
 return 0;
}

Friday, March 21, 2014

JOKER1-Knifes Are Fun

below given code is for jocker1 spoj or knifes are fun spoj.
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <cstring>
#include <sstream>
#include <fstream>
#include <climits>
#include <ctime>
#include <algorithm>
using namespace std;
#define MOD 1000000007
int main()
{
 int t;
 scanf("%d",&t);
 while(t--)
 {
  int n;
  scanf("%d",&n);
  int a[n+9];
  for(int i=0;i<n;i++)
   scanf("%d",&a[i]);
  sort(a,a+n);
  long long result=1,flag=1;
  for(int i=n-1;i>=0;i--)
  {
   if(a[i]-i<=0)
   {
    flag=0;
    break;
   }
   result=(result*(a[i]-i))%MOD;
  }
  if(!flag)
   printf("0\n");
  else
   printf("%lld\n",result);
 }
 printf("KILL BATMAN\n");
 return 0;
}

Thursday, March 20, 2014

QN01-XOR Game

XOR Game

below given code is for qn01 spoj or xor game spoj.
and code was given ANANT
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
 int n,start,end,tempstart,tempend;
 scanf("%d",&n);
 int array[n];
 for (int i=0; i<n; i++)
  scanf("%d",&array[i]);
 int max=-1,temp;
 for (int i=0; i<n; i++)
 {
  temp=array[i];
  tempstart=i;
  if (max<temp)
  {
   max=temp;
   start=tempstart;
   end=i;
  }
   
  for (int j=i+1; j<n; j++)
  {
   temp=temp^array[j];
   if (max<temp)
   {
    max=temp;
    start=tempstart;
    end=j;
   }
  }
 }
 printf("%d\n%d %d\n",max,start+1,end+1);
 return 0;
}

Wednesday, March 19, 2014

IITKWPCO-Create Collections

Create Collections

below given code is for iitkwpco spoj or create collections spoj.
#include <stdio.h>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
 int t;
 scanf("%d",&t);
 while(t--)
 {
  int check[1000001]={0};
  int n;
  scanf("%d",&n);
  int a[n];
  for(int i=0;i<n;i++){
   scanf("%d",&a[i]);
   check[i]=0;}
  sort(a,a+n);
  int count=0;
  for(int i=1;i<n;i++)
  {
   if(a[i]%2==0)
   {
    for(int j=0;j<n;j++)
    {
     if(a[i] == 2*a[j] && check[j]==0)
     {
      count++;
      check[i]=1;
      check[j]=1;
      break;
     }
    }
   }
  }
  printf("%d\n",count);
 }
 return 0;
}

Tuesday, March 11, 2014

GSHOP-Rama and Friends

Rama and Friends

given below code is for gshop spoj or Rama and Friends spoj
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
 int t;
 scanf("%d",&t);
 while(t--)
 {
  int n,k;
  scanf("%d%d",&n,&k);
  int count_n=0,count_z=0,count_p=0,i;
  long long sum=0,min=9999999,temp;
  int a[n+9];
  for(i=0;i<n;i++){
   scanf("%d",&a[i]);
   temp=a[i]>0?a[i]:-1*a[i];
   if(temp<min)
    min=temp;
   if(a[i]<0)
    count_n++;
  }
  if(k<=count_n)
  {
   int j=0;
   for(i=0;i<n;i++)
    if(a[i]<0 && j<k)
    {
     a[i]=-a[i];
     j++;
    }
   for(i=0;i<n;i++)
    sum+=a[i];
   printf("%lld\n",sum);
  }
  else
  {
   for(i=0;i<count_n;i++)
    a[i]=-a[i];
   for(i=0;i<n;i++){
    sum+=a[i];
   }
   if((k-count_n)%2!=0)
    sum+=-(2*min);
   printf("%lld\n",sum);
  }
 }
 return 0;
}

BTCD14A-Game of Pips

Game of Pips

below given code is for btcd14a spoj or Game of Pips spoj
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
long long GCD(long long a,long long b)
{
 return b==0?a:GCD(b,a%b);
}
int main()
{
 int n;
 scanf("%d",&n);
 long long a[n+9],k;
 scanf("%lld",&a[0]);
 int i;
 k=a[0];
 for(i=1;i<n;i++)
 {
  scanf("%lld",&a[i]);
  k=GCD(k,a[i]);
 }
 if(k%2==0)
 {
  while(k%2!=1)
   k=k/2;
  printf("%lld\n",k);
 }
 else 
  printf("%lld\n",k);
 return 0;
}

Sunday, March 9, 2014

HLP_RAMS-Topper Rama Rao

Topper Rama Rao

Below given code is for hlp_rams spoj or Topper Rama Rao
you can Read This for understanding what is logic behind this.

#include <stdio.h>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
 int t;
 scanf("%d",&t);
 while(t--)
 {
  long long num,odd=1,temp;
  scanf("%lld",&num);
  int i;
  if(num==0)
   printf("0 1\n");
  else{
   odd=1;
   temp=num;
   while(temp)
   {
    odd*=((temp & 1)+1);
    temp=temp>>1;
   }
   printf("%lld %lld\n",num+1-odd,odd);
  }
 }
 return 0;
}