The English statement was translated by AI and may be slightly different from the original Chinese statement. Please refer to the Chinese statement if you have any questions.
Given an undirected graph with $$$n$$$ vertices and $$$m$$$ edges. The $$$i$$$-th edge connects vertex $$$u_i$$$ and vertex $$$v_i$$$ with traversal cost $$$w_i$$$.
Now, you may increase the traversal cost of all edges simultaneously by $$$k$$$, where $$$k$$$ is any non-negative real number. Question: how many distinct $$$^{\dagger}$$$ paths from vertex $$$1$$$ to vertex $$$n$$$ exist such that there is at least one value of $$$k$$$ for which that path is one of the shortest paths from $$$1$$$ to $$$n$$$?
$$$^{\dagger}$$$ Two paths are considered different if and only if they have different numbers of edges, or there exists an index $$$i$$$ such that the $$$i$$$-th edge on the two paths has a different edge index.
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 1000$$$). The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 5000$$$, $$$1 \leq m \leq 5000$$$), denoting the number of vertices and edges in the graph.
The next $$$m$$$ lines each contain three integers $$$u_i$$$, $$$v_i$$$ and $$$w_i$$$ ($$$1 \leq u_i, v_i \leq n$$$, $$$1 \leq w_i \leq 10^9$$$), describing an undirected edge between vertices $$$u_i$$$ and $$$v_i$$$ with traversal cost $$$w_i$$$.
For each test file, it is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$, and the sum of $$$m$$$ over all test cases does not exceed $$$5000$$$.
For each test case, output a single integer — the number of paths that can become shortest paths from vertex $$$1$$$ to vertex $$$n$$$. Since the answer may be large, output it modulo $$$998\,244\,353$$$.
33 41 2 11 2 12 3 12 3 15 61 5 31 2 12 5 21 3 13 4 14 5 18 91 2 11 3 52 6 16 4 13 4 13 5 14 8 55 7 17 8 1
434
You are given an ellipse on a 2D plane defined by the standard equation:
$$$$$$ \frac{x^2}{a^2} + \frac{y^2}{b^2} = 1 $$$$$$
where $$$a$$$ and $$$b$$$ are positive real numbers representing the semi-major and semi-minor axes.
You are also given a line in the plane defined by the linear equation:
$$$$$$ y = kx + c $$$$$$
Your task is to compute the area of the larger part when the line divides the ellipse into two regions.
There is only one test case in each test file.
The first line contains two integers $$$a$$$ and $$$b$$$ ($$$1 \leq a, b \leq 10^3$$$), the semi-axes of the ellipse.
The second line contains two integers $$$k$$$ and $$$c$$$ ($$$|k|, |c| \leq 10^3$$$), defining the line $$$y = kx + c$$$.
It is guaranteed that the line always intersects the ellipse at two different points.
Output a single floating-point number representing the area of the larger part of the ellipse.
Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$.
Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \leq 10^{-6}$$$.
2 31 1
12.709803500
The English statement was translated by AI and may be slightly different from the original Chinese statement. Please refer to the Chinese statement if you have any questions.
Given a tree with $$$n$$$ nodes, there are $$$q$$$ queries.
In the $$$i$$$-th query, a sequence $$$a=(a_1,a_2,\dots,a_k)$$$ without duplicate elements is given. Initially:
Then labels keep propagating as follows:
We are interested in, for each label $$$j$$$, the maximum number $$$t_j$$$ of nodes labeled $$$j$$$ at any time during the whole process (including the initial state and after any propagation step).
There is only one test case in each test file.
The first line contains two positive integers $$$n$$$ and $$$q$$$ ($$$1 \leq n, q \leq 5 \times 10^5$$$), denoting the number of nodes in the tree and the number of queries.
The next $$$n−1$$$ lines each contain two positive integers $$$x,y$$$ ($$$1 \leq x,y \leq n$$$, $$$x \neq y$$$), representing an edge of the tree.
The next $$$q$$$ lines each contain a query. For the $$$i$$$-th query, the first integer $$$k_i$$$ denotes that there are $$$k_i$$$ labels in this query ($$$1 \leq k_i \leq n$$$, $$$\sum{k_i} \leq 10^6$$$). Then follow $$$k_i$$$ positive integers $$$a_1, a_2, \dots, a_{k_i}$$$ ($$$1 \leq a_i \leq n$$$, the $$$a_i$$$ in a single query are pairwise distinct), representing the initial positions of the $$$k_i$$$ labels.
Output $$$q$$$ lines. The $$$i$$$-th line contains $$$k_i$$$ integers, representing for each label the maximum number of nodes it ever labels during the whole process.
5 51 21 32 42 51 12 2 33 3 4 54 4 2 5 15 2 1 3 4 5
5 4 2 3 2 2 2 1 2 3 4 1 2 1 1
Little-L has a box divided into an $$$n \times m$$$ grid of cells.
Initially, all cells are empty. You can perform several operations, each of which can be one of the following two types:
For example, in a $$$5 \times 5$$$ box, choosing cell $$$(3,4)$$$ for either the first or the second operation would yield the following results:
The left figure shows the first operation, and the right figure shows the second operation. Little-L wants to know: what is the minimum number of operations needed to fill all cells in the box with balls?
Note: You can perform an operation on a cell that already contains a ball.
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of the test cases follows.
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 10^3$$$), representing the number of rows and columns of the box.
In each test file, it is guaranteed that the sum of $$$n \times m$$$ across all test cases does not exceed $$$10^6$$$.
For each test case:
The first line should contain a single integer $$$p$$$ ($$$1 \leq p \leq n \times m$$$), representing the minimum number of operations.
The next $$$p$$$ lines each contain three integers $$$op, x, y$$$ ($$$op \in \{1,2\}, 1 \leq x \leq n, 1 \leq y \leq m$$$), indicating one operation. If $$$op=1$$$, perform the first type of operation at cell $$$(x,y)$$$; if $$$op=2$$$, perform the second type of operation at cell $$$(x,y)$$$.
23 42 2
3 2 2 2 2 2 3 1 2 4 2 1 1 1 1 2 2
The English statement was translated by AI and may be slightly different from the original Chinese statement. Please refer to the Chinese statement if you have any questions.
Today, Alice wants to fill a permutation of length $$$n \times m$$$$$$^{\dagger}$$$ into an $$$n \times m$$$ matrix $$$A$$$, such that the sum of numbers in any contiguous submatrix with both height and width at least $$$2$$$ is a composite number.
In other words, for any $$$1 \leq a \lt c \leq n, 1 \leq b \lt d \leq m$$$, Alice wants $$$\sum_{i=a}^{c} \sum_{j=b}^{d} A_{i,j}$$$ to be a composite number.
For example, when $$$n=2$$$ and $$$m=3$$$, matrix $$$B$$$ is a valid construction, while matrix $$$C$$$ is not, because $$$C_{1,2}+C_{1,3}+C_{2,2}+C_{2,3}=17$$$.
$$$$$$ B = \begin{bmatrix} 1 & 3 & 2 \\ 6 & 5 & 4 \end{bmatrix}, \quad C = \begin{bmatrix} 1 & 6 & 2 \\ 3 & 5 & 4 \end{bmatrix} $$$$$$
Given $$$n$$$ and $$$m$$$, can the above requirement be satisfied? If yes, output one valid construction.
$$$^{\dagger}$$$ A permutation of length $$$n$$$ is an array containing the integers from $$$1$$$ to $$$n$$$ exactly once in any order. For example, $$$[2,3,1,5,4]$$$ is a permutation, while $$$[1,2,2]$$$ is not (since $$$2$$$ appears twice), and $$$[1,3,4]$$$ is not (since $$$n=3$$$ but the array contains $$$4$$$).
There is only one test case in each test file.
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n, m \leq 50$$$).
For the test case, on the first line output whether a valid construction exists. If it exists, output "Yes"; otherwise, output "No".
If a valid construction exists, you should also output $$$n$$$ lines, each containing $$$m$$$ integers. The $$$j$$$-th number on the $$$i$$$-th line denotes the number at row $$$i$$$, column $$$j$$$ of the matrix. You must ensure that the multiset of all output numbers forms a permutation of length $$$n \times m$$$. If there are multiple valid constructions, you may output any of them.
2 3
Yes 1 3 2 6 5 4
3 3
Yes 5 4 3 6 9 8 7 2 1
To train your ability to calculate huge numerical values, $$$Irelia$$$ requires you to perform $$$n$$$ operations. Let $$$a_i$$$ denote the result produced by the $$$i$$$-th operation.
Each operation is one of the following four forms:
After each operation, output the value of $$$a_i$$$ modulo $$$m$$$.
Please note that the modulo operation is only applied to the answer, and the actual value of $$$a_i$$$ may be huge.
There is only one test case in each test file.
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 201307$$$, $$$2 \leq m \leq 10^9$$$), representing the number of operations and the modulus.
In the next $$$n$$$ lines, each contains an operation. For the $$$i$$$-th line, it starts with a character $$$op$$$ ($$$op \in \{\text{=,+,*,^}\}$$$). If $$$op$$$ is "=", it is followed by an integer $$$v$$$ ($$$1 \leq v \leq 10^9$$$). Otherwise, $$$op$$$ is followed by two integers $$$j$$$ and $$$k$$$ ($$$1 \leq j, k \lt i$$$).
After each operation, output the value of $$$a_i$$$ modulo $$$m$$$.
4 201307= 1+ 1 1* 2 2^ 3 3
1 2 4 256
6 8= 5+ 1 1^ 2 1^ 2 2= 8= 9
5 2 0 0 0 1
The English statement was translated by AI and may be slightly different from the original Chinese statement. Please refer to the Chinese statement if you have any questions.
Given a road of length $$$l$$$ kilometers. The road is divided into $$$n$$$ segments, each with its own speed limit. The $$$i$$$-th segment starts at distance $$$a_{i-1}$$$ kilometers from the road's start, ends at distance $$$a_i$$$ kilometers from the road's start, and has a speed limit of $$$v_i$$$ kilometers per hour.
Bob drove through the entire road. There are $$$m$$$ cameras on the road; the $$$i$$$-th camera is located at position $$$x_i$$$ kilometers from the road's start, and recorded Bob passing it at time $$$t_i$$$ hours after Bob departed from the start. Bob finally took $$$t_{end}$$$ hours to reach the end of the road.
Cameras themselves do not measure speed, but by combining Bob's passing times at multiple cameras, the traffic police may deduce that Bob must have sped on some segments. Suppose there are two cameras at positions $$$x_a$$$ and $$$x_b$$$. If driving from $$$x_a$$$ to $$$x_b$$$ while always traveling at the maximum allowed speed takes $$$t_0$$$ hours, and $$$t_b - t_a \lt t_0$$$, then Bob must have sped on some part of the road between them. (Ignore time spent accelerating or decelerating.)
Now Bob wants to know the minimum number of camera records that must be removed so that the police cannot deduce that Bob definitely sped on any segment. Note: the records at the road's start and end cannot be removed; the police always know when Bob entered and left the road.
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of the test cases follows.
The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$l$$$ and $$$t_{end}$$$ ($$$1 \leq n, m \leq 2 \times 10^5$$$, $$$2 \leq l \leq 10^9$$$, $$$2 \leq t_{end} \leq 10^9$$$), denoting the number of speed-limit segments, the number of cameras, the total length of the road, and the total time Bob took to pass the road, respectively.
The second line contains $$$n-1$$$ integers $$$a_1, a_2, \dots, a_{n-1}$$$ ($$$0 \lt a_i \lt l$$$), representing the boundaries of the segments. In particular, $$$a_0=0$$$ and $$$a_n=l$$$ always hold. When $$$n=1$$$, this line is empty.
The third line contains $$$n$$$ integers $$$v_1, v_2, \dots, v_n$$$ ($$$v_i \in \{5, 10, 15, 20, 25, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120\}$$$), representing the speed limit of each segment.
The next $$$m$$$ lines each contain two integers $$$x_i$$$ and $$$t_i$$$ ($$$0 \lt x_i \lt l$$$, $$$0 \lt t_i \lt t_{end}$$$), representing the position of the $$$i$$$-th camera and the time Bob passed it. It is guaranteed that $$$x_1 \lt x_2 \lt \dots \lt x_m$$$ and $$$t_1 \lt t_2 \lt \dots \lt t_m$$$. For each test file, it is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \times 10^5$$$, and the sum of $$$m$$$ over all test cases does not exceed $$$2 \times 10^5$$$.
For each test case, output a single integer on one line — the minimum number of camera records that must be removed so that the police cannot deduce that Bob definitely sped. If the police will definitely find that Bob sped regardless, output "-1".
41 1 60 23020 11 1 60 23030 11 1 60 23040 11 1 120 23060 1
101-1
35 5 949 953359 506 602 861110 110 10 40 2038 80327 245790 333861 455921 9155 5 615 8113 193 206 35060 100 80 25 603 3285 4318 5348 6402 75 5 824 17951 354 656 67460 15 90 20 5479 4482 21573 22679 78730 97
0-13
Speeding is illegal, dangerous, and subject to legal penalties. This problem is a contest programming exercise and describes a fictional scenario; do not attempt to evade traffic surveillance in real life. Also do not attempt to tamper with or delete camera records, as that may constitute serious criminal offenses such as damaging computer information systems. Please program responsibly and prioritize safety.
Link has a sequence of length $$$n$$$ consisting of positive integers $$$a$$$.
Link wants to know how many subarrays of this sequence have a sum that is a prime number. In other words, how many pairs $$$(l,r)$$$ ($$$1 \leq l \leq r \leq n$$$) satisfy $$$\sum_{i=l}^{r} a_i$$$ being a prime number.
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of the test cases follows.
The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 10^6$$$), the length of the sequence.
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^6$$$, $$$\sum a_i \leq 10^6$$$), representing the elements of the sequence.
It is guaranteed that the sum of all $$$n$$$ across the test cases in a single file does not exceed $$$10^6$$$, and the sum of all $$$\sum a_i$$$ across the test cases in a single file does not exceed $$$10^6$$$.
For each test case, output a single line containing one integer — the number of subarrays whose sum is a prime number.
241 2 3 4121 1 4 5 1 4 1 9 1 9 8 10
516
In the first sample test case, the valid $$$(l,r)$$$ pairs are:
The English statement was translated by AI and may be slightly different from the original Chinese statement. Please refer to the Chinese statement if you have any questions.
Alice has a sheet of paper; some region on the paper contains important information, but the paper was torn into two pieces by Bob. Assuming the tear is random, Alice wants to know the probability that the important information is completely preserved in one of the two halves.
Formally:
There is an $$$n\times m$$$ sheet of paper on the plane, and the coordinate system partitions it into unit square grid cells. There is a "critical region" on the paper — a closed rectangle with vertices $$$(a,b)$$$, $$$(a,d)$$$, $$$(c,d)$$$, $$$(c,b)$$$, where
$$$$$$ 0 \leq a \lt c \leq n,\quad 0 \leq b \lt d \leq m. $$$$$$
Now Bob will tear the paper along a "legal path" from $$$(0,0)$$$ to $$$(n,m)$$$, and this path is chosen uniformly at random among all legal paths. A legal path means a lattice path from $$$(0,0)$$$ to $$$(n,m)$$$ where each step is either to the right (increase $$$x$$$ by $$$1$$$) or up (increase $$$y$$$ by $$$1$$$). Such a path necessarily divides the whole sheet into a "upper-left" part and a "lower-right" part. For convenience, a path that leaves one part empty is also considered legal.
We care whether the critical region is cut by this tearing path. If the entire closed rectangle of the critical region lies exactly on one side of the path (either entirely in the "upper-left" part or entirely in the "lower-right" part), we say the "critical region remains intact"; otherwise, it is considered cut by the tearing path.
We ask: among all possible legal paths, what is the probability that the "critical region remains intact"? Give the result modulo $$$998244353$$$ $$$^{\dagger}$$$.
$$$^{\dagger}$$$ Formally, let $$$M = 998\,244\,353$$$. It can be shown the answer can be expressed as a reduced fraction $$$\frac{p}{q}$$$ with integers $$$p,q$$$ and $$$q \not\equiv 0\pmod{M}$$$. Output the integer $$$p\cdot q^{-1}\bmod M$$$. In other words, output an integer $$$x$$$ with $$$0\le x \lt M$$$ such that $$$x\cdot q \equiv p \pmod{M}$$$.
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of the test cases follows.
The first line of each test case contains six integers $$$n, m, a, b, c, d$$$ ($$$1 \leq n \leq 10^3, 1 \leq m \leq 10^6$$$, $$$0 \leq a \lt c \leq n$$$, $$$0 \leq b \lt d \leq m$$$), with meanings as described above.
For each test case, output one integer on a separate line — the value of the probability that the "critical region remains intact" modulo $$$998244353$$$.
33 3 1 1 2 23 3 0 0 3 33 3 0 1 3 2
1299473306199648871
In the first sample test case, the critical region has size $$$1 \times 1$$$. It can never be split by any tearing path, so the probability that it remains intact is $$$1$$$.
In the second sample test case, the critical region has size $$$3 \times 3$$$, i.e. it is the entire sheet. Since the tearing path always divides the sheet into two nonempty parts unless one part is empty, the probability that the critical region remains intact is $$$\frac{2}{20}$$$, i.e. $$$\frac{1}{10}$$$.
In the third sample test case, the probability that the critical region remains intact is $$$\frac{8}{20}$$$, i.e. $$$\frac{2}{5}$$$.
The English statement was translated by AI and may be slightly different from the original Chinese statement. Please refer to the Chinese statement if you have any questions.
Little L broke into a mysterious underground palace.
On the tightly closed palace door, Little L saw an inscription telling him that to open the door he needs to construct several convex polyhedra whose surfaces are tiled only by regular triangles and squares of side length $$$1$$$, and that a total of $$$x$$$ regular triangles and $$$y$$$ squares must be used.
Over time the power of the door has weakened. Now you only need to answer whether there exists at least one construction scheme such that the constructed several convex polyhedra satisfy the above conditions.
Note:
Each test file contains multiple test cases. The first line contains the number of test cases $$$T$$$ ($$$1 \leq T \leq 10^4$$$). The description of the test cases follows.
The first line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$0 \leq x, y \leq 10^{18}$$$), indicating the number of regular triangles and squares to be used.
For each test case, output "Yes" if there exists at least one valid construction scheme for the given $$$x, y$$$, otherwise output "No".
54 64 102 30 00 5
Yes Yes Yes Yes No
You are given a string $$$s = s_1 s_2 \dots s_n$$$ consisting of lowercase English letters.
For $$$1 \leq i \leq j \leq n$$$, let $$$s_{i,j}$$$ be the substring of $$$s$$$ from position $$$i$$$ to $$$j$$$ inclusive.
Consider the integer points on the number line: $$$0, 1, \dots, n$$$. From a position $$$x$$$, you may move to another position $$$y$$$ if and only if the substring $$$s_{\,\min(x,y)+1,\; \max(x,y)}$$$ is a palindrome.
You are given $$$q$$$ queries. In the $$$i$$$-th query, you start at position $$$a_i$$$. For each query, determine the minimum number of moves required to reach either position $$$0$$$ or position $$$n$$$.
There is only one test case in each test file.
The first line contains the string $$$s$$$ ($$$1 \leq n \leq 10^6$$$, where $$$n$$$ is the length of $$$s$$$).
The second line contains an integer $$$q$$$ ($$$1 \leq q \leq 10^6$$$).
The third line contains $$$q$$$ integers $$$a_1, a_2, \dots, a_q$$$ ($$$0 \leq a_i \leq n$$$), the starting positions.
Print $$$q$$$ integers in a single line, where the $$$i$$$-th integer is the answer to the $$$i$$$-th query.
abcdce22 3
2 3
daabaddabaaaxy22 9
2 3
aabaaaqwertyuiopsdfghjklzxcvnm24 6
2 2
In the third example, one possible way to move to position $$$0$$$ (for both queries) is as follows.