-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpWatsonEscape.java
More file actions
39 lines (32 loc) · 899 Bytes
/
HelpWatsonEscape.java
File metadata and controls
39 lines (32 loc) · 899 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
package problems.codechef;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by hardCode on 4/26/2016.
*/
public class HelpWatsonEscape {
final static int M=1000000007;
public static void main(String[] args) throws IOException {
long t,k,n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
t=Integer.parseInt(br.readLine());
while (t-->0){
String s[]=br.readLine().split("\\s");
n=Long.parseLong(s[0]);
k=Long.parseLong(s[1]);
System.out.println(solve(k,n));
}
}
private static long solve(long k, long n) {
long result=k;
n--;
k--;
while (n>0){
if ((n&1)!=0)result=(result*k)%M;
k=(k*k)%M;
n>>=1;
}
return result;
}
}