Page 2 of 2

Posted: Sat Sep 25, 2004 9:09 pm
by Krzysztof Duleba
Try this case:
2345515 3444441325 3444441325

o...

Posted: Sun Sep 26, 2004 1:58 am
by zzzzzddddd
my program outputs 2345515!!!!
thanks u very much.

Posted: Fri Jul 29, 2005 4:55 am
by james299
hi ! :)
I got WA, and i can't figure it out.

1. I translate N,L,U to binary bit.
2. I use a temp array to store the reslut.
3. First, I copy the data from U to temp, and from leading bit to decide following two case.
if(U==0&&N==0)

I check if I changed the value and it wouldn't exceed the limit(up).

else if(U==1&&N==1)

This part is a little bit complicated for I to explain.
I try to minus 1 for this case ,so I change the value to 0 , and set all rest bits to 1. And then I check it wouldn't exceed the limit(down).
If it's smaller than the lower bound I reset the data as original one.
It starts from the largest one toward smaller one, so I don't have to handle the case

Code: Select all

U[i]==1&&N[i]==0
U[i]==0&&N[i]==1
for it has been the maxium we wanted.

if my statement can't help you to help me, just give me some critical I/O.
As Krzysztof Duleba posted my output :)

Code: Select all

9
4
17
435
905
625
409
712
14721
23460
29462
19554
17216
382924
237589
257219
234343
499600
14223127
16692359
13133233
19429997
10902496
957429345
1305069817
1880088222
704659827
1542920241
I use unsigned int to store data input and use << to pick out every bit and store in a int array.
As the problem statement

Code: Select all

given a 32-bit unsigned integer
I don't think there is any input over 2147483648.
:-?

Thanks for your precious time. :P

Posted: Sat Jul 30, 2005 6:14 pm
by james299
I find what's wrong in my code. And AC now :D .

My judger for upper and lower limits are both wrong when difference is one.

Input

Code: Select all

127 50 60
My output

Code: Select all

51
But should be

Code: Select all

50
:wink: Good luck!

10718 WA

Posted: Mon Jul 03, 2006 8:54 pm
by predator
hi i am getting WA in this problem. now i checked my code with the sample given in the other post .. they don't match but i dont know whats wrong with my code or algo. can anyone check it plz?

Code: Select all

unsigned int L,U,N;


unsigned int get_M();

int main()
{
	while(1)
	{
		cin>>N>>L>>U;
		if(cin.eof())	break;
		cout<<get_M()<<endl;
	}


	return 0;
}

unsigned int get_M()
{
	unsigned int M,t;
	int i;

	for(M=L,i=31;i>=0;i--)
	{
		if(!(N&(1<<i))) //if the ith bit is OFF
		{
			t=M|(1<<i);	//turn ON the ith bit of M
			
			if(t<=U)
				M=t;
		}
	}

	return M;
}

Re: 10718 WA

Posted: Sun Jul 23, 2006 10:35 pm
by Martin Macko
predator wrote:hi i am getting WA in this problem. now i checked my code with the sample given in the other post .. they don't match but i dont know whats wrong with my code or algo. can anyone check it plz?
Consider using (1u<<31) instead of (1<<31).

Re: 10718 - Bit Mask

Posted: Wed Aug 17, 2011 7:14 pm
by CSGrandeur
Try these test cases
??????
inputs

Code: Select all

2709727097 1717317173 3015630156
1142911429 2225322253 3015630156
3083430834 3060830608 3230932309
2832328323 1288712887 3230932309
1390913909 1367713677 3230932309
2150721507 53335333 3230932309
21072107 2429024290 3230932309
3274332743 2447124471 3230932309
2660126601 1153311533 3265532655
outputs

Code: Select all

2122111110
3013643834
3224802381
1462638972
2904053386
2144245788
3230930708
3168118200
1634840694

Re: 10718 - Bit Mask

Posted: Tue Jun 05, 2012 12:49 am
by sadia_atique
I'm getting WA in this problem..What I did is setting ans initially to zero..then checking every bit starting from MSB,if its zero,then set that bit in "ans",and check whether the new ans is smaller than upper limit,if yes,then set that bit,otherwise didn't set.When 1 found in any bit,made that bit 0 in ans and every smaller bit 1,then check whether this new value is greater or equals to lower limit,if yes,then set that bit to zero,otherwise one.I think there might be problem in handling long or unsigned..my algo seems ok,don't know whats wrong..someone please help,what's wrong with my code? :cry: :cry:

Code: Select all

AC

Re: 10718 - Bit Mask

Posted: Tue Jun 05, 2012 1:52 pm
by sadia_atique
Can anyone help??I'm getting WA in this problem...please help :cry:

Re: 10718 - Bit Mask

Posted: Tue Jun 05, 2012 9:27 pm
by brianfry713
Try the I/O CSGrandeur posted.

Re: 10718 - Bit Mask

Posted: Wed Jun 06, 2012 5:25 pm
by sadia_atique
Thanks,Got AC..a stupid mistake in loop..

Re: 10718 - Bit Mask

Posted: Fri Mar 08, 2013 7:19 pm
by AKJ88
I used this algorithm for this question:
I start with a mask equal to 0. I find the MSB in U then try to set corresponding bit in the mask 1 in the condition that 1) the result is less than U
and 2) if this bit is 0 in N or it's 1 but if we don't set this bit, with the remaining bits we can't produce a number which is greater than or equal to L.
Can anyone tell me what's is wrong with this algorithm??? because I keep getting WA!:-(
Here is my code: (I know it produces wrong result for 2 cases in the sample input provided in this forum but I can't understand why!)
Thanks.

Code: Select all

 Algorithm is correct it was because of a simple mistake in implementation that I'd got WA. 

Re: 10718 - Bit Mask

Posted: Wed Mar 20, 2013 8:48 pm
by tiendaotd
I got AC with the following "greedy" method, but I dont know why it works : assigned M = U and M = L . For each value of M , I check every bit position from 32 down to 0 if I can turn it on or turn it off. I can turn it on if the current bit in N is off , and by turned on the value of M still smaller than U . I can turn it off if the current bit in N is on , and by turned off the value of M still bigger than L . Then I simply compare the value of N^M in each case and get the result.

Re: 10718 - Bit Mask

Posted: Sun Aug 31, 2014 5:47 pm
by martijnn2008

Code: Select all

import java.util.*;

public class BitMask {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            solve(sc.nextLong(), sc.nextLong(), sc.nextLong());
        }
        sc.close();
    }

    private static void solve(long N, long L, long U) {
        long M = 0;
        for (int i = 31; i >= 0; --i) {
            if ((N & 1L << i) == 0) {
                if (M + (1L << i) <= U) {
                    M += 1L << i;
                }
            } else {
                if (M + (1L << i) - 1 < L) {
                    M += 1L << i;
                }
            }
        }
        System.out.println(M);
    }
}
Tried a lot of input/output, but it all seems to be correct

Re: 10718 - Bit Mask

Posted: Wed Sep 03, 2014 7:01 pm
by brianfry713
Use class Main