- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtemplate.java
More file actions
Latest commit
104 lines (89 loc) · 2.78 KB
/
Copy pathtemplate.java
File metadata and controls
104 lines (89 loc) · 2.78 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
packagecom.mahbub.algorithm;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.List;
importjava.util.Scanner;
publicclassA {
staticfinaldoubleEPS = 1e-9f;
staticfinaldoubleTO_DEG = 57.29577951;
// UP, RIGHT, DOWN, LEFT, UPPER-RIGHT, LOWER-RIGHT, LOWER-LEFT, UPPER-LEFT
staticint[] dx = { -1, 0, 1, 0, -1, 1, 1, -1 };
staticint[] dy = { 0, 1, 0,-1, 1, 1, -1, -1 };
// Represents all moves of a knight in a chessboard
staticint[] dxKnightMove = { -1, -2, -2, -1, 1, 2, 2, 1 };
staticint[] dyKnightMove = { 2, 1, -1, -2, -2, -1, 1, 2 };
staticfinalcharWHITE = 0;
staticfinalcharGRAY = 1;
staticfinalcharBLACK = 2;
staticfinalintMAX = 100000;
// Bitwise Operations
publicstaticfinalintALL_BITS = (1 << 31) - 1;
publicstaticintNEG_BITS(intn) {
returnn ^ ALL_BITS;
}
publicstaticbooleanTEST_BIT(intn, inti) {
return (n & (1 << i)) != 0;
}
publicstaticintON_BIT(intn, inti) {
returnn | (1 << i);
}
publicstaticintOFF_BIT(intn, inti) {
returnn & NEG_BITS(1 << i);
}
publicstaticintTOGGLE_BIT(intn, inti) {
returnn ^ (1 << 31);
}
publicstaticbooleanIS_POWER_TWO(intn) {
returnn > 0 && (n & (n - 1)) == 0;
}
publicstaticintOFF_LOWEST_SET_BIT(intn) {
returnn & (n - 1);
}
publicstaticintLOWEST_SET_BIT_POSITION(intn) {
returnn ^ (n & (n - 1));
}
publicstaticintDEVIDE_BY_POWER_TWO(intn, intp) {
returnn & (p - 1);
}
publicstaticintbitCountRange(intN, intl, intr) {
intmask = ((1 << (r+1)) - 1) ^ ((1 << l) - 1);
returnInteger.bitCount(N & mask);
}
publicstaticintgetLeftmostBit(intN) {
intm = 0;
while (N > 1) {
N = N >> 1;
m++;
}
returnm;
}
publicstaticbooleanisAlternatingBits(intn) {
returnInteger.bitCount(((n >> 1) ^ n) + 1) == 1;
}
staticlongsqr(intx) { returnx*x; }
staticintgcd(inta, intb) {
while (b != 0) {
b = b ^ a;
a = a ^ b;
b = b ^ a;
a = a % b;
}
returna;
}
staticintlcm(inta, intb) {
returna / gcd(a, b) * b;
}
publicstaticvoidmain(String[] args) {
Scannerscanner = inputFromSystem();
}
staticScannerinputFromFile() {
try { returnnewScanner(newFileInputStream("input.txt")); }
catch (FileNotFoundExceptione) { e.printStackTrace(); }
returnnull;
}
staticScannerinputFromSystem() {
returnnewScanner(System.in);
}
}