After a terrible storm, you receive the locations of $$$n$$$ people waiting to be rescued. To protect them until help arrives, you can create a safety zone in the shape of an equilateral triangle. The safety zone may be placed anywhere and rotated by any angle. A person is protected if their location is inside the triangle or on its boundary.
Since a larger safety zone requires more energy, you want to make it as small as possible. Find the minimum possible area of a safety zone that protects all $$$n$$$ people.
Recall that a triangle is called an equilateral triangle if and only if all its sides have the same length.
The first line contains an integer $$$n$$$.
The $$$i$$$-th line of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$, representing the location of the $$$i$$$-th person.
Output the minimum possible area.
Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$.
3 3 0 4 4 0 3
7.79422863405994782087
5 -1 -2 3 1 0 4 4 3 -3 2
34.02164503538821491556
2 -1000000000 -1000000000 1000000000 1000000000
3464101615137754587.05489268301174473389
The following figure shows a minimum-area safety zone for the first sample. The red points represent the locations of the three people.
Watermelon is a kindergarten teacher who needs to invent games for her energetic kids.
One day, she invented a new game. First, she gathered $$$n$$$ kids and drew a circle on the ground. Every kid is out of the circle initially. The $$$i$$$-th kid is assigned a number $$$a_i$$$. She then announced the rules:
Since the kids are cooperative, they will share the cookie after the game ends. Thus, they only care about the total number of the cookie that Watermelon will give out. Please help the kids determine the order in which they should be called so that they obtain the maximum number of cookies.
Note that if a circle contains no kids, it is considered to be divided into groups of size $$$x$$$ for any positive integer $$$x$$$.
The first line contains a positive integer $$$n$$$, the number of kids. The second line contains $$$n$$$ positive integers $$$a_1, a_2, \ldots, a_n$$$, representing the numbers assigned to each kid.
Output two lines. The first line is the maximum number of cookies they can receive. The second line is a permutation of size $$$n$$$, representing the order they should be called. The kids are numbered from $$$1$$$ to $$$n$$$.
If there are multiple solutions, you can output any of them.
4 1 1 3 4
4 2 1 3 4
8 1 2 3 4 1 2 3 4
6 1 2 3 4 7 8 5 6
In the second sample, the kids can guarantee to earn $$$6$$$ cookies if the teacher calls the kids in the following order $$$1, 2, 3, 4, 7, 8, 5, 6$$$:
You are given a simple undirected graph $$$G$$$ with $$$n$$$ vertices and $$$m$$$ black edges. The $$$i$$$-th black edge connects vertices $$$u_i$$$ and $$$v_i$$$. For every pair of distinct vertices not connected by a black edge, there is a white edge between them.
We call two distinct vertices $$$x$$$ and $$$y$$$ close to gray if both of the following conditions hold:
Find the number of unordered pairs $$$(x, y)$$$, where $$$1 \leq x \lt y \leq n$$$, that are close to gray.
The first line contains two integers $$$n$$$ and $$$m$$$, representing the number of vertices and black edges, respectively.
Each of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$, indicating that there is a black edge between them.
Output a single integer representing the number of unordered pairs that are close to gray.
4 41 32 31 42 4
2
6 111 21 31 52 32 42 63 43 53 64 55 6
4
252525 0
0
You are given an array $$$a$$$ of length $$$n$$$. For each $$$k$$$ from $$$2$$$ to $$$n$$$, find the maximum distance between two distinct elements within any subarray of length $$$k$$$.
Formally, for each $$$k$$$, we want to find:
$$$$$$\max \{ j - i \mid 1 \le i \lt j \le n, \, j - i \lt k, \, a_i \neq a_j \}$$$$$$
If no such pair of distinct elements exists for a given $$$k$$$, the answer is $$$-1$$$.
The first line contains a single integer $$$n$$$, representing the length of the array.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, representing the elements of the array.
Output $$$n-1$$$ space-separated integers on a single line. The $$$i$$$-th integer should be the answer for $$$k = i + 1$$$.
41 2 1 2
1 1 3
53 3 3 3 3
-1 -1 -1 -1
51 2 3 4 5
1 2 3 4
This story is a work of fiction. Any similarity to real persons or events is coincidental.
After competing in the regional contest of the Fruit Kingdom, the team vegetable_advantages officially concluded their ICPC journey. However, this was also a new beginning because they wanted to give back to society: whenever they saw a problem, they would try to make it harder. While waiting for their flight back from the Fruit Kingdom to the Vegetable Kingdom, they happened to see this problem (which originated from 2025 National High School Programming Contest Problem C):
"A-Ming bought an electric vehicle with a battery capacity of $$$B$$$. A-Ming knows the initial battery level $$$b$$$, and he wants to plan a route from the starting point $$$s$$$ to the destination $$$t$$$ such that the total charging cost is as low as possible.
The electric vehicle consumes battery power on certain road segments (such as flat roads or uphill roads) and charges the battery on other road segments (such as downhill roads). These charging road segments are completely free of charge. We represent the map using a directed graph, where the weight of an edge indicates the increase or decrease in battery level when driving across this edge. If driving through this edge charges the battery, the weight is a positive integer; conversely, if it consumes power, the weight is a negative integer. We assume that the graph has no positive cycles.
While driving, the electric vehicle's battery level must always remain greater than or equal to $$$0$$$, and no matter how much it charges, the battery level can at most reach $$$B$$$. More specifically, let $$$p$$$ be the current battery level of the electric vehicle, and consider an edge with weight $$$w$$$: if $$$w$$$ is non-negative, the electric vehicle can definitely drive through this edge (even if the battery level $$$p=0$$$), and the remaining battery level will be $$$\min(B, p+w)$$$; if $$$w$$$ is negative and $$$p+w \ge 0$$$, the electric vehicle can drive through this edge, and the remaining battery level after passing through will be $$$p+w$$$; however, if $$$p+w \lt 0$$$, the electric vehicle cannot drive through this edge.
Some vertices on the map are charging stations. A-Ming can pass through multiple charging stations. Because charging takes time to find a charging pile, A-Ming decided to use at most one charging station to charge during the entire trip. Charging one unit of battery at a charging station costs one dollar. A-Ming's goal is to reach the destination while spending the least amount of money."
Cabbage, a member of vegetable_advantages, felt that A-Ming was far too lazy for being willing to use only one charging station! Besides, isn't A-Ming curious about the answers from the fixed starting point $$$s$$$ to every possible destination $$$t=1,2,\dots,n$$$? Therefore, on the plane, they solved the version where one can charge at any number of charging stations and must output the answers for $$$t=1,2,\dots,n$$$, and decided to use it to test you.
The first line contains three integers $$$n,m,s$$$, representing the number of vertices in the map, the number of edges, and the vertex index of the starting point, respectively.
The second line contains two integers $$$B,b$$$, representing the battery capacity and the initial battery level, respectively.
The following $$$m$$$ lines each contain three integers $$$u_i,v_i,w_i$$$, representing that the $$$i$$$-th edge goes from vertex $$$u_i$$$ to vertex $$$v_i$$$ with weight $$$w_i$$$.
The first integer of the last line is $$$g$$$, representing the number of charging stations. This is followed by $$$g$$$ integers $$$p_1,p_2,\dots,p_g$$$, representing the vertex indices where the charging stations are located.
Output $$$n$$$ integers, where the $$$i$$$-th integer represents the minimum amount of money A-Ming must spend to reach the destination when he can use any number of charging stations and $$$t = i$$$. If it is impossible to reach $$$t$$$, output $$$-1$$$.
7 7 1100 201 2 -102 3 -53 4 -203 5 -304 6 -405 6 -106 7 201 3
0 0 0 15 25 35 35
5 4 11 01 2 -12 3 -13 4 -14 5 -15 1 2 3 4 5
0 1 2 3 4
4 3 21 02 1 01 3 -14 1 10
0 0 -1 -1
In Sample 1, A-Ming is initially at vertex $$$s=1$$$ with an initial battery level of $$$b=20$$$.
For each $$$t \in \{1, 2, 3\}$$$, there is only one path from $$$s$$$ to $$$t$$$, and the initial battery level is sufficient for A-Ming to complete the journey without charging.
For each $$$t \in \{4, 5\}$$$, there is also only one path from $$$s$$$ to $$$t$$$, but the initial battery level is insufficient. Thus, A-Ming has to charge his battery at vertex 3, which costs 15 dollars for $$$t=4$$$ and 25 dollars for $$$t=5$$$.
For $$$t=6$$$, A-Ming takes the path $$$1 \to 2 \to 3 \to 5 \to 6$$$. He charges at vertex 3 up to a battery level of 40 so that he can traverse the edges $$$(3,5)$$$ and $$$(5,6)$$$. This costs 35 dollars.
For $$$t=7$$$, A-Ming takes the path $$$1 \to 2 \to 3 \to 5 \to 6 \to 7$$$ and spends 35 dollars at vertex 3.
Flowers may eventually wither, but the shapes they once formed can still be preserved on paper.
A botanist wishes to recreate a garden with as many flowers as possible on an $$$n \times m$$$ grid. Every cell of the grid must belong to exactly one flower. To represent a garden containing $$$k$$$ flowers, each cell is assigned an integer from $$$1$$$ to $$$k$$$, where a cell labeled $$$i$$$ belongs to the $$$i$$$-th flower.
Each of the $$$k$$$ flowers must occupy at least two cells. Moreover, for each flower, all cells belonging to it must be 4-connected. In other words, one must be able to travel between any two cells of the flower by moving only between side-adjacent cells belonging to the same flower.
The flowers in this garden must also be intertwined so that the grid cannot be separated into two parts by a single horizontal or vertical cut along grid lines. More precisely:
Find the maximum possible number of flowers and construct a corresponding grid.
The first line contains an integer $$$t$$$, denoting the number of test cases.
Each of the next $$$t$$$ lines contains two integers $$$n$$$ and $$$m$$$, denoting the number of rows and columns in that test case, respectively.
For each test case, first output an integer $$$k$$$, the maximum possible number of flowers.
Then, output $$$n$$$ lines, each containing $$$m$$$ integers. The integer in row $$$r$$$ and column $$$c$$$ must be the label of the flower occupying that cell. Every label from $$$1$$$ to $$$k$$$ must appear, and the resulting grid must satisfy all the conditions described above.
If there are multiple valid grids, output any one of them.
3 1 3 2 4 3 5
1 1 1 1 3 1 1 3 3 1 2 2 2 7 1 1 2 2 3 6 7 7 7 3 6 5 5 4 4
The following figure illustrates the output for the third test case in the sample. Cells with the same color and label belong to the same flower.
For example, flower $$$7$$$ occupies three consecutive cells in the second row. The figure also shows that every horizontal or vertical line between consecutive rows or columns is crossed by at least one flower. It can be shown that no valid garden can contain $$$8$$$ or more flowers.
Did you know that cactus graphs once enjoyed a glorious era in Taiwan's competitive programming scene, especially in 2022?
Looking back, the 2021 NTU ICPC Team Preliminary Contest featured a problem that required answering multiple shortest-path distance queries on a cactus graph$$$^{\text{∗}}$$$, marking the beginning of the Great Cactus Era. In 2022, cactus graphs reached the height of their glory. First, IOICamp featured a problem about answering maximum contiguous path-sum queries between pairs of vertices in a cactus graph$$$^{\text{†}}$$$. Later, YTP featured a problem asking for the maximum-weight independent set of a cactus graph$$$^{\text{‡}}$$$. Incidentally, this problem was so difficult that even the first-place team solved every problem except this one! Finally, that year's NTU ICPC Team Preliminary Contest featured yet another problem combining cactus graphs with linear basis$$$^{\text{§}}$$$.
Unfortunately, since 2023, cactus graphs have almost completely disappeared from programming contests in Taiwan. What a shame! Now, you are given a simple, connected, undirected cactus graph with $$$n$$$ vertices and $$$m$$$ edges. Each edge has a positive integer weight. There is a set of vertices $$$S$$$ which is initially empty. Please process $$$q$$$ operations. Each operation specifies a vertex $$$v$$$ and toggles whether $$$v$$$ belongs to $$$S$$$. That is,
After each operation, output the maximum shortest-path distance between any two vertices in $$$S$$$. That is, let $$$d(u,v)$$$ be the length of the shortest path between vertices $$$u$$$ and $$$v$$$. You should output
$$$$$$ \max_{u,v\in S} d(u,v) $$$$$$
after each operation. Note that if $$$|S| \leq 1$$$, the answer is defined to be $$$0$$$.
To make the problem more interesting, all operations are encoded and must be processed online. See the input format for details.
Note that an undirected graph is called a cactus graph if every edge belongs to at most one simple cycle.
$$$^{\text{∗}}$$$https://ntuj.csie.org/problems/3282
$$$^{\text{†}}$$$https://oj.ntucpc.org/problems/181
$$$^{\text{‡}}$$$https://oj.ntucpc.org/problems/329
$$$^{\text{§}}$$$https://ntuj.csie.org/problems/3463
The first line contains three integers $$$n$$$, $$$m$$$, and $$$q$$$, denoting the number of vertices, the number of edges, and the number of operations, respectively.
Each of the next $$$m$$$ lines contains three positive integers $$$u_i$$$, $$$v_i$$$, and $$$w_i$$$, indicating that there is an undirected edge of weight $$$w_i$$$ between vertices $$$u_i$$$ and $$$v_i$$$.
Each of the next $$$q$$$ lines contains an integer $$$x_i$$$, representing the encoded information for the $$$i$$$-th operation.
Let $$$\operatorname{ans}_i$$$ be the answer after the $$$i$$$-th operation, and let $$$y_i$$$ be the vertex that is actually toggled by the $$$i$$$-th operation. Then
$$$$$$ y_i=\operatorname{ans}_{i-1}\oplus x_i, $$$$$$
where $$$\oplus$$$ denotes the bitwise XOR operation. Also, we define $$$\operatorname{ans}_0=0$$$.
Output $$$q$$$ lines. The $$$i$$$-th line should contain one integer $$$\operatorname{ans}_i$$$, the maximum shortest-path distance between any two vertices in $$$S$$$ after the $$$i$$$-th operation.
15 19 2010 13 411 1 214 12 59 5 58 5 914 8 1015 11 513 3 36 3 613 6 1013 7 11 15 54 15 87 10 814 2 614 10 512 8 14 3 72 10 7614312736443742471730441813041683130
0 18 18 38 38 38 38 38 23 17 32 29 14 11 34 11 7 29 27 42
8 8 102 1 52 7 38 5 45 4 16 4 53 2 41 6 54 2 22826134915104
0 7 7 12 7 11 11 11 12 9
If you ask Doludu and DoIudu what their favorite shape is, they will definitely say it is a hexagon. Because they really love hexagons so much, no matter what object it is, they always want to design it in a hexagonal shape, and game items are no exception.
Today, they are going to play a game. In this game, there are $$$N$$$ hexagonal blocks lined up in a row at the beginning, and these hexagonal blocks are divided into blue and red. After the game starts, the two of them will take turns to make a move, with Doludu going first. When it is a player's turn, he must remove either the leftmost or the rightmost block among the currently remaining blocks. At the moment when only one block is left, the game ends. If this block is blue, Doludu wins; otherwise, DoIudu wins.
Both Doludu and DoIudu are extremely smart and will definitely use the optimal strategy to play this game. Based on the initial colors of the $$$N$$$ blocks, please determine who will win among them.
The first line of the input contains an integer $$$T$$$, representing that Doludu and DoIudu will play $$$T$$$ games.
Next are the details of each game in order. The information for a single game consists of two lines. The first line contains an integer $$$N$$$, representing the number of hexagonal blocks. The second line contains a string $$$S$$$ of length $$$N$$$, where the $$$i$$$-th character $$$S_i$$$ represents the color of the $$$i$$$-th block from the left, with B representing blue and R representing red.
Output $$$T$$$ lines, where the $$$i$$$-th line outputs the result of the $$$i$$$-th game. If Doludu wins, output Doludu; otherwise, output DoIudu.
45RRRRR4RBRB4BRRB20RBRBRBBRBRBBRRBRBRBR
DoIudu Doludu DoIudu Doludu
Given a permutation $$$a$$$ from $$$1$$$ to $$$n$$$, an inversion is defined as a pair of indices $$$(i, j)$$$ such that $$$1 \le i \lt j \le n$$$ and $$$a_i \gt a_j$$$.
Counting the total number of inversions is an extremely classic problem. In this problem, we consider a variation.
For each $$$k$$$ from $$$1$$$ to $$$n-1$$$, find the number of pairs $$$(i, j)$$$ such that $$$1 \le i \lt j \le n$$$, $$$j - i = k$$$, and $$$a_i \gt a_j$$$.
The first line contains a single integer $$$n$$$, representing the length of the sequence.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, representing the permutation.
Output $$$n-1$$$ space-separated integers on a single line. The $$$k$$$-th integer should represent the number of pairs $$$(i, j)$$$ satisfying the conditions for the given value of $$$k$$$.
53 1 4 5 2
2 1 0 1
66 5 4 3 2 1
5 4 3 2 1
In the Grand Palace of the Fruits Kingdom, there is a long ornamental chain consisting of $$$n$$$ decorative beads, numbered from $$$1$$$ to $$$n$$$ from left to right. Initially, all beads are uncolored.
To celebrate the upcoming Fruits' Festival, the King has invited $$$m$$$ jesters to color the beads. The jesters are numbered from $$$1$$$ to $$$m$$$. The coloring process runs for $$$m$$$ turns, in the $$$i$$$-th turn, the $$$i$$$-th jester must paint a contiguous segment of the beads with color $$$i$$$. However, the jesters do not view themselves as ordinary performers, instead, all of them are strictly adhered to the sacred number $$$k$$$, insisting that each jester's stroke must cover exactly $$$k$$$ beads. It follows that each jester has $$$n - k + 1$$$ choices in their turn.
Note that painting a colored bead will overwrite its original color.
Since the jesters are paid, the king does not want to see uncolored beads at the ceremony. Thus, all $$$n$$$ beads must be painted by at least one of the jesters by the end of the process.
Please help the King calculate the total number of possible final states of the beads that satisfy the condition above. Two final states are considered different if and only if there exists a bead $$$i$$$ that has a different color in the two states. Since the answer could be large, output it modulo $$$p$$$.
The input contains four integers $$$n, m, k, p$$$, representing the number of beads, the number of jesters, the sacred number and the modulo.
Output a single integer, representing the total number of valid final states modulo $$$p$$$.
4 2 3 588100627
2
5 3 2 573793837
10
4000 100000000 30 809157511
808328768
In the first sample, there are two possible coloring processes:
Note that they can't both paint beads $$$1, 2, 3$$$, since bead $$$4$$$ will be uncolored and violate the King's requirement.
Six seven. Six seven. 67, 67, 67.
"It's joever; pack it up," you thought. The brainrot trend has gone too far. It is the end of society.
First they came as skibidi toilet, and you did not speak out. Because you thought that was fine. Then they came as tralalero tralala, and you did not speak out. Because you thought that was nonsense. Then they released the game Tung Tung Sahur Extreme, and you did not speak out. Because you thought no one cared about that. Then they came as six seven, and there was no one left to speak for you. The problem title itself, you realized, it means "six seven" in Finnish. You are finished.
All that is left, is a string. You are given a string $$$S$$$. It can contain three different characters: six (6), seven (7), and — if you think that's enough, it is not — ASCII code 67: upper case Latin alphabet C.
This is your final chance. Even if it might be futile, you would still want to try for the very last time. You must replace each ASCII code 67 character with one of six (6) or seven (7). Your goal is to minimize the number of subsequences of the string that are identical to six seven (67).
Two subsequences are considered different if there exists at least one position that is in one but not the other.
The first line contains a single integer $$$t$$$, indicating the number of test cases. Next, $$$t$$$ lines follows. Each test case consists of single line containing the string $$$S$$$.
For each test case, output one line containing an integer, indicating the minimum number of subsequences that are identical to 67 after substituting all character with ASCII code 67.
567CCC6767C7CC676666C6767CC6C6C6777766766676677677C67C7766666666666766766C6666666C6677C77776777777C77677777777777
6 7 67 69 727
This is Mango's dark history.
One day, he learned about the linear sieve, an algorithm capable of computing multiplicative functions, such as $$$\phi(n)$$$ and $$$\mu(n)$$$, in linear time. However, when explaining this algorithm to his teammates, he made a silly mistake. He confused the abbreviation LPF with Largest Prime Factor, as it is quite natural for a non-native English speaker to mistake least for largest.
To ensure he never forgets this embarrassing moment, he brings this problem to you. Given an integer $$$r$$$, calculate $$$$$$ \sum_{i=1}^r G(i) $$$$$$ where $$$G(i)$$$ denotes the largest prime factor of $$$i$$$. He uses the letter $$$G$$$ because it is more common to call it Greatest Prime Factor.
For convenience, define $$$G(1) = 0$$$.
The only line of input contains a single integer $$$r$$$.
Output a single integer representing the value of $$$\displaystyle \sum_{i=1}^r G(i)$$$.
It is guaranteed that the answer fits within a standard signed 64-bit integer.
5
12
11
43
In the first sample, $$$\displaystyle \sum_{i=1}^5 = G(1) + G(2) + G(3) + G(4) + G(5) = 0 + 2 + 3 + 2 + 5 = 12$$$.
In the second sample, $$$\displaystyle \sum_{i=1}^{11} = 0 + 2 + 3 + 2 + 5 + 3 + 7 + 2 + 3 + 5 + 11 = 43$$$.
In a certain RPG, there are $$$n$$$ slimes lined up in a row, numbered from $$$1$$$ to $$$n$$$ from left to right. Each slime can be of water-type, fire-type, or elementless. The power of the $$$i$$$-th slime is represented by a signed integer $$$a_i$$$. Specifically:
As a player, you will use a water-type magic to attack these slimes. The mana cost to attack a single slime is determined as follows:
To minimize the total mana cost, you can perform at most $$$k$$$ merging operations before casting the magic:
After performing at most $$$k$$$ operations, you cast the magic. The total final cost will be the sum of the mana costs required to attack all remaining slimes.
Find the minimum possible total cost if you use an optimal strategy.
The first line contains two integers $$$n$$$ and $$$k$$$, representing the number of slimes and the maximum number of merging operations you can perform.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$, representing the initial represented powers of the slimes.
Output a single integer, representing the minimum possible total cost.
3 110 -10 5
5
5 23 -5 4 -2 6
7
4 25 -5 8 -8
0