Thursday, May 03, 2012

[UVa] 12444 - Bits & Pieces


Method: Bitmasking, Bit manipulation
Trick: Analyze the problem a bit with the given Samples.
When for a particular bit
C = 1 & D = 1, obviously both the numbers have to have 1 in that position
C = 0 & D = 1, if this case occurs for the first time make the bit in B = 1 & A = 0, otherwise B = 0 & A = 1. This ensures |B-A] Minimality & A<=B.
No other combination is possible.
/* Faith-M */

//Headers
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <climits>
#include <clocale>
//Defines
#define pow2(i) (1<<i)
#define bit(i) (1<<i)
#define isOdd(i) (i&1)
#define isEven(i) (!(i&1))
#define isPrime(i) ((i==2) || ((i&1) && !pTest[i])) //pTest has to be the bool array's name
#define sz(i) i.size()
#define vec(type,name) vector< type > name
#define rep(i,a,b) for(int i=a ; i<=b ; i++)
#define swap(type,a,b) {type t=a; a=b; b=t;}
#define sum(a,n) ( (n*(n+1)/2) - (a-1)*a/2 )
#define iscap(i) (i>='A'&&i<='Z')
#define issmall(i) (i>='a'&&i<='z')
#define isnum(i) (i>='0'&&i<='9')
#define issymbol(i) (!(i>='a'&&i<='z') && !(i>='A'&&i<='Z') && !(i>='0'&&i<='9'))
#define mk(i,j) make_pair(i,j)
#define ERROR 1e-11
//Type Defs
typedef long long lint;
typedef unsigned long long ulint;
typedef long double ldouble;

using namespace std;

pair <lint, lint> find( lint c, lint d )
{
    lint a=0, b=0, bit_c, bit_d;
    bool fbit=true;
    int i;

    for (i=31 ; i>=0 ; i--)
    {
        if ((1<<i)&c) bit_c = 1;
        else bit_c = 0;

        if ((1<<i)&d) bit_d = 1;
        else bit_d = 0;

        if (bit_c == 1 && bit_d == 1)
        {
            a = a | (1<<i);
            b = b | (1<<i);
        } else if (bit_c == 0 && bit_d == 1)
        {
            if (fbit == true)
            {
                b = b | (1<<i);
            } else
            {
                a = a | (1<<i);
            }
            fbit = false;
        }
    }

    if ( (a&b) == c && (a|b) == d ) return make_pair(a,b);
    else return make_pair(-1,-1);

}

int main()
{
 //freopen("input.txt","r+",stdin);
 //freopen("output.txt","w+",stdout);/**/

 //    TEST CASE     //
 int kase=1, kounter=1;/**/

 lint c, d;

    scanf("%d",&kase);
    while (kase--)
    {
        cin >> c >> d;

        pair <lint, lint> res = find(c,d);

        if (res.first == -1) printf("-1\n");
        else cout << res.first << " " << res.second << endl;
    }


 return 0;
}

No comments:

Post a Comment

Post your comment here. If you want to say something about programming problems, scripts, software etc, please try to be as descriptive as possible.

Connect Rapoo MT750S with Linux (Tested on Manjaro)

 I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...