question
large_stringlengths 265
13.2k
|
|---|
Solve the programming task below in a Python markdown code block.
Students love to celebrate their holidays. Especially if the holiday is the day of the end of exams!
Despite the fact that Igor K., unlike his groupmates, failed to pass a programming test, he decided to invite them to go to a cafe so that each of them could drink a bottle of... fresh cow milk. Having entered the cafe, the m friends found n different kinds of milk on the menu, that's why they ordered n bottles β one bottle of each kind. We know that the volume of milk in each bottle equals w.
When the bottles were brought in, they decided to pour all the milk evenly among the m cups, so that each got a cup. As a punishment for not passing the test Igor was appointed the person to pour the milk. He protested that he was afraid to mix something up and suggested to distribute the drink so that the milk from each bottle was in no more than two different cups. His friends agreed but they suddenly faced the following problem β and what is actually the way to do it?
Help them and write the program that will help to distribute the milk among the cups and drink it as quickly as possible!
Note that due to Igor K.'s perfectly accurate eye and unswerving hands, he can pour any fractional amount of milk from any bottle to any cup.
Input
The only input data file contains three integers n, w and m (1 β€ n β€ 50, 100 β€ w β€ 1000, 2 β€ m β€ 50), where n stands for the number of ordered bottles, w stands for the volume of each of them and m stands for the number of friends in the company.
Output
Print on the first line "YES" if it is possible to pour the milk so that the milk from each bottle was in no more than two different cups. If there's no solution, print "NO".
If there is a solution, then print m more lines, where the i-th of them describes the content of the i-th student's cup. The line should consist of one or more pairs that would look like "b v". Each such pair means that v (v > 0) units of milk were poured into the i-th cup from bottle b (1 β€ b β€ n). All numbers b on each line should be different.
If there are several variants to solve the problem, print any of them. Print the real numbers with no less than 6 digits after the decimal point.
Examples
Input
2 500 3
Output
YES
1 333.333333
2 333.333333
2 166.666667 1 166.666667
Input
4 100 5
Output
YES
3 20.000000 4 60.000000
1 80.000000
4 40.000000 2 40.000000
3 80.000000
2 60.000000 1 20.000000
Input
4 100 7
Output
NO
Input
5 500 2
Output
YES
4 250.000000 5 500.000000 2 500.000000
3 500.000000 1 500.000000 4 250.000000
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 Monty is very fond of Fibonacci numbers and challenges his friend Lopa
that he can solve any question regarding the same. Lopa however is jealous
and after thinking for a long time came up with a problem.
She says given
N(the number of fibonacci numbers) one has to count all the multiples of all
the fibonacci numbers. This statement can be quite confusing hence look at the
mathematical definition and test cases for better understanding.
Let M(x) denote the number of multiple of x in the sequence and F(y) denote
the yth fibonacci number. The required answer is:
Take the first two number of the sequence to be 1 and 1.
Input Format
The first line contains T i.e. the number of test cases.
T lines follow, each line containing an integer, N.
(The sample fibonacci series for n=5 is 1, 1, 2, 3, 5)
Output Format
For each testcase, print the output that Lopa wants in one line.
Constraints
1 β€ T β€ 1000
1 β€ N β€ 1000
SAMPLE INPUT
3
1
2
3
SAMPLE OUTPUT
1
4
7
Explanation
For N=1. The fibonacci number is 1. The multiple of 1 is 1(itself). Hence the answer is 1.
For N=2. The fibonacci number is 1, 1. The multiple of 1(first fibonacci number) is 1(first fibonacci number) and 1(second fibonacci number). The multiple of 1(second fibonacci number) is 1(first fibonacci number) and 1(second fibonacci number). Hence the answer is 4.
For n=3. The fibonacci sequence is 1, 1, 2 . The multiples of 1(first fibonacci number) is 1(itself) and 1(second fibonacci number) and 2. The multiples of 1(second fibonacci number) is 1(first fibonacci number), 1(itself) and 2. The multiple of 2 is 2(itself). Hence the answer is 7.
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 has a string S of length N consisting of digits from `0` through `9`.
He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten.
Here substrings starting with a `0` also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers.
Compute this count to help Takahashi.
Constraints
* 1 \leq N \leq 2 \times 10^5
* S consists of digits.
* |S| = N
* 2 \leq P \leq 10000
* P is a prime number.
Input
Input is given from Standard Input in the following format:
N P
S
Output
Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten.
Examples
Input
4 3
3543
Output
6
Input
4 2
2020
Output
10
Input
20 11
33883322005544116655
Output
68
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.
Consider the following arithmetic progression with n terms:
* x, x + d, x + 2d, \ldots, x + (n-1)d
What is the product of all terms in this sequence? Compute the answer modulo 1\ 000\ 003.
You are given Q queries of this form. In the i-th query, compute the answer in case x = x_i, d = d_i, n = n_i.
Constraints
* 1 \leq Q \leq 10^5
* 0 \leq x_i, d_i \leq 1\ 000\ 002
* 1 \leq n_i \leq 10^9
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
Q
x_1 d_1 n_1
:
x_Q d_Q n_Q
Output
Print Q lines.
In the i-th line, print the answer for the i-th query.
Example
Input
2
7 2 4
12345 67890 2019
Output
9009
916936
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.
Let N be a positive odd number.
There are N coins, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), when Coin i is tossed, it comes up heads with probability p_i and tails with probability 1 - p_i.
Taro has tossed all the N coins. Find the probability of having more heads than tails.
Constraints
* N is an odd number.
* 1 \leq N \leq 2999
* p_i is a real number and has two decimal places.
* 0 < p_i < 1
Input
Input is given from Standard Input in the following format:
N
p_1 p_2 \ldots p_N
Output
Print the probability of having more heads than tails. The output is considered correct when the absolute error is not greater than 10^{-9}.
Examples
Input
3
0.30 0.60 0.80
Output
0.612
Input
1
0.50
Output
0.5
Input
5
0.42 0.01 0.42 0.99 0.42
Output
0.3821815872
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.
An X-layered kagami mochi (X β₯ 1) is a pile of X round mochi (rice cake) stacked vertically where each mochi (except the bottom one) has a smaller diameter than that of the mochi directly below it. For example, if you stack three mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, you have a 3-layered kagami mochi; if you put just one mochi, you have a 1-layered kagami mochi.
Lunlun the dachshund has N round mochi, and the diameter of the i-th mochi is d_i centimeters. When we make a kagami mochi using some or all of them, at most how many layers can our kagami mochi have?
Constraints
* 1 β€ N β€ 100
* 1 β€ d_i β€ 100
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N
d_1
:
d_N
Output
Print the maximum number of layers in a kagami mochi that can be made.
Examples
Input
4
10
8
8
6
Output
3
Input
3
15
15
15
Output
1
Input
7
50
30
50
100
50
80
30
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.
Alice and Bob are controlling a robot. They each have one switch that controls the robot.
Alice started holding down her button A second after the start-up of the robot, and released her button B second after the start-up.
Bob started holding down his button C second after the start-up, and released his button D second after the start-up.
For how many seconds both Alice and Bob were holding down their buttons?
Constraints
* 0β€A<Bβ€100
* 0β€C<Dβ€100
* All input values are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
Print the length of the duration (in seconds) in which both Alice and Bob were holding down their buttons.
Examples
Input
0 75 25 100
Output
50
Input
0 33 66 99
Output
0
Input
10 90 20 80
Output
60
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 is a railroad in Takahashi Kingdom. The railroad consists of N sections, numbered 1, 2, ..., N, and N+1 stations, numbered 0, 1, ..., N. Section i directly connects the stations i-1 and i. A train takes exactly A_i minutes to run through section i, regardless of direction. Each of the N sections is either single-tracked over the whole length, or double-tracked over the whole length. If B_i = 1, section i is single-tracked; if B_i = 2, section i is double-tracked. Two trains running in opposite directions can cross each other on a double-tracked section, but not on a single-tracked section. Trains can also cross each other at a station.
Snuke is creating the timetable for this railroad. In this timetable, the trains on the railroad run every K minutes, as shown in the following figure. Here, bold lines represent the positions of trains running on the railroad. (See Sample 1 for clarification.)
<image>
When creating such a timetable, find the minimum sum of the amount of time required for a train to depart station 0 and reach station N, and the amount of time required for a train to depart station N and reach station 0. It can be proved that, if there exists a timetable satisfying the conditions in this problem, this minimum sum is always an integer.
Formally, the times at which trains arrive and depart must satisfy the following:
* Each train either departs station 0 and is bound for station N, or departs station N and is bound for station 0.
* Each train takes exactly A_i minutes to run through section i. For example, if a train bound for station N departs station i-1 at time t, the train arrives at station i exactly at time t+A_i.
* Assume that a train bound for station N arrives at a station at time s, and departs the station at time t. Then, the next train bound for station N arrives at the station at time s+K, and departs the station at time t+K. Additionally, the previous train bound for station N arrives at the station at time s-K, and departs the station at time t-K. This must also be true for trains bound for station 0.
* Trains running in opposite directions must not be running on the same single-tracked section (except the stations at both ends) at the same time.
Constraints
* 1 \leq N \leq 100000
* 1 \leq K \leq 10^9
* 1 \leq A_i \leq 10^9
* A_i is an integer.
* B_i is either 1 or 2.
Input
The input is given from Standard Input in the following format:
N K
A_1 B_1
A_2 B_2
:
A_N B_N
Output
Print an integer representing the minimum sum of the amount of time required for a train to depart station 0 and reach station N, and the amount of time required for a train to depart station N and reach station 0. If it is impossible to create a timetable satisfying the conditions, print -1 instead.
Examples
Input
3 10
4 1
3 1
4 1
Output
26
Input
1 10
10 1
Output
-1
Input
6 4
1 1
1 1
1 1
1 1
1 1
1 1
Output
12
Input
20 987654321
129662684 2
162021979 1
458437539 1
319670097 2
202863355 1
112218745 1
348732033 1
323036578 1
382398703 1
55854389 1
283445191 1
151300613 1
693338042 2
191178308 2
386707193 1
204580036 1
335134457 1
122253639 1
824646518 2
902554792 2
Output
14829091348
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.
Fukushima Prefecture is also famous for producing fruits, and among them, peaches and apples boast one of the highest production volumes in Japan. By the way, when I made a print manuscript of an English pamphlet for sale, I mistakenly wrote the description about apples and the description about peaches in reverse.
You've been tasked with fixing apple and peach, but it's kind of annoying. Enter a single line of English text and create a program that outputs the English text with all the character strings apple in it replaced with peach and all the character strings peach replaced with apple.
Input
English text (including half-width alphanumeric characters, spaces, and symbols) is given on one line. The length of the string entered is 1000 or less.
Output
Outputs English sentences with the character strings apple and peach exchanged on one line.
Example
Input
the cost of one peach is higher than that of one apple.
Output
the cost of one apple is higher than that of one peach.
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.
Water Country Water Deven has n cities. Each city is surrounded by water and looks like an island country. Water Deven has m bridges, and transportation between cities is carried out by these bridges, which allows you to travel to and from all cities.
Recently, it was decided to reduce the maintenance cost of the bridge by reviewing the road specific financial resources. I couldn't maintain all the bridges and had to demolish some of them. Therefore, the challenge for Water Deven was to minimize the cost of maintaining the bridge while leaving the bridge so that it could reach any city.
Create a program that inputs the number of cities, the number of bridges, and the maintenance cost of each bridge, so that you can go to any city using the bridge, and outputs the minimum value of the maintenance cost when the bridge is demolished. Please give me. There is no cost to demolish the bridge. However, each city shall be numbered sequentially from 0 to n-1.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
a1 b1 cost1
a2 b2 cost2
::
am bm costm
The first line gives the number of cities n (2 β€ n β€ 100) and the number of bridges m (1 β€ m β€ 500). The following m lines give information about the i-th bridge. ai and bi are the numbers of the cities to which the bridge is connected, and costi (1 β€ costi β€ 1000) is the maintenance cost of the bridge.
Output
The total bridge maintenance cost is output to one line for each data set.
Example
Input
5 6
0 2 1
2 1 3
2 3 8
1 3 2
3 4 5
1 4 4
3 3
1 2 3
2 0 3
0 1 3
0 0
Output
10
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.
We, the researchers who discovered and investigated the ancient nation Iwashiro, finally discovered the temple in the center of Iwashiro. A lithograph dedicated to the god of Iwashiro was stored in the temple. On the lithograph, two strings were written, one for each sentence and one for the spell.
In Iwashiro, how many times a spell appears in a sentence has an important meaning. However, it is considered that all the characters contained in the spell appear in order, and some of them appear in the sentence in a discrete manner once. For example, if the sentence is "abab" and the spell is "ab", then "ab" appears three times in "abab", including non-continuous ones (three ways: abab, abab, and abab).
Create a program that prints how many times a spell appears in a sentence when it is given a sentence and a spell.
Input
The input is given in the following format.
t
b b
The first line is given the string t that represents the text written on the lithograph. The second line is given the string b that represents the spell written on the lithograph. Both strings are composed of only lowercase letters and have a length of 1 or more and 1000 or less.
Output
Prints how many times a spell appears in a sentence on one line. However, the value to be output can be very large, so instead output the remainder divided by 1,000,000,007.
Examples
Input
abab
ab
Output
3
Input
aaaabaaaabaaaabaaaab
aaaaa
Output
4368
Input
data
structure
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.
Take the'IOI'train
A new railway has been laid in IOI. Trains running on railways in IOI are a combination of several vehicles, and there are two types of vehicles, I and O. Vehicles can only be connected to different types of vehicles. Also, because the train has a driver's seat, the cars at both ends of the train must be of type I. A train is represented by a character string in which characters indicating the type of vehicle are connected in order, and the length of the train is assumed to be the length of the character string. For example, if vehicles are connected in the order of IOIOI, a train with a length of 5 can be formed, and vehicle I is a train with a length of 1 alone. Trains cannot be formed by arranging vehicles in the order of OIOI or IOOI.
Some vehicles are stored in two garages. Vehicles are lined up in a row in each garage. When forming a train, take out the train from the garage and connect it in front of the garage. Only the vehicle closest to the entrance of the garage can be taken out of the garage, but the order of taking out the vehicle from which garage is arbitrary.
Before forming a train, you can take as many cars out of the garage as you like and move them to another waiting rail. Vehicles that have been moved to the standby rails cannot be used to organize trains in the future. Also, once the train formation is started, the vehicle cannot be moved from the garage to the standby rail until the formation is completed.
When organizing a train, it is not necessary to use up all the cars in the garage. That is, after the train formation is completed, unused cars may remain in the garage.
It is believed that there are so many people on the railroad in IOI, so I want to organize the longest possible train.
<image>
Figure: The train is being organized, and the vehicle in the garage cannot be moved to the standby rail at this time. This figure corresponds to I / O example 1.
Task
Create a program to find the maximum length of trains that can be organized given the information of the cars stored in the garage. The row of vehicles stored in each garage is represented by a string consisting of only two types of letters I and O, and the information in the two garages is represented by the string S of length M and the string T of length N, respectively. Given. Each letter represents one vehicle, the letter of which is the same as the type of vehicle. The first letter of the string represents the vehicle closest to the entrance to the garage, and the last letter represents the vehicle closest to the garage.
Limits
* 1 β€ M β€ 2000 Length of string S
* 1 β€ N β€ 2000 Length of string T
input
Read the following data from standard input.
* In the first line, M and N are written separated by blanks.
* The character string S is written on the second line.
* The character string T is written on the third line.
output
Output an integer representing the maximum train length that can be organized to the standard output on one line. If no train can be organized, output 0.
Input / output example
Input example 1
5 5
OIOOI
OOIOI
Output example 1
7
Let the garage represented by S be the garage S and the garage represented by T be the garage T. At this time, for example, the first vehicle from the garage S, the first two vehicles from the garage T are put out and put on standby, and then the garage S, the garage S, the garage T, the garage S, the garage S, the garage T, and the garage T are placed in this order. If you take out the vehicle, you can form a train IOIOIOI with a length of 7.
In addition, after the first vehicle from garage S and the first two vehicles from garage T are put out and put on standby, the order is garage T, garage T, garage S, garage S, garage T, garage S, and garage S. You can also organize a 7-length train by putting out a vehicle. Since it is not possible to form a train longer than this, 7 is output.
Input example 2
5 9
IIIII
IIIIIIIII
Output example 2
1
Note that train I, which consists of only one car, also meets the conditions for a train.
The question text and the data used for the automatic referee are the question text and the test data for scoring, which are created and published by the Japan Committee for Information Olympics.
Example
Input
5 5
OIOOI
OOIOI
Output
7
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.
Professor Tsukuba invented a mysterious jewelry box that can be opened with a special gold key whose shape is very strange. It is composed of gold bars joined at their ends. Each gold bar has the same length and is placed parallel to one of the three orthogonal axes in a three dimensional space, i.e., x-axis, y-axis and z-axis.
The locking mechanism of the jewelry box is truly mysterious, but the shape of the key is known. To identify the key of the jewelry box, he gave a way to describe its shape.
The description indicates a list of connected paths that completely defines the shape of the key: the gold bars of the key are arranged along the paths and joined at their ends. Except for the first path, each path must start from an end point of a gold bar on a previously defined path. Each path is represented by a sequence of elements, each of which is one of six symbols (+x, -x, +y, -y, +z and -z) or a positive integer. Each symbol indicates the direction from an end point to the other end point of a gold bar along the path. Since each gold bar is parallel to one of the three orthogonal axes, the 6 symbols are enough to indicate the direction. Note that a description of a path has direction but the gold bars themselves have no direction.
An end point of a gold bar can have a label, which is a positive integer. The labeled point may be referred to as the beginnings of other paths. In a key description, the first occurrence of a positive integer defines a label of a point and each subsequent occurrence of the same positive integer indicates the beginning of a new path at the point.
An example of a key composed of 13 gold bars is depicted in Figure 1.
<image>
The following sequence of lines
19
1 +x 1 +y +z 3 +z
3 +y -z +x +y -z -x +z 2 +z
2 +y
is a description of the key in Figure 1. Note that newlines have the same role as space characters in the description, so that `"19 1 +x 1 +y +z 3 +z 3 +y -z +x +y -z -x +z 2 +z 2 +y"` has the same meaning.
The meaning of this description is quite simple. The first integer "19" means the number of the following elements in this description. Each element is one of the 6 symbols or a positive integer.
The integer "1" at the head of the second line is a label attached to the starting point of the first path. Without loss of generality, it can be assumed that the starting point of the first path is the origin, i.e., (0,0,0), and that the length of each gold bar is 1. The next element "+x" indicates that the first gold bar is parallel to the x-axis, so that the other end point of the gold bar is at (1,0,0). These two elements "1" and "+x" indicates the first path consisting of only one gold bar. The third element of the second line in the description is the positive integer "1", meaning that the point with the label "1", i.e., the origin (0,0,0) is the beginning of a new path. The following elements "+y", "+z", "3", and "+z" indicate the second path consisting of three gold bars. Note that this "3" is its first occurrence so that the point with coordinates (0,1,1) is labeled "3". The head of the third line "3" indicates the beginning of the third path and so on. Consequently, there are four paths by which the shape of the key in Figure 1 is completely defined.
Note that there are various descriptions of the same key since there are various sets of paths that cover the shape of the key. For example, the following sequence of lines
19
1 +x 1 +y +z 3 +y -z +x +y -z -x +z 2 +y
3 +z
2 +z
is another description of the key in Figure 1, since the gold bars are placed in the same way.
Furthermore, the key may be turned 90-degrees around x-axis, y-axis or z-axis several times and may be moved parallelly. Since any combinations of rotations and parallel moves don't change the shape of the key, a description of a rotated and moved key also represent the same shape of the original key. For example, a sequence
17
+y 1 +y -z +x
1 +z +y +x +z +y -x -y 2 -y
2 +z
is a description of a key in Figure 2 that represents the same key as in Figure 1. Indeed, they are congruent under a rotation around x-axis and a parallel move.
<image>
Your job is to write a program to judge whether or not the given two descriptions define the same key.
Note that paths may make a cycle. For example, `"4 +x +y -x -y"` and `"6 1 +x 1 +y +x -y"` are valid descriptions. However, two or more gold bars must not be placed at the same position. For example, key descriptions `"2 +x -x"` and `"7 1 +x 1 +y +x -y -x"` are invalid.
Input
An input data is a list of pairs of key descriptions followed by a zero that indicates the end of the input. For p pairs of key descriptions, the input is given in the following format.
key-description1-a
key-description1-b
key-description2-a
key-description2-b
...
key-descriptionp-a
key-descriptionp-b
0
Each key description (key-description) has the following format.
n` ` e1 ` ` e2 ` ` ... ` ` ek ` ` ... ` ` en
The positive integer n indicates the number of the following elements e1, ..., en . They are separated by one or more space characters and/or newlines. Each element ek is one of the six symbols (`+x`, `-x`, `+y`, `-y`, `+z` and `-z`) or a positive integer.
You can assume that each label is a positive integer that is less than 51, the number of elements in a single key description is less than 301, and the number of characters in a line is less than 80. You can also assume that the given key descriptions are valid and contain at least one gold bar.
Output
The number of output lines should be equal to that of pairs of key descriptions given in the input. In each line, you should output one of two words "SAME", when the two key descriptions represent the same key, and "DIFFERENT", when they are different. Note that the letters should be in upper case.
Examples
Input
19
1 +x 1 +y +z 3 +z
3 +y -z +x +y -z -x +z 2 +z
2 +y
19
1 +x 1 +y +z 3 +y -z +x +y -z -x +z 2 +y
3 +z
2 +z
19
1 +x 1 +y +z 3 +z
3 +y -z +x +y -z -x +z 2 +y
2 +z
18
1 -y
1 +y -z +x
1 +z +y +x +z +y -x -y 2 -y
2 +z
3 +x +y +z
3 +y +z -x
0
Output
SAME
SAME
DIFFERENT
Input
19
1 +x 1 +y +z 3 +z
3 +y -z +x +y -z -x +z 2 +z
2 +y
19
1 +x 1 +y +z 3 +y -z +x +y -z -x +z 2 +y
3 +z
2 +z
19
1 +x 1 +y +z 3 +z
3 +y -z +x +y -z -x +z 2 +y
2 +z
18
1 -y
1 +y -z +x
1 +z +y +x +z +y -x -y 2 -y
2 +z
3 +x +y +z
3 +y +z -x
0
Output
SAME
SAME
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 gene is a string consisting of `A`,` T`, `G`,` C`. The genes in this world are strangely known to obey certain syntactic rules.
Syntax rules are given in the following form:
Non-terminal symbol 1: Symbol 1_1 Symbol 1_2 ... Symbol 1_n1
Non-terminal symbol 2: Symbol 2_1 Symbol 2_2 ... Symbol 2_n2
...
Non-terminal symbol m: symbol m_1 symbol m_2 ... symbol m_nm
The symbol is either a nonterminal symbol or a terminal symbol. Non-terminal symbols are represented by lowercase strings, and terminal symbols are some of the characters `A`,` T`, `G`,` C` surrounded by "` [` "and" `]` ". It is represented by a character string.
An example syntax rule looks like this:
dna: a a b b
a: [AT]
b: [GC]
"` Nonterminal symbol i: Symbol i_1 Symbol i_2 ... Symbol i_ni` "is called a rule for nonterminal symbol i, and there is exactly one rule for each nonterminal symbol that appears in the syntax rule.
A string s "` matches `" with the nonterminal i means that there is a substring {sj} of s such that s = s1 + s2 + ... + sni, and sj (1 β€ j β€ It means that ni) matches the symbol j in the rule.
When the string s "` matches `" with a terminal symbol, it means that the string consists of one character and that character is included in the string representing the terminal symbol.
A string that follows syntax rules means that it matches nonterminal symbol 1.
Rule i does not include the nonterminal symbol j (j β€ i) in the symbol.
Given the syntax rules and the four integers Na, Nt, Ng, Nc. According to the syntax rules, find the remainder of the total number of genes that contain exactly Na for A, just Nt for T, just Ng for G, and just Nc for C, divided by 1,000,000,007.
Input
> Na Nt Ng Nc
> m
> Nonterminal 1: Symbol 11 Symbol 12 ... Symbol 1n1
> Non-terminal symbol 2: Symbol 21 Symbol 22 ... Symbol 2n2
> ...
> Non-terminal symbol m: symbol m1 symbol m2 ... symbol mnm
>
0 β€ Na, Nt, Ng, Nc β€ 50
1 β€ m β€ 50
1 β€ ni β€ 10
1 β€ Length of the character string representing the symbol β€ 20 (* Note that it is not the length of the character string that matches the symbol)
Output
The remainder of the total number divided by 1,000,000,007
Examples
Input
1 0 1 0
3
dna: a b
a: [AT]
b: [GC]
Output
1
Input
1 1 1 2
1
k: [ATG] [ATG] [ATG] [C] [C]
Output
6
Input
3 1 1 1
3
inv: at b b b
at: [ATG] b
b: [C]
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.
For a given sequence $A = \\{a_0, a_1, ... a_{n-1}\\}$, the number of pairs $(i, j)$ where $a_i > a_j$ and $i < j$, is called the number of inversions. The number of inversions is equal to the number of swaps of Bubble Sort defined in the following program:
bubbleSort(A)
cnt = 0 // the number of inversions
for i = 0 to A.length-1
for j = A.length-1 downto i+1
if A[j] < A[j-1]
swap(A[j], A[j-1])
cnt++
return cnt
For the given sequence $A$, print the number of inversions of $A$. Note that you should not use the above program, which brings Time Limit Exceeded.
Constraints
* $ 1 \leq n \leq 200,000$
* $ 0 \leq a_i \leq 10^9$
* $a_i$ are all different
Input
In the first line, an integer $n$, the number of elements in $A$, is given. In the second line, the elements $a_i$ ($i = 0, 1, .. n-1$) are given separated by space characters.
Examples
Input
5
3 5 2 1 4
Output
6
Input
3
3 1 2
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 a chessboard of size n Γ n. It is filled with numbers from 1 to n^2 in the following way: the first β (n^2)/(2) β numbers from 1 to β (n^2)/(2) β are written in the cells with even sum of coordinates from left to right from top to bottom. The rest n^2 - β (n^2)/(2) β numbers from β (n^2)/(2) β + 1 to n^2 are written in the cells with odd sum of coordinates from left to right from top to bottom. The operation βx/yβ means division x by y rounded up.
For example, the left board on the following picture is the chessboard which is given for n=4 and the right board is the chessboard which is given for n=5.
<image>
You are given q queries. The i-th query is described as a pair x_i, y_i. The answer to the i-th query is the number written in the cell x_i, y_i (x_i is the row, y_i is the column). Rows and columns are numbered from 1 to n.
Input
The first line contains two integers n and q (1 β€ n β€ 10^9, 1 β€ q β€ 10^5) β the size of the board and the number of queries.
The next q lines contain two integers each. The i-th line contains two integers x_i, y_i (1 β€ x_i, y_i β€ n) β description of the i-th query.
Output
For each query from 1 to q print the answer to this query. The answer to the i-th query is the number written in the cell x_i, y_i (x_i is the row, y_i is the column). Rows and columns are numbered from 1 to n. Queries are numbered from 1 to q in order of the input.
Examples
Input
4 5
1 1
4 4
4 3
3 2
2 4
Output
1
8
16
13
4
Input
5 4
2 1
4 2
3 3
3 4
Output
16
9
7
20
Note
Answers to the queries from examples are on the board in the picture from the problem statement.
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.
Let n be an integer. Consider all permutations on integers 1 to n in lexicographic order, and concatenate them into one big sequence p. For example, if n = 3, then p = [1, 2, 3, 1, 3, 2, 2, 1, 3, 2, 3, 1, 3, 1, 2, 3, 2, 1]. The length of this sequence will be n β
n!.
Let 1 β€ i β€ j β€ n β
n! be a pair of indices. We call the sequence (p_i, p_{i+1}, ..., p_{j-1}, p_j) a subarray of p. Its length is defined as the number of its elements, i.e., j - i + 1. Its sum is the sum of all its elements, i.e., β_{k=i}^j p_k.
You are given n. Find the number of subarrays of p of length n having sum (n(n+1))/(2). Since this number may be large, output it modulo 998244353 (a prime number).
Input
The only line contains one integer n (1 β€ n β€ 10^6), as described in the problem statement.
Output
Output a single integer β the number of subarrays of length n having sum (n(n+1))/(2), modulo 998244353.
Examples
Input
3
Output
9
Input
4
Output
56
Input
10
Output
30052700
Note
In the first sample, there are 16 subarrays of length 3. In order of appearance, they are:
[1, 2, 3], [2, 3, 1], [3, 1, 3], [1, 3, 2], [3, 2, 2], [2, 2, 1], [2, 1, 3], [1, 3, 2], [3, 2, 3], [2, 3, 1], [3, 1, 3], [1, 3, 1], [3, 1, 2], [1, 2, 3], [2, 3, 2], [3, 2, 1].
Their sums are 6, 6, 7, 6, 7, 5, 6, 6, 8, 6, 7, 5, 6, 6, 7, 6. As (n(n+1))/(2) = 6, the answer is 9.
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 a long stick, consisting of m segments enumerated from 1 to m. Each segment is 1 centimeter long. Sadly, some segments are broken and need to be repaired.
You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length t placed at some position s will cover segments s, s+1, β¦, s+t-1.
You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap.
Time is money, so you want to cut at most k continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces?
Input
The first line contains three integers n, m and k (1 β€ n β€ 10^5, n β€ m β€ 10^9, 1 β€ k β€ n) β the number of broken segments, the length of the stick and the maximum number of pieces you can use.
The second line contains n integers b_1, b_2, β¦, b_n (1 β€ b_i β€ m) β the positions of the broken segments. These integers are given in increasing order, that is, b_1 < b_2 < β¦ < b_n.
Output
Print the minimum total length of the pieces.
Examples
Input
4 100 2
20 30 75 80
Output
17
Input
5 100 3
1 2 4 60 87
Output
6
Note
In the first example, you can use a piece of length 11 to cover the broken segments 20 and 30, and another piece of length 6 to cover 75 and 80, for a total length of 17.
In the second example, you can use a piece of length 4 to cover broken segments 1, 2 and 4, and two pieces of length 1 to cover broken segments 60 and 87.
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.
Vasya has written some permutation p_1, p_2, β¦, p_n of integers from 1 to n, so for all 1 β€ i β€ n it is true that 1 β€ p_i β€ n and all p_1, p_2, β¦, p_n are different. After that he wrote n numbers next_1, next_2, β¦, next_n. The number next_i is equal to the minimal index i < j β€ n, such that p_j > p_i. If there is no such j let's let's define as next_i = n + 1.
In the evening Vasya went home from school and due to rain, his notebook got wet. Now it is impossible to read some written numbers. Permutation and some values next_i are completely lost! If for some i the value next_i is lost, let's say that next_i = -1.
You are given numbers next_1, next_2, β¦, next_n (maybe some of them are equal to -1). Help Vasya to find such permutation p_1, p_2, β¦, p_n of integers from 1 to n, that he can write it to the notebook and all numbers next_i, which are not equal to -1, will be correct.
Input
The first line contains one integer t β the number of test cases (1 β€ t β€ 100 000).
Next 2 β
t lines contains the description of test cases,two lines for each. The first line contains one integer n β the length of the permutation, written by Vasya (1 β€ n β€ 500 000). The second line contains n integers next_1, next_2, β¦, next_n, separated by spaces (next_i = -1 or i < next_i β€ n + 1).
It is guaranteed, that the sum of n in all test cases doesn't exceed 500 000.
In hacks you can only use one test case, so T = 1.
Output
Print T lines, in i-th of them answer to the i-th test case.
If there is no such permutations p_1, p_2, β¦, p_n of integers from 1 to n, that Vasya could write, print the only number -1.
In the other case print n different integers p_1, p_2, β¦, p_n, separated by spaces (1 β€ p_i β€ n). All defined values of next_i which are not equal to -1 should be computed correctly p_1, p_2, β¦, p_n using defenition given in the statement of the problem. If there exists more than one solution you can find any of them.
Example
Input
6
3
2 3 4
2
3 3
3
-1 -1 -1
3
3 4 -1
1
2
4
4 -1 4 5
Output
1 2 3
2 1
2 1 3
-1
1
3 2 1 4
Note
In the first test case for permutation p = [1, 2, 3] Vasya should write next = [2, 3, 4], because each number in permutation is less than next. It's easy to see, that it is the only satisfying permutation.
In the third test case, any permutation can be the answer because all numbers next_i are lost.
In the fourth test case, there is no satisfying permutation, so the answer is -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.
While sailing on a boat, Inessa noticed a beautiful water lily flower above the lake's surface. She came closer and it turned out that the lily was exactly H centimeters above the water surface. Inessa grabbed the flower and sailed the distance of L centimeters. Exactly at this point the flower touched the water surface.
<image>
Suppose that the lily grows at some point A on the lake bottom, and its stem is always a straight segment with one endpoint at point A. Also suppose that initially the flower was exactly above the point A, i.e. its stem was vertical. Can you determine the depth of the lake at point A?
Input
The only line contains two integers H and L (1 β€ H < L β€ 10^{6}).
Output
Print a single number β the depth of the lake at point A. The absolute or relative error should not exceed 10^{-6}.
Formally, let your answer be A, and the jury's answer be B. Your answer is accepted if and only if \frac{|A - B|}{max{(1, |B|)}} β€ 10^{-6}.
Examples
Input
1 2
Output
1.5000000000000
Input
3 5
Output
2.6666666666667
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.
Nikolay got a string s of even length n, which consists only of lowercase Latin letters 'a' and 'b'. Its positions are numbered from 1 to n.
He wants to modify his string so that every its prefix of even length has an equal amount of letters 'a' and 'b'. To achieve that, Nikolay can perform the following operation arbitrary number of times (possibly, zero): choose some position in his string and replace the letter on this position with the other letter (i.e. replace 'a' with 'b' or replace 'b' with 'a'). Nikolay can use no letters except 'a' and 'b'.
The prefix of string s of length l (1 β€ l β€ n) is a string s[1..l].
For example, for the string s="abba" there are two prefixes of the even length. The first is s[1...2]="ab" and the second s[1...4]="abba". Both of them have the same number of 'a' and 'b'.
Your task is to calculate the minimum number of operations Nikolay has to perform with the string s to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
Input
The first line of the input contains one even integer n (2 β€ n β€ 2β
10^{5}) β the length of string s.
The second line of the input contains the string s of length n, which consists only of lowercase Latin letters 'a' and 'b'.
Output
In the first line print the minimum number of operations Nikolay has to perform with the string s to modify it so that every its prefix of even length has an equal amount of letters 'a' and 'b'.
In the second line print the string Nikolay obtains after applying all the operations. If there are multiple answers, you can print any of them.
Examples
Input
4
bbbb
Output
2
abba
Input
6
ababab
Output
0
ababab
Input
2
aa
Output
1
ba
Note
In the first example Nikolay has to perform two operations. For example, he can replace the first 'b' with 'a' and the last 'b' with 'a'.
In the second example Nikolay doesn't need to do anything because each prefix of an even length of the initial string already contains an equal amount of letters 'a' and '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.
This is a harder version of the problem. In this version, n β€ 300 000.
Vasya is an experienced developer of programming competitions' problems. As all great minds at some time, Vasya faced a creative crisis. To improve the situation, Petya gifted him a string consisting of opening and closing brackets only. Petya believes, that the beauty of the bracket string is a number of its cyclical shifts, which form a correct bracket sequence.
To digress from his problems, Vasya decided to select two positions of the string (not necessarily distinct) and swap characters located at this positions with each other. Vasya will apply this operation exactly once. He is curious what is the maximum possible beauty he can achieve this way. Please help him.
We remind that bracket sequence s is called correct if:
* s is empty;
* s is equal to "(t)", where t is correct bracket sequence;
* s is equal to t_1 t_2, i.e. concatenation of t_1 and t_2, where t_1 and t_2 are correct bracket sequences.
For example, "(()())", "()" are correct, while ")(" and "())" are not.
The cyclical shift of the string s of length n by k (0 β€ k < n) is a string formed by a concatenation of the last k symbols of the string s with the first n - k symbols of string s. For example, the cyclical shift of string "(())()" by 2 equals "()(())".
Cyclical shifts i and j are considered different, if i β j.
Input
The first line contains an integer n (1 β€ n β€ 300 000), the length of the string.
The second line contains a string, consisting of exactly n characters, where each of the characters is either "(" or ")".
Output
The first line should contain a single integer β the largest beauty of the string, which can be achieved by swapping some two characters.
The second line should contain integers l and r (1 β€ l, r β€ n) β the indices of two characters, which should be swapped in order to maximize the string's beauty.
In case there are several possible swaps, print any of them.
Examples
Input
10
()()())(()
Output
5
8 7
Input
12
)(()(()())()
Output
4
5 10
Input
6
)))(()
Output
0
1 1
Note
In the first example, we can swap 7-th and 8-th character, obtaining a string "()()()()()". The cyclical shifts by 0, 2, 4, 6, 8 of this string form a correct bracket sequence.
In the second example, after swapping 5-th and 10-th character, we obtain a string ")(())()()(()". The cyclical shifts by 11, 7, 5, 3 of this string form a correct bracket sequence.
In the third example, swap of any two brackets results in 0 cyclical shifts being correct bracket sequences.
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 play a computer game. In this game, you lead a party of m heroes, and you have to clear a dungeon with n monsters. Each monster is characterized by its power a_i. Each hero is characterized by his power p_i and endurance s_i.
The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day.
When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated k monsters, the hero fights with the monster k + 1). When the hero fights the monster, there are two possible outcomes:
* if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends;
* otherwise, the monster is defeated.
After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the i-th hero cannot defeat more than s_i monsters during each day), or if all monsters are defeated β otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends.
Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times.
Input
The first line contains one integer t (1 β€ t β€ 10^5) β the number of test cases. Then the test cases follow.
The first line of each test case contains one integer n (1 β€ n β€ 2 β
10^5) β the number of monsters in the dungeon.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9), where a_i is the power of the i-th monster.
The third line contains one integer m (1 β€ m β€ 2 β
10^5) β the number of heroes in your party.
Then m lines follow, each describing a hero. Each line contains two integers p_i and s_i (1 β€ p_i β€ 10^9, 1 β€ s_i β€ n) β the power and the endurance of the i-th hero.
It is guaranteed that the sum of n + m over all test cases does not exceed 2 β
10^5.
Output
For each test case print one integer β the minimum number of days you have to spend to defeat all of the monsters (or -1 if it is impossible).
Example
Input
2
6
2 3 11 14 1 8
2
3 2
100 1
5
3 5 100 2 3
2
30 5
90 1
Output
5
-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.
Guy-Manuel and Thomas are going to build a polygon spaceship.
You're given a strictly convex (i. e. no three points are collinear) polygon P which is defined by coordinates of its vertices. Define P(x,y) as a polygon obtained by translating P by vector \overrightarrow {(x,y)}. The picture below depicts an example of the translation:
<image>
Define T as a set of points which is the union of all P(x,y) such that the origin (0,0) lies in P(x,y) (both strictly inside and on the boundary). There is also an equivalent definition: a point (x,y) lies in T only if there are two points A,B in P such that \overrightarrow {AB} = \overrightarrow {(x,y)}. One can prove T is a polygon too. For example, if P is a regular triangle then T is a regular hexagon. At the picture below P is drawn in black and some P(x,y) which contain the origin are drawn in colored:
<image>
The spaceship has the best aerodynamic performance if P and T are similar. Your task is to check whether the polygons P and T are [similar](https://tinyurl.com/vp5m7vl).
Input
The first line of input will contain a single integer n (3 β€ n β€ 10^5) β the number of points.
The i-th of the next n lines contains two integers x_i, y_i (|x_i|, |y_i| β€ 10^9), denoting the coordinates of the i-th vertex.
It is guaranteed that these points are listed in counterclockwise order and these points form a strictly convex polygon.
Output
Output "YES" in a separate line, if P and T are similar. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower).
Examples
Input
4
1 0
4 1
3 4
0 3
Output
YES
Input
3
100 86
50 0
150 0
Output
nO
Input
8
0 0
1 0
2 1
3 3
4 6
3 6
2 5
1 3
Output
YES
Note
The following image shows the first sample: both P and T are squares. The second sample was shown in the statements.
<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.
Vova had a pretty weird sleeping schedule. There are h hours in a day. Vova will sleep exactly n times. The i-th time he will sleep exactly after a_i hours from the time he woke up. You can assume that Vova woke up exactly at the beginning of this story (the initial time is 0). Each time Vova sleeps exactly one day (in other words, h hours).
Vova thinks that the i-th sleeping time is good if he starts to sleep between hours l and r inclusive.
Vova can control himself and before the i-th time can choose between two options: go to sleep after a_i hours or after a_i - 1 hours.
Your task is to say the maximum number of good sleeping times Vova can obtain if he acts optimally.
Input
The first line of the input contains four integers n, h, l and r (1 β€ n β€ 2000, 3 β€ h β€ 2000, 0 β€ l β€ r < h) β the number of times Vova goes to sleep, the number of hours in a day and the segment of the good sleeping time.
The second line of the input contains n integers a_1, a_2, ..., a_n (1 β€ a_i < h), where a_i is the number of hours after which Vova goes to sleep the i-th time.
Output
Print one integer β the maximum number of good sleeping times Vova can obtain if he acts optimally.
Example
Input
7 24 21 23
16 17 14 20 20 11 22
Output
3
Note
The maximum number of good times in the example is 3.
The story starts from t=0. Then Vova goes to sleep after a_1 - 1 hours, now the time is 15. This time is not good. Then Vova goes to sleep after a_2 - 1 hours, now the time is 15 + 16 = 7. This time is also not good. Then Vova goes to sleep after a_3 hours, now the time is 7 + 14 = 21. This time is good. Then Vova goes to sleep after a_4 - 1 hours, now the time is 21 + 19 = 16. This time is not good. Then Vova goes to sleep after a_5 hours, now the time is 16 + 20 = 12. This time is not good. Then Vova goes to sleep after a_6 hours, now the time is 12 + 11 = 23. This time is good. Then Vova goes to sleep after a_7 hours, now the time is 23 + 22 = 21. This time is also good.
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.
Ridhiman challenged Ashish to find the maximum valued subsequence of an array a of size n consisting of positive integers.
The value of a non-empty subsequence of k elements of a is defined as β 2^i over all integers i β₯ 0 such that at least max(1, k - 2) elements of the subsequence have the i-th bit set in their binary representation (value x has the i-th bit set in its binary representation if β (x)/(2^i) β mod 2 is equal to 1).
Recall that b is a subsequence of a, if b can be obtained by deleting some(possibly zero) elements from a.
Help Ashish find the maximum value he can get by choosing some subsequence of a.
Input
The first line of the input consists of a single integer n (1 β€ n β€ 500) β the size of a.
The next line consists of n space-separated integers β the elements of the array (1 β€ a_i β€ 10^{18}).
Output
Print a single integer β the maximum value Ashish can get by choosing some subsequence of a.
Examples
Input
3
2 1 3
Output
3
Input
3
3 1 4
Output
7
Input
1
1
Output
1
Input
4
7 7 1 1
Output
7
Note
For the first test case, Ashish can pick the subsequence \{{2, 3}\} of size 2. The binary representation of 2 is 10 and that of 3 is 11. Since max(k - 2, 1) is equal to 1, the value of the subsequence is 2^0 + 2^1 (both 2 and 3 have 1-st bit set in their binary representation and 3 has 0-th bit set in its binary representation). Note that he could also pick the subsequence \{{3\}} or \{{2, 1, 3\}}.
For the second test case, Ashish can pick the subsequence \{{3, 4\}} with value 7.
For the third test case, Ashish can pick the subsequence \{{1\}} with value 1.
For the fourth test case, Ashish can pick the subsequence \{{7, 7\}} with value 7.
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.
We hid from you a permutation p of length n, consisting of the elements from 1 to n. You want to guess it. To do that, you can give us 2 different indices i and j, and we will reply with p_{i} mod p_{j} (remainder of division p_{i} by p_{j}).
We have enough patience to answer at most 2 β
n queries, so you should fit in this constraint. Can you do it?
As a reminder, a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Input
The only line of the input contains a single integer n (1 β€ n β€ 10^4) β length of the permutation.
Interaction
The interaction starts with reading n.
Then you are allowed to make at most 2 β
n queries in the following way:
* "? x y" (1 β€ x, y β€ n, x β y).
After each one, you should read an integer k, that equals p_x mod p_y.
When you have guessed the permutation, print a single line "! " (without quotes), followed by array p and quit.
After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Exit immediately after receiving "-1" and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.
Hack format
In the first line output n (1 β€ n β€ 10^4). In the second line print the permutation of n integers p_1, p_2, β¦, p_n.
Example
Input
3
1
2
1
0
Output
? 1 2
? 3 2
? 1 3
? 2 1
! 1 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.
Wabbit is trying to move a box containing food for the rest of the zoo in the coordinate plane from the point (x_1,y_1) to the point (x_2,y_2).
He has a rope, which he can use to pull the box. He can only pull the box if he stands exactly 1 unit away from the box in the direction of one of two coordinate axes. He will pull the box to where he is standing before moving out of the way in the same direction by 1 unit.
<image>
For example, if the box is at the point (1,2) and Wabbit is standing at the point (2,2), he can pull the box right by 1 unit, with the box ending up at the point (2,2) and Wabbit ending at the point (3,2).
Also, Wabbit can move 1 unit to the right, left, up, or down without pulling the box. In this case, it is not necessary for him to be in exactly 1 unit away from the box. If he wants to pull the box again, he must return to a point next to the box. Also, Wabbit can't move to the point where the box is located.
Wabbit can start at any point. It takes 1 second to travel 1 unit right, left, up, or down, regardless of whether he pulls the box while moving.
Determine the minimum amount of time he needs to move the box from (x_1,y_1) to (x_2,y_2). Note that the point where Wabbit ends up at does not matter.
Input
Each test contains multiple test cases. The first line contains a single integer t (1 β€ t β€ 1000): the number of test cases. The description of the test cases follows.
Each of the next t lines contains four space-separated integers x_1, y_1, x_2, y_2 (1 β€ x_1, y_1, x_2, y_2 β€ 10^9), describing the next test case.
Output
For each test case, print a single integer: the minimum time in seconds Wabbit needs to bring the box from (x_1,y_1) to (x_2,y_2).
Example
Input
2
1 2 2 2
1 1 2 2
Output
1
4
Note
In the first test case, the starting and the ending points of the box are (1,2) and (2,2) respectively. This is the same as the picture in the statement. Wabbit needs only 1 second to move as shown in the picture in the statement.
In the second test case, Wabbit can start at the point (2,1). He pulls the box to (2,1) while moving to (3,1). He then moves to (3,2) and then to (2,2) without pulling the box. Then, he pulls the box to (2,2) while moving to (2,3). It takes 4 seconds.
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.
Ridbit starts with an integer n.
In one move, he can perform one of the following operations:
* divide n by one of its proper divisors, or
* subtract 1 from n if n is greater than 1.
A proper divisor is a divisor of a number, excluding itself. For example, 1, 2, 4, 5, and 10 are proper divisors of 20, but 20 itself is not.
What is the minimum number of moves Ridbit is required to make to reduce n to 1?
Input
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases.
The only line of each test case contains a single integer n (1 β€ n β€ 10^9).
Output
For each test case, output the minimum number of moves required to reduce n to 1.
Example
Input
6
1
2
3
4
6
9
Output
0
1
2
2
2
3
Note
For the test cases in the example, n may be reduced to 1 using the following operations in sequence
1
2 \xrightarrow{} 1
3 \xrightarrow{} 2 \xrightarrow{} 1
4 \xrightarrow{} 2 \xrightarrow{} 1
6 \xrightarrow{} 2 \xrightarrow{} 1
9 \xrightarrow{} 3 \xrightarrow{} 2\xrightarrow{} 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 an integer n. Check if n has an odd divisor, greater than one (does there exist such a number x (x > 1) that n is divisible by x and x is odd).
For example, if n=6, then there is x=3. If n=4, then such a number does not exist.
Input
The first line contains one integer t (1 β€ t β€ 10^4) β the number of test cases. Then t test cases follow.
Each test case contains one integer n (2 β€ n β€ 10^{14}).
Please note, that the input for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language.
Output
For each test case, output on a separate line:
* "YES" if n has an odd divisor, greater than one;
* "NO" otherwise.
You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
Example
Input
6
2
3
4
5
998244353
1099511627776
Output
NO
YES
NO
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.
As you know, Bob's brother lives in Flatland. In Flatland there are n cities, connected by n - 1 two-way roads. The cities are numbered from 1 to n. You can get from one city to another moving along the roads.
The Β«Two PathsΒ» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path is a sequence of different cities, connected sequentially by roads. The company is allowed to choose by itself the paths to repair. The only condition they have to meet is that the two paths shouldn't cross (i.e. shouldn't have common cities).
It is known that the profit, the Β«Two PathsΒ» company will get, equals the product of the lengths of the two paths. Let's consider the length of each road equals 1, and the length of a path equals the amount of roads in it. Find the maximum possible profit for the company.
Input
The first line contains an integer n (2 β€ n β€ 200), where n is the amount of cities in the country. The following n - 1 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road ai, bi (1 β€ ai, bi β€ n).
Output
Output the maximum possible profit.
Examples
Input
4
1 2
2 3
3 4
Output
1
Input
7
1 2
1 3
1 4
1 5
1 6
1 7
Output
0
Input
6
1 2
2 3
2 4
5 4
6 4
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 have an initially empty cauldron, and you want to brew a potion in it. The potion consists of two ingredients: magic essence and water. The potion you want to brew should contain exactly k\ \% magic essence and (100 - k)\ \% water.
In one step, you can pour either one liter of magic essence or one liter of water into the cauldron. What is the minimum number of steps to brew a potion? You don't care about the total volume of the potion, only about the ratio between magic essence and water in it.
A small reminder: if you pour e liters of essence and w liters of water (e + w > 0) into the cauldron, then it contains (e)/(e + w) β
100\ \% (without rounding) magic essence and (w)/(e + w) β
100\ \% water.
Input
The first line contains the single t (1 β€ t β€ 100) β the number of test cases.
The first and only line of each test case contains a single integer k (1 β€ k β€ 100) β the percentage of essence in a good potion.
Output
For each test case, print the minimum number of steps to brew a good potion. It can be proved that it's always possible to achieve it in a finite number of steps.
Example
Input
3
3
100
25
Output
100
1
4
Note
In the first test case, you should pour 3 liters of magic essence and 97 liters of water into the cauldron to get a potion with 3\ \% of magic essence.
In the second test case, you can pour only 1 liter of essence to get a potion with 100\ \% of magic essence.
In the third test case, you can pour 1 liter of magic essence and 3 liters of water.
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.
Sherlock Holmes found a mysterious correspondence of two VIPs and made up his mind to read it. But there is a problem! The correspondence turned out to be encrypted. The detective tried really hard to decipher the correspondence, but he couldn't understand anything.
At last, after some thought, he thought of something. Let's say there is a word s, consisting of |s| lowercase Latin letters. Then for one operation you can choose a certain position p (1 β€ p < |s|) and perform one of the following actions:
* either replace letter sp with the one that alphabetically follows it and replace letter sp + 1 with the one that alphabetically precedes it;
* or replace letter sp with the one that alphabetically precedes it and replace letter sp + 1 with the one that alphabetically follows it.
Let us note that letter "z" doesn't have a defined following letter and letter "a" doesn't have a defined preceding letter. That's why the corresponding changes are not acceptable. If the operation requires performing at least one unacceptable change, then such operation cannot be performed.
Two words coincide in their meaning iff one of them can be transformed into the other one as a result of zero or more operations.
Sherlock Holmes needs to learn to quickly determine the following for each word: how many words can exist that coincide in their meaning with the given word, but differs from the given word in at least one character? Count this number for him modulo 1000000007 (109 + 7).
Input
The input data contains several tests. The first line contains the only integer t (1 β€ t β€ 104) β the number of tests.
Next t lines contain the words, one per line. Each word consists of lowercase Latin letters and has length from 1 to 100, inclusive. Lengths of words can differ.
Output
For each word you should print the number of different other words that coincide with it in their meaning β not from the words listed in the input data, but from all possible words. As the sought number can be very large, print its value modulo 1000000007 (109 + 7).
Examples
Input
1
ab
Output
1
Input
1
aaaaaaaaaaa
Output
0
Input
2
ya
klmbfxzb
Output
24
320092793
Note
Some explanations about the operation:
* Note that for each letter, we can clearly define the letter that follows it. Letter "b" alphabetically follows letter "a", letter "c" follows letter "b", ..., "z" follows letter "y".
* Preceding letters are defined in the similar manner: letter "y" precedes letter "z", ..., "a" precedes letter "b".
* Note that the operation never changes a word's length.
In the first sample you can obtain the only other word "ba". In the second sample you cannot obtain any other word, so the correct answer is 0.
Consider the third sample. One operation can transform word "klmbfxzb" into word "klmcexzb": we should choose p = 4, and replace the fourth letter with the following one ("b" β "c"), and the fifth one β with the preceding one ("f" β "e"). Also, we can obtain many other words from this one. An operation can transform word "ya" only into one other word "xb".
Word "ya" coincides in its meaning with words "xb", "wc", "vd", ..., "ay" (overall there are 24 other words). The word "klmbfxzb has many more variants β there are 3320092814 other words that coincide with in the meaning. So the answer for the first word equals 24 and for the second one equals 320092793 β the number 3320092814 modulo 109 + 7
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.
Vasya plays a computer game with ninjas. At this stage Vasya's ninja should get out of a deep canyon.
The canyon consists of two vertical parallel walls, their height is n meters. Let's imagine that we split these walls into 1 meter-long areas and number them with positive integers from 1 to n from bottom to top. Some areas are safe and the ninja can climb them. Others are spiky and ninja can't be there. Let's call such areas dangerous.
Initially the ninja is on the lower area of the left wall. He can use each second to perform one of the following actions:
* climb one area up;
* climb one area down;
* jump to the opposite wall. That gets the ninja to the area that is exactly k meters higher than the area he jumped from. More formally, if before the jump the ninja is located at area x of one wall, then after the jump he is located at area x + k of the other wall.
If at some point of time the ninja tries to get to an area with a number larger than n, then we can assume that the ninja got out of the canyon.
The canyon gets flooded and each second the water level raises one meter. Initially the water level is at the lower border of the first area. Ninja cannot be on the area covered by water. We can assume that the ninja and the water "move in turns" β first the ninja performs some action, then the water raises for one meter, then the ninja performs one more action and so on.
The level is considered completed if the ninja manages to get out of the canyon.
After several failed attempts Vasya started to doubt whether it is possible to complete the level at all. Help him answer the question.
Input
The first line contains two integers n and k (1 β€ n, k β€ 105) β the height of the canyon and the height of ninja's jump, correspondingly.
The second line contains the description of the left wall β a string with the length of n characters. The i-th character represents the state of the i-th wall area: character "X" represents a dangerous area and character "-" represents a safe area.
The third line describes the right wall in the same format.
It is guaranteed that the first area of the left wall is not dangerous.
Output
Print "YES" (without the quotes) if the ninja can get out from the canyon, otherwise, print "NO" (without the quotes).
Examples
Input
7 3
---X--X
-X--XX-
Output
YES
Input
6 2
--X-X-
X--XX-
Output
NO
Note
In the first sample the ninja should first jump to the right wall, then go one meter down along the right wall, then jump to the left wall. The next jump can get the ninja from the canyon.
In the second sample there's no way the ninja can get out of the canyon.
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.
Recently a top secret mission to Mars has taken place. As a result, scientists managed to obtain some information about the Martian DNA. Now we know that any Martian DNA contains at most m different nucleotides, numbered from 1 to m. Special characteristics of the Martian DNA prevent some nucleotide pairs from following consecutively in this chain. For example, if the nucleotide 1 and nucleotide 2 can not follow consecutively in the Martian DNA, then the chain of nucleotides [1, 2] is not a valid chain of Martian DNA, but the chain of nucleotides [2, 1] can be a valid chain (if there is no corresponding restriction). The number of nucleotide pairs that can't follow in the DNA chain consecutively, is k.
The needs of gene research required information about the quantity of correct n-long chains of the Martian DNA. Your task is to write a program that will calculate this value.
Input
The first line contains three space-separated integers n, m, k (1 β€ n β€ 1015, 1 β€ m β€ 52, 0 β€ k β€ m2).
Next k lines contain two characters each, without a space between them, representing a forbidden nucleotide pair. The first character represents the first nucleotide in the forbidden pair, the second character represents the second nucleotide.
The nucleotides with assigned numbers from 1 to 26 are represented by English alphabet letters from "a" to "z" (1 is an "a", 2 is a "b", ..., 26 is a "z"). Nucleotides with assigned numbers from 27 to 52 are represented by English alphabet letters from "A" to "Z" (27 is an "A", 28 is a "B", ..., 52 is a "Z").
It is guaranteed that each forbidden pair occurs at most once in the input. It is guaranteed that nucleotide's numbers in all forbidden pairs cannot be more than m. Note that order is important in nucleotide pairs.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Output
Print a single integer β the sought number modulo 1000000007 (109 + 7).
Examples
Input
3 3 2
ab
ba
Output
17
Input
3 3 0
Output
27
Input
2 1 1
aa
Output
0
Note
In the second test case all possible three-nucleotide DNAs are permitted. Each nucleotide can take one of three values, thus in total there are 27 distinct three nucleotide DNAs.
In the third test sample we cannot make any DNA of two nucleotides β the only possible nucleotide "a" cannot occur two times consecutively.
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've got string s, consisting of small English letters. Some of the English letters are good, the rest are bad.
A substring s[l...r] (1 β€ l β€ r β€ |s|) of string s = s1s2...s|s| (where |s| is the length of string s) is string slsl + 1...sr.
The substring s[l...r] is good, if among the letters sl, sl + 1, ..., sr there are at most k bad ones (look at the sample's explanation to understand it more clear).
Your task is to find the number of distinct good substrings of the given string s. Two substrings s[x...y] and s[p...q] are considered distinct if their content is different, i.e. s[x...y] β s[p...q].
Input
The first line of the input is the non-empty string s, consisting of small English letters, the string's length is at most 1500 characters.
The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the i-th character of this string equals "1", then the i-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on.
The third line of the input consists a single integer k (0 β€ k β€ |s|) β the maximum acceptable number of bad characters in a good substring.
Output
Print a single integer β the number of distinct good substrings of string s.
Examples
Input
ababab
01000000000000000000000000
1
Output
5
Input
acbacbacaa
00000000000000000000000000
2
Output
8
Note
In the first example there are following good substrings: "a", "ab", "b", "ba", "bab".
In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
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.
Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: li, ri, di, (1 β€ li β€ ri β€ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, ..., ri by value di.
Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1 β€ xi β€ yi β€ m). That means that one should apply operations with numbers xi, xi + 1, ..., yi to the array.
Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.
Input
The first line contains integers n, m, k (1 β€ n, m, k β€ 105). The second line contains n integers: a1, a2, ..., an (0 β€ ai β€ 105) β the initial array.
Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1 β€ li β€ ri β€ n), (0 β€ di β€ 105).
Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1 β€ xi β€ yi β€ m).
The numbers in the lines are separated by single spaces.
Output
On a single line print n integers a1, a2, ..., an β the array after executing all the queries. Separate the printed numbers by spaces.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
Examples
Input
3 3 3
1 2 3
1 2 1
1 3 2
2 3 4
1 2
1 3
2 3
Output
9 18 17
Input
1 1 1
1
1 1 1
1 1
Output
2
Input
4 3 6
1 2 3 4
1 2 1
2 3 2
3 4 4
1 2
1 3
2 3
1 2
1 3
2 3
Output
5 18 31 20
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.
Nothing has changed since the last round. Dima and Inna still love each other and want to be together. They've made a deal with Seryozha and now they need to make a deal with the dorm guards...
There are four guardposts in Dima's dorm. Each post contains two guards (in Russia they are usually elderly women). You can bribe a guard by a chocolate bar or a box of juice. For each guard you know the minimum price of the chocolate bar she can accept as a gift and the minimum price of the box of juice she can accept as a gift. If a chocolate bar for some guard costs less than the minimum chocolate bar price for this guard is, or if a box of juice for some guard costs less than the minimum box of juice price for this guard is, then the guard doesn't accept such a gift.
In order to pass through a guardpost, one needs to bribe both guards.
The shop has an unlimited amount of juice and chocolate of any price starting with 1. Dima wants to choose some guardpost, buy one gift for each guard from the guardpost and spend exactly n rubles on it.
Help him choose a post through which he can safely sneak Inna or otherwise say that this is impossible. Mind you, Inna would be very sorry to hear that!
Input
The first line of the input contains integer n (1 β€ n β€ 105) β the money Dima wants to spend. Then follow four lines describing the guardposts. Each line contains four integers a, b, c, d (1 β€ a, b, c, d β€ 105) β the minimum price of the chocolate and the minimum price of the juice for the first guard and the minimum price of the chocolate and the minimum price of the juice for the second guard, correspondingly.
Output
In a single line of the output print three space-separated integers: the number of the guardpost, the cost of the first present and the cost of the second present. If there is no guardpost Dima can sneak Inna through at such conditions, print -1 in a single line.
The guardposts are numbered from 1 to 4 according to the order given in the input.
If there are multiple solutions, you can print any of them.
Examples
Input
10
5 6 5 6
6 6 7 7
5 8 6 6
9 9 9 9
Output
1 5 5
Input
10
6 6 6 6
7 7 7 7
4 4 4 4
8 8 8 8
Output
3 4 6
Input
5
3 3 3 3
3 3 3 3
3 3 3 3
3 3 3 3
Output
-1
Note
Explanation of the first example.
The only way to spend 10 rubles to buy the gifts that won't be less than the minimum prices is to buy two 5 ruble chocolates to both guards from the first guardpost.
Explanation of the second example.
Dima needs 12 rubles for the first guardpost, 14 for the second one, 16 for the fourth one. So the only guardpost we can sneak through is the third one. So, Dima can buy 4 ruble chocolate for the first guard and 6 ruble juice of the second guard.
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 + B is often used as an example of the easiest problem possible to show some contest platform. However, some scientists have observed that sometimes this problem is not so easy to get accepted. Want to try?
Input
The input contains two integers a and b (0 β€ a, b β€ 103), separated by a single space.
Output
Output the sum of the given integers.
Examples
Input
5 14
Output
19
Input
381 492
Output
873
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.
Mr. Kitayuta has just bought an undirected graph with n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi.
Mr. Kitayuta wants you to process the following q queries.
In the i-th query, he gives you two integers - ui and vi.
Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly.
Input
The first line of the input contains space-separated two integers - n and m(2 β€ n β€ 105, 1 β€ m β€ 105), denoting the number of the vertices and the number of the edges, respectively.
The next m lines contain space-separated three integers - ai, bi(1 β€ ai < bi β€ n) and ci(1 β€ ci β€ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β j, (ai, bi, ci) β (aj, bj, cj).
The next line contains a integer- q(1 β€ q β€ 105), denoting the number of the queries.
Then follows q lines, containing space-separated two integers - ui and vi(1 β€ ui, vi β€ n). It is guaranteed that ui β vi.
Output
For each query, print the answer in a separate line.
Examples
Input
4 5
1 2 1
1 2 2
2 3 1
2 3 3
2 4 3
3
1 2
3 4
1 4
Output
2
1
0
Input
5 7
1 5 1
2 5 1
3 5 1
4 5 1
1 2 2
2 3 2
3 4 2
5
1 5
5 1
2 5
1 5
1 4
Output
1
1
1
1
2
Note
Let's consider the first sample.
<image> The figure above shows the first sample.
* Vertex 1 and vertex 2 are connected by color 1 and 2.
* Vertex 3 and vertex 4 are connected by color 3.
* Vertex 1 and vertex 4 are not connected by any single color.
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.
Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area.
Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard.
Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules.
Input
The first line of the input contains six positive integers x1, y1, x2, y2, x3, y3 (1 β€ x1, y1, x2, y2, x3, y3 β€ 100), where xi and yi determine the length and width of the logo of the i-th company respectively.
Output
If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes).
If it is possible, print in the first line the length of a side of square n, where you can place all the three logos. Each of the next n lines should contain n uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that:
* the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company,
* the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company,
* the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company,
Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them.
See the samples to better understand the statement.
Examples
Input
5 1 2 5 5 2
Output
5
AAAAA
BBBBB
BBBBB
CCCCC
CCCCC
Input
4 4 2 6 4 2
Output
6
BBBBBB
BBBBBB
AAAACC
AAAACC
AAAACC
AAAACC
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.
Vasya works as a watchman in the gallery. Unfortunately, one of the most expensive paintings was stolen while he was on duty. He doesn't want to be fired, so he has to quickly restore the painting. He remembers some facts about it.
* The painting is a square 3 Γ 3, each cell contains a single integer from 1 to n, and different cells may contain either different or equal integers.
* The sum of integers in each of four squares 2 Γ 2 is equal to the sum of integers in the top left square 2 Γ 2.
* Four elements a, b, c and d are known and are located as shown on the picture below.
<image>
Help Vasya find out the number of distinct squares the satisfy all the conditions above. Note, that this number may be equal to 0, meaning Vasya remembers something wrong.
Two squares are considered to be different, if there exists a cell that contains two different integers in different squares.
Input
The first line of the input contains five integers n, a, b, c and d (1 β€ n β€ 100 000, 1 β€ a, b, c, d β€ n) β maximum possible value of an integer in the cell and four integers that Vasya remembers.
Output
Print one integer β the number of distinct valid squares.
Examples
Input
2 1 1 1 2
Output
2
Input
3 3 1 2 3
Output
6
Note
Below are all the possible paintings for the first sample. <image> <image>
In the second sample, only paintings displayed below satisfy all the rules. <image> <image> <image> <image> <image> <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.
Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:
1. on this day the gym is closed and the contest is not carried out;
2. on this day the gym is closed and the contest is carried out;
3. on this day the gym is open and the contest is not carried out;
4. on this day the gym is open and the contest is carried out.
On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).
Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has β he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.
Input
The first line contains a positive integer n (1 β€ n β€ 100) β the number of days of Vasya's vacations.
The second line contains the sequence of integers a1, a2, ..., an (0 β€ ai β€ 3) separated by space, where:
* ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out;
* ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out;
* ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out;
* ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
Output
Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:
* to do sport on any two consecutive days,
* to write the contest on any two consecutive days.
Examples
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
Note
In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.
In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.
In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.
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 is the following puzzle popular among nuclear physicists.
A reactor contains a set of n atoms of some chemical elements. We shall understand the phrase "atomic number" as the number of this atom's element in the periodic table of the chemical elements.
You are allowed to take any two different atoms and fuse a new one from them. That results in a new atom, whose number is equal to the sum of the numbers of original atoms. The fusion operation can be performed several times.
The aim is getting a new pregiven set of k atoms.
The puzzle's difficulty is that it is only allowed to fuse two atoms into one, it is not allowed to split an atom into several atoms. You are suggested to try to solve the puzzle.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 17). The second line contains space-separated symbols of elements of n atoms, which are available from the start. The third line contains space-separated symbols of elements of k atoms which need to be the result of the fusion. The symbols of the elements coincide with the symbols from the periodic table of the chemical elements. The atomic numbers do not exceed 100 (elements possessing larger numbers are highly unstable). Some atoms can have identical numbers (that is, there can be several atoms of the same element). The sum of numbers of initial atoms is equal to the sum of numbers of the atoms that need to be synthesized.
Output
If it is impossible to synthesize the required atoms, print "NO" without the quotes. Otherwise, print on the first line Β«YESΒ», and on the next k lines print the way of synthesizing each of k atoms as equations. Each equation has the following form: "x1+x2+...+xt->yi", where xj is the symbol of the element of some atom from the original set, and yi is the symbol of the element of some atom from the resulting set. Each atom from the input data should occur in the output data exactly one time. The order of summands in the equations, as well as the output order does not matter. If there are several solutions, print any of them. For a better understanding of the output format, see the samples.
Examples
Input
10 3
Mn Co Li Mg C P F Zn Sc K
Sn Pt Y
Output
YES
Mn+C+K->Sn
Co+Zn+Sc->Pt
Li+Mg+P+F->Y
Input
2 1
H H
He
Output
YES
H+H->He
Input
2 2
Bk Fm
Cf Es
Output
NO
Note
The reactions from the first example possess the following form (the atomic number is written below and to the left of the element):
<image>
<image>
<image>
To find a periodic table of the chemical elements, you may use your favorite search engine.
The pretest set contains each of the first 100 elements of the periodic table at least once. You can use that information to check for misprints.
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.
Just to remind, girls in Arpa's land are really nice.
Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship groups. Two Hoses x and y are in the same friendship group if and only if there is a sequence of Hoses a1, a2, ..., ak such that ai and ai + 1 are friends for each 1 β€ i < k, and a1 = x and ak = y.
<image>
Arpa allowed to use the amphitheater of palace to Mehrdad for this party. Arpa's amphitheater can hold at most w weight on it.
Mehrdad is so greedy that he wants to invite some Hoses such that sum of their weights is not greater than w and sum of their beauties is as large as possible. Along with that, from each friendship group he can either invite all Hoses, or no more than one. Otherwise, some Hoses will be hurt. Find for Mehrdad the maximum possible total beauty of Hoses he can invite so that no one gets hurt and the total weight doesn't exceed w.
Input
The first line contains integers n, m and w (1 β€ n β€ 1000, <image>, 1 β€ w β€ 1000) β the number of Hoses, the number of pair of friends and the maximum total weight of those who are invited.
The second line contains n integers w1, w2, ..., wn (1 β€ wi β€ 1000) β the weights of the Hoses.
The third line contains n integers b1, b2, ..., bn (1 β€ bi β€ 106) β the beauties of the Hoses.
The next m lines contain pairs of friends, the i-th of them contains two integers xi and yi (1 β€ xi, yi β€ n, xi β yi), meaning that Hoses xi and yi are friends. Note that friendship is bidirectional. All pairs (xi, yi) are distinct.
Output
Print the maximum possible total beauty of Hoses Mehrdad can invite so that no one gets hurt and the total weight doesn't exceed w.
Examples
Input
3 1 5
3 2 5
2 4 2
1 2
Output
6
Input
4 2 11
2 4 6 6
6 4 2 1
1 2
2 3
Output
7
Note
In the first sample there are two friendship groups: Hoses {1, 2} and Hos {3}. The best way is to choose all of Hoses in the first group, sum of their weights is equal to 5 and sum of their beauty is 6.
In the second sample there are two friendship groups: Hoses {1, 2, 3} and Hos {4}. Mehrdad can't invite all the Hoses from the first group because their total weight is 12 > 11, thus the best way is to choose the first Hos from the first group and the only one from the second group. The total weight will be 8, and the total beauty will be 7.
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 literally dozens of snooker competitions held each year, and team Jinotega tries to attend them all (for some reason they prefer name "snookah")! When a competition takes place somewhere far from their hometown, Ivan, Artsem and Konstantin take a flight to the contest and back.
Jinotega's best friends, team Base have found a list of their itinerary receipts with information about departure and arrival airports. Now they wonder, where is Jinotega now: at home or at some competition far away? They know that:
* this list contains all Jinotega's flights in this year (in arbitrary order),
* Jinotega has only flown from his hometown to a snooker contest and back,
* after each competition Jinotega flies back home (though they may attend a competition in one place several times),
* and finally, at the beginning of the year Jinotega was at home.
Please help them to determine Jinotega's location!
Input
In the first line of input there is a single integer n: the number of Jinotega's flights (1 β€ n β€ 100). In the second line there is a string of 3 capital Latin letters: the name of Jinotega's home airport. In the next n lines there is flight information, one flight per line, in form "XXX->YYY", where "XXX" is the name of departure airport "YYY" is the name of arrival airport. Exactly one of these airports is Jinotega's home airport.
It is guaranteed that flights information is consistent with the knowledge of Jinotega's friends, which is described in the main part of the statement.
Output
If Jinotega is now at home, print "home" (without quotes), otherwise print "contest".
Examples
Input
4
SVO
SVO->CDG
LHR->SVO
SVO->LHR
CDG->SVO
Output
home
Input
3
SVO
SVO->HKT
HKT->SVO
SVO->RAP
Output
contest
Note
In the first sample Jinotega might first fly from SVO to CDG and back, and then from SVO to LHR and back, so now they should be at home. In the second sample Jinotega must now be at RAP because a flight from RAP back to SVO is not on the list.
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.
Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration <image>. Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration <image>. The drink should also be tasty, so the glass can contain only integer number of liters of each Coke type (some types can be not presented in the glass). Also, they want to minimize the total volume of Coke in the glass.
Carbon dioxide concentration is defined as the volume of carbone dioxide in the Coke divided by the total volume of Coke. When you mix two Cokes, the volume of carbon dioxide sums up, and the total volume of Coke sums up as well.
Help them, find the minimal natural number of liters needed to create a glass with carbon dioxide concentration <image>. Assume that the friends have unlimited amount of each Coke type.
Input
The first line contains two integers n, k (0 β€ n β€ 1000, 1 β€ k β€ 106) β carbon dioxide concentration the friends want and the number of Coke types.
The second line contains k integers a1, a2, ..., ak (0 β€ ai β€ 1000) β carbon dioxide concentration of each type of Coke. Some Coke types can have same concentration.
Output
Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration <image>, or -1 if it is impossible.
Examples
Input
400 4
100 300 450 500
Output
2
Input
50 2
100 25
Output
3
Note
In the first sample case, we can achieve concentration <image> using one liter of Coke of types <image> and <image>: <image>.
In the second case, we can achieve concentration <image> using two liters of <image> type and one liter of <image> type: <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.
Harry Potter is on a mission to destroy You-Know-Who's Horcruxes. The first Horcrux that he encountered in the Chamber of Secrets is Tom Riddle's diary. The diary was with Ginny and it forced her to open the Chamber of Secrets. Harry wants to know the different people who had ever possessed the diary to make sure they are not under its influence.
He has names of n people who possessed the diary in order. You need to tell, for each person, if he/she possessed the diary at some point before or not.
Formally, for a name si in the i-th line, output "YES" (without quotes) if there exists an index j such that si = sj and j < i, otherwise, output "NO" (without quotes).
Input
First line of input contains an integer n (1 β€ n β€ 100) β the number of names in the list.
Next n lines each contain a string si, consisting of lowercase English letters. The length of each string is between 1 and 100.
Output
Output n lines each containing either "YES" or "NO" (without quotes), depending on whether this string was already present in the stream or not.
You can print each letter in any case (upper or lower).
Examples
Input
6
tom
lucius
ginny
harry
ginny
harry
Output
NO
NO
NO
NO
YES
YES
Input
3
a
a
a
Output
NO
YES
YES
Note
In test case 1, for i = 5 there exists j = 3 such that si = sj and j < i, which means that answer for i = 5 is "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.
This time the Berland Team Olympiad in Informatics is held in a remote city that can only be reached by one small bus. Bus has n passenger seats, seat i can be occupied only by a participant from the city ai.
Today the bus has completed m trips, each time bringing n participants. The participants were then aligned in one line in the order they arrived, with people from the same bus standing in the order of their seats (i. e. if we write down the cities where the participants came from, we get the sequence a1, a2, ..., an repeated m times).
After that some teams were formed, each consisting of k participants form the same city standing next to each other in the line. Once formed, teams left the line. The teams were formed until there were no k neighboring participants from the same city.
Help the organizers determine how many participants have left in the line after that process ended. We can prove that answer doesn't depend on the order in which teams were selected.
Input
The first line contains three integers n, k and m (1 β€ n β€ 105, 2 β€ k β€ 109, 1 β€ m β€ 109).
The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105), where ai is the number of city, person from which must take seat i in the bus.
Output
Output the number of remaining participants in the line.
Examples
Input
4 2 5
1 2 3 1
Output
12
Input
1 9 10
1
Output
1
Input
3 2 10
1 2 1
Output
0
Note
In the second example, the line consists of ten participants from the same city. Nine of them will form a team. At the end, only one participant will stay in the line.
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.
Ivan's classes at the university have just finished, and now he wants to go to the local CFK cafe and eat some fried chicken.
CFK sells chicken chunks in small and large portions. A small portion contains 3 chunks; a large one β 7 chunks. Ivan wants to eat exactly x chunks. Now he wonders whether he can buy exactly this amount of chicken.
Formally, Ivan wants to know if he can choose two non-negative integers a and b in such a way that a small portions and b large ones contain exactly x chunks.
Help Ivan to answer this question for several values of x!
Input
The first line contains one integer n (1 β€ n β€ 100) β the number of testcases.
The i-th of the following n lines contains one integer xi (1 β€ xi β€ 100) β the number of chicken chunks Ivan wants to eat.
Output
Print n lines, in i-th line output YES if Ivan can buy exactly xi chunks. Otherwise, print NO.
Example
Input
2
6
5
Output
YES
NO
Note
In the first example Ivan can buy two small portions.
In the second example Ivan cannot buy exactly 5 chunks, since one small portion is not enough, but two small portions or one large is too much.
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 department of some software company has n servers of different specifications. Servers are indexed with consecutive integers from 1 to n. Suppose that the specifications of the j-th server may be expressed with a single integer number c_j of artificial resource units.
In order for production to work, it is needed to deploy two services S_1 and S_2 to process incoming requests using the servers of the department. Processing of incoming requests of service S_i takes x_i resource units.
The described situation happens in an advanced company, that is why each service may be deployed using not only one server, but several servers simultaneously. If service S_i is deployed using k_i servers, then the load is divided equally between these servers and each server requires only x_i / k_i (that may be a fractional number) resource units.
Each server may be left unused at all, or be used for deploying exactly one of the services (but not for two of them simultaneously). The service should not use more resources than the server provides.
Determine if it is possible to deploy both services using the given servers, and if yes, determine which servers should be used for deploying each of the services.
Input
The first line contains three integers n, x_1, x_2 (2 β€ n β€ 300 000, 1 β€ x_1, x_2 β€ 10^9) β the number of servers that the department may use, and resource units requirements for each of the services.
The second line contains n space-separated integers c_1, c_2, β¦, c_n (1 β€ c_i β€ 10^9) β the number of resource units provided by each of the servers.
Output
If it is impossible to deploy both services using the given servers, print the only word "No" (without the quotes).
Otherwise print the word "Yes" (without the quotes).
In the second line print two integers k_1 and k_2 (1 β€ k_1, k_2 β€ n) β the number of servers used for each of the services.
In the third line print k_1 integers, the indices of the servers that will be used for the first service.
In the fourth line print k_2 integers, the indices of the servers that will be used for the second service.
No index may appear twice among the indices you print in the last two lines. If there are several possible answers, it is allowed to print any of them.
Examples
Input
6 8 16
3 5 2 9 8 7
Output
Yes
3 2
1 2 6
5 4
Input
4 20 32
21 11 11 12
Output
Yes
1 3
1
2 3 4
Input
4 11 32
5 5 16 16
Output
No
Input
5 12 20
7 8 4 11 9
Output
No
Note
In the first sample test each of the servers 1, 2 and 6 will will provide 8 / 3 = 2.(6) resource units and each of the servers 5, 4 will provide 16 / 2 = 8 resource units.
In the second sample test the first server will provide 20 resource units and each of the remaining servers will provide 32 / 3 = 10.(6) resource units.
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 city of Fishtopia can be imagined as a grid of 4 rows and an odd number of columns. It has two main villages; the first is located at the top-left cell (1,1), people who stay there love fishing at the Tuna pond at the bottom-right cell (4, n). The second village is located at (4, 1) and its people love the Salmon pond at (1, n).
The mayor of Fishtopia wants to place k hotels in the city, each one occupying one cell. To allow people to enter the city from anywhere, hotels should not be placed on the border cells.
A person can move from one cell to another if those cells are not occupied by hotels and share a side.
Can you help the mayor place the hotels in a way such that there are equal number of shortest paths from each village to its preferred pond?
Input
The first line of input contain two integers, n and k (3 β€ n β€ 99, 0 β€ k β€ 2Γ(n-2)), n is odd, the width of the city, and the number of hotels to be placed, respectively.
Output
Print "YES", if it is possible to place all the hotels in a way that satisfies the problem statement, otherwise print "NO".
If it is possible, print an extra 4 lines that describe the city, each line should have n characters, each of which is "#" if that cell has a hotel on it, or "." if not.
Examples
Input
7 2
Output
YES
.......
.#.....
.#.....
.......
Input
5 3
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.
And again a misfortune fell on Poor Student. He is being late for an exam.
Having rushed to a bus stop that is in point (0, 0), he got on a minibus and they drove along a straight line, parallel to axis OX, in the direction of increasing x.
Poor Student knows the following:
* during one run the minibus makes n stops, the i-th stop is in point (xi, 0)
* coordinates of all the stops are different
* the minibus drives at a constant speed, equal to vb
* it can be assumed the passengers get on and off the minibus at a bus stop momentarily
* Student can get off the minibus only at a bus stop
* Student will have to get off the minibus at a terminal stop, if he does not get off earlier
* the University, where the exam will be held, is in point (xu, yu)
* Student can run from a bus stop to the University at a constant speed vs as long as needed
* a distance between two points can be calculated according to the following formula: <image>
* Student is already on the minibus, so, he cannot get off at the first bus stop
Poor Student wants to get to the University as soon as possible. Help him to choose the bus stop, where he should get off. If such bus stops are multiple, choose the bus stop closest to the University.
Input
The first line contains three integer numbers: 2 β€ n β€ 100, 1 β€ vb, vs β€ 1000. The second line contains n non-negative integers in ascending order: coordinates xi of the bus stop with index i. It is guaranteed that x1 equals to zero, and xn β€ 105. The third line contains the coordinates of the University, integers xu and yu, not exceeding 105 in absolute value.
Output
In the only line output the answer to the problem β index of the optimum bus stop.
Examples
Input
4 5 2
0 2 4 6
4 1
Output
3
Input
2 1 1
0 100000
100000 100000
Output
2
Note
As you know, students are a special sort of people, and minibuses usually do not hurry. That's why you should not be surprised, if Student's speed is higher than the speed of the minibus.
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 lowercase English letters.
If the length of S is at most K, print S without change.
If the length of S exceeds K, extract the first K characters in S, append `...` to the end of them, and print the result.
Constraints
* K is an integer between 1 and 100 (inclusive).
* S is a string consisting of lowercase English letters.
* The length of S is between 1 and 100 (inclusive).
Input
Input is given from Standard Input in the following format:
K
S
Output
Print a string as stated in Problem Statement.
Examples
Input
7
nikoandsolstice
Output
nikoand...
Input
40
ferelibenterhominesidquodvoluntcredunt
Output
ferelibenterhominesidquodvoluntcredunt
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 finite set of integers X, let f(X)=\max X - \min X.
Given are N integers A_1,...,A_N.
We will choose K of them and let S be the set of the integers chosen. If we distinguish elements with different indices even when their values are the same, there are {}_N C_K ways to make this choice. Find the sum of f(S) over all those ways.
Since the answer can be enormous, print it \bmod (10^9+7).
Constraints
* 1 \leq N \leq 10^5
* 1 \leq K \leq N
* |A_i| \leq 10^9
Input
Input is given from Standard Input in the following format:
N K
A_1 ... A_N
Output
Print the answer \bmod (10^9+7).
Examples
Input
4 2
1 1 3 4
Output
11
Input
6 3
10 10 10 -10 -10 -10
Output
360
Input
3 1
1 1 1
Output
0
Input
10 6
1000000000 1000000000 1000000000 1000000000 1000000000 0 0 0 0 0
Output
999998537
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 2028 and after a continuous growth, AtCoder Inc. finally built an empire with six cities (City 1, 2, 3, 4, 5, 6)!
There are five means of transport in this empire:
* Train: travels from City 1 to 2 in one minute. A train can occupy at most A people.
* Bus: travels from City 2 to 3 in one minute. A bus can occupy at most B people.
* Taxi: travels from City 3 to 4 in one minute. A taxi can occupy at most C people.
* Airplane: travels from City 4 to 5 in one minute. An airplane can occupy at most D people.
* Ship: travels from City 5 to 6 in one minute. A ship can occupy at most E people.
For each of them, one vehicle leaves the city at each integer time (time 0, 1, 2, ...).
There is a group of N people at City 1, and they all want to go to City 6.
At least how long does it take for all of them to reach there? You can ignore the time needed to transfer.
Constraints
* 1 \leq N, A, B, C, D, E \leq 10^{15}
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A
B
C
D
E
Output
Print the minimum time required for all of the people to reach City 6, in minutes.
Examples
Input
5
3
2
4
3
5
Output
7
Input
10
123
123
123
123
123
Output
5
Input
10000000007
2
3
5
7
11
Output
5000000008
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 is a train going from Station A to Station B that costs X yen (the currency of Japan).
Also, there is a bus going from Station B to Station C that costs Y yen.
Joisino got a special ticket. With this ticket, she can take the bus for half the fare if she travels from Station A to Station B by train and then travels from Station B to Station C by bus.
How much does it cost to travel from Station A to Station C if she uses this ticket?
Constraints
* 1 \leq X,Y \leq 100
* Y is an even number.
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
X Y
Output
If it costs x yen to travel from Station A to Station C, print x.
Examples
Input
81 58
Output
110
Input
4 54
Output
31
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.
Joisino wants to evaluate the formula "A op B". Here, A and B are integers, and the binary operator op is either `+` or `-`. Your task is to evaluate the formula instead of her.
Constraints
* 1β¦A,Bβ¦10^9
* op is either `+` or `-`.
Input
The input is given from Standard Input in the following format:
A op B
Output
Evaluate the formula and print the result.
Examples
Input
1 + 2
Output
3
Input
5 - 7
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 that extracts n different numbers from the numbers 0 to 100 and outputs the number of combinations that add up to s. Each n number is from 0 to 100, and the same number cannot be used in one combination. For example, if n is 3 and s is 6, the combination of the three numbers totaling 6 is
1 + 2 + 3 = 6
0 + 1 + 5 = 6
0 + 2 + 4 = 6
There are three ways.
Input
Given multiple datasets. For each dataset, n (1 β€ n β€ 9) and s (0 β€ s β€ 1000) are given on one line, separated by a space. When both n and s are 0, it is the end of the input.
The number of datasets does not exceed 50.
Output
For each dataset, output the number of combinations in which the sum of n integers is s on one line.
No input is given with more than 1010 combinations.
Example
Input
3 6
3 1
0 0
Output
3
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.
Ataru Oafoot of Aizu Gakuen University High School decided to play with a slot machine.
When you insert a medal on this machine, three reels will start spinning and each reel will stop automatically. In a normal game (normal game), 3 medals are inserted, and when the symbols are aligned, the following medals are obtained according to the symbols.
<image>
A special service will be started depending on how the patterns are aligned. A big bonus starts when you have 3 of 7 symbols, and you can play 5 bonus games. Also, when you have 3 BAR symbols, the regular bonus will start and you can play 3 bonus games.
If you have 3 star symbols, the free game will start and you will not be able to get medals, but you can start the next game without inserting medals.
During the bonus game, if you insert 2 medals per game, you will automatically get 3 grape patterns and 15 medals.
Oafoot started playing on the machine with 100 medals. After playing for a while, it ended in a normal game. How many medals did you have left?
Create a program that inputs play information and outputs the number of medals left at hand. The play information is given as the number of big bonuses b, the number of regular bonuses r, the number of grapes aligned during a normal game g, the number of cherries aligned c, the number of stars aligned s, and the total number of games t.
Note that t includes the number of bonus games. Also, medals will not disappear in the middle of the game.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a zero line. Each dataset is given in the following format:
b r g c s t
b, r, g, c, and s are integers greater than or equal to 0 and less than 200, and t is an integer less than or equal to 1000.
The number of datasets does not exceed 120.
Output
For each input dataset, the number of medals remaining at hand is output on one line.
Example
Input
3 2 30 3 26 226
9 0 18 3 20 118
5 5 12 2 15 203
7 4 19 2 22 197
7 4 24 4 17 209
0 0 0 0 0 0
Output
127
793
414
629
617
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.
Mr. Kobou found a bundle of old paper when he was cleaning his family home. On each paper, two series of numbers are written. Strange as it appeared to him, Mr. Kobou further went through the storehouse and found out a note his ancestor left. According to it, the bundle of paper is a treasure map, in which the two sequences of numbers seem to give a clue to the whereabouts of the treasure the ancestor buried.
Mr. Kobouβs ancestor divided the area where he buried his treasure in a reticular pattern and used only some of the grid sections. The two series of numbers indicate the locations: the $i$-th member of the first series indicates the number of locations in the $i$-th column (form left) of the grid sections where a part of the treasure is buried, and the $j$-th member of the second indicates the same information regarding the $j$-th row from the top. No more than one piece of treasure is buried in one grid section. An example of a 5 Γ 4 case is shown below. If the pieces of treasure are buried in the grid sections noted as "#" the two series of numbers become "0,2,2,1,1" and "1,1,1,3".
| 0| 2| 2| 1| 1
---|---|---|---|---|---
1| | | #| |
1| | #| | |
1| | | | | #
3| | #| #| #|
Mr. Kobouβs ancestor seems to be a very careful person. He slipped some pieces of paper with completely irrelevant information into the bundle. For example, a set of number series "3,2,3,0,0" and "4,2,0,0,2" does not match any combination of 5 Γ 5 matrixes. So, Mr. Kobou has first to exclude these pieces of garbage information.
Given the set of information written on the pieces of paper, make a program to judge if the information is relevant.
Input
The input is given in the following format.
$W$ $H$
$a_1$ $a_2$ $...$ $a_W$
$b_1$ $b_2$ $...$ $b_H$
The first line provides the number of horizontal partitions $W$ ($1 \leq W \leq 1000$) and vertical partitions $H$ ($1 \leq H \leq 1000$). The second line provides the $i$-th member of the first number series $a_i$ ($0 \leq a_i \leq H$) written on the paper, and the third line the $j$-th member of the second series $b_j$ ($0 \leq b_j \leq W$).
Output
Output "1" if the information written on the paper is relevant, or "0" otherwise.
Examples
Input
5 4
0 2 2 1 1
1 1 1 3
Output
1
Input
5 5
3 2 3 0 0
4 2 0 0 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.
Emacs is a text editor which is widely used by many programmers.
The advantage of Emacs is that we can move a cursor without arrow keys and the mice. For example, the cursor can be moved right, left, down, and up by pushing f, b, n, p with the Control Key respectively. In addition, cut-and-paste can be performed without the mouse.
Your task is to write a program which simulates key operations in the Emacs-like editor. The program should read a text and print the corresponding edited text.
The text consists of several lines and each line consists of zero or more alphabets and space characters. A line, which does not have any character, is a blank line.
The editor has a cursor which can point out a character or the end-of-line in the corresponding line. The cursor can also point out the end-of-line in a blank line.
In addition, the editor has a buffer which can hold either a string (a sequence of characters) or a linefeed.
The editor accepts the following set of commands (If the corresponding line is a blank line, the word "the first character" should be "the end-of-line"):
* a
Move the cursor to the first character of the current line.
* e
Move the cursor to the end-of-line of the current line.
* p
Move the cursor to the first character of the next upper line, if it exists.
If there is no line above the current line, move the cursor to the first character of the current line.
* n
Move the cursor to the first character of the next lower line, if it exists.
If there is no line below the current line, move the cursor to the first character of the current line.
* f
Move the cursor by one character to the right, unless the cursor points out the end-of-line.
If the cursor points out the end-of-line and there is a line below the current line, move the cursor to the first character of the next lower line. Otherwise, do nothing.
* b
Move the cursor by one character to the left, unless the cursor points out the first character.
If the cursor points out the first character and there is a line above the current line, move the cursor to the end-of-line of the next upper line. Otherwise, do nothing.
* d
If the cursor points out a character, delete the character (Characters and end-of-line next to the deleted character are shifted to the left).
If the cursor points out the end-of-line and there is a line below, the next lower line is appended to the end-of-line of the current line (Lines below the current line are shifted to the upper).
Otherwise, do nothing.
* k
If the cursor points out the end-of-line and there is a line below the current line, perform the command d mentioned above, and record a linefeed on the buffer.
If the cursor does not point out the end-of-line, cut characters between the cursor (inclusive) and the end-of-line, and record them on the buffer. After this operation, the cursor indicates the end-of-line of the current line.
* y
If the buffer is empty, do nothing.
If the buffer is holding a linefeed, insert the linefeed at the cursor. The cursor moves to the first character of the new line.
If the buffer is holding characters, insert the characters at the cursor. The cursor moves to the character or end-of-line which is originally pointed by the cursor.
The cursor position just after reading the text is the beginning of the first line, and the initial buffer is empty.
Constraints
* The number of lines in the text given as input β€ 10
* The number of characters in a line given as input β€ 20
* The number of commands β€ 300
* The maximum possible number of lines in the text during operations β€ 100
* The maximum possible number of characters in a line during operations β€ 1000
Input
The input consists of only one data-set which includes two parts. The first part gives a text consisting of several lines. The end of the text is indicated by a line (without quotes):
"END_OF_TEXT"
This line should not be included in the text.
Next part gives a series of commands. Each command is given in a line. The end of the commands is indicated by a character '-'.
Output
For the input text, print the text edited by the commands.
Example
Input
hyo
ni
END_OF_TEXT
f
d
f
f
k
p
p
e
y
a
k
y
y
n
y
-
Output
honihoni
honi
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 many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards.
A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table.
<image>
Figure E-1: Four blue cards and three red cards
For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three.
Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two.
Your job is to find the largest number of pairs that can be removed from the given set of cards on the table.
Input
The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows.
> m n
> b1 ... bk ... bm
> r1 ... rk ... rn
>
The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 β€ m β€ 500 and 1β€ n β€ 500. bk (1 β€ k β€ m) and rk (1 β€ k β€ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset.
The end of the input is indicated by a line containing two zeros separated by a space.
Output
For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs.
Sample Input
4 3
2 6 6 15
2 3 5
2 3
4 9
8 16 32
4 2
4 9 11 13
5 7
5 5
2 3 5 1001 1001
7 11 13 30 30
10 10
2 3 5 7 9 11 13 15 17 29
4 6 10 14 18 22 26 30 34 38
20 20
195 144 903 63 137 513 44 626 75 473
876 421 568 519 755 840 374 368 570 872
363 650 155 265 64 26 426 391 15 421
373 984 564 54 823 477 565 866 879 638
100 100
195 144 903 63 137 513 44 626 75 473
876 421 568 519 755 840 374 368 570 872
363 650 155 265 64 26 426 391 15 421
373 984 564 54 823 477 565 866 879 638
117 755 835 683 52 369 302 424 513 870
75 874 299 228 140 361 30 342 750 819
761 123 804 325 952 405 578 517 49 457
932 941 988 767 624 41 912 702 241 426
351 92 300 648 318 216 785 347 556 535
166 318 434 746 419 386 928 996 680 975
231 390 916 220 933 319 37 846 797 54
272 924 145 348 350 239 563 135 362 119
446 305 213 879 51 631 43 755 405 499
509 412 887 203 408 821 298 443 445 96
274 715 796 417 839 147 654 402 280 17
298 725 98 287 382 923 694 201 679 99
699 188 288 364 389 694 185 464 138 406
558 188 897 354 603 737 277 35 139 556
826 213 59 922 499 217 846 193 416 525
69 115 489 355 256 654 49 439 118 961
0 0
Output for the Sample Input
3
1
0
4
9
18
85
Example
Input
4 3
2 6 6 15
2 3 5
2 3
4 9
8 16 32
4 2
4 9 11 13
5 7
5 5
2 3 5 1001 1001
7 11 13 30 30
10 10
2 3 5 7 9 11 13 15 17 29
4 6 10 14 18 22 26 30 34 38
20 20
195 144 903 63 137 513 44 626 75 473
876 421 568 519 755 840 374 368 570 872
363 650 155 265 64 26 426 391 15 421
373 984 564 54 823 477 565 866 879 638
100 100
195 144 903 63 137 513 44 626 75 473
876 421 568 519 755 840 374 368 570 872
363 650 155 265 64 26 426 391 15 421
373 984 564 54 823 477 565 866 879 638
117 755 835 683 52 369 302 424 513 870
75 874 299 228 140 361 30 342 750 819
761 123 804 325 952 405 578 517 49 457
932 941 988 767 624 41 912 702 241 426
351 92 300 648 318 216 785 347 556 535
166 318 434 746 419 386 928 996 680 975
231 390 916 220 933 319 37 846 797 54
272 924 145 348 350 239 563 135 362 119
446 305 213 879 51 631 43 755 405 499
509 412 887 203 408 821 298 443 445 96
274 715 796 417 839 147 654 402 280 17
298 725 98 287 382 923 694 201 679 99
699 188 288 364 389 694 185 464 138 406
558 188 897 354 603 737 277 35 139 556
826 213 59 922 499 217 846 193 416 525
69 115 489 355 256 654 49 439 118 961
0 0
Output
3
1
0
4
9
18
85
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 earth is under an attack of a deadly virus. Luckily, prompt actions of the Ministry of Health against this emergency successfully confined the spread of the infection within a square grid of areas. Recently, public health specialists found an interesting pattern with regard to the transition of infected areas. At each step in time, every area in the grid changes its infection state according to infection states of its directly (horizontally, vertically, and diagonally) adjacent areas.
* An infected area continues to be infected if it has two or three adjacent infected areas.
* An uninfected area becomes infected if it has exactly three adjacent infected areas.
* An area becomes free of the virus, otherwise.
Your mission is to fight against the virus and disinfect all the areas. The Ministry of Health lets an anti-virus vehicle prototype under your command. The functionality of the vehicle is summarized as follows.
* At the beginning of each time step, you move the vehicle to one of the eight adjacent areas. The vehicle is not allowed to move to an infected area (to protect its operators from the virus). It is not allowed to stay in the same area.
* Following vehicle motion, all the areas, except for the area where the vehicle is in, change their infection states according to the transition rules described above.
Special functionality of the vehicle protects its area from virus infection even if the area is adjacent to exactly three infected areas. Unfortunately, this virus-protection capability of the vehicle does not last. Once the vehicle leaves the area, depending on the infection states of the adjacent areas, the area can be infected.
The area where the vehicle is in, which is uninfected, has the same effect to its adjacent areas as an infected area as far as the transition rules are concerned.
The following series of figures illustrate a sample scenario that successfully achieves the goal.
Initially, your vehicle denoted by @ is found at (1, 5) in a 5 Γ 5-grid of areas, and you see some infected areas which are denoted by #'s.
<image>
Firstly, at the beginning of time step 1, you move your vehicle diagonally to the southwest, that is, to the area (2, 4). Note that this vehicle motion was possible because this area was not infected at the start of time step 1.
Following this vehicle motion, infection state of each area changes according to the transition rules. The column "1-end" of the figure illustrates the result of such changes at the end of time step 1. Note that the area (3, 3) becomes infected because there were two adjacent infected areas and the vehicle was also in an adjacent area, three areas in total.
In time step 2, you move your vehicle to the west and position it at (2, 3).
Then infection states of other areas change. Note that even if your vehicle had exactly three infected adjacent areas (west, southwest, and south), the area that is being visited by the vehicle is not infected. The result of such changes at the end of time step 2 is as depicted in "2-end".
Finally, in time step 3, you move your vehicle to the east. After the change of the infection states, you see that all the areas have become virus free! This completely disinfected situation is the goal. In the scenario we have seen, you have successfully disinfected all the areas in three time steps by commanding the vehicle to move (1) southwest, (2) west, and (3) east.
Your mission is to find the length of the shortest sequence(s) of vehicle motion commands that can successfully disinfect all the areas.
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing a single zero. Each dataset is formatted as follows.
n
a11 a12 ... a1n
a21 a22 ... a2n
...
an1 an2 ... ann
Here, n is the size of the grid. That means that the grid is comprised of n Γ n areas. You may assume 1 β€ n β€ 5. The rest of the dataset consists of n lines of n letters. Each letter aij specifies the state of the area at the beginning: '#' for infection, '.' for free of virus, and '@' for the initial location of the vehicle. The only character that can appear in a line is '#', '.', or '@'. Among n Γ n areas, there exists exactly one area which has '@'.
Output
For each dataset, output the minimum number of time steps that is required to disinfect all the areas. If there exists no motion command sequence that leads to complete disinfection, output -1. The output should not contain any other extra character.
Examples
Input
3
...
.@.
...
3
.##
.#.
@##
3
##.
#..
@..
5
....@
##...
#....
...#.
##.##
5
#...#
...#.
#....
...##
..@..
5
#....
.....
.....
.....
..@..
5
#..#.
#.#.#
.#.#.
....#
.#@##
5
..##.
..#..
#....
#....
.#@..
0
Output
0
10
-1
3
2
1
6
4
Input
3
...
.@.
...
3
.##
.#.
@##
3
.
..
@..
5
....@
...
....
...#.
.##
5
...#
...#.
....
...##
..@..
5
....
.....
.....
.....
..@..
5
..#.
.#.#
.#.#.
....#
.#@##
5
..##.
..#..
....
....
.#@..
0
Output
0
10
-1
3
2
1
6
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.
We understand that reading English is a great pain to many of you. So weβll keep this problem statememt simple. Write a program that reports the point equally distant from a set of lines given as the input. In case of no solutions or multiple solutions, your program should report as such.
Input
The input consists of multiple datasets. Each dataset is given in the following format:
n
x1,1 y1,1 x1,2 y1,2
x2,1 y2,1 x2,2 y2,2
...
xn,1 yn,1 xn,2 yn,2
n is the number of lines (1 β€ n β€ 100); (xi,1, yi,1) and (xi,2, yi,2) denote the different points the i-th line passes through. The lines do not coincide each other. The coordinates are all integers between -10000 and 10000.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print a line as follows. If there is exactly one point equally distant from all the given lines, print the x- and y-coordinates in this order with a single space between them. If there is more than one such point, just print "Many" (without quotes). If there is none, just print "None" (without quotes).
The coordinates may be printed with any number of digits after the decimal point, but should be accurate to 10-4.
Example
Input
2
-35 -35 100 100
-49 49 2000 -2000
4
0 0 0 3
0 0 3 0
0 3 3 3
3 0 3 3
4
0 3 -4 6
3 0 6 -4
2 3 6 6
-1 2 -4 6
0
Output
Many
1.5000 1.5000
1.000 1.000
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 statement
A programming contest will be held in the Russian Federation. The contest has N questions and has M participants. Question i has a score a_i, and it is known that participant j's ability is b_j. For problem i and participant j, participant j can always solve problem i if a_i β€ b_j and only then. The score of a participant through the contest is the sum of the scores of the problems that the person was able to solve. Participant j also sets a target score c_j for this contest.
Determine if each participant can score more than the target score.
input
The input is given in the following format.
N
a_ {0} a_ {1} a_ {2}β¦ a_ {Nβ1}
M
b_ {0} b_ {1} b_ {2}β¦ b_ {Mβ1}
c_ {0} c_ {1} c_ {2}β¦ c_ {Mβ1}
Constraint
* All inputs are integers
* 1 \ β€ N \ β€ 300 \,000
* 1 \ β€ M \ β€ 300 \,000
* 0 \ β€ a_ {i} \ β€ 1 \, 000 \,000
* 0 \ β€ b_ {i} \ β€ 1 \, 000 \,000
* 0 \ β€ c_ {i} \ β€ βa_k
output
Print the answer on line M. On the i-line, output `Yes` if the participant i-1 can get more points than the target score, and` No` if not.
sample
Sample input 1
6
1 2 1 3 4 5
7
1 3 4 5 3 1 0
2 4 5 3 4 5 3
Sample output 1
Yes
Yes
Yes
Yes
Yes
No
No
The points obtained by each participant are as follows.
* Participant 0: 1 + 1 = 2
* Participant 1: 1 + 2 + 1 + 3 = 7
* Participant 2: 1 + 2 + 1 + 3 + 4 = 11
* Participant 3: 1 + 2 + 1 + 3 + 4 + 5 = 16
* Participant 4: 1 + 2 + 1 + 3 = 7
* Participant 5: 1 + 1 = 2
* Participants 6: 0
Sample input 2
8
1 1 2 3 3 4 6 100
Four
1 3 4 99
1 10 15 120
Sample output 2
Yes
Yes
No
No
Example
Input
6
1 2 1 3 4 5
7
1 3 4 5 3 1 0
2 4 5 3 4 5 3
Output
Yes
Yes
Yes
Yes
Yes
No
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.
D: Sunburn-Suntan-
story
Aizunyan is a second-year student who belongs to the programming contest club of Wakagamatsu High School, commonly known as the Prokon club. Cute like an angel. Aizu Nyan is planning to participate in this summer festival, so I made a schedule for the band to go to listen to. I'm worried about sunburn here. All live performances are held outdoors, but Aizu Nyan has a constitution that makes it easy to get sunburned, so if you are exposed to too much ultraviolet rays outdoors for a long time, you will get sunburned immediately. I plan to avoid UV rays as much as possible by evacuating indoors while there is no live performance, but during the live performance, it will inevitably hit the sun. Therefore, Aizu Nyan thought about taking measures against ultraviolet rays by applying sunscreen.
problem
If you apply sunscreen, you can get the effect for T minutes from the time you apply it. Sunscreen can only be applied once, so I want to use it effectively. Aizu Nyan is outdoors from the start time to the end time of the live, and is indoors at other times. You'll be given a live schedule that Aizu Nyan will listen to, so find the maximum amount of time you can get the sunscreen effect while you're outdoors.
Input format
The input can be given in the following format.
T
N
s_1 t_1
...
s_N t_N
The first line is given an integer T that represents the time it takes to get the sunscreen effect. The second line is given an integer N that represents the number of live concerts Aizu Nyan listens to. The following N lines are given the integer s_i, which represents the start time of the live that Aizu Nyan listens to thi, and the integer t_i, which represents the end time, separated by spaces.
Constraint
* 1 β€ T β€ 10 ^ {15}
* 1 β€ N β€ 10 ^ 5
* 0 β€ s_i <t_i β€ 10 ^ {15} (1 β€ i β€ N)
* The start time of the (i + 1) th live is the same as or later than the end time of the i-th live. That is, t_i β€ s_ {i + 1} (1 β€ i <N)
output
Print one line for the maximum amount of time you can get the sunscreen effect while you're outdoors.
Input example 1
20
1
0 10
Output example 1
Ten
Input example 2
20
1
0 100
Output example 2
20
Input example 3
9
3
1 5
9 11
13 20
Output example 3
7
Input example 4
twenty five
Five
9 12
15 20
21 25
28 40
45 60
Output example 4
twenty one
Example
Input
20
1
0 10
Output
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.
problem
There is a mysterious device $ M $, and if you put Tanuki and Fox in this device, one animal will come out from the device (hereinafter, Tanuki will be $ T $ and Fox will be $ F $).
$ M (x, y) $ represents an animal that came out by putting animals in the device $ M $ in the order of $ x, y $.
As a result of various trials, the following was found.
$ M (T, T) = T $
$ M (T, F) = F $
$ M (F, T) = T $
$ M (F, F) = T $
You put the animals in a row $ P_1, P_2, ..., P_N $ into the device as follows.
$ M (.... M (M (P_1, P_2), P_3) ...., P_N) $
Answer the last animal that appears.
output
Output the last animal character $ T $ or $ F $, and also a newline at the end.
Example
Input
3
F T T
Output
T
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: Find the difference
problem
Given rectangular boards A and B with N squares vertically and M squares horizontally. For each board, each square is painted white or black.
The color of the square in the i-th row and j-th column of the board X is written as C (i, j, X).
Count how many pairs of integers (i, j) meet the following conditions:
* 1 \ leq i \ leq N
* 1 \ leq j \ leq M
* C (i, j, A) \ neq C (i, j, B)
Input format
N M
A_1_1
...
A_N
B_1_1
...
B_N
When the jth character of A_i (1 \ leq i \ leq N) is `#`, C (i, j, A) is black, and when it is `.`, C (i, j, A) is white. Represents that. The same is true for B.
Constraint
* 1 \ leq N, M \ leq 2,000
* | A_i | = | B_i | = M
* A_i and B_i are strings consisting only of `#` and `.`
Output format
Print the answer on one line.
Input example 1
twenty three
.. #
..
.##
..
Output example 1
2
Input example 2
7 28
............................
... # ..... ### ... #### .... ### ..
.. #. # ... # ... # ... # ... # .... # ... #.
. # ... # ... # ...... #### ... # .....
. ##### .. # ... # .. # ...... # ... #.
. # ... # ... ### ... # ....... ### ..
............................
............................
.. ### .... ### ..... ## .... ### ..
. # ... # ... # ... # ... #. # ... # ... #.
...##...#...# ..... # .... ####.
.. # ..... # ... # ..... # ....... #.
. ##### ... ### .... #### ... ### ..
............................
Output example 2
40
Example
Input
2 3
..#
##.
.##
#..
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.
For a given weighted undirected graph G(V, E), find the distance of the shortest route that meets the following criteria:
* It is a closed cycle where it ends at the same point it starts.
* The route must go through every edge at least once.
Constraints
* 2 β€ |V| β€ 15
* 0 β€ |E| β€ 1,000
* 0 β€ di β€ 1,000
* si β ti
* The graph is connected
Input
|V| |E|
s0 t0 d0
s1 t1 d1
:
s|E|-1 t|E|-1 d|E|-1
, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively.
si and ti represent source and target verticess of i-th edge (undirected) and di represents the distance between si and ti (the i-th edge).
Note that there can be multiple edges between a pair of vertices.
Output
Print the shortest distance in a line.
Examples
Input
4 4
0 1 1
0 2 2
1 3 3
2 3 4
Output
10
Input
4 5
0 1 1
0 2 2
1 3 3
2 3 4
1 2 5
Output
18
Input
2 3
0 1 1
0 1 2
0 1 3
Output
7
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 least common multiple (LCM) of given n integers.
Constraints
* 2 β€ n β€ 10
* 1 β€ ai β€ 1000
* Product of given integers ai(i = 1, 2, ... n) does not exceed 231-1
Input
n
a1 a2 ... an
n is given in the first line. Then, n integers are given in the second line.
Output
Print the least common multiple of the given integers in a line.
Examples
Input
3
3 4 6
Output
12
Input
4
1 2 3 5
Output
30
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 a long day, Alice and Bob decided to play a little game. The game board consists of n cells in a straight line, numbered from 1 to n, where each cell contains a number a_i between 1 and n. Furthermore, no two cells contain the same number.
A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell i to cell j only if the following two conditions are satisfied:
* the number in the new cell j must be strictly larger than the number in the old cell i (i.e. a_j > a_i), and
* the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. |i-j|mod a_i = 0).
Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players.
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the number of numbers.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n). Furthermore, there are no pair of indices i β j such that a_i = a_j.
Output
Print s β a string of n characters, where the i-th character represents the outcome of the game if the token is initially placed in the cell i. If Alice wins, then s_i has to be equal to "A"; otherwise, s_i has to be equal to "B".
Examples
Input
8
3 6 5 4 2 7 1 8
Output
BAAAABAB
Input
15
3 11 2 5 10 9 7 13 15 8 4 12 6 1 14
Output
ABAAAABBBAABAAB
Note
In the first sample, if Bob puts the token on the number (not position):
* 1: Alice can move to any number. She can win by picking 7, from which Bob has no move.
* 2: Alice can move to 3 and 5. Upon moving to 5, Bob can win by moving to 8. If she chooses 3 instead, she wins, as Bob has only a move to 4, from which Alice can move to 8.
* 3: Alice can only move to 4, after which Bob wins by moving to 8.
* 4, 5, or 6: Alice wins by moving to 8.
* 7, 8: Alice has no move, and hence she loses immediately.
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 a set of n weights. You know that their masses are a_1, a_2, ..., a_n grams, but you don't know which of them has which mass. You can't distinguish the weights.
However, your friend does know the mass of each weight. You can ask your friend to give you exactly k weights with the total mass m (both parameters k and m are chosen by you), and your friend will point to any valid subset of weights, if it is possible.
You are allowed to make this query only once. Find the maximum possible number of weights you can reveal after this query.
Input
The first line contains a single integer n (1 β€ n β€ 100) β the number of weights.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 100) β the masses of the weights.
Output
Print the maximum number of weights you can learn the masses for after making a single query.
Examples
Input
4
1 4 2 2
Output
2
Input
6
1 2 4 4 4 9
Output
2
Note
In the first example we can ask for a subset of two weights with total mass being equal to 4, and the only option is to get \{2, 2\}.
Another way to obtain the same result is to ask for a subset of two weights with the total mass of 5 and get \{1, 4\}. It is easy to see that the two remaining weights have mass of 2 grams each.
In the second example we can ask for a subset of two weights with total mass being 8, and the only answer is \{4, 4\}. We can prove it is not possible to learn masses for three weights in one query, but we won't put the proof here.
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 Sofia is in fourth grade. Today in the geometry lesson she learned about segments and squares. On the way home, she decided to draw n squares in the snow with a side length of 1. For simplicity, we assume that Sofia lives on a plane and can draw only segments of length 1, parallel to the coordinate axes, with vertices at integer points.
In order to draw a segment, Sofia proceeds as follows. If she wants to draw a vertical segment with the coordinates of the ends (x, y) and (x, y+1). Then Sofia looks if there is already a drawn segment with the coordinates of the ends (x', y) and (x', y+1) for some x'. If such a segment exists, then Sofia quickly draws a new segment, using the old one as a guideline. If there is no such segment, then Sofia has to take a ruler and measure a new segment for a long time. Same thing happens when Sofia wants to draw a horizontal segment, but only now she checks for the existence of a segment with the same coordinates x, x+1 and the differing coordinate y.
For example, if Sofia needs to draw one square, she will have to draw two segments using a ruler:
<image>
After that, she can draw the remaining two segments, using the first two as a guide:
<image>
If Sofia needs to draw two squares, she will have to draw three segments using a ruler:
<image>
After that, she can draw the remaining four segments, using the first three as a guide:
<image>
Sofia is in a hurry, so she wants to minimize the number of segments that she will have to draw with a ruler without a guide. Help her find this minimum number.
Input
The only line of input contains a single integer n (1 β€ n β€ 10^{9}), the number of squares that Sofia wants to draw.
Output
Print single integer, the minimum number of segments that Sofia will have to draw with a ruler without a guide in order to draw n squares in the manner described above.
Examples
Input
1
Output
2
Input
2
Output
3
Input
4
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 an array a_1, a_2, ..., a_n. All a_i are pairwise distinct.
Let's define function f(l, r) as follows:
* let's define array b_1, b_2, ..., b_{r - l + 1}, where b_i = a_{l - 1 + i};
* sort array b in increasing order;
* result of the function f(l, r) is β_{i = 1}^{r - l + 1}{b_i β
i}.
Calculate \left(β_{1 β€ l β€ r β€ n}{f(l, r)}\right) mod (10^9+7), i.e. total sum of f for all subsegments of a modulo 10^9+7.
Input
The first line contains one integer n (1 β€ n β€ 5 β
10^5) β the length of array a.
The second line contains n integers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9, a_i β a_j for i β j) β array a.
Output
Print one integer β the total sum of f for all subsegments of a modulo 10^9+7
Examples
Input
4
5 2 4 7
Output
167
Input
3
123456789 214365879 987654321
Output
582491518
Note
Description of the first example:
* f(1, 1) = 5 β
1 = 5;
* f(1, 2) = 2 β
1 + 5 β
2 = 12;
* f(1, 3) = 2 β
1 + 4 β
2 + 5 β
3 = 25;
* f(1, 4) = 2 β
1 + 4 β
2 + 5 β
3 + 7 β
4 = 53;
* f(2, 2) = 2 β
1 = 2;
* f(2, 3) = 2 β
1 + 4 β
2 = 10;
* f(2, 4) = 2 β
1 + 4 β
2 + 7 β
3 = 31;
* f(3, 3) = 4 β
1 = 4;
* f(3, 4) = 4 β
1 + 7 β
2 = 18;
* f(4, 4) = 7 β
1 = 7;
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.
Vus the [Cossack](https://en.wikipedia.org/wiki/Cossacks) holds a programming competition, in which n people participate. He decided to award them all with pens and notebooks. It is known that Vus has exactly m pens and k notebooks.
Determine whether the Cossack can reward all participants, giving each of them at least one pen and at least one notebook.
Input
The first line contains three integers n, m, and k (1 β€ n, m, k β€ 100) β the number of participants, the number of pens, and the number of notebooks respectively.
Output
Print "Yes" if it possible to reward all the participants. Otherwise, print "No".
You can print each letter in any case (upper or lower).
Examples
Input
5 8 6
Output
Yes
Input
3 9 3
Output
Yes
Input
8 5 20
Output
No
Note
In the first example, there are 5 participants. The Cossack has 8 pens and 6 notebooks. Therefore, he has enough pens and notebooks.
In the second example, there are 3 participants. The Cossack has 9 pens and 3 notebooks. He has more than enough pens but only the minimum needed number of notebooks.
In the third example, there are 8 participants but only 5 pens. Since the Cossack does not have enough pens, the answer is "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.
Alice and Bob play a game. Initially they have a string s_1, s_2, ..., s_n, consisting of only characters . and X. They take alternating turns, and Alice is moving first. During each turn, the player has to select a contiguous substring consisting only of characters . and replaces each of them with X. Alice must select a substing of length a, and Bob must select a substring of length b. It is guaranteed that a > b.
For example, if s = ...X.. and a = 3, b = 2, then after Alice's move string can turn only into XXXX... And if it's Bob's turn and the string s = ...X.., then after Bob's move the string can turn into XX.X.., .XXX.. or ...XXX.
Whoever is unable to make a move, loses. You have to determine who wins if they both play optimally.
You have to answer q independent queries.
Input
The first line contains one integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
The first line of each query contains two integers a and b (1 β€ b < a β€ 3 β
10^5).
The second line of each query contains the string s (1 β€ |s| β€ 3 β
10^5), consisting of only characters . and X.
It is guaranteed that sum of all |s| over all queries not exceed 3 β
10^5.
Output
For each test case print YES if Alice can win 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
3
3 2
XX......XX...X
4 2
X...X.X..X
5 3
.......X..X
Output
YES
NO
YES
Note
In the first query Alice can select substring s_3 ... s_5. After that s turns into XXXXX...XX...X. After that, no matter what move Bob makes, Alice can make the move (this will be her second move), but Bob can't make his second move.
In the second query Alice can not win because she cannot even make one move.
In the third query Alice can choose substring s_2 ... s_6. After that s turns into .XXXXX.X..X, and Bob can't make a move after that.
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.
Gardener Alexey teaches competitive programming to high school students. To congratulate Alexey on the Teacher's Day, the students have gifted him a collection of wooden sticks, where every stick has an integer length. Now Alexey wants to grow a tree from them.
The tree looks like a polyline on the plane, consisting of all sticks. The polyline starts at the point (0, 0). While constructing the polyline, Alexey will attach sticks to it one by one in arbitrary order. Each stick must be either vertical or horizontal (that is, parallel to OX or OY axis). It is not allowed for two consecutive sticks to be aligned simultaneously horizontally or simultaneously vertically. See the images below for clarification.
Alexey wants to make a polyline in such a way that its end is as far as possible from (0, 0). Please help him to grow the tree this way.
Note that the polyline defining the form of the tree may have self-intersections and self-touches, but it can be proved that the optimal answer does not contain any self-intersections or self-touches.
Input
The first line contains an integer n (1 β€ n β€ 100 000) β the number of sticks Alexey got as a present.
The second line contains n integers a_1, β¦, a_n (1 β€ a_i β€ 10 000) β the lengths of the sticks.
Output
Print one integer β the square of the largest possible distance from (0, 0) to the tree end.
Examples
Input
3
1 2 3
Output
26
Input
4
1 1 2 2
Output
20
Note
The following pictures show optimal trees for example tests. The squared distance in the first example equals 5 β
5 + 1 β
1 = 26, and in the second example 4 β
4 + 2 β
2 = 20.
<image> <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.
Adilbek was assigned to a special project. For Adilbek it means that he has n days to run a special program and provide its results. But there is a problem: the program needs to run for d days to calculate the results.
Fortunately, Adilbek can optimize the program. If he spends x (x is a non-negative integer) days optimizing the program, he will make the program run in \leftβ (d)/(x + 1) \rightβ days (\leftβ a \rightβ is the ceiling function: \leftβ 2.4 \rightβ = 3, \leftβ 2 \rightβ = 2). The program cannot be run and optimized simultaneously, so the total number of days he will spend is equal to x + \leftβ (d)/(x + 1) \rightβ.
Will Adilbek be able to provide the generated results in no more than n days?
Input
The first line contains a single integer T (1 β€ T β€ 50) β the number of test cases.
The next T lines contain test cases β one per line. Each line contains two integers n and d (1 β€ n β€ 10^9, 1 β€ d β€ 10^9) β the number of days before the deadline and the number of days the program runs.
Output
Print T answers β one per test case. For each test case print YES (case insensitive) if Adilbek can fit in n days or NO (case insensitive) otherwise.
Example
Input
3
1 1
4 5
5 11
Output
YES
YES
NO
Note
In the first test case, Adilbek decides not to optimize the program at all, since d β€ n.
In the second test case, Adilbek can spend 1 day optimizing the program and it will run \leftβ 5/2 \rightβ = 3 days. In total, he will spend 4 days and will fit in the limit.
In the third test case, it's impossible to fit in the limit. For example, if Adilbek will optimize the program 2 days, it'll still work \leftβ (11)/(2+1) \rightβ = 4 days.
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.
Input
The only line of the input contains a 7-digit hexadecimal number. The first "digit" of the number is letter A, the rest of the "digits" are decimal digits 0-9.
Output
Output a single integer.
Examples
Input
A278832
Output
0
Input
A089956
Output
0
Input
A089957
Output
1
Input
A144045
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.
Vasya claims that he had a paper square. He cut it into two rectangular parts using one vertical or horizontal cut. Then Vasya informed you the dimensions of these two rectangular parts. You need to check whether Vasya originally had a square. In other words, check if it is possible to make a square using two given rectangles.
Input
The first line contains an integer t (1 β€ t β€ 10^4) β the number of test cases in the input. Then t test cases follow.
Each test case is given in two lines.
The first line contains two integers a_1 and b_1 (1 β€ a_1, b_1 β€ 100) β the dimensions of the first one obtained after cutting rectangle. The sizes are given in random order (that is, it is not known which of the numbers is the width, and which of the numbers is the length).
The second line contains two integers a_2 and b_2 (1 β€ a_2, b_2 β€ 100) β the dimensions of the second obtained after cutting rectangle. The sizes are given in random order (that is, it is not known which of the numbers is the width, and which of the numbers is the length).
Output
Print t answers, each of which is a string "YES" (in the case of a positive answer) or "NO" (in the case of a negative answer). The letters in words can be printed in any case (upper or lower).
Example
Input
3
2 3
3 1
3 2
1 3
3 3
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.
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.
In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.
Du will chat in the group for n days. On the i-th day:
* If Du can speak, he'll make fun of Boboniu with fun factor a_i. But after that, he may be muzzled depending on Boboniu's mood.
* Otherwise, Du won't do anything.
Boboniu's mood is a constant m. On the i-th day:
* If Du can speak and a_i>m, then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the i+1, i+2, β
β
β
, min(i+d,n)-th days.
* Otherwise, Boboniu won't do anything.
The total fun factor is the sum of the fun factors on the days when Du can speak.
Du asked you to find the maximum total fun factor among all possible permutations of a.
Input
The first line contains three integers n, d and m (1β€ dβ€ nβ€ 10^5,0β€ mβ€ 10^9).
The next line contains n integers a_1, a_2, β¦,a_n (0β€ a_iβ€ 10^9).
Output
Print one integer: the maximum total fun factor among all permutations of a.
Examples
Input
5 2 11
8 10 15 23 5
Output
48
Input
20 2 16
20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7
Output
195
Note
In the first example, you can set a'=[15, 5, 8, 10, 23]. Then Du's chatting record will be:
1. Make fun of Boboniu with fun factor 15.
2. Be muzzled.
3. Be muzzled.
4. Make fun of Boboniu with fun factor 10.
5. Make fun of Boboniu with fun factor 23.
Thus the total fun factor is 48.
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 this problem MEX of a certain array is the smallest positive integer not contained in this array.
Everyone knows this definition, including Lesha. But Lesha loves MEX, so he comes up with a new problem involving MEX every day, including today.
You are given an array a of length n. Lesha considers all the non-empty subarrays of the initial array and computes MEX for each of them. Then Lesha computes MEX of the obtained numbers.
An array b is a subarray of an array a, if b can be obtained from a by deletion of several (possible none or all) elements from the beginning and several (possibly none or all) elements from the end. In particular, an array is a subarray of itself.
Lesha understands that the problem is very interesting this time, but he doesn't know how to solve it. Help him and find the MEX of MEXes of all the subarrays!
Input
The first line contains a single integer n (1 β€ n β€ 10^5) β the length of the array.
The next line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β the elements of the array.
Output
Print a single integer β the MEX of MEXes of all subarrays.
Examples
Input
3
1 3 2
Output
3
Input
5
1 4 3 1 2
Output
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.
Ron is a happy owner of a permutation a of length n.
A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
<image>
Ron's permutation is subjected to m experiments of the following type: (r_i, p_i). This means that elements in range [1, r_i] (in other words, the prefix of length r_i) have to be sorted in ascending order with the probability of p_i. All experiments are performed in the same order in which they are specified in the input data.
As an example, let's take a look at a permutation [4, 2, 1, 5, 3] and an experiment (3, 0.6). After such an experiment with the probability of 60\% the permutation will assume the form [1, 2, 4, 5, 3] and with a 40\% probability it will remain unchanged.
You have to determine the probability of the permutation becoming completely sorted in ascending order after m experiments.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case contains two integers n and m (1 β€ n, m β€ 10^5) β the length of the permutation and the number of experiments, respectively.
The second line of each test case contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β contents of the permutation.
The following m lines of each test case each contain an integer r_i and a real number p_i (1 β€ r_i β€ n, 0 β€ p_i β€ 1) β the length of the prefix and the probability of it being sorted. All probabilities are given with at most 6 decimal places.
It is guaranteed that the sum of n and the sum of m does not exceed 10^5 (β n, β m β€ 10^5).
Output
For each test case, print a single number β the probability that after all experiments the permutation becomes sorted in ascending order. Your answer will be considered correct if its absolute or relative error does not exceed 10^{-6}.
Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} β€ 10^{-6}.
Example
Input
4
4 3
4 3 2 1
1 0.3
3 1
4 0.6
5 3
4 2 1 3 5
3 0.8
4 0.6
5 0.3
6 5
1 3 2 4 5 6
4 0.9
5 0.3
2 0.4
6 0.7
3 0.5
4 2
1 2 3 4
2 0.5
4 0.1
Output
0.600000
0.720000
0.989500
1.000000
Note
Explanation of the first test case: It can be demonstrated that whether the final permutation is sorted or not depends solely on sorting being performed in the (4, 0.6) experiment.
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 given an array a of length n. Find a subarray a[l..r] with length at least k with the largest median.
A median in an array of length n is an element which occupies position number β (n + 1)/(2) β after we sort the elements in non-decreasing order. For example: median([1, 2, 3, 4]) = 2, median([3, 2, 1]) = 2, median([2, 1, 2, 1]) = 1.
Subarray a[l..r] is a contiguous part of the array a, i. e. the array a_l,a_{l+1},β¦,a_r for some 1 β€ l β€ r β€ n, its length is r - l + 1.
Input
The first line contains two integers n and k (1 β€ k β€ n β€ 2 β
10^5).
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n).
Output
Output one integer m β the maximum median you can get.
Examples
Input
5 3
1 2 3 2 1
Output
2
Input
4 2
1 2 3 4
Output
3
Note
In the first example all the possible subarrays are [1..3], [1..4], [1..5], [2..4], [2..5] and [3..5] and the median for all of them is 2, so the maximum possible median is 2 too.
In the second example median([3..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.
Diana loves playing with numbers. She's got n cards with positive integer numbers a_i written on them. She spends her free time multiplying the numbers on the cards. She picks a non-empty subset of the cards and multiplies all the numbers a_i written on them.
Diana is happy when the product of the numbers ends with her favorite digit d. Now she is curious what cards she should pick so that the product of the numbers on them is the largest possible and the last decimal digit of the product is d. Please, help her.
Input
The first line contains the integers n and d (1β€ nβ€ 10^5, 0β€ dβ€ 9). The second line contains n integers a_i (1β€ a_iβ€ 1000).
Output
On the first line, print the number of chosen cards k (1β€ kβ€ n). On the next line, print the numbers written on the chosen cards in any order.
If it is impossible to choose a subset of cards with the product that ends with the digit d, print the single line with -1.
Examples
Input
6 4
4 11 8 2 1 13
Output
5
1 2 4 11 13
Input
3 1
2 4 6
Output
-1
Input
5 7
1 3 1 5 3
Output
-1
Input
6 3
8 9 4 17 11 5
Output
3
9 11 17
Input
5 6
2 2 2 2 2
Output
4
2 2 2 2
Note
In the first example, 1 Γ 2 Γ 4 Γ 11 Γ 13 = 1144, which is the largest product that ends with the digit 4. The same set of cards without the number 1 is also a valid answer, as well as a set of 8, 11, and 13 with or without 1 that also has the product of 1144.
In the second example, all the numbers on the cards are even and their product cannot end with an odd digit 1.
In the third example, the only possible products are 1, 3, 5, 9, 15, and 45, none of which end with the digit 7.
In the fourth example, 9 Γ 11 Γ 17 = 1683, which ends with the digit 3.
In the fifth example, 2 Γ 2 Γ 2 Γ 2 = 16, which ends with the digit 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.
Vasya the Great Magician and Conjurer loves all kinds of miracles and wizardry. In one wave of a magic wand he can turn an object into something else. But, as you all know, there is no better magic in the Universe than the magic of numbers. That's why Vasya adores math and spends a lot of time turning some numbers into some other ones.
This morning he has n cards with integers lined up in front of him. Each integer is not less than 1, but not greater than l. When Vasya waves his magic wand, two rightmost cards vanish from the line and a new card magically appears in their place. It contains the difference between the left and the right numbers on the two vanished cards. Vasya was very interested to know what would happen next, and so he waved with his magic wand on and on, until the table had a single card left.
Suppose that Vasya originally had the following cards: 4, 1, 1, 3 (listed from left to right). Then after the first wave the line would be: 4, 1, -2, and after the second one: 4, 3, and after the third one the table would have a single card with number 1.
Please note that in spite of the fact that initially all the numbers on the cards were not less than 1 and not greater than l, the numbers on the appearing cards can be anything, no restrictions are imposed on them.
It is now evening. Vasya is very tired and wants to return everything back, but does not remember which cards he had in the morning. He only remembers that there were n cards, they contained integers from 1 to l, and after all magical actions he was left with a single card containing number d.
Help Vasya to recover the initial set of cards with numbers.
Input
The single line contains three space-separated integers: n (2 β€ n β€ 100) β the initial number of cards on the table, d (|d| β€ 104) β the number on the card that was left on the table after all the magical actions, and l (1 β€ l β€ 100) β the limits for the initial integers.
Output
If Vasya is mistaken, that is, if there doesn't exist a set that meets the requirements given in the statement, then print a single number -1, otherwise print the sought set containing n integers from 1 to l. Separate the integers by spaces. Print the integers in the order, in which they were written on the cards from left to right. If there are several suitable sets of numbers, you can print any of them.
Examples
Input
3 3 2
Output
2 1 2
Input
5 -4 3
Output
-1
Input
5 -4 4
Output
2 4 1 4 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.
Vasya has got many devices that work on electricity. He's got n supply-line filters to plug the devices, the i-th supply-line filter has ai sockets.
Overall Vasya has got m devices and k electrical sockets in his flat, he can plug the devices or supply-line filters directly. Of course, he can plug the supply-line filter to any other supply-line filter. The device (or the supply-line filter) is considered plugged to electricity if it is either plugged to one of k electrical sockets, or if it is plugged to some supply-line filter that is in turn plugged to electricity.
What minimum number of supply-line filters from the given set will Vasya need to plug all the devices he has to electricity? Note that all devices and supply-line filters take one socket for plugging and that he can use one socket to plug either one device or one supply-line filter.
Input
The first line contains three integers n, m, k (1 β€ n, m, k β€ 50) β the number of supply-line filters, the number of devices and the number of sockets that he can plug to directly, correspondingly. The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 50) β number ai stands for the number of sockets on the i-th supply-line filter.
Output
Print a single number β the minimum number of supply-line filters that is needed to plug all the devices to electricity. If it is impossible to plug all the devices even using all the supply-line filters, print -1.
Examples
Input
3 5 3
3 1 2
Output
1
Input
4 7 2
3 3 2 4
Output
2
Input
5 5 1
1 3 1 2 1
Output
-1
Note
In the first test case he can plug the first supply-line filter directly to electricity. After he plug it, he get 5 (3 on the supply-line filter and 2 remaining sockets for direct plugging) available sockets to plug. Thus, one filter is enough to plug 5 devices.
One of the optimal ways in the second test sample is to plug the second supply-line filter directly and plug the fourth supply-line filter to one of the sockets in the second supply-line filter. Thus, he gets exactly 7 sockets, available to plug: one to plug to the electricity directly, 2 on the second supply-line filter, 4 on the fourth supply-line filter. There's no way he can plug 7 devices if he use one supply-line filter.
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 two rectangles on a plane. The centers of both rectangles are located in the origin of coordinates (meaning the center of the rectangle's symmetry). The first rectangle's sides are parallel to the coordinate axes: the length of the side that is parallel to the Ox axis, equals w, the length of the side that is parallel to the Oy axis, equals h. The second rectangle can be obtained by rotating the first rectangle relative to the origin of coordinates by angle Ξ±.
<image>
Your task is to find the area of the region which belongs to both given rectangles. This region is shaded in the picture.
Input
The first line contains three integers w, h, Ξ± (1 β€ w, h β€ 106; 0 β€ Ξ± β€ 180). Angle Ξ± is given in degrees.
Output
In a single line print a real number β the area of the region which belongs to both given rectangles.
The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.
Examples
Input
1 1 45
Output
0.828427125
Input
6 4 30
Output
19.668384925
Note
The second sample has been drawn on the picture above.
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.
Yaroslav likes algorithms. We'll describe one of his favorite algorithms.
1. The algorithm receives a string as the input. We denote this input string as a.
2. The algorithm consists of some number of command. Π‘ommand number i looks either as si >> wi, or as si <> wi, where si and wi are some possibly empty strings of length at most 7, consisting of digits and characters "?".
3. At each iteration, the algorithm looks for a command with the minimum index i, such that si occurs in a as a substring. If this command is not found the algorithm terminates.
4. Let's denote the number of the found command as k. In string a the first occurrence of the string sk is replaced by string wk. If the found command at that had form sk >> wk, then the algorithm continues its execution and proceeds to the next iteration. Otherwise, the algorithm terminates.
5. The value of string a after algorithm termination is considered to be the output of the algorithm.
Yaroslav has a set of n positive integers, he needs to come up with his favorite algorithm that will increase each of the given numbers by one. More formally, if we consider each number as a string representing the decimal representation of the number, then being run on each of these strings separately, the algorithm should receive the output string that is a recording of the corresponding number increased by one.
Help Yaroslav.
Input
The first line contains integer n (1 β€ n β€ 100) β the number of elements in the set. The next n lines contains one positive integer each. All the given numbers are less than 1025.
Output
Print the algorithm which can individually increase each number of the set. In the i-th line print the command number i without spaces.
Your algorithm will be launched for each of these numbers. The answer will be considered correct if:
* Each line will a correct algorithm command (see the description in the problem statement).
* The number of commands should not exceed 50.
* The algorithm will increase each of the given numbers by one.
* To get a respond, the algorithm will perform no more than 200 iterations for each number.
Examples
Input
2
10
79
Output
10<>11
79<>80
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.
According to the regulations of Berland's army, a reconnaissance unit should consist of exactly two soldiers. Since these two soldiers shouldn't differ much, their heights can differ by at most d centimeters. Captain Bob has n soldiers in his detachment. Their heights are a1, a2, ..., an centimeters. Some soldiers are of the same height. Bob wants to know, how many ways exist to form a reconnaissance unit of two soldiers from his detachment.
Ways (1, 2) and (2, 1) should be regarded as different.
Input
The first line contains two integers n and d (1 β€ n β€ 1000, 1 β€ d β€ 109) β amount of soldiers in Bob's detachment and the maximum allowed height difference respectively. The second line contains n space-separated integers β heights of all the soldiers in Bob's detachment. These numbers don't exceed 109.
Output
Output one number β amount of ways to form a reconnaissance unit of two soldiers, whose height difference doesn't exceed d.
Examples
Input
5 10
10 20 50 60 65
Output
6
Input
5 1
55 30 29 31 55
Output
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.
Alexey, a merry Berland entrant, got sick of the gray reality and he zealously wants to go to university. There are a lot of universities nowadays, so Alexey is getting lost in the diversity β he has not yet decided what profession he wants to get. At school, he had bad grades in all subjects, and it's only thanks to wealthy parents that he was able to obtain the graduation certificate.
The situation is complicated by the fact that each high education institution has the determined amount of voluntary donations, paid by the new students for admission β ni berubleys. He cannot pay more than ni, because then the difference between the paid amount and ni can be regarded as a bribe!
Each rector is wearing the distinctive uniform of his university. Therefore, the uniform's pockets cannot contain coins of denomination more than ri. The rector also does not carry coins of denomination less than li in his pocket β because if everyone pays him with so small coins, they gather a lot of weight and the pocket tears. Therefore, a donation can be paid only by coins of denomination x berubleys, where li β€ x β€ ri (Berland uses coins of any positive integer denomination). Alexey can use the coins of different denominations and he can use the coins of the same denomination any number of times. When Alexey was first confronted with such orders, he was puzzled because it turned out that not all universities can accept him! Alexey is very afraid of going into the army (even though he had long wanted to get the green uniform, but his dad says that the army bullies will beat his son and he cannot pay to ensure the boy's safety). So, Alexey wants to know for sure which universities he can enter so that he could quickly choose his alma mater.
Thanks to the parents, Alexey is not limited in money and we can assume that he has an unlimited number of coins of each type.
In other words, you are given t requests, each of them contains numbers ni, li, ri. For each query you need to answer, whether it is possible to gather the sum of exactly ni berubleys using only coins with an integer denomination from li to ri berubleys. You can use coins of different denominations. Coins of each denomination can be used any number of times.
Input
The first line contains the number of universities t, (1 β€ t β€ 1000) Each of the next t lines contain three space-separated integers: ni, li, ri (1 β€ ni, li, ri β€ 109; li β€ ri).
Output
For each query print on a single line: either "Yes", if Alexey can enter the university, or "No" otherwise.
Examples
Input
2
5 2 3
6 4 5
Output
Yes
No
Note
You can pay the donation to the first university with two coins: one of denomination 2 and one of denomination 3 berubleys. The donation to the second university cannot be paid.
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 X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A and B. The following two conditions must be satisfied:
* If number x belongs to set A, then number a - x must also belong to set A.
* If number x belongs to set B, then number b - x must also belong to set B.
Help Little X divide the numbers into two sets or determine that it's impossible.
Input
The first line contains three space-separated integers n, a, b (1 β€ n β€ 105; 1 β€ a, b β€ 109). The next line contains n space-separated distinct integers p1, p2, ..., pn (1 β€ pi β€ 109).
Output
If there is a way to divide the numbers into two sets, then print "YES" in the first line. Then print n integers: b1, b2, ..., bn (bi equals either 0, or 1), describing the division. If bi equals to 0, then pi belongs to set A, otherwise it belongs to set B.
If it's impossible, print "NO" (without the quotes).
Examples
Input
4 5 9
2 3 4 5
Output
YES
0 0 1 1
Input
3 3 4
1 2 4
Output
NO
Note
It's OK if all the numbers are in the same set, and the other one is empty.
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 lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.
Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).
After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.
Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.
Input
The first line contains integer n (2 β€ n β€ 2Β·105) β the number of students in the queue.
Then n lines follow, i-th line contains the pair of integers ai, bi (0 β€ ai, bi β€ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist.
The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order.
Output
Print a sequence of n integers x1, x2, ..., xn β the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.
Examples
Input
4
92 31
0 7
31 0
7 141
Output
92 7 31 141
Note
The picture illustrates the queue for the first sample.
<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.
There are n Imperial stormtroopers on the field. The battle field is a plane with Cartesian coordinate system. Each stormtrooper is associated with his coordinates (x, y) on this plane.
Han Solo has the newest duplex lazer gun to fight these stormtroopers. It is situated at the point (x0, y0). In one shot it can can destroy all the stormtroopers, situated on some line that crosses point (x0, y0).
Your task is to determine what minimum number of shots Han Solo needs to defeat all the stormtroopers.
The gun is the newest invention, it shoots very quickly and even after a very large number of shots the stormtroopers don't have enough time to realize what's happening and change their location.
Input
The first line contains three integers n, x0 ΠΈ y0 (1 β€ n β€ 1000, - 104 β€ x0, y0 β€ 104) β the number of stormtroopers on the battle field and the coordinates of your gun.
Next n lines contain two integers each xi, yi ( - 104 β€ xi, yi β€ 104) β the coordinates of the stormtroopers on the battlefield. It is guaranteed that no stormtrooper stands at the same point with the gun. Multiple stormtroopers can stand at the same point.
Output
Print a single integer β the minimum number of shots Han Solo needs to destroy all the stormtroopers.
Examples
Input
4 0 0
1 1
2 2
2 0
-1 -1
Output
2
Input
2 1 2
1 1
1 0
Output
1
Note
Explanation to the first and second samples from the statement, respectively:
<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 play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice.
The level of the cave where you are is a rectangular square grid of n rows and m columns. Each cell consists either from intact or from cracked ice. From each cell you can move to cells that are side-adjacent with yours (due to some limitations of the game engine you cannot make jumps on the same place, i.e. jump from a cell to itself). If you move to the cell with cracked ice, then your character falls down through it and if you move to the cell with intact ice, then the ice on this cell becomes cracked.
Let's number the rows with integers from 1 to n from top to bottom and the columns with integers from 1 to m from left to right. Let's denote a cell on the intersection of the r-th row and the c-th column as (r, c).
You are staying in the cell (r1, c1) and this cell is cracked because you've just fallen here from a higher level. You need to fall down through the cell (r2, c2) since the exit to the next level is there. Can you do this?
Input
The first line contains two integers, n and m (1 β€ n, m β€ 500) β the number of rows and columns in the cave description.
Each of the next n lines describes the initial state of the level of the cave, each line consists of m characters "." (that is, intact ice) and "X" (cracked ice).
The next line contains two integers, r1 and c1 (1 β€ r1 β€ n, 1 β€ c1 β€ m) β your initial coordinates. It is guaranteed that the description of the cave contains character 'X' in cell (r1, c1), that is, the ice on the starting cell is initially cracked.
The next line contains two integers r2 and c2 (1 β€ r2 β€ n, 1 β€ c2 β€ m) β the coordinates of the cell through which you need to fall. The final cell may coincide with the starting one.
Output
If you can reach the destination, print 'YES', otherwise print 'NO'.
Examples
Input
4 6
X...XX
...XX.
.X..X.
......
1 6
2 2
Output
YES
Input
5 4
.X..
...X
X.X.
....
.XX.
5 3
1 1
Output
NO
Input
4 7
..X.XX.
.XX..X.
X...X..
X......
2 2
1 6
Output
YES
Note
In the first sample test one possible path is:
<image>
After the first visit of cell (2, 2) the ice on it cracks and when you step there for the second time, your character falls through the ice as intended.
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.
Limak is a little polar bear. In the snow he found a scroll with the ancient prophecy. Limak doesn't know any ancient languages and thus is unable to understand the prophecy. But he knows digits!
One fragment of the prophecy is a sequence of n digits. The first digit isn't zero. Limak thinks that it's a list of some special years. It's hard to see any commas or spaces, so maybe ancient people didn't use them. Now Limak wonders what years are listed there.
Limak assumes three things:
* Years are listed in the strictly increasing order;
* Every year is a positive integer number;
* There are no leading zeros.
Limak is going to consider all possible ways to split a sequence into numbers (years), satisfying the conditions above. He will do it without any help. However, he asked you to tell him the number of ways to do so. Since this number may be very large, you are only asked to calculate it modulo 109 + 7.
Input
The first line of the input contains a single integer n (1 β€ n β€ 5000) β the number of digits.
The second line contains a string of digits and has length equal to n. It's guaranteed that the first digit is not '0'.
Output
Print the number of ways to correctly split the given sequence modulo 109 + 7.
Examples
Input
6
123434
Output
8
Input
8
20152016
Output
4
Note
In the first sample there are 8 ways to split the sequence:
* "123434" = "123434" (maybe the given sequence is just one big number)
* "123434" = "1" + "23434"
* "123434" = "12" + "3434"
* "123434" = "123" + "434"
* "123434" = "1" + "23" + "434"
* "123434" = "1" + "2" + "3434"
* "123434" = "1" + "2" + "3" + "434"
* "123434" = "1" + "2" + "3" + "4" + "34"
Note that we don't count a split "123434" = "12" + "34" + "34" because numbers have to be strictly increasing.
In the second sample there are 4 ways:
* "20152016" = "20152016"
* "20152016" = "20" + "152016"
* "20152016" = "201" + "52016"
* "20152016" = "2015" + "2016"
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.
Blake is a CEO of a large company called "Blake Technologies". He loves his company very much and he thinks that his company should be the best. That is why every candidate needs to pass through the interview that consists of the following problem.
We define function f(x, l, r) as a bitwise OR of integers xl, xl + 1, ..., xr, where xi is the i-th element of the array x. You are given two arrays a and b of length n. You need to determine the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 β€ l β€ r β€ n.
<image>
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the length of the arrays.
The second line contains n integers ai (0 β€ ai β€ 109).
The third line contains n integers bi (0 β€ bi β€ 109).
Output
Print a single integer β the maximum value of sum f(a, l, r) + f(b, l, r) among all possible 1 β€ l β€ r β€ n.
Examples
Input
5
1 2 4 3 2
2 3 3 12 1
Output
22
Input
10
13 2 7 11 8 4 9 8 5 1
5 7 18 9 2 3 0 11 8 6
Output
46
Note
Bitwise OR of two non-negative integers a and b is the number c = a OR b, such that each of its digits in binary notation is 1 if and only if at least one of a or b have 1 in the corresponding position in binary notation.
In the first sample, one of the optimal answers is l = 2 and r = 4, because f(a, 2, 4) + f(b, 2, 4) = (2 OR 4 OR 3) + (3 OR 3 OR 12) = 7 + 15 = 22. Other ways to get maximum value is to choose l = 1 and r = 4, l = 1 and r = 5, l = 2 and r = 4, l = 2 and r = 5, l = 3 and r = 4, or l = 3 and r = 5.
In the second sample, the maximum value is obtained for l = 1 and r = 9.
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.
Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all programmers do, he loves the famous drink "Beecola", which can be bought in n different shops in the city. It's known that the price of one bottle in the shop i is equal to xi coins.
Vasiliy plans to buy his favorite drink for q consecutive days. He knows, that on the i-th day he will be able to spent mi coins. Now, for each of the days he want to know in how many different shops he can buy a bottle of "Beecola".
Input
The first line of the input contains a single integer n (1 β€ n β€ 100 000) β the number of shops in the city that sell Vasiliy's favourite drink.
The second line contains n integers xi (1 β€ xi β€ 100 000) β prices of the bottles of the drink in the i-th shop.
The third line contains a single integer q (1 β€ q β€ 100 000) β the number of days Vasiliy plans to buy the drink.
Then follow q lines each containing one integer mi (1 β€ mi β€ 109) β the number of coins Vasiliy can spent on the i-th day.
Output
Print q integers. The i-th of them should be equal to the number of shops where Vasiliy will be able to buy a bottle of the drink on the i-th day.
Example
Input
5
3 10 8 6 11
4
1
10
3
11
Output
0
4
1
5
Note
On the first day, Vasiliy won't be able to buy a drink in any of the shops.
On the second day, Vasiliy can buy a drink in the shops 1, 2, 3 and 4.
On the third day, Vasiliy can buy a drink only in the shop number 1.
Finally, on the last day Vasiliy can buy a drink in any shop.
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 integers from 1 to n. Exactly once you apply the following operation to this permutation: pick a random segment and shuffle its elements. Formally:
1. Pick a random segment (continuous subsequence) from l to r. All <image> segments are equiprobable.
2. Let k = r - l + 1, i.e. the length of the chosen segment. Pick a random permutation of integers from 1 to k, p1, p2, ..., pk. All k! permutation are equiprobable.
3. This permutation is applied to elements of the chosen segment, i.e. permutation a1, a2, ..., al - 1, al, al + 1, ..., ar - 1, ar, ar + 1, ..., an is transformed to a1, a2, ..., al - 1, al - 1 + p1, al - 1 + p2, ..., al - 1 + pk - 1, al - 1 + pk, ar + 1, ..., an.
Inversion if a pair of elements (not necessary neighbouring) with the wrong relative order. In other words, the number of inversion is equal to the number of pairs (i, j) such that i < j and ai > aj. Find the expected number of inversions after we apply exactly one operation mentioned above.
Input
The first line contains a single integer n (1 β€ n β€ 100 000) β the length of the permutation.
The second line contains n distinct integers from 1 to n β elements of the permutation.
Output
Print one real value β the expected number of inversions. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 9.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.
Example
Input
3
2 3 1
Output
1.916666666666666666666666666667
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.