-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModularExponentiation.java
More file actions
40 lines (35 loc) · 1022 Bytes
/
ModularExponentiation.java
File metadata and controls
40 lines (35 loc) · 1022 Bytes
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
/**
* Created by MalhotR1 on 01/09/2018.
* hello 2018, A
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ModularExponentiation {
public static void main(String[] args) throws IOException {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
long n = Long.parseLong(br.readLine().trim());
long m = Long.parseLong(br.readLine().trim());
if (m < n) {
System.out.println(m);
return;
}
long lgm = calculatelgm(m);
if (lgm < n) {
System.out.println(m);
return;
}
long res = m - ((m >> n) << n);
System.out.println(res);
}
}
private static long calculatelgm(long m) {
if (m == 1) return 0;
long count = 0;
while (m > 1) {
m = m >> 1;
count++;
}
return count;
}
}