You are given a non-empty sequence of letters "a", "b", and "c". Find the number of ways to swap two distinct letters (distinct by value, you are not allowed to pick two equal letters at distinct positions) in this sequence so that the resulting sequence is correct.
A correct sequence of letters is defined as follows:
The only line contains a non-empty sequence of characters "a", "b", and "c". The length of the sequence does not exceed 100 000 characters.
Print one number — the number of ways to swap two distinct letters so that the resulting sequence is correct.
abba
2
abcabc
6
aaba
0
In the first sample we can obtain correct sequences "aabb" and «bbaa» by swapping letters. Note that the initial sequence may be correct, but it is not allowed to leave it unchanged.
In the second sample we can obtain correct sequences "abbacc", "abccba", "accabb", "aacbbc", "bbcaac", and "cbaabc".
In the third sample the number of "a" letters is odd, hence making a correct sequence is impossible.
You are given an array a of length n, and an array b of length m. All elements of both arrays are digits from 0 to 9. Using these arrays, construct a table c of size n × m, where the element in the i-th row and the j-th column is determined by ci, j = ai·109 + bj.
Consider all paths from the cell c1, 1 to the cell cn, m that consist exclusively of moving down and to the right (that is, from the cell ci, j you can only move to cells ci + 1, j and ci, j + 1, provided that these cells are inside the table boundaries). Out of all these paths, find one with the largest possible sum of numbers in the visited cells, and print this sum.
The first line contains two integers n and m — sizes of arrays a and b respectively (1 ≤ n, m ≤ 100 000).
The second line contains n integers a1, ..., an (0 ≤ ai ≤ 9).
The third line contains m integers b1, ..., bm (0 ≤ bj ≤ 9).
Print one integer — the largest sum in visited cells among all paths satisfying rules above in the table c.
3 4
1 1 1
1 1 1 1
6000000006
5 3
1 2 3 4 5
6 7 8
25000000045
7 4
0 7 1 7 6 7 6
4 1 9 7
55000000068
In the second sample, the optimal path first goes down from cell (1, 1) to cell (5, 1), then goes to the right till the end at the cell (5, 3).
In the third sample the optimum path is: 
.
Roma plays a new version of the "World of Darkraft" game as a wizard character. Roma values his time and tries to complete in-game tasks as efficient as possible.
Roma is now fighting n monsters. The i-th of these monsters has hi health points. A monster dies when his health points become zero of negative (at that moment Roma's character gains experience).
Roma's character has a pool of m mana points. The character possesses two spell abilities: "Lightning strike" and "Ring of ice".
The character can not use a spell if the number of mana points left is strictly less than the cost of this spell. The battle finishes as soon as all monsters are dead, or if it is not possible to cast any of two spells (then the character needs to retreat and rest).
In this battle Roma wants to gain as much experience as possible. Help Roma determine the maximum possible number of monsters he can kill.
The first line contains six integers n, m, ds, cs, da and ca (1 ≤ n, m, ds, cs, da, ca ≤ 100) — the number of monsters, the amount of characters' mana, and also damage and mana cost of "Lightning strike" and "Ring of ice" spells respectively.
The second line contains n integers h1, h2, ..., hn — monsters' health points (1 ≤ hi ≤ 100).
Print one number — the maximum number of monsters that can be defeated.
3 5 1 1 3 3
4 1 5
2
5 70 10 10 1 20
11 11 11 11 11
5
In the first sample, the character can cast "Ring of ice" once (as a result, the first monster will have one health point, the third monster will have two health points, and the second monster will die). After that, the character can apply "Lightning strike" twice to kill another monster.
In the second sample, the only way to defeat all monsters is to cast "Ring of ice" once, and "Lightning strike" once for each monster.
n athletes are going to take part in the Berlandian multisport championship. The competition will consist of k stages, and in each stage an athlete can score any real number of points in range from 0 to 1.
Little boy Oleg loves sports analytics, and he is going to highlight the championship in his sports blog. Oleg doesn't know anything about the competitors, or how the competition is arranged. Though, Oleg knows probability theory very well, so he is going to assume that each athlete scores random number of points equidistributed on the segment [0, 1] in each stage. Oleg also assumes that scores of all athletes on all stages are mutually independent.
The most important part of analytics is to comprise a final rating list, that is, order all athletes from the best to the worst. After studying hundreds of different rating systems, Oleg decided to upload all possible fair rating lists. Oleg thinks that in a fair rating list any athlete (except for the last) scored strictly greater number of points than the subsequent athlete in at least one stage of the competition.
Note that in a fair rating list one athlete can still be higher than another athlete even if he lost him in all stages. Consider a situation where in a two-stage competition the scores of the athlete 1 are (0.2, 0.2), the scores of the athlete 2 are (0.8, 0.8), and the scores of the athlete 3 are (0, 1). Then, the rating table (1, 3, 2) is fair, since the athlete 1 beat the athlete 3 in the first stage, and the athlete 3 beat the athlete 2 in the second stage, however, the athlete 1 lost to the athlete 2 in both.
The championship has not started yet, but Oleg wants to reserve enough space for the rating tables on his hosting. To do this, he asked you to find the average number of ways to comprise a fair rating list at the end of the competition. Oleg also wants to train his number theory skills, so he wants the answer modulo P = 998 244 353, that is, if the answer is an irreducible fraction A / B, you have to output such an integer x that 0 ≤ x < P and A ≡ B·x ± od P (it is guaranteed that such x exists and is unique).
The average value of an integer-valued random variable is defined as
, where the sum is over all integers, and px is the probability of the variable begin equal to x.
The only line contains two integers n and k — the number of athletes and stages in the competition respectively (1 ≤ n, k ≤ 1000).
Print one number — the average number of fair rating lists modulo 998 244 353.
10 1
1
1 10
1
2 2
499122178
5 3
778699985
In the first sample, there is only one fair rating list for every outcome, that is, sorted by points in the only stage in decreasing order (except for the case when several athletes share the same score, but the probability of this event is zero).
In the second sample, there is only one fair list that consists of one athlete.
In the third sample the answer is
, and in the fourth sample the answer is
.
Vasily likes bonsasi — the art of growing decorative trees. He has two beatiful trees, but his aesthetical sense tells him that these trees should be made as similar as possible.
Consider the task formally. Let us represent each tree with a connected graph without cycles with a specified root vertex. Vertices of both trees are numbered starting from one, and the root of each tree has number 1 among the vertices of this tree. Let us direct all edges away from the root of the tree. All edges starting at the same vertex are ordered (from left to right when looking at the tree from the front) by increasing of the index of vertices where the edges are directed to.
Vasily can perform two kinds of operation with either of the trees:
Vasily thinks that the trees have become equal if it is possible to put vertices, as well as edges of both trees in one-to-one correspondence so that:
In particular, it follows from the rules that the roots of the trees must be corresponding.
Of course, Vasily doesn't want to ruin his trees too much. What is the smallest number of operations needed to make the trees equal?
The first line contains an integer n — the number of vertices of the first tree (1 ≤ n ≤ 5000).
The second line contains integers p2, ..., pn (1 ≤ pi < i). Here, the number pi is the number of the parent vertex of a non-root vertex i — the start vertex of the unique edge entering i.
The third line contains an integer m — the number of vertices of the second tree (1 ≤ m ≤ 5000).
The fourth line describes the parents of vertices 2, ..., m of the second tree in the same format as described above.
Print one number — the smallest number of operations needed to make the trees equal.
3
1 1
3
1 2
2
7
1 1 2 2 3 3
7
1 1 2 3 2 3
0
1
5
1 2 1 4
4
In the first sample it is possible to reach the goal in two operations: for example, we can cut the leaf vertex 3 of the first tree, and tie the edges (1, 2) and (1, 3) in the second tree.
In the second sample the trees are already equal regardless of the fact that the leaves are numbered differently.
In the third sample we have to successively cut all vertices of the second tree except for the root. Note that if a tree has only one vertex, the second line of its description is empty.
Mathematicians Alfred and Georg play a game. They take a white rectangular sheet of paper of size n × m divided into unit square cells. First, Alfred paints some of the cells black, and then Georg has to paint all the cells white again. To do that, Georg can perform the following operation an arbitrary number of times:
While Georg is working, Alfred mentally tries to count the smallest number of operations needed to finish the game starting from the initial configuration. He asked you to write a program that would answer this question.
The first line contains three integers n, m and k (1 ≤ n, m ≤ 500 000, 0 ≤ k ≤ 500 000) — the dimensions of the sheet, and the number of black cells respectively.
The subsequent k lines describe the black cells. The i-th of these lines contains two integers xi, yi — coordinates of the bottom left corner of the i-the black cell in the coordinate system described above (0 ≤ xi < n, 0 ≤ yi < m). It is guaranteed that all cells in the input are distinct.
Print one number — the smallest number of operations needed to finish the game. If it is impossible to paint the whole sheet white, print - 1.
2 2 3
0 0
1 0
1 1
1
2 2 3
0 0
0 1
1 1
2
2 2 3
1 0
0 1
1 1
3
In first sample one path is enough: (0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2).
In the second sample two paths are enough: (0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) and (0, 0) → (0, 2) → (2, 2).