Wor is a plumbing repairman.
Wor often helps neighbors in the community to repair broken plumbing. With a high talent for fixing, Wor is confident in his capability. However, now he is facing trouble, as a serious accident happened in the central water supply station that interrupted the whole city's water supply. Because of the complexity of the pipe network, he failed and felt sad. Now, he is seeking your help. Can you help him?
There are four types of plumbing in the central water supply station. Now we define the operation , which means a degree clockwise rotation of the water pipe.
type plumbing: -type water pipe and -type water pipe.
When we do the operation , we can get the -type pipe. Meanwhile, operation can get the -type pipe.


type plumbing: -type water pipe.
No matter how we use the operation , we can only get the -type pipe.

type plumbing:-type water pipe, -type water pipe, -type water pipe, and -type water pipe.
Operation would get the -type water pipe, would get the -type water pipe, would get the -type water pipe, and would get the -type pipe.




type plumbing: -type water pipe and -type water pipe.
Doing operation , we can get the -type water pipe, and we can get the -type pipe when we do the operation .


type plumbing: starting tube and ending tube, which is the entrance of the water flow and the exit of the water flow. There is only one starting tube and one ending tube.
Starting tube and ending tube have only one direction, , respectively representing the water flow out and in direction as up, down, left, or right. Attention please, starting tube and ending tube cannot rotate.
The order of following pictures stick to .


In fact, distinguishing between starting tube and ending tube is meaningless, so there is no distinction between starting tube and ending tube in the data.
Now, Wor would give you the initial status of the plumbing matrix, which is an matrix . Your mission is to find a final fixed matrix that is available by only using the operation in the initial plumbing matrix, and the water flow can pass through from the starting tube to the ending tube.
The first line contains two integers representing the size of the matrix as .
Then following lines and columns character matrix represent the initial status of the plumbing matrix .
The character represents the starting pipe or ending pipe direction. There must be two characters that are , no matter which is the starting pipe or ending pipe.
We ensure that the total number of -type pipes and -type pipes is less than or equal to .
An lines and columns character matrix represents the fixed status of the plumbing matrix that can be obtained by only using the operation in the initial matrix, and the water flow can pass through from the starting tube to the ending tube.
5 7
pppiipi
iiupixl
ipi+pii
pipppii
iipiipi
pqp--qi
iiup-xl
ib-+qii
b-qbdii
iib--di
2 5
riiil
iiiii
r---l
iiiii
3 3
oii
pip
iiu
oii
b-q
iiu
5 2
ox
+x
+x
+x
ux
ox
+x
+x
+x
ux
There may be many legal solutions, you only need to output one of them.
The data ensures that there is a legitimate solution, so you don't need to consider situations where there is no solution.
Example 1 explanation:
Snow has a string, and now he wants to use magic to shorten this string. Each time he casts a spell, he can eliminate three adjacent identical characters. But Snow finds it too time-consuming to cast the spell repeatedly, so he hopes you can help him calculate what the shortest form of the string will be after using magic any number of times.
Input a string, ensuring that it only contains lowercase letters, and the length of the string does not exceed .
Output a string representing the shortest string after casting magic.If the entire string is eliminated, the output should be "NAN".
bcabaaabbccabcc
bcaccabcc
cccpppccc
NAN
There is a rich kingdom on the mainland, and the road structure of the whole mainland can be regarded as a rooted tree. The capital is located at node 1 as the root of the whole tree.
In the past, people lived in peace and contentment. However, with the evil monsters flooding the mainland, people are suffering. The king decided to send the army to hunt the monsters. Before that, the king sent out reconnaissance teams, to watch for monster traces, if found at a certain node, it meant that there was at least one monster in the subtree. After a period of searching, the reconnaissance team found some nodes with traces of the monster, we call the nodes with traces of the monster as markers.
However, the reconnaissance is still not over, and the reconnaissance team may make new discoveries, which may be the discovery of new markers, or it may be determined that the previous markers are false positives, that is, cancel the marking of a certain node. And you, as the brain of the Kingdom, your task is to update how many monsters at least when new discoveries emerge.
The first line of input contains integers , indicating the number of nodes of the tree, the number of markers at the beginning, and the number of new discoveries.
The next line contains integers indicating the father of nodes.
The next line contains integers indicating the nodes with markers at the beginning. Guarantee that if , .
Each of the following lines contains integers . , which mean marked node ; , which mean cancel the marking of node . Guarantee that if , node is not marked before it, if , node is marked before it.
It guaranteed that .
Output lines, each line contains an integer. The first line indicates the minimum number of monsters possible before new discoveries emerge. The line indicates the minimum number of monsters possible after the new discoveries emerge.
8 3 4
1 1 2 2 3 3 4
2 6 8
1 3
1 7
2 3
2 6
2
2
3
3
2
The problem requires a large amount of input, please use the appropriate input method.
A and B are playing card games, the game starts with A and B each having some health, called and . Each player starts with cards, called and , and each player plays one card he has not played before per turn. There are two types of cards: attack cards and defense cards. Each attack card has an atk, if one of them plays an attack card and the other doesn't play a defense card, the other's health will reduce the atk of the attack card; If the other player plays a defense card, his health doesn't decrease. If a player plays a defense card, it has no effect other than to defend against the other's attack.
When a turn ends, if any player's health is less than or equal to zero, the game ends. If only one player's health is less than or equal to zero, the other wins. If both player's health is less than or equal to zero, it is a draw. If after all turns, no player has zero or less health, it is also a draw.
We call the sequence of A's cards played , and call the sequence of B's cards played , they are permutations from to . If the game hasn't been over before the turn, in the turn, A will play , and B will play .
Now you know A and B's cards. The question is whether there exists a pair of sequences of A and B's cards played that will make A win.
The first line contains an integer , indicating the number of test sets.
And then for each set of data. The first line contains three integers , , and , which indicate the number of cards per player and the initial health of two players. The second line contains integers , indicating A's cards, where indicates the defense card, and in other cases indicates the attack card and is the atk. The third line contains integers , indicating B's cards, in the same way.
It guaranteed that . For all test sets, the sum of is less than . . or equal to .
For each set of data, output a string, "yes" or "no", indicating whether there exists a pair of sequences of A and B's cards played that will make A win.
2
3 4 9
-1 7 3
5 2 1
5 9 11
3 -1 6 4 1
-1 -1 10 5 2
yes
no
Joey and Grey are playing a poker game.
Initially, Joey has poker cards and Grey has none. They would repeat the following steps to help Grey get as many cards as possible:
They are curious about one question --- if they operate optimally, how many cards could Grey get expected.
The first line of input contains four integers, they are , , , . We guarantee that and .
The second line of input is a string of length to represent the cards Joey owns originally, in which represents hearts, for spades, for diamonds and for clubs. We promise .
Just one integer represented the maximum expected number of cards modulo Grey can get. If this number is infinity, please output .
25 25 25 25
S
6
0 100 0 0
HSDC
INF
For the first example, the maximum expected number of cards Grey can get is . I can not provide all the cases here (which are infinity), but one possible case that Grey gets cards is that:
The first round, they picked one heart. Joey used his spade to exchange it and got a new club from the pile. Now Joey has two cards, one heart and one club. Grey got the spade card and started a new round.
The second round to the fifth round, they kept picking club from the pile and Joey did nothing. Grey got clubs.
The sixth round, they picked one diamond. Joey used his club to exchange it. Now Joey owns two cards, one heart and one diamond.
The seventh round, they picked one heart, it can be proved that they will stop here.
For the second example, obviously, Grey can get infinity number of cards, so the output is .
Paulliant loves photography. Today, he arrived in Harbin to take photos at various landmarks. For simplicity, Harbin can be represented as a undirected graph with nodes and edges, where each node represents a different landmark and each edge represents a road connecting these landmarks. Paulliant will choose a landmark to start his photography tour. Once he finishes taking photos at one landmark, he will travel along the edges to his next preferred landmark to continue his tour. Due to time constraints, he can visit at most distinct landmarks. Naturally, Paulliant does not want to repeat his visits, so these landmarks must be different. Before the tour, Paulliant has assigned a "happiness value" to each landmark, where the -th landmark has a happiness value . Paulliant wants the total happiness value from all the landmarks he visits to be maximized. Determine the maximum total happiness value Paulliant can achieve.
The first line contains two space-separated integers and , representing the number of landmarks and the number of roads in Harbin, respectively.
The second line contains space-separated integers , representing the happiness value of each landmark.
The following lines, each contains two space-separated integers and , indicating a road connecting the landmarks and . It is guaranteed that each road is unique.
Output a single integer representing the maximum total happiness value Paulliant can achieve from his photography tour.
4 5
5 6 3 4
1 2
1 3
2 3
2 4
3 4
18
7 10
6 7 8 6 1 7 10
1 2
1 5
2 3
2 5
3 5
3 6
3 7
4 5
4 6
5 6
34
Khada_Jhin is learning theoretical computer science.
Today, Jwong asked Khada_Jhin a question: for a string of length n consisting of and , how many fundamentally different arrangements of grey code are there?
A permutation of length n is an array consisting of n distinct integers from to in any order. A circular permutation is treated as a special permutation whose is always 0.
An arrangement of grey code is a circular permutation , such that all and are just one digit different in binary. In particular, and also need to satisfy just one digit difference in binary.
But this question is so hard for Khada_Jhin, so Jwong simplifies the problem, now Khada_Jhin must compute for a string of length consisting of , how many fundamentally different arrangements of grey-like code?
We also define an arrangement of grey-like code as a circular permutation , such that for all and , the last bit of must be the same as the first bit of in binary.In particular, and also need to satisfy the last bit of must be the same as the first bit of in binary.
For example, when , the answer is equal to 1. Just one legal answer is in binary.
Now it becomes an easy question for Khada_Jhin, but he wants to check if he made a mistake, so please tell him what the answer is modulo 998244353.
Just input one integer n, satisfying .
Just output one integer, which is the answer modulo 998244353.
2
1
114514
518172623
Snow wants to buy some goods to celebrate the Spring Festival.
He found that there were kinds of goods in the store, and each kind of goods had several colors on it. (It means two goods have the same colors if they are of the same kind.) Each color will be represented as a number between and .
Snow wants to buy goods so that the set of colors on those goods is exactly what he wants. Snow wants to know how many different plans he can buy. (Note that the good in the same kind is also different. For example, the first kind has 3 goods, and you want to choose 2 from the first kind, you have 3 plans, not 1.)
More formally, Snow will make queries, each providing you with a set of colors. For each query, you must determine the number of plans that involve purchasing exactly goods, with the color set on those goods equal to the provided set.
Because the answer may be so big that you need to modulo it by .
The first line contains three integers , , and .
Then lines follow. The i-th of them contains two integers and , meaning the number of colors in this kind of goods, represents the number of goods in kind , followed by distinct integers , where denotes the j-th color on i-th kind .
Then one line contains an integer , denotes the number of queries.
Then lines follow. The i-th of them contains an integer meaning the size of color set, and followed by distinct integers , which denotes colors in this set.
For all queries, print an integer which means the number of plans modulo .
4 8 2
3 1 1 2 3
2 1 2 4
1 1 4
3 1 1 2 4
2 1 1 3
4 1 1 2 3 4
1 1 3
2 1 1 3
2
4 1 2 3 4
3 2 3 4
15
1
4 1 1
1 3 1
1
1 1
3
For the first example, every kind only has one good.
For the first query, you can choose plans as follows:,,,,,,,,,,,,,,. (Each plan is expressed in the form (i, j), indicating the selection of kind i and kind j).
For the second query, your only plan is .
For the second example, you want to choose 1 good in the first kind, and the first kind has 3 goods, so you have plans.
As you can see, this is an easy problem.
You are given an integer , and you only need to output how many "1" in its binary representation.
For example, the integer 5 in binary representation is "101", which has 2 "1", so you should output "2".
Only one integer , and .
Only one integer, the number of "1" in the binary representation of .
5
2
1024
1
In a prosperous country, Kingsnow decided to engage in trade.
The country consists of cities. Each city is represented by an integer pair , where and , indicating its position in a grid. In city , the item's price is denoted by , and the travel expense to reach this city is denoted by .
Before embarking on his journey, Kingsnow needs you to plan a route for him. The route must meet the following criteria:
Once on the route, Kingsnow will purchase an item in the first city of the route . He will then arbitrarily select another city on the route to sell this item. Additionally, he will pay travel expenses for every city visited between the city where he starts the journey and the city where he sells the item.
That is to say, for any given route , Kingsnow will arbitrarily select an integer from the interval . The profit he makes from selling the item in city will be calculated as:
Kingsnow seeks a route such that no matter which city along the route he chooses to sell his item, he will always make a nonnegative profit.
The first line contains two integers and --- the number of lines and rows. Ensure that n and m do not equal 1 at the same time.
For the next lines, each line contains integers in range denoting the price of the item in each city. The -th integer in the -th line is .
For the next lines, each line contains integers in range denoting the travel expense in each city. The -th integer in the -th line is .
If there is at least one route that meets the requirements output "YES", otherwise output "NO".
4 4
1 3 4 2
1 0 5 3
5 2 6 1
3 2 7 3
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
YES
4 4
1 5 5 4
5 5 5 4
5 5 5 4
4 4 4 4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
NO
The 24 puzzle is a classical arithmetical puzzle in which the objective is to find a way to manipulate four integers so that the end result is 24.
Nerifish is fond of this kind of puzzle, so he designed a similar puzzle for himself.
Nerifish has four Poker cards, the numbers on each card are . He can arrange the cards in any order and connect them with plus, minus, and multiplication signs. Please note that brackets are NOT allowed to be used, the final expression should contain 4 integers and 3 operators.
Nerifish wants to know how many results he can get.
The first line of the input contains four integers and , indicating the numbers on each card.
Output one line containing one integer indicating the number of results Nerifish can get.
3 1 2 2
21
When the final expression is
There are a total of 21 different results here.
"" can get -13, but this expression is illegal(it does not meet the requirements of "3 operators").
"" can get 8, but there is already an expression "32+21" which can also get 8.
"" can get 18, but this expression is illegal(it does not meet the requirements of "no brackets").
The better a person plays badminton, the higher the requirements for badminton rackets will be. Joey is looking at a badminton racket now and evaluating who it is suitable for.
The strings on a badminton racket can be described as a graph with nodes and directed edges. Each node has an elasticity value and toughness value .
A badminton player's ability can be represented by an integer . When he uses the badminton racket, all nodes with will be considered activated, and other nodes will be considered inactive. Let represent node is activated, and represent node is inactive.
When a badminton racket is used, each node has a pressure value . The definition of is as follows:
is a path in the directed graph, are points in sequence on the path(the same point or edge can be passed through repeatedly). Therefore, this definition means that is equal to the maximum sum of of all nodes among all paths ending with .
If each node satisfies , then this badminton racket is suitable for this badminton player.
Joey wants to know what the maximum a badminton player can have. (note that this badminton racket should be suitable for this player)
The first line contains two integers and , the number of nodes and edges.
The second line contains integers, and the -th integer represents the elasticity value of node .
The third line contains integers, and the -th integer represents the toughness value of node .
For the next lines, each line contains integers , which represents a directed edge from to .
Output the largest the badminton player can have, if can be larger than then output .
5 10
10 1 3 5 6
9 9 5 10 14
1 5
4 2
4 2
2 2
5 1
5 1
3 4
4 1
1 1
3 4
5