-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatteyMultiplication.java
42 lines (40 loc) · 1.02 KB
/
MatteyMultiplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* https://www.hackerearth.com/practice/basic-programming/bit-manipulation/basics-of-bit-manipulation/practice-problems/algorithm/mattey-multiplication-6/
* #math #number-theory #bit-manipulation
*/
import java.util.Scanner;
class MatteyMultiplication {
static long getMaxLeftShiftNum(long n) {
long ans = 0;
while ((n >> 1) > 0) {
ans++;
n >>= 1;
}
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcases = sc.nextInt();
long n, m;
long p, count;
for (int i = 0; i < testcases; i++) {
n = sc.nextLong();
m = sc.nextLong();
StringBuffer ans = new StringBuffer();
count = 0;
long val = 1;
while (m > 0) {
p = getMaxLeftShiftNum(m);
if (count > 0) {
ans.append(" + ");
}
ans.append(String.format("(%d<<%d)", n, p));
if (p == 0) break;
m -= val << p;
count++;
}
System.out.println(ans.toString());
}
return;
}
}