-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path10229.java
81 lines (54 loc) · 1.58 KB
/
10229.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
String b;
while ((b = ReadLn(255)) != null && !b.equals("\r") && !b.equals("\n")) {
StringTokenizer tokens = new StringTokenizer(b);
int n = (int) Long.parseLong(tokens.nextToken());
int m = (int) Long.parseLong(tokens.nextToken());
long mod = 1 << m;
System.out.println(mpow(n, mod)[0][1] % mod);
}
}
private static long sq(long fn) {
return fn * fn;
}
public static long[][] mpow(int p, long mod) {
long[][] f = new long[][] { { 1, 1 }, { 1, 0 } };
if (p == 0)
return new long[2][2];
if (p == 1)
return f;
else if (p % 2 == 0) {
long[][] a = mpow(p / 2, mod);
return multiply(a, a, mod);
} else
return multiply(f, mpow(p - 1, mod), mod);
}
public static long[][] multiply(long[][] a, long[][] a2, long mod) {
long[][] r = new long[2][2];
r[0][0] = ( a[0][0] * a2[0][0] + a[0][1] * a2[1][0] ) % mod;
r[0][1] = ( a[0][0] * a2[0][1] + a[0][1] * a2[1][1] ) % mod;
r[1][0] = ( a[1][0] * a2[0][0] + a[1][1] * a2[1][0] ) % mod;
r[1][1] = ( a[1][0] * a2[0][1] + a[1][1] * a2[1][1] ) % mod;
return r;
}
static String ReadLn(int maxLg) // utility function to read from stdin
{
byte lin[] = new byte[maxLg];
int lg = 0, car = -1;
try {
while (lg < maxLg) {
car = System.in.read();
if ((car < 0) || (car == '\n'))
break;
lin[lg++] += car;
}
} catch (Exception e) {
return (null);
}
if ((car < 0) && (lg == 0))
return (null); // eof
return (new String(lin, 0, lg));
}
}