Sort by

recency

|

206 Discussions

|

  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while(n--!=0){
            long a=sc.nextLong();
            long b=sc.nextLong();
            long and=a;
            if(a%4==0){
                for(long i=a+4;i<=b;i+=4){
                    and&=i;
                }
            }
            else if(a%2==0){
                for(long i=a+2;i<=b;i+=2){
                    and&=i;
                }
            }
            else{
                for(long i=a+1;i<=b;i++){
                    and&=i;
                }
            }
            System.out.println(and);
        }
    }
    

    }

  • + 0 comments
    # greedy, needs to be compiled to pass the time limit with PyPy3
    def andProduct(a, b):
        return reduce(lambda x, count: count & x, range(a, b+1, (a%2==0)+1))
    
    def andProduct(a, b):
        differing_position = (a ^ b).bit_length()
        
        mask = ~((1 << differing_position) - 1)
        
        return a & mask
    

    The same idea as the above but handling the binary representation as a string throws a runtime error on test-case 3, however, I can't understand what is wrong

        position = b.bit_length() - (a^b).bit_length()
    
        return int(bin(a)[2:][:position] + '0'*(a.bit_length() - position), 2)
    
  • + 0 comments

    The question is missing edge cases where a=b and a

  • + 0 comments

    The question is missing edge cases where a=b and a

  • + 0 comments

    Haskell

    I'm kind of surprised to see implementations that AND over the list in the discussion; a Haskell foldl1 (.&.) [a..b] (basically the same thing) would often timeout.

    module Main where
    
    import Data.Bits (shiftL, shiftR, (.&.))
    import Control.Monad (replicateM_)
    
    solve :: [Integer] -> Integer
    solve [a, b] = commonPrefix a b
      where
        commonPrefix x y
            | x == y = x
            | otherwise = commonPrefix (x `shiftR` 1) (y `shiftR` 1) `shiftL` 1
    
    main :: IO ()
    main = do
        cases <- readLn :: IO Int
        replicateM_ cases $ do
            getLine >>= print . solve . map read . words