12444 - Bits and Pieces

All about problems in Volume CXXIV. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

12444 - Bits and Pieces

Postby annhy » Mon Aug 06, 2012 11:20 am

The sample output:
Code: Select all
2 3
-1
7 11

I got WA, but I think the following answer should also be valid. Am I right?
Code: Select all
2 3
-1
11 15
annhy
New poster
 
Posts: 40
Joined: Sun May 27, 2007 1:42 am
Location: Taiwan

Re: 12444 - Bits and Pieces

Postby brianfry713 » Tue Aug 07, 2012 1:25 am

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

Postby annhy » Tue Aug 07, 2012 6:07 am

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. :oops:
annhy
New poster
 
Posts: 40
Joined: Sun May 27, 2007 1:42 am
Location: Taiwan

Re: 12444 - Bits and Pieces

Postby uvasarker » Mon Aug 20, 2012 9:15 am

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
Location: Dhaka, Bangladesh

Re: 12444 - Bits and Pieces

Postby brianfry713 » Mon Aug 20, 2012 10:58 pm

Think bitwise, not as whole ints.
brianfry713
Guru
 
Posts: 1761
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA


Return to Volume CXXIV

Who is online

Users browsing this forum: No registered users and 1 guest