Monday, August 15, 2016

Uva 11933: Splitting Numbers

Problem Link:
                      [https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=3084&mosmsg=Submission+received+with+ID+17843361]

Catagory :Basic Data structure, Bit manipulation

Strategy: Count the number of 1 in binary representation of input n. Take an integer ans=0.Suppose , we have kth 1 in n. If k is odd and in jth position of n, then put this 1  in jth position of ans.

            n -> 6 -> 1(2nd 1) 1(1st 1) 0
           ans ->      000
           ans ->      01( 1st 1 of n in 2nd position) 0

then minus ans from n which is the 2nd output.


Code:

#include<bits/stdc++.h>
#define ll   unsigned long long int
using namespace std;
ll n,n1,n2,ans2;
vector<ll>ans;
int main()
{
/*ifstream infile;
     infile.open("A-large.in");
     //freopen("output_file_name.out","w",stdout);
      ofstream outfile;
   outfile.open("output_file_name.out");*/
while(cin>>n)
{
          if(!n)
             break;
           n1=1;
           n2=0;
           ll s=0;
           while(n1<=n)
           {          if((n & n1))
                       {    n2++;
                                 if(n2 % 2)
                                 s+=n1;}
                     n1=n1<<1;

           }
           cout<<s<<" "<<n-s<<endl;
}



}


No comments:

Post a Comment