## 12444 - Bits and Pieces

Moderator: Board moderators

### 12444 - Bits and Pieces

The sample output:
Code: Select all
`2 3-17 11`

I got WA, but I think the following answer should also be valid. Am I right?
Code: Select all
`2 3-111 15`
annhy
New poster

Posts: 40
Joined: Sun May 27, 2007 1:42 am
Location: Taiwan

### Re: 12444 - Bits and Pieces

For the third case, c=3 and d=15. The correct answer is a=7 and b=11. For your output of a=11 and b=15, a&b=11 which doesn't equal c.
brianfry713
Guru

Posts: 1761
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 12444 - Bits and Pieces

brianfry713 wrote:For the third case, c=3 and d=15. The correct answer is a=7 and b=11. For your output of a=11 and b=15, a&b=11 which doesn't equal c.

Now I see there is at most one valid solution.
Thanks very much. I made a very silly mistake.
annhy
New poster

Posts: 40
Joined: Sun May 27, 2007 1:42 am
Location: Taiwan

### Re: 12444 - Bits and Pieces

I am getting TLE. Please, anyone will give me some tricky tricks so that I can handle the time for this code:
Code: Select all
`#include <cstdio>#include <cmath>#include <algorithm>#include <cctype>#include <iostream>using namespace std;int main(){    int T;    scanf("%d",&T);    while(T--)    {        long long c, d, a, b, andd, orr, diff=0, MIN=2147483648;        cin>>c>>d;        int fag=0, maxx=max(c,d);        for(int i=maxx ; i>=0 ; i--)        {            for(int j=maxx ; j>=0 ; j--)            {                andd= i&j;                orr = i|j;                if(c==andd && d==orr)                {                    diff=j-i;                    if(diff<0) diff=diff*(-1);                    if(diff<MIN){                        a=i; b=j;                        MIN=diff;                    }                    fag=1;                }            }        }        if(a>b){            long long tmp=a;            a=b;            b=tmp;        }        if(fag==0)            printf("-1\n");        else            cout<<a<<" "<<b<<endl;    }    return 0;}`
uvasarker
Learning poster

Posts: 96
Joined: Tue Jul 19, 2011 12:19 pm

### Re: 12444 - Bits and Pieces

Think bitwise, not as whole ints.
brianfry713
Guru

Posts: 1761
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA