This is an array problem.
Given $$$N$$$ numbers, $$$A_1, \dots A_N$$$, answer $$$Q$$$ queries as follows:
Given $$$L, R$$$ where $$$R - L + 1 \geq 4$$$, among all possible $$$L \lt X \lt Y \lt R$$$, maximise $$$A_L \times A_X \times A_Y \times A_R$$$. Output on a single line this maximised value.
The first line contains two space-separated integers, $$$N$$$, the number of numbers, and $$$Q$$$, the number of queries.
The second line contains $$$N$$$ space-separated integers, $$$A_1, A_2, \dots, A_N$$$.
$$$Q$$$ lines follow, the $$$i^{th}$$$ line contains 2 space-separated integers, $$$L_i, R_i$$$, which are the $$$L, R$$$ values for the $$$i^{th}$$$ query.
Output $$$Q$$$ lines, each containing a single integer which is the answer for the $$$i^{th}$$$ query.
$$$1 \leq N, Q \leq 5 \times 10^5$$$
$$$-10^4 \leq A_i \leq 10^4$$$
$$$1 \leq L_i \lt L_i+3 \leq R_i \leq N$$$
7 3 -1 2 1 4 -2 -3 2 1 7 2 7 1 6
24 24 24
10 10 564 7167 -4069 -3244 579 199 -9838 2913 9796 4734 2 6 1 6 2 7 1 7 4 10 1 9 5 10 1 7 4 9 5 9
18826041697788 1481496793296 166115621837646 161812108318536 1480010149948608 221168049823968 78216085767528 161812108318536 910703330545056 3287917420308
As we know, the security of the RSA problem relies on the difficulty of factoring integers.
Generally, the RSA modulus is generated as follows:
Generally speaking, if $$$p$$$ and $$$q$$$ are large enough (e.g. $$$1024$$$-bit primes), then it is infeasible to factorize $$$n$$$, which is our only known strategy to break the security of RSA.
The problem with this strategy, however, is that if we have two modulus $$$n = p \cdot q$$$ and $$$n' = p \cdot q'$$$ for $$$q \ne q'$$$, then we can compute $$$p = \gcd(n, n')$$$, thus factoring both $$$n$$$ and $$$n'$$$. See https://crypto.stackexchange.com/q/76757 for some recent researches on this topic, the most recent research can factors $$$1$$$ in $$$172$$$ in RSA certificates.
Usually, the cause of such collision is poor randomization in the devices used to generate the random primes. Some ways to mitigate the issue is discussed in the question linked above.
If you have a huge collection of, for example, 340 million integers, it would be completely infeasible to compute the $$$\gcd$$$ of each pair. Instead, a batch GCD algorithm is needed. Common implementation of that algorithm is the following:
Using one of the fast integer multiplication algorithms based on FFT, this step can be done in time $$$\tilde O(n)$$$, where $$$n$$$ is the total number of bits representing the numbers in the input.
In this problem, you will be solving a much simpler instance with smaller size of integers: Given $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, determine if there exists any pair with $$$\gcd(a_i, a_j) \gt 1$$$.
The first line contains a positive integer $$$n \le 10^6$$$, the second line contains $$$n$$$ positive integers $$$a_i \le 10^{10}$$$.
For each test case, print the string "YES" or "NO" on a line.
1 2
NO
2 1 1
NO
2 2 2
YES
3 6 10 15
YES
It's SoCCat's birthday, and they have baked a delicious cake for the occasion. Unfortunately, there are too many students in SoC who is attending the party and interested in eating the cake. It is not feasible to cut the cake into sectors, otherwise the angle of each sector would be too small.
We model the cake on the Cartesian coordinate plane as a circle with radius $$$10000$$$ (in some unknown unit) with center $$$(0, 0)$$$. $$$N$$$ SoC students are invited to cut the cake, and each of them will be making a single straight cut. The $$$i$$$-th student will make a cut on the straight line from point $$$(x_i, y_i)$$$ to point $$$(x'_i, y'_i)$$$, where $$$(x_i, y_i)$$$ and $$$(x'_i, y'_i)$$$ are distinct points lying on the circumference of the circle.
Obviously, the cake will be divided into several pieces by the cuts. SoCCat wants to know the number of pieces that the cake will be divided into after all the cuts are made, in order to call the correct number of students to eat the cake.
The first line of input contains an integer $$$N$$$ ($$$1 \leq N \leq 30$$$), the number of cuts by the students.
The next $$$N$$$ lines each contain four integers $$$x_i$$$, $$$y_i$$$, $$$x'_i$$$, $$$y'_i$$$ ($$$-10000 \leq x_i, y_i, x'_i, y'_i \leq 10000$$$), the coordinates of the two endpoints of the cut made by the $$$i$$$-th student.
It is guaranteed that the cuts are distinct, i.e. there are no two cuts which entirely overlap.
Output a single integer, the number of pieces that the cake will be divided into after all the cuts are made.
2 10000 0 -10000 0 0 10000 0 -10000
4
3 8000 6000 -8000 -6000 10000 0 -10000 0 0 10000 0 -10000
6
Mara hates the word "perfect" - it means there's nothing left to improve! That's why she really hates graphs with perfect matchings - surely there's something that can be done to make them better, so they shouldn't be called perfect!
You are given a randomly generated tree $$$T$$$ with $$$N$$$ vertices. The $$$i$$$-th edge connects vertices $$$P_{i+1}$$$ and $$$i+1$$$ ($$$1 \leq i \leq N-1$$$). The value of $$$P_i$$$ ($$$2 \leq i \leq N$$$) is uniformly and independently chosen from the set $$$\{1, 2, \ldots, i-1\}$$$.
According to Mara, a graph $$$G = (V, E)$$$ is good (but definitely not perfect) if all of the following conditions are satisfied:
As a gift for Mara, you want to construct a set $$$S$$$ of good graphs, such that the size of the set $$$S$$$ is maximum possible. However, you realize that two isomorphic graphs are really just duplicates of each other, so you also want to ensure that no two graphs in the set $$$S$$$ are isomorphic to each other.
Find the maximum value of $$$|S|$$$. Since the value of $$$|S|$$$ may be very large, you only need to output the value of $$$|S| \bmod 998\,244\,353$$$.
Formally, let $$$\mathcal{G}$$$ be the set of all good graphs. You want to construct a set $$$S \subseteq \mathcal{G}$$$ of good graphs, such that for any distinct pairs of graphs $$$G_1, G_2 \in S$$$, $$$G_1$$$ and $$$G_2$$$ are not isomorphic, and the size of the set $$$S$$$ is maximum possible.
The first line of input contains an integer $$$N$$$ ($$$2 \leq N \leq 25$$$), the number of vertices in the tree.
The second line of input contains $$$N-1$$$ integers $$$P_2, P_3, \ldots, P_N$$$ ($$$1 \leq P_i \leq i-1$$$), representing the edges of the tree.
There are 20 test cases in total, excluding the sample test case. It is guaranteed that each $$$P_i$$$ ($$$2 \leq i \leq N$$$) is chosen uniformly and independently from the set $$$\{1, 2, \ldots, i-1\}$$$.
Output a single integer $$$|S|$$$, the maximum number of good graphs in the set $$$S$$$, modulo $$$998\,244\,353$$$.
2 1
0
3 1 1
1
6 1 1 3 3 3
2
7 1 1 3 2 3 3
1
In sample 1, the tree $$$T$$$ already has a perfect matching, hence there is no way to construct a good graph.
In sample 2, we can construct $$$S$$$ with the following graph:
There is no way to add an edge to the graph such that the graph remains simple and has no self-loops, so the graph is good. The maximum value of $$$|S|$$$ is 1.
In sample 3, we can construct $$$S$$$ with the following two graphs:
Both graphs are good and not isomorphic to each other. It can be proven that the maximum value of $$$|S|$$$ is 2.
However, we cannot insert the following graph into $$$S$$$, as it is isomorphic to the first graph in $$$S$$$:
There is an event involving $$$N$$$ banks and $$$M$$$ stakeholders, where Bob is stakeholder 1. The banks are giving money to the stakeholders, where bank $$$i$$$ needs to give out a total of $$$A_i$$$ dollars, and each bank can give each person at most $$$K$$$ dollars, to mitigate blatant favouritism. The banks can only give an integer number of dollars to each person, and bank $$$i$$$ must give out all $$$A_i$$$ dollars in this event. Additionally, person $$$i$$$ starts with $$$B_i$$$ dollars, but can only have at most $$$C_i$$$ dollars after receiving money from the banks, as a measure to reduce collusion.
However, Bob has blackmail material on all of the banks, and thus he has the power to decide how much each bank gives each person. He has a nasty and greedy personality, so he wants to be as rich as possible compared to the other $$$M - 1$$$ people. In particular, after the event, let $$$X$$$ be the amount of money (in dollars) that Bob (stakeholder 1) has, and $$$Y$$$ be the maximum amount of money (in dollars), that any of the stakeholders $$$2, 3, \dots, M$$$ has. Then Bob wants to maximise $$$X - Y$$$ (though it can be negative). Help Bob determine the maximum possible value of $$$X - Y$$$.
The first line of input contains an two integers $$$N, M, K$$$ $$$(1 \leq N\leq 10^5, 2 \leq M \leq 10^5, 1 \leq K \leq 10^{8})$$$, the number of banks, number of stakeholders, and the maximum amount of money (in dollars) that each bank can give each stakeholder.
The next line contains a $$$N$$$ space-separated integers $$$A_1, A_2, \dots, A_N$$$ $$$(0 \leq A_i \leq 10^{12})$$$, the amount of money (in dollars) each bank needs to give out.
The next $$$M$$$ lines each contain two space-separated integers, where the $$$i$$$-th line contains $$$B_i, C_i$$$ $$$(0 \leq B_i \leq C_i \leq 10^{13})$$$, the amount of money (in dollars) stakeholder $$$i$$$ starts with and the maximum amount of money (in dollars) he can have after the event.
Additionally, it is guaranteed that the banks will be able to give out all the money for the input data provided.
Output a single integer, the maximum possible value of $$$X - Y$$$ as described above.
4 4 3 7 5 3 2 2 8 1 3 5 15 3 12
-1
5 5 5 13 7 9 21 5 15 32 1 5 10 30 2 12 15 29
7
At the other side of Kivotos, the Trinity General School detected an abnormally high number of monsters in the school's underground system. While many students are trying their best to fend off the monsters, Mika charges in to the center of the anomaly, hoping to find a way to stop the monster raid. Mika finds an obelisk with high concentrations of energy, which is the source of energy of the monsters.
Upon touching the obelisk, a lowercase alphabet letter string $$$S$$$ of length $$$N$$$ is displayed in a hologram. From the given string $$$S$$$, a key must be retrieved to shut down the obelisk. The key of the string $$$S$$$ is defined to be the shortest substring such that it is possible to construct the original string $$$S$$$ from the substring by concatenating and overlapping operations.
For example, the key to the string abbabbababbab is abbab, as the string can be composed of the key like the following:
S = abbabbababbab
key = abbab
abbab
abbab
As the beloved Sensei of Kivotos students, you decide to help Mika to find the key of string $$$S$$$.
The first line of input contains an integer $$$N$$$ ($$$1 \leq N \leq 10^7$$$), the length of the string $$$S$$$.
The second line contains the string $$$S$$$ of length $$$N$$$ consisting of lowercase alphabet letters.
Output a single integer, the length of the key.
13 abbabbababbab
5
Gear wheels can be connected to form chains, where the teeth of adjacent gears interlock. This helps to transmit rotary motion over a long distance.
Now, you have $$$N$$$ gear wheels arranged on a straight line, numbered from $$$1$$$ to $$$N$$$ from left to right. Gear wheel $$$i$$$ has $$$T_i$$$ teeth.
If we connect gear wheel $$$i$$$ to gear wheel $$$j$$$, we can calculate the gear ratio as $$$T_i / T_j$$$ (or $$$T_j / T_i$$$). For the transmission to work perfectly, the gear ratio must be an integer.
You would like to form a longest chain of gear wheels such that the transmission between every pair of adjacent gear wheels works perfectly. Also, as moving the gears around is a lot of work, you are not allowed to reorder the gears on a chain. That is, if gear wheels $$$w_1, w_2, \ldots, w_k$$$ are connected in this order from left to right, then $$$w_1 \lt w_2 \lt \ldots \lt w_k$$$.
That is an easy task for you, so you would like to challenge yourself further. On top of finding one longest chain of gear wheels, you wish to find the maximum number of longest chains of gear wheels that can be formed, while each gear wheel can only be used in one chain.
The first line of input contains an integer $$$N$$$ $$$(1 \leq N \leq 1000)$$$, the number of gear wheels.
The next line contains $$$N$$$ integers $$$T_1, T_2, \ldots, T_N$$$ $$$(1 \leq T_i \leq 10^{18})$$$, the number of teeth on each gear wheel.
Output two integers $$$L$$$ and $$$C$$$ on the first line: the length of the longest chain of gear wheels that can be formed, and the maximum number of longest chains of gear wheels that can be formed.
Then, output $$$C$$$ lines, each containing $$$L$$$ integers. The $$$i$$$-th line should contain the indices of the gear wheels in the $$$i$$$-th longest chain of gear wheels, in increasing order.
If there are multiple valid answers, you may output any of them.
7 9 7 18 11 14 2 3
3 2 2 5 6 1 3 7
5 2 3 4 6 12
3 1 1 3 5
This is an array problem.
Given two arrays of $$$N$$$ numbers with values $$$A_1, \dots, A_N$$$ and $$$B_1, \dots, B_N$$$. Let $$$A_{L, R}$$$ be the sum of elements in the subarray of $$$A$$$ from index $$$L$$$ to $$$R$$$ inclusive. Let $$$B_{L, R}$$$ be defined similarly.
Minimise $$$A_{L, R}^2 + B_{L, R}^2$$$ across all $$$1\leq L \leq R \leq N$$$.
The first line contains a single integer, $$$N$$$, the length of both arrays.
The second line contains $$$N$$$ space-separated integers, $$$A_1, A_2, \dots, A_N$$$.
The second line contains $$$N$$$ space-separated integers, $$$B_1, B_2, \dots, B_N$$$.
Output a single integer, the minimised sum.
$$$1 \leq N \leq 5 \times 10^5$$$
$$$-10^9 \leq A_i, B_i \leq 10^9$$$
3 4 -1 -1 -1 -1 4
2
2 4 -4 1 1
4
Alice and Bob will play in a directed graph $$$H$$$. The $$$i$$$-th vertex in $$$H$$$ initially has a value $$$Y_i$$$. The game will proceed as follows.
If $$$H$$$ contains a cycle, then the game is a tie.
Otherwise, Alice and Bob will take turns to choose a vertex in $$$H$$$ to start a chain. Alice starts first. Each player will do all of the following in their turn:
The player who cannot make a move loses the game.
Note that, because a player can change the value of a vertex to an arbitrarily large value, the number of moves in a game might be unbounded; i.e., "infinite". However, it turns out that the game will eventually always end; i.e., "finite". We shall leave this philosophical question of the "finite" and "infinite" nature of the game to the reader.
We want to find out if Bob can win the game, assuming both players play optimally.
There's a twist: the author of this problem is too lazy to generate strong test cases, so the graph $$$H$$$ and initial values $$$Y_i$$$ are generated randomly, according to the following rules.
You are given a simple undirected graph $$$G = (V, E)$$$ (with no self-loops, and the graph is not necessarily connected) with $$$N$$$ vertices and $$$M$$$ edges. The vertices are numbered from $$$1$$$ to $$$N$$$, where vertex $$$i$$$ has a value $$$X_i$$$. Each edge $$$j$$$ also has a value $$$A_j$$$, $$$B_j$$$, and $$$C_j$$$.
Create the directed graph $$$H$$$ and values $$$Y_i$$$ as follows:
Now, the problem becomes: given the graph $$$G$$$ and the values $$$X_i$$$, $$$A_i$$$, $$$B_i$$$, and $$$C_i$$$, find the probability that Bob wins the game, modulo $$$998\,244\,353$$$. It can be proven that the probability that Bob wins the game can be represented as an irreducible fraction $$$\frac{P}{Q}$$$, where $$$P$$$ and $$$Q$$$ are integers, and $$$Q$$$ is coprime to $$$998\,244\,353$$$. You need to output the value of $$$P \cdot Q^{-1} \bmod 998\,244\,353$$$.
The first line of input contains two integers $$$N$$$ $$$(1 \leq N \leq 12)$$$ and $$$M$$$ $$$(0 \leq M \leq \frac{N(N-1)}{2})$$$, the number of vertices and edges in the graph $$$G$$$.
The next line contains $$$N$$$ integers $$$X_1, X_2, \ldots, X_N$$$ $$$(0 \leq X_i \lt 998\,244\,353 - 1)$$$, the values of the vertices in $$$G$$$.
The next $$$M$$$ lines each contain five integers $$$u_i$$$, $$$v_i$$$, $$$A_i$$$, $$$B_i$$$, and $$$C_i$$$ $$$(1 \leq u_i, v_i \leq N$$$, $$$0 \leq A_i, B_i, C_i \lt 998\,244\,353$$$, $$$1 \leq A_i + B_i + C_i \lt 998\,244\,353)$$$, representing an edge $$$(u_i,v_i)$$$ in $$$G$$$.
Output a single integer, the probability that Bob wins the game, modulo $$$998\,244\,353$$$.
3 0 1 2 3
748683265
4 6 1 2 3 4 1 3 42 42 42 1 4 3 3 3 1 2 2 2 2 3 4 3 3 3 2 4 3 3 3 2 3 42 42 42
597577278
In the first sample, there are $$$24$$$ possible games $$$(H, Y)$$$ generated. The probability that Bob wins the game is $$$\frac{1}{4} \equiv 748\,683\,265 \pmod{998\,244\,353}$$$.
After watching the novel "Around the World in Eighty Days", Alice becomes enthusiastic in travelling abroad. However, due to the lack of flights during the COVID-19 pandemic, Alice decides to travel around the world on foot!
However, Alice is suffering from decidophobia, so she is unable to decide how to travel around the world. Hence, she develops an innovative method for generating her path: she randomly generates a grid with size $$$n \times m$$$, with each cell containing an arrow pointing one of the four directions (north, east, south or west). Then, she packs the grid infinitely many times on the map without rotation. The following figure shows what the map looks like after packing the grid:
Demonstration on how Alice packs the grid on the map It is known that the initial position of Alice matches the top-left corner of the grid. After packing the grids on the map, she starts moving on the map according to the direction of the arrows. Once she reaches another cell, she will continue moving according to the arrow on that new cell. She never stops moving.
When Alice draws her journey on the map, she discovers that some grids may lead to a infinite path: none of the cells she visits will be visited infinitely many times. She doesn't like infinite paths as these paths will often lead to a lonely and boring experience. She likes repetitive paths instead: some of the cells she visits will be visited infinitely many times. The figures below shows an example of a repetitive path and an infinite path:
An example of a repetitive path
An example of an infinite path Alice plans to change some of the cells in her grid so that the path will become repeating. Once she changes a cell in the grid, all occurrences of the cell in the map will be changed. For example, the infinite path in the figure above can be made repetitive by changing the cell at $$$(3, 3)$$$ to 'S'. The new map is shown below:
The repetitive path after changing the cell at $$$(3, 3)$$$ to 'S' Can you help Alice to determine the minimum number of cells she needs to change in order to make the path repetitive?
The first line of input contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n, m \leq 3000$$$).
The next $$$n$$$ lines of input each contains $$$m$$$ characters, the cells on the grid ('N' for north, 'E' for east, 'S' for south, 'W' for west).
Output the minimum number of changes made to the grid, $$$k$$$, on the first line.
The next $$$k$$$ lines should each contain a change, denoted by 2 integers representing the position (row & column) followed by a character (the edited direction of that cell, either 'N', 'E', 'S' or 'W'). You may output the cells in any order.
If there are multiple solutions, you may output any of them.
3 4 WWNW SWWE ESEN
0
3 4 SNWS EENE WEEN
1 2 1 N
This is an array problem.
You are given an array $$$A$$$ that contains $$$N$$$ numbers, $$$A_1, \dots, A_N$$$. Count the number of distinct subarrays of $$$A$$$ where the sum of the elements in the subarray is even.
Note that an subarray is a contiguous part of an array (E.g. $$$[2, 3, 4]$$$ is a subaarray of $$$[1, 2, 3, 4, 5]$$$). Two subarrays $$$B_i$$$ and $$$C_i$$$ are considered identical if they are of the same length $$$k$$$ and for $$$1 \leq i \leq k, B_i = C_i$$$.
The first line contains a single integer, $$$N$$$, the length of both arrays.
The second line contains $$$N$$$ space-separated integers, $$$A_1, A_2, \dots, A_N$$$.
Output a single integer, the number of distinct even sum subarrays.
$$$1 \leq N \leq 10^6$$$
$$$-10^9 \leq A_i \leq 10^9$$$
3 8 8 8
3
4 5 5 4 4
5
Consider a grid of $$$n$$$ columns and $$$m$$$ rows. Number the columns from $$$0$$$ to $$$n-1$$$ from left to right.
Within the grid, some double-sided mirrors of the form "\" or "/" are placed.
When a laser is shined from the top, it will travel down the grid and might get reflected through the mirrors. For example:
In the example above, the laser ray entering column $$$2$$$ from the top travels out at column $$$4$$$ at the bottom.
Given an integer $$$n$$$ and a permutation $$$p = (p_0, p_1, \dots, p_{n-1})$$$ of $$$\{ 0, 1, \dots, n-1 \}$$$. Find a grid with the minimum nonnegative number of rows $$$m$$$, such that for every $$$i = 0, \dots, n-1$$$, the laser entering column $$$i$$$ from the top will travel out at column $$$p_i$$$ at the bottom.
There will be multiple test cases, terminated by EOF.
For each test case: The first line contains a positive integer $$$n$$$, the second line contains $$$n$$$ space-separated integers, $$$p_0\ p_1\ \dots\ p_n$$$.
For each test case, print the number of rows $$$m$$$ on the first line. The $$$m$$$ subsequent lines should contain the mirror configurations of each row, using character "\", "/" or "." (without quotes) to represent each cell.
For example, the image above corresponding to the following output:
3
.../
./..
.../.
If there are multiple possible configurations, output any of them.
Each file has at least one test case.
The sum of $$$n$$$ for each file does not exceed $$$10^4$$$.
The output file size does not exceed $$$5 \cdot 10^6$$$ bytes.
1 0 5 1 2 3 4 0
0 1 \\\\\
There are $$$N$$$ planets in Vita's galaxy. In order to improve the security of the planets, Vita assigned each planet to guard exactly one other planet. In particular, the $$$i$$$-th planet guards the $$$A_i$$$-th planet for every $$$1 \le i \le N$$$.
Vita wants to construct a protective barrier surrounding multiple planets. Let the set of planets to be protected by the barrier be $$$S$$$, then each planet in $$$S$$$ must be guarding a planet not in $$$S$$$.
Help Vita determine the maximum number of planets to be protected by the barrier.
The first line of input contains an integer $$$N$$$ ($$$2 \leq N \leq 10^6$$$), the number of planets.
The second line contains $$$N$$$ integers. For every $$$1 \le i \le N$$$, $$$i$$$-th integer represents $$$A_i$$$ ($$$1 \le A_i \le N$$$, $$$A_i \ne i$$$), which means the $$$i$$$-th planet guards the $$$A_i$$$-th planet.
Output a single integer, the maximum number of planets in the protective barrier.
6 3 6 2 5 4 3
3