You are the host of a street experiment show called TOPC (Take Or Pass Challenge). In the show, you approach people one by one and offer them some money. Each person can either:
The game starts with $$$x$$$ dollars. You go down the street and meet a random person. You would like to predict in advance what each of them will say — either "take" or "double".
You have a limited budget of $$$d$$$ dollars in your pocket. If, at any point, a person's decision could result in an amount strictly greater than your available funds $$$d$$$, you must force the person to say "take it", regardless of what they wanted to say. Taiwanese people are very kind-hearted, so whenever they have a choice, they would always say "double it" and pass the opportunity to others — unless they are forced to take the money.
Your task is to simulate the game and determine if the person you meet will take the money or double it and give it to the next person.
The input contains two integers $$$x$$$ and $$$d$$$.
Output a string "take it" or "double it".
123 246
double it
345 678
take it
789 101112
double it
In a distant Mathematical Kingdom, there stands an ancient gate known as the "Twin Gate." Legend has it that the gate will only open when two "Twin Guardians" stand before it at the same time.
The king has summoned a wise adventurer to help determine whether the two given numbers are "Twin Primes." If they genuinely are Twin Guardians (i.e., Twin Primes), the gate will shine brightly and open; otherwise, it will remain firmly shut.
Definition of Twin Primes: If two numbers $$$i$$$ and $$$i+2$$$ are both prime, they are called "twin primes."
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$. The description of the test cases follows.
The only line of each test case contains two integers $$$a$$$ and $$$b$$$, representing a task assigned by the king that asks you to check these two numbers.
For each test case, if $$$a$$$ and $$$b$$$ are twin primes, output Y (indicating "Yes, the Twin Gate opens!"); otherwise, output N.
52 311 1312 143 55 7
N Y N Y Y
517 1929 3115 1719 2141 43
Y Y N N Y
Mitty is a brave adventurer exploring a mysterious underground cave system known as The Abyss. The Abyss is composed of $$$n$$$ parallel vertical shafts and $$$m$$$ horizontal tunnels. Each tunnel connects exactly two shafts at the same depth. All $$$m$$$ tunnels have distinct depths, and surprisingly, there is a treasure in the middle of every tunnel!
Mitty can pick any shaft to start with. He moves downward from the top of the chosen shaft, obeying the following rules:
These treasures in the tunnels have various values. Mitty wants to collect as much treasure as possible. Please help him calculate the maximum total value of treasures he can collect when starting from one of the shafts.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$. The description of the test cases follows.
The first line contains two integers $$$n$$$ and $$$m$$$, representing the number of vertical shafts and horizontal tunnels, respectively.
Each of the following $$$m$$$ lines contains three integers $$$x$$$, $$$y$$$ and $$$v$$$, representing a horizontal tunnel at a certain depth that connects shafts numbered $$$x$$$ and $$$y$$$, and contains a treasure worth $$$v$$$.
The horizontal tunnels are given from top to bottom. This implies that no two horizontal tunnels situated at the same depth.
For each test case, print a single integer, representing the maximum total value of treasures Mitty can collect.
13 31 2 32 3 41 3 9
16
25 81 4 51 3 41 3 21 3 92 4 11 3 22 3 01 5 67 25 6 165 7 4
28 20
The edit distance between two strings $$$u$$$ and $$$v$$$ is the minimum number of edit operations that turns $$$u$$$ into $$$v$$$. There are three edit operations that can be applied to a string: Insert a character, delete a character, and substitute some character by a different one.
For example, we can turn hello into world with four substitutions, so the edit distance is at most $$$4$$$. You can turn wally into walter with two substitutions and one insertion, so the edit distance is at most $$$3$$$. Computing the edit distance between two strings is a well-known problem with many applications.
The task at hand is to compute the edit distance of a string to one of the closest palindromes. A palindrom is a string that is the same when read backwords, for example madam.
The edit distance of hello to the closest palindrome is only $$$2$$$: We can turn hello into ollo, or hllh, or elle with two edit operations.
Write a program that can find the distance of a word to a closest palindrome.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$. The description of the test cases follows.
The only line of each test case contains a word $$$w$$$ consisting only of lowercase letters.
For each test case, output the edit distance of the input word $$$w$$$ to its closest palindrome.
6aaaabahellopalindromeabbaxbababac
1 2 5 0 0 1
Barbara has a garden. The garden can be represented as a $$$n \times m$$$ grid. She has placed $$$k$$$ slabstones in the garden, so she can enjoy stepping slabstones from one to another every day. The slabstones are indexed from $$$1$$$ to $$$k$$$. Each slabstone fully occupies some cell of the garden, and no two slabstones are placed at the same cell.
However, an evil person, Babara, is going to place a special device that will occupy a rectangular region in the garden. If any slabstone overlaps with the device, it will explode and destroy the whole garden!
To avoid the explosion, Barbara wants to rearrange the slabstones by shifting the slabstones within the garden. The slabstones should remain non-overlapping during slabstone rearrangement. The energy spent is equal to the largest index among all slabstones that have been moved. Now Barbara wants to know the minimum energy required to rearrange the slabstones, so she can save her energy for "other purposes".
For example, suppose the device will be placed at the blue rectangle. Then Barbara can rearrange the slabstones as follows. Please note that the slabstones do not overlap during the whole process, not only after the rearrangement. All slabstones that have been moved have index at most $$$4$$$, so the energy spent is $$$4$$$.
The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$.
Followed by $$$k$$$ lines, the $$$i$$$-th of which contains two integers $$$x_i$$$ and $$$y_i$$$, representing that the $$$i$$$-th slabstone is located at the $$$y_i$$$-th cell of the $$$x_i$$$-th row.
The last line contains four integers $$$u_1$$$, $$$v_1$$$, $$$u_2$$$ and $$$v_2$$$, representing that the top-left corner of the device is located at the $$$v_1$$$-th cell of the $$$u_1$$$-th row, and the bottom-right corner of the device is located at the $$$v_2$$$-th cell of the $$$u_2$$$-th row.
Print the minimum energy required to rearrange the slabstones. If no slabstones need to be moved, the energy spent is $$$0$$$. If the explosion cannot be avoided, just output $$$-1$$$.
3 5 81 41 52 32 23 33 42 51 21 3 1 5
4
3 3 12 21 1 3 3
-1
Generated by ChatGPT 4o Alice and Bob run a fruit business that delivers delicious fruits to customers around the world. Their brand showcases four iconic fruits from Taiwan: guava, lychee, mango, and sugar apple.
They've opened multiple branches, and each branch uses the same layout to display fruits. Each day, the storefront of every shop is arranged as a $$$4 \times 4$$$ grid. The grid displays fruits such that every row and every column contains exactly one of each of the four different fruits—no duplicates per row or column.
To ensure consistency across branches, every evening, Alice and Bob prepare a $$$4 \times 4$$$ fruit box with a valid display layout and have their employees deliver the boxes to each branch the next morning.
Alice and Bob have two children, Charlie and Dianne, who love eating fruit. One evening, they sneak into the office and find the prepared fruit boxes sitting on the table.
Charlie can't resist—he grabs one fruit and eats it. "Mmm... I wish I could eat as many as possible without confusing the staff tomorrow," he thinks.
Dianne, noticing her brother's mischief and feeling hungry herself, takes another fruit. Soon, the two begin taking turns, each grabbing one fruit at a time from the box.
Their rule is simple: They can only take a fruit if, after removing it, the remaining fruits in the box still uniquely determine the original $$$4 \times 4$$$ display (i.e., there's only one way to complete the display while preserving the distinct-fruit-per-row-and-column rule).
Neither Charlie nor Dianne wants to be the one who can't grab a fruit anymore. So they both follow the same strategy:
"If there's a way I can eventually grab the last fruit without violating the rule, I'll take it, and I'll try to maximize the total number of fruits I can eat. But if my sibling could outsmart me and grab the last fruit, I'll instead try to minimize the number of fruits they can take."
Now, you are given the current status of the fruit box. That is, some fruits are already taken under the rule. Your task is to write a program that predicts the total number of fruits that will remain in the box after both kids have played optimally.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$. The description of the test cases follows.
Each test case contains four lines, the $$$i$$$-th of which contains a string of length four, representing the $$$i$$$-th row of the layout of the fruit box. Each character will be in the set $$$\{$$$G, L, M, S, X$$$\}$$$, indicating each of the four fruits, or an empty slot denoted by X.
For each test case, please output the specified answer in each line.
2GLMSLSGMMGXLSMLGGLMSLMSGMSXLSGLM
5 5
Picture of Bafuko for reference (X @bafuko_seiso) Bafuko loves gaming! She is currently playing the latest title, Young Bracelet – Light of the Saint Grass. As an experienced gamer, she wants to explore every location on the map.
The map can be represented as a tree with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. Each edge of the tree has weight $$$1$$$. In addition, there is a special portal connecting two designated distinct vertices $$$x$$$ and $$$y$$$. This portal can be used any number of times and allows instantaneous travel between $$$x$$$ and $$$y$$$ at zero cost.
Bafuko wants to visit every vertex of the tree at least once by following a tour. A tour is a sequence of adjacent vertices that begins at a chosen vertex, ends at a chosen vertex (possibly the same one), and visits every vertex of the tree at least once. She may freely choose the starting and ending vertices. During the tour, both edges and the portal may be traversed multiple times if necessary. The cost of the tour is defined as the sum of the costs of all traversed edges and portal uses, where repeated traversals are counted multiple times.
Your task is to determine the minimum total cost of such a tour.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$. The description of the test cases follows.
The first line of each test case contains three integers $$$n$$$, $$$x$$$, and $$$y$$$, representing the number of vertices in the tree and the two vertices connected by the portal.
Followed by $$$n - 1$$$ lines, the $$$i$$$-th of which contains two integers $$$u_i, v_i$$$, representing an edge between vertices $$$u_i$$$ and $$$v_i$$$.
For each test case, print a single integer in a new line, representing the minimum total cost of a tour that visits every vertex of the tree at least once.
34 1 31 22 32 48 1 31 22 32 44 77 84 55 68 1 21 22 33 44 53 66 77 8
2 8 7
Chisato works at a traditional Japanese restaurant that just received a shipment of beautifully handcrafted chopsticks. There are $$$m$$$ different types of chopsticks, and for each type $$$i$$$ ($$$1 \le i \le m$$$), there are exactly $$$k_i$$$ chopsticks.
Tonight, $$$n$$$ guests have arrived, and each guest needs exactly one pair of chopsticks. Since no type of chopstick has at least $$$2n$$$ pieces, Chisato decides to randomly select $$$2n$$$ chopsticks from the full collection, which contains $$$s = \sum_{i=1}^{m} k_i$$$ chopsticks in total.
After selecting the $$$2n$$$ chopsticks, Chisato will try to distribute them in a way that maximizes the number of guests receiving a matching pair, that is, two chopsticks of the same type. If it's not possible to provide matching pairs for everyone, some guests will receive mismatched pairs.
Your task is to compute the expected number of guests who receive mismatched pairs of chopsticks under this strategy.
The first line contains two integers $$$n$$$ and $$$m$$$, representing the number of people and the number of the chopstick type, respectively.
The second line contains $$$m$$$ integers, the $$$i$$$-th integer $$$k_i$$$ represents the number of chopstick for the $$$i$$$-th type.
Print a single integer, the expected number of people who cannot get a pair of chopsticks of the same type, multiplied by $$$\binom{s}{2n}$$$ (where $$$s = \sum_{i=1}^{m} k_i$$$). It can be proven that this product is an integer. Output the result modulo $$$998244353$$$.
3 32 2 2
0
5 33 3 4
1
5 28 8
4032
In a high-tech industrial facility, a series of nuclear reactors are arranged in a linear configuration. Each reactor operates under strict pressure regulations to ensure safety and efficiency. To prevent critical failures, each reactor has a specific maximum pressure limit. When a reactor's internal pressure reaches or exceeds this limit, a controlled pressure release (venting) is initiated. This system requires sophisticated management due to dynamic operational adjustments and the need for continuous monitoring.
You are tasked with designing and implementing a system to manage the pressure of a line of $$$n$$$ reactors. Each reactor, indexed from $$$1$$$ to $$$n$$$, has an initial maximum pressure limit $$$p_i$$$. All of the reactors' initial pressure are $$$0$$$. The system must support two types of operations:
The first line contains two integers $$$n$$$ and $$$q$$$, representing the number of reactors and the number of operations, respectively.
The second line contains $$$n$$$ integers, the $$$i$$$-th integer $$$p_i$$$ represents the initial maximum pressure limit of the $$$i$$$-th reactor.
The following $$$q$$$ lines describe the operations. Each line begins with an integer $$$op$$$.
For each query that $$$op = 2$$$, print a single integer on a new line, representing the total number of venting operations that have occurred among all reactors within the specified range since the beginning of the system's operation.
10 55 10 23 45 10 45 65 10 68 91 5 10 6641 2 9 52 4 101 8 8 52 1 10
8 9
10 1079 26 9 28 13 40 26 54 69 191 1 5 61 5 7 22 4 71 9 10 192 5 71 5 7 272 10 102 9 91 6 6 201 3 8 6
0 0 1 0
10 1056 29 49 42 47 21 23 54 8 312 9 91 5 6 232 6 72 4 71 5 6 682 1 92 3 61 2 10 892 6 81 3 6 53
0 1 1 3 3 5
Alex is planning rest area placements on a simplified model of Taiwan's freeway system. The system contains $$$n$$$ interchanges, connected by $$$n - 1$$$ bidirectional roads. The network is connected, and there is exactly one shortest route between any pair of interchanges. The $$$i$$$-th road connects interchanges $$$u_i$$$ and $$$v_i$$$, and has a length of $$$l_i$$$.
Exactly $$$k$$$ rest areas with gas stations can be built, each located at an interchange. A driver may start a trip from any interchange and travel to any other, always following the unique shortest path. They begin each trip with a full tank of gas and can refuel only at interchanges that have a rest area.
Alex is curious about the smallest possible fuel tank capacity $$$d$$$ such that it's possible to place the $$$k$$$ rest areas in a way that ensures no driver will ever run out of gas. On any trip, the driver must never have to travel more than $$$d$$$ units along the path without passing through a rest area, including at the beginning or end of the journey. The goal is to figure out the minimum such $$$d$$$, assuming the rest areas are placed in the best possible way.
The first line contains two integer $$$n, k$$$.
Followed by $$$n - 1$$$ lines, the $$$i$$$-th of which contains three integers $$$u_i, v_i, l_i$$$, representing the $$$i$$$-th road connects interchanges $$$u_i$$$ and $$$v_i$$$ with a length $$$l_i$$$.
Output one integer, the smallest possible fuel tank capacity $$$d$$$.
5 11 2 31 5 22 3 32 4 1
5
5 21 2 31 5 22 3 32 4 1
3
You are given an $$$n \times n$$$ grid. Each cell initially contains some number of stones, such that the total number of stones is exactly $$$n^2$$$.
In one move, you may take a single stone and move it to any other cell in the same row or the same column.
Your goal is to minimize the number of moves needed to make each cell contain exactly one stone.
The first line contains an integer $$$n$$$, representing the size of the grid.
Followed by $$$n$$$ lines, the $$$i$$$-th of which contains $$$n$$$ integers, the $$$j$$$-th integer $$$a_{i, j}$$$ represents the number of stones in cell $$$(i, j)$$$.
Output a single integer, the minimum number of moves required to make each cell contain exactly one stone.
30 1 20 2 21 1 0
3
51 2 4 0 12 0 0 2 01 4 1 0 12 0 0 0 01 2 0 1 0
11
Alice is selling a new model of game console in the toy store. When selling a new console, Alice uses a stapler to attach the receipt to the box.
However, the stapler pierces the box at two points, causing damage along the entire line segment between those two points. If any part of this segment (including its endpoints) overlaps with the console's screen (including its boundary), it will be damaged and Alice must compensate for the console.
Fortunately, a laser reveals the exact position of the screen inside the box. The game console's screen is a rectangle with sides parallel to the coordinate axes. Its bottom-left corner is at $$$(x_l, y_l)$$$ and its top-right corner is at $$$(x_r, y_r)$$$.
Alice plans to staple the box at the points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$. Please help her determine whether the stapler will damage the screen. If it will, stop her immediately.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$. The description of the test cases follows.
The first line of each test case contains four integers $$$x_l, y_l, x_r, y_r$$$, representing the coordinates of the bottom-left and top-right corners of the screen.
The second line of each test case contains four integers $$$x_1, y_1, x_2, y_2$$$, representing the coordinates of the expected stapler penetration positions.
For each test case, print STOP on a line if Alice will damage the screen, and print OK otherwise.
30 0 5 50 10 10 00 0 5 56 6 6 70 0 5 51 1 2 2
STOP OK STOP