-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMathematicsForCP.java
69 lines (38 loc) · 1.1 KB
/
MathematicsForCP.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
package math;
import java.util.Arrays;
public class MathematicsForCP {
public static void main(String[] args) {
// boolean isPrime[] = seiveOfEratoSthenes(20);
// for(int i = 0; i<=20; i++) {
// System.out.println(i +" " + isPrime[i]);
// }
// System.out.println(gcd(24, 60));
System.out.println(fastPower(3978432, 5, 1000000007));
}
static long fastPower(long a, long b, int n) {
long res = 1;
while ( b > 0) {
if ( (b&1) != 0) {
res = (res * a % n) % n;
}
a = (a % n * a % n) % n;
b = b >> 1;
}
return res;
}
static int gcd(int a, int b) {
return a % b == 0 ? b : gcd ( b, a%b);
}
static boolean[] seiveOfEratoSthenes(int n) {
boolean isPrime[] = new boolean[n+1];
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
for(int i = 2; i * i <= n; i++) {
for(int j = 2*i; j<=n; j += i) {
isPrime[j] = false;
}
}
return isPrime;
}
}