The 17th Annual Donald Knuth Programming Contest has just begun, and the very first challenge, the problem A, sets the tone for the event. The problemsetters wanted to test not only algorithmic skill but also a deep intuition about numbers.
This year's opening problem comes with a playful story: Miguel, an enthusiast of mathematics, prepared a mysterious box containing slips of paper with numbers from $$$1$$$ to $$$n$$$. The task is simple at first glance: you may take any subset of these slips and add them together.
But there is a twist — Miguel was not interested in prime numbers. He believed primes were already "too famous," so he asked contestants to ignore them.
A number is called prime if it is greater than $$$1$$$ and has no positive divisors other than $$$1$$$ and itself. In particular, $$$1$$$ is not considered prime.
The question Miguel posed is: among all possible subset sums, what is the largest number that is not prime?
The input consists of a single integer $$$n$$$ ($$$1 \leq n \leq 10^9$$$).
Print the maximum subset sum that is not prime.
1
1
3
6
1000000000
500000000500000000
As everyone knows, the Legendary Huron loves playing with magnetic sticks of many colors. After finishing his colorful polygon constructions last Saturday, he now wants to store his sticks in boxes.
Legendary Huron owns $$$m$$$ magnetic sticks. Each stick has a color, represented by an integer in the range $$$[0, m-1]$$$. Sticks of the same color are indistinguishable.
He plans to store these sticks in $$$n$$$ distinct boxes. Each stick must be placed in exactly one box.
Once the sticks are distributed, Legendary Huron will hang the boxes in the form of a colorful rooted tree:
Legendary Huron defines the beauty of his arrangement as follows:
Legendary Huron is still undecided about two things:
He wants to know the total sum of beauties of the trees over all possible valid configurations. Two configurations are considered different if:
Note: The boxes are distinct (labeled), but the sticks of the same color are indistinguishable.
The file contains multiple test cases. The first line contains an integer $$$t$$$ $$$(1 \leq t \leq 10^5)$$$ — the number of test cases.
The first line of each test case description contains two integers $$$n$$$ and $$$m$$$ $$$(1 \leq n, m \leq 10^6)$$$.
The following line contains $$$m$$$ integers between $$$0$$$ and $$$m-1$$$, representing the colors of each magnetic stick.
It is guaranteed that the sum of all values of $$$m$$$ over the input does not exceed $$$10^6$$$.
For each test case, answer modulo $$$998244353$$$ — the total sum of beauties of the trees over all possible ways of distributing the sticks and constructing the colorful rooted tree.
31 30 1 22 30 1 23 30 1 2
3 28 351
110 73 0 1 2 0 3 1
335613770
After retiring from competitive programming, May decided to become a Pokémon trainer. She has $$$N$$$ Pokémon, and each Pokémon is described by three values: defense ($$$d$$$), health ($$$h$$$), and attack ($$$a$$$).
May is creating a training plan called Chained Training. Her Pokémon are arranged in a line, and the Pokémon at index $$$i$$$ can only fight a Pokémon at index $$$j$$$ such that $$$j \lt i$$$.
When Pokémon $$$i$$$ battles Pokémon $$$j$$$, Pokémon $$$i$$$ gains experience according to the following formula:
$$$$$$ \text{experience}(i, j) = d_j \cdot a_i + \frac{h_j}{d_i} $$$$$$
May has a gym battle soon, so she wants her Pokémon to gain as much experience as possible by fighting against a single Pokémon. For each Pokémon, determine the maximum amount of experience it can gain if she chooses its rival optimally.
The first line contains a single integer $$$N$$$ ($$$1 \leq N \leq 10^6$$$) — the number of Pokémon.
The next $$$N$$$ lines each contain three integers $$$d_i$$$, $$$h_i$$$, and $$$a_i$$$ ($$$1 \leq d_i, h_i, a_i \leq 10^6$$$) — the defense, health, and attack of the $$$i$$$-th Pokémon.
Print $$$N$$$ lines. For each Pokémon $$$i$$$, output two integers — the numerator and denominator of the irreducible fraction representing the maximum experience that the $$$i$$$-th Pokémon can gain.
54 3 175 2 54 1 301 2 28 1 10
0 1 103 5 301 2 12 1 201 4
51 2 103 3 218 2 341 1 164 2 53
0 1 65 3 819 8 130 1 849 2
11 2 3
0 1
Welcome to the XVII Annual Drone Kaleidoscope (DK for short)! A one-lane track is written as a string $$$s$$$ of $$$n$$$ lowercase characters.
During the annual DK, several teams, one by one, perform a show as follow:
A team's show value is $$$$$$ \text{(length of its palindrome)} \times \text{(number of drones it places)}. $$$$$$
Choose how many teams to field (each using a distinct palindrome) and, for each chosen team, pick the starting positions of its drones so as to maximize the total show value across all shows.
For example, if we have $$$s=abaaba$$$ and k=3, a valid show configuration is to field $$$3$$$ teams as follows:
The total show value in this example is $$$6+4+2=12$$$. Note that this is not the configuration that maximizes the total show value.
Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases.
The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1\leq n \leq 10^5$$$ and $$$1 \leq k \leq n$$$) — the length of string $$$s$$$ and the parameter $$$k$$$ described in the statements.
The following line contains a string $$$s$$$ of length $$$n$$$ consisting only of English lowercase characters.
The sum of $$$n$$$ over all test cases is guaranteed to be at most $$$10^5$$$.
Print one integer for each test case — the maximum total show value you can achieve.
47 2abacaba7 4abacaba7 6abacaba6 3abaaba
28 26 22 22
During the Segunda Fecha del Gran Premio de México, Ángel built his unusual computer prototype. Thanks to your help, he was able to complete his assignment, but a new defect has now appeared.
This time, the problem lies in the rotation instruction. Ángel wants to transform an array $$$A = [a_1, a_2, \dots, a_n]$$$ into another array $$$B = [b_1, b_2, \dots, b_n]$$$. He can perform the following operation any number of times:
For example, if $$$A = [12, 345, 67, 8, 9]$$$ and you apply the operation on the subarray $$$A[2,4]$$$ with $$$k=2$$$, the result is $$$[12, 567, 83, 4, 9]$$$.
Decide whether it is possible to transform $$$A$$$ into $$$B$$$ using a sequence of such operations. If it is possible, you must also output one valid sequence.
The file contains multiple test cases. The first line contains an integer $$$t$$$ $$$(1 \leq t \leq 5 \cdot 10^4)$$$ — the number of test cases.
The first line of each test case contains an integer $$$n$$$ ($$$1 \leq n \leq 5 \cdot 10^4$$$) — the size of the arrays $$$A$$$ and $$$B$$$.
The second line of each test case contains $$$n$$$ integers $$$A_1,A_2,\dots,A_n$$$ ($$$1 \leq A_i \lt 10^4$$$) — the array $$$A$$$.
The third lin of each test casee contains $$$n$$$ integers $$$B_1,B_2,\dots,B_n$$$ ($$$1 \leq B_i \lt 10^4$$$) — the array $$$B$$$.
It is guaranteed that no element of the arrays contains the digit $$$0$$$.
It is guaranteed that the sum of all values of $$$n$$$ over the input does not exceed $$$5 \cdot 10^4$$$.
For each test case, print an integer $$$m$$$ ($$$0 \leq m \leq 16n$$$) in the first line of your output if it is possible to transform array $$$A$$$ in array $$$B$$$; otherwise print $$$-1$$$.
If it is possible, then print $$$m$$$ lines with the operations. Each operation should be indicated with three integers $$$l$$$, $$$r$$$, and $$$k$$$ ($$$1 \leq l \leq r \leq n$$$ and $$$0 \leq k \leq 10^6$$$) — the parameters of each operation.
14123 45 6 78345 26 7 81
2 1 2 2 2 4 1
The coaches of TCMX 2025 are preparing the fifth contest. Racso came up with the idea of stealing the samples of the problems. Abraham and Ivan want to stop him, so they have protected the samples with laser beams. Each laser beam can be represented as a line segment from $$$(x_1,y_1)$$$ to $$$(x_2,y_2)$$$ parallel to one of the axis.
To verify how safe the test cases are, they will check $$$Q$$$ scenarios.
In each scenario, the initial position of Racso and the position of the samples are given. Racso will succeed in stealing the samples if he can move from his starting position to the place where the samples are located. Racso is represented as a point in the plane and fails if he touches any laser beam at any moment, even if it happens at the beginning or at the end of his path.
For each scenario, determine whether Racso can succeed.
The first line contains two integers $$$n$$$ and $$$Q$$$ ($$$1 \leq n \leq 1000$$$, $$$1 \leq Q \leq 10^6$$$) — the number of laser beams and the number of scenarios.
Each of the next $$$n$$$ lines contains four integers $$$x_1, y_1, x_2, y_2$$$ ($$$|x_1|, |y_1|, |x_2|, |y_2|\leq 10^9$$$), describing a laser beam represented by the segment between $$$(x_1,y_1)$$$ and $$$(x_2,y_2)$$$. Each beam is either vertical or horizontal (i.e. it has either $$$x_1=x_2$$$ or $$$y_1=y_2$$$).
Then follow $$$Q$$$ lines. Each line contains four integers $$$x_s, y_s, x_t, y_t$$$ ($$$|x_s|,|y_s|,|x_t|,|y_t|\leq 10^9$$$), indicating that the starting position of Racso is $$$(x_s,y_s)$$$ and the position of the samples is $$$(x_t,y_t)$$$.
For each scenario, print YES if Racso can succeed in reaching the samples, or NO otherwise.
4 30 0 0 54 0 4 5-1 1 5 1-1 4 5 42 2 5 52 2 4 42 2 3 3
NO NO YES
The Galactic Empire has organized its planets in a strict chain of command. At the very top lies Coruscant, the capital of the system. The planets are numbered from $$$1$$$ to $$$n$$$ according to their closeness to Coruscant (being Coruscat the planet with number $$$1$$$). Every other planet $$$u$$$ has a direct supervisor $$$p_u$$$ with $$$1 \le p_u \lt u$$$, forming a rooted tree with root in planet Coruscant.
Emperor Palpatine has decreed that all planets must be directly supervised by Coruscant, eliminating intermediate overseers (i.e., in the end every $$$u \ge 2$$$ must satisfy $$$p_u=1$$$).
You are granted the following operation:
Your task is to determine the minimum number of operations required so that, after applying them, every planet reports directly to Coruscant.
The first line contains an integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^5$$$) — the number of test cases.
Each test case begins with a line containing an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of planets. The second line contains $$$n-1$$$ integers $$$p_2, p_3, \dots, p_n$$$ ($$$1 \le p_i \lt i$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print a single integer: the minimum number of operations needed so that $$$p_u = 1$$$ for all $$$u = 2,3,\dots,n$$$.
341 2 141 2 241 2 3
1 2 2
In a previous Concurso Anual de Programación "Donald Knuth", Rem faced a peculiar problem: counting the number of different ways to travel across a hive-shaped grid.
The grid is formed by columns of hexagonal cells:
Thus, the grid consists of $$$2n+1$$$ columns of hexagonal cells, forming the shape of a "diamond hive." The entry cell is the single hexagon in the first column, and the storage cell is the single hexagon in the last column.
Below is an example of the grid with size $$$n=3$$$ with the valid set of movements for a single cell:
Rem solved the original challenge: finding how many paths exist from the entry to the storage without visiting the same hexagonal cell twice.
Later, he wondered: "If someone naïvely tried to simulate every single path with a brute-force depth-first search, how many total steps would such an algorithm actually take?"
In this brute-force simulation, for each valid path, a DFS would traverse the entire path from beginning to end. Therefore, the total cost of this approach equals the sum of the lengths of all distinct paths, where the length of a path is the number of hexagonal cells it visits.
Your task is to compute the sum of the lengths of all distinct paths.
The file contains multiple test cases. The first line contains an integer $$$t$$$ $$$(1 \leq t \leq 10^5)$$$ — the number of test cases.
Each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^6$$$) — the size of the hive.
For each test case, print a single integer — the answer to the problem, modulo $$$998244353$$$.
412310
14 432 24704 214063417
You are given $$$n$$$ identical biased coins. In each toss, every coin shows sol with probability $$$\tfrac{p}{q}$$$ and águila with probability $$$1 - \tfrac{p}{q}$$$. Initially, none of the coins has shown sol.
You perform the following process:
Let $$$T$$$ denote the number of rounds until this happens. Your task is to compute the expected value of $$$T$$$.
The input consists of three integers $$$n$$$, $$$p$$$, and $$$q$$$ ($$$1 \leq n \leq 10^7$$$, $$$1 \leq p \leq q \lt 998244353$$$), where $$$\tfrac{p}{q}$$$ is the probability that a coin shows heads in a single toss.
Print a single integer: the expected number of rounds until all $$$n$$$ coins have shown sol, modulo $$$998244353$$$.
1 1 2
2
2 1 2
665496238
You are given a directed graph $$$G = (V,E)$$$ with $$$n$$$ nodes. The set of vertices is $$$V = \{1,2,\dots,n\}$$$. For every node $$$u \in V$$$, there is a directed edge from $$$u$$$ to each of its proper divisors. Formally: $$$$$$ (u,v) \in E \quad \text{iff} \quad v \mid u \quad \text{and} \quad 1 \leq v \lt u. $$$$$$
For each node $$$u$$$, define:
The length of a path is the number of edges it contains.
Your task is to compute the following sum: $$$$$$ S(n) = \sum_{u=1}^{n} \max\big(I(u), O(u)\big). $$$$$$
The input consists of a single integer $$$n$$$ ($$$1 \leq n \leq 10^{10}$$$).
Output a single integer — the value of $$$S(n)$$$.
1
0
3
3
4
6
5
7
José is practicing graph and tree problems on the HuronCoder platform, and he came across the following challenge.
You are given a tree with $$$n$$$ nodes, rooted at node $$$1$$$. The parent of each node $$$u$$$ is $$$p_u$$$, with $$$1 \le p_u \lt u$$$. Each node $$$u$$$ has an associated value $$$a_u$$$.
You need to process queries of three types:
José needs your help.
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$ and $$$1 \leq q \leq 2 \cdot 10^5$$$) — the number of nodes in the tree and the number of queries.
The second line contains $$$n-1$$$ integers $$$p_2,p_2,\dots,p_n$$$ ($$$1 \leq p_i \lt i$$$) — the parent of each node.
The third line contains $$$n$$$ integers $$$a_1,a_2\dots,a_n$$$ ($$$|a_i| \leq 10^9$$$) — the initial values of each node.
The next $$$q$$$ lines describe the queries:
For each query of type $$$2$$$, print an integer representing the answer to this query.
5 71 1 3 33 -5 2 -1 -32 4 53 2 5 12 4 51 2 12 4 53 4 5 22 4 2
2 -1 1 4
Leo is now training for the ICPC 2025 Regional Finals. As part of his preparation, he uses the popular site HuronCoder, which contains many problems with different tags.
The site has $$$N$$$ problems numbered from $$$1$$$ to $$$N$$$ and $$$M$$$ tags numbered from $$$1$$$ to $$$M$$$. Each problem can be tagged with any subset of tags (including the empty subset or the full set of tags).
Leo wonders if it is possible to pick a subset of problems so that, when combined, they cover all tags.
The first line contains two integers $$$N, M$$$ ($$$1 \le N \le 20$$$ and $$$1 \le M \le 20$$$) — the number of problems and the number of tags.
Each of the next $$$N$$$ lines contains $$$M$$$ integers, where the $$$j$$$-th integer is $$$1$$$ if the problem has tag $$$j$$$, and $$$0$$$ otherwise.
Print "Yes" if it is possible to choose a subset of problems that covers all $$$M$$$ tags. Otherwise, print "No".
4 41 0 0 00 1 0 00 0 1 00 0 0 1
Yes
3 41 0 0 00 1 0 00 0 1 0
No
3 20 00 11 1
Yes
3 30 1 01 1 10 1 0
Yes
Okarun is studying alien art. Alien art is represented by a square matrix that contains blank characters, digits from $$$1$$$ to $$$9$$$, the symbol '$$$+$$$', or the symbol '$$$*$$$'. We call such square matrix an art matrix, and it is defined recursively as follows:
The inner submatrix is obtained by removing the leftmost and rightmost columns, as well as the top and bottom rows. Two art matrices are considered non-adjacent if there is at least one blank character adjacent to every cell on their borders.
Each art matrix has an associated beauty value $$$b$$$, defined as:
Given an art matrix, tell Okarun the beauty of the matrix.
The first line contains a single integer $$$N$$$ ($$$1 \leq N \leq 1000$$$) — the size of the matrix.
The next $$$N$$$ lines each contain a string of $$$N$$$ characters representing the $$$i$$$-th row of the matrix. Blank cells are represented by the '.' character.
Print a single integer $$$b$$$ — the beauty of the Alien art matrix modulo $$$10^9 + 7$$$.
6+....+.3.4....1.......2..1.1..+....+
12
11+.........+.*...*.......3.3..+.+....2....5....3.3..+.+..*...*...................+.+.*.*.....1...4.....+.+.*.*.+.........+
172
2****
1
12
2