question
large_stringlengths 265
13.2k
|
|---|
Solve the programming task below in a Python markdown code block.
On Bertown's main street n trees are growing, the tree number i has the height of ai meters (1 β€ i β€ n). By the arrival of the President of Berland these trees were decided to be changed so that their heights formed a beautiful sequence. This means that the heights of trees on ends (the 1st one and the n-th one) should be equal to each other, the heights of the 2-nd and the (n - 1)-th tree must also be equal to each other, at that the height of the 2-nd tree should be larger than the height of the first tree by 1, and so on. In other words, the heights of the trees, standing at equal distance from the edge (of one end of the sequence) must be equal to each other, and with the increasing of the distance from the edge by 1 the tree height must also increase by 1. For example, the sequences "2 3 4 5 5 4 3 2" and "1 2 3 2 1" are beautiful, and '1 3 3 1" and "1 2 3 1" are not.
Changing the height of a tree is a very expensive operation, using advanced technologies invented by Berland scientists. In one operation you can choose any tree and change its height to any number, either increase or decrease. Note that even after the change the height should remain a positive integer, i. e, it can't be less than or equal to zero. Identify the smallest number of changes of the trees' height needed for the sequence of their heights to become beautiful.
Input
The first line contains integer n (1 β€ n β€ 105) which is the number of trees. The second line contains integers ai (1 β€ ai β€ 105) which are the heights of the trees.
Output
Print a single number which is the minimal number of trees whose heights will have to be changed for the sequence to become beautiful.
Examples
Input
3
2 2 2
Output
1
Input
4
1 2 2 1
Output
0
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Do you know the story about the three musketeers? Anyway, you must help them now.
Richelimakieu is a cardinal in the city of Bearis. He found three brave warriors and called them the three musketeers. Athos has strength a, Borthos strength b, and Caramis has strength c.
The year 2015 is almost over and there are still n criminals to be defeated. The i-th criminal has strength ti. It's hard to defeat strong criminals β maybe musketeers will have to fight together to achieve it.
Richelimakieu will coordinate musketeers' actions. In each hour each musketeer can either do nothing or be assigned to one criminal. Two or three musketeers can be assigned to the same criminal and then their strengths are summed up. A criminal can be defeated in exactly one hour (also if two or three musketeers fight him). Richelimakieu can't allow the situation where a criminal has strength bigger than the sum of strengths of musketeers fighting him β a criminal would win then!
In other words, there are three ways to defeat a criminal.
* A musketeer of the strength x in one hour can defeat a criminal of the strength not greater than x. So, for example Athos in one hour can defeat criminal i only if ti β€ a.
* Two musketeers can fight together and in one hour defeat a criminal of the strength not greater than the sum of strengths of these two musketeers. So, for example Athos and Caramis in one hour can defeat criminal i only if ti β€ a + c. Note that the third remaining musketeer can either do nothing or fight some other criminal.
* Similarly, all three musketeers can fight together and in one hour defeat a criminal of the strength not greater than the sum of musketeers' strengths, i.e. ti β€ a + b + c.
Richelimakieu doesn't want musketeers to fight during the New Year's Eve. Thus, he must coordinate their actions in order to minimize the number of hours till all criminals will be defeated.
Find the minimum number of hours to defeat all criminals. If musketeers can't defeat them all then print "-1" (without the quotes) instead.
Input
The first line of the input contains a single integer n (1 β€ n β€ 200 000) β the number of criminals.
The second line contains three integers a, b and c (1 β€ a, b, c β€ 108) β strengths of musketeers.
The third line contains n integers t1, t2, ..., tn (1 β€ ti β€ 108) β strengths of criminals.
Output
Print one line with the answer.
If it's impossible to defeat all criminals, print "-1" (without the quotes). Otherwise, print the minimum number of hours the three musketeers will spend on defeating all criminals.
Examples
Input
5
10 20 30
1 1 1 1 50
Output
2
Input
5
10 20 30
1 1 1 1 51
Output
3
Input
7
30 20 10
34 19 50 33 88 15 20
Output
-1
Input
6
10 5 10
10 9 5 25 20 5
Output
3
Note
In the first sample Athos has strength 10, Borthos 20, and Caramis 30. They can defeat all criminals in two hours:
* Borthos and Caramis should together fight a criminal with strength 50. In the same hour Athos can fight one of four criminals with strength 1.
* There are three criminals left, each with strength 1. Each musketeer can fight one criminal in the second hour.
In the second sample all three musketeers must together fight a criminal with strength 51. It takes one hour. In the second hour they can fight separately, each with one criminal. In the third hour one criminal is left and any of musketeers can fight him.
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.
Kris works in a large company "Blake Technologies". As a best engineer of the company he was assigned a task to develop a printer that will be able to print horizontal and vertical strips. First prototype is already built and Kris wants to tests it. He wants you to implement the program that checks the result of the printing.
Printer works with a rectangular sheet of paper of size n Γ m. Consider the list as a table consisting of n rows and m columns. Rows are numbered from top to bottom with integers from 1 to n, while columns are numbered from left to right with integers from 1 to m. Initially, all cells are painted in color 0.
Your program has to support two operations:
1. Paint all cells in row ri in color ai;
2. Paint all cells in column ci in color ai.
If during some operation i there is a cell that have already been painted, the color of this cell also changes to ai.
Your program has to print the resulting table after k operation.
Input
The first line of the input contains three integers n, m and k (1 β€ n, m β€ 5000, nΒ·m β€ 100 000, 1 β€ k β€ 100 000) β the dimensions of the sheet and the number of operations, respectively.
Each of the next k lines contains the description of exactly one query:
* 1 ri ai (1 β€ ri β€ n, 1 β€ ai β€ 109), means that row ri is painted in color ai;
* 2 ci ai (1 β€ ci β€ m, 1 β€ ai β€ 109), means that column ci is painted in color ai.
Output
Print n lines containing m integers each β the resulting table after all operations are applied.
Examples
Input
3 3 3
1 1 3
2 2 1
1 2 2
Output
3 1 3
2 2 2
0 1 0
Input
5 3 5
1 1 1
1 3 1
1 5 1
2 1 1
2 3 1
Output
1 1 1
1 0 1
1 1 1
1 0 1
1 1 1
Note
The figure below shows all three operations for the first sample step by step. The cells that were painted on the corresponding step are marked gray.
<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.
Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help.
Vasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only operation he is allowed to do is to reverse any of them (first character becomes last, second becomes one before last and so on).
To reverse the i-th string Vasiliy has to spent ci units of energy. He is interested in the minimum amount of energy he has to spent in order to have strings sorted in lexicographical order.
String A is lexicographically smaller than string B if it is shorter than B (|A| < |B|) and is its prefix, or if none of them is a prefix of the other and at the first position where they differ character in A is smaller than the character in B.
For the purpose of this problem, two equal strings nearby do not break the condition of sequence being sorted lexicographically.
Input
The first line of the input contains a single integer n (2 β€ n β€ 100 000) β the number of strings.
The second line contains n integers ci (0 β€ ci β€ 109), the i-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the i-th string.
Then follow n lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed 100 000.
Output
If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print - 1. Otherwise, print the minimum total amount of energy Vasiliy has to spent.
Examples
Input
2
1 2
ba
ac
Output
1
Input
3
1 3 1
aa
ba
ac
Output
1
Input
2
5 5
bbb
aaa
Output
-1
Input
2
3 3
aaa
aa
Output
-1
Note
In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller.
In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is - 1.
In the fourth sample, both strings consists of characters 'a' only, but in the sorted order string "aa" should go before string "aaa", thus 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.
There are n workers in a company, each of them has a unique id from 1 to n. Exaclty one of them is a chief, his id is s. Each worker except the chief has exactly one immediate superior.
There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate superior, the immediate superior of the his immediate superior, and so on. For example, if there are three workers in the company, from which the first is the chief, the second worker's immediate superior is the first, the third worker's immediate superior is the second, then the third worker has two superiors, one of them is immediate and one not immediate. The chief is a superior to all the workers except himself.
Some of the workers were in a hurry and made a mistake. You are to find the minimum number of workers that could make a mistake.
Input
The first line contains two positive integers n and s (1 β€ n β€ 2Β·105, 1 β€ s β€ n) β the number of workers and the id of the chief.
The second line contains n integers a1, a2, ..., an (0 β€ ai β€ n - 1), where ai is the number of superiors (not only immediate) the worker with id i reported about.
Output
Print the minimum number of workers that could make a mistake.
Examples
Input
3 2
2 0 2
Output
1
Input
5 3
1 0 0 4 1
Output
2
Note
In the first example it is possible that only the first worker made a mistake. Then:
* the immediate superior of the first worker is the second worker,
* the immediate superior of the third worker is the first worker,
* the second worker is the chief.
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 is an array of n positive integers, all of which are not greater than n.
You have to process q queries to this array. Each query is represented by two numbers p and k. Several operations are performed in each query; each operation changes p to p + ap + k. There operations are applied until p becomes greater than n. The answer to the query is the number of performed operations.
Input
The first line contains one integer n (1 β€ n β€ 100000).
The second line contains n integers β elements of a (1 β€ ai β€ n for each i from 1 to n).
The third line containts one integer q (1 β€ q β€ 100000).
Then q lines follow. Each line contains the values of p and k for corresponding query (1 β€ p, k β€ n).
Output
Print q integers, ith integer must be equal to the answer to ith query.
Example
Input
3
1 1 1
3
1 1
2 1
3 1
Output
2
1
1
Note
Consider first example:
In first query after first operation p = 3, after second operation p = 5.
In next two queries p is greater than n after the first operation.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
There are n students who have taken part in an olympiad. Now it's time to award the students.
Some of them will receive diplomas, some wiil get certificates, and others won't receive anything. Students with diplomas and certificates are called winners. But there are some rules of counting the number of diplomas and certificates. The number of certificates must be exactly k times greater than the number of diplomas. The number of winners must not be greater than half of the number of all students (i.e. not be greater than half of n). It's possible that there are no winners.
You have to identify the maximum possible number of winners, according to these rules. Also for this case you have to calculate the number of students with diplomas, the number of students with certificates and the number of students who are not winners.
Input
The first (and the only) line of input contains two integers n and k (1 β€ n, k β€ 1012), where n is the number of students and k is the ratio between the number of certificates and the number of diplomas.
Output
Output three numbers: the number of students with diplomas, the number of students with certificates and the number of students who are not winners in case when the number of winners is maximum possible.
It's possible that there are no winners.
Examples
Input
18 2
Output
3 6 9
Input
9 10
Output
0 0 9
Input
1000000000000 5
Output
83333333333 416666666665 500000000002
Input
1000000000000 499999999999
Output
1 499999999999 500000000000
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.
You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to x.
More formally, there is a singly liked list built on an array of n elements. Element with index i contains two integers: valuei is the integer value in this element, and nexti that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if nexti β - 1, then valuenexti > valuei.
You are given the number of elements in the list n, the index of the first element start, and the integer x.
You can make up to 2000 queries of the following two types:
* ? i (1 β€ i β€ n) β ask the values valuei and nexti,
* ! ans β give the answer for the problem: the minimum integer, greater than or equal to x, or ! -1, if there are no such integers. Your program should terminate after this query.
Write a program that solves this problem.
Input
The first line contains three integers n, start, x (1 β€ n β€ 50000, 1 β€ start β€ n, 0 β€ x β€ 109) β the number of elements in the list, the index of the first element and the integer x.
Output
To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to x, or -1, if there is no such integer.
Interaction
To make a query of the first type, print ? i (1 β€ i β€ n), where i is the index of element you want to know information about.
After each query of type ? read two integers valuei and nexti (0 β€ valuei β€ 109, - 1 β€ nexti β€ n, nexti β 0).
It is guaranteed that if nexti β - 1, then valuenexti > valuei, and that the array values give a valid singly linked list with start being the first element.
Note that you can't ask more than 1999 queries of the type ?.
If nexti = - 1 and valuei = - 1, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream.
Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer.
To flush you can use (just after printing a query and line end):
* fflush(stdout) in C++;
* System.out.flush() in Java;
* stdout.flush() in Python;
* flush(output) in Pascal;
* For other languages see documentation.
Hacks format
For hacks, use the following format:
In the first line print three integers n, start, x (1 β€ n β€ 50000, 1 β€ start β€ n, 0 β€ x β€ 109).
In the next n lines print the description of the elements of the list: in the i-th line print two integers valuei and nexti (0 β€ valuei β€ 109, - 1 β€ nexti β€ n, nexti β 0).
The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from start by following links nexti, and the last element end should have -1 in the nextend.
Example
Input
5 3 80
97 -1
58 5
16 2
81 1
79 4
Output
? 1
? 2
? 3
? 4
? 5
! 81
Note
You can read more about singly linked list by the following link: <https://en.wikipedia.org/wiki/Linked_list#Singly_linked_list>
The illustration for the first sample case. Start and finish elements are marked dark. <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 are given a set of n points on the plane. A line containing the origin is called good, if projection of the given set to this line forms a symmetric multiset of points. Find the total number of good lines.
Multiset is a set where equal elements are allowed.
Multiset is called symmetric, if there is a point P on the plane such that the multiset is [centrally symmetric](https://en.wikipedia.org/wiki/Point_reflection) in respect of point P.
Input
The first line contains a single integer n (1 β€ n β€ 2000) β the number of points in the set.
Each of the next n lines contains two integers xi and yi ( - 106 β€ xi, yi β€ 106) β the coordinates of the points. It is guaranteed that no two points coincide.
Output
If there are infinitely many good lines, print -1.
Otherwise, print single integer β the number of good lines.
Examples
Input
3
1 2
2 1
3 3
Output
3
Input
2
4 3
1 2
Output
-1
Note
Picture to the first sample test:
<image>
In the second sample, any line containing the origin is 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.
Since Grisha behaved well last year, at New Year's Eve he was visited by Ded Moroz who brought an enormous bag of gifts with him! The bag contains n sweet candies from the good ol' bakery, each labeled from 1 to n corresponding to its tastiness. No two candies have the same tastiness.
The choice of candies has a direct effect on Grisha's happiness. One can assume that he should take the tastiest ones β but no, the holiday magic turns things upside down. It is the xor-sum of tastinesses that matters, not the ordinary sum!
A xor-sum of a sequence of integers a1, a2, ..., am is defined as the bitwise XOR of all its elements: <image>, here <image> denotes the bitwise XOR operation; more about bitwise XOR can be found [here.](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)
Ded Moroz warned Grisha he has more houses to visit, so Grisha can take no more than k candies from the bag. Help Grisha determine the largest xor-sum (largest xor-sum means maximum happiness!) he can obtain.
Input
The sole string contains two integers n and k (1 β€ k β€ n β€ 1018).
Output
Output one number β the largest possible xor-sum.
Examples
Input
4 3
Output
7
Input
6 6
Output
7
Note
In the first sample case, one optimal answer is 1, 2 and 4, giving the xor-sum of 7.
In the second sample case, one can, for example, take all six candies and obtain the xor-sum of 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.
Puchi and Ghissi are playing a game with strings. As Ghissi is a champion at strings, Puchi decides to challenge her. He gives Ghissi a string S and an integer K . The objective for Ghissi is to find a substring of S such that:
- The substring occurs at the start of the string S.
- The substring occurs at the end of the string S.
- The substring also occurs somewhere in the middle of the string S but should not be a prefix of S and it should end on or before position K (1-Based Index).
In other words, this substring should not start from the beginning of the string and must end on or before the position K.
Help Ghissi win the game by finding the largest possible substring.
INPUT
First line contains an integer T. T test cases follow.
Each test case contains a string S of lowercase alphabets ( [a-z] ) and an integer K separated by a space.
OUTPUT
Print the largest possible substring that satisfies the given criteria.
If such a substring does not exist, print "Puchi is a cheat!" without the quotes.
CONSTRAINTS
1 β€ T β€ 10
1 β€ |S| β€ 10^6
1 β€ K β€ |S|
S contains only lowercase alphabets [a-z]
SAMPLE INPUT
3
setupsetpreset 7
setupsetpreset 8
setupsetpreset 9
SAMPLE OUTPUT
Puchi is a cheat!
set
set
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.
Pankaj is a very intelligent student studying in one of the best colleges of this country. He's good enough to challenge the smartest minds in the country, but even the smart ones make mistakes and so did Pankaj - he fell in love, ah. He was so deeply in love that he decided to propose the love of his life. What he didn't know was the fact that there is a different species called the "in-laws" and against them no amount of intelligence, smartness or anything works!
But, this was no ordinary person - this was Pankaj - he decided to take the challenge of his in-laws. So, in order to verify the compatibility of the two love birds, they gave them a sequence to solve - given a sequence, they had to tell the longest increasing subsequence of the given sequence.
But, this was a tricky game - since, any sequence could have multiple increasing subsequences of the same length - so, the answer they were giving were different almost all the time, which got the young couple worried.
So, in order increase their compatibility Pankaj proposes that instead of telling them the sequence, this time the couple would tell them the length of the longest increasing subsequence and not the sequence.
To give further twist by the in-laws, they asked Pankaj to answer the length not in decimal numbers, but in binary numbers!
Input Format:
In the first line, a number t which will indicate the number of numbers in the sequence given to Pankaj by his in-laws.
Then, in the next line, t integers denoting the numbers in the sequence.
Output Format:
Print the length of the longest increasing subsequence in binary numbers.
Constraints:
1 β€ Numbers\; in\; the\; sequence. β€ 100
-10^3 β€ t β€ 10^3
SAMPLE INPUT
6
650 945 -832 -999 678 702
SAMPLE OUTPUT
11
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Utkarsh's mother recently received N piles of books as a gift from someone. The i^th pile contains Bi books.
She neither wants to keep the voluminous books with herself nor she wants to throw them away. So, she decided to distribute them to students of the nearby school. She decided to call K students to her home and ask one of her sons (Utkarsh or Saharsh) to distribute books.
Also, she will get happy only if the following condition is satisfied: For every pile i there must be at least one student who receives more than one books from pile i.
She knows that Utkarsh is very lazy. So he will randomly pick a student and give him a book from any pile. Since he distributes randomly, he might make her sad.
On the other hand, Saharsh is smart and obedient so he will always find a way of distribution (if possible) which will make her happy.
You need to output 2 integers : The maximum value of K
When Utkarsh is asked to distribute the books. His mother must remain happy irrespective of the way of his distribution.
When Saharsh is asked to distribute the books.
Input format:
The first line contains an integer T, denoting the number of test cases.
The first line of each test case contains an integer N, denoting the number of piles.
The next line contains N integers, the array B.
Output format:
For each test case, print two space separated integers: The maximum value of K if Utkarsh distributes and the maximum value of K if Saharsh distributes the books.
Constraints:
1 β€ T β€ 10
1 β€ N β€ 1000
2 β€ Bi β€ 1000
SAMPLE INPUT
1
2
5 5
SAMPLE OUTPUT
4 8
Explanation
When Utkarsh is distributing, 4 is the maximum value of K. If K = 5 then there is a chance that he can give the 5 books of the first pile to 5 different students. This might make his mother unhappy.
Saharsh can distribute in this way to have K = 8: (1,1)(2) (1)(2) (1)(2,2)(1)(2)
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Takahashi loves gold coins. He gains 1000 happiness points for each 500-yen coin he has and gains 5 happiness points for each 5-yen coin he has. (Yen is the currency of Japan.)
Takahashi has X yen. If he exchanges his money so that he will gain the most happiness points, how many happiness points will he earn?
(We assume that there are six kinds of coins available: 500-yen, 100-yen, 50-yen, 10-yen, 5-yen, and 1-yen coins.)
Constraints
* 0 \leq X \leq 10^9
* X is an integer.
Input
Input is given from Standard Input in the following format:
X
Output
Print the maximum number of happiness points that can be earned.
Examples
Input
1024
Output
2020
Input
0
Output
0
Input
1000000000
Output
2000000000
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Chokudai made a rectangular cake for contestants in DDCC 2020 Finals.
The cake has H - 1 horizontal notches and W - 1 vertical notches, which divide the cake into H \times W equal sections. K of these sections has a strawberry on top of each of them.
The positions of the strawberries are given to you as H \times W characters s_{i, j} (1 \leq i \leq H, 1 \leq j \leq W). If s_{i, j} is `#`, the section at the i-th row from the top and the j-th column from the left contains a strawberry; if s_{i, j} is `.`, the section does not contain one. There are exactly K occurrences of `#`s.
Takahashi wants to cut this cake into K pieces and serve them to the contestants. Each of these pieces must satisfy the following conditions:
* Has a rectangular shape.
* Contains exactly one strawberry.
One possible way to cut the cake is shown below:
Find one way to cut the cake and satisfy the condition. We can show that this is always possible, regardless of the number and positions of the strawberries.
Constraints
* 1 \leq H \leq 300
* 1 \leq W \leq 300
* 1 \leq K \leq H \times W
* s_{i, j} is `#` or `.`.
* There are exactly K occurrences of `#` in s.
Input
Input is given from Standard Input in the following format:
H W K
s_{1, 1} s_{1, 2} \cdots s_{1, W}
s_{2, 1} s_{2, 2} \cdots s_{2, W}
:
s_{H, 1} s_{H, 2} \cdots s_{H, W}
Output
Assign the numbers 1, 2, 3, \dots, K to the K pieces obtained after the cut, in any order. Then, let a_{i, j} be the number representing the piece containing the section at the i-th row from the top and the j-th column from the left of the cake. Output should be in the following format:
a_{1, 1} \ a_{1, 2} \ \cdots \ a_{1, W}
a_{2, 1} \ a_{2, 2} \ \cdots \ a_{2, W}
:
a_{H, 1} \ a_{H, 2} \ \cdots \ a_{H, W}
If multiple solutions exist, any of them will be accepted.
Examples
Input
3 3 5
#.#
.#.
#.#
Output
1 2 2
1 3 4
5 5 4
Input
3 3 5
.#
.#.
.#
Output
1 2 2
1 3 4
5 5 4
Input
3 7 7
...#.#
..#...#
.#..#..
Output
1 1 2 2 3 4 4
6 6 2 2 3 5 5
6 6 7 7 7 7 7
Input
13 21 106
.....................
.####.####.####.####.
..#.#..#.#.#....#....
..#.#..#.#.#....#....
..#.#..#.#.#....#....
.####.####.####.####.
.....................
.####.####.####.####.
....#.#..#....#.#..#.
.####.#..#.####.#..#.
.#....#..#.#....#..#.
.####.####.####.####.
.....................
Output
12 12 23 34 45 45 60 71 82 93 93 2 13 24 35 35 17 28 39 50 50
12 12 23 34 45 45 60 71 82 93 93 2 13 24 35 35 17 28 39 50 50
12 12 56 89 89 89 60 104 82 31 31 46 13 24 35 35 61 61 39 50 50
12 12 67 67 100 100 60 9 9 42 42 57 13 24 6 72 72 72 72 72 72
12 12 78 5 5 5 20 20 20 53 68 68 90 24 6 83 83 83 83 83 83
16 16 27 38 49 49 64 75 86 97 79 79 90 101 6 94 94 105 10 21 21
16 16 27 38 49 49 64 75 86 97 79 79 90 101 6 94 94 105 10 21 21
32 32 43 54 65 65 80 11 106 95 22 22 33 44 55 55 70 1 96 85 85
32 32 43 54 76 76 91 11 106 84 84 4 99 66 66 66 81 1 96 74 74
14 14 3 98 87 87 102 11 73 73 73 4 99 88 77 77 92 92 63 63 63
25 25 3 98 87 87 7 29 62 62 62 15 99 88 77 77 103 19 30 52 52
36 36 47 58 69 69 18 29 40 51 51 26 37 48 59 59 8 19 30 41 41
36 36 47 58 69 69 18 29 40 51 51 26 37 48 59 59 8 19 30 41 41
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.
Ken loves ken-ken-pa (Japanese version of hopscotch). Today, he will play it on a directed graph G. G consists of N vertices numbered 1 to N, and M edges. The i-th edge points from Vertex u_i to Vertex v_i.
First, Ken stands on Vertex S. He wants to reach Vertex T by repeating ken-ken-pa. In one ken-ken-pa, he does the following exactly three times: follow an edge pointing from the vertex on which he is standing.
Determine if he can reach Vertex T by repeating ken-ken-pa. If the answer is yes, find the minimum number of ken-ken-pa needed to reach Vertex T. Note that visiting Vertex T in the middle of a ken-ken-pa does not count as reaching Vertex T by repeating ken-ken-pa.
Constraints
* 2 \leq N \leq 10^5
* 0 \leq M \leq \min(10^5, N (N-1))
* 1 \leq u_i, v_i \leq N(1 \leq i \leq M)
* u_i \neq v_i (1 \leq i \leq M)
* If i \neq j, (u_i, v_i) \neq (u_j, v_j).
* 1 \leq S, T \leq N
* S \neq T
Input
Input is given from Standard Input in the following format:
N M
u_1 v_1
:
u_M v_M
S T
Output
If Ken cannot reach Vertex T from Vertex S by repeating ken-ken-pa, print -1. If he can, print the minimum number of ken-ken-pa needed to reach vertex T.
Examples
Input
4 4
1 2
2 3
3 4
4 1
1 3
Output
2
Input
3 3
1 2
2 3
3 1
1 2
Output
-1
Input
2 0
1 2
Output
-1
Input
6 8
1 2
2 3
3 4
4 5
5 1
1 4
1 5
4 6
1 6
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.
Ringo is giving a present to Snuke.
Ringo has found out that Snuke loves yakiniku (a Japanese term meaning grilled meat. yaki: grilled, niku: meat). He supposes that Snuke likes grilled things starting with `YAKI` in Japanese, and does not like other things.
You are given a string S representing the Japanese name of Ringo's present to Snuke. Determine whether S starts with `YAKI`.
Constraints
* 1 \leq |S| \leq 10
* S consists of uppercase English letters.
Input
Input is given from Standard Input in the following format:
S
Output
If S starts with `YAKI`, print `Yes`; otherwise, print `No`.
Examples
Input
YAKINIKU
Output
Yes
Input
TAKOYAKI
Output
No
Input
YAK
Output
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.
The problem set at CODE FESTIVAL 20XX Finals consists of N problems.
The score allocated to the i-th (1β¦iβ¦N) problem is i points.
Takahashi, a contestant, is trying to score exactly N points. For that, he is deciding which problems to solve.
As problems with higher scores are harder, he wants to minimize the highest score of a problem among the ones solved by him.
Determine the set of problems that should be solved.
Constraints
* 1β¦Nβ¦10^7
Input
The input is given from Standard Input in the following format:
N
Output
Among the sets of problems with the total score of N, find a set in which the highest score of a problem is minimum, then print the indices of the problems in the set in any order, one per line.
If there exists more than one such set, any of them will be accepted.
Examples
Input
4
Output
1
3
Input
7
Output
1
2
4
Input
1
Output
1
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Write a program which computes the area of a shape represented by the following three lines:
$y = x^2$
$y = 0$
$x = 600$
It is clear that the area is $72000000$, if you use an integral you learn in high school. On the other hand, we can obtain an approximative area of the shape by adding up areas of many rectangles in the shape as shown in the following figure:
<image>
$f(x) = x^2$
The approximative area $s$ where the width of the rectangles is $d$ is:
area of rectangle where its width is $d$ and height is $f(d)$ $+$
area of rectangle where its width is $d$ and height is $f(2d)$ $+$
area of rectangle where its width is $d$ and height is $f(3d)$ $+$
...
area of rectangle where its width is $d$ and height is $f(600 - d)$
The more we decrease $d$, the higer-precision value which is close to $72000000$ we could obtain. Your program should read the integer $d$ which is a divisor of $600$, and print the area $s$.
Input
The input consists of several datasets. Each dataset consists of an integer $d$ in a line. The number of datasets is less than or equal to 20.
Output
For each dataset, print the area $s$ in a line.
Example
Input
20
10
Output
68440000
70210000
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 phantom thief "Lupin IV" is told by the beautiful "Fujiko Mine", a descendant of the Aizu clan, that the military funds left by the Aizu clan are sleeping in Aizuwakamatsu city. According to a report by Lupine's longtime companion, "Ishikawa Koshiemon," military funds are stored in several warehouses in a Senryobako box. There is no lookout in the warehouse, but it is tightly locked. However, Koshiemon says that if he uses the secret "steel slashing" technique taught by his father, he can instantly break the warehouse.
<image>
The remaining problem is the transportation of Senryobako. Lupine and Yuemon, who have no physical strength, cannot carry a Senryobako. So, I asked the reliable man "Infinity" to carry it. In order to carry out all the Senryobako, Lupine made the following plan.
First, drive Lupine to the first warehouse and drop off Koshiemon and Daisuke.
* Koshiemon breaks the warehouse
* Daisuke carries out all Senryobako
* While holding the Senryobako, head to the next warehouse decided by Lupine
Repeat this and carry out the Senryobako to the last warehouse. Meanwhile, Lupine prepares a helicopter and carries a thousand boxes with them in the last warehouse and escapes. Daisuke can carry anything heavy, but the speed of movement slows down depending on the weight of the luggage. Lupine must take this into consideration when deciding the order in which to break the warehouse.
Instead of Lupine, create a program that outputs the order of breaking the warehouse so that the travel time from breaking the first warehouse to reaching the last warehouse is minimized. However,
* All the warehouses face the street that runs straight north from Tsuruga Castle. The number of warehouses is at most 15, and the distance from the castle is at most 10,000 meters or less.
* Each Senryobako weighs 20 kilograms. The number of Senryobako in each warehouse is less than 10,000.
* To move from warehouse to warehouse, use the underground passage that is installed underground along the street.
* Daisuke travels at 2,000 / (70 + w) meters per minute to carry w kilograms of luggage.
The input data is given the number of the warehouse (integer of 100 or less), the distance from the castle (meters), and the number of Senryobako stored in the warehouse for each warehouse.
Input
The input is given in the following format:
n
s1 d1 v1
s2 d2 v2
::
sn dn vn
The number of warehouses n (n β€ 15) is given in the first line, and the information of the i-th warehouse is given in the following n lines. As information on the warehouse, the number si (1 β€ si β€ 100), the distance from the castle di (1 β€ di β€ 10000), and the number of Senryobako vi (1 β€ vi β€ 10000) are given in one line.
Output
Please output the order of breaking the warehouse on one line. Please separate the warehouse numbers with a blank.
Examples
Input
2
1 100 1
2 200 2
Output
1 2
Input
3
11 100 1
13 200 20
12 300 3
Output
11 12 13
Input
5
13 199 1
51 1000 1
37 350 10
27 300 2
99 200 1000
Output
51 37 27 13 99
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 the planning manager of an animation production company. What animation production companies produce these days is not limited to animation itself. Related products, such as figures of characters and character song CDs, are also important sources of revenue. How much this profit can be increased depends solely on you, the director of the planning department.
Obviously, these related products are not something that should be released in the dark clouds. This is because it takes a lot of time and budget to develop a product. Even if you release a figure of an anime character, you have to limit it to popular characters that are expected to sell a lot.
One of the means to measure the popularity of a character is popularity voting. You've been planning some popularity polls so far, but it seems that viewers are becoming dissatisfied with the project. There is an urgent need to establish other methods for measuring popularity.
So you made a hypothesis. The popularity of a character is proportional to the total appearance time in the main part of the anime. To test this hypothesis, you decided to write a program that aggregates the appearance times of each character.
Input
The input consists of multiple cases. Each case is given in the following format.
n
name0 m0 d0,0 ... d0, m0-1
name1 m1 d1,0 ... d1, m1-1
..
..
..
namen-1 mn-1 dn-1,0 ... dn-1, mn-1-1
n represents the number of characters.
namei represents the name of the character.
mi represents the type of time the character was shown.
After mi, mi integers di and j are given.
di and j represent the time when the character was shown.
The end of the input is given by a line consisting of n = 0
If only one character appears on the screen at a given time, that character can get n points.
Every time one more character appears at the same time, the points obtained for the character reflected at that time decrease by one.
If n characters are shown at the same time, 1 point will be added to each of n characters.
The input satisfies the following constraints.
2 β€ n β€ 20
0 β€ mi β€ 30
0 β€ di, j <30
Di and j are all different for the i-th character.
namei contains only uppercase or lowercase letters and is no longer than 10.
Output
Output the points of the character with the smallest points and their names separated by a single space.
If there are multiple characters with the smallest points, select the person with the smallest name in lexicographical order.
Example
Input
4
akzakr 2 1 8
fnmyi 5 2 3 4 6 9
tsnukk 5 2 3 4 7 9
yskwcnt 6 3 4 7 8 9 10
4
akzakr 1 2
fnmyi 3 10 11 12
tsnukk 2 1 8
yskwcnt 2 1 8
5
knmmdk 2 13 19
akmhmr 4 13 15 19 22
mksyk 6 3 7 12 13 15 19
skrkyk 3 1 4 8
tmemm 3 1 17 24
5
knmmdk 5 5 6 16 18 26
akmhmr 9 3 4 7 11 14 15 17 18 20
mksyk 5 6 13 25 27 28
skrkyk 4 3 16 23 24
tmemm 3 6 12 20
14
hrkamm 6 4 6 8 10 20 22
mkhsi 6 1 10 12 15 22 27
hbkgnh 2 4 9
chyksrg 8 3 4 12 15 16 20 25 28
mktkkc 6 0 1 8 15 17 20
tknsj 6 6 8 18 19 25 26
yyitktk 3 3 12 18
ykhhgwr 5 3 9 10 11 24
amftm 7 2 6 8 12 13 14 17
mmftm 4 6 12 16 18
rtkakzk 3 14 15 21
azsmur 7 1 2 4 12 15 18 20
iormns 2 4 20
ktrotns 6 7 12 14 17 20 25
14
hrkamm 2 4 21
mkhsi 6 2 3 8 11 18 20
hbkgnh 4 4 16 28 29
chyksrg 5 0 3 9 17 22
mktkkc 2 7 18
tknsj 3 3 9 23
yyitktk 3 10 13 25
ykhhgwr 2 18 26
amftm 3 13 18 22
mmftm 4 1 20 24 27
rtkakzk 2 1 10
azsmur 5 2 5 10 14 17
iormns 3 9 15 16
ktrotns 5 7 12 16 17 20
0
Output
7 akzakr
4 akzakr
6 knmmdk
12 tmemm
19 iormns
24 mktkkc
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.
Description
THE BY DOLM @ STER is a training simulation game scheduled to be released on EXIDNA by 1rem on April 1, 2010. For the time being, it probably has nothing to do with an arcade game where the network connection service stopped earlier this month.
This game is a game in which members of the unit (formation) to be produced are selected from the bidders, and through lessons and communication with the members, they (they) are raised to the top of the bidders, the top biddles.
Each bidle has three parameters, vocal, dance, and looks, and the unit's ability score is the sum of all the parameters of the biddles that belong to the unit. The highest of the three stats of a unit is the rank of the unit.
There is no limit to the number of people in a unit, and you can work as a unit with one, three, or 100 members. Of course, you can hire more than one of the same bidle, but hiring a bidle is expensive and must be taken into account.
As a producer, you decided to write and calculate a program to make the best unit.
Input
The input consists of multiple test cases.
The first line of each test case is given the number N of bid dollars and the available cost M. (1 <= N, M <= 300) The next 2 * N lines contain information about each bidle.
The name of the bidle is on the first line of the bidle information. Biddle names consist of alphabets and spaces, with 30 characters or less. Also, there is no bidle with the same name.
The second line is given the integers C, V, D, L. C is the cost of hiring one Biddle, V is the vocal, D is the dance, and L is the stat of the looks. (1 <= C, V, D, L <= 300)
Input ends with EOF.
Output
Answer the maximum rank of units that can be made within the given cost.
If you cannot make a unit, output 0.
Example
Input
3 10
Dobkeradops
7 5 23 10
PataPata
1 1 2 1
dop
5 3 11 14
2 300
Bydo System Alpha
7 11 4 7
Green Inferno
300 300 300 300
Output
29
462
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.
H --Bit Operation Game
Given a rooted tree with N vertices. The vertices are numbered from 0 to N β 1, and the 0th vertex represents the root. `T = 0` for the root, but for the other vertices
* `T = T & X;`
* `T = T & Y;`
* `T = T | X`
* `T = T | Y`
* `T = T ^ X`
* `T = T ^ Y`
One of the operations is written. Here, the operators &, |, ^ mean the bitwise operators and, or, xor, respectively.
Mr. A and Mr. B played the following games M times using this tree. The two start from the root and select the child vertices and proceed alternately, starting from Mr. A and reaching the leaves. The final T value when the operations written in the passed nodes are applied in the order of passing is the score. Mr. B wants to make the score as small as possible, and Mr. A wants to make it large. Given the X and Y values ββof the M games, output the score for each game when the two make the best choice.
Constraints
* 1 β€ N β€ 100000
* 1 β€ M β€ 100000
* 0 β€ X, Y <2 ^ {16}
Input Format
Input is given from standard input in the following format.
N M
o_1_1
o_2
...
o_ {Nβ1}
u_1 v_1
u_2 v_2
...
u_ {Nβ1} v_ {Nβ1}
X_1 Y_1
X_2 Y_2
...
X_M Y_M
On the first line, the number of vertices N of the tree and the integer M representing the number of games played are entered.
From the 2nd line to the Nth line, the operation written at the 1st ... N-1st vertex is input.
In addition, the numbers of the two vertices connected by each side are entered in the N-1 line.
Finally, the values ββof X and Y in M ββgames are entered over M lines.
Output Format
Print the final T value for each game on the M line.
Sample Input 1
6 3
T = T | X
T = T | Y
T = T | Y
T = T ^ Y
T = T & X;
0 1
0 2
13
14
twenty five
5 6
3 5
0 0
Sample Output 1
Four
6
0
<image>
For X = 5, Y = 6, proceed to vertices 0-> 2-> 5, and T = 5 & 6 = 4.
If X = 3, Y = 5, then go to vertices 0-> 1-> 4, and T = 3 ^ 5 = 6.
If X = 0, Y = 0, T does not change from 0 no matter where you go
Example
Input
6 3
T=T|X
T=T|Y
T=T|Y
T=T^Y
T=T&X
0 1
0 2
1 3
1 4
2 5
5 6
3 5
0 0
Output
4
6
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.
Problem
Mr. ukuku1333 is a little sloppy, so when I expanded the product of the linear expressions of x, I couldn't figure out the original linear expression.
Given the nth degree polynomial of x, factor it into the product of the original linear expressions of x.
The nth degree polynomial of x is given by the following BNF.
<Polynomial>: = <Term> | <Polynomial> & plus; <Polynomial> | <Polynomial> β <Term>
<Term>: = x ^ <exponent> | <coefficient> x ^ <index> | <coefficient> x | <constant>
<Index>: = [2-5]
<Coefficient>: = [1-9] [0-9] *
<Constant>: = [1-9] [0-9] *
If the exponent and coefficient are omitted, it is regarded as 1.
Constraints
The input satisfies the following conditions.
* 2 β€ n β€ 5
* For any set of i, j such that 1 β€ i <j β€ m, where m is the number of terms in the given expression,
The degree of the i-th term is guaranteed to be greater than the degree of the j-th term
* It is guaranteed that the nth degree polynomial of x given can be factored into the product form of the linear expression of x.
* Absolute values ββof coefficients and constants are 2 Γ 103 or less, respectively.
* The coefficient with the highest degree is 1, which is guaranteed to be omitted.
* The original constant term of each linear expression before expansion is guaranteed to be a non-zero integer
* It is guaranteed that the original constant terms of each linear expression before expansion are different.
Input
The input is given in the following format.
S
The string S representing the nth degree polynomial of x is given on one line.
Output
Factor S into the product of a linear expression of x, and output it in ascending order of the constant term.
Insert a line break at the end of the output.
Examples
Input
x^2+3x+2
Output
(x+1)(x+2)
Input
x^2-1
Output
(x-1)(x+1)
Input
x^5+15x^4+85x^3+225x^2+274x+120
Output
(x+1)(x+2)(x+3)(x+4)(x+5)
Input
x^3-81x^2-1882x-1800
Output
(x-100)(x+1)(x+18)
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Depth-first search (DFS) follows the strategy to search βdeeperβ in the graph whenever possible. In DFS, edges are recursively explored out of the most recently discovered vertex $v$ that still has unexplored edges leaving it. When all of $v$'s edges have been explored, the search βbacktracksβ to explore edges leaving the vertex from which $v$ was discovered.
This process continues until all the vertices that are reachable from the original source vertex have been discovered. If any undiscovered vertices remain, then one of them is selected as a new source and the search is repeated from that source.
DFS timestamps each vertex as follows:
* $d[v]$ records when $v$ is first discovered.
* $f[v]$ records when the search finishes examining $v$βs adjacency list.
Write a program which reads a directed graph $G = (V, E)$ and demonstrates DFS on the graph based on the following rules:
* $G$ is given in an adjacency-list. Vertices are identified by IDs $1, 2,... n$ respectively.
* IDs in the adjacency list are arranged in ascending order.
* The program should report the discover time and the finish time for each vertex.
* When there are several candidates to visit during DFS, the algorithm should select the vertex with the smallest ID.
* The timestamp starts with 1.
Constraints
* $1 \leq n \leq 100$
Input
In the first line, an integer $n$ denoting the number of vertices of $G$ is given. In the next $n$ lines, adjacency lists of $u$ are given in the following format:
$u$ $k$ $v_1$ $v_2$ ... $v_k$
$u$ is ID of the vertex and $k$ denotes its degree. $v_i$ are IDs of vertices adjacent to $u$.
Output
For each vertex, print $id$, $d$ and $f$ separated by a space character in a line. $id$ is ID of the vertex, $d$ and $f$ is the discover time and the finish time respectively. Print in order of vertex IDs.
Examples
Input
4
1 1 2
2 1 4
3 0
4 1 3
Output
1 1 8
2 2 7
3 4 5
4 3 6
Input
6
1 2 2 3
2 2 3 4
3 1 5
4 1 6
5 1 6
6 0
Output
1 1 12
2 2 11
3 3 8
4 9 10
5 4 7
6 5 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.
Construct a dice from a given sequence of integers in the same way as Dice I.
You are given integers on the top face and the front face after the dice was rolled in the same way as Dice I. Write a program to print an integer on the right side face.
<image>
Constraints
* $0 \leq $ the integer assigned to a face $ \leq 100$
* The integers are all different
* $1 \leq q \leq 24$
Input
In the first line, six integers assigned to faces are given in ascending order of their corresponding labels. In the second line, the number of questions $q$ is given.
In the following $q$ lines, $q$ questions are given. Each question consists of two integers on the top face and the front face respectively.
Output
For each question, print the integer on the right side face.
Example
Input
1 2 3 4 5 6
3
6 5
1 3
3 2
Output
3
5
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.
You are given a positive integer n.
Let S(x) be sum of digits in base 10 representation of x, for example, S(123) = 1 + 2 + 3 = 6, S(0) = 0.
Your task is to find two integers a, b, such that 0 β€ a, b β€ n, a + b = n and S(a) + S(b) is the largest possible among all such pairs.
Input
The only line of input contains an integer n (1 β€ n β€ 10^{12}).
Output
Print largest S(a) + S(b) among all pairs of integers a, b, such that 0 β€ a, b β€ n and a + b = n.
Examples
Input
35
Output
17
Input
10000000000
Output
91
Note
In the first example, you can choose, for example, a = 17 and b = 18, so that S(17) + S(18) = 1 + 7 + 1 + 8 = 17. It can be shown that it is impossible to get a larger answer.
In the second test example, you can choose, for example, a = 5000000001 and b = 4999999999, with S(5000000001) + S(4999999999) = 91. It can be shown that it is impossible to get a larger answer.
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 accordion is a string (yes, in the real world accordions are musical instruments, but let's forget about it for a while) which can be represented as a concatenation of: an opening bracket (ASCII code 091), a colon (ASCII code 058), some (possibly zero) vertical line characters (ASCII code 124), another colon, and a closing bracket (ASCII code 093). The length of the accordion is the number of characters in it.
For example, [::], [:||:] and [:|||:] are accordions having length 4, 6 and 7. (:|:), {:||:}, [:], ]:||:[ are not accordions.
You are given a string s. You want to transform it into an accordion by removing some (possibly zero) characters from it. Note that you may not insert new characters or reorder existing ones. Is it possible to obtain an accordion by removing characters from s, and if so, what is the maximum possible length of the result?
Input
The only line contains one string s (1 β€ |s| β€ 500000). It consists of lowercase Latin letters and characters [, ], : and |.
Output
If it is not possible to obtain an accordion by removing some characters from s, print -1. Otherwise print maximum possible length of the resulting accordion.
Examples
Input
|[a:b:|]
Output
4
Input
|]:[|:]
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.
When Petya went to school, he got interested in large numbers and what they were called in ancient times. For instance, he learned that the Russian word "tma" (which now means "too much to be counted") used to stand for a thousand and "tma tmyschaya" (which literally means "the tma of tmas") used to stand for a million.
Petya wanted to modernize the words we use for numbers and invented a word petricium that represents number k. Moreover, petricium la petricium stands for number k2, petricium la petricium la petricium stands for k3 and so on. All numbers of this form are called petriciumus cifera, and the number's importance is the number of articles la in its title.
Petya's invention brought on a challenge that needed to be solved quickly: does some number l belong to the set petriciumus cifera? As Petya is a very busy schoolboy he needs to automate the process, he asked you to solve it.
Input
The first input line contains integer number k, the second line contains integer number l (2 β€ k, l β€ 231 - 1).
Output
You should print in the first line of the output "YES", if the number belongs to the set petriciumus cifera and otherwise print "NO". If the number belongs to the set, then print on the seconds line the only number β the importance of number l.
Examples
Input
5
25
Output
YES
1
Input
3
8
Output
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.
Bob is about to take a hot bath.
There are two taps to fill the bath: a hot water tap and a cold water tap. The cold water's temperature is t1, and the hot water's temperature is t2. The cold water tap can transmit any integer number of water units per second from 0 to x1, inclusive. Similarly, the hot water tap can transmit from 0 to x2 water units per second.
If y1 water units per second flow through the first tap and y2 water units per second flow through the second tap, then the resulting bath water temperature will be:
<image>
Bob wants to open both taps so that the bath water temperature was not less than t0. However, the temperature should be as close as possible to this value. If there are several optimal variants, Bob chooses the one that lets fill the bath in the quickest way possible.
Determine how much each tap should be opened so that Bob was pleased with the result in the end.
Input
You are given five integers t1, t2, x1, x2 and t0 (1 β€ t1 β€ t0 β€ t2 β€ 106, 1 β€ x1, x2 β€ 106).
Output
Print two space-separated integers y1 and y2 (0 β€ y1 β€ x1, 0 β€ y2 β€ x2).
Examples
Input
10 70 100 100 25
Output
99 33
Input
300 500 1000 1000 300
Output
1000 0
Input
143 456 110 117 273
Output
76 54
Note
In the second sample the hot water tap shouldn't be opened, but the cold water tap should be opened at full capacity in order to fill the bath in the quickest way possible.
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 integers n and m (m < n). Consider a convex regular polygon of n vertices. Recall that a regular polygon is a polygon that is equiangular (all angles are equal in measure) and equilateral (all sides have the same length).
<image> Examples of convex regular polygons
Your task is to say if it is possible to build another convex regular polygon with m vertices such that its center coincides with the center of the initial polygon and each of its vertices is some vertex of the initial polygon.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 10^4) β the number of test cases.
The next t lines describe test cases. Each test case is given as two space-separated integers n and m (3 β€ m < n β€ 100) β the number of vertices in the initial polygon and the number of vertices in the polygon you want to build.
Output
For each test case, print the answer β "YES" (without quotes), if it is possible to build another convex regular polygon with m vertices such that its center coincides with the center of the initial polygon and each of its vertices is some vertex of the initial polygon and "NO" otherwise.
Example
Input
2
6 3
7 3
Output
YES
NO
Note
<image> The first test case of the example
It can be shown that the answer for the second test case of the example 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.
You are given a positive integer D. Let's build the following graph from it:
* each vertex is a divisor of D (not necessarily prime, 1 and D itself are also included);
* two vertices x and y (x > y) have an undirected edge between them if x is divisible by y and \frac x y is a prime;
* the weight of an edge is the number of divisors of x that are not divisors of y.
For example, here is the graph for D=12:
<image>
Edge (4,12) has weight 3 because 12 has divisors [1,2,3,4,6,12] and 4 has divisors [1,2,4]. Thus, there are 3 divisors of 12 that are not divisors of 4 β [3,6,12].
There is no edge between 3 and 2 because 3 is not divisible by 2. There is no edge between 12 and 3 because 12/3=4 is not a prime.
Let the length of the path between some vertices v and u in the graph be the total weight of edges on it. For example, path [(1, 2), (2, 6), (6, 12), (12, 4), (4, 2), (2, 6)] has length 1+2+2+3+1+2=11. The empty path has length 0.
So the shortest path between two vertices v and u is the path that has the minimal possible length.
Two paths a and b are different if there is either a different number of edges in them or there is a position i such that a_i and b_i are different edges.
You are given q queries of the following form:
* v u β calculate the number of the shortest paths between vertices v and u.
The answer for each query might be large so print it modulo 998244353.
Input
The first line contains a single integer D (1 β€ D β€ 10^{15}) β the number the graph is built from.
The second line contains a single integer q (1 β€ q β€ 3 β
10^5) β the number of queries.
Each of the next q lines contains two integers v and u (1 β€ v, u β€ D). It is guaranteed that D is divisible by both v and u (both v and u are divisors of D).
Output
Print q integers β for each query output the number of the shortest paths between the two given vertices modulo 998244353.
Examples
Input
12
3
4 4
12 1
3 4
Output
1
3
1
Input
1
1
1 1
Output
1
Input
288807105787200
4
46 482955026400
12556830686400 897
414 12556830686400
4443186242880 325
Output
547558588
277147129
457421435
702277623
Note
In the first example:
* The first query is only the empty path β length 0;
* The second query are paths [(12, 4), (4, 2), (2, 1)] (length 3+1+1=5), [(12, 6), (6, 2), (2, 1)] (length 2+2+1=5) and [(12, 6), (6, 3), (3, 1)] (length 2+2+1=5).
* The third query is only the path [(3, 1), (1, 2), (2, 4)] (length 1+1+1=3).
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
This is an interactive problem. Don't forget to flush output after printing queries using cout.flush() or fflush(stdout) in C++ or similar functions in other programming languages.
There are n gift boxes in a row, numbered from 1 to n from left to right. It's known that exactly k of them contain valuable gifts β other boxes contain just lucky stones. All boxes look the same and differ only in weight. All boxes with stones have the same weight and are strictly heavier than boxes with valuable items. But valuable gifts may be different, so the boxes with valuable items may have different weights.
You can ask no more than 50 queries (printing an answer doesn't count). By each query you can compare total weights of two non-intersecting subsets of boxes a_1, a_2, ..., a_{k_a} and b_1, b_2, ..., b_{k_b}. In return you'll get one of four results:
* FIRST, if subset a_1, a_2, ..., a_{k_a} is strictly heavier;
* SECOND, if subset b_1, b_2, ..., b_{k_b} is strictly heavier;
* EQUAL, if subsets have equal total weights;
* WASTED, if the query is incorrect or the limit of queries is exceeded.
Using such queries (or, maybe, intuition) find the box with a valuable gift with the minimum index.
Input
The input consists of several cases. In the beginning, you receive the integer T (1 β€ T β€ 500) β the number of test cases.
At the beginning of each test case, you receive two integers n and k (2 β€ n β€ 1000, 1 β€ k β€ n/2) β the number of boxes in a row and the number of boxes with valuable gifts.
It's guaranteed that the order of boxes is fixed beforehand and that the sum of n in one test doesn't exceed 1000.
Output
For each test case print the minimum index among all boxes with a valuable gift in the following format: "! x" where x (1 β€ x β€ n) β the index of the box.
Interaction
Print each query in three lines. In the first line print the sizes of subset in the following format: "? k_a k_b" where k_a and k_b (1 β€ k_a, k_b β€ n; k_a + k_b β€ n) β the corresponding sizes.
In the second line print k_a integers a_1, a_2, ..., a_{k_a} (1 β€ a_i β€ n; a_i β a_j if i β j) β indexes of boxes in the first subset.
In the third line print k_b integers b_1, b_2, ..., b_{k_b} (1 β€ b_i β€ n; b_i β b_j if i β j) β indexes of boxes in the second subset.
The subsets shouldn't intersect, i. e. a_i β b_j for all i and j.
You'll receive one of four responses described above. In the case of WASTED stop your program to avoid getting random verdict instead of Wrong Answer.
Example
Input
2
2 1
-
-
-
FIRST
-
5 2
-
-
-
FIRST
-
-
-
SECOND
-
-
-
EQUAL
-
Output
-
-
? 1 1
1
2
-
! 2
-
? 1 1
1
2
-
? 2 3
4 2
1 3 5
-
? 1 1
4
5
-
! 1
Note
Additional separators "β" in the sample are used only to increase the readability of the sample. Don't print any unnecessary symbols or line breaks in your solution when you send it to the system.
Hacks are forbidden in this task.
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. They have a binary string s (a string such that each character in it is either 0 or 1). Alice moves first, then Bob, then Alice again, and so on.
During their move, the player can choose any number (not less than one) of consecutive equal characters in s and delete them.
For example, if the string is 10110, there are 6 possible moves (deleted characters are bold):
1. 10110 β 0110;
2. 10110 β 1110;
3. 10110 β 1010;
4. 10110 β 1010;
5. 10110 β 100;
6. 10110 β 1011.
After the characters are removed, the characters to the left and to the right of the removed block become adjacent. I. e. the following sequence of moves is valid: 10110 β 100 β 1.
The game ends when the string becomes empty, and the score of each player is the number of 1-characters deleted by them.
Each player wants to maximize their score. Calculate the resulting score of Alice.
Input
The first line contains one integer T (1 β€ T β€ 500) β the number of test cases.
Each test case contains exactly one line containing a binary string s (1 β€ |s| β€ 100).
Output
For each test case, print one integer β the resulting score of Alice (the number of 1-characters deleted by her).
Example
Input
5
01111001
0000
111111
101010101
011011110111
Output
4
0
6
3
6
Note
Questions about the optimal strategy will be ignored.
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 some time the program of rounding numbers that had been developed by the Codeforces participants during one of the previous rounds, helped the citizens of Far Far Away to convert numbers into a more easily readable format. However, as time went by, the economy of the Far Far Away developed and the scale of operations grew. So the King ordered to found the Bank of Far Far Away and very soon even the rounding didn't help to quickly determine even the order of the numbers involved in operations. Besides, rounding a number to an integer wasn't very convenient as a bank needed to operate with all numbers with accuracy of up to 0.01, and not up to an integer.
The King issued yet another order: to introduce financial format to represent numbers denoting amounts of money. The formal rules of storing a number in the financial format are as follows:
* A number contains the integer part and the fractional part. The two parts are separated with a character "." (decimal point).
* To make digits in the integer part of a number easier to read, they are split into groups of three digits, starting from the least significant ones. The groups are separated with the character "," (comma). For example, if the integer part of a number equals 12345678, then it will be stored in the financial format as 12,345,678
* In the financial format a number's fractional part should contain exactly two digits. So, if the initial number (the number that is converted into the financial format) contains less than two digits in the fractional part (or contains no digits at all), it is complemented with zeros until its length equals 2. If the fractional part contains more than two digits, the extra digits are simply discarded (they are not rounded: see sample tests).
* When a number is stored in the financial format, the minus sign is not written. Instead, if the initial number had the minus sign, the result is written in round brackets.
* Please keep in mind that the bank of Far Far Away operates using an exotic foreign currency β snakes ($), that's why right before the number in the financial format we should put the sign "$". If the number should be written in the brackets, then the snake sign should also be inside the brackets.
For example, by the above given rules number 2012 will be stored in the financial format as "$2,012.00" and number -12345678.9 will be stored as "($12,345,678.90)".
The merchants of Far Far Away visited you again and expressed much hope that you supply them with the program that can convert arbitrary numbers to the financial format. Can you help them?
Input
The input contains a number that needs to be converted into financial format. The number's notation length does not exceed 100 characters, including (possible) signs "-" (minus) and "." (decimal point). The number's notation is correct, that is:
* The number's notation only contains characters from the set {"0" β "9", "-", "."}.
* The decimal point (if it is present) is unique and is preceded and followed by a non-zero quantity on decimal digits
* A number cannot start with digit 0, except for a case when its whole integer part equals zero (in this case the integer parts is guaranteed to be a single zero: "0").
* The minus sign (if it is present) is unique and stands in the very beginning of the number's notation
* If a number is identically equal to 0 (that is, if it is written as, for example, "0" or "0.000"), than it is not preceded by the minus sign.
* The input data contains no spaces.
* The number's notation contains at least one decimal digit.
Output
Print the number given in the input in the financial format by the rules described in the problem statement.
Examples
Input
2012
Output
$2,012.00
Input
0.000
Output
$0.00
Input
-0.00987654321
Output
($0.00)
Input
-12345678.9
Output
($12,345,678.90)
Note
Pay attention to the second and third sample tests. They show that the sign of a number in the financial format (and consequently, the presence or absence of brackets) is determined solely by the sign of the initial number. It does not depend on the sign of the number you got after translating the number to the financial format.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
You are given an array a consisting of n integers. Initially all elements of a are either 0 or 1. You need to process q queries of two kinds:
* 1 x : Assign to a_x the value 1 - a_x.
* 2 k : Print the k-th largest value of the array.
As a reminder, k-th largest value of the array b is defined as following:
* Sort the array in the non-increasing order, return k-th element from it.
For example, the second largest element in array [0, 1, 0, 1] is 1, as after sorting in non-increasing order it becomes [1, 1, 0, 0], and the second element in this array is equal to 1.
Input
The first line contains two integers n and q (1 β€ n, q β€ 10^5) β the length of the given array and the number of queries.
The second line contains n integers a_1, a_2, a_3, ..., a_n (0 β€ a_i β€ 1) β elements of the initial array.
Each of the following q lines contains two integers. The first integer is t (1 β€ t β€ 2) β the type of query.
* If t = 1 the second integer is x (1 β€ x β€ n) β the position of the modified number. You have to assign to a_x the value 1 - a_x.
* If t = 2 the second integer is k (1 β€ k β€ n) β you need to print the k-th largest value of the array.
It's guaranteed that there will be at least one query of the second type (satisfying t = 2).
Output
For each query of the second type, print a single integer β the answer to the query.
Example
Input
5 5
1 1 0 1 0
2 3
1 2
2 3
2 1
2 5
Output
1
0
1
0
Note
Initially a = [1, 1, 0, 1, 0].
The first operation is printing the third largest value, which is 1.
The second operation is assigning a_2 the value 0, a becomes [1, 0, 0, 1, 0].
The third operation is printing the third largest value, it is 0.
The fourth operation is printing the first largest value, it is 1.
The last operation is printing the fifth largest value, it is 0.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
You are given an array a of n (n β₯ 2) positive integers and an integer p. Consider an undirected weighted graph of n vertices numbered from 1 to n for which the edges between the vertices i and j (i<j) are added in the following manner:
* If gcd(a_i, a_{i+1}, a_{i+2}, ..., a_{j}) = min(a_i, a_{i+1}, a_{i+2}, ..., a_j), then there is an edge of weight min(a_i, a_{i+1}, a_{i+2}, ..., a_j) between i and j.
* If i+1=j, then there is an edge of weight p between i and j.
Here gcd(x, y, β¦) denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers x, y, ....
Note that there could be multiple edges between i and j if both of the above conditions are true, and if both the conditions fail for i and j, then there is no edge between these vertices.
The goal is to find the weight of the [minimum spanning tree](https://en.wikipedia.org/wiki/Minimum_spanning_tree) of this graph.
Input
The first line contains a single integer t (1 β€ t β€ 10^4) β the number of test cases.
The first line of each test case contains two integers n (2 β€ n β€ 2 β
10^5) and p (1 β€ p β€ 10^9) β the number of nodes and the parameter p.
The second line contains n integers a_1, a_2, a_3, ..., a_n (1 β€ a_i β€ 10^9).
It is guaranteed that the sum of n over all test cases does not exceed 2 β
10^5.
Output
Output t lines. For each test case print the weight of the corresponding graph.
Example
Input
4
2 5
10 10
2 5
3 3
4 5
5 2 4 9
8 8
5 3 3 6 10 100 9 15
Output
5
3
12
46
Note
Here are the graphs for the four test cases of the example (the edges of a possible MST of the graphs are marked pink):
For test case 1
<image>
For test case 2
<image>
For test case 3
<image>
For test case 4
<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.
Let's dive into one of the most interesting areas of magic β writing spells. Learning this exciting but challenging science is very troublesome, so now you will not learn the magic words, but only get to know the basic rules of writing spells.
Each spell consists of several lines. The line, whose first non-space character is character "#" is an amplifying line and it is responsible for spell power. The remaining lines are common, and determine the effect of the spell.
You came across the text of some spell. Spell was too long, so you cannot understand its meaning. So you want to make it as short as possible without changing the meaning.
The only way to shorten a spell that you know is the removal of some spaces and line breaks. We know that when it comes to texts of spells, the spaces carry meaning only in the amplifying lines, so we should remove all spaces in other lines. Newlines also do not matter, unless any of the two separated lines is amplifying. Thus, if two consecutive lines are not amplifying, they need to be joined into one (i.e. we should concatenate the second line to the first one). Removing spaces in amplifying lines and concatenating the amplifying lines to anything is forbidden.
Note that empty lines must be processed just like all the others: they must be joined to the adjacent non-amplifying lines, or preserved in the output, if they are surrounded with amplifying lines on both sides (i.e. the line above it, if there is one, is amplifying, and the line below it, if there is one, is amplifying too).
For now those are the only instructions for removing unnecessary characters that you have to follow (oh yes, a newline is a character, too).
The input contains the text of the spell, which should be reduced. Remove the extra characters and print the result to the output.
Input
The input contains multiple lines. All characters in the lines have codes from 32 to 127 (inclusive). Please note that the lines may begin with or end with one or more spaces. The size of the input does not exceed 1048576 ( = 220) bytes. Newlines are included in this size.
In the Windows operating system used on the testing computer, a newline is a sequence of characters with codes #13#10. It is guaranteed that after each line of input there is a newline. In particular, the input ends with a newline. Note that the newline is the end of the line, and not the beginning of the next one.
It is guaranteed that the input contains at least one character other than a newline.
It is recommended to organize the input-output line by line, in this case the newlines will be processed correctly by the language means.
Output
Print the text of the spell where all extra characters are deleted. Please note that each output line should be followed by a newline.
Please be careful: your answers will be validated by comparing them to the jury's answer byte-by-byte. So, all spaces and newlines matter.
Examples
Input
# include <cstdio>
using namespace std;
int main ( ){
puts("Hello # World"); #
#
}
Output
# include <cstdio>
usingnamespacestd;intmain(){puts("Hello#World");#
#
}
Input
#
#
Output
#
#
Note
In the first sample the amplifying lines are lines 1 and 7. So, lines 2 to 6 are concatenated to each other, all spaces are deleted from them.
In the second sample the amplifying lines are lines 1 and 3. So, no lines are concatenated to each other.
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 weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n.
Input
The first line contains two integers n and m (2 β€ n β€ 105, 0 β€ m β€ 105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1 β€ ai, bi β€ n, 1 β€ wi β€ 106), where ai, bi are edge endpoints and wi is the length of the edge.
It is possible that the graph has loops and multiple edges between pair of vertices.
Output
Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.
Examples
Input
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
Output
1 4 3 5
Input
5 6
1 2 2
2 5 5
2 3 4
1 4 1
4 3 3
3 5 1
Output
1 4 3 5
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Let's denote d(n) as the number of divisors of a positive integer n. You are given three integers a, b and c. Your task is to calculate the following sum:
<image>
Find the sum modulo 1073741824 (230).
Input
The first line contains three space-separated integers a, b and c (1 β€ a, b, c β€ 2000).
Output
Print a single integer β the required sum modulo 1073741824 (230).
Examples
Input
2 2 2
Output
20
Input
4 4 4
Output
328
Input
10 10 10
Output
11536
Note
For the first example.
* d(1Β·1Β·1) = d(1) = 1;
* d(1Β·1Β·2) = d(2) = 2;
* d(1Β·2Β·1) = d(2) = 2;
* d(1Β·2Β·2) = d(4) = 3;
* d(2Β·1Β·1) = d(2) = 2;
* d(2Β·1Β·2) = d(4) = 3;
* d(2Β·2Β·1) = d(4) = 3;
* d(2Β·2Β·2) = d(8) = 4.
So the result is 1 + 2 + 2 + 3 + 2 + 3 + 3 + 4 = 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.
Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right.
Once Vasya chose one of the boxes, let's assume that its number is i, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting balls (one at a time) to the boxes with numbers i + 1, i + 2, i + 3 and so on. If Vasya puts a ball into the box number n, then the next ball goes to box 1, the next one goes to box 2 and so on. He did it until he had no balls left in his hands. It is possible that Vasya puts multiple balls to the same box, and it is also possible that one or more balls will go to the box number i. If i = n, Vasya puts the first ball into the box number 1, then the next ball goes to box 2 and so on.
For example, let's suppose that initially Vasya had four boxes, and the first box had 3 balls, the second one had 2, the third one had 5 and the fourth one had 4 balls. Then, if i = 3, then Vasya will take all five balls out of the third box and put them in the boxes with numbers: 4, 1, 2, 3, 4. After all Vasya's actions the balls will lie in the boxes as follows: in the first box there are 4 balls, 3 in the second one, 1 in the third one and 6 in the fourth one.
At this point Vasya has completely forgotten the original arrangement of the balls in the boxes, but he knows how they are arranged now, and the number x β the number of the box, where he put the last of the taken out balls.
He asks you to help to find the initial arrangement of the balls in the boxes.
Input
The first line of the input contains two integers n and x (2 β€ n β€ 105, 1 β€ x β€ n), that represent the number of the boxes and the index of the box that got the last ball from Vasya, correspondingly. The second line contains n space-separated integers a1, a2, ..., an, where integer ai (0 β€ ai β€ 109, ax β 0) represents the number of balls in the box with index i after Vasya completes all the actions.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Output
Print n integers, where the i-th one represents the number of balls in the box number i before Vasya starts acting. Separate the numbers in the output by spaces. If there are multiple correct solutions, you are allowed to print any of them.
Examples
Input
4 4
4 3 1 6
Output
3 2 5 4
Input
5 2
3 2 0 2 7
Output
2 1 4 1 6
Input
3 3
2 3 1
Output
1 2 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.
Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following:
1. Add the integer xi to the first ai elements of the sequence.
2. Append an integer ki to the end of the sequence. (And hence the size of the sequence increases by 1)
3. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence.
After each operation, the cows would like to know the average of all the numbers in the sequence. Help them!
Input
The first line contains a single integer n (1 β€ n β€ 2Β·105) β the number of operations. The next n lines describe the operations. Each line will start with an integer ti (1 β€ ti β€ 3), denoting the type of the operation (see above). If ti = 1, it will be followed by two integers ai, xi (|xi| β€ 103; 1 β€ ai). If ti = 2, it will be followed by a single integer ki (|ki| β€ 103). If ti = 3, it will not be followed by anything.
It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence.
Output
Output n lines each containing the average of the numbers in the sequence after the corresponding operation.
The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 6.
Examples
Input
5
2 1
3
2 3
2 1
3
Output
0.500000
0.000000
1.500000
1.333333
1.500000
Input
6
2 1
1 2 20
2 2
1 2 -3
3
3
Output
0.500000
20.500000
14.333333
12.333333
17.500000
17.000000
Note
In the second sample, the sequence becomes <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.
Everybody knows that the Berland citizens are keen on health, especially students. Berland students are so tough that all they drink is orange juice!
Yesterday one student, Vasya and his mates made some barbecue and they drank this healthy drink only. After they ran out of the first barrel of juice, they decided to play a simple game. All n people who came to the barbecue sat in a circle (thus each person received a unique index bi from 0 to n - 1). The person number 0 started the game (this time it was Vasya). All turns in the game were numbered by integers starting from 1. If the j-th turn was made by the person with index bi, then this person acted like that:
1. he pointed at the person with index (bi + 1) mod n either with an elbow or with a nod (x mod y is the remainder after dividing x by y);
2. if j β₯ 4 and the players who had turns number j - 1, j - 2, j - 3, made during their turns the same moves as player bi on the current turn, then he had drunk a glass of juice;
3. the turn went to person number (bi + 1) mod n.
The person who was pointed on the last turn did not make any actions.
The problem was, Vasya's drunk too much juice and can't remember the goal of the game. However, Vasya's got the recorded sequence of all the participants' actions (including himself). Now Vasya wants to find out the maximum amount of juice he could drink if he played optimally well (the other players' actions do not change). Help him.
You can assume that in any scenario, there is enough juice for everybody.
Input
The first line contains a single integer n (4 β€ n β€ 2000) β the number of participants in the game. The second line describes the actual game: the i-th character of this line equals 'a', if the participant who moved i-th pointed at the next person with his elbow, and 'b', if the participant pointed with a nod. The game continued for at least 1 and at most 2000 turns.
Output
Print a single integer β the number of glasses of juice Vasya could have drunk if he had played optimally well.
Examples
Input
4
abbba
Output
1
Input
4
abbab
Output
0
Note
In both samples Vasya has got two turns β 1 and 5. In the first sample, Vasya could have drunk a glass of juice during the fifth turn if he had pointed at the next person with a nod. In this case, the sequence of moves would look like "abbbb". In the second sample Vasya wouldn't drink a single glass of juice as the moves performed during turns 3 and 4 are different.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Vasya has recently found out what a digital root of a number is and he decided to share his knowledge with you.
Let's assume that S(n) is the sum of digits of number n, for example, S(4098) = 4 + 0 + 9 + 8 = 21. Then the digital root of number n equals to:
1. dr(n) = S(n), if S(n) < 10;
2. dr(n) = dr( S(n) ), if S(n) β₯ 10.
For example, dr(4098) = dr(21) = 3.
Vasya is afraid of large numbers, so the numbers he works with are at most 101000. For all such numbers, he has proved that dr(n) = S( S( S( S(n) ) ) ) (n β€ 101000).
Now Vasya wants to quickly find numbers with the given digital root. The problem is, he hasn't learned how to do that and he asked you to help him. You task is, given numbers k and d, find the number consisting of exactly k digits (the leading zeroes are not allowed), with digital root equal to d, or else state that such number does not exist.
Input
The first line contains two integers k and d (1 β€ k β€ 1000; 0 β€ d β€ 9).
Output
In a single line print either any number that meets the requirements (without the leading zeroes) or "No solution" (without the quotes), if the corresponding number does not exist.
The chosen number must consist of exactly k digits. We assume that number 0 doesn't contain any leading zeroes.
Examples
Input
4 4
Output
5881
Input
5 1
Output
36172
Input
1 0
Output
0
Note
For the first test sample dr(5881) = dr(22) = 4.
For the second test sample dr(36172) = dr(19) = dr(10) = 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.
The New Year is coming! That's why many people today are busy preparing New Year presents. Vasily the Programmer is no exception.
Vasily knows that the best present is (no, it's not a contest) money. He's put n empty wallets from left to right in a row and decided how much money to put in what wallet. Vasily decided to put ai coins to the i-th wallet from the left.
Vasily is a very busy man, so the money are sorted into the bags by his robot. Initially, the robot stands by the leftmost wallet in the row. The robot can follow instructions of three types: go to the wallet that is to the left of the current one (if such wallet exists), go to the wallet that is to the right of the current one (if such wallet exists), put a coin to the current wallet. Due to some technical malfunctions the robot cannot follow two "put a coin" instructions in a row.
Vasily doesn't want to wait for long, so he wants to write a program for the robot that contains at most 106 operations (not necessarily minimum in length) the robot can use to put coins into the wallets. Help him.
Input
The first line contains integer n (2 β€ n β€ 300) β the number of wallets. The next line contains n integers a1, a2, ..., an (0 β€ ai β€ 300).
It is guaranteed that at least one ai is positive.
Output
Print the sequence that consists of k (1 β€ k β€ 106) characters, each of them equals: "L", "R" or "P". Each character of the sequence is an instruction to the robot. Character "L" orders to move to the left, character "R" orders to move to the right, character "P" orders the robot to put a coin in the wallet. The robot is not allowed to go beyond the wallet line. In other words, you cannot give instructions "L" if the robot is at wallet 1, or "R" at wallet n.
As a result of the performed operations, the i-th wallet from the left must contain exactly ai coins. If there are multiple answers, you can print any of them.
Examples
Input
2
1 2
Output
PRPLRP
Input
4
0 2 0 2
Output
RPRRPLLPLRRRP
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.
School holidays come in Berland. The holidays are going to continue for n days. The students of school βN are having the time of their lives and the IT teacher Marina Sergeyevna, who has spent all the summer busy checking the BSE (Berland State Examination) results, has finally taken a vacation break! Some people are in charge of the daily watering of flowers in shifts according to the schedule. However when Marina Sergeyevna was making the schedule, she was so tired from work and so lost in dreams of the oncoming vacation that she perhaps made several mistakes. In fact, it is possible that according to the schedule, on some days during the holidays the flowers will not be watered or will be watered multiple times. Help Marina Sergeyevna to find a mistake.
Input
The first input line contains two numbers n and m (1 β€ n, m β€ 100) β the number of days in Berland holidays and the number of people in charge of the watering respectively. The next m lines contain the description of the duty schedule. Each line contains two integers ai and bi (1 β€ ai β€ bi β€ n), meaning that the i-th person in charge should water the flowers from the ai-th to the bi-th day inclusively, once a day. The duty shifts are described sequentially, i.e. bi β€ ai + 1 for all i from 1 to n - 1 inclusively.
Output
Print "OK" (without quotes), if the schedule does not contain mistakes. Otherwise you have to find the minimal number of a day when the flowers will not be watered or will be watered multiple times, and output two integers β the day number and the number of times the flowers will be watered that day.
Examples
Input
10 5
1 2
3 3
4 6
7 7
8 10
Output
OK
Input
10 5
1 2
2 3
4 5
7 8
9 10
Output
2 2
Input
10 5
1 2
3 3
5 7
7 7
7 10
Output
4 0
Note
Keep in mind that in the second sample the mistake occurs not only on the second day, but also on the sixth day, when nobody waters the flowers. However, you have to print the second day, i.e. the day with the minimal number.
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 and B are preparing themselves for programming contests.
An important part of preparing for a competition is sharing programming knowledge from the experienced members to those who are just beginning to deal with the contests. Therefore, during the next team training A decided to make teams so that newbies are solving problems together with experienced participants.
A believes that the optimal team of three people should consist of one experienced participant and two newbies. Thus, each experienced participant can share the experience with a large number of people.
However, B believes that the optimal team should have two experienced members plus one newbie. Thus, each newbie can gain more knowledge and experience.
As a result, A and B have decided that all the teams during the training session should belong to one of the two types described above. Furthermore, they agree that the total number of teams should be as much as possible.
There are n experienced members and m newbies on the training session. Can you calculate what maximum number of teams can be formed?
Input
The first line contains two integers n and m (0 β€ n, m β€ 5Β·105) β the number of experienced participants and newbies that are present at the training session.
Output
Print the maximum number of teams that can be formed.
Examples
Input
2 6
Output
2
Input
4 5
Output
3
Note
Let's represent the experienced players as XP and newbies as NB.
In the first test the teams look as follows: (XP, NB, NB), (XP, NB, NB).
In the second test sample the teams look as follows: (XP, NB, NB), (XP, NB, NB), (XP, XP, NB).
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 girl Susie went shopping with her mom and she wondered how to improve service quality.
There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time when all the people who stand in the queue in front of him are served. Susie thought that if we swap some people in the queue, then we can decrease the number of people who are disappointed.
Help Susie find out what is the maximum number of not disappointed people can be achieved by swapping people in the queue.
Input
The first line contains integer n (1 β€ n β€ 105).
The next line contains n integers ti (1 β€ ti β€ 109), separated by spaces.
Output
Print a single number β the maximum number of not disappointed people in the queue.
Examples
Input
5
15 2 1 5 3
Output
4
Note
Value 4 is achieved at such an arrangement, for example: 1, 2, 3, 5, 15. Thus, you can make everything feel not disappointed except for the person with time 5.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
You are given two arrays A and B consisting of integers, sorted in non-decreasing order. Check whether it is possible to choose k numbers in array A and choose m numbers in array B so that any number chosen in the first array is strictly less than any number chosen in the second array.
Input
The first line contains two integers nA, nB (1 β€ nA, nB β€ 105), separated by a space β the sizes of arrays A and B, correspondingly.
The second line contains two integers k and m (1 β€ k β€ nA, 1 β€ m β€ nB), separated by a space.
The third line contains nA numbers a1, a2, ... anA ( - 109 β€ a1 β€ a2 β€ ... β€ anA β€ 109), separated by spaces β elements of array A.
The fourth line contains nB integers b1, b2, ... bnB ( - 109 β€ b1 β€ b2 β€ ... β€ bnB β€ 109), separated by spaces β elements of array B.
Output
Print "YES" (without the quotes), if you can choose k numbers in array A and m numbers in array B so that any number chosen in array A was strictly less than any number chosen in array B. Otherwise, print "NO" (without the quotes).
Examples
Input
3 3
2 1
1 2 3
3 4 5
Output
YES
Input
3 3
3 3
1 2 3
3 4 5
Output
NO
Input
5 2
3 1
1 1 1 1 1
2 2
Output
YES
Note
In the first sample test you can, for example, choose numbers 1 and 2 from array A and number 3 from array B (1 < 3 and 2 < 3).
In the second sample test the only way to choose k elements in the first array and m elements in the second one is to choose all numbers in both arrays, but then not all the numbers chosen in A will be less than all the numbers chosen in B: <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.
This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n points connected by m segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions:
1. Only segments already presented on the picture can be painted;
2. The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment;
3. The numbers of points from the beginning of the tail to the end should strictly increase.
Masha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is the endpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get.
Note that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications.
Input
First line of the input contains two integers n and m(2 β€ n β€ 100 000, 1 β€ m β€ 200 000) β the number of points and the number segments on the picture respectively.
Then follow m lines, each containing two integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points.
Output
Print the maximum possible value of the hedgehog's beauty.
Examples
Input
8 6
4 5
3 5
2 5
1 2
2 8
6 7
Output
9
Input
4 6
1 2
1 3
1 4
2 3
2 4
3 4
Output
12
Note
The picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3Β·3 = 9.
<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.
Johnny drives a truck and must deliver a package from his hometown to the district center. His hometown is located at point 0 on a number line, and the district center is located at the point d.
Johnny's truck has a gas tank that holds exactly n liters, and his tank is initially full. As he drives, the truck consumes exactly one liter per unit distance traveled. Moreover, there are m gas stations located at various points along the way to the district center. The i-th station is located at the point xi on the number line and sells an unlimited amount of fuel at a price of pi dollars per liter. Find the minimum cost Johnny must pay for fuel to successfully complete the delivery.
Input
The first line of input contains three space separated integers d, n, and m (1 β€ n β€ d β€ 109, 1 β€ m β€ 200 000) β the total distance to the district center, the volume of the gas tank, and the number of gas stations, respectively.
Each of the next m lines contains two integers xi, pi (1 β€ xi β€ d - 1, 1 β€ pi β€ 106) β the position and cost of gas at the i-th gas station. It is guaranteed that the positions of the gas stations are distinct.
Output
Print a single integer β the minimum cost to complete the delivery. If there is no way to complete the delivery, print -1.
Examples
Input
10 4 4
3 5
5 8
6 3
8 4
Output
22
Input
16 5 2
8 2
5 1
Output
-1
Note
In the first sample, Johnny's truck holds 4 liters. He can drive 3 units to the first gas station, buy 2 liters of gas there (bringing the tank to 3 liters total), drive 3 more units to the third gas station, buy 4 liters there to fill up his tank, and then drive straight to the district center. His total cost is 2Β·5 + 4Β·3 = 22 dollars.
In the second sample, there is no way for Johnny to make it to the district center, as his tank cannot hold enough gas to take him from the latest gas station to the district center.
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.
International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of form IAO'y, where y stands for some number of consequent last digits of the current year. Organizers always pick an abbreviation with non-empty string y that has never been used before. Among all such valid abbreviations they choose the shortest one and announce it to be the abbreviation of this year's competition.
For example, the first three Olympiads (years 1989, 1990 and 1991, respectively) received the abbreviations IAO'9, IAO'0 and IAO'1, while the competition in 2015 received an abbreviation IAO'15, as IAO'5 has been already used in 1995.
You are given a list of abbreviations. For each of them determine the year it stands for.
Input
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of abbreviations to process.
Then n lines follow, each containing a single abbreviation. It's guaranteed that each abbreviation contains at most nine digits.
Output
For each abbreviation given in the input, find the year of the corresponding Olympiad.
Examples
Input
5
IAO'15
IAO'2015
IAO'1
IAO'9
IAO'0
Output
2015
12015
1991
1989
1990
Input
4
IAO'9
IAO'99
IAO'999
IAO'9999
Output
1989
1999
2999
9999
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.
PolandBall is a young, clever Ball. He is interested in prime numbers. He has stated a following hypothesis: "There exists such a positive integer n that for each positive integer m number nΒ·m + 1 is a prime number".
Unfortunately, PolandBall is not experienced yet and doesn't know that his hypothesis is incorrect. Could you prove it wrong? Write a program that finds a counterexample for any n.
Input
The only number in the input is n (1 β€ n β€ 1000) β number from the PolandBall's hypothesis.
Output
Output such m that nΒ·m + 1 is not a prime number. Your answer will be considered correct if you output any suitable m such that 1 β€ m β€ 103. It is guaranteed the the answer exists.
Examples
Input
3
Output
1
Input
4
Output
2
Note
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
For the first sample testcase, 3Β·1 + 1 = 4. We can output 1.
In the second sample testcase, 4Β·1 + 1 = 5. We cannot output 1 because 5 is prime. However, m = 2 is okay since 4Β·2 + 1 = 9, which is not a prime number.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
You are given an array a consisting of n positive integers. You pick two integer numbers l and r from 1 to n, inclusive (numbers are picked randomly, equiprobably and independently). If l > r, then you swap values of l and r. You have to calculate the expected value of the number of unique elements in segment of the array from index l to index r, inclusive (1-indexed).
Input
The first line contains one integer number n (1 β€ n β€ 106). The second line contains n integer numbers a1, a2, ... an (1 β€ ai β€ 106) β elements of the array.
Output
Print one number β the expected number of unique elements in chosen segment.
Your answer will be considered correct if its absolute or relative error doesn't exceed 10 - 4 β formally, the answer is correct if <image>, where x is jury's answer, and y is your answer.
Examples
Input
2
1 2
Output
1.500000
Input
2
2 2
Output
1.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.
Have you ever tried to explain to the coordinator, why it is eight hours to the contest and not a single problem has been prepared yet? Misha had. And this time he has a really strong excuse: he faced a space-time paradox! Space and time replaced each other.
The entire universe turned into an enormous clock face with three hands β hour, minute, and second. Time froze, and clocks now show the time h hours, m minutes, s seconds.
Last time Misha talked with the coordinator at t1 o'clock, so now he stands on the number t1 on the clock face. The contest should be ready by t2 o'clock. In the terms of paradox it means that Misha has to go to number t2 somehow. Note that he doesn't have to move forward only: in these circumstances time has no direction.
Clock hands are very long, and Misha cannot get round them. He also cannot step over as it leads to the collapse of space-time. That is, if hour clock points 12 and Misha stands at 11 then he cannot move to 1 along the top arc. He has to follow all the way round the clock center (of course, if there are no other hands on his way).
Given the hands' positions, t1, and t2, find if Misha can prepare the contest on time (or should we say on space?). That is, find if he can move from t1 to t2 by the clock face.
Input
Five integers h, m, s, t1, t2 (1 β€ h β€ 12, 0 β€ m, s β€ 59, 1 β€ t1, t2 β€ 12, t1 β t2).
Misha's position and the target time do not coincide with the position of any hand.
Output
Print "YES" (quotes for clarity), if Misha can prepare the contest on time, and "NO" otherwise.
You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").
Examples
Input
12 30 45 3 11
Output
NO
Input
12 0 1 12 1
Output
YES
Input
3 47 0 4 9
Output
YES
Note
The three examples are shown on the pictures below from left to right. The starting position of Misha is shown with green, the ending position is shown with pink. Note that the positions of the hands on the pictures are not exact, but are close to the exact and the answer is the same.
<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 are given two positive integer numbers x and y. An array F is called an y-factorization of x iff the following conditions are met:
* There are y elements in F, and all of them are integer numbers;
* <image>.
You have to count the number of pairwise distinct arrays that are y-factorizations of x. Two arrays A and B are considered different iff there exists at least one index i (1 β€ i β€ y) such that Ai β Bi. Since the answer can be very large, print it modulo 109 + 7.
Input
The first line contains one integer q (1 β€ q β€ 105) β the number of testcases to solve.
Then q lines follow, each containing two integers xi and yi (1 β€ xi, yi β€ 106). Each of these lines represents a testcase.
Output
Print q integers. i-th integer has to be equal to the number of yi-factorizations of xi modulo 109 + 7.
Example
Input
2
6 3
4 2
Output
36
6
Note
In the second testcase of the example there are six y-factorizations:
* { - 4, - 1};
* { - 2, - 2};
* { - 1, - 4};
* {1, 4};
* {2, 2};
* {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.
Let's denote a m-free matrix as a binary (that is, consisting of only 1's and 0's) matrix such that every square submatrix of size m Γ m of this matrix contains at least one zero.
Consider the following problem:
You are given two integers n and m. You have to construct an m-free square matrix of size n Γ n such that the number of 1's in this matrix is maximum possible. Print the maximum possible number of 1's in such matrix.
You don't have to solve this problem. Instead, you have to construct a few tests for it.
You will be given t numbers x1, x2, ..., xt. For every <image>, find two integers ni and mi (ni β₯ mi) such that the answer for the aforementioned problem is exactly xi if we set n = ni and m = mi.
Input
The first line contains one integer t (1 β€ t β€ 100) β the number of tests you have to construct.
Then t lines follow, i-th line containing one integer xi (0 β€ xi β€ 109).
Note that in hacks you have to set t = 1.
Output
For each test you have to construct, output two positive numbers ni and mi (1 β€ mi β€ ni β€ 109) such that the maximum number of 1's in a mi-free ni Γ ni matrix is exactly xi. If there are multiple solutions, you may output any of them; and if this is impossible to construct a test, output a single integer - 1.
Example
Input
3
21
0
1
Output
5 2
1 1
-1
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
A rectangle with sides A and B is cut into rectangles with cuts parallel to its sides. For example, if p horizontal and q vertical cuts were made, (p + 1) β
(q + 1) rectangles were left after the cutting. After the cutting, rectangles were of n different types. Two rectangles are different if at least one side of one rectangle isn't equal to the corresponding side of the other. Note that the rectangle can't be rotated, this means that rectangles a Γ b and b Γ a are considered different if a β b.
For each type of rectangles, lengths of the sides of rectangles are given along with the amount of the rectangles of this type that were left after cutting the initial rectangle.
Calculate the amount of pairs (A; B) such as the given rectangles could be created by cutting the rectangle with sides of lengths A and B. Note that pairs (A; B) and (B; A) are considered different when A β B.
Input
The first line consists of a single integer n (1 β€ n β€ 2 β
10^{5}) β amount of different types of rectangles left after cutting the initial rectangle.
The next n lines each consist of three integers w_{i}, h_{i}, c_{i} (1 β€ w_{i}, h_{i}, c_{i} β€ 10^{12}) β the lengths of the sides of the rectangles of this type and the amount of the rectangles of this type.
It is guaranteed that the rectangles of the different types are different.
Output
Output one integer β the answer to the problem.
Examples
Input
1
1 1 9
Output
3
Input
2
2 3 20
2 4 40
Output
6
Input
2
1 2 5
2 3 5
Output
0
Note
In the first sample there are three suitable pairs: (1; 9), (3; 3) and (9; 1).
In the second sample case there are 6 suitable pairs: (2; 220), (4; 110), (8; 55), (10; 44), (20; 22) and (40; 11).
Here the sample of cut for (20; 22).
<image>
The third sample has no suitable pairs.
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 to handle a very complex water distribution system. The system consists of n junctions and m pipes, i-th pipe connects junctions x_i and y_i.
The only thing you can do is adjusting the pipes. You have to choose m integer numbers f_1, f_2, ..., f_m and use them as pipe settings. i-th pipe will distribute f_i units of water per second from junction x_i to junction y_i (if f_i is negative, then the pipe will distribute |f_i| units of water per second from junction y_i to junction x_i). It is allowed to set f_i to any integer from -2 β
10^9 to 2 β
10^9.
In order for the system to work properly, there are some constraints: for every i β [1, n], i-th junction has a number s_i associated with it meaning that the difference between incoming and outcoming flow for i-th junction must be exactly s_i (if s_i is not negative, then i-th junction must receive s_i units of water per second; if it is negative, then i-th junction must transfer |s_i| units of water per second to other junctions).
Can you choose the integers f_1, f_2, ..., f_m in such a way that all requirements on incoming and outcoming flows are satisfied?
Input
The first line contains an integer n (1 β€ n β€ 2 β
10^5) β the number of junctions.
The second line contains n integers s_1, s_2, ..., s_n (-10^4 β€ s_i β€ 10^4) β constraints for the junctions.
The third line contains an integer m (0 β€ m β€ 2 β
10^5) β the number of pipes.
i-th of the next m lines contains two integers x_i and y_i (1 β€ x_i, y_i β€ n, x_i β y_i) β the description of i-th pipe. It is guaranteed that each unordered pair (x, y) will appear no more than once in the input (it means that there won't be any pairs (x, y) or (y, x) after the first occurrence of (x, y)). It is guaranteed that for each pair of junctions there exists a path along the pipes connecting them.
Output
If you can choose such integer numbers f_1, f_2, ..., f_m in such a way that all requirements on incoming and outcoming flows are satisfied, then output "Possible" in the first line. Then output m lines, i-th line should contain f_i β the chosen setting numbers for the pipes. Pipes are numbered in order they appear in the input.
Otherwise output "Impossible" in the only line.
Examples
Input
4
3 -10 6 1
5
1 2
3 2
2 4
3 4
3 1
Output
Possible
4
-6
8
-7
7
Input
4
3 -10 6 4
5
1 2
3 2
2 4
3 4
3 1
Output
Impossible
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Chandu is a very strict mentor. He always gives a lot of work to his interns. So his interns decided to kill him. There is a party in the office on Saturday Night, and the interns decided to kill him on the same day. In the party, there are N beer bottles. Each bottle has a integer X written on it. Interns decided to mix poison in some of the beer bottles. They made a plan that they will add poison into a bottle only if the integer on the beer bottle has number of divisors strictly less than 4. Chandu came to know about the plan of the interns. So he needs to your help to save his life. Tell Chandu if he can drink the beer or not.
Input:
First line of contains an integer N, denoting the number of bottles.
Each test case contains an integer X, denoting the number on the beer bottle.
Output:
For each beer bottle, print YES if Chandu can drink that beer, otherwise print NO.
Constraints:
1 β€ N β€ 10^5
1 β€ X β€ 10^7
SAMPLE INPUT
3
2
3
6
SAMPLE OUTPUT
NO
NO
YES
Explanation
In first case, X = 2 which has only two divisors 1 and 2. So it will have poison.
Similarly, for second case, X = 3 which has two divisors 1 and 3. So it will have poison.
In third case, X = 6 which has four divisors i.e 1, 2, 3 and 6. So it will not have poison.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Today is the first class after a long vacation and as always Bagha woke up late. His friend, Nathumama has a motorcycle which they can use to reach class on time. However, Nathumama doesn't have enough fuel to reach class so Bagha makes a magic machine which can regenerate the fuel after travelling for some kilometers. (Sorry, Law of Conservation of Energy ;D)
Nathumama's motorcycle has initially n liters of fuel. It takes 1 liter of fuel to travel 2 kilometers. The machine can recover 2 liters of fuel after travelling for 6 kilometers. Help, Bagha to determine if his machine can help him and Nathumama to reach class without getting their fuel exhausted.
Input Format:
First line of input will contain the number of test cases T.
Next T lines of input will consist two integers N and D where N denotes liters initially in the motorcycle and D denotes the distance between class and hostel.
Output Format:
The only line of output per test case will consist of "Yes" if Bagha and Nathumama can reach class else output "No".
Note :
Bagha can only gain 2 liters of fuel by travelling 6 kilometers. He cannot gain 1 liter by travelling 3 kilometers i.e 2 liters is lowest amount of fuel that could be recovered.
Constraints :
1 β€ T β€ 50
0 β€ N,D β€ 100
Problem Setter : Swastik Mundra
SAMPLE INPUT
2
3 10
4 25
SAMPLE OUTPUT
Yes
No
Explanation
For first test case, Nathumama has 3 liters of fuel. They travel for 6 kilometers and waste 3 liters of fuel but at the same time generate 2 more liters. They have now 2 liters still left which they can use to travel 4 more kilometers and hence reach the class.
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.
Walter and Jesse's friend Mike had helped them in making Crymeth and hence, they wanted to give him a share.
For deciding the share, they both decided to choose one number each, X and Y and found out that K^th Highest Common Factor of their two numbers is a good amount of Crymeth that can be given to Mike .
Walter and Jesse did this activity for D days in total. For each day, find out what quantity of Crymeth they decided to give to Mike.
Input:
First line contains a natural number D - the total number of days.
D lines follow. Each line contains three natural numbers - X, Y and K.
Output:
For each day, print the quantity given to Mike on a new line.
If the K^th HCF of X and Y does not exist, simply print "No crymeth today" (Without the quotes)
Constraints:
1 β€ D β€ 10^3
1 β€ X, Y, K β€ 10^10
SAMPLE INPUT
3
8 16 2
8 16 4
8 16 5
SAMPLE OUTPUT
4
1
No crymeth today
Explanation
1st Highest Common Factor of 8 and 16 = 8
2nd Highest Common Factor of 8 and 16 = 4
3rd Highest Common Factor of 8 and 16 = 2
4th Highest Common Factor of 8 and 16 = 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.
Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together.
Given an integer N, determine whether N can be represented as the product of two integers between 1 and 9. If it can, print `Yes`; if it cannot, print `No`.
Constraints
* 1 \leq N \leq 100
* N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
If N can be represented as the product of two integers between 1 and 9 (inclusive), print `Yes`; if it cannot, print `No`.
Examples
Input
10
Output
Yes
Input
50
Output
No
Input
81
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.
You are given a string S of length 2N consisting of lowercase English letters.
There are 2^{2N} ways to color each character in S red or blue. Among these ways, how many satisfy the following condition?
* The string obtained by reading the characters painted red from left to right is equal to the string obtained by reading the characters painted blue from right to left.
Constraints
* 1 \leq N \leq 18
* The length of S is 2N.
* S consists of lowercase English letters.
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of ways to paint the string that satisfy the condition.
Examples
Input
4
cabaacba
Output
4
Input
11
mippiisssisssiipsspiim
Output
504
Input
4
abcdefgh
Output
0
Input
18
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Output
9075135300
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.
AtCoDeer the deer has found two positive integers, a and b. Determine whether the concatenation of a and b in this order is a square number.
Constraints
* 1 β€ a,b β€ 100
* a and b are integers.
Input
Input is given from Standard Input in the following format:
a b
Output
If the concatenation of a and b in this order is a square number, print `Yes`; otherwise, print `No`.
Examples
Input
1 21
Output
Yes
Input
100 100
Output
No
Input
12 10
Output
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.
You've come to your favorite store Infinitesco to buy some ice tea.
The store sells ice tea in bottles of different volumes at different costs. Specifically, a 0.25-liter bottle costs Q yen, a 0.5-liter bottle costs H yen, a 1-liter bottle costs S yen, and a 2-liter bottle costs D yen. The store has an infinite supply of bottles of each type.
You want to buy exactly N liters of ice tea. How many yen do you have to spend?
Constraints
* 1 \leq Q, H, S, D \leq 10^8
* 1 \leq N \leq 10^9
* All input values are integers.
Input
Input is given from Standard Input in the following format:
Q H S D
N
Output
Print the smallest number of yen you have to spend to buy exactly N liters of ice tea.
Examples
Input
20 30 70 90
3
Output
150
Input
10000 1000 100 10
1
Output
100
Input
10 100 1000 10000
1
Output
40
Input
12345678 87654321 12345678 87654321
123456789
Output
1524157763907942
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.
For two positive integers A and B, we will define F(A,B) as the larger of the following: the number of digits in the decimal notation of A, and the number of digits in the decimal notation of B.
For example, F(3,11) = 2 since 3 has one digit and 11 has two digits.
Find the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \times B.
Constraints
* 1 \leq N \leq 10^{10}
* N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \times B.
Examples
Input
10000
Output
3
Input
1000003
Output
7
Input
9876543210
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.
There is a rectangle in the xy-plane, with its lower left corner at (0, 0) and its upper right corner at (W, H). Each of its sides is parallel to the x-axis or y-axis. Initially, the whole region within the rectangle is painted white.
Snuke plotted N points into the rectangle. The coordinate of the i-th (1 β¦ i β¦ N) point was (x_i, y_i).
Then, he created an integer sequence a of length N, and for each 1 β¦ i β¦ N, he painted some region within the rectangle black, as follows:
* If a_i = 1, he painted the region satisfying x < x_i within the rectangle.
* If a_i = 2, he painted the region satisfying x > x_i within the rectangle.
* If a_i = 3, he painted the region satisfying y < y_i within the rectangle.
* If a_i = 4, he painted the region satisfying y > y_i within the rectangle.
Find the area of the white region within the rectangle after he finished painting.
Constraints
* 1 β¦ W, H β¦ 100
* 1 β¦ N β¦ 100
* 0 β¦ x_i β¦ W (1 β¦ i β¦ N)
* 0 β¦ y_i β¦ H (1 β¦ i β¦ N)
* W, H (21:32, added), x_i and y_i are integers.
* a_i (1 β¦ i β¦ N) is 1, 2, 3 or 4.
Input
The input is given from Standard Input in the following format:
W H N
x_1 y_1 a_1
x_2 y_2 a_2
:
x_N y_N a_N
Output
Print the area of the white region within the rectangle after Snuke finished painting.
Examples
Input
5 4 2
2 1 1
3 3 4
Output
9
Input
5 4 3
2 1 1
3 3 4
1 4 2
Output
0
Input
10 10 5
1 6 1
4 1 3
6 9 4
9 4 2
3 1 3
Output
64
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 Kannon-do in the mountain behind Ichiro's house. There are 30 steps from the foot to this Kannon-do, and Ichiro goes to Kannon-do almost every day. Ichiro can go up the stairs up to 3 steps with one foot. While playing, I noticed that there are so many types of stair climbing (the number of steps to skip).
So I decided to do 10 different climbs a day and try all the climbs. However, if you are familiar with mathematics, you should know that such a thing will end the life of Ichiro.
In order to convince Ichiro that Ichiro's plan is not feasible, Ichiro will enter all the steps of the stairs n and make 10 different ways of climbing a day. Create a program that outputs the number of years required to execute. Calculate a year as 365 days. If you need even one day, it will be one year. 365 days is one year, and 366 days is two years.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, one integer n (1 β€ n β€ 30) representing the number of stages is given on one line.
The number of datasets does not exceed 30.
Output
For each dataset, Ichiro outputs the number of years (integer) required to execute all the climbs on one line.
Example
Input
1
10
20
25
0
Output
1
1
34
701
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.
Aiz, which is located in cyberspace, trades information with Wakamatsu. The two countries are developing their economies by exchanging useful data with each other. The two countries, whose national policy is philanthropy and equality, and above all, the old word of the Aizu region, "what must be done", conducts regular surveys of trade conditions.
In the survey, a table is given in which the value obtained by subtracting the outflow amount from the data inflow amount seen from Aiz country in byte units is calculated every 1 nanosecond. From that table, find the longest interval where the sum of the values ββis zero. It is judged that the longer this section is, the more equality is maintained.
Given a table with trade status, write a program to find the length of the longest interval where the sum of the values ββis zero.
Input
The input is given in the following format.
N
d1
d2
::
dN
The first row gives the number N (1 β€ N β€ 200000) of the values ββwritten in the table. The next N rows are given the integer di (-109 β€ di β€ 109), which indicates the value written in row i of the table.
Output
The length of the longest section obtained from the table where the sum is 0 is output in one line. If such an interval does not exist, "0" is output on one line.
Examples
Input
5
18
102
-155
53
32
Output
3
Input
4
1
1
-1
-1
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.
Taro decided to go to the summer festival held at JOI Shrine.
N night shops are open along the way to JOI Shrine. Each night shop is numbered from 1 to N in order, and the fun of playing and the time it takes to play are determined by integers. The fun of playing at night shop i is Ai, and the time it takes to play at night shop i is Bi.
In addition, there is a fireworks display as a summer festival event, and the largest fireworks are launched at time S. Taro wants to see this biggest fireworks.
In order to enjoy both the night shop and the fireworks, Taro decided to make a schedule from the time 0 when he arrives at the summer festival to the time T when the summer festival ends.
Taro selects k (1 β€ k β€ N) night stores from the night stores, and determines the time to visit for each by an integer. You cannot choose the same night shop twice. Assuming that the numbers of the selected night shops are y1, y2, ... yk in ascending order and the time to visit the night shop yi is xyi, Taro plays at the night shop yi from the time xyi to the time xyi + Byi.
Taro plays in ascending order of night shop numbers, and cannot play at two night shops at the same time. Also, the time it takes to move between night stores can be ignored.
After the time T, the summer festival ends, so you can't play at the night shop. Also, you cannot see the fireworks while playing at the night shop. However, if time S is the time to start playing or the time to finish playing at a certain night shop, Taro shall be able to see the fireworks.
That is, the schedule must meet the following conditions.
* y1 <y2 <... <yk
* xy1, xy2, ... xyk are integers.
* 0 β€ xy1 <xy1 + By1 β€ xy2 <xy2 + By2 β€ ... β€ xyk <xyk + Byk β€ T
* There is no i such that xyi <S <xyi + Byi.
The fun of the selected night shop Let M be the sum of Ay1, Ay2, ... Ayk. Taro wants to make a plan so that M is as large as possible.
input
Read the following input from standard input.
The integers N, T, S are written on the first line of the input, separated by blanks, the number of night shops is N, the time when the summer festival ends is T, and the time when the largest fireworks are launched is S. Represents.
The following N lines contain information about the night shop. The integers Ai and Bi are written on the input i + 1 (1 β€ i β€ N) lines, separated by blanks. Indicates that the time is Bi.
It is also guaranteed that one or more appointments can be made for all inputs.
output
Output the integer representing the maximum value of M to the standard output on one line.
Examples
Input
5 20 14
8 9
2 4
7 13
6 3
5 8
Output
16
Input
None
Output
None
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Your mission in this problem is to write a computer program that manipulates molecular for- mulae in virtual chemistry. As in real chemistry, each molecular formula represents a molecule consisting of one or more atoms. However, it may not have chemical reality.
The following are the definitions of atomic symbols and molecular formulae you should consider.
* An atom in a molecule is represented by an atomic symbol, which is either a single capital letter or a capital letter followed by a small letter. For instance H and He are atomic symbols.
* A molecular formula is a non-empty sequence of atomic symbols. For instance, HHHeHHHe is a molecular formula, and represents a molecule consisting of four Hβs and two Heβs.
* For convenience, a repetition of the same sub-formula <image> where n is an integer between 2 and 99 inclusive, can be abbreviated to (X)n. Parentheses can be omitted if X is an atomic symbol. For instance, HHHeHHHe is also written as H2HeH2He, (HHHe)2, (H2He)2, or even ((H)2He)2.
The set of all molecular formulae can be viewed as a formal language. Summarizing the above description, the syntax of molecular formulae is defined as follows.
<image>
Each atom in our virtual chemistry has its own atomic weight. Given the weights of atoms, your program should calculate the weight of a molecule represented by a molecular formula. The molecular weight is defined by the sum of the weights of the constituent atoms. For instance, assuming that the atomic weights of the atoms whose symbols are H and He are 1 and 4, respectively, the total weight of a molecule represented by (H2He)2 is 12.
Input
The input consists of two parts. The first part, the Atomic Table, is composed of a number of lines, each line including an atomic symbol, one or more spaces, and its atomic weight which is a positive integer no more than 1000. No two lines include the same atomic symbol.
The first part ends with a line containing only the string END OF FIRST PART.
The second part of the input is a sequence of lines. Each line is a molecular formula, not exceeding 80 characters, and contains no spaces. A molecule contains at most 105 atoms. Some atomic symbols in a molecular formula may not appear in the Atomic Table.
The sequence is followed by a line containing a single zero, indicating the end of the input.
Output
The output is a sequence of lines, one for each line of the second part of the input. Each line contains either an integer, the molecular weight for a given molecular formula in the correspond- ing input line if all its atomic symbols appear in the Atomic Table, or UNKNOWN otherwise. No extra characters are allowed.
Example
Input
H 1
He 4
C 12
O 16
F 19
Ne 20
Cu 64
Cc 333
END_OF_FIRST_PART
H2C
(MgF)2As
Cu(OH)2
H((CO)2F)99
0
Output
14
UNKNOWN
98
7426
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.
ICPC Calculator
In mathematics, we usually specify the order of operations by using parentheses. For example, 7 Γ (3 + 2) always means multiplying 7 by the result of 3 + 2 and never means adding 2 to the result of 7 Γ 3. However, there are people who do not like parentheses. International Counter of Parentheses Council (ICPC) is attempting to make a notation without parentheses the world standard. They are always making studies of such no-parentheses notations.
Dr. Tsukuba, a member of ICPC, invented a new parenthesis-free notation. In his notation, a single expression is represented by multiple lines, each of which contains an addition operator (+), a multiplication operator (*) or an integer. An expression is either a single integer or an operator application to operands. Integers are denoted in decimal notation in one line. An operator application is denoted by a line of its operator immediately followed by lines denoting its two or more operands, each of which is an expression, recursively. Note that when an operand is an operator application, it comprises multiple lines.
As expressions may be arbitrarily nested, we have to make it clear which operator is applied to which operands. For that purpose, each of the expressions is given its nesting level. The top level expression has the nesting level of 0. When an expression of level n is an operator application, its operands are expressions of level n + 1. The first line of an expression starts with a sequence of periods (.), the number of which indicates the level of the expression.
For example, 2 + 3 in the regular mathematics is denoted as in Figure 1. An operator can be applied to two or more operands. Operators + and * represent operations of summation of all operands and multiplication of all operands, respectively. For example, Figure 2 shows an expression multiplying 2, 3, and 4. For a more complicated example, an expression (2 + 3 + 4) Γ 5 in the regular mathematics can be expressed as in Figure 3 while (2 + 3) Γ 4 Γ 5 can be expressed as in Figure 4.
+
.2
.3
Figure 1: 2 + 3
*
.2
.3
.4
Figure 2: An expression multiplying 2, 3, and 4
*
.+
..2
..3
..4
.5
Figure 3: (2 + 3 + 4) Γ 5
*
.+
..2
..3
.4
.5
Figure 4: (2 + 3) Γ 4 Γ 5
Your job is to write a program that computes the value of expressions written in Dr. Tsukuba's notation to help him.
Input
The input consists of multiple datasets. Each dataset starts with a line containing a positive integer n, followed by n lines denoting a single expression in Dr. Tsukuba's notation.
You may assume that, in the expressions given in the input, every integer comprises a single digit and that every expression has no more than nine integers. You may also assume that all the input expressions are valid in the Dr. Tsukuba's notation. The input contains no extra characters, such as spaces or empty lines.
The last dataset is immediately followed by a line with a single zero.
Output
For each dataset, output a single line containing an integer which is the value of the given expression.
Sample Input
1
9
4
+
.1
.2
.3
9
+
.0
.+
..*
...1
...*
....1
....2
..0
10
+
.+
..6
..2
.+
..1
..*
...7
...6
.3
0
Output for the Sample Input
9
6
2
54
Example
Input
1
9
4
+
.1
.2
.3
9
+
.0
.+
..*
...1
...*
....1
....2
..0
10
+
.+
..6
..2
.+
..1
..*
...7
...6
.3
0
Output
9
6
2
54
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 full exploration sister is a very talented woman. Your sister can easily count the number of routes in a grid pattern if it is in the thousands. You and your exploration sister are now in a room lined with hexagonal tiles. The older sister seems to be very excited about the hexagon she sees for the first time. The older sister, who is unfamiliar with expressing the arrangement of hexagons in coordinates, represented the room in the coordinate system shown in Fig. 1.
<image> Figure 1
You want to move from one point on this coordinate system to another. But every minute your sister tells you the direction you want to move. Your usual sister will instruct you to move so that you don't go through locations with the same coordinates. However, an older sister who is unfamiliar with this coordinate system corresponds to the remainder of | x Γ y Γ t | (x: x coordinate, y: y coordinate, t: elapsed time [minutes] from the first instruction) divided by 6. Simply indicate the direction (corresponding to the number shown in the figure) and it will guide you in a completely random direction.
<image> Figure 2
If you don't want to hurt your sister, you want to reach your destination while observing your sister's instructions as much as possible. You are allowed to do the following seven actions.
* Move 1 tile to direction 0
* Move 1 tile to direction 1
* Move 1 tile in direction 2
* Move 1 tile in direction 3
* Move 1 tile in direction 4
* Move 1 tile in direction 5
* Stay on the spot
You always do one of these actions immediately after your sister gives instructions. The room is furnished and cannot be moved into the tiles where the furniture is placed. Also, movements where the absolute value of the y coordinate exceeds ly or the absolute value of the x coordinate exceeds lx are not allowed. However, the older sister may instruct such a move. Ignoring instructions means moving in a different direction than your sister indicated, or staying there. Output the minimum number of times you should ignore the instructions to get to your destination. Output -1 if it is not possible to reach your destination.
Constraints
* All inputs are integers
* -lx β€ sx, gx β€ lx
* -ly β€ sy, gy β€ ly
* (sx, sy) β (gx, gy)
* (xi, yi) β (sx, sy) (1 β€ i β€ n)
* (xi, yi) β (gx, gy) (1 β€ i β€ n)
* (xi, yi) β (xj, yj) (i β j)
* 0 β€ n β€ 1000
* -lx β€ xi β€ lx (1 β€ i β€ n)
* -ly β€ yi β€ ly (1 β€ i β€ n)
* 0 <lx, ly β€ 100
Input
The input is given in the following format.
> sx sy gx gy
> n
> x1 y1
> ...
> xi yi
> ...
> xn yn
> lx ly
>
here,
* sx, sy are the coordinates of the starting point
* gx, gy are the coordinates of the destination
* n is the number of furniture placed in the room
* xi, yi are the coordinates of the tile with furniture
* lx, ly are the upper limits of the absolute values ββof the movable X and Y coordinates.
Output
Output in one line containing one integer. If you can reach your destination, print the number of times you ignore the minimum instructions. Output -1 if it is not possible to reach your destination.
Examples
Input
0 0 0 2
0
2 2
Output
0
Input
0 0 0 2
6
0 1
1 0
1 -1
0 -1
-1 -1
-1 0
2 2
Output
-1
Input
0 0 0 2
1
0 1
2 2
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.
JAG-channel
Nathan O. Davis operates an electronic bulletin board called JAG-channel. He is currently working on adding a new feature called Thread View.
Like many other electronic bulletin boards, JAG-channel is thread-based. Here, a thread refers to a group of conversations consisting of a series of posts. There are two types of posts:
* First post to create a new thread
* Reply to past posts of existing threads
The thread view is a tree-like view that represents the logical structure of the reply / reply relationship between posts. Each post becomes a node of the tree and has a reply to that post as a child node. Note that direct and indirect replies to a post are subtrees as a whole.
Let's look at an example. For example, the first post "hoge" has two replies "fuga" and "piyo", "fuga" has more replies "foobar" and "jagjag", and "jagjag" Suppose you get a reply "zigzag". The tree of this thread looks like this:
hoge
ββfuga
β ββfoobar
β ββjagjag
β ββ zigzag
ββpiyo
Nathan O. Davis hired a programmer to implement the feature, but the programmer disappeared in the final stages. This programmer has created a tree of threads and completed it to the point of displaying it in a simple format. In this simple format, the depth of the reply is represented by'.' (Half-width dot), and the reply to a certain post has one more'.' To the left than the original post. Also, the reply to a post always comes below the original post. Between the reply source post and the reply, other replies to the reply source post (and direct and indirect replies to it) may appear, but no other posts appear between them. .. The simple format display of the above tree is as follows.
hoge
.fuga
..foobar
..jagjag
... zigzag
.piyo
Your job is to receive this simple format display and format it for easy viewing. That is,
* The'.' Immediately to the left of each post (the rightmost'.' To the left of each post) is'+' (half-width plus),
* For direct replies to the same post, the'.' Located between the'+' immediately to the left of each is'|' (half-width vertical line),
* Other'.' Is''(half-width space)
I want you to replace it with.
The formatted display for the above simple format display is as follows.
hoge
+ fuga
| + foobar
| + jagjag
| + zigzag
+ piyo
Input
The input consists of multiple datasets. The format of each data set is as follows.
> $ n $
> $ s_1 $
> $ s_2 $
> ...
> $ s_n $
$ n $ is an integer representing the number of lines in the simple format display, and can be assumed to be $ 1 $ or more and $ 1 {,} 000 $ or less. The following $ n $ line contains a simple format display of the thread tree. $ s_i $ represents the $ i $ line in the simplified format display and consists of a string consisting of several'.' Followed by lowercase letters of $ 1 $ or more and $ 50 $ or less. $ s_1 $ is the first post in the thread and does not contain a'.'. $ s_2 $, ..., $ s_n $ are replies in that thread and always contain one or more'.'.
$ n = 0 $ indicates the end of input. This is not included in the dataset.
Output
Print a formatted display for each dataset on each $ n $ line.
Sample Input
6
hoge
.fuga
..foobar
..jagjag
... zigzag
.piyo
8
jagjag
.hogehoge
..fugafuga
... ponyoponyo
.... evaeva
.... pokemon
... nowawa
.buhihi
8
hello
.goodmorning
..howareyou
.goodafternoon
..letshavealunch
.goodevening
.goodnight
..gotobed
3
caution
.themessagelengthislessthanorequaltofifty
..sothelengthOFentirelinecanexceedfiftycharacters
0
Output for Sample Input
hoge
+ fuga
| + foobar
| + jagjag
| + zigzag
+ piyo
jagjag
+ hogehoge
| + fugafuga
| + ponyoponyo
| | + evaeva
| | + pokemon
| + nowawa
+ buhihi
hello
+ good morning
| + how are you
+ goodafternoon
| + Letshavealunch
+ goodevening
+ goodnight
+ gotobed
caution
+ themessagelengthislessthanorequaltofifty
+ sothelengthOFentirelinecanexceedfiftycharacters
Example
Input
6
hoge
.fuga
..foobar
..jagjag
...zigzag
.piyo
8
jagjag
.hogehoge
..fugafuga
...ponyoponyo
....evaeva
....pokemon
...nowawa
.buhihi
8
hello
.goodmorning
..howareyou
.goodafternoon
..letshavealunch
.goodevening
.goodnight
..gotobed
3
caution
.themessagelengthislessthanorequaltofifty
..sothelengthoftheentirelinecanexceedfiftycharacters
0
Output
hoge
+fuga
|+foobar
|+jagjag
| +zigzag
+piyo
jagjag
+hogehoge
|+fugafuga
| +ponyoponyo
| |+evaeva
| |+pokemon
| +nowawa
+buhihi
hello
+goodmorning
|+howareyou
+goodafternoon
|+letshavealunch
+goodevening
+goodnight
+gotobed
caution
+themessagelengthislessthanorequaltofifty
+sothelengthoftheentirelinecanexceedfiftycharacters
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-Aun's breathing
Problem Statement
Maeda-san and Goto-san, who will both turn 70 years old in 2060, are long-time friends and friends who fought together at the ACM-ICPC in college.
The two are still excited about competitive programming, drinking tea together.
When the two of us drank tea together, Mr. Maeda said "A`" once, and after that statement, Mr. Goto replied "Un`" exactly once.
However, recently, Mr. Goto often forgets or misunderstands, and even if Mr. Maeda says `A`, Mr. Goto sometimes forgets to reply to` Un` or makes an extra reply.
It seems that Maeda-san and Goto-san were talking about their favorite data structures while drinking tea.
From the conversation at this time, when a record consisting only of Mr. Maeda's remarks represented by `A` and Mr. Goto's reply represented by` Un` was given in chronological order, Mr. Goto was as usual. Please check if it can be considered that you have reacted to.
It should be noted that even if Mr. Goto's reply to Mr. Maeda's remark is delayed a little, it may be considered that Mr. Goto responded as usual. For example, if Mr. Maeda said `A` twice in a row and then Mr. Goto replied with` Un` twice in a row and the conversation ended, Mr. Goto replied as usual. (See Sample Input 2).
Also, at the end of the conversation, even if the number of times Maeda said `A` and the number of times Goto replied` Un` match, it is not considered that Mr. Goto replied as usual. Please note that it may happen. For example, if Mr. Maeda says `A` once, Mr. Goto replies twice in a row with` Un`, and then Mr. Maeda says once `A` and the conversation ends. It is not considered that Mr. Goto responded as usual (see Sample Input 3).
Input
The input is given in the following format.
$ N $
$ S_1 $
$ S_2 $
$β¦ $
$ S_N $
The first line consists of an integer. $ N $ represents the total number of times Maeda-san said `A` and Goto-san replied` Un` in the record, and satisfies $ 1 \ leq N \ leq 100 $. Then the $ N $ line is followed by the string $ S_i $, and each $ S_i (1 \ leq i \ leq N) $ matches either `A` or` Un`. Here, `A` represents Mr. Maeda's remark, and` Un` represents Mr. Goto's reply. It is assumed that $ S_i $ is recorded in ascending order of $ i $. It is assumed that Mr. Maeda and Mr. Goto did not speak at the same time.
Output
If it can be considered that Mr. Goto responded according to the habit, output `YES`, otherwise output` NO` in one line.
Sample Input 1
Four
A
Un
A
Un
Output for the Sample Input 1
YES YES
Sample Input 2
Four
A
A
Un
Un
Output for the Sample Input 2
YES YES
Sample Input 3
Four
A
Un
Un
A
Output for the Sample Input 3
NO
Sample Input 4
1
Un
Output for the Sample Input 4
NO
Example
Input
4
A
Un
A
Un
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.
problem
AOR Ika made a set $ S = \\ {a_1, ..., a_N \\} $ and a map $ f: S β S $. $ f (a_i) = b_i $. For any element $ x $ in the set $ S $, all maps $ g, h: S β S $ satisfying $ g (f (x)) = h (f (x)) $ are $ g (x). ) = Determine if h (x) $ is satisfied, and if not, configure one counterexample.
Example
Input
5
1 2 3 4 5
3 4 2 5 1
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.
The chef is preparing a birthday cake for one of his guests,
and his decided to write the age of the guest in candles on the cake.
There are 10 types of candles, one for each of the digits '0' through '9'.
The chef has forgotten the age of the guest, however, so doesn't know whether he has enough candles of the right types.
For example, if the guest were 101 years old, the chef would need two '1' candles and one '0' candle.
Given the candles the chef has, your task is to determine the smallest positive integer that cannot be represented with those candles.
Input:
Input will begin with an integer Tβ€100, the number of test cases.
Each test case consists of a single line with exactly 10 integers, each between 0 and 8, inclusive.
The first integer of each test case represents the number of '0' candles the chef has,
the second integer represents the number of '1' candles the chef has, and so on.
Output:
For each test case, output on a single line the smallest positive integer that cannot be expressed with the given candles.
Sample input:
3
2 1 1 4 0 6 3 2 2 2
0 1 1 1 1 1 1 1 1 1
2 2 1 2 1 1 3 1 1 1
Sample output:
4
10
22
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.
Chef has learned a new technique for comparing two recipes. A recipe contains a list of ingredients in increasing order of the times they will be processed. An ingredient is represented by a letter 'a'-'z'. The i-th letter in a recipe denotes the i-th ingredient. An ingredient can be used multiple times in a recipe.
The technique is as follows. Compare two recipes by comparing their respective lists. If the sets of ingredients used in both recipes are equal and each ingredient is used the same number of times in both of them (processing order does not matter), they are declared as granama recipes. ("granama" is the Chef-ian word for "similar".)
Chef took two recipes he invented yesterday. He wanted to compare them using the technique. Unfortunately, Chef forgot to keep track of the number of times each ingredient has been used in a recipe. He only compared the ingredients but NOT their frequencies. More precisely, Chef considers two recipes as granama if there are no ingredients which are used in one recipe and not used in the other recipe.
Your task is to report whether Chef has correctly classified the two recipes (as granama or not granama) although he forgot to keep track of the frequencies.
Input
The first line of the input contains a single integer T denoting the number of test cases. The description for T test cases follows. Each test case consists of a single line containing two space-separated strings R and S denoting the two recipes.
Output
For each test case, output a single line containing "YES" (quotes for clarity) if Chef correctly classified the two recipes as granama or not granama. Otherwise, output a single line containing "NO" (quotes for clarity) if Chef declared two recipes as granama when they actually are not.
Constraints
1 β€ T β€ 1001 β€ |R|, |S| β€ 1000
Example
Input:
3
alex axle
paradise diapers
alice bob
Output:
YES
NO
YES
Explanation:
Example case 1: Chef declared them as granama recipes. They are actually granama because the sets of ingredients and the number of times each ingredient has been used are equal. The Chef got it right!
Example case 2: Chef declared them as granama recipes because both sets of ingredients are equal. But they are NOT granama since ingredient 'a' has been used twice in the first recipe but only once in the second. The Chef was incorrect!
Example case 3: Chef declare them as not granama. They are not granama as the sets of ingredients are different. Hence, the Chef was right!
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length 0 are not allowed).
Let's consider empty cells are denoted by '.', then the following figures are stars:
<image> The leftmost figure is a star of size 1, the middle figure is a star of size 2 and the rightmost figure is a star of size 3.
You are given a rectangular grid of size n Γ m consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from 1 to n, columns are numbered from 1 to m. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed n β
m. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.
In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most n β
m stars.
Input
The first line of the input contains two integers n and m (3 β€ n, m β€ 100) β the sizes of the given grid.
The next n lines contains m characters each, the i-th line describes the i-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only.
Output
If it is impossible to draw the given grid using stars only, print "-1".
Otherwise in the first line print one integer k (0 β€ k β€ n β
m) β the number of stars needed to draw the given grid. The next k lines should contain three integers each β x_j, y_j and s_j, where x_j is the row index of the central star character, y_j is the column index of the central star character and s_j is the size of the star. Each star should be completely inside the grid.
Examples
Input
6 8
....*...
...**...
..*****.
...**...
....*...
........
Output
3
3 4 1
3 5 2
3 5 1
Input
5 5
.*...
****.
.****
..**.
.....
Output
3
2 2 1
3 3 1
3 4 1
Input
5 5
.*...
***..
.*...
.*...
.....
Output
-1
Input
3 3
*.*
.*.
*.*
Output
-1
Note
In the first example the output
2
3 4 1
3 5 2
is also correct.
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 Metropolis computer network consists of n servers, each has an encryption key in the range from 0 to 2^k - 1 assigned to it. Let c_i be the encryption key assigned to the i-th server. Additionally, m pairs of servers are directly connected via a data communication channel. Because of the encryption algorithms specifics, a data communication channel can only be considered safe if the two servers it connects have distinct encryption keys. The initial assignment of encryption keys is guaranteed to keep all data communication channels safe.
You have been informed that a new virus is actively spreading across the internet, and it is capable to change the encryption key of any server it infects. More specifically, the virus body contains some unknown number x in the same aforementioned range, and when server i is infected, its encryption key changes from c_i to c_i β x, where β denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Sadly, you know neither the number x nor which servers of Metropolis are going to be infected by the dangerous virus, so you have decided to count the number of such situations in which all data communication channels remain safe. Formally speaking, you need to find the number of pairs (A, x), where A is some (possibly empty) subset of the set of servers and x is some number in the range from 0 to 2^k - 1, such that when all servers from the chosen subset A and none of the others are infected by a virus containing the number x, all data communication channels remain safe. Since this number can be quite big, you are asked to find its remainder modulo 10^9 + 7.
Input
The first line of input contains three integers n, m and k (1 β€ n β€ 500 000, 0 β€ m β€ min((n(n - 1))/(2), 500 000), 0 β€ k β€ 60) β the number of servers, the number of pairs of servers directly connected by a data communication channel, and the parameter k, which defines the range of possible values for encryption keys.
The next line contains n integers c_i (0 β€ c_i β€ 2^k - 1), the i-th of which is the encryption key used by the i-th server.
The next m lines contain two integers u_i and v_i each (1 β€ u_i, v_i β€ n, u_i β v_i) denoting that those servers are connected by a data communication channel. It is guaranteed that each pair of servers appears in this list at most once.
Output
The only output line should contain a single integer β the number of safe infections of some subset of servers by a virus with some parameter, modulo 10^9 + 7.
Examples
Input
4 4 2
0 1 0 1
1 2
2 3
3 4
4 1
Output
50
Input
4 5 3
7 1 7 2
1 2
2 3
3 4
4 1
2 4
Output
96
Note
Consider the first example.
Possible values for the number x contained by the virus are 0, 1, 2 and 3.
For values 0, 2 and 3 the virus can infect any subset of the set of servers, which gives us 16 pairs for each values. A virus containing the number 1 can infect either all of the servers, or none. This gives us 16 + 2 + 16 + 16 = 50 pairs in total.
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 matrix a, consisting of n rows and m columns. Each cell contains an integer in it.
You can change the order of rows arbitrarily (including leaving the initial order), but you can't change the order of cells in a row. After you pick some order of rows, you traverse the whole matrix the following way: firstly visit all cells of the first column from the top row to the bottom one, then the same for the second column and so on. During the traversal you write down the sequence of the numbers on the cells in the same order you visited them. Let that sequence be s_1, s_2, ..., s_{nm}.
The traversal is k-acceptable if for all i (1 β€ i β€ nm - 1) |s_i - s_{i + 1}| β₯ k.
Find the maximum integer k such that there exists some order of rows of matrix a that it produces a k-acceptable traversal.
Input
The first line contains two integers n and m (1 β€ n β€ 16, 1 β€ m β€ 10^4, 2 β€ nm) β the number of rows and the number of columns, respectively.
Each of the next n lines contains m integers (1 β€ a_{i, j} β€ 10^9) β the description of the matrix.
Output
Print a single integer k β the maximum number such that there exists some order of rows of matrix a that it produces an k-acceptable traversal.
Examples
Input
4 2
9 9
10 8
5 3
4 3
Output
5
Input
2 4
1 2 3 4
10 3 7 3
Output
0
Input
6 1
3
6
2
5
1
4
Output
3
Note
In the first example you can rearrange rows as following to get the 5-acceptable traversal:
5 3
10 8
4 3
9 9
Then the sequence s will be [5, 10, 4, 9, 3, 8, 3, 9]. Each pair of neighbouring elements have at least k = 5 difference between them.
In the second example the maximum k = 0, any order is 0-acceptable.
In the third example the given order is already 3-acceptable, you can leave it as it is.
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.
Student Dima from Kremland has a matrix a of size n Γ m filled with non-negative integers.
He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him!
Formally, he wants to choose an integers sequence c_1, c_2, β¦, c_n (1 β€ c_j β€ m) so that the inequality a_{1, c_1} β a_{2, c_2} β β¦ β a_{n, c_n} > 0 holds, where a_{i, j} is the matrix element from the i-th row and the j-th column.
Here x β y denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of integers x and y.
Input
The first line contains two integers n and m (1 β€ n, m β€ 500) β the number of rows and the number of columns in the matrix a.
Each of the next n lines contains m integers: the j-th integer in the i-th line is the j-th element of the i-th row of the matrix a, i.e. a_{i, j} (0 β€ a_{i, j} β€ 1023).
Output
If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE".
Otherwise print "TAK" in the first line, in the next line print n integers c_1, c_2, β¦ c_n (1 β€ c_j β€ m), so that the inequality a_{1, c_1} β a_{2, c_2} β β¦ β a_{n, c_n} > 0 holds.
If there is more than one possible answer, you may output any.
Examples
Input
3 2
0 0
0 0
0 0
Output
NIE
Input
2 3
7 7 7
7 7 10
Output
TAK
1 3
Note
In the first example, all the numbers in the matrix are 0, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero.
In the second example, the selected numbers are 7 (the first number in the first line) and 10 (the third number in the second line), 7 β 10 = 13, 13 is more than 0, so the answer is found.
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.
Nauuo is a girl who loves writing comments.
One day, she posted a comment on Codeforces, wondering whether she would get upvotes or downvotes.
It's known that there were x persons who would upvote, y persons who would downvote, and there were also another z persons who would vote, but you don't know whether they would upvote or downvote. Note that each of the x+y+z people would vote exactly one time.
There are three different results: if there are more people upvote than downvote, the result will be "+"; if there are more people downvote than upvote, the result will be "-"; otherwise the result will be "0".
Because of the z unknown persons, the result may be uncertain (i.e. there are more than one possible results). More formally, the result is uncertain if and only if there exist two different situations of how the z persons vote, that the results are different in the two situations.
Tell Nauuo the result or report that the result is uncertain.
Input
The only line contains three integers x, y, z (0β€ x,y,zβ€100), corresponding to the number of persons who would upvote, downvote or unknown.
Output
If there is only one possible result, print the result : "+", "-" or "0".
Otherwise, print "?" to report that the result is uncertain.
Examples
Input
3 7 0
Output
-
Input
2 0 1
Output
+
Input
1 1 0
Output
0
Input
0 0 1
Output
?
Note
In the first example, Nauuo would definitely get three upvotes and seven downvotes, so the only possible result is "-".
In the second example, no matter the person unknown downvotes or upvotes, Nauuo would get more upvotes than downvotes. So the only possible result is "+".
In the third example, Nauuo would definitely get one upvote and one downvote, so the only possible result is "0".
In the fourth example, if the only one person upvoted, the result would be "+", otherwise, the result would be "-". There are two possible results, so the result is uncertain.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
There are n points on the plane, the i-th of which is at (x_i, y_i). Tokitsukaze wants to draw a strange rectangular area and pick all the points in the area.
The strange area is enclosed by three lines, x = l, y = a and x = r, as its left side, its bottom side and its right side respectively, where l, r and a can be any real numbers satisfying that l < r. The upper side of the area is boundless, which you can regard as a line parallel to the x-axis at infinity. The following figure shows a strange rectangular area.
<image>
A point (x_i, y_i) is in the strange rectangular area if and only if l < x_i < r and y_i > a. For example, in the above figure, p_1 is in the area while p_2 is not.
Tokitsukaze wants to know how many different non-empty sets she can obtain by picking all the points in a strange rectangular area, where we think two sets are different if there exists at least one point in one set of them but not in the other.
Input
The first line contains a single integer n (1 β€ n β€ 2 Γ 10^5) β the number of points on the plane.
The i-th of the next n lines contains two integers x_i, y_i (1 β€ x_i, y_i β€ 10^9) β the coordinates of the i-th point.
All points are distinct.
Output
Print a single integer β the number of different non-empty sets of points she can obtain.
Examples
Input
3
1 1
1 2
1 3
Output
3
Input
3
1 1
2 1
3 1
Output
6
Input
4
2 1
2 2
3 1
3 2
Output
6
Note
For the first example, there is exactly one set having k points for k = 1, 2, 3, so the total number is 3.
For the second example, the numbers of sets having k points for k = 1, 2, 3 are 3, 2, 1 respectively, and their sum is 6.
For the third example, as the following figure shows, there are
* 2 sets having one point;
* 3 sets having two points;
* 1 set having four points.
Therefore, the number of different non-empty sets in this example is 2 + 3 + 0 + 1 = 6.
<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.
This is an easier version of the next problem. In this version, q = 0.
A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal.
Let's define difficulty of a sequence as a minimum possible number of elements to change to get a nice sequence. However, if you change at least one element of value x to value y, you must also change all other elements of value x into y as well. For example, for [3, 3, 1, 3, 2, 1, 2] it isn't allowed to change first 1 to 3 and second 1 to 2. You need to leave 1's untouched or change them to the same value.
You are given a sequence of integers a_1, a_2, β¦, a_n and q updates.
Each update is of form "i x" β change a_i to x. Updates are not independent (the change stays for the future).
Print the difficulty of the initial sequence and of the sequence after every update.
Input
The first line contains integers n and q (1 β€ n β€ 200 000, q = 0), the length of the sequence and the number of the updates.
The second line contains n integers a_1, a_2, β¦, a_n (1 β€ a_i β€ 200 000), the initial sequence.
Each of the following q lines contains integers i_t and x_t (1 β€ i_t β€ n, 1 β€ x_t β€ 200 000), the position and the new value for this position.
Output
Print q+1 integers, the answer for the initial sequence and the answer after every update.
Examples
Input
5 0
3 7 3 7 3
Output
2
Input
10 0
1 2 1 2 3 1 1 1 50 1
Output
4
Input
6 0
6 6 3 3 4 4
Output
0
Input
7 0
3 3 1 3 2 1 2
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.
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya calls a number almost lucky if it could be evenly divided by some lucky number. Help him find out if the given number n is almost lucky.
Input
The single line contains an integer n (1 β€ n β€ 1000) β the number that needs to be checked.
Output
In the only line print "YES" (without the quotes), if number n is almost lucky. Otherwise, print "NO" (without the quotes).
Examples
Input
47
Output
YES
Input
16
Output
YES
Input
78
Output
NO
Note
Note that all lucky numbers are almost lucky as any number is evenly divisible by itself.
In the first sample 47 is a lucky number. In the second sample 16 is divisible by 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 n integers a_1, a_2, ..., a_n, such that for each 1β€ i β€ n holds i-nβ€ a_iβ€ i-1.
Find some nonempty subset of these integers, whose sum is equal to 0. It can be shown that such a subset exists under given constraints. If there are several possible subsets with zero-sum, you can find any of them.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1 β€ t β€ 10^6). The description of the test cases follows.
The first line of each test case contains a single integer n (1β€ n β€ 10^6).
The second line of each test case contains n integers a_1, a_2, ..., a_n (i-n β€ a_i β€ i-1).
It is guaranteed that the sum of n over all test cases does not exceed 10^6.
Output
For each test case, output two lines.
In the first line, output s (1β€ s β€ n) β the number of elements in your subset.
In the second line, output s integers i_1, i_2, ..., i_s (1β€ i_k β€ n). All integers have to be pairwise different, and a_{i_1} + a_{i_2} + ... + a_{i_s} has to be equal to 0. If there are several possible subsets with zero-sum, you can find any of them.
Example
Input
2
5
0 1 2 3 4
4
-3 1 1 1
Output
1
1
4
1 4 3 2
Note
In the first example, we get sum is a_1 = 0.
In the second example, we get sum is a_1 + a_4 + a_3 + a_2 = 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.
Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself.
<image>
Recently Xenia has bought n_r red gems, n_g green gems and n_b blue gems. Each of the gems has a weight.
Now, she is going to pick three gems.
Xenia loves colorful things, so she will pick exactly one gem of each color.
Xenia loves balance, so she will try to pick gems with little difference in weight.
Specifically, supposing the weights of the picked gems are x, y and z, Xenia wants to find the minimum value of (x-y)^2+(y-z)^2+(z-x)^2. As her dear friend, can you help her?
Input
The first line contains a single integer t (1β€ t β€ 100) β the number of test cases. Then t test cases follow.
The first line of each test case contains three integers n_r,n_g,n_b (1β€ n_r,n_g,n_bβ€ 10^5) β the number of red gems, green gems and blue gems respectively.
The second line of each test case contains n_r integers r_1,r_2,β¦,r_{n_r} (1β€ r_i β€ 10^9) β r_i is the weight of the i-th red gem.
The third line of each test case contains n_g integers g_1,g_2,β¦,g_{n_g} (1β€ g_i β€ 10^9) β g_i is the weight of the i-th green gem.
The fourth line of each test case contains n_b integers b_1,b_2,β¦,b_{n_b} (1β€ b_i β€ 10^9) β b_i is the weight of the i-th blue gem.
It is guaranteed that β n_r β€ 10^5, β n_g β€ 10^5, β n_b β€ 10^5 (the sum for all test cases).
Output
For each test case, print a line contains one integer β the minimum value which Xenia wants to find.
Example
Input
5
2 2 3
7 8
6 3
3 1 4
1 1 1
1
1
1000000000
2 2 2
1 2
5 4
6 7
2 2 2
1 2
3 4
6 7
3 4 1
3 2 1
7 3 3 4
6
Output
14
1999999996000000002
24
24
14
Note
In the first test case, Xenia has the following gems:
<image>
If she picks the red gem with weight 7, the green gem with weight 6, and the blue gem with weight 4, she will achieve the most balanced selection with (x-y)^2+(y-z)^2+(z-x)^2=(7-6)^2+(6-4)^2+(4-7)^2=14.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Levian works as an accountant in a large company. Levian knows how much the company has earned in each of the n consecutive months β in the i-th month the company had income equal to a_i (positive income means profit, negative income means loss, zero income means no change). Because of the general self-isolation, the first β n/2 β months income might have been completely unstable, but then everything stabilized and for the last β n/2 β months the income was the same.
Levian decided to tell the directors n-k+1 numbers β the total income of the company for each k consecutive months. In other words, for each i between 1 and n-k+1 he will say the value a_i + a_{i+1} + β¦ + a_{i + k - 1}. For example, if a=[-1, 0, 1, 2, 2] and k=3 he will say the numbers 0, 3, 5.
Unfortunately, if at least one total income reported by Levian is not a profit (income β€ 0), the directors will get angry and fire the failed accountant.
Save Levian's career: find any such k, that for each k months in a row the company had made a profit, or report that it is impossible.
Input
The first line contains a single integer n (2 β€ n β€ 5β
10^5) β the number of months for which Levian must account.
The second line contains β{n/2}β integers a_1, a_2, β¦, a_{β{n/2}β}, where a_i (-10^9 β€ a_i β€ 10^9) β the income of the company in the i-th month.
Third line contains a single integer x (-10^9 β€ x β€ 10^9) β income in every month from β{n/2}β + 1 to n.
Output
In a single line, print the appropriate integer k or -1, if it does not exist.
If there are multiple possible answers, you can print any.
Examples
Input
3
2 -1
2
Output
2
Input
5
2 2 -8
2
Output
-1
Input
6
-2 -2 6
-1
Output
4
Note
In the first example, k=2 and k=3 satisfy: in the first case, Levian will report the numbers 1, 1, and in the second case β one number 3.
In the second example, there is no such k.
In the third example, the only answer is k=4: he will report the numbers 1,2,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.
Acacius is studying strings theory. Today he came with the following problem.
You are given a string s of length n consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once?
Each question mark should be replaced with exactly one lowercase English letter. For example, string "a?b?c" can be transformed into strings "aabbc" and "azbzc", but can't be transformed into strings "aabc", "a?bbc" and "babbc".
Occurrence of a string t of length m in the string s of length n as a substring is a index i (1 β€ i β€ n - m + 1) such that string s[i..i+m-1] consisting of m consecutive symbols of s starting from i-th equals to string t. For example string "ababa" has two occurrences of a string "aba" as a substring with i = 1 and i = 3, but there are no occurrences of a string "aba" in the string "acba" as a substring.
Please help Acacius to check if it is possible to replace all question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulting string exactly once.
Input
First line of input contains an integer T (1 β€ T β€ 5000), number of test cases. T pairs of lines with test case descriptions follow.
The first line of a test case description contains a single integer n (7 β€ n β€ 50), length of a string s.
The second line of a test case description contains string s of length n consisting of lowercase English letters and question marks.
Output
For each test case output an answer for it.
In case if there is no way to replace question marks in string s with a lowercase English letters in such a way that there is exactly one occurrence of a string "abacaba" in the resulting string as a substring output "No".
Otherwise output "Yes" and in the next line output a resulting string consisting of n lowercase English letters. If there are multiple possible strings, output any.
You may print every letter in "Yes" and "No" in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer).
Example
Input
6
7
abacaba
7
???????
11
aba?abacaba
11
abacaba?aba
15
asdf???f???qwer
11
abacabacaba
Output
Yes
abacaba
Yes
abacaba
Yes
abadabacaba
Yes
abacabadaba
No
No
Note
In first example there is exactly one occurrence of a string "abacaba" in the string "abacaba" as a substring.
In second example seven question marks can be replaced with any seven lowercase English letters and with "abacaba" in particular.
In sixth example there are two occurrences of a string "abacaba" as a substring.
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.
Easy and hard versions are actually different problems, so we advise you to read both statements carefully.
You are given a weighted rooted tree, vertex 1 is the root of this tree. Also, each edge has its own cost.
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex v is the last different from v vertex on the path from the root to the vertex v. Children of vertex v are all vertices for which v is the parent. A vertex is a leaf if it has no children. The weighted tree is such a tree that each edge of this tree has some weight.
The weight of the path is the sum of edges weights on this path. The weight of the path from the vertex to itself is 0.
You can make a sequence of zero or more moves. On each move, you select an edge and divide its weight by 2 rounding down. More formally, during one move, you choose some edge i and divide its weight by 2 rounding down (w_i := \leftβ(w_i)/(2)\rightβ).
Each edge i has an associated cost c_i which is either 1 or 2 coins. Each move with edge i costs c_i coins.
Your task is to find the minimum total cost to make the sum of weights of paths from the root to each leaf at most S. In other words, if w(i, j) is the weight of the path from the vertex i to the vertex j, then you have to make β_{v β leaves} w(root, v) β€ S, where leaves is the list of all leaves.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1 β€ t β€ 2 β
10^4) β the number of test cases. Then t test cases follow.
The first line of the test case contains two integers n and S (2 β€ n β€ 10^5; 1 β€ S β€ 10^{16}) β the number of vertices in the tree and the maximum possible sum of weights you have to obtain. The next n-1 lines describe edges of the tree. The edge i is described as four integers v_i, u_i, w_i and c_i (1 β€ v_i, u_i β€ n; 1 β€ w_i β€ 10^6; 1 β€ c_i β€ 2), where v_i and u_i are vertices the edge i connects, w_i is the weight of this edge and c_i is the cost of this edge.
It is guaranteed that the sum of n does not exceed 10^5 (β n β€ 10^5).
Output
For each test case, print the answer: the minimum total cost required to make the sum of weights paths from the root to each leaf at most S.
Example
Input
4
4 18
2 1 9 2
3 2 4 1
4 1 1 2
3 20
2 1 8 1
3 1 7 2
5 50
1 3 100 1
1 5 10 2
2 3 123 2
5 4 55 1
2 100
1 2 409 2
Output
0
0
11
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.
Consider a long corridor which can be divided into n square cells of size 1 Γ 1. These cells are numbered from 1 to n from left to right.
There are two people in this corridor, a hooligan and a security guard. Initially, the hooligan is in the a-th cell, the guard is in the b-th cell (a β b).
<image> One of the possible situations. The corridor consists of 7 cells, the hooligan is in the 3-rd cell, the guard is in the 6-th (n = 7, a = 3, b = 6).
There are m firecrackers in the hooligan's pocket, the i-th firecracker explodes in s_i seconds after being lit.
The following events happen each second (sequentially, exactly in the following order):
1. firstly, the hooligan either moves into an adjacent cell (from the cell i, he can move to the cell (i + 1) or to the cell (i - 1), and he cannot leave the corridor) or stays in the cell he is currently. If the hooligan doesn't move, he can light one of his firecrackers and drop it. The hooligan can't move into the cell where the guard is;
2. secondly, some firecrackers that were already dropped may explode. Formally, if the firecracker j is dropped on the T-th second, then it will explode on the (T + s_j)-th second (for example, if a firecracker with s_j = 2 is dropped on the 4-th second, it explodes on the 6-th second);
3. finally, the guard moves one cell closer to the hooligan. If the guard moves to the cell where the hooligan is, the hooligan is caught.
Obviously, the hooligan will be caught sooner or later, since the corridor is finite. His goal is to see the maximum number of firecrackers explode before he is caught; that is, he will act in order to maximize the number of firecrackers that explodes before he is caught.
Your task is to calculate the number of such firecrackers, if the hooligan acts optimally.
Input
The first line contains one integer t (1 β€ t β€ 1000) β the number of test cases.
Each test case consists of two lines. The first line contains four integers n, m, a and b (2 β€ n β€ 10^9; 1 β€ m β€ 2 β
10^5; 1 β€ a, b β€ n; a β b) β the size of the corridor, the number of firecrackers, the initial location of the hooligan and the initial location of the guard, respectively.
The second line contains m integers s_1, s_2, ..., s_m (1 β€ s_i β€ 10^9), where s_i is the time it takes the i-th firecracker to explode after it is lit.
It is guaranteed that the sum of m over all test cases does not exceed 2 β
10^5.
Output
For each test case, print one integer β the maximum number of firecrackers that the hooligan can explode before he is caught.
Example
Input
3
7 2 3 6
1 4
7 2 3 6
5 1
7 2 3 6
4 4
Output
2
1
1
Note
In the first test case, the hooligan should act, for example, as follows:
* second 1: drop the second firecracker, so it will explode on the 5-th second. The guard moves to the cell 5;
* second 2: move to the cell 2. The guard moves to the cell 4;
* second 3: drop the first firecracker, so it will explode on the 4-th second. The guard moves to the cell 3;
* second 4: move to the cell 1. The first firecracker explodes. The guard moves to the cell 2;
* second 5: stay in the cell 1. The second firecracker explodes. The guard moves to the cell 1 and catches the hooligan.
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.
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: s of length n and t of length m.
A sequence p_1, p_2, β¦, p_m, where 1 β€ p_1 < p_2 < β¦ < p_m β€ n, is called beautiful, if s_{p_i} = t_i for all i from 1 to m. The width of a sequence is defined as max_{1 β€ i < m} \left(p_{i + 1} - p_i\right).
Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings s and t there is at least one beautiful sequence.
Input
The first input line contains two integers n and m (2 β€ m β€ n β€ 2 β
10^5) β the lengths of the strings s and t.
The following line contains a single string s of length n, consisting of lowercase letters of the Latin alphabet.
The last line contains a single string t of length m, consisting of lowercase letters of the Latin alphabet.
It is guaranteed that there is at least one beautiful sequence for the given strings.
Output
Output one integer β the maximum width of a beautiful sequence.
Examples
Input
5 3
abbbc
abc
Output
3
Input
5 2
aaaaa
aa
Output
4
Input
5 5
abcdf
abcdf
Output
1
Input
2 2
ab
ab
Output
1
Note
In the first example there are two beautiful sequences of width 3: they are \{1, 2, 5\} and \{1, 4, 5\}.
In the second example the beautiful sequence with the maximum width is \{1, 5\}.
In the third example there is exactly one beautiful sequence β it is \{1, 2, 3, 4, 5\}.
In the fourth example there is exactly one beautiful sequence β it is \{1, 2\}.
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
To satisfy his love of matching socks, Phoenix has brought his n socks (n is even) to the sock store. Each of his socks has a color c_i and is either a left sock or right sock.
Phoenix can pay one dollar to the sock store to either:
* recolor a sock to any color c' (1 β€ c' β€ n)
* turn a left sock into a right sock
* turn a right sock into a left sock
The sock store may perform each of these changes any number of times. Note that the color of a left sock doesn't change when it turns into a right sock, and vice versa.
A matching pair of socks is a left and right sock with the same color. What is the minimum cost for Phoenix to make n/2 matching pairs? Each sock must be included in exactly one matching pair.
Input
The input consists of multiple test cases. The first line contains an integer t (1 β€ t β€ 1000) β the number of test cases.
The first line of each test case contains three integers n, l, and r (2 β€ n β€ 2 β
10^5; n is even; 0 β€ l, r β€ n; l+r=n) β the total number of socks, and the number of left and right socks, respectively.
The next line contains n integers c_i (1 β€ c_i β€ n) β the colors of the socks. The first l socks are left socks, while the next r socks are right socks.
It is guaranteed that the sum of n across all the test cases will not exceed 2 β
10^5.
Output
For each test case, print one integer β the minimum cost for Phoenix to make n/2 matching pairs. Each sock must be included in exactly one matching pair.
Example
Input
4
6 3 3
1 2 3 2 2 2
6 2 4
1 1 2 2 2 2
6 5 1
6 5 4 3 2 1
4 0 4
4 4 4 3
Output
2
3
5
3
Note
In the first test case, Phoenix can pay 2 dollars to:
* recolor sock 1 to color 2
* recolor sock 3 to color 2
There are now 3 matching pairs. For example, pairs (1, 4), (2, 5), and (3, 6) are matching.
In the second test case, Phoenix can pay 3 dollars to:
* turn sock 6 from a right sock to a left sock
* recolor sock 3 to color 1
* recolor sock 4 to color 1
There are now 3 matching pairs. For example, pairs (1, 3), (2, 4), and (5, 6) are matching.
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 f(i) denote the minimum positive integer x such that x is not a divisor of i.
Compute β_{i=1}^n f(i) modulo 10^9+7. In other words, compute f(1)+f(2)+...+f(n) modulo 10^9+7.
Input
The first line contains a single integer t (1β€ tβ€ 10^4), the number of test cases. Then t cases follow.
The only line of each test case contains a single integer n (1β€ nβ€ 10^{16}).
Output
For each test case, output a single integer ans, where ans=β_{i=1}^n f(i) modulo 10^9+7.
Example
Input
6
1
2
3
4
10
10000000000000000
Output
2
5
7
10
26
366580019
Note
In the fourth test case n=4, so ans=f(1)+f(2)+f(3)+f(4).
* 1 is a divisor of 1 but 2 isn't, so 2 is the minimum positive integer that isn't a divisor of 1. Thus, f(1)=2.
* 1 and 2 are divisors of 2 but 3 isn't, so 3 is the minimum positive integer that isn't a divisor of 2. Thus, f(2)=3.
* 1 is a divisor of 3 but 2 isn't, so 2 is the minimum positive integer that isn't a divisor of 3. Thus, f(3)=2.
* 1 and 2 are divisors of 4 but 3 isn't, so 3 is the minimum positive integer that isn't a divisor of 4. Thus, f(4)=3.
Therefore, ans=f(1)+f(2)+f(3)+f(4)=2+3+2+3=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.
Reca company makes monitors, the most popular of their models is AB999 with the screen size a Γ b centimeters. Because of some production peculiarities a screen parameters are integer numbers. Recently the screen sides ratio x: y became popular with users. That's why the company wants to reduce monitor AB999 size so that its screen sides ratio becomes x: y, at the same time they want its total area to be maximal of all possible variants. Your task is to find the screen parameters of the reduced size model, or find out that such a reduction can't be performed.
Input
The first line of the input contains 4 integers β a, b, x and y (1 β€ a, b, x, y β€ 2Β·109).
Output
If the answer exists, output 2 positive integers β screen parameters of the reduced size model. Output 0 0 otherwise.
Examples
Input
800 600 4 3
Output
800 600
Input
1920 1200 16 9
Output
1920 1080
Input
1 1 1 2
Output
0 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.
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?
You are given a system of equations:
<image>
You should count, how many there are pairs of integers (a, b) (0 β€ a, b) which satisfy the system.
Input
A single line contains two integers n, m (1 β€ n, m β€ 1000) β the parameters of the system. The numbers on the line are separated by a space.
Output
On a single line print the answer to the problem.
Examples
Input
9 3
Output
1
Input
14 28
Output
1
Input
4 20
Output
0
Note
In the first sample the suitable pair is integers (3, 0). In the second sample the suitable pair is integers (3, 5). In the third sample there is no suitable pair.
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.
Maxim has opened his own restaurant! The restaurant has got a huge table, the table's length is p meters.
Maxim has got a dinner party tonight, n guests will come to him. Let's index the guests of Maxim's restaurant from 1 to n. Maxim knows the sizes of all guests that are going to come to him. The i-th guest's size (ai) represents the number of meters the guest is going to take up if he sits at the restaurant table.
Long before the dinner, the guests line up in a queue in front of the restaurant in some order. Then Maxim lets the guests in, one by one. Maxim stops letting the guests in when there is no place at the restaurant table for another guest in the queue. There is no place at the restaurant table for another guest in the queue, if the sum of sizes of all guests in the restaurant plus the size of this guest from the queue is larger than p. In this case, not to offend the guest who has no place at the table, Maxim doesn't let any other guest in the restaurant, even if one of the following guests in the queue would have fit in at the table.
Maxim is now wondering, what is the average number of visitors who have come to the restaurant for all possible n! orders of guests in the queue. Help Maxim, calculate this number.
Input
The first line contains integer n (1 β€ n β€ 50) β the number of guests in the restaurant. The next line contains integers a1, a2, ..., an (1 β€ ai β€ 50) β the guests' sizes in meters. The third line contains integer p (1 β€ p β€ 50) β the table's length in meters.
The numbers in the lines are separated by single spaces.
Output
In a single line print a real number β the answer to the problem. The answer will be considered correct, if the absolute or relative error doesn't exceed 10 - 4.
Examples
Input
3
1 2 3
3
Output
1.3333333333
Note
In the first sample the people will come in the following orders:
* (1, 2, 3) β there will be two people in the restaurant;
* (1, 3, 2) β there will be one person in the restaurant;
* (2, 1, 3) β there will be two people in the restaurant;
* (2, 3, 1) β there will be one person in the restaurant;
* (3, 1, 2) β there will be one person in the restaurant;
* (3, 2, 1) β there will be one person in the restaurant.
In total we get (2 + 1 + 2 + 1 + 1 + 1) / 6 = 8 / 6 = 1.(3).
The input will be give
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Gerald is very particular to eight point sets. He thinks that any decent eight point set must consist of all pairwise intersections of three distinct integer vertical straight lines and three distinct integer horizontal straight lines, except for the average of these nine points. In other words, there must be three integers x1, x2, x3 and three more integers y1, y2, y3, such that x1 < x2 < x3, y1 < y2 < y3 and the eight point set consists of all points (xi, yj) (1 β€ i, j β€ 3), except for point (x2, y2).
You have a set of eight points. Find out if Gerald can use this set?
Input
The input consists of eight lines, the i-th line contains two space-separated integers xi and yi (0 β€ xi, yi β€ 106). You do not have any other conditions for these points.
Output
In a single line print word "respectable", if the given set of points corresponds to Gerald's decency rules, and "ugly" otherwise.
Examples
Input
0 0
0 1
0 2
1 0
1 2
2 0
2 1
2 2
Output
respectable
Input
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
Output
ugly
Input
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
Output
ugly
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.