-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
385 lines (337 loc) · 11.7 KB
/
Main.java
File metadata and controls
385 lines (337 loc) · 11.7 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) throws IOException {
int t = scanInt();
while (t-- > 0) {
solve();
}
}
public static void solve() throws IOException {
int a = scanInt();
int b = scanInt();
print(a+b);
}
static int MOD = 1_000_000_007;
static int INF = (int) 1e9;
static long fact[];
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringTokenizer st;
static int scanInt() throws IOException {
return Integer.parseInt(nextToken());
}
static long scanLong() throws IOException {
return Long.parseLong(nextToken());
}
static String scanString() throws IOException {
return nextToken();
}
static int[] scanIntArray(int size) throws IOException {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = scanInt();
}
return array;
}
static long[] scanLongArray(int size) throws IOException {
long array[] = new long[size];
for (int i = 0; i < size; i++) {
array[i] = scanLong();
}
return array;
}
static int GCD(int a, int b) {
return (b == 0) ? (a) : GCD(b, a % b);
}
static int LCM(int a, int b) {
return ((a * b) / GCD(a, b));
}
static String nextToken() throws IOException {
if (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
static List<Integer> getPrimeList(int from, int tillWhere) {
boolean isPrime[] = new boolean[tillWhere + 1];
List<Integer> primesList = new ArrayList<>();
Arrays.fill(isPrime, true);
for (int i = 2; i <= tillWhere; i++) {
if (isPrime[i]) {
if (i >= from) {
primesList.add(i);
}
for (int j = i * i; j <= tillWhere; j += i) {
isPrime[j] = false;
}
}
}
return primesList;
}
static List<Integer> getDivisorListOf(int num) {
List<Integer> divisorList = new ArrayList<>();
for (int i = 1; i * i <= num; i++) {
if (num % i == 0) {
divisorList.add(i);
if (num / i != i) {
divisorList.add(num / i);
}
}
}
return divisorList;
}
static void printArray(int arr[]) throws IOException {
StringBuilder sb = new StringBuilder();
for (int e : arr) {
sb.append(e + " ");
}
bw.write(sb.toString().trim());
bw.newLine();
bw.flush();
}
static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i * i <= n; i++) {
if ((n % i) == 0) {
return false;
}
}
return true;
}
static List<Integer> getPrimeFactorsListOf(int num) {
if (num <= 1) {
return new ArrayList<>();
}
List<Integer> primefactorsList = new ArrayList<>();
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
primefactorsList.add(i);
while (num % i == 0) {
num /= i;
}
}
}
if (num != 1) {
primefactorsList.add(num);
}
return primefactorsList;
}
static long pow(long base, long exp) {
long ans = 1l;
boolean isNegativeExponent = exp < 0;
exp = Math.abs(exp);
while (exp > 0) {
if ((exp & 1) == 1) {
ans = (ans * base * 1l) % MOD;
}
base = (base * base * 1l) % MOD;
exp >>= 1;
}
return isNegativeExponent ? (1l / ans) : ans;
}
static void compute_fact() {
fact = new long[100001];
fact[0] = fact[1] = 1;
for (int i = 2; i <= 100000; i++) {
fact[i] = (i * 1l * fact[i - 1]) % MOD;
}
}
static long nCr(int n, int r) {
long nr = fact[n];
long dr = (fact[n - r] * 1l * fact[r]) % MOD;
long inv = pow(dr, MOD - 2);// using fermat little theorm, inverse(x)=pow(x,m-2) given m is prime
long ans = (nr * 1l * inv) % MOD;
return ans;
}
static void print(Object o) throws IOException {
bw.write(o.toString());
bw.newLine();
bw.flush();
}
static List<List<Integer>> get_adj(int graph[][], int nNodes, boolean isDirected) {
List<List<Integer>> adj = new ArrayList<>();
for (int i = 0; i < nNodes; i++)
adj.add(new ArrayList<>());
for (int con[] : graph) {
adj.get(con[0] - 1).add(con[1] - 1);
if (!isDirected)
adj.get(con[1] - 1).add(con[0] - 1);
}
return adj;
}
static List<List<int[]>> get_adj_weighted(int graph[][], int nNodes, boolean isDirected) {
List<List<int[]>> adj = new ArrayList<>();
for (int i = 0; i < nNodes; i++)
adj.add(new ArrayList<>());
for (int con[] : graph) {
adj.get(con[0] - 1).add(new int[] { con[1] - 1, con[2] });
if (!isDirected)
adj.get(con[1] - 1).add(new int[] { con[0] - 1, con[2] });
}
return adj;
}
static int[][] scan_graph(int nConnections, boolean isWeighted) throws IOException {
int graph[][] = new int[nConnections][isWeighted ? 3 : 2];
for (int i = 0; i < nConnections; i++)
graph[i] = scanIntArray(isWeighted ? 3 : 2);
return graph;
}
static int djikstra(int g[][], int nNodes, int src, int dest) {// use when all edges r positive
List<List<int[]>> adj = get_adj_weighted(g, nNodes, true);
PriorityQueue<int[]> pq = new PriorityQueue<>((x, y) -> x[1] - y[1]);
int dis[] = new int[nNodes];
Arrays.fill(dis, INF);
dis[src] = 0;
pq.offer(new int[] { src, 0 });
while (!pq.isEmpty()) {
int top[] = pq.poll();
int curr = top[0];
int d = top[1];
if (d > dis[curr])
continue;
for (int edge[] : adj.get(curr)) {
int to = edge[0], w = edge[1];
if (dis[curr] + w < dis[to]) {
dis[to] = dis[curr] + w;
pq.offer(new int[] { to, dis[to] });
}
}
}
return dis[dest];
}
static int[] bellmanFord(int n, int[][] edges, int src) {// use when edges can be negative
int nNodes = n;
int dis[] = new int[nNodes];
Arrays.fill(dis, Integer.MAX_VALUE);
dis[src] = 0;
for (int i = 0; i < nNodes - 1; i++) {// we will update n-1 times by relaxing 1 edge at a time
for (int each[] : edges)
relaxEdges(each[0], each[1], each[2], dis);
}
if (hasCycles(edges, dis))
return new int[] { -1 };// relaxing edges for one more time ie nth time , if dis array changes compared
// to previous version, there existsa cycle
return dis;
}
static void relaxEdges(int u, int v, int wt, int dis[]) {
if (dis[u] != Integer.MAX_VALUE && dis[u] + wt < dis[v])
dis[v] = dis[u] + wt;
}
static boolean hasCycles(int edges[][], int dis[]) {
int clone[] = dis.clone();
for (int each[] : edges)
relaxEdges(each[0], each[1], each[2], clone);
for (int i = 0; i < dis.length; i++)
if (dis[i] != clone[i])
return true;
return false;
}
static long[][] floyd_warshall(int nNodes, int g[][], boolean isDirected) {// when i want miDis(u,v) for each query
// in O(1) time
long dis[][] = new long[nNodes][nNodes];
for (int i = 0; i < nNodes; i++) {
Arrays.fill(dis[i], INF);
dis[i][i] = 0;
}
for (int[] e : g) {
dis[e[0] - 1][e[1] - 1] = Math.min(dis[e[0] - 1][e[1] - 1], e[2]);
if (!isDirected)
dis[e[1] - 1][e[0] - 1] = Math.min(dis[e[1] - 1][e[0] - 1], e[2]);
}
for (int k = 0; k < nNodes; k++) {
long[] disK = dis[k];
for (int i = 0; i < nNodes; i++) {
long dik = dis[i][k];
if (dik == INF)
continue;
long[] disI = dis[i];
for (int j = 0; j < nNodes; j++) {
long alt = dik + disK[j];
if (alt < disI[j]) {
disI[j] = alt;
}
}
}
}
return dis;
}
static List<int[]> get_mst_graph(int nNodes, int graph[][], boolean isDirected) {// prims
List<int[]> mst_edges = new ArrayList<>();
List<List<int[]>> adj = get_adj_weighted(graph, nNodes, isDirected);
boolean isVis[] = new boolean[nNodes];
int src = 0;
PriorityQueue<int[]> pq = new PriorityQueue<>((x, y) -> x[2] - y[2]);
pq.offer(new int[] { src, -1, 0 });// store format:[node,parent,wtWithParent]
int ans = 0;
while (!pq.isEmpty()) {
int front[] = pq.poll();
int curr = front[0];
int parentOfCurr = front[1];
int wt = front[2];
if (isVis[curr]) {
continue;
}
if (parentOfCurr != -1) {
mst_edges.add(new int[] { parentOfCurr, curr, wt });
}
isVis[curr] = true;
ans += wt;
for (int neigh[] : adj.get(curr)) {
int neighbourNodeNumber = neigh[0];
int wtOfCurrWithThatNeighBour = neigh[1];
pq.offer(new int[] { neighbourNodeNumber, curr, wtOfCurrWithThatNeighBour });
}
}
if (mst_edges.size() != nNodes - 1) {
return null; // Graph is disconnected
}
System.out.println("The Minimum spanning tree of given graph has the following adjacency list:");
for (var e : mst_edges)
System.out.println(Arrays.toString(e));
System.out.println("The sum of all weights in MST of given graph is " + ans);
return mst_edges;
}
static class ModularFunction {
long x;
public ModularFunction(long x) {
this.x = x;
}
// (a*b)%k = ((a%k)*(b*k))%k
@SuppressWarnings("unused")
private ModularFunction multiply(long b) {
x = (((b % MOD) * 1l * (x % MOD)) % MOD);
return this;
}
// (a/b)%k = ((a%k)*inv(b))%k
@SuppressWarnings("unused")
private ModularFunction divideBy(long b) {
x = (((x % MOD) * 1l * (pow(b, MOD - 2))) % MOD);
return this;
}
// (a+b)%k
@SuppressWarnings("unused")
private ModularFunction add(long b) {
x = (((x % MOD) + (b % MOD)) % MOD);
return this;
}
// (a-b)%k = ((a%k)-(b%k)+k)%k
@SuppressWarnings("unused")
private ModularFunction subtract(long b) {
x = (((x % MOD) - (b % MOD) + MOD) % MOD);
return this;
}
// (a^b)=((a%k)^b)%k
@SuppressWarnings("unused")
private ModularFunction power(long b) {
x = ((pow(x % MOD, b)) % MOD);
return this;
}
@Override
public String toString() {
return Long.toString(x);
}
}
}