简体   繁体   中英

Multiplying Using Bitwise Shift Operators Giving TLE

Question

Given N and M , write an equation using left shift operators whose result will be equal to the product N * M .

Input : First line has 0 < T ≤ 50000 denoting number of test cases.
Next T lines have two integers 0 < N, M ≤ 10¹⁶.

Output : For each test case print an equation for N * M resembling

(N << p1) + (N << p2)+ ...+(N << pk) where p1 ≥ p2 ≥ ... ≥ pk and k is minimum.

 SAMPLE INPUT SAMPLE OUTPUT 2 2 1 (2<<0) 2 3 (2<<1) + (2<<0) 

Time Limit: 1.0 sec

My Solution 1st approach

int dig = (int)(Math.floor(Math.log10(m)/Math.log10(2))+1);
boolean flag = false;
for(long i = dig; i>=0; --i) {       
      if(((m>>(i-1l)) & 1l) == 1l) {
           if(flag)
               System.out.print(" + ("+n+ "<<"+(i-1)+")");
           else {
               System.out.print("("+n+"<<"+(i-1)+")");
               flag = true; 
                }
             } 
         }

Second Approach

boolean[] arr = new boolean[dig];
        int i = dig-1;
        while(m > 0) {
            if((m&1) == 1 ) {
                arr[i] = true;
            }
            i--;
            m = m>>1l;
        }
        int j = dig-1;
        for( i = 0; i < dig; ++i) {

            if(arr[i]) {
               if(flag) 
               System.out.print(" + ("+n+"<<"+j+")");
               else {
                   System.out.print("("+n+"<<"+j+")");
                   flag = true;
               }
            }

            j--;
        }

In both cases I am getting 5 correct out of 8 and rest 3 are TLE why?

I don't actually see anything in both of your approaches preventing some ten-thousands of products of numbers up to 57 bit to be represented as String s in one second:
TLE may be due to System.out.print() taking an inordinate amount of time.
That said, use a utility like

   /** builds <code>n * m</code> in the form
    * <code>(n&lt;&lt;p1) + (n&lt;&lt;p2) + ... + (n&lt;&lt;pk)</code>
    * using left shift.
    * @param n  (0 < multiplicand <= 10**16)
    * @param m  0 < multiplier <= 10**16
    * @return a verbose <code>String</code> for <code>n * m</code>
    */
    static String verboseBinaryProduct(Object n, long m) {
        int shift = Long.SIZE - Long.numberOfLeadingZeros(m) - 1;
        final long highest = 1 << shift;
        final StringBuilder binary = new StringBuilder(42);
        final String chatter = ") + (" + n + "<<";
        final long rest = highest - 1;
        while (true) {
            if (0 != (highest & m))
                binary.append(chatter).append(shift);
            if (0 == (rest & m)) {
                binary.append(')');
                return binary.substring(4);
            }
            m <<= 1;
            shift -= 1;
        }
    }

and System.out.println(verboseBinaryProduct(n, m)); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM