question large_stringlengths 265 13.2k |
|---|
Solve the programming task below in a Python markdown code block.
The door of Snuke's laboratory is locked with a security code.
The security code is a 4-digit number. We say the security code is hard to enter when it contains two consecutive digits that are the same.
You are given the current security code S. If S is hard to enter, print `Bad`; otherwise, print `Good`.
Constraints
* S is a 4-character string consisting of digits.
Input
Input is given from Standard Input in the following format:
S
Output
If S is hard to enter, print `Bad`; otherwise, print `Good`.
Examples
Input
3776
Output
Bad
Input
8080
Output
Good
Input
1333
Output
Bad
Input
0024
Output
Bad
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
We have N cards. A number a_i is written on the i-th card.
Alice and Bob will play a game using these cards. In this game, Alice and Bob alternately take one card. Alice goes first.
The game ends when all the cards are taken by the two players, and the score of each player is the sum of the numbers written on the cards he/she has taken. When both players take the optimal strategy to maximize their scores, find Alice's score minus Bob's score.
Constraints
* N is an integer between 1 and 100 (inclusive).
* a_i \ (1 \leq i \leq N) is an integer between 1 and 100 (inclusive).
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 a_3 ... a_N
Output
Print Alice's score minus Bob's score when both players take the optimal strategy to maximize their scores.
Examples
Input
2
3 1
Output
2
Input
3
2 7 4
Output
5
Input
4
20 18 2 18
Output
18
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are N towns in Takahashi Kingdom. They are conveniently numbered 1 through N.
Takahashi the king is planning to go on a tour of inspection for M days. He will determine a sequence of towns c, and visit town c_i on the i-th day. That is, on the i-th day, he will travel from his current location to town c_i. If he is already at town c_i, he will stay at that town. His location just before the beginning of the tour is town 1, the capital. The tour ends at town c_M, without getting back to the capital.
The problem is that there is no paved road in this kingdom. He decided to resolve this issue by paving the road himself while traveling. When he travels from town a to town b, there will be a newly paved one-way road from town a to town b.
Since he cares for his people, he wants the following condition to be satisfied after his tour is over: "it is possible to travel from any town to any other town by traversing roads paved by him". How many sequences of towns c satisfy this condition?
Constraints
* 2β¦Nβ¦300
* 1β¦Mβ¦300
Input
The input is given from Standard Input in the following format:
N M
Output
Print the number of sequences of towns satisfying the condition, modulo 1000000007 (=10^9+7).
Examples
Input
3 3
Output
2
Input
150 300
Output
734286322
Input
300 150
Output
0
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
When a boy was cleaning up after his grand father passing, he found an old paper:
<image>
In addition, other side of the paper says that "go ahead a number of steps equivalent to the first integer, and turn clockwise by degrees equivalent to the second integer".
His grand mother says that Sanbonmatsu was standing at the center of town. However, now buildings are crammed side by side and people can not walk along exactly what the paper says in. Your task is to write a program which hunts for the treature on the paper.
For simplicity, 1 step is equivalent to 1 meter. Input consists of several pairs of two integers d (the first integer) and t (the second integer) separated by a comma. Input ends with "0, 0". Your program should print the coordinate (x, y) of the end point. There is the treature where x meters to the east and y meters to the north from the center of town.
You can assume that d β€ 100 and -180 β€ t β€ 180.
Input
A sequence of pairs of integers d and t which end with "0,0".
Output
Print the integer portion of x and y in a line respectively.
Example
Input
56,65
97,54
64,-4
55,76
42,-27
43,80
87,-86
55,-6
89,34
95,5
0,0
Output
171
-302
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
In Group C of the 3rd year, we decided to use the "class flag" used at the sports festival on November 10, 2007 at future class reunions. So, in order to decide which students to keep the "class flag", I decided to play the following game using a large amount of candy that the teacher gave me the other day.
* Each student will take one candy in the order of student number.
* If candy remains after one round, continue to take candy in order from the person with the first student number.
* The person who picks up the last candy will be the student who keeps the "class flag".
There are 39 students in the 3rd grade C class. Their student numbers are 3C01 to 3C39. For example, if you have 50 candies and everyone in the class finishes taking the first candy, you will have 11 candies left. If you take it again in student number order, the last one will be taken by the 3C11 student. That is, the 3C11 student is the student who keeps the "class flag".
Create a program that takes the number of candies as input and outputs the student number of the student who stores the "class flag".
Input
Multiple test cases are given. Each test case is given in the following format. For each test case, an integer a (1 β€ a β€ 10000) representing the number of candies is given on one line. Process until the end of input (EOF).
The number of datasets does not exceed 20.
Output
For each test case, output the student number (half-width alphanumeric characters) of the student who stores the "class flag" on one line.
Example
Input
50
5576
5577
5578
Output
3C11
3C38
3C39
3C01
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
She is an apprentice wizard. She first learned the magic of manipulating time. And she decided to open a liquor store to earn a living. It has to do with the fact that all the inhabitants of the country where she lives love alcohol. Residents especially like sake that has been aged for many years, and its value increases proportionally with the number of years it has been aged. It's easy to imagine that 100-year-old liquor is more valuable than 50-year-old liquor. And it's also difficult to get. She used her magic to instantly make a long-aged liquor and sell it to earn money.
She priced the sake according to the number of years it was aged. For example, if you need 5 years of aging, you will have to pay 5 emers, and if you have 100 years of aging, you will have to pay 100 emers. For her, making either one uses the same magic, so there is no difference. A long-term aging order is better for her.
Since she is still immature, there are two restrictions on the magic that can be used now. She can't age sake for more than n years now. Also, there are m integers, and it is impossible for her to age sake for those and multiple years.
She is worried if she can live only by running a liquor store. She cannot predict how much income she will earn. So I decided to decide if I needed to do another job based on my expected daily income. Fortunately, she can grow vegetables, take care of pets, cook, sew, carpentry, and so on, so even if she has a small income at a liquor store, she doesn't seem to be in trouble.
It's your job to calculate the expected value of her daily income. The following three may be assumed when calculating the expected value.
Only one order can be accepted per day.
Residents do not order liquor for years that she cannot make.
The number of years of inhabitants' orders is evenly distributed.
Input
The input consists of multiple cases. Each case is given in the following format.
n m
p0 ... pm-1
Residents request liquor that has been aged for the number of years with an integer value between 1 and n.
However, no one asks for m integer pi and its multiples.
The end of the input is given with n = 0 and m = 0.
Each value meets the following conditions
2 β€ n β€ 2,000,000,000
1 β€ m β€ 20
Also, pi is guaranteed to be divisible by n.
The number of test cases does not exceed 200.
Output
Output the expected value of her daily income.
If there is no possible number of years for the resident's order, output 0 as the expected value of the answer.
The correct answer prepared by the judge and an error of 1e-5 or less are allowed as the correct answer.
Example
Input
12 3
2 3 6
12 4
1 2 3 6
0 0
Output
6.0000000000
0.0000000000
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Example
Input
3
3 0 1
Output
2
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Write a program which reads relations in a SNS (Social Network Service), and judges that given pairs of users are reachable each other through the network.
Constraints
* $2 \leq n \leq 100,000$
* $0 \leq m \leq 100,000$
* $1 \leq q \leq 10,000$
Input
In the first line, two integer $n$ and $m$ are given. $n$ is the number of users in the SNS and $m$ is the number of relations in the SNS. The users in the SNS are identified by IDs $0, 1, ..., n-1$.
In the following $m$ lines, the relations are given. Each relation is given by two integers $s$ and $t$ that represents $s$ and $t$ are friends (and reachable each other).
In the next line, the number of queries $q$ is given. In the following $q$ lines, $q$ queries are given respectively. Each query consists of two integers $s$ and $t$ separated by a space character.
Output
For each query, print "yes" if $t$ is reachable from $s$ through the social network, "no" otherwise.
Example
Input
10 9
0 1
0 2
3 4
5 7
5 6
6 7
6 8
7 8
8 9
3
0 1
5 9
1 3
Output
yes
yes
no
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III.
Constraints
* $2 \leq n \leq 100$
* $0 \leq $ the integer assigned to a face $ \leq 100$
Input
In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III.
Output
Print "Yes" if given dices are all different, otherwise "No" in a line.
Examples
Input
3
1 2 3 4 5 6
6 2 4 3 5 1
6 5 4 3 2 1
Output
No
Input
3
1 2 3 4 5 6
6 5 4 3 2 1
5 4 3 2 1 6
Output
Yes
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Polycarp has prepared n competitive programming problems. The topic of the i-th problem is a_i, and some problems' topics may coincide.
Polycarp has to host several thematic contests. All problems in each contest should have the same topic, and all contests should have pairwise distinct topics. He may not use all the problems. It is possible that there are no contests for some topics.
Polycarp wants to host competitions on consecutive days, one contest per day. Polycarp wants to host a set of contests in such a way that:
* number of problems in each contest is exactly twice as much as in the previous contest (one day ago), the first contest can contain arbitrary number of problems;
* the total number of problems in all the contests should be maximized.
Your task is to calculate the maximum number of problems in the set of thematic contests. Note, that you should not maximize the number of contests.
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the number of problems Polycarp has prepared.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9) where a_i is the topic of the i-th problem.
Output
Print one integer β the maximum number of problems in the set of thematic contests.
Examples
Input
18
2 1 2 10 2 10 10 2 2 1 10 10 10 10 1 1 10 10
Output
14
Input
10
6 6 6 3 6 1000000000 3 3 6 6
Output
9
Input
3
1337 1337 1337
Output
3
Note
In the first example the optimal sequence of contests is: 2 problems of the topic 1, 4 problems of the topic 2, 8 problems of the topic 10.
In the second example the optimal sequence of contests is: 3 problems of the topic 3, 6 problems of the topic 6.
In the third example you can take all the problems with the topic 1337 (the number of such problems is 3 so the answer is 3) and host a single contest.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Pavel has several sticks with lengths equal to powers of two.
He has a_0 sticks of length 2^0 = 1, a_1 sticks of length 2^1 = 2, ..., a_{n-1} sticks of length 2^{n-1}.
Pavel wants to make the maximum possible number of triangles using these sticks. The triangles should have strictly positive area, each stick can be used in at most one triangle.
It is forbidden to break sticks, and each triangle should consist of exactly three sticks.
Find the maximum possible number of triangles.
Input
The first line contains a single integer n (1 β€ n β€ 300 000) β the number of different lengths of sticks.
The second line contains n integers a_0, a_1, ..., a_{n-1} (1 β€ a_i β€ 10^9), where a_i is the number of sticks with the length equal to 2^i.
Output
Print a single integer β the maximum possible number of non-degenerate triangles that Pavel can make.
Examples
Input
5
1 2 2 2 2
Output
3
Input
3
1 1 1
Output
0
Input
3
3 3 3
Output
3
Note
In the first example, Pavel can, for example, make this set of triangles (the lengths of the sides of the triangles are listed): (2^0, 2^4, 2^4), (2^1, 2^3, 2^3), (2^1, 2^2, 2^2).
In the second example, Pavel cannot make a single triangle.
In the third example, Pavel can, for example, create this set of triangles (the lengths of the sides of the triangles are listed): (2^0, 2^0, 2^0), (2^1, 2^1, 2^1), (2^2, 2^2, 2^2).
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Bob has a string s consisting of lowercase English letters. He defines s' to be the string after removing all "a" characters from s (keeping all other characters in the same order). He then generates a new string t by concatenating s and s'. In other words, t=s+s' (look at notes for an example).
You are given a string t. Your task is to find some s that Bob could have used to generate t. It can be shown that if an answer exists, it will be unique.
Input
The first line of input contains a string t (1 β€ |t| β€ 10^5) consisting of lowercase English letters.
Output
Print a string s that could have generated t. It can be shown if an answer exists, it is unique. If no string exists, print ":(" (without double quotes, there is no space between the characters).
Examples
Input
aaaaa
Output
aaaaa
Input
aacaababc
Output
:(
Input
ababacacbbcc
Output
ababacac
Input
baba
Output
:(
Note
In the first example, we have s = "aaaaa", and s' = "".
In the second example, no such s can work that will generate the given t.
In the third example, we have s = "ababacac", and s' = "bbcc", and t = s + s' = "ababacacbbcc".
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The main characters have been omitted to be short.
You are given a directed unweighted graph without loops with n vertexes and a path in it (that path is not necessary simple) given by a sequence p_1, p_2, β¦, p_m of m vertexes; for each 1 β€ i < m there is an arc from p_i to p_{i+1}.
Define the sequence v_1, v_2, β¦, v_k of k vertexes as good, if v is a subsequence of p, v_1 = p_1, v_k = p_m, and p is one of the shortest paths passing through the vertexes v_1, β¦, v_k in that order.
A sequence a is a subsequence of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements. It is obvious that the sequence p is good but your task is to find the shortest good subsequence.
If there are multiple shortest good subsequences, output any of them.
Input
The first line contains a single integer n (2 β€ n β€ 100) β the number of vertexes in a graph.
The next n lines define the graph by an adjacency matrix: the j-th character in the i-st line is equal to 1 if there is an arc from vertex i to the vertex j else it is equal to 0. It is guaranteed that the graph doesn't contain loops.
The next line contains a single integer m (2 β€ m β€ 10^6) β the number of vertexes in the path.
The next line contains m integers p_1, p_2, β¦, p_m (1 β€ p_i β€ n) β the sequence of vertexes in the path. It is guaranteed that for any 1 β€ i < m there is an arc from p_i to p_{i+1}.
Output
In the first line output a single integer k (2 β€ k β€ m) β the length of the shortest good subsequence. In the second line output k integers v_1, β¦, v_k (1 β€ v_i β€ n) β the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct.
Examples
Input
4
0110
0010
0001
1000
4
1 2 3 4
Output
3
1 2 4
Input
4
0110
0010
1001
1000
20
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
Output
11
1 2 4 2 4 2 4 2 4 2 4
Input
3
011
101
110
7
1 2 3 1 3 2 1
Output
7
1 2 3 1 3 2 1
Input
4
0110
0001
0001
1000
3
1 2 4
Output
2
1 4
Note
Below you can see the graph from the first example:
<image>
The given path is passing through vertexes 1, 2, 3, 4. The sequence 1-2-4 is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes 1, 2 and 4 in that order is 1-2-3-4. Note that subsequences 1-4 and 1-3-4 aren't good because in both cases the shortest path passing through the vertexes of these sequences is 1-3-4.
In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes.
In the fourth example, the paths 1-2-4 and 1-3-4 are the shortest paths passing through the vertexes 1 and 4.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are playing a variation of game 2048. Initially you have a multiset s of n integers. Every integer in this multiset is a power of two.
You may perform any number (possibly, zero) operations with this multiset.
During each operation you choose two equal integers from s, remove them from s and insert the number equal to their sum into s.
For example, if s = \{1, 2, 1, 1, 4, 2, 2\} and you choose integers 2 and 2, then the multiset becomes \{1, 1, 1, 4, 4, 2\}.
You win if the number 2048 belongs to your multiset. For example, if s = \{1024, 512, 512, 4\} you can win as follows: choose 512 and 512, your multiset turns into \{1024, 1024, 4\}. Then choose 1024 and 1024, your multiset turns into \{2048, 4\} and you win.
You have to determine if you can win this game.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 100) β the number of queries.
The first line of each query contains one integer n (1 β€ n β€ 100) β the number of elements in multiset.
The second line of each query contains n integers s_1, s_2, ..., s_n (1 β€ s_i β€ 2^{29}) β the description of the multiset. It is guaranteed that all elements of the multiset are powers of two.
Output
For each query print YES if it is possible to obtain the number 2048 in your multiset, and NO otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
Example
Input
6
4
1024 512 64 512
1
2048
3
64 512 2
2
4096 4
7
2048 2 2048 2048 2048 2048 2048
2
2048 4096
Output
YES
YES
NO
NO
YES
YES
Note
In the first query you can win as follows: choose 512 and 512, and s turns into \{1024, 64, 1024\}. Then choose 1024 and 1024, and s turns into \{2048, 64\} and you win.
In the second query s contains 2048 initially.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Bob is playing with 6-sided dice. A net of such standard cube is shown below.
<image>
He has an unlimited supply of these dice and wants to build a tower by stacking multiple dice on top of each other, while choosing the orientation of each dice. Then he counts the number of visible pips on the faces of the dice.
For example, the number of visible pips on the tower below is 29 β the number visible on the top is 1, from the south 5 and 3, from the west 4 and 2, from the north 2 and 4 and from the east 3 and 5.
<image>
The one at the bottom and the two sixes by which the dice are touching are not visible, so they are not counted towards total.
Bob also has t favourite integers x_i, and for every such integer his goal is to build such a tower that the number of visible pips is exactly x_i. For each of Bob's favourite integers determine whether it is possible to build a tower that has exactly that many visible pips.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of favourite integers of Bob.
The second line contains t space-separated integers x_i (1 β€ x_i β€ 10^{18}) β Bob's favourite integers.
Output
For each of Bob's favourite integers, output "YES" if it is possible to build the tower, or "NO" otherwise (quotes for clarity).
Example
Input
4
29 34 19 38
Output
YES
YES
YES
NO
Note
The first example is mentioned in the problem statement.
In the second example, one can build the tower by flipping the top dice from the previous tower.
In the third example, one can use a single die that has 5 on top.
The fourth example is impossible.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Evlampiy was gifted a rooted tree. The vertices of the tree are numbered from 1 to n. Each of its vertices also has an integer a_i written on it. For each vertex i, Evlampiy calculated c_i β the number of vertices j in the subtree of vertex i, such that a_j < a_i.
<image>Illustration for the second example, the first integer is a_i and the integer in parentheses is c_i
After the new year, Evlampiy could not remember what his gift was! He remembers the tree and the values of c_i, but he completely forgot which integers a_i were written on the vertices.
Help him to restore initial integers!
Input
The first line contains an integer n (1 β€ n β€ 2000) β the number of vertices in the tree.
The next n lines contain descriptions of vertices: the i-th line contains two integers p_i and c_i (0 β€ p_i β€ n; 0 β€ c_i β€ n-1), where p_i is the parent of vertex i or 0 if vertex i is root, and c_i is the number of vertices j in the subtree of vertex i, such that a_j < a_i.
It is guaranteed that the values of p_i describe a rooted tree with n vertices.
Output
If a solution exists, in the first line print "YES", and in the second line output n integers a_i (1 β€ a_i β€ {10}^{9}). If there are several solutions, output any of them. One can prove that if there is a solution, then there is also a solution in which all a_i are between 1 and 10^9.
If there are no solutions, print "NO".
Examples
Input
3
2 0
0 2
2 0
Output
YES
1 2 1
Input
5
0 1
1 3
2 1
3 0
2 0
Output
YES
2 3 2 1 2
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Bessie is out grazing on the farm, which consists of n fields connected by m bidirectional roads. She is currently at field 1, and will return to her home at field n at the end of the day.
The Cowfederation of Barns has ordered Farmer John to install one extra bidirectional road. The farm has k special fields and he has decided to install the road between two different special fields. He may add the road between two special fields that already had a road directly connecting them.
After the road is added, Bessie will return home on the shortest path from field 1 to field n. Since Bessie needs more exercise, Farmer John must maximize the length of this shortest path. Help him!
Input
The first line contains integers n, m, and k (2 β€ n β€ 2 β
10^5, n-1 β€ m β€ 2 β
10^5, 2 β€ k β€ n) β the number of fields on the farm, the number of roads, and the number of special fields.
The second line contains k integers a_1, a_2, β¦, a_k (1 β€ a_i β€ n) β the special fields. All a_i are distinct.
The i-th of the following m lines contains integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i), representing a bidirectional road between fields x_i and y_i.
It is guaranteed that one can reach any field from every other field. It is also guaranteed that for any pair of fields there is at most one road connecting them.
Output
Output one integer, the maximum possible length of the shortest path from field 1 to n after Farmer John installs one road optimally.
Examples
Input
5 5 3
1 3 5
1 2
2 3
3 4
3 5
2 4
Output
3
Input
5 4 2
2 4
1 2
2 3
3 4
4 5
Output
3
Note
The graph for the first example is shown below. The special fields are denoted by red. It is optimal for Farmer John to add a road between fields 3 and 5, and the resulting shortest path from 1 to 5 is length 3.
<image>
The graph for the second example is shown below. Farmer John must add a road between fields 2 and 4, and the resulting shortest path from 1 to 5 is length 3.
<image>
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Dreamoon likes sequences very much. So he created a problem about the sequence that you can't find in OEIS:
You are given two integers d, m, find the number of arrays a, satisfying the following constraints:
* The length of a is n, n β₯ 1
* 1 β€ a_1 < a_2 < ... < a_n β€ d
* Define an array b of length n as follows: b_1 = a_1, β i > 1, b_i = b_{i - 1} β a_i, where β is the bitwise exclusive-or (xor). After constructing an array b, the constraint b_1 < b_2 < ... < b_{n - 1} < b_n should hold.
Since the number of possible arrays may be too large, you need to find the answer modulo m.
Input
The first line contains an integer t (1 β€ t β€ 100) denoting the number of test cases in the input.
Each of the next t lines contains two integers d, m (1 β€ d, m β€ 10^9).
Note that m is not necessary the prime!
Output
For each test case, print the number of arrays a, satisfying all given constrains, modulo m.
Example
Input
10
1 1000000000
2 999999999
3 99999998
4 9999997
5 999996
6 99995
7 9994
8 993
9 92
10 1
Output
1
3
5
11
17
23
29
59
89
0
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Slime has a sequence of positive integers a_1, a_2, β¦, a_n.
In one operation Orac can choose an arbitrary subsegment [l β¦ r] of this sequence and replace all values a_l, a_{l + 1}, β¦, a_r to the value of median of \\{a_l, a_{l + 1}, β¦, a_r\}.
In this problem, for the integer multiset s, the median of s is equal to the β (|s|+1)/(2)β-th smallest number in it. For example, the median of \{1,4,4,6,5\} is 4, and the median of \{1,7,5,8\} is 5.
Slime wants Orac to make a_1 = a_2 = β¦ = a_n = k using these operations.
Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times.
Input
The first line of the input is a single integer t: the number of queries.
The first line of each query contains two integers n\ (1β€ nβ€ 100 000) and k\ (1β€ kβ€ 10^9), the second line contains n positive integers a_1,a_2,...,a_n\ (1β€ a_iβ€ 10^9)
The total sum of n is at most 100 000.
Output
The output should contain t lines. The i-th line should be equal to 'yes' if it is possible to make all integers k in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase.
Example
Input
5
5 3
1 5 2 6 1
1 6
6
3 2
1 2 3
4 3
3 1 2 3
10 3
1 2 3 4 5 6 7 8 9 10
Output
no
yes
yes
no
yes
Note
In the first query, Orac can't turn all elements into 3.
In the second query, a_1=6 is already satisfied.
In the third query, Orac can select the complete array and turn all elements into 2.
In the fourth query, Orac can't turn all elements into 3.
In the fifth query, Orac can select [1,6] at first and then select [2,10].
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Pinkie Pie has bought a bag of patty-cakes with different fillings! But it appeared that not all patty-cakes differ from one another with filling. In other words, the bag contains some patty-cakes with the same filling.
Pinkie Pie eats the patty-cakes one-by-one. She likes having fun so she decided not to simply eat the patty-cakes but to try not to eat the patty-cakes with the same filling way too often. To achieve this she wants the minimum distance between the eaten with the same filling to be the largest possible. Herein Pinkie Pie called the distance between two patty-cakes the number of eaten patty-cakes strictly between them.
Pinkie Pie can eat the patty-cakes in any order. She is impatient about eating all the patty-cakes up so she asks you to help her to count the greatest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating!
Pinkie Pie is going to buy more bags of patty-cakes so she asks you to solve this problem for several bags!
Input
The first line contains a single integer T (1 β€ T β€ 100): the number of bags for which you need to solve the problem.
The first line of each bag description contains a single integer n (2 β€ n β€ 10^5): the number of patty-cakes in it. The second line of the bag description contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n): the information of patty-cakes' fillings: same fillings are defined as same integers, different fillings are defined as different integers. It is guaranteed that each bag contains at least two patty-cakes with the same filling.
It is guaranteed that the sum of n over all bags does not exceed 10^5.
Output
For each bag print in separate line one single integer: the largest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating for that bag.
Example
Input
4
7
1 7 1 6 4 4 6
8
1 1 4 6 4 6 4 7
3
3 3 3
6
2 5 2 3 1 4
Output
3
2
0
4
Note
For the first bag Pinkie Pie can eat the patty-cakes in the following order (by fillings): 1, 6, 4, 7, 1, 6, 4 (in this way, the minimum distance is equal to 3).
For the second bag Pinkie Pie can eat the patty-cakes in the following order (by fillings): 1, 4, 6, 7, 4, 1, 6, 4 (in this way, the minimum distance is equal to 2).
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given an array a, consisting of n integers.
Each position i (1 β€ i β€ n) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.
For example, let a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}], the underlined positions are locked. You can obtain the following arrays:
* [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}];
* [1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}];
* [1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}];
* and some others.
Let p be a sequence of prefix sums of the array a after the rearrangement. So p_1 = a_1, p_2 = a_1 + a_2, p_3 = a_1 + a_2 + a_3, ..., p_n = a_1 + a_2 + ... + a_n.
Let k be the maximum j (1 β€ j β€ n) such that p_j < 0. If there are no j such that p_j < 0, then k = 0.
Your goal is to rearrange the values in such a way that k is minimum possible.
Output the array a after the rearrangement such that the value k for it is minimum possible. If there are multiple answers then print any of them.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of testcases.
Then t testcases follow.
The first line of each testcase contains a single integer n (1 β€ n β€ 100) β the number of elements in the array a.
The second line of each testcase contains n integers a_1, a_2, ..., a_n (-10^5 β€ a_i β€ 10^5) β the initial array a.
The third line of each testcase contains n integers l_1, l_2, ..., l_n (0 β€ l_i β€ 1), where l_i = 0 means that the position i is unlocked and l_i = 1 means that the position i is locked.
Output
Print n integers β the array a after the rearrangement. Value k (the maximum j such that p_j < 0 (or 0 if there are no such j)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones.
If there are multiple answers then print any of them.
Example
Input
5
3
1 3 2
0 0 0
4
2 -3 4 -1
1 1 1 1
7
-8 4 -2 -6 4 7 1
1 0 0 0 1 1 0
5
0 1 -4 6 3
0 0 0 1 1
6
-1 7 10 4 -8 -1
1 0 0 0 0 1
Output
1 2 3
2 -3 4 -1
-8 -6 1 4 4 7 -2
-4 0 1 6 3
-1 4 7 -8 10 -1
Note
In the first testcase you can rearrange all values however you want but any arrangement will result in k = 0. For example, for an arrangement [1, 2, 3], p=[1, 3, 6], so there are no j such that p_j < 0. Thus, k = 0.
In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.
In the third testcase the prefix sums for the printed array are p = [-8, -14, -13, -9, -5, 2, 0]. The maximum j is 5, thus k = 5. There are no arrangements such that k < 5.
In the fourth testcase p = [-4, -4, -3, 3, 6].
In the fifth testcase p = [-1, 3, 10, 2, 12, 11].
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
For a given array a consisting of n integers and a given integer m find if it is possible to reorder elements of the array a in such a way that β_{i=1}^{n}{β_{j=i}^{n}{(a_j)/(j)}} equals m? It is forbidden to delete elements as well as insert new elements. Please note that no rounding occurs during division, for example, 5/2=2.5.
Input
The first line contains a single integer t β the number of test cases (1 β€ t β€ 100). The test cases follow, each in two lines.
The first line of a test case contains two integers n and m (1 β€ n β€ 100, 0 β€ m β€ 10^6). The second line contains integers a_1, a_2, β¦, a_n (0 β€ a_i β€ 10^6) β the elements of the array.
Output
For each test case print "YES", if it is possible to reorder the elements of the array in such a way that the given formula gives the given value, and "NO" otherwise.
Example
Input
2
3 8
2 5 1
4 4
0 1 2 3
Output
YES
NO
Note
In the first test case one of the reorders could be [1, 2, 5]. The sum is equal to (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8. The brackets denote the inner sum β_{j=i}^{n}{(a_j)/(j)}, while the summation of brackets corresponds to the sum over i.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given an array of integers b_1, b_2, β¦, b_n.
An array a_1, a_2, β¦, a_n of integers is hybrid if for each i (1 β€ i β€ n) at least one of these conditions is true:
* b_i = a_i, or
* b_i = β_{j=1}^{i} a_j.
Find the number of hybrid arrays a_1, a_2, β¦, a_n. As the result can be very large, you should print the answer modulo 10^9 + 7.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains a single integer n (1 β€ n β€ 2 β
10^5).
The second line of each test case contains n integers b_1, b_2, β¦, b_n (-10^9 β€ b_i β€ 10^9).
It is guaranteed that the sum of n for all test cases does not exceed 2 β
10^5.
Output
For each test case, print a single integer: the number of hybrid arrays a_1, a_2, β¦, a_n modulo 10^9 + 7.
Example
Input
4
3
1 -1 1
4
1 2 3 4
10
2 -1 1 -2 2 3 -5 0 2 -1
4
0 0 0 1
Output
3
8
223
1
Note
In the first test case, the hybrid arrays are [1, -2, 1], [1, -2, 2], [1, -1, 1].
In the second test case, the hybrid arrays are [1, 1, 1, 1], [1, 1, 1, 4], [1, 1, 3, -1], [1, 1, 3, 4], [1, 2, 0, 1], [1, 2, 0, 4], [1, 2, 3, -2], [1, 2, 3, 4].
In the fourth test case, the only hybrid array is [0, 0, 0, 1].
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
I, Fischl, Prinzessin der Verurteilung, descend upon this land by the call of fate an β Oh, you are also a traveler from another world? Very well, I grant you permission to travel with me.
It is no surprise Fischl speaks with a strange choice of words. However, this time, not even Oz, her raven friend, can interpret her expressions! Maybe you can help us understand what this young princess is saying?
You are given a string of n lowercase Latin letters, the word that Fischl just spoke. You think that the MEX of this string may help you find the meaning behind this message. The MEX of the string is defined as the shortest string that doesn't appear as a contiguous substring in the input. If multiple strings exist, the lexicographically smallest one is considered the MEX. Note that the empty substring does NOT count as a valid MEX.
A string a is lexicographically smaller than a string b if and only if one of the following holds:
* a is a prefix of b, but a β b;
* in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
Find out what the MEX of the string is!
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 1000). Description of the test cases follows.
The first line of each test case contains an integer n (1 β€ n β€ 1000) β the length of the word. The second line for each test case contains a single string of n lowercase Latin letters.
The sum of n over all test cases will not exceed 1000.
Output
For each test case, output the MEX of the string on a new line.
Example
Input
3
28
qaabzwsxedcrfvtgbyhnujmiklop
13
cleanairactbd
10
aannttoonn
Output
ac
f
b
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
One day Polycarpus got hold of two non-empty strings s and t, consisting of lowercase Latin letters. Polycarpus is quite good with strings, so he immediately wondered, how many different pairs of "x y" are there, such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. Two pairs are considered different, if they contain different substrings of string s or different subsequences of string t. Read the whole statement to understand the definition of different substrings and subsequences.
The length of string s is the number of characters in it. If we denote the length of the string s as |s|, we can write the string as s = s1s2... s|s|.
A substring of s is a non-empty string x = s[a... b] = sasa + 1... sb (1 β€ a β€ b β€ |s|). For example, "code" and "force" are substrings or "codeforces", while "coders" is not. Two substrings s[a... b] and s[c... d] are considered to be different if a β c or b β d. For example, if s="codeforces", s[2...2] and s[6...6] are different, though their content is the same.
A subsequence of s is a non-empty string y = s[p1p2... p|y|] = sp1sp2... sp|y| (1 β€ p1 < p2 < ... < p|y| β€ |s|). For example, "coders" is a subsequence of "codeforces". Two subsequences u = s[p1p2... p|u|] and v = s[q1q2... q|v|] are considered different if the sequences p and q are different.
Input
The input consists of two lines. The first of them contains s (1 β€ |s| β€ 5000), and the second one contains t (1 β€ |t| β€ 5000). Both strings consist of lowercase Latin letters.
Output
Print a single number β the number of different pairs "x y" such that x is a substring of string s, y is a subsequence of string t, and the content of x and y is the same. As the answer can be rather large, print it modulo 1000000007 (109 + 7).
Examples
Input
aa
aa
Output
5
Input
codeforces
forceofcode
Output
60
Note
Let's write down all pairs "x y" that form the answer in the first sample: "s[1...1] t[1]", "s[2...2] t[1]", "s[1...1] t[2]","s[2...2] t[2]", "s[1...2] t[1 2]".
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The tournament Β«Sleepyhead-2010Β» in the rapid falling asleep has just finished in Berland. n best participants from the country have participated in it. The tournament consists of games, each of them is a match between two participants. nΒ·(n - 1) / 2 games were played during the tournament, and each participant had a match with each other participant.
The rules of the game are quite simple β the participant who falls asleep first wins. The secretary made a record of each game in the form Β«xi yiΒ», where xi and yi are the numbers of participants. The first number in each pair is a winner (i.e. xi is a winner and yi is a loser). There is no draws.
Recently researches form the Β«Institute Of SleepΒ» have found that every person is characterized by a value pj β the speed of falling asleep. The person who has lower speed wins. Every person has its own value pj, constant during the life.
It is known that all participants of the tournament have distinct speeds of falling asleep. Also it was found that the secretary made records about all the games except one. You are to find the result of the missing game.
Input
The first line contains one integer n (3 β€ n β€ 50) β the number of participants. The following nΒ·(n - 1) / 2 - 1 lines contain the results of the games. Each game is described in a single line by two integers xi, yi (1 β€ xi, yi β€ n, xi β yi), where xi ΠΈ yi are the numbers of the opponents in this game. It is known that during the tournament each of the n participants played n - 1 games, one game with each other participant.
Output
Output two integers x and y β the missing record. If there are several solutions, output any of them.
Examples
Input
4
4 2
4 1
2 3
2 1
3 1
Output
4 3
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Eugeny has array a = a1, a2, ..., an, consisting of n integers. Each integer ai equals to -1, or to 1. Also, he has m queries:
* Query number i is given as a pair of integers li, ri (1 β€ li β€ ri β€ n).
* The response to the query will be integer 1, if the elements of array a can be rearranged so as the sum ali + ali + 1 + ... + ari = 0, otherwise the response to the query will be integer 0.
Help Eugeny, answer all his queries.
Input
The first line contains integers n and m (1 β€ n, m β€ 2Β·105). The second line contains n integers a1, a2, ..., an (ai = -1, 1). Next m lines contain Eugene's queries. The i-th line contains integers li, ri (1 β€ li β€ ri β€ n).
Output
Print m integers β the responses to Eugene's queries in the order they occur in the input.
Examples
Input
2 3
1 -1
1 1
1 2
2 2
Output
0
1
0
Input
5 5
-1 1 1 1 -1
1 1
2 3
3 5
2 5
1 5
Output
0
1
0
1
0
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You're a mikemon breeder currently in the middle of your journey to become a mikemon master. Your current obstacle is go through the infamous Biridian Forest.
The forest
The Biridian Forest is a two-dimensional grid consisting of r rows and c columns. Each cell in Biridian Forest may contain a tree, or may be vacant. A vacant cell may be occupied by zero or more mikemon breeders (there may also be breeders other than you in the forest). Mikemon breeders (including you) cannot enter cells with trees. One of the cells is designated as the exit cell.
The initial grid, including your initial position, the exit cell, and the initial positions of all other breeders, will be given to you. Here's an example of such grid (from the first example):
<image>
Moves
Breeders (including you) may move in the forest. In a single move, breeders may perform one of the following actions:
* Do nothing.
* Move from the current cell to one of the four adjacent cells (two cells are adjacent if they share a side). Note that breeders cannot enter cells with trees.
* If you are located on the exit cell, you may leave the forest. Only you can perform this move β all other mikemon breeders will never leave the forest by using this type of movement.
After each time you make a single move, each of the other breeders simultaneously make a single move (the choice of which move to make may be different for each of the breeders).
Mikemon battle
If you and t (t > 0) mikemon breeders are located on the same cell, exactly t mikemon battles will ensue that time (since you will be battling each of those t breeders once). After the battle, all of those t breeders will leave the forest to heal their respective mikemons.
Note that the moment you leave the forest, no more mikemon battles can ensue, even if another mikemon breeder move to the exit cell immediately after that. Also note that a battle only happens between you and another breeders β there will be no battle between two other breeders (there may be multiple breeders coexisting in a single cell).
Your goal
You would like to leave the forest. In order to do so, you have to make a sequence of moves, ending with a move of the final type. Before you make any move, however, you post this sequence on your personal virtual idol Blog. Then, you will follow this sequence of moves faithfully.
Goal of other breeders
Because you post the sequence in your Blog, the other breeders will all know your exact sequence of moves even before you make your first move. All of them will move in such way that will guarantee a mikemon battle with you, if possible. The breeders that couldn't battle you will do nothing.
Your task
Print the minimum number of mikemon battles that you must participate in, assuming that you pick the sequence of moves that minimize this number. Note that you are not required to minimize the number of moves you make.
Input
The first line consists of two integers: r and c (1 β€ r, c β€ 1000), denoting the number of rows and the number of columns in Biridian Forest. The next r rows will each depict a row of the map, where each character represents the content of a single cell:
* 'T': A cell occupied by a tree.
* 'S': An empty cell, and your starting position. There will be exactly one occurence of this in the map.
* 'E': An empty cell, and where the exit is located. There will be exactly one occurence of this in the map.
* A digit (0-9): A cell represented by a digit X means that the cell is empty and is occupied by X breeders (in particular, if X is zero, it means that the cell is not occupied by any breeder).
It is guaranteed that it will be possible for you to go from your starting position to the exit cell through a sequence of moves.
Output
A single line denoted the minimum possible number of mikemon battles that you have to participate in if you pick a strategy that minimize this number.
Examples
Input
5 7
000E0T3
T0TT0T0
010T0T0
2T0T0T0
0T0S000
Output
3
Input
1 4
SE23
Output
2
Note
The following picture illustrates the first example. The blue line denotes a possible sequence of moves that you should post in your blog:
<image>
The three breeders on the left side of the map will be able to battle you β the lone breeder can simply stay in his place until you come while the other two breeders can move to where the lone breeder is and stay there until you come. The three breeders on the right does not have a way to battle you, so they will stay in their place.
For the second example, you should post this sequence in your Blog:
<image>
Here's what happens. First, you move one cell to the right.
<image>
Then, the two breeders directly to the right of the exit will simultaneously move to the left. The other three breeder cannot battle you so they will do nothing.
<image>
You end up in the same cell with 2 breeders, so 2 mikemon battles are conducted. After those battles, all of your opponents leave the forest.
<image>
Finally, you make another move by leaving the forest.
<image>
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
On a number line there are n balls. At time moment 0 for each ball the following data is known: its coordinate xi, speed vi (possibly, negative) and weight mi. The radius of the balls can be ignored.
The balls collide elastically, i.e. if two balls weighing m1 and m2 and with speeds v1 and v2 collide, their new speeds will be:
<image>.
Your task is to find out, where each ball will be t seconds after.
Input
The first line contains two integers n and t (1 β€ n β€ 10, 0 β€ t β€ 100) β amount of balls and duration of the process. Then follow n lines, each containing three integers: xi, vi, mi (1 β€ |vi|, mi β€ 100, |xi| β€ 100) β coordinate, speed and weight of the ball with index i at time moment 0.
It is guaranteed that no two balls have the same coordinate initially. Also each collision will be a collision of not more than two balls (that is, three or more balls never collide at the same point in all times from segment [0;t]).
Output
Output n numbers β coordinates of the balls t seconds after. Output the numbers accurate to at least 4 digits after the decimal point.
Examples
Input
2 9
3 4 5
0 7 8
Output
68.538461538
44.538461538
Input
3 10
1 2 3
4 -5 6
7 -8 9
Output
-93.666666667
-74.666666667
-15.666666667
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an n Γ m chessboard, a very tasty candy and two numbers a and b.
Dima put the chessboard in front of Inna and placed the candy in position (i, j) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types:
* move the candy from position (x, y) on the board to position (x - a, y - b);
* move the candy from position (x, y) on the board to position (x + a, y - b);
* move the candy from position (x, y) on the board to position (x - a, y + b);
* move the candy from position (x, y) on the board to position (x + a, y + b).
Naturally, Dima doesn't allow to move the candy beyond the chessboard borders.
Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (i, j) to one of the chessboard corners. Help them cope with the task!
Input
The first line of the input contains six integers n, m, i, j, a, b (1 β€ n, m β€ 106; 1 β€ i β€ n; 1 β€ j β€ m; 1 β€ a, b β€ 106).
You can assume that the chessboard rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to m from left to right. Position (i, j) in the statement is a chessboard cell on the intersection of the i-th row and the j-th column. You can consider that the corners are: (1, m), (n, 1), (n, m), (1, 1).
Output
In a single line print a single integer β the minimum number of moves needed to get the candy.
If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
Examples
Input
5 7 1 3 2 2
Output
2
Input
5 5 2 3 1 1
Output
Poor Inna and pony!
Note
Note to sample 1:
Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
DZY loves chemistry, and he enjoys mixing chemicals.
DZY has n chemicals, and m pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order.
Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is.
Find the maximum possible danger after pouring all the chemicals one by one in optimal order.
Input
The first line contains two space-separated integers n and m <image>.
Each of the next m lines contains two space-separated integers xi and yi (1 β€ xi < yi β€ n). These integers mean that the chemical xi will react with the chemical yi. Each pair of chemicals will appear at most once in the input.
Consider all the chemicals numbered from 1 to n in some order.
Output
Print a single integer β the maximum possible danger.
Examples
Input
1 0
Output
1
Input
2 1
1 2
Output
2
Input
3 2
1 2
2 3
Output
4
Note
In the first sample, there's only one way to pour, and the danger won't increase.
In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2.
In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring).
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, he didn't have enough money, so George was going to work as a programmer. Now he faced the following problem at the work.
Given a sequence of n integers p1, p2, ..., pn. You are to choose k pairs of integers:
[l1, r1], [l2, r2], ..., [lk, rk] (1 β€ l1 β€ r1 < l2 β€ r2 < ... < lk β€ rk β€ n; ri - li + 1 = m),
in such a way that the value of sum <image> is maximal possible. Help George to cope with the task.
Input
The first line contains three integers n, m and k (1 β€ (m Γ k) β€ n β€ 5000). The second line contains n integers p1, p2, ..., pn (0 β€ pi β€ 109).
Output
Print an integer in a single line β the maximum possible value of sum.
Examples
Input
5 2 1
1 2 3 4 5
Output
9
Input
7 1 3
2 10 7 18 5 33 0
Output
61
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given a permutation of n numbers p1, p2, ..., pn. We perform k operations of the following type: choose uniformly at random two indices l and r (l β€ r) and reverse the order of the elements pl, pl + 1, ..., pr. Your task is to find the expected value of the number of inversions in the resulting permutation.
Input
The first line of input contains two integers n and k (1 β€ n β€ 100, 1 β€ k β€ 109). The next line contains n integers p1, p2, ..., pn β the given permutation. All pi are different and in range from 1 to n.
The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
* In subproblem G1 (3 points), the constraints 1 β€ n β€ 6, 1 β€ k β€ 4 will hold.
* In subproblem G2 (5 points), the constraints 1 β€ n β€ 30, 1 β€ k β€ 200 will hold.
* In subproblem G3 (16 points), the constraints 1 β€ n β€ 100, 1 β€ k β€ 109 will hold.
Output
Output the answer with absolute or relative error no more than 1e - 9.
Examples
Input
3 1
1 2 3
Output
0.833333333333333
Input
3 4
1 3 2
Output
1.458333333333334
Note
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability <image>, the interval will consist of a single element and the permutation will not be altered. With probability <image> we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability <image> the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to <image>.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
King of Berland Berl IV has recently died. Hail Berl V! As a sign of the highest achievements of the deceased king the new king decided to build a mausoleum with Berl IV's body on the main square of the capital.
The mausoleum will be constructed from 2n blocks, each of them has the shape of a cuboid. Each block has the bottom base of a 1 Γ 1 meter square. Among the blocks, exactly two of them have the height of one meter, exactly two have the height of two meters, ..., exactly two have the height of n meters.
The blocks are arranged in a row without spacing one after the other. Of course, not every arrangement of blocks has the form of a mausoleum. In order to make the given arrangement in the form of the mausoleum, it is necessary that when you pass along the mausoleum, from one end to the other, the heights of the blocks first were non-decreasing (i.e., increasing or remained the same), and then β non-increasing (decrease or remained unchanged). It is possible that any of these two areas will be omitted. For example, the following sequences of block height meet this requirement:
* [1, 2, 2, 3, 4, 4, 3, 1];
* [1, 1];
* [2, 2, 1, 1];
* [1, 2, 3, 3, 2, 1].
Suddenly, k more requirements appeared. Each of the requirements has the form: "h[xi] signi h[yi]", where h[t] is the height of the t-th block, and a signi is one of the five possible signs: '=' (equals), '<' (less than), '>' (more than), '<=' (less than or equals), '>=' (more than or equals). Thus, each of the k additional requirements is given by a pair of indexes xi, yi (1 β€ xi, yi β€ 2n) and sign signi.
Find the number of possible ways to rearrange the blocks so that both the requirement about the shape of the mausoleum (see paragraph 3) and the k additional requirements were met.
Input
The first line of the input contains integers n and k (1 β€ n β€ 35, 0 β€ k β€ 100) β the number of pairs of blocks and the number of additional requirements.
Next k lines contain listed additional requirements, one per line in the format "xi signi yi" (1 β€ xi, yi β€ 2n), and the sign is on of the list of the five possible signs.
Output
Print the sought number of ways.
Examples
Input
3 0
Output
9
Input
3 1
2 > 3
Output
1
Input
4 1
3 = 6
Output
3
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Petya has recently started working as a programmer in the IT city company that develops computer games.
Besides game mechanics implementation to create a game it is necessary to create tool programs that can be used by game designers to create game levels. Petya's first assignment is to create a tool that allows to paint different arrows on the screen.
A user of this tool will choose a point on the screen, specify a vector (the arrow direction) and vary several parameters to get the required graphical effect. In the first version of the program Petya decided to limit parameters of the arrow by the following: a point with coordinates (px, py), a nonzero vector with coordinates (vx, vy), positive scalars a, b, c, d, a > c.
The produced arrow should have the following properties. The arrow consists of a triangle and a rectangle. The triangle is isosceles with base of length a and altitude of length b perpendicular to the base. The rectangle sides lengths are c and d. Point (px, py) is situated in the middle of the triangle base and in the middle of side of rectangle that has length c. Area of intersection of the triangle and the rectangle is zero. The direction from (px, py) point to the triangle vertex opposite to base containing the point coincides with direction of (vx, vy) vector.
Enumerate the arrow points coordinates in counter-clockwise order starting from the tip.
<image>
Input
The only line of the input contains eight integers px, py, vx, vy ( - 1000 β€ px, py, vx, vy β€ 1000, vx2 + vy2 > 0), a, b, c, d (1 β€ a, b, c, d β€ 1000, a > c).
Output
Output coordinates of the arrow points in counter-clockwise order. Each line should contain two coordinates, first x, then y. Relative or absolute error should not be greater than 10 - 9.
Examples
Input
8 8 0 2 8 3 4 5
Output
8.000000000000 11.000000000000
4.000000000000 8.000000000000
6.000000000000 8.000000000000
6.000000000000 3.000000000000
10.000000000000 3.000000000000
10.000000000000 8.000000000000
12.000000000000 8.000000000000
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Long ago, Vasily built a good fence at his country house. Vasily calls a fence good, if it is a series of n consecutively fastened vertical boards of centimeter width, the height of each in centimeters is a positive integer. The house owner remembers that the height of the i-th board to the left is hi.
Today Vasily decided to change the design of the fence he had built, by cutting his top connected part so that the fence remained good. The cut part should consist of only the upper parts of the boards, while the adjacent parts must be interconnected (share a non-zero length before cutting out of the fence).
You, as Vasily's curious neighbor, will count the number of possible ways to cut exactly one part as is described above. Two ways to cut a part are called distinct, if for the remaining fences there is such i, that the height of the i-th boards vary.
As Vasily's fence can be very high and long, get the remainder after dividing the required number of ways by 1 000 000 007 (109 + 7).
Input
The first line contains integer n (1 β€ n β€ 1 000 000) β the number of boards in Vasily's fence.
The second line contains n space-separated numbers h1, h2, ..., hn (1 β€ hi β€ 109), where hi equals the height of the i-th board to the left.
Output
Print the remainder after dividing r by 1 000 000 007, where r is the number of ways to cut exactly one connected part so that the part consisted of the upper parts of the boards and the remaining fence was good.
Examples
Input
2
1 1
Output
0
Input
3
3 4 2
Output
13
Note
From the fence from the first example it is impossible to cut exactly one piece so as the remaining fence was good.
All the possible variants of the resulting fence from the second sample look as follows (the grey shows the cut out part):
<image>
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
After returned from forest, Alyona started reading a book. She noticed strings s and t, lengths of which are n and m respectively. As usual, reading bored Alyona and she decided to pay her attention to strings s and t, which she considered very similar.
Alyona has her favourite positive integer k and because she is too small, k does not exceed 10. The girl wants now to choose k disjoint non-empty substrings of string s such that these strings appear as disjoint substrings of string t and in the same order as they do in string s. She is also interested in that their length is maximum possible among all variants.
Formally, Alyona wants to find a sequence of k non-empty strings p1, p2, p3, ..., pk satisfying following conditions:
* s can be represented as concatenation a1p1a2p2... akpkak + 1, where a1, a2, ..., ak + 1 is a sequence of arbitrary strings (some of them may be possibly empty);
* t can be represented as concatenation b1p1b2p2... bkpkbk + 1, where b1, b2, ..., bk + 1 is a sequence of arbitrary strings (some of them may be possibly empty);
* sum of the lengths of strings in sequence is maximum possible.
Please help Alyona solve this complicated problem and find at least the sum of the lengths of the strings in a desired sequence.
A substring of a string is a subsequence of consecutive characters of the string.
Input
In the first line of the input three integers n, m, k (1 β€ n, m β€ 1000, 1 β€ k β€ 10) are given β the length of the string s, the length of the string t and Alyona's favourite number respectively.
The second line of the input contains string s, consisting of lowercase English letters.
The third line of the input contains string t, consisting of lowercase English letters.
Output
In the only line print the only non-negative integer β the sum of the lengths of the strings in a desired sequence.
It is guaranteed, that at least one desired sequence exists.
Examples
Input
3 2 2
abc
ab
Output
2
Input
9 12 4
bbaaababb
abbbabbaaaba
Output
7
Note
The following image describes the answer for the second sample case:
<image>
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are n applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't).
q events are about to happen (in chronological order). They are of three types:
1. Application x generates a notification (this new notification is unread).
2. Thor reads all notifications generated so far by application x (he may re-read some notifications).
3. Thor reads the first t notifications generated by phone applications (notifications generated in first t events of the first type). It's guaranteed that there were at least t events of the first type before this event. Please note that he doesn't read first t unread notifications, he just reads the very first t notifications generated on his phone and he may re-read some of them in this operation.
Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone.
Input
The first line of input contains two integers n and q (1 β€ n, q β€ 300 000) β the number of applications and the number of events to happen.
The next q lines contain the events. The i-th of these lines starts with an integer typei β type of the i-th event. If typei = 1 or typei = 2 then it is followed by an integer xi. Otherwise it is followed by an integer ti (1 β€ typei β€ 3, 1 β€ xi β€ n, 1 β€ ti β€ q).
Output
Print the number of unread notifications after each event.
Examples
Input
3 4
1 3
1 1
1 2
2 3
Output
1
2
3
2
Input
4 6
1 2
1 4
1 2
3 3
1 3
1 3
Output
1
2
3
0
1
2
Note
In the first sample:
1. Application 3 generates a notification (there is 1 unread notification).
2. Application 1 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads the notification generated by application 3, there are 2 unread notifications left.
In the second sample test:
1. Application 2 generates a notification (there is 1 unread notification).
2. Application 4 generates a notification (there are 2 unread notifications).
3. Application 2 generates a notification (there are 3 unread notifications).
4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left.
5. Application 3 generates a notification (there is 1 unread notification).
6. Application 3 generates a notification (there are 2 unread notifications).
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Bachgold problem is very easy to formulate. Given a positive integer n represent it as a sum of maximum possible number of prime numbers. One can prove that such representation exists for any integer greater than 1.
Recall that integer k is called prime if it is greater than 1 and has exactly two positive integer divisors β 1 and k.
Input
The only line of the input contains a single integer n (2 β€ n β€ 100 000).
Output
The first line of the output contains a single integer k β maximum possible number of primes in representation.
The second line should contain k primes with their sum equal to n. You can print them in any order. If there are several optimal solution, print any of them.
Examples
Input
5
Output
2
2 3
Input
6
Output
3
2 2 2
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
After returning from the army Makes received a gift β an array a consisting of n positive integer numbers. He hadn't been solving problems for a long time, so he became interested to answer a particular question: how many triples of indices (i, j, k) (i < j < k), such that aiΒ·ajΒ·ak is minimum possible, are there in the array? Help him with it!
Input
The first line of input contains a positive integer number n (3 β€ n β€ 105) β the number of elements in array a. The second line contains n positive integer numbers ai (1 β€ ai β€ 109) β the elements of a given array.
Output
Print one number β the quantity of triples (i, j, k) such that i, j and k are pairwise distinct and aiΒ·ajΒ·ak is minimum possible.
Examples
Input
4
1 1 1 1
Output
4
Input
5
1 3 2 3 4
Output
2
Input
6
1 3 3 1 3 2
Output
1
Note
In the first example Makes always chooses three ones out of four, and the number of ways to choose them is 4.
In the second example a triple of numbers (1, 2, 3) is chosen (numbers, not indices). Since there are two ways to choose an element 3, then the answer is 2.
In the third example a triple of numbers (1, 1, 2) is chosen, and there's only one way to choose indices.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Gleb ordered pizza home. When the courier delivered the pizza, he was very upset, because several pieces of sausage lay on the crust, and he does not really like the crust.
The pizza is a circle of radius r and center at the origin. Pizza consists of the main part β circle of radius r - d with center at the origin, and crust around the main part of the width d. Pieces of sausage are also circles. The radius of the i -th piece of the sausage is ri, and the center is given as a pair (xi, yi).
Gleb asks you to help determine the number of pieces of sausage caught on the crust. A piece of sausage got on the crust, if it completely lies on the crust.
Input
First string contains two integer numbers r and d (0 β€ d < r β€ 500) β the radius of pizza and the width of crust.
Next line contains one integer number n β the number of pieces of sausage (1 β€ n β€ 105).
Each of next n lines contains three integer numbers xi, yi and ri ( - 500 β€ xi, yi β€ 500, 0 β€ ri β€ 500), where xi and yi are coordinates of the center of i-th peace of sausage, ri β radius of i-th peace of sausage.
Output
Output the number of pieces of sausage that lay on the crust.
Examples
Input
8 4
7
7 8 1
-7 3 2
0 2 1
0 -2 2
-3 -3 1
0 6 2
5 3 1
Output
2
Input
10 8
4
0 0 9
0 0 10
1 0 1
1 0 2
Output
0
Note
Below is a picture explaining the first example. Circles of green color denote pieces of sausage lying on the crust.
<image>
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers.
Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2Β·n people in the group (including Vadim), and they have exactly n - 1 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. i-th person's weight is wi, and weight is an important matter in kayaking β if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash.
Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks.
Help the party to determine minimum possible total instability!
Input
The first line contains one number n (2 β€ n β€ 50).
The second line contains 2Β·n integer numbers w1, w2, ..., w2n, where wi is weight of person i (1 β€ wi β€ 1000).
Output
Print minimum possible total instability.
Examples
Input
2
1 2 3 4
Output
1
Input
4
1 3 4 6 3 4 100 200
Output
5
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are n points marked on the plane. The points are situated in such a way that they form a regular polygon (marked points are its vertices, and they are numbered in counter-clockwise order). You can draw n - 1 segments, each connecting any two marked points, in such a way that all points have to be connected with each other (directly or indirectly).
But there are some restrictions. Firstly, some pairs of points cannot be connected directly and have to be connected undirectly. Secondly, the segments you draw must not intersect in any point apart from the marked points (that is, if any two segments intersect and their intersection is not a marked point, then the picture you have drawn is invalid).
How many ways are there to connect all vertices with n - 1 segments? Two ways are considered different iff there exist some pair of points such that a segment is drawn between them in the first way of connection, but it is not drawn between these points in the second one. Since the answer might be large, output it modulo 109 + 7.
Input
The first line contains one number n (3 β€ n β€ 500) β the number of marked points.
Then n lines follow, each containing n elements. ai, j (j-th element of line i) is equal to 1 iff you can connect points i and j directly (otherwise ai, j = 0). It is guaranteed that for any pair of points ai, j = aj, i, and for any point ai, i = 0.
Output
Print the number of ways to connect points modulo 109 + 7.
Examples
Input
3
0 0 1
0 0 1
1 1 0
Output
1
Input
4
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
Output
12
Input
3
0 0 0
0 0 1
0 1 0
Output
0
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array. An inversion in a permutation p is a pair of indices (i, j) such that i > j and ai < aj. For example, a permutation [4, 1, 3, 2] contains 4 inversions: (2, 1), (3, 1), (4, 1), (4, 3).
You are given a permutation a of size n and m queries to it. Each query is represented by two indices l and r denoting that you have to reverse the segment [l, r] of the permutation. For example, if a = [1, 2, 3, 4] and a query l = 2, r = 4 is applied, then the resulting permutation is [1, 4, 3, 2].
After each query you have to determine whether the number of inversions is odd or even.
Input
The first line contains one integer n (1 β€ n β€ 1500) β the size of the permutation.
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ n) β the elements of the permutation. These integers are pairwise distinct.
The third line contains one integer m (1 β€ m β€ 2Β·105) β the number of queries to process.
Then m lines follow, i-th line containing two integers li, ri (1 β€ li β€ ri β€ n) denoting that i-th query is to reverse a segment [li, ri] of the permutation. All queries are performed one after another.
Output
Print m lines. i-th of them must be equal to odd if the number of inversions in the permutation after i-th query is odd, and even otherwise.
Examples
Input
3
1 2 3
2
1 2
2 3
Output
odd
even
Input
4
1 2 4 3
4
1 1
1 4
1 4
2 3
Output
odd
odd
odd
even
Note
The first example:
1. after the first query a = [2, 1, 3], inversion: (2, 1);
2. after the second query a = [2, 3, 1], inversions: (3, 1), (3, 2).
The second example:
1. a = [1, 2, 4, 3], inversion: (4, 3);
2. a = [3, 4, 2, 1], inversions: (3, 1), (4, 1), (3, 2), (4, 2), (4, 3);
3. a = [1, 2, 4, 3], inversion: (4, 3);
4. a = [1, 4, 2, 3], inversions: (3, 2), (4, 2).
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
In order to put away old things and welcome a fresh new year, a thorough cleaning of the house is a must.
Little Tommy finds an old polynomial and cleaned it up by taking it modulo another. But now he regrets doing this...
Given two integers p and k, find a polynomial f(x) with non-negative integer coefficients strictly less than k, whose remainder is p when divided by (x + k). That is, f(x) = q(x)Β·(x + k) + p, where q(x) is a polynomial (not necessarily with integer coefficients).
Input
The only line of input contains two space-separated integers p and k (1 β€ p β€ 1018, 2 β€ k β€ 2 000).
Output
If the polynomial does not exist, print a single integer -1, or output two lines otherwise.
In the first line print a non-negative integer d β the number of coefficients in the polynomial.
In the second line print d space-separated integers a0, a1, ..., ad - 1, describing a polynomial <image> fulfilling the given requirements. Your output should satisfy 0 β€ ai < k for all 0 β€ i β€ d - 1, and ad - 1 β 0.
If there are many possible solutions, print any of them.
Examples
Input
46 2
Output
7
0 1 0 0 1 1 1
Input
2018 214
Output
3
92 205 1
Note
In the first example, f(x) = x6 + x5 + x4 + x = (x5 - x4 + 3x3 - 6x2 + 12x - 23)Β·(x + 2) + 46.
In the second example, f(x) = x2 + 205x + 92 = (x - 9)Β·(x + 214) + 2018.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Year 2118. Androids are in mass production for decades now, and they do all the work for humans. But androids have to go to school to be able to solve creative tasks. Just like humans before.
It turns out that high school struggles are not gone. If someone is not like others, he is bullied. Vasya-8800 is an economy-class android which is produced by a little-known company. His design is not perfect, his characteristics also could be better. So he is bullied by other androids.
One of the popular pranks on Vasya is to force him to compare x^y with y^x. Other androids can do it in milliseconds while Vasya's memory is too small to store such big numbers.
Please help Vasya! Write a fast program to compare x^y with y^x for Vasya, maybe then other androids will respect him.
Input
On the only line of input there are two integers x and y (1 β€ x, y β€ 10^{9}).
Output
If x^y < y^x, then print '<' (without quotes). If x^y > y^x, then print '>' (without quotes). If x^y = y^x, then print '=' (without quotes).
Examples
Input
5 8
Output
>
Input
10 3
Output
<
Input
6 6
Output
=
Note
In the first example 5^8 = 5 β
5 β
5 β
5 β
5 β
5 β
5 β
5 = 390625, and 8^5 = 8 β
8 β
8 β
8 β
8 = 32768. So you should print '>'.
In the second example 10^3 = 1000 < 3^{10} = 59049.
In the third example 6^6 = 46656 = 6^6.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
In McD's Burger, n hungry burger fans are ordering burgers. The ith order is placed by the ith fan at ti time and it takes di time to procees. What is the order in which the fans will get their burgers?
Input Format
On the first line you will get n, the number of orders. Then n lines will follow. On the (i+1)th line, you will get ti and di separated by a single space.
Output Format
Print the order ( as single space separated integers ) in which the burger fans get their burgers. If two fans get the burger at the same time, then print the smallest numbered order first.(remember, the fans are numbered 1 to n).
Constraints
1β€nβ€103
1β€ti,diβ€106
SAMPLE INPUT
5
8 1
4 2
5 6
3 1
4 3
SAMPLE OUTPUT
4 2 5 1 3
Explanation
The first order is placed at time 3 and it takes 1 unit of time to process, so the burger is sent to the customer at time 4.
The second order is placed at time 4 and it takes 2 units of time to process, the burger is sent to customer at time 6.
The third order is placed at time 4 and it takes 3 units of time to process, the burger is sent to the customer at time 7.
Similarly, the fourth and fifth orders are sent to the customer at time 9 and time 11.
So the order of delivery of burgers is, 4 2 5 1 3.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You have just purchased a new mobile phone and you want to call all of your relatives to brag about your new phone. You have N relatives. You will talk to i^th relative for exactly Ti minutes. Each minute costs you 1 dollar . However, your relatives are generous. Hence after the conversation, they will add a recharge of Xi dollars in your mobile. Initially, you have M dollars balance in your mobile phone.
Find the minimum value of M, that you must have initially, in your phone, so that you don't run out of balance during any of the call (encounter negative balance).
Note : You can call relatives in any order. Each relative will be called exactly once.
INPUT
The first line will contain N, the number of relatives. Next N lines will have two space separated integers, "Ti Xi" (quotes for clarity), representing call duration with the i^th relative and the amount of recharge you will get from that relative after the conversation.
OUTPUT
Output a single integer M, the minimum required initial balance in your mobile phone.
CONSTRAINTS
1 β€ N,X,T β€ 10^5
SAMPLE INPUT
2
1 1
2 1
SAMPLE OUTPUT
2
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given N real values A_1, A_2, \ldots, A_N. Compute the number of pairs of indices (i, j) such that i < j and the product A_i \cdot A_j is integer.
Constraints
* 2 \leq N \leq 200\,000
* 0 < A_i < 10^4
* A_i is given with at most 9 digits after the decimal.
Input
Input is given from Standard Input in the following format.
N
A_1
A_2
\vdots
A_N
Output
Print the number of pairs with integer product A_i \cdot A_j (and i < j).
Examples
Input
5
7.5
2.4
17.000000001
17
16.000000000
Output
3
Input
11
0.9
1
1
1.25
2.30000
5
70
0.000000001
9999.999999999
0.999999999
1.000000001
Output
8
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Given any integer x, Aoki can do the operation below.
Operation: Replace x with the absolute difference of x and K.
You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.
Constraints
* 0 β€ N β€ 10^{18}
* 1 β€ K β€ 10^{18}
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K
Output
Print the minimum possible value taken by N after Aoki does the operation zero or more times.
Examples
Input
7 4
Output
1
Input
2 6
Output
2
Input
1000000000000000000 1
Output
0
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
We have a string S consisting of uppercase English letters. Additionally, an integer N will be given.
Shift each character of S by N in alphabetical order (see below), and print the resulting string.
We assume that `A` follows `Z`. For example, shifting `A` by 2 results in `C` (`A` \to `B` \to `C`), and shifting `Y` by 3 results in `B` (`Y` \to `Z` \to `A` \to `B`).
Constraints
* 0 \leq N \leq 26
* 1 \leq |S| \leq 10^4
* S consists of uppercase English letters.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the string resulting from shifting each character of S by N in alphabetical order.
Examples
Input
2
ABCXYZ
Output
CDEZAB
Input
0
ABCXYZ
Output
ABCXYZ
Input
13
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Output
NOPQRSTUVWXYZABCDEFGHIJKLM
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are N monsters, numbered 1, 2, ..., N.
Initially, the health of Monster i is A_i.
Below, a monster with at least 1 health is called alive.
Until there is only one alive monster, the following is repeated:
* A random alive monster attacks another random alive monster.
* As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.
Find the minimum possible final health of the last monster alive.
Constraints
* All values in input are integers.
* 2 \leq N \leq 10^5
* 1 \leq A_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the minimum possible final health of the last monster alive.
Examples
Input
4
2 10 8 40
Output
2
Input
4
5 13 8 1000000000
Output
1
Input
3
1000000000 1000000000 1000000000
Output
1000000000
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Takahashi throws N dice, each having K sides with all integers from 1 to K. The dice are NOT pairwise distinguishable. For each i=2,3,...,2K, find the following value modulo 998244353:
* The number of combinations of N sides shown by the dice such that the sum of no two different sides is i.
Note that the dice are NOT distinguishable, that is, two combinations are considered different when there exists an integer k such that the number of dice showing k is different in those two.
Constraints
* 1 \leq K \leq 2000
* 2 \leq N \leq 2000
* K and N are integers.
Input
Input is given from Standard Input in the following format:
K N
Output
Print 2K-1 integers. The t-th of them (1\leq t\leq 2K-1) should be the answer for i=t+1.
Examples
Input
3 3
Output
7
7
4
7
7
Input
4 5
Output
36
36
20
20
20
36
36
Input
6 1000
Output
149393349
149393349
668669001
668669001
4000002
4000002
4000002
668669001
668669001
149393349
149393349
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.
Constraints
* 1\leq N \leq 10^{16}
* N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.
Examples
Input
100
Output
18
Input
9995
Output
35
Input
3141592653589793
Output
137
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are N lines in the xy-plane. The i-th line is represented by A_ix+B_iy=C_i. Any two lines among the N+2 lines, the above N lines plus the x-axis and y-axis, cross each other at exactly one point.
For each pair 1 \leq i < j \leq N, there is a car at the cross point of the i-th and j-th lines. Even where three or more lines intersect at a point, a car is individually placed for each pair of lines. That is, there will be k(k-1)/2 cars placed at the intersection of k lines.
Those cars are already very old, and can only be moved parallel to the x-axis or y-axis.
Takahashi will hold an exhibition of antique cars at a place on the xy-plane. In order to avoid damaging the half-broken cars too much, he will select the place of the exhibition so that the total distance covered will be minimized when all the cars are moved to the place. If such a place is not uniquely determined, among the places that satisfy the condition above, the place with the minimum x-coordinate will be selected. If the place is still not uniquely determined, among the places that satisfy the two conditions above, the place with the minimum y-coordinate will be selected.
Find the place of the exhibition that will be selected.
Constraints
* 2 \leq N \leq 4 Γ 10^4
* 1 \leq |A_i|,|B_i| \leq 10^4(1 \leq i \leq N)
* 0 \leq |C_i| \leq 10^4(1 \leq i \leq N)
* No two given lines are parallel.
* All input values are integers.
Inputs
Input is given from Standard Input in the following format:
N
A_1 B_1 C_1
:
A_N B_N C_N
Outputs
Print the x-coordinate and y-coordinate of the place of the exhibition that will be selected, in this order, with a space in between. The output will be judged as correct when the absolute or relative error is at most 10^{-9}.
Examples
Input
3
1 1 1
2 -1 2
-1 2 2
Output
1.000000000000000 1.000000000000000
Input
4
1 1 2
1 -1 0
3 -1 -2
1 -3 4
Output
-1.000000000000000 -1.000000000000000
Input
7
1 7 8
-2 4 9
3 -8 -5
9 2 -14
6 7 5
-8 -9 3
3 8 10
Output
-1.722222222222222 1.325000000000000
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Write a program which reads an integer n and prints the number of prime numbers which are less than or equal to n. A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7.
Input
Input consists of several datasets. Each dataset has an integer n (1 β€ n β€ 999,999) in a line.
The number of datasets is less than or equal to 30.
Output
For each dataset, prints the number of prime numbers.
Example
Input
10
3
11
Output
4
2
5
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
I decided to create a program that displays a "round and round pattern". The "round and round pattern" is as follows.
* If the length of one side is n, it is displayed as a character string with n rows and n columns.
* A spiral pattern that rotates clockwise with the lower left corner as the base point.
* The part with a line is represented by # (half-width sharp), and the blank part is represented by "" (half-width blank).
* Leave a space between the lines.
Create a program that takes an integer n as an input and outputs a "round and round pattern" with a side length of n.
Input
The input is given in the following format:
d
n1
n2
::
nd
The number of datasets d (d β€ 20) is given to the first line, and the side length ni (1 β€ ni β€ 100) of the i-th round pattern is given to each of the following d lines.
Output
Please output a round and round pattern for each data set. Insert a blank line between the datasets.
Example
Input
2
5
6
Output
#####
# #
# # #
# # #
# ###
######
# #
# ## #
# # #
# # #
# ####
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
problem
JOI Pizza sells pizza home delivery along the d-meter-long ring road that runs through the city center.
JOI Pizza has n stores S1, ..., Sn on the loop line. The head office is S1. The distance from S1 to Si when moving the loop line clockwise is set to di meters. D2 , ..., dn is an integer greater than or equal to 1 and less than or equal to d -1. D2, ..., dn are all different. Bake and deliver pizza at the shortest store.
The location of the delivery destination is represented by an integer k that is greater than or equal to 0 and less than or equal to d -1. This means that the distance from the head office S1 to the delivery destination in the clockwise direction is k meters. Pizza delivery is done along the loop line and no other road is allowed. However, the loop line may move clockwise or counterclockwise.
For example, if the location of the store and the location of the delivery destination are as shown in the figure below (this example corresponds to Example 1 of "I / O example").
<image>
The store closest to the delivery destination 1 is S2, so the delivery is from store S2. At this time, the distance traveled from the store is 1. Also, the store closest to delivery destination 2 is S1 (main store), so store S1 (main store). ) To deliver to home. At this time, the distance traveled from the store is 2.
Total length of the loop line d, Number of JOI pizza stores n, Number of orders m, N --1 integer representing a location other than the main store d2, ..., dn, Integer k1, .. representing the location of the delivery destination Given ., km, create a program to find the sum of all orders for each order by the distance traveled during delivery (ie, the distance from the nearest store to the delivery destination).
input
The input consists of multiple datasets. Each dataset is given in the following format.
The first line is a positive integer d (2 β€ d β€ 1000000000 = 109) that represents the total length of the loop line, the second line is a positive integer n (2 β€ n β€ 100000) that represents the number of stores, and the third line is A positive integer m (1 β€ m β€ 10000) is written to represent the number of orders. The n --1 lines after the 4th line are integers d2, d3, ..., dn that represent the location of stores other than the main store. (1 β€ di β€ d -1) is written in this order, and the integers k1, k2, ..., km (0 β€ ki β€ d) representing the delivery destination location are in the m lines after the n + 3rd line. --1) are written in this order.
Of the scoring data, for 40% of the points, n β€ 10000 is satisfied. For 40% of the points, the total distance traveled and the value of d are both 1000000 or less. In the scoring data, the total distance traveled is 1000000000 = 109 or less.
When d is 0, it indicates the end of input. The number of data sets does not exceed 10.
output
For each data set, one integer representing the total distance traveled during delivery is output on one line.
Examples
Input
8
3
2
3
1
4
6
20
4
4
12
8
16
7
7
11
8
0
Output
3
3
Input
None
Output
None
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Turtle Shi-ta and turtle Be-ko decided to divide a chocolate. The shape of the chocolate is rectangle. The corners of the chocolate are put on (0,0), (w,0), (w,h) and (0,h). The chocolate has lines for cutting. They cut the chocolate only along some of these lines.
The lines are expressed as follows. There are m points on the line connected between (0,0) and (0,h), and between (w,0) and (w,h). Each i-th point, ordered by y value (i = 0 then y =0), is connected as cutting line. These lines do not share any point where 0 < x < w . They can share points where x = 0 or x = w . However, (0, li ) = (0,lj ) and (w,ri ) = (w,rj ) implies i = j . The following figure shows the example of the chocolate.
<image>
There are n special almonds, so delicious but high in calories on the chocolate. Almonds are circle but their radius is too small. You can ignore radius of almonds. The position of almond is expressed as (x,y) coordinate.
Two turtles are so selfish. Both of them have requirement for cutting.
Shi-ta's requirement is that the piece for her is continuous. It is possible for her that she cannot get any chocolate. Assume that the minimum index of the piece is i and the maximum is k . She must have all pieces between i and k . For example, chocolate piece 1,2,3 is continuous but 1,3 or 0,2,4 is not continuous.
Be-ko requires that the area of her chocolate is at least S . She is worry about her weight. So she wants the number of almond on her chocolate is as few as possible.
They doesn't want to make remainder of the chocolate because the chocolate is expensive.
Your task is to compute the minumum number of Beko's almonds if both of their requirment are satisfied.
Input
Input consists of multiple test cases.
Each dataset is given in the following format.
n m w h S
l0 r0
...
lm-1 rm-1
x0 y0
...
xn-1 yn-1
The last line contains five 0s. n is the number of almonds. m is the number of lines. w is the width of the chocolate. h is the height of the chocolate. S is the area that Be-ko wants to eat. Input is integer except xi yi.
Input satisfies following constraints.
1 β€ n β€ 30000
1 β€ m β€ 30000
1 β€ w β€ 200
1 β€ h β€ 1000000
0 β€ S β€ w*h
If i < j then li β€ lj and ri β€ rj and then i -th lines and j -th lines share at most 1 point. It is assured that lm-1 and rm-1 are h. xi yi is floating point number with ten digits. It is assured that they are inside of the chocolate. It is assured that the distance between points and any lines is at least 0.00001.
Output
You should output the minumum number of almonds for Be-ko.
Example
Input
2 3 10 10 50
3 3
6 6
10 10
4.0000000000 4.0000000000
7.0000000000 7.0000000000
2 3 10 10 70
3 3
6 6
10 10
4.0000000000 4.0000000000
7.0000000000 7.0000000000
2 3 10 10 30
3 3
6 6
10 10
4.0000000000 4.0000000000
7.0000000000 7.0000000000
2 3 10 10 40
3 3
6 6
10 10
4.0000000000 4.0000000000
7.0000000000 7.0000000000
2 3 10 10 100
3 3
6 6
10 10
4.0000000000 4.0000000000
7.0000000000 7.0000000000
0 0 0 0 0
Output
1
1
0
1
2
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
ABC Gene
There is a gene sequence represented by the string `ABC`. You can rewrite this gene sequence by performing the following operations several times.
* Choose one of the letters `A`,` B`, `C`. Let this be x. Replace all x in the gene sequence with `ABC` at the same time.
Given a string S consisting only of `A`,` B`, and `C`. Determine if the gene sequence can be matched to S.
Constraints
* 1 β€ | S | β€ 5,000
* S consists only of `A`,` B`, and `C`.
Input Format
Input is given from standard input in the following format.
S
Output Format
Output `Yes` if the gene sequence can be matched to S, and` No` if it cannot be matched.
Sample Input 1
ABC
Sample Output 1
Yes
The gene sequence is `ABC` from the beginning.
Sample Input 2
AABCC
Sample Output 2
Yes
If you select `B` and perform the operation, it becomes` ABC` β `AABCC`.
Sample Input 3
AABCABC
Sample Output 3
No
For example, even if you select `C` and perform an operation, it does not change from` AABCC` to `AABCABC`. Since all `C`s are replaced with` ABC` at the same time, the actual result is `AABCC` β` AABABCABC`.
Example
Input
ABC
Output
Yes
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Chokudai loves eating so much. However, his doctor Akensho told him that he was overweight, so he finally decided to lose his weight.
Chokudai made a slimming plan of a $D$-day cycle. It is represented by $D$ integers $w_0, ..., w_{D-1}$. His weight is $S$ on the 0-th day of the plan and he aims to reduce it to $T$ ($S > T$). If his weight on the $i$-th day of the plan is $x$, it will be $x + w_{i\%D}$ on the $(i+1)$-th day. Note that $i\%D$ is the remainder obtained by dividing $i$ by $D$. If his weight successfully gets less than or equal to $T$, he will stop slimming immediately.
If his slimming plan takes too many days or even does not end forever, he should reconsider it.
Determine whether it ends or not, and report how many days it takes if it ends.
Input
The input consists of a single test case formatted as follows.
$S$ $T$ $D$
$w_0 ... w_{D-1}$
The first line consists of three integers $S$, $T$, $D$ ($1 \leq S, T, D \leq 100,000, S > T$). The second line consists of $D$ integers $w_0, ..., w_{D-1}$ ($-100,000 \leq w_i \leq 100,000$ for each $i$).
Output
If Chokudai's slimming plan ends on the $d$-th day, print $d$ in one line. If it never ends, print $-1$.
Examples
Input
65 60 3
-2 3 -4
Output
4
Input
65 60 3
-2 10 -3
Output
-1
Input
100000 1 1
-1
Output
99999
Input
60 59 1
-123
Output
1
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Write a program which prints $n$-th fibonacci number for a given integer $n$. The $n$-th fibonacci number is defined by the following recursive formula:
\begin{equation*} fib(n)= \left \\{ \begin{array}{ll} 1 & (n = 0) \\\ 1 & (n = 1) \\\ fib(n - 1) + fib(n - 2) & \\\ \end{array} \right. \end{equation*}
Constraints
* $0 \leq n \leq 44$
Input
An integer $n$ is given.
Example
Input
3
Output
3
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Write a program which calculates the distance between two points P1(x1, y1) and P2(x2, y2).
Input
Four real numbers x1, y1, x2 and y2 are given in a line.
Output
Print the distance in real number. The output should not contain an absolute error greater than 10-4.
Example
Input
0 0 1 1
Output
1.41421356
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
This is an interactive problem.
Imur Ishakov decided to organize a club for people who love to play the famous game Β«The hatΒ». The club was visited by n students, where n is even. Imur arranged them all in a circle and held a draw to break the students in pairs, but something went wrong. The participants are numbered so that participant i and participant i + 1 (1 β€ i β€ n - 1) are adjacent, as well as participant n and participant 1. Each student was given a piece of paper with a number in such a way, that for every two adjacent students, these numbers differ exactly by one. The plan was to form students with the same numbers in a pair, but it turned out that not all numbers appeared exactly twice.
As you know, the most convenient is to explain the words to the partner when he is sitting exactly across you. Students with numbers i and <image> sit across each other. Imur is wondering if there are two people sitting across each other with the same numbers given. Help him to find such pair of people if it exists.
You can ask questions of form Β«which number was received by student i?Β», and the goal is to determine whether the desired pair exists in no more than 60 questions.
Input
At the beginning the even integer n (2 β€ n β€ 100 000) is given β the total number of students.
You are allowed to ask no more than 60 questions.
Output
To ask the question about the student i (1 β€ i β€ n), you should print Β«? iΒ». Then from standard output you can read the number ai received by student i ( - 109 β€ ai β€ 109).
When you find the desired pair, you should print Β«! iΒ», where i is any student who belongs to the pair (1 β€ i β€ n). If you determined that such pair doesn't exist, you should output Β«! -1Β». In both cases you should immediately terminate the program.
The query that contains your answer is not counted towards the limit of 60 queries.
Please make sure to flush the standard output after each command. For example, in C++ use function fflush(stdout), in Java call System.out.flush(), in Pascal use flush(output) and stdout.flush() for Python language.
Hacking
Use the following format for hacking:
In the first line, print one even integer n (2 β€ n β€ 100 000) β the total number of students.
In the second line print n integers ai ( - 109 β€ ai β€ 109) separated by spaces, where ai is the number to give to i-th student. Any two adjacent elements, including n and 1, must differ by 1 or - 1.
The hacked solution will not have direct access to the sequence ai.
Examples
Input
8
<span class="tex-span"></span>
2
<span class="tex-span"></span>
2
Output
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 8
<span class="tex-span"></span>
! 4
Input
6
<span class="tex-span"></span>
1
<span class="tex-span"></span>
2
<span class="tex-span"></span>
3
<span class="tex-span"></span>
2
<span class="tex-span"></span>
1
<span class="tex-span"></span>
0
Output
<span class="tex-span"></span>
? 1
<span class="tex-span"></span>
? 2
<span class="tex-span"></span>
? 3
<span class="tex-span"></span>
? 4
<span class="tex-span"></span>
? 5
<span class="tex-span"></span>
? 6
<span class="tex-span"></span>
! -1
Note
Input-output in statements illustrates example interaction.
In the first sample the selected sequence is 1, 2, 1, 2, 3, 4, 3, 2
In the second sample the selection sequence is 1, 2, 3, 2, 1, 0.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are n benches in the Berland Central park. It is known that a_i people are currently sitting on the i-th bench. Another m people are coming to the park and each of them is going to have a seat on some bench out of n available.
Let k be the maximum number of people sitting on one bench after additional m people came to the park. Calculate the minimum possible k and the maximum possible k.
Nobody leaves the taken seat during the whole process.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of benches in the park.
The second line contains a single integer m (1 β€ m β€ 10 000) β the number of people additionally coming to the park.
Each of the next n lines contains a single integer a_i (1 β€ a_i β€ 100) β the initial number of people on the i-th bench.
Output
Print the minimum possible k and the maximum possible k, where k is the maximum number of people sitting on one bench after additional m people came to the park.
Examples
Input
4
6
1
1
1
1
Output
3 7
Input
1
10
5
Output
15 15
Input
3
6
1
6
5
Output
6 12
Input
3
7
1
6
5
Output
7 13
Note
In the first example, each of four benches is occupied by a single person. The minimum k is 3. For example, it is possible to achieve if two newcomers occupy the first bench, one occupies the second bench, one occupies the third bench, and two remaining β the fourth bench. The maximum k is 7. That requires all six new people to occupy the same bench.
The second example has its minimum k equal to 15 and maximum k equal to 15, as there is just a single bench in the park and all 10 people will occupy it.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Lunar New Year is approaching, and Bob decides to take a wander in a nearby park.
The park can be represented as a connected graph with n nodes and m bidirectional edges. Initially Bob is at the node 1 and he records 1 on his notebook. He can wander from one node to another through those bidirectional edges. Whenever he visits a node not recorded on his notebook, he records it. After he visits all nodes at least once, he stops wandering, thus finally a permutation of nodes a_1, a_2, β¦, a_n is recorded.
Wandering is a boring thing, but solving problems is fascinating. Bob wants to know the lexicographically smallest sequence of nodes he can record while wandering. Bob thinks this problem is trivial, and he wants you to solve it.
A sequence x is lexicographically smaller than a sequence y if and only if one of the following holds:
* x is a prefix of y, but x β y (this is impossible in this problem as all considered sequences have the same length);
* in the first position where x and y differ, the sequence x has a smaller element than the corresponding element in y.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 10^5), denoting the number of nodes and edges, respectively.
The following m lines describe the bidirectional edges in the graph. The i-th of these lines contains two integers u_i and v_i (1 β€ u_i, v_i β€ n), representing the nodes the i-th edge connects.
Note that the graph can have multiple edges connecting the same two nodes and self-loops. It is guaranteed that the graph is connected.
Output
Output a line containing the lexicographically smallest sequence a_1, a_2, β¦, a_n Bob can record.
Examples
Input
3 2
1 2
1 3
Output
1 2 3
Input
5 5
1 4
3 4
5 4
3 2
1 5
Output
1 4 3 2 5
Input
10 10
1 4
6 8
2 5
3 7
9 4
5 6
3 4
8 10
8 9
1 10
Output
1 4 3 7 9 8 6 5 2 10
Note
In the first sample, Bob's optimal wandering path could be 1 β 2 β 1 β 3. Therefore, Bob will obtain the sequence \{1, 2, 3\}, which is the lexicographically smallest one.
In the second sample, Bob's optimal wandering path could be 1 β 4 β 3 β 2 β 3 β 4 β 1 β 5. Therefore, Bob will obtain the sequence \{1, 4, 3, 2, 5\}, which is the lexicographically smallest one.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are a coach at your local university. There are n students under your supervision, the programming skill of the i-th student is a_i.
You have to form k teams for yet another new programming competition. As you know, the more students are involved in competition the more probable the victory of your university is! So you have to form no more than k (and at least one) non-empty teams so that the total number of students in them is maximized. But you also know that each team should be balanced. It means that the programming skill of each pair of students in each team should differ by no more than 5. Teams are independent from one another (it means that the difference between programming skills of two students from two different teams does not matter).
It is possible that some students not be included in any team at all.
Your task is to report the maximum possible total number of students in no more than k (and at least one) non-empty balanced teams.
If you are Python programmer, consider using PyPy instead of Python when you submit your code.
Input
The first line of the input contains two integers n and k (1 β€ k β€ n β€ 5000) β the number of students and the maximum number of teams, correspondingly.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is a programming skill of the i-th student.
Output
Print one integer β the maximum possible total number of students in no more than k (and at least one) non-empty balanced teams.
Examples
Input
5 2
1 2 15 15 15
Output
5
Input
6 1
36 4 1 25 9 16
Output
2
Input
4 4
1 10 100 1000
Output
4
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given a sequence a_1, a_2, ..., a_n consisting of n integers.
You can choose any non-negative integer D (i.e. D β₯ 0), and for each a_i you can:
* add D (only once), i. e. perform a_i := a_i + D, or
* subtract D (only once), i. e. perform a_i := a_i - D, or
* leave the value of a_i unchanged.
It is possible that after an operation the value a_i becomes negative.
Your goal is to choose such minimum non-negative integer D and perform changes in such a way, that all a_i are equal (i.e. a_1=a_2=...=a_n).
Print the required D or, if it is impossible to choose such value D, print -1.
For example, for array [2, 8] the value D=3 is minimum possible because you can obtain the array [5, 5] if you will add D to 2 and subtract D from 8. And for array [1, 4, 7, 7] the value D=3 is also minimum possible. You can add it to 1 and subtract it from 7 and obtain the array [4, 4, 4, 4].
Input
The first line of the input contains one integer n (1 β€ n β€ 100) β the number of elements in a.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 100) β the sequence a.
Output
Print one integer β the minimum non-negative integer value D such that if you add this value to some a_i, subtract this value from some a_i and leave some a_i without changes, all obtained values become equal.
If it is impossible to choose such value D, print -1.
Examples
Input
6
1 4 4 7 4 1
Output
3
Input
5
2 2 5 2 5
Output
3
Input
4
1 3 3 7
Output
-1
Input
2
2 8
Output
3
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Today Adilbek is taking his probability theory test. Unfortunately, when Adilbek arrived at the university, there had already been a long queue of students wanting to take the same test. Adilbek has estimated that he will be able to start the test only T seconds after coming.
Fortunately, Adilbek can spend time without revising any boring theorems or formulas. He has an app on this smartphone which contains n Japanese crosswords to solve. Adilbek has decided to solve them all one by one in the order they are listed in the app, without skipping any crossword. For each crossword, a number t_i is given that represents the time it takes an average crossword expert to solve this crossword (the time is given in seconds).
Adilbek is a true crossword expert, but, unfortunately, he is sometimes unlucky in choosing the way to solve the crossword. So, it takes him either t_i seconds or t_i + 1 seconds to solve the i-th crossword, equiprobably (with probability 1/2 he solves the crossword in exactly t_i seconds, and with probability 1/2 he has to spend an additional second to finish the crossword). All these events are independent.
After T seconds pass (or after solving the last crossword, if he manages to do it in less than T seconds), Adilbek closes the app (if he finishes some crossword at the same moment, that crossword is considered solved; otherwise Adilbek does not finish solving the current crossword at all). He thinks it would be an interesting probability theory problem to calculate E β the expected number of crosswords he will be able to solve completely. Can you calculate it?
Recall that the expected value of a discrete random variable is the probability-weighted average of all possible values β in this problem it means that the expected value of the number of solved crosswords can be calculated as E = β _{i = 0}^{n} i p_i, where p_i is the probability that Adilbek will solve exactly i crosswords.
We can represent E as rational fraction P/Q with Q > 0. To give the answer, you should print P β
Q^{-1} mod (10^9 + 7).
Input
The first line contains two integers n and T (1 β€ n β€ 2 β
10^5, 1 β€ T β€ 2 β
10^{14}) β the number of crosswords and the time Adilbek has to spend, respectively.
The second line contains n integers t_1, t_2, ..., t_n (1 β€ t_i β€ 10^9), where t_i is the time it takes a crossword expert to solve the i-th crossword.
Note that Adilbek solves the crosswords in the order they are given in the input without skipping any of them.
Output
Print one integer β the expected value of the number of crosswords Adilbek solves in T seconds, expressed in the form of P β
Q^{-1} mod (10^9 + 7).
Examples
Input
3 5
2 2 2
Output
750000007
Input
3 5
2 1 2
Output
125000003
Note
The answer for the first sample is equal to 14/8.
The answer for the second sample is equal to 17/8.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
The only difference between easy and hard versions are constraints on n and k.
You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most k most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals 0).
Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend.
You (suddenly!) have the ability to see the future. You know that during the day you will receive n messages, the i-th message will be received from the friend with ID id_i (1 β€ id_i β€ 10^9).
If you receive a message from id_i in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages.
Otherwise (i.e. if there is no conversation with id_i on the screen):
* Firstly, if the number of conversations displayed on the screen is k, the last conversation (which has the position k) is removed from the screen.
* Now the number of conversations on the screen is guaranteed to be less than k and the conversation with the friend id_i is not displayed on the screen.
* The conversation with the friend id_i appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down.
Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all n messages.
Input
The first line of the input contains two integers n and k (1 β€ n, k β€ 200) β the number of messages and the number of conversations your smartphone can show.
The second line of the input contains n integers id_1, id_2, ..., id_n (1 β€ id_i β€ 10^9), where id_i is the ID of the friend which sends you the i-th message.
Output
In the first line of the output print one integer m (1 β€ m β€ min(n, k)) β the number of conversations shown after receiving all n messages.
In the second line print m integers ids_1, ids_2, ..., ids_m, where ids_i should be equal to the ID of the friend corresponding to the conversation displayed on the position i after receiving all n messages.
Examples
Input
7 2
1 2 3 2 1 3 2
Output
2
2 1
Input
10 4
2 3 3 1 1 2 1 2 3 3
Output
3
1 3 2
Note
In the first example the list of conversations will change in the following way (in order from the first to last message):
* [];
* [1];
* [2, 1];
* [3, 2];
* [3, 2];
* [1, 3];
* [1, 3];
* [2, 1].
In the second example the list of conversations will change in the following way:
* [];
* [2];
* [3, 2];
* [3, 2];
* [1, 3, 2];
* and then the list will not change till the end.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are four stones on an infinite line in integer coordinates a_1, a_2, a_3, a_4. The goal is to have the stones in coordinates b_1, b_2, b_3, b_4. The order of the stones does not matter, that is, a stone from any position a_i can end up in at any position b_j, provided there is a required number of stones in each position (that is, if a coordinate x appears k times among numbers b_1, β¦, b_4, there should be exactly k stones at x in the end).
We are allowed to move stones with the following operation: choose two stones at distinct positions x and y with at least one stone each, and move one stone from x to 2y - x. In other words, the operation moves a stone to a symmetric position relative to some other stone. At any moment it is allowed to have any number of stones at the same position.
Find any sequence of operations that achieves the goal, or determine that it is impossible. The sequence does not have to be shortest, but it may contain at most 1000 operations.
Input
The first line contains four integers a_1, β¦, a_4 (-10^9 β€ a_i β€ 10^9) β initial coordinates of the stones. There may be multiple stones sharing the same coordinate.
The second line contains four integers b_1, β¦, b_4 (-10^9 β€ b_i β€ 10^9) β target coordinates of the stones. There may be multiple targets sharing the same coordinate.
Output
If there is no sequence of operations that achieves the goal, print a single integer -1. Otherwise, on the first line print a single integer k (0 β€ k β€ 1000) β the number of operations in your sequence. On the next k lines, describe the operations. The i-th of these lines should contain two integers x_i and y_i (x_i β y_i) β coordinates of the moved stone and the center of symmetry stone for the i-th operation.
For each operation i, there should at least one stone in each of the coordinates x_i and y_i, and the resulting coordinate 2y_i - x_i must not exceed 10^{18} by absolute value.
If there are multiple suitable sequences, print any of them. It is guaranteed that if there is a suitable sequence of operations, then there is also a suitable sequence that satisfies all the additional requirement.
Examples
Input
0 1 2 3
3 5 6 8
Output
3
1 3
2 5
0 3
Input
0 0 0 0
1 1 1 1
Output
-1
Input
0 0 0 1
0 1 0 1
Output
-1
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
This is a hard version of the problem. The actual problems are different, but the easy version is almost a subtask of the hard version. Note that the constraints and the output format are different.
You are given a string s consisting of n lowercase Latin letters.
You have to color all its characters the minimum number of colors (each character to exactly one color, the same letters can be colored the same or different colors, i.e. you can choose exactly one color for each index in s).
After coloring, you can swap any two neighboring characters of the string that are colored different colors. You can perform such an operation arbitrary (possibly, zero) number of times.
The goal is to make the string sorted, i.e. all characters should be in alphabetical order.
Your task is to find the minimum number of colors which you have to color the given string in so that after coloring it can become sorted by some sequence of swaps. Note that you have to restore only coloring, not the sequence of swaps.
Input
The first line of the input contains one integer n (1 β€ n β€ 2 β
10^5) β the length of s.
The second line of the input contains the string s consisting of exactly n lowercase Latin letters.
Output
In the first line print one integer res (1 β€ res β€ n) β the minimum number of colors in which you have to color the given string so that after coloring it can become sorted by some sequence of swaps.
In the second line print any possible coloring that can be used to sort the string using some sequence of swaps described in the problem statement. The coloring is the array c of length n, where 1 β€ c_i β€ res and c_i means the color of the i-th character.
Examples
Input
9
abacbecfd
Output
2
1 1 2 1 2 1 2 1 2
Input
8
aaabbcbb
Output
2
1 2 1 2 1 2 1 1
Input
7
abcdedc
Output
3
1 1 1 1 1 2 3
Input
5
abcde
Output
1
1 1 1 1 1
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given four positive integers n, m, a, b (1 β€ b β€ n β€ 50; 1 β€ a β€ m β€ 50). Find any such rectangular matrix of size n Γ m that satisfies all of the following conditions:
* each row of the matrix contains exactly a ones;
* each column of the matrix contains exactly b ones;
* all other elements are zeros.
If the desired matrix does not exist, indicate this.
For example, for n=3, m=6, a=2, b=1, there exists a matrix satisfying the conditions above:
$$$ \begin{vmatrix} 0 & 1 & 0 & 0 & 0 & 1 \\\ 1 & 0 & 0 & 1 & 0 & 0 \\\ 0 & 0 & 1 & 0 & 1 & 0 \end{vmatrix} $$$
Input
The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases. Then t test cases follow.
Each test case is described by four positive integers n, m, a, b (1 β€ b β€ n β€ 50; 1 β€ a β€ m β€ 50), where n and m are the sizes of the matrix, and a and b are the number of ones for rows and columns, respectively.
Output
For each test case print:
* "YES" (without quotes) and the required matrix (if there are several answers, print any) if it exists, or
* "NO" (without quotes) if it does not exist.
To print the matrix n Γ m, print n rows, each of which consists of m numbers 0 or 1 describing a row of the matrix. Numbers must be printed without spaces.
Example
Input
5
3 6 2 1
2 2 2 1
2 2 2 2
4 4 2 2
2 1 1 2
Output
YES
010001
100100
001010
NO
YES
11
11
YES
1100
1100
0011
0011
YES
1
1
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
We have a point A with coordinate x = n on OX-axis. We'd like to find an integer point B (also on OX-axis), such that the absolute difference between the distance from O to B and the distance from A to B is equal to k.
<image> The description of the first test case.
Since sometimes it's impossible to find such point B, we can, in one step, increase or decrease the coordinate of A by 1. What is the minimum number of steps we should do to make such point B exist?
Input
The first line contains one integer t (1 β€ t β€ 6000) β the number of test cases.
The only line of each test case contains two integers n and k (0 β€ n, k β€ 10^6) β the initial position of point A and desirable absolute difference.
Output
For each test case, print the minimum number of steps to make point B exist.
Example
Input
6
4 0
5 8
0 1000000
0 0
1 0
1000000 1000000
Output
0
3
1000000
0
1
0
Note
In the first test case (picture above), if we set the coordinate of B as 2 then the absolute difference will be equal to |(2 - 0) - (4 - 2)| = 0 and we don't have to move A. So the answer is 0.
In the second test case, we can increase the coordinate of A by 3 and set the coordinate of B as 0 or 8. The absolute difference will be equal to |8 - 0| = 8, so the answer is 3.
<image>
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya loves long lucky numbers very much. He is interested in the minimum lucky number d that meets some condition. Let cnt(x) be the number of occurrences of number x in number d as a substring. For example, if d = 747747, then cnt(4) = 2, cnt(7) = 4, cnt(47) = 2, cnt(74) = 2. Petya wants the following condition to fulfil simultaneously: cnt(4) = a1, cnt(7) = a2, cnt(47) = a3, cnt(74) = a4. Petya is not interested in the occurrences of other numbers. Help him cope with this task.
Input
The single line contains four integers a1, a2, a3 and a4 (1 β€ a1, a2, a3, a4 β€ 106).
Output
On the single line print without leading zeroes the answer to the problem β the minimum lucky number d such, that cnt(4) = a1, cnt(7) = a2, cnt(47) = a3, cnt(74) = a4. If such number does not exist, print the single number "-1" (without the quotes).
Examples
Input
2 2 1 1
Output
4774
Input
4 7 3 1
Output
-1
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Polycarp is an organizer of a Berland ICPC regional event. There are n universities in Berland numbered from 1 to n. Polycarp knows all competitive programmers in the region. There are n students: the i-th student is enrolled at a university u_i and has a programming skill s_i.
Polycarp has to decide on the rules now. In particular, the number of members in the team.
Polycarp knows that if he chooses the size of the team to be some integer k, each university will send their k strongest (with the highest programming skill s) students in the first team, the next k strongest students in the second team and so on. If there are fewer than k students left, then the team can't be formed. Note that there might be universities that send zero teams.
The strength of the region is the total skill of the members of all present teams. If there are no teams present, then the strength is 0.
Help Polycarp to find the strength of the region for each choice of k from 1 to n.
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of testcases.
The first line of each testcase contains a single integer n (1 β€ n β€ 2 β
10^5) β the number of universities and the number of students.
The second line of each testcase contains n integers u_1, u_2, ..., u_n (1 β€ u_i β€ n) β the university the i-th student is enrolled at.
The third line of each testcase contains n integers s_1, s_2, ..., s_n (1 β€ s_i β€ 10^9) β the programming skill of the i-th student.
The sum of n over all testcases doesn't exceed 2 β
10^5.
Output
For each testcase print n integers: the strength of the region β the total skill of the members of the present teams β for each choice of team size k.
Example
Input
4
7
1 2 1 2 1 2 1
6 8 3 1 5 1 5
10
1 1 1 2 2 2 2 3 3 3
3435 3014 2241 2233 2893 2102 2286 2175 1961 2567
6
3 3 3 3 3 3
5 9 6 7 9 7
1
1
3083
Output
29 28 26 19 0 0 0
24907 20705 22805 9514 0 0 0 0 0 0
43 43 43 32 38 43
3083
Note
In the first testcase the teams from each university for each k are:
* k=1:
* university 1: [6], [5], [5], [3];
* university 2: [8], [1], [1];
* k=2:
* university 1: [6, 5], [5, 3];
* university 2: [8, 1];
* k=3:
* university 1: [6, 5, 5];
* university 2: [8, 1, 1];
* k=4:
* university 1: [6, 5, 5, 3];
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
AquaMoon has n friends. They stand in a row from left to right, and the i-th friend from the left wears a T-shirt with a number a_i written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.
AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.
AquaMoon hopes that after some operations, the numbers written on the T-shirt of n friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
Input
The input consists of multiple test cases. The first line contains a single integer t (1 β€ t β€ 50) β the number of test cases.
The first line of each test case contains a single integer n (1 β€ n β€ 10^5) β the number of Aquamoon's friends.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^5) β the numbers, written on the T-shirts.
It is guaranteed that the sum of n for all test cases does not exceed 10^5.
Output
For each test case, if there exists a possible sequence of operations, print "YES" (without quotes); otherwise, print "NO" (without quotes).
You can print each letter in any case (upper or lower).
Example
Input
3
4
4 3 2 5
4
3 3 2 2
5
1 2 3 5 4
Output
YES
YES
NO
Note
The possible list of operations in the first test case:
1. Swap a_1 and a_2. The resulting sequence is 3, 4, 2, 5. The directions are: left, left, right, right.
2. Swap a_2 and a_3. The resulting sequence is 3, 2, 4, 5. The directions are: left, left, right, right.
3. Swap a_1 and a_2. The resulting sequence is 2, 3, 4, 5. The directions are: right, right, right, right.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
John Doe has a list of all Fibonacci numbers modulo 1013. This list is infinite, it starts with numbers 0 and 1. Each number in the list, apart from the first two, is a sum of previous two modulo 1013. That is, John's list is made from the Fibonacci numbers' list by replacing each number there by the remainder when divided by 1013.
John got interested in number f (0 β€ f < 1013) and now wants to find its first occurrence in the list given above. Help John and find the number of the first occurence of number f in the list or otherwise state that number f does not occur in the list.
The numeration in John's list starts from zero. There, the 0-th position is the number 0, the 1-st position is the number 1, the 2-nd position is the number 1, the 3-rd position is the number 2, the 4-th position is the number 3 and so on. Thus, the beginning of the list looks like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
Input
The first line contains the single integer f (0 β€ f < 1013) β the number, which position in the list we should find.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
Print a single number β the number of the first occurrence of the given number in John's list. If this number doesn't occur in John's list, print -1.
Examples
Input
13
Output
7
Input
377
Output
14
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given a square matrix consisting of n rows and n columns. We assume that the rows are numbered from 1 to n from top to bottom and the columns are numbered from 1 to n from left to right. Some cells (n - 1 cells in total) of the the matrix are filled with ones, the remaining cells are filled with zeros. We can apply the following operations to the matrix:
1. Swap i-th and j-th rows of the matrix;
2. Swap i-th and j-th columns of the matrix.
You are asked to transform the matrix into a special form using these operations. In that special form all the ones must be in the cells that lie below the main diagonal. Cell of the matrix, which is located on the intersection of the i-th row and of the j-th column, lies below the main diagonal if i > j.
Input
The first line contains an integer n (2 β€ n β€ 1000) β the number of rows and columns. Then follow n - 1 lines that contain one's positions, one per line. Each position is described by two integers xk, yk (1 β€ xk, yk β€ n), separated by a space. A pair (xk, yk) means that the cell, which is located on the intersection of the xk-th row and of the yk-th column, contains one.
It is guaranteed that all positions are distinct.
Output
Print the description of your actions. These actions should transform the matrix to the described special form.
In the first line you should print a non-negative integer m (m β€ 105) β the number of actions. In each of the next m lines print three space-separated integers t, i, j (1 β€ t β€ 2, 1 β€ i, j β€ n, i β j), where t = 1 if you want to swap rows, t = 2 if you want to swap columns, and i and j denote the numbers of rows or columns respectively.
Please note, that you do not need to minimize the number of operations, but their number should not exceed 105. If there are several solutions, you may print any of them.
Examples
Input
2
1 2
Output
2
2 1 2
1 1 2
Input
3
3 1
1 3
Output
3
2 2 3
1 1 3
1 1 2
Input
3
2 1
3 2
Output
0
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are n students living in the campus. Every morning all students wake up at the same time and go to wash. There are m rooms with wash basins. The i-th of these rooms contains ai wash basins. Every student independently select one the rooms with equal probability and goes to it. After all students selected their rooms, students in each room divide into queues by the number of wash basins so that the size of the largest queue is the least possible. Calculate the expected value of the size of the largest queue among all rooms.
Input
The first line contains two positive integers n and m (1 β€ n, m β€ 50) β the amount of students and the amount of rooms. The second line contains m integers a1, a2, ... , am (1 β€ ai β€ 50). ai means the amount of wash basins in the i-th room.
Output
Output single number: the expected value of the size of the largest queue. Your answer must have an absolute or relative error less than 10 - 9.
Examples
Input
1 1
2
Output
1.00000000000000000000
Input
2 2
1 1
Output
1.50000000000000000000
Input
2 3
1 1 1
Output
1.33333333333333350000
Input
7 5
1 1 2 3 1
Output
2.50216960000000070000
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
During the last Sereja's Codesecrof round the server crashed many times, so the round was decided to be made unrated for some participants.
Let's assume that n people took part in the contest. Let's assume that the participant who got the first place has rating a1, the second place participant has rating a2, ..., the n-th place participant has rating an. Then changing the rating on the Codesecrof site is calculated by the formula <image>.
After the round was over, the Codesecrof management published the participants' results table. They decided that if for a participant di < k, then the round can be considered unrated for him. But imagine the management's surprise when they found out that the participants' rating table is dynamic. In other words, when some participant is removed from the rating, he is removed from the results' table and the rating is recalculated according to the new table. And of course, all applications for exclusion from the rating are considered in view of the current table.
We know that among all the applications for exclusion from the rating the first application to consider is from the participant with the best rank (the rank with the minimum number), for who di < k. We also know that the applications for exclusion from rating were submitted by all participants.
Now Sereja wonders, what is the number of participants to be excluded from the contest rating, and the numbers of the participants in the original table in the order of their exclusion from the rating. Pay attention to the analysis of the first test case for a better understanding of the statement.
Input
The first line contains two integers n, k (1 β€ n β€ 2Β·105, - 109 β€ k β€ 0). The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 109) β ratings of the participants in the initial table.
Output
Print the numbers of participants in the order in which they were removed from the table. Print the initial numbers of the participants, that is, the numbers that the participants had in the initial table.
Examples
Input
5 0
5 3 4 1 2
Output
2
3
4
Input
10 -10
5 5 1 7 5 1 2 4 9 2
Output
2
4
5
7
8
9
Note
Consider the first test sample.
1. Initially the sequence of the contest participants' ratings equals [5, 3, 4, 1, 2]. You can use this sequence to calculate the sequence of rating changes: [0, -9, -13, 8, 14]. According to the problem statement, the application of the participant who won the second place will be considered first.
2. As soon as the second place winner is out from the ratings, the participants' rating sequence will equal [5, 4, 1, 2]. By this sequence you can count the new sequence of rating changes: [0, -8, 2, 6]. According to the problem statement, the application of the participant who won the second place will be considered. Initially this participant won third place.
3. The new rating sequence equals [5, 1, 2], the new sequence of rating changes equals [0, -1, 1]. The second place participant's application is taken into consideration, initially this participant won the fourth place.
4. The new rating sequence equals [5, 2], the new sequence of rating changes equals [0, 0]. No more applications will be considered.
Thus, you should print 2, 3, 4.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 β the total number of bombs in adjacent squares.
For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs.
Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end.
Input
The first line contains sequence of characters without spaces s1s2... sn (1 β€ n β€ 106), containing only characters "*", "?" and digits "0", "1" or "2". If character si equals "*", then the i-th cell of the field contains a bomb. If character si equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square.
Output
Print a single integer β the number of ways Valera can fill the empty cells and get a correct field.
As the answer can be rather large, print it modulo 1000000007 (109 + 7).
Examples
Input
?01???
Output
4
Input
?
Output
2
Input
**12
Output
0
Input
1
Output
0
Note
In the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning:
a1, a2, ..., an β an, a1, a2, ..., an - 1.
Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence?
Input
The first line contains an integer n (2 β€ n β€ 105). The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 105).
Output
If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it.
Examples
Input
2
2 1
Output
1
Input
3
1 3 2
Output
-1
Input
2
1 2
Output
0
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and "BA" (the substrings can go in any order).
Input
The only line of input contains a string s of length between 1 and 105 consisting of uppercase Latin letters.
Output
Print "YES" (without the quotes), if string s contains two non-overlapping substrings "AB" and "BA", and "NO" otherwise.
Examples
Input
ABA
Output
NO
Input
BACFAB
Output
YES
Input
AXBYBXA
Output
NO
Note
In the first sample test, despite the fact that there are substrings "AB" and "BA", their occurrences overlap, so the answer is "NO".
In the second sample test there are the following occurrences of the substrings: BACFAB.
In the third sample test there is no substring "AB" nor substring "BA".
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You are given a sequence of numbers a1, a2, ..., an, and a number m.
Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m.
Input
The first line contains two numbers, n and m (1 β€ n β€ 106, 2 β€ m β€ 103) β the size of the original sequence and the number such that sum should be divisible by it.
The second line contains n integers a1, a2, ..., an (0 β€ ai β€ 109).
Output
In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist.
Examples
Input
3 5
1 2 3
Output
YES
Input
1 6
5
Output
NO
Input
4 6
3 1 1 3
Output
YES
Input
6 6
5 5 5 5 5 5
Output
YES
Note
In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5.
In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist.
In the third sample test you need to choose two numbers 3 on the ends.
In the fourth sample test you can take the whole subsequence.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Little Artem is fond of dancing. Most of all dances Artem likes rueda β Cuban dance that is danced by pairs of boys and girls forming a circle and dancing together.
More detailed, there are n pairs of boys and girls standing in a circle. Initially, boy number 1 dances with a girl number 1, boy number 2 dances with a girl number 2 and so on. Girls are numbered in the clockwise order. During the dance different moves are announced and all pairs perform this moves. While performing moves boys move along the circle, while girls always stay at their initial position. For the purpose of this problem we consider two different types of moves:
1. Value x and some direction are announced, and all boys move x positions in the corresponding direction.
2. Boys dancing with even-indexed girls swap positions with boys who are dancing with odd-indexed girls. That is the one who was dancing with the girl 1 swaps with the one who was dancing with the girl number 2, while the one who was dancing with girl number 3 swaps with the one who was dancing with the girl number 4 and so one. It's guaranteed that n is even.
Your task is to determine the final position of each boy.
Input
The first line of the input contains two integers n and q (2 β€ n β€ 1 000 000, 1 β€ q β€ 2 000 000) β the number of couples in the rueda and the number of commands to perform, respectively. It's guaranteed that n is even.
Next q lines contain the descriptions of the commands. Each command has type as the integer 1 or 2 first. Command of the first type is given as x ( - n β€ x β€ n), where 0 β€ x β€ n means all boys moves x girls in clockwise direction, while - x means all boys move x positions in counter-clockwise direction. There is no other input for commands of the second type.
Output
Output n integers, the i-th of them should be equal to the index of boy the i-th girl is dancing with after performing all q moves.
Examples
Input
6 3
1 2
2
1 2
Output
4 3 6 5 2 1
Input
2 3
1 1
2
1 -2
Output
1 2
Input
4 2
2
1 3
Output
1 4 3 2
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger.
But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?"
Petya knows only 5 integer types:
1) byte occupies 1 byte and allows you to store numbers from - 128 to 127
2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767
3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647
4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807
5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower.
For all the types given above the boundary values are included in the value range.
From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him.
Input
The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number n can't be represented as an empty string.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Output
Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above.
Examples
Input
127
Output
byte
Input
130
Output
short
Input
123456789101112131415161718192021222324
Output
BigInteger
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Heidi has finally found the mythical Tree of Life β a legendary combinatorial structure which is said to contain a prophecy crucially needed to defeat the undead armies.
On the surface, the Tree of Life is just a regular undirected tree well-known from computer science. This means that it is a collection of n points (called vertices), some of which are connected using n - 1 line segments (edges) so that each pair of vertices is connected by a path (a sequence of one or more edges).
To decipher the prophecy, Heidi needs to perform a number of steps. The first is counting the number of lifelines in the tree β these are paths of length 2, i.e., consisting of two edges. Help her!
Input
The first line of the input contains a single integer n β the number of vertices in the tree (1 β€ n β€ 10000). The vertices are labeled with the numbers from 1 to n. Then n - 1 lines follow, each describing one edge using two space-separated numbers a b β the labels of the vertices connected by the edge (1 β€ a < b β€ n). It is guaranteed that the input represents a tree.
Output
Print one integer β the number of lifelines in the tree.
Examples
Input
4
1 2
1 3
1 4
Output
3
Input
5
1 2
2 3
3 4
3 5
Output
4
Note
In the second sample, there are four lifelines: paths between vertices 1 and 3, 2 and 4, 2 and 5, and 4 and 5.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Innokenty is a president of a new football league in Byteland. The first task he should do is to assign short names to all clubs to be shown on TV next to the score. Of course, the short names should be distinct, and Innokenty wants that all short names consist of three letters.
Each club's full name consist of two words: the team's name and the hometown's name, for example, "DINAMO BYTECITY". Innokenty doesn't want to assign strange short names, so he wants to choose such short names for each club that:
1. the short name is the same as three first letters of the team's name, for example, for the mentioned club it is "DIN",
2. or, the first two letters of the short name should be the same as the first two letters of the team's name, while the third letter is the same as the first letter in the hometown's name. For the mentioned club it is "DIB".
Apart from this, there is a rule that if for some club x the second option of short name is chosen, then there should be no club, for which the first option is chosen which is the same as the first option for the club x. For example, if the above mentioned club has short name "DIB", then no club for which the first option is chosen can have short name equal to "DIN". However, it is possible that some club have short name "DIN", where "DI" are the first two letters of the team's name, and "N" is the first letter of hometown's name. Of course, no two teams can have the same short name.
Help Innokenty to choose a short name for each of the teams. If this is impossible, report that. If there are multiple answer, any of them will suit Innokenty. If for some team the two options of short name are equal, then Innokenty will formally think that only one of these options is chosen.
Input
The first line contains a single integer n (1 β€ n β€ 1000) β the number of clubs in the league.
Each of the next n lines contains two words β the team's name and the hometown's name for some club. Both team's name and hometown's name consist of uppercase English letters and have length at least 3 and at most 20.
Output
It it is not possible to choose short names and satisfy all constraints, print a single line "NO".
Otherwise, in the first line print "YES". Then print n lines, in each line print the chosen short name for the corresponding club. Print the clubs in the same order as they appeared in input.
If there are multiple answers, print any of them.
Examples
Input
2
DINAMO BYTECITY
FOOTBALL MOSCOW
Output
YES
DIN
FOO
Input
2
DINAMO BYTECITY
DINAMO BITECITY
Output
NO
Input
3
PLAYFOOTBALL MOSCOW
PLAYVOLLEYBALL SPB
GOGO TECHNOCUP
Output
YES
PLM
PLS
GOG
Input
3
ABC DEF
ABC EFG
ABD OOO
Output
YES
ABD
ABE
ABO
Note
In the first sample Innokenty can choose first option for both clubs.
In the second example it is not possible to choose short names, because it is not possible that one club has first option, and the other has second option if the first options are equal for both clubs.
In the third example Innokenty can choose the second options for the first two clubs, and the first option for the third club.
In the fourth example note that it is possible that the chosen short name for some club x is the same as the first option of another club y if the first options of x and y are different.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
A few years ago Sajjad left his school and register to another one due to security reasons. Now he wishes to find Amir, one of his schoolmates and good friends.
There are n schools numerated from 1 to n. One can travel between each pair of them, to do so, he needs to buy a ticket. The ticker between schools i and j costs <image> and can be used multiple times. Help Sajjad to find the minimum cost he needs to pay for tickets to visit all schools. He can start and finish in any school.
Input
The first line contains a single integer n (1 β€ n β€ 105) β the number of schools.
Output
Print single integer: the minimum cost of tickets needed to visit all schools.
Examples
Input
2
Output
0
Input
10
Output
4
Note
In the first example we can buy a ticket between the schools that costs <image>.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
You might have heard about the next game in Lara Croft series coming out this year. You also might have watched its trailer. Though you definitely missed the main idea about its plot, so let me lift the veil of secrecy.
Lara is going to explore yet another dangerous dungeon. Game designers decided to use good old 2D environment. The dungeon can be represented as a rectangle matrix of n rows and m columns. Cell (x, y) is the cell in the x-th row in the y-th column. Lara can move between the neighbouring by side cells in all four directions.
Moreover, she has even chosen the path for herself to avoid all the traps. She enters the dungeon in cell (1, 1), that is top left corner of the matrix. Then she goes down all the way to cell (n, 1) β the bottom left corner. Then she starts moving in the snake fashion β all the way to the right, one cell up, then to the left to the cell in 2-nd column, one cell up. She moves until she runs out of non-visited cells. n and m given are such that she always end up in cell (1, 2).
Lara has already moved to a neighbouring cell k times. Can you determine her current position?
Input
The only line contains three integers n, m and k (2 β€ n, m β€ 109, n is always even, 0 β€ k < nΒ·m). Note that k doesn't fit into 32-bit integer type!
Output
Print the cell (the row and the column where the cell is situated) where Lara ends up after she moves k times.
Examples
Input
4 3 0
Output
1 1
Input
4 3 11
Output
1 2
Input
4 3 7
Output
3 2
Note
Here is her path on matrix 4 by 3:
<image>
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
In the race for the best Internet browser, there's now a new contender for it, this browser is called the: "The Semantic Mind-Reader!" After its promo on the world wide web, everyone's been desperately waiting for the browser to be released. And why shouldn't they be curious about it, after all, it's the new project of our very own genius "Little Jhool!" He's worked very hard for this browser, and to add new mind reading features to it.
Apart from the various security powers it possesses, it's called the mind-reader for a reason. Here's why:
You don't need to type 'www.' to open a website anymore.
Though, you still need to type '.com' to open a website.
The browser predicts ALL THE VOWELS in the name of the website. (Not '.com', though. Again!)
Obviously, this means you can type the name of a website faster and save some time.
Now to convince users that his browser will indeed save A LOT of time for users to open a particular website, Little Jhool has asked you to prepare a report on the same.
Input format:
The first line contains tc, the number of test cases.
The second line contains the name of websites, as a string.
Output format:
You have to print the ratio of characters you would have typed in Jhool's browser, to your normal browser.
Constraints:
1 β€ tc β€ 100
1 β€ Length of the website β€ 200
NOTE: You do NOT need to print the output in its lowest format. You should print in its original fraction format.
The names of all the websites will be in small case only.
Every string will start from *www. and end with *.com, so well!**
SAMPLE INPUT
2
www.google.com
www.hackerearth.com
SAMPLE OUTPUT
7/14
11/19
Explanation
Consider the first case:
In Jhool's browser, you'll only have to type: ggl.com (7 characters) while in a normal browser, you'll have to type www.google.com, which is 14 characters.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
In the Kingdom of AtCoder, only banknotes are used as currency. There are 10^{100}+1 kinds of banknotes, with the values of 1, 10, 10^2, 10^3, \dots, 10^{(10^{100})}. You have come shopping at a mall and are now buying a takoyaki machine with a value of N. (Takoyaki is the name of a Japanese snack.)
To make the payment, you will choose some amount of money which is at least N and give it to the clerk. Then, the clerk gives you back the change, which is the amount of money you give minus N.
What will be the minimum possible number of total banknotes used by you and the clerk, when both choose the combination of banknotes to minimize this count?
Assume that you have sufficient numbers of banknotes, and so does the clerk.
Constraints
* N is an integer between 1 and 10^{1,000,000} (inclusive).
Input
Input is given from Standard Input in the following format:
N
Output
Print the minimum possible number of total banknotes used by you and the clerk.
Examples
Input
36
Output
8
Input
91
Output
3
Input
314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170
Output
243
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Takahashi will do a tap dance. The dance is described by a string S where each character is `L`, `R`, `U`, or `D`. These characters indicate the positions on which Takahashi should step. He will follow these instructions one by one in order, starting with the first character.
S is said to be easily playable if and only if it satisfies both of the following conditions:
* Every character in an odd position (1-st, 3-rd, 5-th, \ldots) is `R`, `U`, or `D`.
* Every character in an even position (2-nd, 4-th, 6-th, \ldots) is `L`, `U`, or `D`.
Your task is to print `Yes` if S is easily playable, and `No` otherwise.
Constraints
* S is a string of length between 1 and 100 (inclusive).
* Each character of S is `L`, `R`, `U`, or `D`.
Input
Input is given from Standard Input in the following format:
S
Output
Print `Yes` if S is easily playable, and `No` otherwise.
Examples
Input
RUDLUDR
Output
Yes
Input
DULL
Output
No
Input
UUUUUUUUUUUUUUU
Output
Yes
Input
ULURU
Output
No
Input
RDULULDURURLRDULRLR
Output
Yes
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are N cards placed face down in a row. On each card, an integer 1 or 2 is written.
Let A_i be the integer written on the i-th card.
Your objective is to guess A_1, A_2, ..., A_N correctly.
You know the following facts:
* For each i = 1, 2, ..., M, the value A_{X_i} + A_{Y_i} + Z_i is an even number.
You are a magician and can use the following magic any number of times:
Magic: Choose one card and know the integer A_i written on it. The cost of using this magic is 1.
What is the minimum cost required to determine all of A_1, A_2, ..., A_N?
It is guaranteed that there is no contradiction in given input.
Constraints
* All values in input are integers.
* 2 \leq N \leq 10^5
* 1 \leq M \leq 10^5
* 1 \leq X_i < Y_i \leq N
* 1 \leq Z_i \leq 100
* The pairs (X_i, Y_i) are distinct.
* There is no contradiction in input. (That is, there exist integers A_1, A_2, ..., A_N that satisfy the conditions.)
Input
Input is given from Standard Input in the following format:
N M
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\vdots
X_M Y_M Z_M
Output
Print the minimum total cost required to determine all of A_1, A_2, ..., A_N.
Examples
Input
3 1
1 2 1
Output
2
Input
6 5
1 2 1
2 3 2
1 3 3
4 5 4
5 6 5
Output
2
Input
100000 1
1 100000 100
Output
99999
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Takahashi Lake has a perimeter of L. On the circumference of the lake, there is a residence of the lake's owner, Takahashi. Each point on the circumference of the lake has a coordinate between 0 and L (including 0 but not L), which is the distance from the Takahashi's residence, measured counter-clockwise.
There are N trees around the lake; the coordinate of the i-th tree is X_i. There is no tree at coordinate 0, the location of Takahashi's residence.
Starting at his residence, Takahashi will repeat the following action:
* If all trees are burnt, terminate the process.
* Specify a direction: clockwise or counter-clockwise.
* Walk around the lake in the specified direction, until the coordinate of a tree that is not yet burnt is reached for the first time.
* When the coordinate with the tree is reached, burn that tree, stay at the position and go back to the first step.
Find the longest possible total distance Takahashi walks during the process.
Constraints
* 2 \leq L \leq 10^9
* 1 \leq N \leq 2\times 10^5
* 1 \leq X_1 < ... < X_N \leq L-1
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
L N
X_1
:
X_N
Output
Print the longest possible total distance Takahashi walks during the process.
Examples
Input
10 3
2
7
9
Output
15
Input
10 6
1
2
3
6
7
9
Output
27
Input
314159265 7
21662711
77271666
89022761
156626166
160332356
166902656
298992265
Output
1204124749
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Takahashi is doing a research on sets of points in a plane. Takahashi thinks a set S of points in a coordinate plane is a good set when S satisfies both of the following conditions:
* The distance between any two points in S is not \sqrt{D_1}.
* The distance between any two points in S is not \sqrt{D_2}.
Here, D_1 and D_2 are positive integer constants that Takahashi specified.
Let X be a set of points (i,j) on a coordinate plane where i and j are integers and satisfy 0 β€ i,j < 2N.
Takahashi has proved that, for any choice of D_1 and D_2, there exists a way to choose N^2 points from X so that the chosen points form a good set. However, he does not know the specific way to choose such points to form a good set. Find a subset of X whose size is N^2 that forms a good set.
Constraints
* 1 β€ N β€ 300
* 1 β€ D_1 β€ 2Γ10^5
* 1 β€ D_2 β€ 2Γ10^5
* All values in the input are integers.
Input
Input is given from Standard Input in the following format:
N D_1 D_2
Output
Print N^2 distinct points that satisfy the condition in the following format:
x_1 y_1
x_2 y_2
:
x_{N^2} y_{N^2}
Here, (x_i,y_i) represents the i-th chosen point. 0 β€ x_i,y_i < 2N must hold, and they must be integers. The chosen points may be printed in any order. In case there are multiple possible solutions, you can output any.
Examples
Input
2 1 2
Output
0 0
0 2
2 0
2 2
Input
3 1 5
Output
0 0
0 2
0 4
1 1
1 3
1 5
2 0
2 2
2 4
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are N positive integers written on a blackboard: A_1, ..., A_N.
Snuke can perform the following operation when all integers on the blackboard are even:
* Replace each integer X on the blackboard by X divided by 2.
Find the maximum possible number of operations that Snuke can perform.
Constraints
* 1 \leq N \leq 200
* 1 \leq A_i \leq 10^9
Input
Input is given from Standard Input in the following format:
N
A_1 A_2 ... A_N
Output
Print the maximum possible number of operations that Snuke can perform.
Examples
Input
3
8 12 40
Output
2
Input
4
5 6 8 10
Output
0
Input
6
382253568 723152896 37802240 379425024 404894720 471526144
Output
8
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
There are N integers written on a blackboard. The i-th integer is A_i.
Takahashi and Aoki will arrange these integers in a row, as follows:
* First, Takahashi will arrange the integers as he wishes.
* Then, Aoki will repeatedly swap two adjacent integers that are coprime, as many times as he wishes.
We will assume that Takahashi acts optimally so that the eventual sequence will be lexicographically as small as possible, and we will also assume that Aoki acts optimally so that the eventual sequence will be lexicographically as large as possible. Find the eventual sequence that will be produced.
Constraints
* 1 β¦ N β¦ 2000
* 1 β¦ A_i β¦ 10^8
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 β¦ A_N
Output
Print the eventual sequence that will be produced, in a line.
Examples
Input
5
1 2 3 4 5
Output
5 3 2 4 1
Input
4
2 3 4 6
Output
2 4 6 3
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Solve the programming task below in a Python markdown code block.
Hit n nails one by one at the coordinates P1 (x1, y1), P2 (x2, y2), P3 (x3, y3), ..., Pn (xn, yn) on the flat plate, and put them on the rubber band ring. Surround it with a single rubber band so that all the nails fit inside. At this time, the rubber bands must not intersect.
Create a program that reads the coordinates of the nails and outputs the number of nails that are not in contact with the rubber band when the nail is surrounded by the rubber band as described above. The rubber band shall expand and contract sufficiently. You may not hit more than one nail at the same coordinates. In addition, it is assumed that the nails covered with rubber bands are connected by a straight line, and that no more than three nails are lined up on the straight line. For example, there can be no input as shown in Figure 1. As shown in Figure 2, it is possible for nails without rubber bands to line up in a straight line.
<image> | <image>
--- | ---
Figure 1 | Figure 2
However, each coordinate value is a real number between -1000.0 and 1000.0. Also, n is an integer between 3 and 100.
Hint
Below is a diagram for the second sample input.
<image>
---
Input
Given multiple datasets. Each dataset is given in the following format:
n
x1, y1
x2, y2
...
...
xn, yn
When n is 0, it indicates the end of input. The number of datasets does not exceed 50.
Output
For each dataset, output the number of nails that are not in contact with the rubber. For example, if there is an input representing the four nails shown in Fig. 3, it will be surrounded as shown in Fig. 4, so the number of nails that are not in contact with the rubber band is one.
<image> | <image>
--- | ---
Figure 3 | Figure 4
Example
Input
4
1.0,0.0
0.0,1.0
2.0,1.0
1.0,2.0
9
-509.94,892.63
567.62,639.99
-859.32,-64.84
-445.99,383.69
667.54,430.49
551.12,828.21
-940.2,-877.2
-361.62,-970
-125.42,-178.48
0
Output
0
3
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.