$$$112358$$$ is a team from Hunan University that participated in this year's ICPC WuHan station.
Why this name? Because zmy is very fond of playing a game called $$$112358$$$.
![]() | ![]() |
The rules of the game are as follows:
There are a total of 16 squares, and each move can shift the tiles up, down, left, or right. If two adjacent numbers satisfy the condition that they are consecutive terms in the $$$\bf{Fibonacci\ sequence}^\dagger$$$, they will merge into their sum, and the score increases by the value of the new number.
After each move, you can either add a new number $$$1$$$, or not add at all. Note that the newly added $$$1$$$ is not included in the total score.
Now, given a current board state of the $$$112358$$$ game, you are asked to output the current score modulo 998244353.
$$$^\dagger$$$ The Fibonacci sequence is defined as
$$$$$$F_1 = 1,\ \ \ F_2 = 1,\ \ \ F_i = F_{i-1} + F_{i-2} \ \text{for all } i \ge 3.$$$$$$
The first $$$10$$$ terms are $$$[1,\ 1,\ 2,\ 3,\ 5,\ 8,\ 13,\ 21,\ 34,\ 55].$$$
$$$4$$$ rows and $$$4$$$ columns, totaling $$$16$$$ numbers $$$a_{i,j}\ (0 \le a_{i,j} \le 10^6)$$$, indicating that the element in the i-th row and j-th column is $$$F_{a_{i,j}}$$$.
Specifically, $$$a_{i,j}=0$$$ means the current position is empty.
One line with a number — the current score, modulo $$$998244353$$$.
1 2 0 0 0 0 0 0 3 4 0 1 0 0 6 0
32
1. The real current board state is shown below:
| $$$1$$$ | $$$1$$$ | $$$0$$$ | $$$0$$$ |
| $$$0$$$ | $$$0$$$ | $$$0$$$ | $$$0$$$ |
| $$$2$$$ | $$$3$$$ | $$$0$$$ | $$$1$$$ |
| $$$0$$$ | $$$0$$$ | $$$8$$$ | $$$0$$$ |
Some examples about the rule of points:
2. In this problem, $$$a$$$ modulo $$$p$$$ refers to taking the remainder of $$$a$$$ after division by $$$p$$$. For example:
Reimu and Marisa are playing a card game, described as follows:
There are $$$2n$$$ cards in total, with numbers $$$1$$$ to $$$n$$$ each appearing twice. Each player holds $$$n$$$ cards. They take turns playing one card, with Reimu going first.
After a player plays a card, if there are at least $$$2$$$ cards in the pile, and the top card of the pile is the same as the bottom card of the pile, then that player takes all the cards from the pile and gets one point.
Now, given Reimu's card sequence, Marisa wants to minimize Reimu's score. Under optimal strategy, what is the minimum score Reimu can get?
First line: a positive integer $$$n\ (1\le n\le 10^5)$$$.
Second line: $$$n$$$ positive integers $$$a_i\ (1\le a_i\le n)$$$, with the guarantee that the same $$$a_i$$$ appears at most twice.
First line: a positive integer, indicating Reimu's minimum score.
Next line: $$$n$$$ positive integers $$$b_i$$$, such that the multiset $$$[a_1, ..., a_n, b_1, ..., b_n]$$$ contains each number from $$$1$$$ to $$$n$$$ exactly twice.
This represents while the card sequence is $$$[a_1, b_1, a_2, b_2, ..., a_n, b_n]$$$, Reimu's score is exactly the minimum.
If there are multiple answers, output any one of them.
3 1 2 3
0 2 3 1
4 1 2 1 4
1 2 4 3 3
Example 1:
The card sequence is $$$[1,2,2,3,3,1]$$$. Marisa gets $$$1$$$ point after the last $$$1$$$, Reimu gets $$$0$$$ points. So $$$0$$$ points is the minimum score.
Example 2: The card sequence is $$$[1,2,2,4,1,3,4,3]$$$. Reimu gets $$$1$$$ point after the second $$$1$$$, then Marisa gets $$$1$$$ point after the last $$$3$$$.
It is proved that there is no solution in which Reimu gets $$$0$$$ points, so $$$1$$$ point is the minimum score.
A caterpillar is a special type of tree.
First, it satisfies the basic property of a tree: it is a connected graph with $$$n$$$ vertices and $$$n-1$$$ edges. Then, the caterpillar can be divided into four parts: head, body, tail, and feet.
Among them, the head is vertex $$$1$$$, the body is a chain length of $$$len$$$ connected by vertices $$$2,3,\dots,len+1$$$ in order, the tail is vertex $$$len+2$$$, and the feet are other vertices directly connected to the body.
The head connects to the body, and the body connects to the tail. So vertices $$$1$$$ to $$$len+2$$$ are connected into a chain in order (There is a caterpillar with $$$n=14$$$ and $$$len=3$$$).
Now, you are given a caterpillar. You need to assign a weight to each vertex, which must be a permutation of $$$1$$$ to $$$n$$$. The edge weight is defined as the absolute difference between the weights of the two vertices it connects.
You need to construct a vertex weight scheme such that the edge weights is a permutation of $$$1$$$ to $$$n-1$$$.
The first line contains two positive integers $$$n$$$ and $$$len$$$ $$$(3 \le n \le 2\cdot 10^5,1\le len\le n-2)$$$. $$$n$$$ is the total number of vertices, and $$$len$$$ is the length of the body of the caterpillar.
Vertex $$$1$$$ is the head of the caterpillar, vertices $$$2$$$ to $$$len+1$$$ are the body, and vertex $$$len+2$$$ is the tail. Vertices $$$1$$$ to $$$len+2$$$ are connected into a chain in order.
The second line contains $$$n-len-2$$$ numbers $$$a_{i+len+2}\ (2\le a_{i+len+2}\le len+1)$$$, indicating vertex number $$$i+len+2$$$ and $$$a_{i+len+2}$$$ are connected.
Output one line with $$$n$$$ positive integers, which must be a permutation, so that after assigning vertex weights, the edge weights form a permutation of $$$1$$$ to $$$n-1$$$.
If there are multiple solutions, output any.
4 2
1 4 2 3
Evil Neuro loves cubes!
Inside the $$$n \times n \times n$$$ electronic toy chest, Evil controls cubes, each occupying a $$$1 \times 1 \times 1$$$ coordinate within the grid. Since the cubes are digital, they can defy gravity and float in mid-air. That is to say, Evil can place them anywhere inside the chest.
Evil wants the chest to look completely packed. To pull it off, the arrangement must be filled from every angle. Whether viewed from the front, the side, or above, the chest must appear as a solid $$$n \times n$$$ block, with no space left.
Unfortunately, Evil has a limited supply of cubes. She asks you to help her do this using the minimum number of cubes.
The first and only line contains a single integer $$$n$$$ $$$(1\le n\le 200)$$$ — the size of the chest.
First, print an integer $$$k$$$, indicating the minimum number of cubes required.
Then, print $$$k$$$ lines, each containing three integers $$$x, y, z\ (1\le x,y,z \le n)$$$, indicating the coordinates of the cube.
If there exist multiple answers, output any of them.
2
4 1 1 2 1 2 1 2 1 1 2 2 2
One possible construction of the sample is shown below:
Alice and Bob are playing a classic game known as "Dots and Boxes".
The game is played on a rectangular grid of size $$$n \times m$$$. The game proceeds as follows:
Alice and Bob have paused the game in a specific state. Currently, every single cell $$$(i, j)$$$ in the grid is surrounded by exactly 2 drawn edges and 2 empty edges. Since no cells are fully completed yet, the current score is $$$0-0$$$.
Given this board state, Alice takes the next move. Determine whether Alice has a winning strategy regardless of how Bob plays. Alice wins if Alice's score is strictly greater than Bob's score at the end of the game.
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1\le n \cdot m \le 4 \cdot 10^5$$$) — the number of rows and columns of the grid.
The next $$$n \cdot m$$$ lines of each test case describe the state of each cell. The lines are ordered cell by cell: row $$$1$$$ from left to right, then row $$$2$$$, and so on, up to row $$$n$$$.
Each line contains two characters $$$c_1, c_2$$$ ($$$c_1, c_2 \in $$$ { 'U', 'D', 'L', 'R' }), representing the positions of the two edges already drawn for that cell:
It is guaranteed that the input describes a valid grid topology (e.g., if cell $$$(1,1)$$$ has a right edge 'R', then cell $$$(1,2)$$$ must have a left edge 'L').
Additional constraints on the input: the sum of $$$n \cdot m$$$ over all test cases does not exceed $$$4 \cdot 10^5$$$.
For each case, print a single line containing "YES" if Alice has a winning strategy, and "NO" otherwise. The output is case-insensitive (e.g., "Yes", "yes", "yEs" are all accepted).
2 2 2 U L U R D L D R 3 3 U L U D U R L D U D D R L U U D U R
NO YES
In the first example, the board is a $$$2 \times 2$$$ grid. The outer boundary edges are drawn, but the internal cross is missing. Regardless of which internal edge Alice draws, she will not complete a square on that turn, and Bob will be able to take all $$$4$$$ squares in the subsequent turns.
The illustration of the first sample.
In the beautiful Sweet Apple Acres, Applejack is facing her busiest harvest season of the year. She has sorted the harvested apples into $$$n$$$ quality grades (numbered from $$$1$$$ to $$$n$$$). Specifically, she has harvested exactly $$$a_i$$$ apples of grade $$$i$$$.
However, the energetic Rainbow Dash decided to "help" out. She attempted to clear the fallen leaves quickly using her Sonic Rainboom. Unfortunately, she used a bit too much force, and a gust of wind blew away exactly $$$k$$$ apples, causing them to disappear.
As the Element of Honesty, Applejack didn't blame Rainbow Dash. However, she is now facing a mathematical puzzle. Among all possible ways to remove exactly $$$k$$$ apples, there are various outcomes for the "mode" (the most frequent grade) of the remaining pile. She wants to find the maximum possible grade that can serve as a mode in any such outcome.
The problem setter, Mofusigil, who is secretly snacking on apples nearby, wipes his mouth and throws this problem to you.
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n, q \le 2 \times 10^5$$$), representing the number of apple grades (the value range) and the number of queries.
The second line contains $$$n$$$ non-negative integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ ($$$0 \le a_i \le 10^6$$$) represents the initial count of apples of grade $$$i$$$.
The following $$$q$$$ lines each contain a single integer $$$k$$$ ($$$0 \le k \le \sum_{i=1}^{n} a_i$$$), representing the number of apples blown away by Rainbow Dash.The queries are independent.
It is guaranteed that the initial sequence contains at least one number.
For each query, output a single line containing one integer representing the maximum possible mode of the remaining sequence after removing exactly $$$k$$$ elements.
7 2 1 9 1 9 8 1 0 5 23
5 6
A grade $$$i$$$ is considered a mode if its frequency in the remaining pile is greater than or equal to the frequency of any other grade.
Joseph's game is like this: $$$N$$$ people are arranged in a circle clockwise, and each person's number is from $$$1$$$ to $$$N$$$. Starting from No. 1, one person is eliminated by $$$M$$$ people clockwise, leaving the last person as the winner, whose number is the lucky number.
Given $$$N$$$ and $$$M$$$, find the lucky number.
The first line contains one integer $$$T$$$ $$$(1\le T \le 10^4)$$$ — the number of test cases.
For each test case, there are $$$2$$$ integers $$$N$$$ and $$$M$$$ $$$(1\le N \le 10^8, 2\le M\le 10)$$$ in one line.
For each test case, output the lucky number.
3 10 2 10 3 1024 2
5 4 1
Case 1: The elimination order is $$$2, 4, 6, 8, 10, 3, 7, 1, 9$$$, so the lucky number is $$$5$$$.
Case 2: The elimination order is $$$3, 6, 9, 2, 7, 1, 8, 5, 10$$$, so the lucky number is $$$4$$$.
ACM is the world's most influential collegiate programming competition, known as the "Olympics of Programming," requiring teams to solve complex algorithmic problems efficiently within a limited time.
In the ACM competition, the rules for award allocation stipulate that gold, silver, and bronze medals are awarded to the top $$$10\%\ ,20\%$$$, and $$$30\%$$$ of valid teams, respectively, with rounding up applied.
Now given the number of valid teams $$$n$$$, please answer how many teams get gold, silver, and bronze medals, respectively.
One line containing a number $$$n\ (100 \le n \le 500)$$$.
One line containing $$$3$$$ numbers, the number of gold, silver, and bronze medals.
123
13 25 37
Mofusigil decides to test the wit of his old friend, Caiwen.
Mofusigil starts with a hidden original string of length $$$n$$$. He then performs exactly $$$m$$$ operations on it. In each operation, he selects a random position in the string and replaces the character at that position with a different lowercase English letter.
It is important to note that:
After performing these $$$m$$$ operations, Mofusigil shows Caiwen the final result string and asks: "Based on this final string, how many different original strings could I have started with?"
Mofusigil expected Caiwen to solve this instantly, but Caiwen stared at the string in total confusion. Seeing his friend's struggle, Mofusigil decided to go easy on him and guaranteed that the number of operations $$$m$$$ is very small: $$$m \le 2$$$.
Unfortunately, even with this simplification, Caiwen is completely stumped. Not wanting to lose face in front of Mofusigil, Caiwen has secretly asked for your help.
The first line contains two integers $$$n$$$ ($$$1 \le n \le 10^6$$$) and $$$m$$$ ($$$0 \le m \le 2$$$), denoting the length of the final string and the number of operations.
The second line contains the final string.
Print a number denoting the number of possible original strings.
4 1 momo
100
4 2 momo
3851
Neuro-sama has a single banknote with a value of $$$n$$$ dollars. She can repeatedly apply the following operation:
Neuro keeps applying this operation until all banknotes have a value less than $$$k$$$. Now she is interested in the number of $$$0$$$-dollar banknotes that she will have at the end. As Neuro-sama's fan, please find the answer for her.
You will be given $$$q$$$ independent queries. For each query, output the number of $$$0$$$-dollar banknotes obtained at the end, modulo $$$10^9+7$$$.
The first line contains an integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^{5}$$$) — the number of queries.
Then $$$2q$$$ lines follow, describing the queries:
The total number of integers $$$m$$$ across all queries satisfies $$$1\le \sum m \le 2\cdot 10^5$$$.
For each query, output a single integer — the answer modulo $$$10^9+7$$$.
13 101 2 1 0 0 2 1 2 1 2
972
In this problem, $$$a$$$ modulo $$$p$$$ refers to taking the remainder of $$$a$$$ after division by $$$p$$$. For example:
$$$~\\$$$
Neuro-sama and Evil are planning to build houses on a tree. The tree has $$$n$$$ nodes and $$$n-1$$$ edges. Each node is capable of hosting exactly one room, and together these rooms will form the house.
Evil, always overflowing with ideas, has drafted $$$q$$$ different construction plans. In each plan, she selects a set of nodes $$$S$$$, and places one room on every node in $$$S$$$. However, her sister Neuro has a rather strict aesthetic sense. For her, a construction plan is considered elegant only if all rooms form a single connected area — that is, for any two nodes $$$u, v \in S$$$, there exists a path between $$$u$$$ and $$$v$$$ entirely within $$$S$$$.
To keep the peace (and prevent Evil from storming off to draft even stranger structures), Neuro asks for your help: for each of the $$$q$$$ plans, determine whether the plan is elegant.
The first line contains two integers $$$n$$$ and $$$q$$$ $$$(1\le n, q\le 5\cdot 10^{5})$$$.
The next line contains $$$n-1$$$ integers $$$p_2,p_3,\dots,p_n$$$ $$$(1\le p_i\le n)$$$, where $$$p_i$$$ is the parent of the $$$i$$$-th vertex in the tree. Vertex $$$1$$$ is the root.
Then $$$q$$$ lines follow. Each line begins with an integer $$$k$$$ $$$(1 \le k \le n)$$$, followed by $$$k$$$ distinct integers $$$s_1, s_2, \dots, s_k$$$ $$$(1 \le s_i \le n;\ \forall i\neq j, s_i\neq s_j)$$$, representing the selected nodes.
It is guaranteed that the sum of $$$k$$$ over all queries does not exceed $$$5\cdot 10^{5}$$$.
For each query, output "Yes" if the plan is elegant or "No" if not.
You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", and "YES" will be recognized as a positive response).
5 21 2 2 12 1 23 5 4 3
Yes No
You are given two integers $$$n$$$ and $$$p$$$, where $$$p$$$ is a prime number.
Find a permutation $$$a_1,a_2,\dots,a_n$$$ of the integers from $$$1$$$ to $$$n$$$ (each number appears exactly once), such that for every index $$$i$$$ with $$$2\le i\le n-1$$$, the following holds:
If such a permutation exists, output any valid permutation; otherwise, output $$$-1$$$.
A single line with two integers $$$n$$$ and $$$p$$$ ($$$2\le p\le n\le 2\cdot10^{5}$$$, $$$p$$$ is prime), separated by a space.
If a valid permutation exists, output $$$n$$$ integers: the permutation $$$a_1\;a_2\;\dots\;a_n$$$ (space-separated) on one line. Otherwise, output a single line containing $$$-1$$$.
3 2
2 1 3
6 2
-1
In this problem, $$$a \bmod p$$$ refers to taking the remainder of $$$a$$$ after division by $$$p$$$. For example:
Ris, the administrator of the Prism Area, has regained her memories and power. However, to reconstruct the world that was once on the verge of collapse, she needs to stabilize the "Kaleidoscope" — the core system that connects all seven worlds.
The Kaleidoscope is built from $$$n$$$ memory fragments. Each fragment $$$i$$$ possesses a unique energy signature represented by a non-negative integer $$$a_i$$$.
In the world of 7sRef, connections between fragments are formed based on "resonance." Two distinct fragments $$$i$$$ and $$$j$$$ are connected by a bond (an undirected edge) if and only if their energy signatures share at least one common "color". Mathematically, this means the bitwise AND of their values is non-zero: $$$$$$a_i \ \& \ a_j \neq 0$$$$$$
To activate the Kaleidoscope, Acid (Ris's partner) needs to find a closed loop of resonance within these fragments. Specifically, she needs to find the smallest cycle in this graph formed by the fragments and their connections.
Mofusigil decided to help Acid calculate the length of the shortest cycle (the number of edges in the cycle). If no cycle exists in the graph, output $$$-1$$$. However, Mofusigil's ability is limited and he could not calculate the result, so this important task is handed over to you.
The first line contains a single integer $$$n$$$ $$$(1 \le n \le 10^6)$$$, denoting the number of memory fragments.
The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$ $$$(0 \le a_i \le 10^{18})$$$, representing the energy signature of each fragment.
Output a single integer representing the length of the smallest cycle of the graph. If the graph contains no cycles, output $$$-1$$$.
3 3 5 6
3
Explanation for Sample 1:
The binary representations are:
$$$a_1 = 3_{(10)} = 011_{(2)}$$$
$$$a_2 = 5_{(10)} = 101_{(2)}$$$
$$$a_3 = 6_{(10)} = 110_{(2)}$$$
Connections:
$$$a_1 \ \& \ a_2 = 1 \neq 0$$$ (Edge $$$1-2$$$)
$$$a_2 \ \& \ a_3 = 4 \neq 0$$$ (Edge $$$2-3$$$)
$$$a_3 \ \& \ a_1 = 2 \neq 0$$$ (Edge $$$3-1$$$)
This forms a triangle (cycle of length 3).