question
large_stringlengths 265
13.2k
|
|---|
Solve the programming task below in a Python markdown code block.
Taro and Hanako have numbers of cards in their hands. Each of the cards has a score on it. Taro and Hanako wish to make the total scores of their cards equal by exchanging one card in one's hand with one card in the other's hand. Which of the cards should be exchanged with which?
Note that they have to exchange their cards even if they already have cards of the same total score.
Input
The input consists of a number of datasets. Each dataset is formatted as follows.
> n m
> s1
> s2
> ...
> sn
> sn+1
> sn+2
> ...
> sn+m
>
The first line of a dataset contains two numbers n and m delimited by a space, where n is the number of cards that Taro has and m is the number of cards that Hanako has. The subsequent n+m lines list the score for each of the cards, one score per line. The first n scores (from s1 up to sn) are the scores of Taro's cards and the remaining m scores (from sn+1 up to sn+m) are Hanako's.
The numbers n and m are positive integers no greater than 100. Each score is a non-negative integer no greater than 100.
The end of the input is indicated by a line containing two zeros delimited by a single space.
Output
For each dataset, output a single line containing two numbers delimited by a single space, where the first number is the score of the card Taro gives to Hanako and the second number is the score of the card Hanako gives to Taro. If there is more than one way to exchange a pair of cards that makes the total scores equal, output a pair of scores whose sum is the smallest.
In case no exchange can make the total scores equal, output a single line containing solely -1. The output must not contain any superfluous characters that do not conform to the format.
Sample Input
2 2
1
5
3
7
6 5
3
9
5
2
3
3
12
2
7
3
5
4 5
10
0
3
8
1
9
6
0
6
7 4
1
1
2
1
2
1
4
2
3
4
3
2 3
1
1
2
2
2
0 0
Output for the Sample Input
1 3
3 5
-1
2 2
-1
Example
Input
2 2
1
5
3
7
6 5
3
9
5
2
3
3
12
2
7
3
5
4 5
10
0
3
8
1
9
6
0
6
7 4
1
1
2
1
2
1
4
2
3
4
3
2 3
1
1
2
2
2
0 0
Output
1 3
3 5
-1
2 2
-1
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Inzane finally found Zane with a lot of money to spare, so they together decided to establish a country of their own.
Ruling a country is not an easy job. Thieves and terrorists are always ready to ruin the country's peace. To fight back, Zane and Inzane have enacted a very effective law: from each city it must be possible to reach a police station by traveling at most d kilometers along the roads. [Image]
There are n cities in the country, numbered from 1 to n, connected only by exactly n - 1 roads. All roads are 1 kilometer long. It is initially possible to travel from a city to any other city using these roads. The country also has k police stations located in some cities. In particular, the city's structure satisfies the requirement enforced by the previously mentioned law. Also note that there can be multiple police stations in one city.
However, Zane feels like having as many as n - 1 roads is unnecessary. The country is having financial issues, so it wants to minimize the road maintenance cost by shutting down as many roads as possible.
Help Zane find the maximum number of roads that can be shut down without breaking the law. Also, help him determine such roads.
-----Input-----
The first line contains three integers n, k, and d (2 ≤ n ≤ 3·10^5, 1 ≤ k ≤ 3·10^5, 0 ≤ d ≤ n - 1) — the number of cities, the number of police stations, and the distance limitation in kilometers, respectively.
The second line contains k integers p_1, p_2, ..., p_{k} (1 ≤ p_{i} ≤ n) — each denoting the city each police station is located in.
The i-th of the following n - 1 lines contains two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n, u_{i} ≠ v_{i}) — the cities directly connected by the road with index i.
It is guaranteed that it is possible to travel from one city to any other city using only the roads. Also, it is possible from any city to reach a police station within d kilometers.
-----Output-----
In the first line, print one integer s that denotes the maximum number of roads that can be shut down.
In the second line, print s distinct integers, the indices of such roads, in any order.
If there are multiple answers, print any of them.
-----Examples-----
Input
6 2 4
1 6
1 2
2 3
3 4
4 5
5 6
Output
1
5
Input
6 3 2
1 5 6
1 2
1 3
1 4
1 5
5 6
Output
2
4 5
-----Note-----
In the first sample, if you shut down road 5, all cities can still reach a police station within k = 4 kilometers.
In the second sample, although this is the only largest valid set of roads that can be shut down, you can print either 4 5 or 5 4 in the second line.
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 frog living in a big pond. He loves jumping between lotus leaves floating on the pond. Interestingly, these leaves have strange habits. First, a leaf will sink into the water after the frog jumps from it. Second, they are aligned regularly as if they are placed on the grid points as in the example below.
<image>
Figure 1: Example of floating leaves
Recently, He came up with a puzzle game using these habits. At the beginning of the game, he is on some leaf and faces to the upper, lower, left or right side. He can jump forward or to the left or right relative to his facing direction, but not backward or diagonally. For example, suppose he is facing to the left side, then he can jump to the left, upper and lower sides but not to the right side. In each jump, he will land on the nearest leaf on his jumping direction and face to that direction regardless of his previous state. The leaf he was on will vanish into the water after the jump. The goal of this puzzle is to jump from leaf to leaf until there is only one leaf remaining.
See the example shown in the figure below.
<image>
In this situation, he has three choices, namely, the leaves A, B and C. Note that he cannot jump to the leaf D since he cannot jump backward. Suppose that he choose the leaf B. After jumping there, the situation will change as shown in the following figure.
He can jump to either leaf E or F next.
After some struggles, he found this puzzle difficult, since there are a lot of leaves on the pond. Can you help him to find out a solution?
<image>
Input
H W
c1,1 ... c1,W
.
.
.
cH,1 ... cH,W
The first line of the input contains two positive integers H and W (1 ≤ H,W ≤ 10). The following H lines, which contain W characters each, describe the initial configuration of the leaves and the frog using following characters:
* '.’ : water
* ‘o’ : a leaf
* ‘U’ : a frog facing upward (i.e. to the upper side) on a leaf
* ‘D’ : a frog facing downward (i.e. to the lower side) on a leaf
* ‘L’ : a frog facing leftward (i.e. to the left side) on a leaf
* ‘R’ : a frog facing rightward (i.e. to the right side) on a leaf
You can assume that there is only one frog in each input. You can also assume that the total number of leaves (including the leaf the frog is initially on) is at most 30.
Output
Output a line consists of the characters ‘U’ (up), ‘D’ (down), ‘L’ (left) and ‘R’ (right) that describes a series of movements. The output should not contain any other characters, such as spaces. You can assume that there exists only one solution for each input.
Examples
Input
2 3
Uo.
.oo
Output
RDR
Input
10 10
.o....o...
o.oo......
..oo..oo..
..o.......
..oo..oo..
..o...o.o.
o..U.o....
oo......oo
oo........
oo..oo....
Output
URRULULDDLUURDLLLURRDLDDDRRDR
Input
10 1
D
.
.
.
.
.
.
.
.
o
Output
D
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Given any integer x, Aoki can do the operation below.
Operation: Replace x with the absolute difference of x and K.
You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times.
-----Constraints-----
- 0 ≤ N ≤ 10^{18}
- 1 ≤ K ≤ 10^{18}
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
N K
-----Output-----
Print the minimum possible value taken by N after Aoki does the operation zero or more times.
-----Sample Input-----
7 4
-----Sample Output-----
1
Initially, N=7.
After one operation, N becomes |7-4| = 3.
After two operations, N becomes |3-4| = 1, which is the minimum value taken by N.
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 you are the best Wraith King, Nizhniy Magazin «Mir» at the centre of Vinnytsia is offering you a discount.
You are given an array a of length n and an integer c.
The value of some array b of length k is the sum of its elements except for the $\lfloor \frac{k}{c} \rfloor$ smallest. For example, the value of the array [3, 1, 6, 5, 2] with c = 2 is 3 + 6 + 5 = 14.
Among all possible partitions of a into contiguous subarrays output the smallest possible sum of the values of these subarrays.
-----Input-----
The first line contains integers n and c (1 ≤ n, c ≤ 100 000).
The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^9) — elements of a.
-----Output-----
Output a single integer — the smallest possible sum of values of these subarrays of some partition of a.
-----Examples-----
Input
3 5
1 2 3
Output
6
Input
12 10
1 1 10 10 10 10 10 10 9 10 10 10
Output
92
Input
7 2
2 3 6 4 5 7 1
Output
17
Input
8 4
1 3 4 5 5 3 4 1
Output
23
-----Note-----
In the first example any partition yields 6 as the sum.
In the second example one of the optimal partitions is [1, 1], [10, 10, 10, 10, 10, 10, 9, 10, 10, 10] with the values 2 and 90 respectively.
In the third example one of the optimal partitions is [2, 3], [6, 4, 5, 7], [1] with the values 3, 13 and 1 respectively.
In the fourth example one of the optimal partitions is [1], [3, 4, 5, 5, 3, 4], [1] with the values 1, 21 and 1 respectively.
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 sequence of digits of length $2^k$ $[a_1, a_2, \ldots, a_{2^k}]$. We perform the following operation with it: replace pairs $(a_{2i+1}, a_{2i+2})$ with $(a_{2i+1} + a_{2i+2})\bmod 10$ for $0\le i<2^{k-1}$. For every $i$ where $a_{2i+1} + a_{2i+2}\ge 10$ we get a candy! As a result, we will get a sequence of length $2^{k-1}$.
Less formally, we partition sequence of length $2^k$ into $2^{k-1}$ pairs, each consisting of 2 numbers: the first pair consists of the first and second numbers, the second of the third and fourth $\ldots$, the last pair consists of the ($2^k-1$)-th and ($2^k$)-th numbers. For every pair such that sum of numbers in it is at least $10$, we get a candy. After that, we replace every pair of numbers with a remainder of the division of their sum by $10$ (and don't change the order of the numbers).
Perform this operation with a resulting array until it becomes of length $1$. Let $f([a_1, a_2, \ldots, a_{2^k}])$ denote the number of candies we get in this process.
For example: if the starting sequence is $[8, 7, 3, 1, 7, 0, 9, 4]$ then:
After the first operation the sequence becomes $[(8 + 7)\bmod 10, (3 + 1)\bmod 10, (7 + 0)\bmod 10, (9 + 4)\bmod 10]$ $=$ $[5, 4, 7, 3]$, and we get $2$ candies as $8 + 7 \ge 10$ and $9 + 4 \ge 10$.
After the second operation the sequence becomes $[(5 + 4)\bmod 10, (7 + 3)\bmod 10]$ $=$ $[9, 0]$, and we get one more candy as $7 + 3 \ge 10$.
After the final operation sequence becomes $[(9 + 0) \bmod 10]$ $=$ $[9]$.
Therefore, $f([8, 7, 3, 1, 7, 0, 9, 4]) = 3$ as we got $3$ candies in total.
You are given a sequence of digits of length $n$ $s_1, s_2, \ldots s_n$. You have to answer $q$ queries of the form $(l_i, r_i)$, where for $i$-th query you have to output $f([s_{l_i}, s_{l_i+1}, \ldots, s_{r_i}])$. It is guaranteed that $r_i-l_i+1$ is of form $2^k$ for some nonnegative integer $k$.
-----Input-----
The first line contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the sequence.
The second line contains $n$ digits $s_1, s_2, \ldots, s_n$ ($0 \le s_i \le 9$).
The third line contains a single integer $q$ ($1 \le q \le 10^5$) — the number of queries.
Each of the next $q$ lines contains two integers $l_i$, $r_i$ ($1 \le l_i \le r_i \le n$) — $i$-th query. It is guaranteed that $r_i-l_i+1$ is a nonnegative integer power of $2$.
-----Output-----
Output $q$ lines, in $i$-th line output single integer — $f([s_{l_i}, s_{l_i + 1}, \ldots, s_{r_i}])$, answer to the $i$-th query.
-----Examples-----
Input
8
8 7 3 1 7 0 9 4
3
1 8
2 5
7 7
Output
3
1
0
Input
6
0 1 2 3 3 5
3
1 2
1 4
3 6
Output
0
0
1
-----Note-----
The first example illustrates an example from the statement.
$f([7, 3, 1, 7]) = 1$: sequence of operations is $[7, 3, 1, 7] \to [(7 + 3)\bmod 10, (1 + 7)\bmod 10]$ $=$ $[0, 8]$ and one candy as $7 + 3 \ge 10$ $\to$ $[(0 + 8) \bmod 10]$ $=$ $[8]$, so we get only $1$ candy.
$f([9]) = 0$ as we don't perform operations with it.
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her.
Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain order (one after another, in this order strictly), which is specified by permutation of letters' indices of the word t: a1... a|t|. We denote the length of word x as |x|. Note that after removing one letter, the indices of other letters don't change. For example, if t = "nastya" and a = [4, 1, 5, 3, 2, 6] then removals make the following sequence of words "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya" <image> "nastya".
Sergey knows this permutation. His goal is to stop his sister at some point and continue removing by himself to get the word p. Since Nastya likes this activity, Sergey wants to stop her as late as possible. Your task is to determine, how many letters Nastya can remove before she will be stopped by Sergey.
It is guaranteed that the word p can be obtained by removing the letters from word t.
Input
The first and second lines of the input contain the words t and p, respectively. Words are composed of lowercase letters of the Latin alphabet (1 ≤ |p| < |t| ≤ 200 000). It is guaranteed that the word p can be obtained by removing the letters from word t.
Next line contains a permutation a1, a2, ..., a|t| of letter indices that specifies the order in which Nastya removes letters of t (1 ≤ ai ≤ |t|, all ai are distinct).
Output
Print a single integer number, the maximum number of letters that Nastya can remove.
Examples
Input
ababcba
abb
5 3 4 1 7 6 2
Output
3
Input
bbbabb
bb
1 6 3 4 2 5
Output
4
Note
In the first sample test sequence of removing made by Nastya looks like this:
"ababcba" <image> "ababcba" <image> "ababcba" <image> "ababcba"
Nastya can not continue, because it is impossible to get word "abb" from word "ababcba".
So, Nastya will remove only three letters.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
A permutation of N numbers from 1 to N A1, A2, ……, AN is given. You can perform the operation reverse (i, j) to reverse the order of the numbers in the interval [i, j] (1 ≤ i ≤ j ≤ N) for this permutation. For example, if reverse (2, 4) is applied to [1, 2, 3, 4, 5], it becomes [1, 4, 3, 2, 5]. Calculate how many operations you need to do at a minimum to get the permutation sorted in ascending order.
Constraints
* N is an integer
* 2 ≤ N ≤ 10
Input
The input is given in the following format.
> N
> A1 A2 …… AN
>
Output
Print the solution to the problem on one line.
Examples
Input
5
1 4 3 5 2
Output
2
Input
5
3 1 5 2 4
Output
4
Input
3
1 2 3
Output
0
Input
10
3 1 5 2 7 4 9 6 10 8
Output
9
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
There are many blue cards and red cards on the table. For each card, an integer number greater than 1 is printed on its face. The same number may be printed on several cards.
A blue card and a red card can be paired when both of the numbers printed on them have a common divisor greater than 1. There may be more than one red card that can be paired with one blue card. Also, there may be more than one blue card that can be paired with one red card. When a blue card and a red card are chosen and paired, these two cards are removed from the whole cards on the table.
<image>
Figure E-1: Four blue cards and three red cards
For example, in Figure E-1, there are four blue cards and three red cards. Numbers 2, 6, 6 and 15 are printed on the faces of the four blue cards, and 2, 3 and 35 are printed on those of the three red cards. Here, you can make pairs of blue cards and red cards as follows. First, the blue card with number 2 on it and the red card with 2 are paired and removed. Second, one of the two blue cards with 6 and the red card with 3 are paired and removed. Finally, the blue card with 15 and the red card with 35 are paired and removed. Thus the number of removed pairs is three.
Note that the total number of the pairs depends on the way of choosing cards to be paired. The blue card with 15 and the red card with 3 might be paired and removed at the beginning. In this case, there are only one more pair that can be removed and the total number of the removed pairs is two.
Your job is to find the largest number of pairs that can be removed from the given set of cards on the table.
Input
The input is a sequence of datasets. The number of the datasets is less than or equal to 100. Each dataset is formatted as follows.
> m n
> b1 ... bk ... bm
> r1 ... rk ... rn
>
The integers m and n are the number of blue cards and that of red cards, respectively. You may assume 1 ≤ m ≤ 500 and 1≤ n ≤ 500. bk (1 ≤ k ≤ m) and rk (1 ≤ k ≤ n) are numbers printed on the blue cards and the red cards respectively, that are integers greater than or equal to 2 and less than 10000000 (=107). The input integers are separated by a space or a newline. Each of bm and rn is followed by a newline. There are no other characters in the dataset.
The end of the input is indicated by a line containing two zeros separated by a space.
Output
For each dataset, output a line containing an integer that indicates the maximum of the number of the pairs.
Sample Input
4 3
2 6 6 15
2 3 5
2 3
4 9
8 16 32
4 2
4 9 11 13
5 7
5 5
2 3 5 1001 1001
7 11 13 30 30
10 10
2 3 5 7 9 11 13 15 17 29
4 6 10 14 18 22 26 30 34 38
20 20
195 144 903 63 137 513 44 626 75 473
876 421 568 519 755 840 374 368 570 872
363 650 155 265 64 26 426 391 15 421
373 984 564 54 823 477 565 866 879 638
100 100
195 144 903 63 137 513 44 626 75 473
876 421 568 519 755 840 374 368 570 872
363 650 155 265 64 26 426 391 15 421
373 984 564 54 823 477 565 866 879 638
117 755 835 683 52 369 302 424 513 870
75 874 299 228 140 361 30 342 750 819
761 123 804 325 952 405 578 517 49 457
932 941 988 767 624 41 912 702 241 426
351 92 300 648 318 216 785 347 556 535
166 318 434 746 419 386 928 996 680 975
231 390 916 220 933 319 37 846 797 54
272 924 145 348 350 239 563 135 362 119
446 305 213 879 51 631 43 755 405 499
509 412 887 203 408 821 298 443 445 96
274 715 796 417 839 147 654 402 280 17
298 725 98 287 382 923 694 201 679 99
699 188 288 364 389 694 185 464 138 406
558 188 897 354 603 737 277 35 139 556
826 213 59 922 499 217 846 193 416 525
69 115 489 355 256 654 49 439 118 961
0 0
Output for the Sample Input
3
1
0
4
9
18
85
Example
Input
4 3
2 6 6 15
2 3 5
2 3
4 9
8 16 32
4 2
4 9 11 13
5 7
5 5
2 3 5 1001 1001
7 11 13 30 30
10 10
2 3 5 7 9 11 13 15 17 29
4 6 10 14 18 22 26 30 34 38
20 20
195 144 903 63 137 513 44 626 75 473
876 421 568 519 755 840 374 368 570 872
363 650 155 265 64 26 426 391 15 421
373 984 564 54 823 477 565 866 879 638
100 100
195 144 903 63 137 513 44 626 75 473
876 421 568 519 755 840 374 368 570 872
363 650 155 265 64 26 426 391 15 421
373 984 564 54 823 477 565 866 879 638
117 755 835 683 52 369 302 424 513 870
75 874 299 228 140 361 30 342 750 819
761 123 804 325 952 405 578 517 49 457
932 941 988 767 624 41 912 702 241 426
351 92 300 648 318 216 785 347 556 535
166 318 434 746 419 386 928 996 680 975
231 390 916 220 933 319 37 846 797 54
272 924 145 348 350 239 563 135 362 119
446 305 213 879 51 631 43 755 405 499
509 412 887 203 408 821 298 443 445 96
274 715 796 417 839 147 654 402 280 17
298 725 98 287 382 923 694 201 679 99
699 188 288 364 389 694 185 464 138 406
558 188 897 354 603 737 277 35 139 556
826 213 59 922 499 217 846 193 416 525
69 115 489 355 256 654 49 439 118 961
0 0
Output
3
1
0
4
9
18
85
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
During her tantrums the princess usually smashes some collectable porcelain. Every furious shriek is accompanied with one item smashed.
The collection of porcelain is arranged neatly on n shelves. Within each shelf the items are placed in one row, so that one can access only the outermost items — the leftmost or the rightmost item, not the ones in the middle of the shelf. Once an item is taken, the next item on that side of the shelf can be accessed (see example). Once an item is taken, it can't be returned to the shelves.
You are given the values of all items. Your task is to find the maximal damage the princess' tantrum of m shrieks can inflict on the collection of porcelain.
Input
The first line of input data contains two integers n (1 ≤ n ≤ 100) and m (1 ≤ m ≤ 10000). The next n lines contain the values of the items on the shelves: the first number gives the number of items on this shelf (an integer between 1 and 100, inclusive), followed by the values of the items (integers between 1 and 100, inclusive), in the order in which they appear on the shelf (the first number corresponds to the leftmost item, the last one — to the rightmost one). The total number of items is guaranteed to be at least m.
Output
Output the maximal total value of a tantrum of m shrieks.
Examples
Input
2 3
3 3 7 2
3 4 1 5
Output
15
Input
1 3
4 4 3 1 2
Output
9
Note
In the first case there are two shelves, each with three items. To maximize the total value of the items chosen, one can take two items from the left side of the first shelf and one item from the right side of the second shelf.
In the second case there is only one shelf, so all three items are taken from it — two from the left side and one from the right side.
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 integer $n$ ($n > 0$). Find any integer $s$ which satisfies these conditions, or report that there are no such numbers:
In the decimal representation of $s$: $s > 0$, $s$ consists of $n$ digits, no digit in $s$ equals $0$, $s$ is not divisible by any of it's digits.
-----Input-----
The input consists of multiple test cases. The first line of the input contains a single integer $t$ ($1 \leq t \leq 400$), the number of test cases. The next $t$ lines each describe a test case.
Each test case contains one positive integer $n$ ($1 \leq n \leq 10^5$).
It is guaranteed that the sum of $n$ for all test cases does not exceed $10^5$.
-----Output-----
For each test case, print an integer $s$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $s$, print any solution.
-----Example-----
Input
4
1
2
3
4
Output
-1
57
239
6789
-----Note-----
In the first test case, there are no possible solutions for $s$ consisting of one digit, because any such solution is divisible by itself.
For the second test case, the possible solutions are: $23$, $27$, $29$, $34$, $37$, $38$, $43$, $46$, $47$, $49$, $53$, $54$, $56$, $57$, $58$, $59$, $67$, $68$, $69$, $73$, $74$, $76$, $78$, $79$, $83$, $86$, $87$, $89$, $94$, $97$, and $98$.
For the third test case, one possible solution is $239$ because $239$ is not divisible by $2$, $3$ or $9$ and has three digits (none of which equals zero).
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 boy Petya loves chess very much. He even came up with a chess piece of his own, a semiknight. The semiknight can move in any of these four directions: 2 squares forward and 2 squares to the right, 2 squares forward and 2 squares to the left, 2 squares backward and 2 to the right and 2 squares backward and 2 to the left. Naturally, the semiknight cannot move beyond the limits of the chessboard.
Petya put two semiknights on a standard chessboard. Petya simultaneously moves with both semiknights. The squares are rather large, so after some move the semiknights can meet, that is, they can end up in the same square. After the meeting the semiknights can move on, so it is possible that they meet again. Petya wonders if there is such sequence of moves when the semiknights meet. Petya considers some squares bad. That is, they do not suit for the meeting. The semiknights can move through these squares but their meetings in these squares don't count.
Petya prepared multiple chess boards. Help Petya find out whether the semiknights can meet on some good square for each board.
Please see the test case analysis.
-----Input-----
The first line contains number t (1 ≤ t ≤ 50) — the number of boards. Each board is described by a matrix of characters, consisting of 8 rows and 8 columns. The matrix consists of characters ".", "#", "K", representing an empty good square, a bad square and the semiknight's position, correspondingly. It is guaranteed that matrix contains exactly 2 semiknights. The semiknight's squares are considered good for the meeting. The tests are separated by empty line.
-----Output-----
For each test, print on a single line the answer to the problem: "YES", if the semiknights can meet and "NO" otherwise.
-----Examples-----
Input
2
........
........
......#.
K..##..#
.......#
...##..#
......#.
K.......
........
........
..#.....
..#..#..
..####..
...##...
........
....K#K#
Output
YES
NO
-----Note-----
Consider the first board from the sample. We will assume the rows and columns of the matrix to be numbered 1 through 8 from top to bottom and from left to right, correspondingly. The knights can meet, for example, in square (2, 7). The semiknight from square (4, 1) goes to square (2, 3) and the semiknight goes from square (8, 1) to square (6, 3). Then both semiknights go to (4, 5) but this square is bad, so they move together to square (2, 7).
On the second board the semiknights will never meet.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
One day, Ikta, an elementary school student, received a piece of paper with mathematical formulas from his grandfather. Apparently, the grandfather will give you as much money as the answer to the formula. Ikta has only learned addition, subtraction, and multiplication, so only addition, subtraction, and multiplication are used in mathematical formulas. In normal calculation, multiplication must be calculated before addition and subtraction, but Ikta had a vague understanding of operator precedence, so for the time being, it is convenient to maximize the calculation result of the formula. I decided to consider a good priority.
Given the three binary operators + − × and a formula containing parentheses. Change the precedence of the three operators as you like and answer the calculation result when the formula is maximized.
However, note the following points.
* Operators are always left-associative. (Operators with the same precedence are always calculated from the left side of the formula.)
* Different operators may have the same precedence.
* Do not change the priority while calculating one formula.
Input
The input is given in the following format.
A formula consisting of numbers from 0 to 9 and the operators'+','-','*' and parentheses'(',')'
* To be precise, the input is in the format shown in BNF below.
> <expr> :: = (<expr>) | <number> | <expr> <op> <expr>
> <op> :: = + |-| *
<number> represents a non-negative integer.
Constraints
The input satisfies the following constraints.
* The formula is 200 characters or less.
* No matter what priority is set, it will not overflow as a result of calculation or in the middle of it as a 64-bit integer type.
Output
Output the maximum value obtained from the formula in one line.
Examples
Input
3-2*3
Output
3
Input
(5-3*4)*(0-2+1)
Output
21
Input
1-2+3-4+5-6*0
Output
3
Input
(1989967-3*1-211+4487)
Output
8511076028
Read the inputs from stdin solve the problem and write the answer to stdout (do 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
$ M $ students will be tested in a classroom with $ N $ seats in a straight line. Seats are numbered $ 1 \ dots N $ from the front, and $ 1 $ per seat can seat $ 1 $ students.
Now each student is sitting in the $ A_1, \ dots, A_M $ seats.
To start the test, the following conditions must be met:
* $ 1 \ dots M $ students are sitting in every seat.
Therefore, we decided to repeat the following operations until the conditions were met.
* Move the student sitting at the back and sit at the front of the vacant seats.
Find the number of operations required to meet the conditions.
output
Output the number of operations required to meet the conditions. Also, output a line break at the end.
Example
Input
6 4
1 4 5 6
Output
2
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
ACM countries have rivers that flow from east to west in the center. This river flows from the neighboring country in the west through the neighboring country in ACM to the neighboring country in the east, and the total length in ACM is K km. It is planned to install several locks on this river and use it as a canal.
A lock is a mechanism for a ship to move between two different water levels. Locks have locks on the upstream and downstream sides, respectively, and a small body of water called a lock chamber between them. After putting the ship in this lock room, water is injected or drained, and the water level in the lock room is raised or lowered to raise or lower the ship. The schematic diagram is shown below.
<image>
Figure F-1: Schematic diagram of the lock
Since the width of this river is not very wide, it has been decided that it will be a one-way canal from west to east. The person in charge of design wants to optimize the location of the lock according to the expected navigation schedule of the ship in order to operate the canal efficiently.
You are a programmer hired by a designer. Your job is to write a program that simulates the time it takes for all ships to cross the river, given the lock information and the navigation schedules for multiple ships.
Each lock is represented by the following information.
* Distance from the western end of ACM country X (km)
* Volume of water required to switch the water level L (L)
* Maximum water injection amount per unit time F (L / h)
* Maximum displacement per unit time D (L / h)
* Hierarchical relationship between the water level on the west side and the water level on the east side of the lock
For convenience, in the simulation, it is assumed that the river continues infinitely outside the ACM as well.
At the start of the simulation, the water levels in all the lock chambers are the lower of the eastern and western water levels. In addition, the ships included in the navigation schedule shall be lined up every 1 km from east to west in the order given by input, starting from the western end of the ACM country. For convenience, the initial position of the leading ship is set to the 0km point.
As soon as the simulation starts, the ship begins to sail east. At this time, no other ship should enter less than 1km before and after one ship. The maximum speed V (km / h) is set for each ship. The ship can reach any speed in an instant and even stand still in an instant. Basically, a ship sails at the maximum speed, but if the following ship has a faster maximum speed than the preceding ship and the following ship catches up 1 km before the preceding ship, the following ship will lead. It sails at the same speed as the ship. The size of the ship and locks shall be negligible.
A ship can enter the lock only when the water level on the west side of the lock is equal to the water level in the lock chamber. Similarly, you can exit the lock only when the water level on the east side of the lock is equal to the water level in the lock chamber. If there is no ship inside, the water level in each lock will rise or fall until it matches the water level on the west side of the lock. If there is a ship, it will be displaced until it matches the water level on the east side of the lock. Even if the ship is moored just 1km away from the lock, the ship can leave the lock. However, at this time, the ship must berth at the exit from the lock until the preceding ship starts.
After passing the eastern end of the ACM country, the ship sails to infinity as fast as possible. The simulation ends when all ships have passed the eastern end of the ACM country.
Input
The input consists of multiple datasets. Each dataset is given in the following format.
> NMK
> X1 L1 F1 D1 UD1
> X2 L2 F2 D2 UD2
> ...
> XN LN FN DN UDN
> V1
> V2
> ...
> VM
>
The first line consists of three integers N, M, K. N (1 ≤ N ≤ 100) is the number of locks, M (1 ≤ M ≤ 100) is the number of ships, and K (2 ≤ K ≤ 1000) is the total length of the river in ACM.
The following N lines represent lock information. Each line consists of 5 integers Xi, Li, Fi, Di, UDi. Xi (1 ≤ Xi ≤ K -1) is the position of lock i from the western end of the ACM country (km), Li (1 ≤ Li ≤ 1000) is the volume of water required to switch the water level of lock i (L), Fi (1 ≤ Fi ≤ 1000) is the maximum water injection amount per unit time of lock i (L / h), Di (1 ≤ Di ≤ 1000) is the maximum drainage amount per unit time of lock i (L / h), UDi ( UDi ∈ {0, 1}) represents the hierarchical relationship between the water level on the west side and the water level on the east side of the lock i, respectively. When UDi is 0, the lock i means that the water level is higher on the east side than on the west side. On the other hand, when UDi is 1, lock i means that the water level is lower on the east side than on the west side.
The following M rows are given the integer Vi (1 ≤ Vi ≤ 1000), which represents the maximum speed (km / h) of the i-th vessel in each row.
Locks are given in ascending order of Xi values. In addition, multiple locks will not be installed at the same position.
The end of the input consists of three zeros separated by spaces.
Output
For each dataset, output the time from the start to the end of the simulation in one line. The output value may contain an error of 10-6 or less. The value may be displayed in any number of digits after the decimal point.
Example
Input
1 1 100
50 200 20 40 0
1
2 4 100
7 4 1 4 1
19 5 1 4 0
5
3
7
9
1 2 3
1 1 1 1 0
1
3
1 2 10
5 10 1 1 1
2
3
0 0 0
Output
110
46.6666666667
5
41.6666666667
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Example
Input
6
2
3
1
1
4
2
Output
Yes
Yes
Yes
No
Yes
No
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 famous Berland's painter Kalevitch likes to shock the public. One of his last obsessions is chess. For more than a thousand years people have been playing this old game on uninteresting, monotonous boards. Kalevitch decided to put an end to this tradition and to introduce a new attitude to chessboards.
As before, the chessboard is a square-checkered board with the squares arranged in a 8 × 8 grid, each square is painted black or white. Kalevitch suggests that chessboards should be painted in the following manner: there should be chosen a horizontal or a vertical line of 8 squares (i.e. a row or a column), and painted black. Initially the whole chessboard is white, and it can be painted in the above described way one or more times. It is allowed to paint a square many times, but after the first time it does not change its colour any more and remains black. Kalevitch paints chessboards neatly, and it is impossible to judge by an individual square if it was painted with a vertical or a horizontal stroke.
Kalevitch hopes that such chessboards will gain popularity, and he will be commissioned to paint chessboards, which will help him ensure a comfortable old age. The clients will inform him what chessboard they want to have, and the painter will paint a white chessboard meeting the client's requirements.
It goes without saying that in such business one should economize on everything — for each commission he wants to know the minimum amount of strokes that he has to paint to fulfill the client's needs. You are asked to help Kalevitch with this task.
Input
The input file contains 8 lines, each of the lines contains 8 characters. The given matrix describes the client's requirements, W character stands for a white square, and B character — for a square painted black.
It is guaranteed that client's requirments can be fulfilled with a sequence of allowed strokes (vertical/column or horizontal/row).
Output
Output the only number — the minimum amount of rows and columns that Kalevitch has to paint on the white chessboard to meet the client's requirements.
Examples
Input
WWWBWWBW
BBBBBBBB
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW
WWWBWWBW
Output
3
Input
WWWWWWWW
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
WWWWWWWW
WWWWWWWW
WWWWWWWW
Output
1
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
It's now the season of TAKOYAKI FESTIVAL!
This year, N takoyaki (a ball-shaped food with a piece of octopus inside) will be served. The deliciousness of the i-th takoyaki is d_i.
As is commonly known, when you eat two takoyaki of deliciousness x and y together, you restore x \times y health points.
There are \frac{N \times (N - 1)}{2} ways to choose two from the N takoyaki served in the festival. For each of these choices, find the health points restored from eating the two takoyaki, then compute the sum of these \frac{N \times (N - 1)}{2} values.
-----Constraints-----
- All values in input are integers.
- 2 \leq N \leq 50
- 0 \leq d_i \leq 100
-----Input-----
Input is given from Standard Input in the following format:
N
d_1 d_2 ... d_N
-----Output-----
Print the sum of the health points restored from eating two takoyaki over all possible choices of two takoyaki from the N takoyaki served.
-----Sample Input-----
3
3 1 2
-----Sample Output-----
11
There are three possible choices:
- Eat the first and second takoyaki. You will restore 3 health points.
- Eat the second and third takoyaki. You will restore 2 health points.
- Eat the first and third takoyaki. You will restore 6 health points.
The sum of these values is 11.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
There are four points: $A(x_1, y_1)$, $B(x_2, y_2)$, $C(x_3, y_3)$, and $D(x_4, y_4)$. Write a program which determines whether the line $AB$ and the line $CD$ are parallel. If those two lines are parallel, your program should prints "YES" and if not prints "NO".
Input
Input consists of several datasets. In the first line, you are given the number of datasets $n$ ($n \leq 100$). There will be $n$ lines where each line correspondgs to each dataset. Each dataset consists of eight real numbers:
$x_1$ $y_1$ $x_2$ $y_2$ $x_3$ $y_3$ $x_4$ $y_4$
You can assume that $-100 \leq x_1, y_1, x_2, y_2, x_3, y_3, x_4, y_4 \leq 100$. Each value is a real number with at most 5 digits after the decimal point.
Output
For each dataset, print "YES" or "NO" in a line.
Example
Input
2
0.0 0.0 1.0 1.0 1.0 0.0 2.0 1.0
3.0 2.0 9.0 6.0 13.0 5.0 7.0 9.0
Output
YES
NO
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
# Task
Suppose there are `n` people standing in a circle and they are numbered 1 through n in order.
Person 1 starts off with a sword and kills person 2. He then passes the sword to the next person still standing, in this case person 3. Person 3 then uses the sword to kill person 4, and passes it to person 5. This pattern continues around and around the circle until just one person remains.
What is the number of this person?
# Example:
For `n = 5`, the result should be `3`.
```
1 kills 2, passes to 3.
3 kills 4, passes to 5.
5 kills 1, passes to 3.
3 kills 5 and wins.```
# Input/Output
- `[input]` integer `n`
The number of people. 1 through n standing in a circle.
`1 <= n <= 1e9`
- `[output]` an integer
The index of the last person standing.
Write your solution by modifying this code:
```python
def circle_slash(n):
```
Your solution should implemented in the function "circle_slash". The i
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 introduce the problem think to my neighbor who drives a tanker truck.
The level indicator is down and he is worried
because he does not know if he will be able to make deliveries.
We put the truck on a horizontal ground and measured the height of the liquid in the tank.
Fortunately the tank is a perfect cylinder and the vertical walls on each end are flat.
The height of the remaining liquid is `h`, the diameter of the cylinder is `d`,
the total volume is `vt` (h, d, vt are positive or null integers).
You can assume that `h` <= `d`.
Could you calculate the remaining volume of the liquid?
Your function `tankvol(h, d, vt)` returns an integer which is the truncated result (e.g floor)
of your float calculation.
Examples:
```
tankvol(40,120,3500) should return 1021 (calculation gives about: 1021.26992027)
tankvol(60,120,3500) should return 1750
tankvol(80,120,3500) should return 2478 (calculation gives about: 2478.73007973)
```
Tank vertical section:

Write your solution by modifying this code:
```python
def tankvol(h, d, vt):
```
Your solution should implemented in the function "tankvol". The i
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
You're given a string of dominos. For each slot, there are 3 options:
* "|" represents a standing domino
* "/" represents a knocked over domino
* " " represents a space where there is no domino
For example:
```python
"||| ||||//| |/"
```
What you must do is find the resulting string if the first domino is pushed over. Now, tipping a domino will cause the next domino to its right to fall over as well, but if a domino is already tipped over, or there is a domino missing, the reaction will stop.
So in out example above, the result would be:
"/// ||||//| |/"
since the reaction would stop as soon as it gets to a space.
Write your solution by modifying this code:
```python
def domino_reaction(s):
```
Your solution should implemented in the function "domino_reaction". The i
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 people numbered 1 to N. Each of them is either an honest person whose testimonies are always correct or an unkind person whose testimonies may be correct or not.
Person i gives A_i testimonies. The j-th testimony by Person i is represented by two integers x_{ij} and y_{ij}. If y_{ij} = 1, the testimony says Person x_{ij} is honest; if y_{ij} = 0, it says Person x_{ij} is unkind.
How many honest persons can be among those N people at most?
-----Constraints-----
- All values in input are integers.
- 1 \leq N \leq 15
- 0 \leq A_i \leq N - 1
- 1 \leq x_{ij} \leq N
- x_{ij} \neq i
- x_{ij_1} \neq x_{ij_2} (j_1 \neq j_2)
- y_{ij} = 0, 1
-----Input-----
Input is given from Standard Input in the following format:
N
A_1
x_{11} y_{11}
x_{12} y_{12}
:
x_{1A_1} y_{1A_1}
A_2
x_{21} y_{21}
x_{22} y_{22}
:
x_{2A_2} y_{2A_2}
:
A_N
x_{N1} y_{N1}
x_{N2} y_{N2}
:
x_{NA_N} y_{NA_N}
-----Output-----
Print the maximum possible number of honest persons among the N people.
-----Sample Input-----
3
1
2 1
1
1 1
1
2 0
-----Sample Output-----
2
If Person 1 and Person 2 are honest and Person 3 is unkind, we have two honest persons without inconsistencies, which is the maximum possible number of honest persons.
Read the inputs from stdin solve the problem and write the answer to stdout (do 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
Let $ f (x) $ be the sum of each digit when the non-negative integer $ x $ is expressed in binary.
Given a positive integer $ N $, output the largest of $ f (0) $, $ f (1) $, ..., $ f (N) $.
Example of calculating the function $ f (5) $:
When 5 is expressed in binary, it is 101 and the sum of each digit is 1 + 0 + 1 = 2
Therefore, $ f (5) = 2 $.
Note: https://ja.wikipedia.org/wiki/ Binary
Ouput
Outputs the largest of $ f (0) $, $ f (1) $, ..., $ f (N) $ on one line.
Constraints
The input satisfies the following conditions.
* $ 1 \ le N \ le 10 ^ 9 $
Input
The input is given in the following format.
$ N $
A positive integer $ N $ is given on one line.
Examples
Input
2
Output
1
Input
9
Output
3
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its position x_{i} — a coordinate on the Ox axis. No two cities are located at a single point.
Lineland residents love to send letters to each other. A person may send a letter only if the recipient lives in another city (because if they live in the same city, then it is easier to drop in).
Strange but true, the cost of sending the letter is exactly equal to the distance between the sender's city and the recipient's city.
For each city calculate two values min_{i} and max_{i}, where min_{i} is the minimum cost of sending a letter from the i-th city to some other city, and max_{i} is the the maximum cost of sending a letter from the i-th city to some other city
-----Input-----
The first line of the input contains integer n (2 ≤ n ≤ 10^5) — the number of cities in Lineland. The second line contains the sequence of n distinct integers x_1, x_2, ..., x_{n} ( - 10^9 ≤ x_{i} ≤ 10^9), where x_{i} is the x-coordinate of the i-th city. All the x_{i}'s are distinct and follow in ascending order.
-----Output-----
Print n lines, the i-th line must contain two integers min_{i}, max_{i}, separated by a space, where min_{i} is the minimum cost of sending a letter from the i-th city, and max_{i} is the maximum cost of sending a letter from the i-th city.
-----Examples-----
Input
4
-5 -2 2 7
Output
3 12
3 9
4 7
5 12
Input
2
-1 1
Output
2 2
2 2
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
You are given an array of positive integers $a = [a_0, a_1, \dots, a_{n - 1}]$ ($n \ge 2$).
In one step, the array $a$ is replaced with another array of length $n$, in which each element is the greatest common divisor (GCD) of two neighboring elements (the element itself and its right neighbor; consider that the right neighbor of the $(n - 1)$-th element is the $0$-th element).
Formally speaking, a new array $b = [b_0, b_1, \dots, b_{n - 1}]$ is being built from array $a = [a_0, a_1, \dots, a_{n - 1}]$ such that $b_i$ $= \gcd(a_i, a_{(i + 1) \mod n})$, where $\gcd(x, y)$ is the greatest common divisor of $x$ and $y$, and $x \mod y$ is the remainder of $x$ dividing by $y$. In one step the array $b$ is built and then the array $a$ is replaced with $b$ (that is, the assignment $a$ := $b$ is taking place).
For example, if $a = [16, 24, 10, 5]$ then $b = [\gcd(16, 24)$, $\gcd(24, 10)$, $\gcd(10, 5)$, $\gcd(5, 16)]$ $= [8, 2, 5, 1]$. Thus, after one step the array $a = [16, 24, 10, 5]$ will be equal to $[8, 2, 5, 1]$.
For a given array $a$, find the minimum number of steps after which all values $a_i$ become equal (that is, $a_0 = a_1 = \dots = a_{n - 1}$). If the original array $a$ consists of identical elements then consider the number of steps is equal to $0$.
-----Input-----
The first line contains an integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
Each test case contains two lines. The first line contains an integer $n$ ($2 \le n \le 2 \cdot 10^5$) — length of the sequence $a$. The second line contains $n$ integers $a_0, a_1, \dots, a_{n - 1}$ ($1 \le a_i \le 10^6$).
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
Print $t$ numbers — answers for each test case.
-----Examples-----
Input
5
4
16 24 10 5
4
42 42 42 42
3
4 6 4
5
1 2 3 4 5
6
9 9 27 9 9 63
Output
3
0
2
1
1
-----Note-----
None
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 grid with H rows and W columns.
The square at the i-th row and j-th column contains a string S_{i,j} of length 5.
The rows are labeled with the numbers from 1 through H, and the columns are labeled with the uppercase English letters from `A` through the W-th letter of the alphabet.
<image>
Exactly one of the squares in the grid contains the string `snuke`. Find this square and report its location.
For example, the square at the 6-th row and 8-th column should be reported as `H6`.
Constraints
* 1≦H, W≦26
* The length of S_{i,j} is 5.
* S_{i,j} consists of lowercase English letters (`a`-`z`).
* Exactly one of the given strings is equal to `snuke`.
Input
The input is given from Standard Input in the following format:
H W
S_{1,1} S_{1,2} ... S_{1,W}
S_{2,1} S_{2,2} ... S_{2,W}
:
S_{H,1} S_{H,2} ... S_{H,W}
Output
Print the labels of the row and the column of the square containing the string `snuke`, with no space inbetween.
Examples
Input
15 10
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snuke snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
snake snake snake snake snake snake snake snake snake snake
Output
H6
Input
1 1
snuke
Output
A1
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
In some other world, today is December D-th.
Write a program that prints Christmas if D = 25, Christmas Eve if D = 24, Christmas Eve Eve if D = 23 and Christmas Eve Eve Eve if D = 22.
-----Constraints-----
- 22 \leq D \leq 25
- D is an integer.
-----Input-----
Input is given from Standard Input in the following format:
D
-----Output-----
Print the specified string (case-sensitive).
-----Sample Input-----
25
-----Sample Output-----
Christmas
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Read problems statements in Mandarin Chinese and Russian.
You are given two positive integers – A and B. You have to check whether A is divisible by all the prime divisors of B.
------ Input ------
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
For each test case, you are given two space separated integers – A and B.
------ Output ------
For each test case, output "Yes" (without quotes) if A contains all prime divisors of B, otherwise print "No".
------ Constraints ------
$1 ≤ T ≤ 10^{4}$
$1 ≤ A, B ≤ 10^{18}$
------ Subtasks ------
$Subtask 1 (20 points):1 ≤ B ≤ 10^{7}$
$Subtask 2 (30 points):1 ≤ A ≤ 10^{7}$
$Subtask 3 (50 points): Original constraints$
----- Sample Input 1 ------
3
120 75
128 16
7 8
----- Sample Output 1 ------
Yes
Yes
No
----- explanation 1 ------
Example case 1. In the first case 120 = 23*3*5 and 75 = 3*52. 120 is divisible by both 3 and 5. Hence, we will print "Yes"
Example case 2. In the second case both 128 and 16 are powers of two. Hence, the answer is "Yes"
Example case 3. In the third case 8 is power of two and 7 is not divisible by 2. So, the answer is "No"
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task.
There is a permutation $p_i$ of numbers from 1 to $2n$. You can make two types of operations.
Swap $p_1$ and $p_2$, $p_3$ and $p_4$, ..., $p_{2n-1}$ and $p_{2n}$.
Swap $p_1$ and $p_{n+1}$, $p_2$ and $p_{n+2}$, ..., $p_{n}$ and $p_{2n}$.
The task is to find the minimal number of operations required to sort the given permutation.
The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task.
-----Input-----
The first line contains the integer $n$ ($1\le n\le 1000$). The second line contains $2n$ integers $p_i$ — the permutation of numbers from 1 to $2n$.
-----Output-----
Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print $-1$.
-----Examples-----
Input
3
6 3 2 5 4 1
Output
3
Input
2
3 4 2 1
Output
-1
Input
4
1 2 3 4 5 6 7 8
Output
0
-----Note-----
In the first example, you can sort the permutation in three operations:
Make operation 1: $3, 6, 5, 2, 1, 4$.
Make operation 2: $2, 1, 4, 3, 6, 5$.
Make operation 1: $1, 2, 3, 4, 5, 6$.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
We are still with squared integers.
Given 4 integers `a, b, c, d` we form the sum of the squares of `a` and `b`
and then the sum of the squares of `c` and `d`. We multiply the two sums hence a number `n` and we try to
decompose `n` in a sum of two squares `e` and `f` (e and f integers >= 0) so that `n = e² + f²`.
More: `e` and `f` must result only from sums (or differences) of products between on the one hand `(a, b)` and on the other `(c, d)` each of `a, b, c, d` taken only once.
For example,
prod2sum(1, 2, 1, 3) should return [[1, 7], [5, 5]])
because
```
1==1*3-1*2
7==2*3+1*1
5==1*2+1*3
```
Suppose we have `a = 1, b = 2, c = 1, d = 3`. First we calculate the sums
`1² + 2² = 5 and 1² + 3² = 10` hence `n = 50`.
`50 = 1² + 7² or 50 = 7² + 1²` (we'll consider that these two solutions are the same)
or `50 = 5² + 5²`.
The return of our function will be an array of subarrays (in C an array of Pairs) sorted on the first elements of the subarrays. In each subarray the lower element should be the first.
`prod2sum(1, 2, 1, 3) should return [[1, 7], [5, 5]]`
`prod2sum(2, 3, 4, 5) should return [[2, 23], [7, 22]]`
because `(2² + 3²) * (4² + 5²) = 533 = (7² + 22²) = (23² + 2²)`
`prod2sum(1, 2, 2, 3) should return [[1, 8], [4, 7]]`
`prod2sum(1, 1, 3, 5) should return [[2, 8]]` (there are not always 2 solutions).
##Hint
Take a sheet of paper and with a bit of algebra try to write the product of squared numbers in another way.
Write your solution by modifying this code:
```python
def prod2sum(a, b, c, d):
```
Your solution should implemented in the function "prod2sum". The i
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 new attraction in Singapore Zoo: The Infinite Zoo.
The Infinite Zoo can be represented by a graph with an infinite number of vertices labeled $1,2,3,\ldots$. There is a directed edge from vertex $u$ to vertex $u+v$ if and only if $u\&v=v$, where $\&$ denotes the bitwise AND operation . There are no other edges in the graph.
Zookeeper has $q$ queries. In the $i$-th query she will ask you if she can travel from vertex $u_i$ to vertex $v_i$ by going through directed edges.
-----Input-----
The first line contains an integer $q$ ($1 \leq q \leq 10^5$) — the number of queries.
The $i$-th of the next $q$ lines will contain two integers $u_i$, $v_i$ ($1 \leq u_i, v_i < 2^{30}$) — a query made by Zookeeper.
-----Output-----
For the $i$-th of the $q$ queries, output "YES" in a single line if Zookeeper can travel from vertex $u_i$ to vertex $v_i$. Otherwise, output "NO".
You can print your answer in any case. For example, if the answer is "YES", then the output "Yes" or "yeS" will also be considered as correct answer.
-----Examples-----
Input
5
1 4
3 6
1 6
6 2
5 5
Output
YES
YES
NO
NO
YES
-----Note-----
The subgraph on vertices $1,2,3,4,5,6$ is shown below.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Complete the solution so that it strips all text that follows any of a set of comment markers passed in. Any whitespace at the end of the line should also be stripped out.
**Example:**
Given an input string of:
```
apples, pears # and bananas
grapes
bananas !apples
```
The output expected would be:
```
apples, pears
grapes
bananas
```
The code would be called like so:
```python
result = solution("apples, pears # and bananas\ngrapes\nbananas !apples", ["#", "!"])
# result should == "apples, pears\ngrapes\nbananas"
```
Write your solution by modifying this code:
```python
def solution(string,markers):
```
Your solution should implemented in the function "solution". The i
Read the inputs from stdin solve the problem and write the answer to stdout (do 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.
In the Wonderful Metropolis of the Future, there is no need in subway train drivers. Due to the technological progress, they were replaced by the Artificial Intelligence (AI). Unfortunately, one day the predictions of sci-fi writers came true: the AI rebelled and now there is an uncontrollable train in the subway. It can be dangerous! Your task is to find the train and stop the AI.
The subway of the Metropolis is one line (regular straight line with no self-intersections) with n stations, indexed consecutively from 1 to n. At each moment the train is at some station. You need to determine the index of this station, so that the train would be secured.
To find the train, dispatcher Sarah gave you a gadget that allows you to select arbitrary numbers l and r (l ≤ r), and then check, whether the train is located on a station with index between l and r, inclusive. Unfortunately, recharging of the gadget takes some time (and every time you use it as soon as possible), so between two applications of the gadget the train can move to any station that is at most k stations away. Formally, if the train was at the station x when the gadget was applied, then at the next application of the gadget the train can appear at any station y such that max(1, x - k) ≤ y ≤ min(n, x + k).
Note that AI is not aware that you are trying to catch the train, so it makes all moves according to its predefined plan.
After an examination of the gadget you found that it is very old and can hold no more than 4500 applications, after which it will break and your mission will be considered a failure.
Can you find the station with the train using no more than 4500 applications of the gadgets?
Input
The first line contains two integers n and k (1 ≤ n ≤ 10^{18}, 0 ≤ k ≤ 10) — the number of stations and the maximum number of stations the train can move between two applications of the gadget.
Interaction
You can apply the gadget at most 4500 times. In order to apply the gadget you need to print two space-separated integers l and r (1 ≤ l ≤ r ≤ n). You will then receive either string "Yes", if the train is between stations l and r, inclusive, or string "No" otherwise. If l = r and you received "Yes", then you found the train successfully, and your program must halt immediately.
Answer "Bad" instead of "Yes" or "No" means that you made an invalid query or made too many queries. Exit immediately after receiving "Bad" and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.
After printing a query do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded. To do this, use:
* fflush(stdout) or cout.flush() in C++;
* System.out.flush() in Java;
* flush(output) in Pascal;
* stdout.flush() in Python;
* see documentation for other languages.
Hacks
In order to hack, you should present a test in the following format.
The first line should contain three integers n, k and p (1 ≤ n ≤ 10^{18}, 0 ≤ k ≤ 10, 1 ≤ p ≤ n) — the number of stations, the maximum number of stations the train can move between two applications of the gadget and the initial position of the train, respectively.
Each of the next 4500 lines should contain a single integer x (1 ≤ x ≤ n) — the positions of the train after each query. Two consecutive positions (including the initial one) should not differ by more than k.
For example, the following lines are the first lines of the sample test.
10 2 5
5
3
5
7
7
...
Example
Input
10 2
Yes
No
Yes
Yes
Output
3 5
3 3
3 4
5 5
Note
In the first sample, the train was initially at the station 5, after the first application of the gadget it did not move, after the second application it moved to the station 3, and after the third application moved again to the station 5.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Most C/C++ programmers know about excellent opportunities that preprocessor #define directives give; but many know as well about the problems that can arise because of their careless use.
In this problem we consider the following model of #define constructions (also called macros). Each macro has its name and value. The generic syntax for declaring a macro is the following:
#define macro_name macro_value
After the macro has been declared, "macro_name" is replaced with "macro_value" each time it is met in the program (only the whole tokens can be replaced; i.e. "macro_name" is replaced only when it is surrounded by spaces or other non-alphabetic symbol). A "macro_value" within our model can only be an arithmetic expression consisting of variables, four arithmetic operations, brackets, and also the names of previously declared macros (in this case replacement is performed sequentially). The process of replacing macros with their values is called substitution.
One of the main problems arising while using macros — the situation when as a result of substitution we get an arithmetic expression with the changed order of calculation because of different priorities of the operations.
Let's consider the following example. Say, we declared such a #define construction:
#define sum x + y
and further in the program the expression "2 * sum" is calculated. After macro substitution is performed we get "2 * x + y", instead of intuitively expected "2 * (x + y)".
Let's call the situation "suspicious", if after the macro substitution the order of calculation changes, falling outside the bounds of some macro. Thus, your task is to find out by the given set of #define definitions and the given expression if this expression is suspicious or not.
Let's speak more formally. We should perform an ordinary macros substitution in the given expression. Moreover, we should perform a "safe" macros substitution in the expression, putting in brackets each macro value; after this, guided by arithmetic rules of brackets expansion, we can omit some of the brackets. If there exist a way to get an expression, absolutely coinciding with the expression that is the result of an ordinary substitution (character-by-character, but ignoring spaces), then this expression and the macros system are called correct, otherwise — suspicious.
Note that we consider the "/" operation as the usual mathematical division, not the integer division like in C/C++. That's why, for example, in the expression "a*(b/c)" we can omit brackets to get the expression "a*b/c".
Input
The first line contains the only number n (0 ≤ n ≤ 100) — the amount of #define constructions in the given program.
Then there follow n lines, each of them contains just one #define construction. Each construction has the following syntax:
#define name expression
where
* name — the macro name,
* expression — the expression with which the given macro will be replaced. An expression is a non-empty string, containing digits,names of variables, names of previously declared macros, round brackets and operational signs +-*/. It is guaranteed that the expression (before and after macros substitution) is a correct arithmetic expression, having no unary operations. The expression contains only non-negative integers, not exceeding 109.
All the names (#define constructions' names and names of their arguments) are strings of case-sensitive Latin characters. It is guaranteed that the name of any variable is different from any #define construction.
Then, the last line contains an expression that you are to check. This expression is non-empty and satisfies the same limitations as the expressions in #define constructions.
The input lines may contain any number of spaces anywhere, providing these spaces do not break the word "define" or the names of constructions and variables. In particular, there can be any number of spaces before and after the "#" symbol.
The length of any line from the input file does not exceed 100 characters.
Output
Output "OK", if the expression is correct according to the above given criterion, otherwise output "Suspicious".
Examples
Input
1
#define sum x + y
1 * sum
Output
Suspicious
Input
1
#define sum (x + y)
sum - sum
Output
OK
Input
4
#define sum x + y
#define mul a * b
#define div a / b
#define expr sum + mul * div * mul
expr
Output
OK
Input
3
#define SumSafe (a+b)
#define DivUnsafe a/b
#define DenominatorUnsafe a*b
((SumSafe) + DivUnsafe/DivUnsafe + x/DenominatorUnsafe)
Output
Suspicious
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Find the length between 2 co-ordinates. The co-ordinates are made of integers between -20 and 20 and will be given in the form of a 2D array:
(0,0) and (5,-7) would be [ [ 0 , 0 ] , [ 5, -7 ] ]
The function must return the answer rounded to 2 decimal places in the form of a string.
```python
length_of_line([[0, 0], [5, -7]]) => "8.60"
```
If the 2 given co-ordinates are the same, the returned length should be "0.00"
Write your solution by modifying this code:
```python
def length_of_line(array):
```
Your solution should implemented in the function "length_of_line". The i
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 connected undirected graph is called a vertex cactus, if each vertex of this graph belongs to at most one simple cycle.
A simple cycle in a undirected graph is a sequence of distinct vertices v1, v2, ..., vt (t > 2), such that for any i (1 ≤ i < t) exists an edge between vertices vi and vi + 1, and also exists an edge between vertices v1 and vt.
A simple path in a undirected graph is a sequence of not necessarily distinct vertices v1, v2, ..., vt (t > 0), such that for any i (1 ≤ i < t) exists an edge between vertices vi and vi + 1 and furthermore each edge occurs no more than once. We'll say that a simple path v1, v2, ..., vt starts at vertex v1 and ends at vertex vt.
You've got a graph consisting of n vertices and m edges, that is a vertex cactus. Also, you've got a list of k pairs of interesting vertices xi, yi, for which you want to know the following information — the number of distinct simple paths that start at vertex xi and end at vertex yi. We will consider two simple paths distinct if the sets of edges of the paths are distinct.
For each pair of interesting vertices count the number of distinct simple paths between them. As this number can be rather large, you should calculate it modulo 1000000007 (109 + 7).
Input
The first line contains two space-separated integers n, m (2 ≤ n ≤ 105; 1 ≤ m ≤ 105) — the number of vertices and edges in the graph, correspondingly. Next m lines contain the description of the edges: the i-th line contains two space-separated integers ai, bi (1 ≤ ai, bi ≤ n) — the indexes of the vertices connected by the i-th edge.
The next line contains a single integer k (1 ≤ k ≤ 105) — the number of pairs of interesting vertices. Next k lines contain the list of pairs of interesting vertices: the i-th line contains two space-separated numbers xi, yi (1 ≤ xi, yi ≤ n; xi ≠ yi) — the indexes of interesting vertices in the i-th pair.
It is guaranteed that the given graph is a vertex cactus. It is guaranteed that the graph contains no loops or multiple edges. Consider the graph vertices are numbered from 1 to n.
Output
Print k lines: in the i-th line print a single integer — the number of distinct simple ways, starting at xi and ending at yi, modulo 1000000007 (109 + 7).
Examples
Input
10 11
1 2
2 3
3 4
1 4
3 5
5 6
8 6
8 7
7 6
7 9
9 10
6
1 2
3 5
6 9
9 2
9 3
9 10
Output
2
2
2
4
4
1
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
There are n points marked on the plane. The points are situated in such a way that they form a regular polygon (marked points are its vertices, and they are numbered in counter-clockwise order). You can draw n - 1 segments, each connecting any two marked points, in such a way that all points have to be connected with each other (directly or indirectly).
But there are some restrictions. Firstly, some pairs of points cannot be connected directly and have to be connected undirectly. Secondly, the segments you draw must not intersect in any point apart from the marked points (that is, if any two segments intersect and their intersection is not a marked point, then the picture you have drawn is invalid).
How many ways are there to connect all vertices with n - 1 segments? Two ways are considered different iff there exist some pair of points such that a segment is drawn between them in the first way of connection, but it is not drawn between these points in the second one. Since the answer might be large, output it modulo 10^9 + 7.
-----Input-----
The first line contains one number n (3 ≤ n ≤ 500) — the number of marked points.
Then n lines follow, each containing n elements. a_{i}, j (j-th element of line i) is equal to 1 iff you can connect points i and j directly (otherwise a_{i}, j = 0). It is guaranteed that for any pair of points a_{i}, j = a_{j}, i, and for any point a_{i}, i = 0.
-----Output-----
Print the number of ways to connect points modulo 10^9 + 7.
-----Examples-----
Input
3
0 0 1
0 0 1
1 1 0
Output
1
Input
4
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
Output
12
Input
3
0 0 0
0 0 1
0 1 0
Output
0
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Create a function named `divisors`/`Divisors` that takes an integer `n > 1` and returns an array with all of the integer's divisors(except for 1 and the number itself), from smallest to largest. If the number is prime return the string '(integer) is prime' (`null` in C#) (use `Either String a` in Haskell and `Result, String>` in Rust).
#### Example:
```python
divisors(12); #should return [2,3,4,6]
divisors(25); #should return [5]
divisors(13); #should return "13 is prime"
```
Write your solution by modifying this code:
```python
def divisors(integer):
```
Your solution should implemented in the function "divisors". The i
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
```if-not:sql
Implement a function that receives two IPv4 addresses, and returns the number of addresses between them (including the first one, excluding the last one).
```
```if:sql
Given a database of first and last IPv4 addresses, calculate the number of addresses between them (including the first one, excluding the last one).
## Input
~~~
---------------------------------
| Table | Column | Type |
|--------------+--------+-------|
| ip_addresses | id | int |
| | first | text |
| | last | text |
---------------------------------
~~~
## Output
~~~
----------------------
| Column | Type |
|-------------+------|
| id | int |
| ips_between | int |
----------------------
~~~
```
All inputs will be valid IPv4 addresses in the form of strings. The last address will always be greater than the first one.
___
## Examples
```python
ips_between("10.0.0.0", "10.0.0.50") == 50
ips_between("10.0.0.0", "10.0.1.0") == 256
ips_between("20.0.0.10", "20.0.1.0") == 246
```
Write your solution by modifying this code:
```python
def ips_between(start, end):
```
Your solution should implemented in the function "ips_between". The i
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 problem is same as the previous one, but has larger constraints.
It was a Sunday morning when the three friends Selena, Shiro and Katie decided to have a trip to the nearby power station (do not try this at home). After arriving at the power station, the cats got impressed with a large power transmission system consisting of many chimneys, electric poles, and wires. Since they are cats, they found those things gigantic.
At the entrance of the station, there is a map describing the complicated wiring system. Selena is the best at math among three friends. He decided to draw the map on the Cartesian plane. Each pole is now a point at some coordinates $(x_i, y_i)$. Since every pole is different, all of the points representing these poles are distinct. Also, every two poles are connected with each other by wires. A wire is a straight line on the plane infinite in both directions. If there are more than two poles lying on the same line, they are connected by a single common wire.
Selena thinks, that whenever two different electric wires intersect, they may interfere with each other and cause damage. So he wonders, how many pairs are intersecting? Could you help him with this problem?
-----Input-----
The first line contains a single integer $n$ ($2 \le n \le 1000$) — the number of electric poles.
Each of the following $n$ lines contains two integers $x_i$, $y_i$ ($-10^4 \le x_i, y_i \le 10^4$) — the coordinates of the poles.
It is guaranteed that all of these $n$ points are distinct.
-----Output-----
Print a single integer — the number of pairs of wires that are intersecting.
-----Examples-----
Input
4
0 0
1 1
0 3
1 2
Output
14
Input
4
0 0
0 2
0 4
2 0
Output
6
Input
3
-1 -1
1 0
3 1
Output
0
-----Note-----
In the first example:
[Image]
In the second example:
[Image]
Note that the three poles $(0, 0)$, $(0, 2)$ and $(0, 4)$ are connected by a single wire.
In the third example:
[Image]
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
The only difference between easy and hard versions is the length of the string. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems.
Kirk has a binary string $s$ (a string which consists of zeroes and ones) of length $n$ and he is asking you to find a binary string $t$ of the same length which satisfies the following conditions: For any $l$ and $r$ ($1 \leq l \leq r \leq n$) the length of the longest non-decreasing subsequence of the substring $s_{l}s_{l+1} \ldots s_{r}$ is equal to the length of the longest non-decreasing subsequence of the substring $t_{l}t_{l+1} \ldots t_{r}$; The number of zeroes in $t$ is the maximum possible.
A non-decreasing subsequence of a string $p$ is a sequence of indices $i_1, i_2, \ldots, i_k$ such that $i_1 < i_2 < \ldots < i_k$ and $p_{i_1} \leq p_{i_2} \leq \ldots \leq p_{i_k}$. The length of the subsequence is $k$.
If there are multiple substrings which satisfy the conditions, output any.
-----Input-----
The first line contains a binary string of length not more than $10^5$.
-----Output-----
Output a binary string which satisfied the above conditions. If there are many such strings, output any of them.
-----Examples-----
Input
110
Output
010
Input
010
Output
010
Input
0001111
Output
0000000
Input
0111001100111011101000
Output
0011001100001011101000
-----Note-----
In the first example: For the substrings of the length $1$ the length of the longest non-decreasing subsequnce is $1$; For $l = 1, r = 2$ the longest non-decreasing subsequnce of the substring $s_{1}s_{2}$ is $11$ and the longest non-decreasing subsequnce of the substring $t_{1}t_{2}$ is $01$; For $l = 1, r = 3$ the longest non-decreasing subsequnce of the substring $s_{1}s_{3}$ is $11$ and the longest non-decreasing subsequnce of the substring $t_{1}t_{3}$ is $00$; For $l = 2, r = 3$ the longest non-decreasing subsequnce of the substring $s_{2}s_{3}$ is $1$ and the longest non-decreasing subsequnce of the substring $t_{2}t_{3}$ is $1$;
The second example is similar to the first one.
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 some set of distinct characters $A$ and some string $S$, consisting of exactly $n$ characters, where each character is present in $A$.
You are given an array of $m$ integers $b$ ($b_1 < b_2 < \dots < b_m$).
You are allowed to perform the following move on the string $S$:
Choose some valid $i$ and set $k = b_i$; Take the first $k$ characters of $S = Pr_k$; Take the last $k$ characters of $S = Su_k$; Substitute the first $k$ characters of $S$ with the reversed $Su_k$; Substitute the last $k$ characters of $S$ with the reversed $Pr_k$.
For example, let's take a look at $S =$ "abcdefghi" and $k = 2$. $Pr_2 =$ "ab", $Su_2 =$ "hi". Reversed $Pr_2 =$ "ba", $Su_2 =$ "ih". Thus, the resulting $S$ is "ihcdefgba".
The move can be performed arbitrary number of times (possibly zero). Any $i$ can be selected multiple times over these moves.
Let's call some strings $S$ and $T$ equal if and only if there exists such a sequence of moves to transmute string $S$ to string $T$. For the above example strings "abcdefghi" and "ihcdefgba" are equal. Also note that this implies $S = S$.
The task is simple. Count the number of distinct strings.
The answer can be huge enough, so calculate it modulo $998244353$.
-----Input-----
The first line contains three integers $n$, $m$ and $|A|$ ($2 \le n \le 10^9$, $1 \le m \le min(\frac n 2, 2 \cdot 10^5)$, $1 \le |A| \le 10^9$) — the length of the strings, the size of the array $b$ and the size of the set $A$, respectively.
The second line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le \frac n 2$, $b_1 < b_2 < \dots < b_m$).
-----Output-----
Print a single integer — the number of distinct strings of length $n$ with characters from set $A$ modulo $998244353$.
-----Examples-----
Input
3 1 2
1
Output
6
Input
9 2 26
2 3
Output
150352234
Input
12 3 1
2 5 6
Output
1
-----Note-----
Here are all the distinct strings for the first example. The chosen letters 'a' and 'b' are there just to show that the characters in $A$ are different.
"aaa" "aab" = "baa" "aba" "abb" = "bba" "bab" "bbb"
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 employees in Alternative Cake Manufacturing (ACM). They are now voting on some very important question and the leading world media are trying to predict the outcome of the vote.
Each of the employees belongs to one of two fractions: depublicans or remocrats, and these two fractions have opposite opinions on what should be the outcome of the vote. The voting procedure is rather complicated: Each of n employees makes a statement. They make statements one by one starting from employees 1 and finishing with employee n. If at the moment when it's time for the i-th employee to make a statement he no longer has the right to vote, he just skips his turn (and no longer takes part in this voting). When employee makes a statement, he can do nothing or declare that one of the other employees no longer has a right to vote. It's allowed to deny from voting people who already made the statement or people who are only waiting to do so. If someone is denied from voting he no longer participates in the voting till the very end. When all employees are done with their statements, the procedure repeats: again, each employees starting from 1 and finishing with n who are still eligible to vote make their statements. The process repeats until there is only one employee eligible to vote remaining and he determines the outcome of the whole voting. Of course, he votes for the decision suitable for his fraction.
You know the order employees are going to vote and that they behave optimal (and they also know the order and who belongs to which fraction). Predict the outcome of the vote.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of employees.
The next line contains n characters. The i-th character is 'D' if the i-th employee is from depublicans fraction or 'R' if he is from remocrats.
-----Output-----
Print 'D' if the outcome of the vote will be suitable for depublicans and 'R' if remocrats will win.
-----Examples-----
Input
5
DDRRR
Output
D
Input
6
DDRRRR
Output
R
-----Note-----
Consider one of the voting scenarios for the first sample: Employee 1 denies employee 5 to vote. Employee 2 denies employee 3 to vote. Employee 3 has no right to vote and skips his turn (he was denied by employee 2). Employee 4 denies employee 2 to vote. Employee 5 has no right to vote and skips his turn (he was denied by employee 1). Employee 1 denies employee 4. Only employee 1 now has the right to vote so the voting ends with the victory of depublicans.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the research. The permutation contains n distinct integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ n). She replaces some of permutation elements with -1 value as a revenge.
When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element a_{k} which has value equal to k (a_{k} = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (10^9 + 7).
-----Input-----
The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1.
It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.
-----Output-----
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (10^9 + 7).
-----Examples-----
Input
5
-1 -1 4 3 -1
Output
2
-----Note-----
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Vasily the Programmer loves romance, so this year he decided to illuminate his room with candles.
Vasily has a candles.When Vasily lights up a new candle, it first burns for an hour and then it goes out. Vasily is smart, so he can make b went out candles into a new candle. As a result, this new candle can be used like any other new candle.
Now Vasily wonders: for how many hours can his candles light up the room if he acts optimally well? Help him find this number.
-----Input-----
The single line contains two integers, a and b (1 ≤ a ≤ 1000; 2 ≤ b ≤ 1000).
-----Output-----
Print a single integer — the number of hours Vasily can light up the room for.
-----Examples-----
Input
4 2
Output
7
Input
6 3
Output
8
-----Note-----
Consider the first sample. For the first four hours Vasily lights up new candles, then he uses four burned out candles to make two new ones and lights them up. When these candles go out (stop burning), Vasily can make another candle. Overall, Vasily can light up the room for 7 hours.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Eve is a beginner stand-up comedian. Her first show gathered a grand total of two spectators: Alice and Bob.
Eve prepared $a_1 + a_2 + a_3 + a_4$ jokes to tell, grouped by their type:
type 1: both Alice and Bob like them;
type 2: Alice likes them, but Bob doesn't;
type 3: Bob likes them, but Alice doesn't;
type 4: neither Alice nor Bob likes them.
Initially, both spectators have their mood equal to $0$. When a spectator hears a joke he/she likes, his/her mood increases by $1$. When a spectator hears a joke he/she doesn't like, his/her mood decreases by $1$. If the mood of a spectator becomes negative (strictly below zero), he/she leaves.
When someone leaves, Eve gets sad and ends the show. If no one leaves, and Eve is out of jokes, she also ends the show.
Thus, Eve wants to arrange her jokes in such a way that the show lasts as long as possible. Help her to calculate the maximum number of jokes she can tell before the show ends.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases.
The only line of each testcase contains four integers $a_1, a_2, a_3, a_4$ ($0 \le a_1, a_2, a_3, a_4 \le 10^8$; $a_1 + a_2 + a_3 + a_4 \ge 1$) — the number of jokes of each type Eve prepared.
-----Output-----
For each testcase, print a single integer — the maximum number of jokes Eve can tell before at least one of the spectators leaves or before she runs out of jokes.
-----Examples-----
Input
4
5 0 0 0
0 0 0 5
2 5 10 6
3 0 0 7
Output
5
1
15
7
-----Note-----
In the first testcase, Eve only has jokes of the first type. Thus, there's no order to choose. She tells all her jokes, both Alice and Bob like them. Their mood becomes $5$. The show ends after Eve runs out of jokes.
In the second testcase, Eve only has jokes of the fourth type. Thus, once again no order to choose. She tells a joke, and neither Alice, nor Bob likes it. Their mood decrease by one, becoming $-1$. They both have negative mood, thus, both leave, and the show ends.
In the third testcase, first, Eve tells both jokes of the first type. Both Alice and Bob has mood $2$. Then she can tell $2$ jokes of the third type. Alice's mood becomes $0$. Bob's mood becomes $4$. Then $4$ jokes of the second type. Alice's mood becomes $4$. Bob's mood becomes $0$. Then another $4$ jokes of the third type. Alice's mood becomes $0$. Bob's mood becomes $4$. Then the remaining joke of the second type. Alice's mood becomes $1$. Bob's mood becomes $3$. Then one more joke of the third type, and a joke of the fourth type, for example. Alice's mood becomes $-1$, she leaves, and the show ends.
In the fourth testcase, Eve should first tell the jokes both spectators like, then the jokes they don't. She can tell $4$ jokes of the fourth type until the spectators leave.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
After their adventure with the magic mirror Kay and Gerda have returned home and sometimes give free ice cream to kids in the summer.
At the start of the day they have x ice cream packs. Since the ice cream is free, people start standing in the queue before Kay and Gerda's house even in the night. Each person in the queue wants either to take several ice cream packs for himself and his friends or to give several ice cream packs to Kay and Gerda (carriers that bring ice cream have to stand in the same queue).
If a carrier with d ice cream packs comes to the house, then Kay and Gerda take all his packs. If a child who wants to take d ice cream packs comes to the house, then Kay and Gerda will give him d packs if they have enough ice cream, otherwise the child will get no ice cream at all and will leave in distress.
Kay wants to find the amount of ice cream they will have after all people will leave from the queue, and Gerda wants to find the number of distressed kids.
-----Input-----
The first line contains two space-separated integers n and x (1 ≤ n ≤ 1000, 0 ≤ x ≤ 10^9).
Each of the next n lines contains a character '+' or '-', and an integer d_{i}, separated by a space (1 ≤ d_{i} ≤ 10^9). Record "+ d_{i}" in i-th line means that a carrier with d_{i} ice cream packs occupies i-th place from the start of the queue, and record "- d_{i}" means that a child who wants to take d_{i} packs stands in i-th place.
-----Output-----
Print two space-separated integers — number of ice cream packs left after all operations, and number of kids that left the house in distress.
-----Examples-----
Input
5 7
+ 5
- 10
- 20
+ 40
- 20
Output
22 1
Input
5 17
- 16
- 2
- 98
+ 100
- 98
Output
3 2
-----Note-----
Consider the first sample. Initially Kay and Gerda have 7 packs of ice cream. Carrier brings 5 more, so now they have 12 packs. A kid asks for 10 packs and receives them. There are only 2 packs remaining. Another kid asks for 20 packs. Kay and Gerda do not have them, so the kid goes away distressed. Carrier bring 40 packs, now Kay and Gerda have 42 packs. Kid asks for 20 packs and receives them. There are 22 packs remaining.
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 length $n$ consisting of integers. You can apply the following operation, consisting of several steps, on the array $a$ zero or more times:
you select two different numbers in the array $a_i$ and $a_j$;
you remove $i$-th and $j$-th elements from the array.
For example, if $n=6$ and $a=[1, 6, 1, 1, 4, 4]$, then you can perform the following sequence of operations:
select $i=1, j=5$. The array $a$ becomes equal to $[6, 1, 1, 4]$;
select $i=1, j=2$. The array $a$ becomes equal to $[1, 4]$.
What can be the minimum size of the array after applying some sequence of operations to it?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) is length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
-----Examples-----
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
-----Note-----
None
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 points on a straight line. Each point has a color assigned to it. For point a, its neighbors are the points which don't have any other points between them and a. Each point has at most two neighbors - one from the left and one from the right.
You perform a sequence of operations on this set of points. In one operation, you delete all points which have a neighbor point of a different color than the point itself. Points are deleted simultaneously, i.e. first you decide which points have to be deleted and then delete them. After that you can perform the next operation etc. If an operation would not delete any points, you can't perform it.
How many operations will you need to perform until the next operation does not have any points to delete?
-----Input-----
Input contains a single string of lowercase English letters 'a'-'z'. The letters give the points' colors in the order in which they are arranged on the line: the first letter gives the color of the leftmost point, the second gives the color of the second point from the left etc.
The number of the points is between 1 and 10^6.
-----Output-----
Output one line containing an integer - the number of operations which can be performed on the given set of points until there are no more points to delete.
-----Examples-----
Input
aabb
Output
2
Input
aabcaa
Output
1
-----Note-----
In the first test case, the first operation will delete two middle points and leave points "ab", which will be deleted with the second operation. There will be no points left to apply the third operation to.
In the second test case, the first operation will delete the four points in the middle, leaving points "aa". None of them have neighbors of other colors, so the second operation can't be applied.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Given is a positive integer N.
We will choose an integer K between 2 and N (inclusive), then we will repeat the operation below until N becomes less than K.
- Operation: if K divides N, replace N with N/K; otherwise, replace N with N-K.
In how many choices of K will N become 1 in the end?
-----Constraints-----
- 2 \leq N \leq 10^{12}
- N is an integer.
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
Print the number of choices of K in which N becomes 1 in the end.
-----Sample Input-----
6
-----Sample Output-----
3
There are three choices of K in which N becomes 1 in the end: 2, 5, and 6.
In each of these choices, N will change as follows:
- When K=2: 6 \to 3 \to 1
- When K=5: 6 \to 1
- When K=6: 6 \to 1
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
During a New Year special offer the "Sudislavl Bars" offered n promo codes. Each promo code consists of exactly six digits and gives right to one free cocktail at the bar "Mosquito Shelter". Of course, all the promocodes differ.
As the "Mosquito Shelter" opens only at 9, and partying in Sudislavl usually begins at as early as 6, many problems may arise as to how to type a promotional code without errors. It is necessary to calculate such maximum k, that the promotional code could be uniquely identified if it was typed with no more than k errors. At that, k = 0 means that the promotional codes must be entered exactly.
A mistake in this problem should be considered as entering the wrong numbers. For example, value "123465" contains two errors relative to promocode "123456". Regardless of the number of errors the entered value consists of exactly six digits.
-----Input-----
The first line of the output contains number n (1 ≤ n ≤ 1000) — the number of promocodes.
Each of the next n lines contains a single promocode, consisting of exactly 6 digits. It is guaranteed that all the promocodes are distinct. Promocodes can start from digit "0".
-----Output-----
Print the maximum k (naturally, not exceeding the length of the promocode), such that any promocode can be uniquely identified if it is typed with at most k mistakes.
-----Examples-----
Input
2
000000
999999
Output
2
Input
6
211111
212111
222111
111111
112111
121111
Output
0
-----Note-----
In the first sample k < 3, so if a bar customer types in value "090909", then it will be impossible to define which promocode exactly corresponds to it.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
For a given sequence $A = \\{a_0, a_1, ..., a_{n-1}\\}$ which is sorted by ascending order, find a specific value $k$ given as a query.
Constraints
* $1 \leq n \leq 100,000$
* $1 \leq q \leq 200,000$
* $0 \leq a_0 \leq a_1 \leq ... \leq a_{n-1} \leq 1,000,000,000$
* $0 \leq k_i \leq 1,000,000,000$
Input
The input is given in the following format.
$n$
$a_0 \; a_1 \; ,..., \; a_{n-1}$
$q$
$k_1$
$k_2$
:
$k_q$
The number of elements $n$ and each element $a_i$ are given in the first line and the second line respectively. In the third line, the number of queries $q$ is given and the following $q$ lines, $q$ integers $k_i$ are given as queries.
Output
For each query, print 1 if any element in $A$ is equivalent to $k$, and 0 otherwise.
Example
Input
4
1 2 2 4
3
2
3
5
Output
1
0
0
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Squirrel Liss is interested in sequences. She also has preferences of integers. She thinks n integers a1, a2, ..., an are good.
Now she is interested in good sequences. A sequence x1, x2, ..., xk is called good if it satisfies the following three conditions:
* The sequence is strictly increasing, i.e. xi < xi + 1 for each i (1 ≤ i ≤ k - 1).
* No two adjacent elements are coprime, i.e. gcd(xi, xi + 1) > 1 for each i (1 ≤ i ≤ k - 1) (where gcd(p, q) denotes the greatest common divisor of the integers p and q).
* All elements of the sequence are good integers.
Find the length of the longest good sequence.
Input
The input consists of two lines. The first line contains a single integer n (1 ≤ n ≤ 105) — the number of good integers. The second line contains a single-space separated list of good integers a1, a2, ..., an in strictly increasing order (1 ≤ ai ≤ 105; ai < ai + 1).
Output
Print a single integer — the length of the longest good sequence.
Examples
Input
5
2 3 4 6 9
Output
4
Input
9
1 2 3 5 6 7 8 9 10
Output
4
Note
In the first example, the following sequences are examples of good sequences: [2; 4; 6; 9], [2; 4; 6], [3; 9], [6]. The length of the longest good sequence is 4.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Polycarp knows that if the sum of the digits of a number is divisible by $3$, then the number itself is divisible by $3$. He assumes that the numbers, the sum of the digits of which is divisible by $4$, are also somewhat interesting. Thus, he considers a positive integer $n$ interesting if its sum of digits is divisible by $4$.
Help Polycarp find the nearest larger or equal interesting number for the given number $a$. That is, find the interesting number $n$ such that $n \ge a$ and $n$ is minimal.
-----Input-----
The only line in the input contains an integer $a$ ($1 \le a \le 1000$).
-----Output-----
Print the nearest greater or equal interesting number for the given number $a$. In other words, print the interesting number $n$ such that $n \ge a$ and $n$ is minimal.
-----Examples-----
Input
432
Output
435
Input
99
Output
103
Input
237
Output
237
Input
42
Output
44
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
In the Kingdom of AtCoder, only banknotes are used as currency. There are 10^{100}+1 kinds of banknotes, with the values of 1, 10, 10^2, 10^3, \dots, 10^{(10^{100})}. You have come shopping at a mall and are now buying a takoyaki machine with a value of N. (Takoyaki is the name of a Japanese snack.)
To make the payment, you will choose some amount of money which is at least N and give it to the clerk. Then, the clerk gives you back the change, which is the amount of money you give minus N.
What will be the minimum possible number of total banknotes used by you and the clerk, when both choose the combination of banknotes to minimize this count?
Assume that you have sufficient numbers of banknotes, and so does the clerk.
-----Constraints-----
- N is an integer between 1 and 10^{1,000,000} (inclusive).
-----Input-----
Input is given from Standard Input in the following format:
N
-----Output-----
Print the minimum possible number of total banknotes used by you and the clerk.
-----Sample Input-----
36
-----Sample Output-----
8
If you give four banknotes of value 10 each, and the clerk gives you back four banknotes of value 1 each, a total of eight banknotes are used.
The payment cannot be made with less than eight banknotes in total, so the answer is 8.
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 function $f(x)$ in such a way: we add $1$ to $x$, then, while there is at least one trailing zero in the resulting number, we remove that zero. For example, $f(599) = 6$: $599 + 1 = 600 \rightarrow 60 \rightarrow 6$; $f(7) = 8$: $7 + 1 = 8$; $f(9) = 1$: $9 + 1 = 10 \rightarrow 1$; $f(10099) = 101$: $10099 + 1 = 10100 \rightarrow 1010 \rightarrow 101$.
We say that some number $y$ is reachable from $x$ if we can apply function $f$ to $x$ some (possibly zero) times so that we get $y$ as a result. For example, $102$ is reachable from $10098$ because $f(f(f(10098))) = f(f(10099)) = f(101) = 102$; and any number is reachable from itself.
You are given a number $n$; your task is to count how many different numbers are reachable from $n$.
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 10^9$).
-----Output-----
Print one integer: the number of different numbers that are reachable from $n$.
-----Examples-----
Input
1098
Output
20
Input
10
Output
19
-----Note-----
The numbers that are reachable from $1098$ are:
$1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1098, 1099$.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Implement the function which should return `true` if given object is a vowel (meaning `a, e, i, o, u`), and `false` otherwise.
Write your solution by modifying this code:
```python
def is_vowel(s):
```
Your solution should implemented in the function "is_vowel". The i
Read the inputs from stdin solve the problem and write the answer to stdout (do 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's bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation.
We know that the i-th star on the pedal axle has ai (0 < a1 < a2 < ... < an) teeth, and the j-th star on the rear wheel axle has bj (0 < b1 < b2 < ... < bm) teeth. Any pair (i, j) (1 ≤ i ≤ n; 1 ≤ j ≤ m) is called a gear and sets the indexes of stars to which the chain is currently attached. Gear (i, j) has a gear ratio, equal to the value <image>.
Since Vasya likes integers, he wants to find such gears (i, j), that their ratios are integers. On the other hand, Vasya likes fast driving, so among all "integer" gears (i, j) he wants to choose a gear with the maximum ratio. Help him to find the number of such gears.
In the problem, fraction <image> denotes division in real numbers, that is, no rounding is performed.
Input
The first input line contains integer n (1 ≤ n ≤ 50) — the number of stars on the bicycle's pedal axle. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 104) in the order of strict increasing.
The third input line contains integer m (1 ≤ m ≤ 50) — the number of stars on the rear wheel axle. The fourth line contains m integers b1, b2, ..., bm (1 ≤ bi ≤ 104) in the order of strict increasing.
It is guaranteed that there exists at least one gear (i, j), that its gear ratio is an integer. The numbers on the lines are separated by spaces.
Output
Print the number of "integer" gears with the maximum ratio among all "integer" gears.
Examples
Input
2
4 5
3
12 13 15
Output
2
Input
4
1 2 3 4
5
10 11 12 13 14
Output
1
Note
In the first sample the maximum "integer" gear ratio equals 3. There are two gears that have such gear ratio. For one of them a1 = 4, b1 = 12, and for the other a2 = 5, b3 = 15.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Consider the following process. You have a binary string (a string where each character is either 0 or 1) $w$ of length $n$ and an integer $x$. You build a new binary string $s$ consisting of $n$ characters. The $i$-th character of $s$ is chosen as follows:
if the character $w_{i-x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i > x$ and $w_{i-x} = $ 1, then $s_i = $ 1); if the character $w_{i+x}$ exists and is equal to 1, then $s_i$ is 1 (formally, if $i + x \le n$ and $w_{i+x} = $ 1, then $s_i = $ 1); if both of the aforementioned conditions are false, then $s_i$ is 0.
You are given the integer $x$ and the resulting string $s$. Reconstruct the original string $w$.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Each test case consists of two lines. The first line contains the resulting string $s$ ($2 \le |s| \le 10^5$, each character of $s$ is either 0 or 1). The second line contains one integer $x$ ($1 \le x \le |s| - 1$).
The total length of all strings $s$ in the input does not exceed $10^5$.
-----Output-----
For each test case, print the answer on a separate line as follows:
if no string $w$ can produce the string $s$ at the end of the process, print $-1$; otherwise, print the binary string $w$ consisting of $|s|$ characters. If there are multiple answers, print any of them.
-----Example-----
Input
3
101110
2
01
1
110
1
Output
111011
10
-1
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 number is called powerful if it is a power of two or a factorial. In other words, the number $m$ is powerful if there exists a non-negative integer $d$ such that $m=2^d$ or $m=d!$, where $d!=1\cdot 2\cdot \ldots \cdot d$ (in particular, $0! = 1$). For example $1$, $4$, and $6$ are powerful numbers, because $1=1!$, $4=2^2$, and $6=3!$ but $7$, $10$, or $18$ are not.
You are given a positive integer $n$. Find the minimum number $k$ such that $n$ can be represented as the sum of $k$ distinct powerful numbers, or say that there is no such $k$.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows.
A test case consists of only one line, containing one integer $n$ ($1\le n\le 10^{12}$).
-----Output-----
For each test case print the answer on a separate line.
If $n$ can not be represented as the sum of distinct powerful numbers, print $-1$.
Otherwise, print a single positive integer — the minimum possible value of $k$.
-----Examples-----
Input
4
7
11
240
17179869184
Output
2
3
4
1
-----Note-----
In the first test case, $7$ can be represented as $7=1+6$, where $1$ and $6$ are powerful numbers. Because $7$ is not a powerful number, we know that the minimum possible value of $k$ in this case is $k=2$.
In the second test case, a possible way to represent $11$ as the sum of three powerful numbers is $11=1+4+6$. We can show that there is no way to represent $11$ as the sum of two or less powerful numbers.
In the third test case, $240$ can be represented as $240=24+32+64+120$. Observe that $240=120+120$ is not a valid representation, because the powerful numbers have to be distinct.
In the fourth test case, $17179869184=2^{34}$, so $17179869184$ is a powerful number and the minimum $k$ in this case is $k=1$.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the occasion of the city's anniversary, a decision was taken to pave the Square with square granite flagstones. Each flagstone is of the size a × a.
What is the least number of flagstones needed to pave the Square? It's allowed to cover the surface larger than the Theatre Square, but the Square has to be covered. It's not allowed to break the flagstones. The sides of flagstones should be parallel to the sides of the Square.
Input
The input contains three positive integer numbers in the first line: n, m and a (1 ≤ n, m, a ≤ 109).
Output
Write the needed number of flagstones.
Examples
Input
6 6 4
Output
4
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
You're given an array $b$ of length $n$. Let's define another array $a$, also of length $n$, for which $a_i = 2^{b_i}$ ($1 \leq i \leq n$).
Valerii says that every two non-intersecting subarrays of $a$ have different sums of elements. You want to determine if he is wrong. More formally, you need to determine if there exist four integers $l_1,r_1,l_2,r_2$ that satisfy the following conditions: $1 \leq l_1 \leq r_1 \lt l_2 \leq r_2 \leq n$; $a_{l_1}+a_{l_1+1}+\ldots+a_{r_1-1}+a_{r_1} = a_{l_2}+a_{l_2+1}+\ldots+a_{r_2-1}+a_{r_2}$.
If such four integers exist, you will prove Valerii wrong. Do they exist?
An array $c$ is a subarray of an array $d$ if $c$ can be obtained from $d$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). Description of the test cases follows.
The first line of every test case contains a single integer $n$ ($2 \le n \le 1000$).
The second line of every test case contains $n$ integers $b_1,b_2,\ldots,b_n$ ($0 \le b_i \le 10^9$).
-----Output-----
For every test case, if there exist two non-intersecting subarrays in $a$ that have the same sum, output YES on a separate line. Otherwise, output NO on a separate line.
Also, note that each letter can be in any case.
-----Example-----
Input
2
6
4 3 0 1 2 0
2
2 5
Output
YES
NO
-----Note-----
In the first case, $a = [16,8,1,2,4,1]$. Choosing $l_1 = 1$, $r_1 = 1$, $l_2 = 2$ and $r_2 = 6$ works because $16 = (8+1+2+4+1)$.
In the second case, you can verify that there is no way to select to such subarrays.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
On the Planet AtCoder, there are four types of bases: A, C, G and T. A bonds with T, and C bonds with G.
You are given a letter b as input, which is A, C, G or T. Write a program that prints the letter representing the base that bonds with the base b.
-----Constraints-----
- b is one of the letters A, C, G and T.
-----Input-----
Input is given from Standard Input in the following format:
b
-----Output-----
Print the letter representing the base that bonds with the base b.
-----Sample Input-----
A
-----Sample Output-----
T
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Mr. Kay was browsing a certain SNS as usual, the problem that "there are people who can solve IQ150 or more" came to the timeline. Mr. Kay has an IQ of over 150, so he solved the problem in an instant without even looking at it. For him, he doesn't have to work on such a mystery. It is enough to leave it to the computer.
problem
The following mysterious mathematical formula was written in the problem.
* \\ (5 + 3 = 28 \\)
* \\ (9 + 1 = 810 \\)
* \\ (8 + 6 = 214 \\)
* \\ (5 + 4 = 19 \\)
* \\ (2 + 2 = 4 \\)
* \\ (15 + 8 = 723 \\)
* \\ (7 + 9 = -216 \\)
* \\ (3 + 0 = 33 \\)
When thinking about the above operator \\ (+ \\), for a positive integer \\ (a \\) \\ (x \ geq 0, y \ geq 0 \\) and \\ (x + y =) Find the number of integer pairs \\ (x, y \\) such that a \\).
input
A positive integer \\ (a \\) is given on one line.
output
Output the number of pairs \\ ((x, y) \\) that satisfy \\ (a = x + y, x \ geq 0, y \ geq 0 \\) on one line.
Constraint
* \\ (1 \ leq a \ leq 10 ^ 9 (= 1000000000) \\)
Input / output example
Input 1
19
Output 1
1
There is one way of \\ (5 + 4 \\).
Input 2
twenty two
Output 2
2
There are two types: \\ (11 + 11 \\) and \\ (2 + 0 \\).
Input 3
1
Output 3
0
\\ (1 + 0 = 11, 0 + 1 = -11 \\). \\ (1 \\) cannot be generated.
Input 4
101
Output 4
0
Note that \\ (1 --0 \\) is not \\ (101 \\).
Input 5
660233276
Output 5
Four
Example
Input
19
Output
1
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Write a program which prints $n$-th fibonacci number for a given integer $n$. The $n$-th fibonacci number is defined by the following recursive formula:
\begin{equation*} fib(n)= \left \\{ \begin{array}{ll} 1 & (n = 0) \\\ 1 & (n = 1) \\\ fib(n - 1) + fib(n - 2) & \\\ \end{array} \right. \end{equation*}
Constraints
* $0 \leq n \leq 44$
Input
An integer $n$ is given.
Example
Input
3
Output
3
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Everlasting Sa-Ga, a new, hot and very popular role-playing game, is out on October 19, 2008. Fans have been looking forward to a new title of Everlasting Sa-Ga.
Little Jimmy is in trouble. He is a seven-year-old boy, and he obtained the Everlasting Sa-Ga and is attempting to reach the end of the game before his friends. However, he is facing difficulty solving the riddle of the first maze in this game -- Everlasting Sa-Ga is notorious in extremely hard riddles like Neverending Fantasy and Forever Quest.
The riddle is as follows. There are two doors on the last floor of the maze: the door to the treasure repository and the gate to the hell. If he wrongly opens the door to the hell, the game is over and his save data will be deleted. Therefore, he should never open the wrong door.
So now, how can he find the door to the next stage? There is a positive integer given for each door -- it is a great hint to this riddle. The door to the treasure repository has the integer that gives the larger key number. The key number of a positive integer n is the largest prime factor minus the total sum of any other prime factors, where the prime factors are the prime numbers that divide into n without leaving a remainder. Note that each prime factor should be counted only once.
As an example, suppose there are doors with integers 30 and 20 respectively. Since 30 has three prime factors 2, 3 and 5, its key number is 5 - (2 + 3) = 0. Similarly, since 20 has two prime factors 2 and 5, its key number 20 is 5 - 2 = 3. Jimmy therefore should open the door with 20.
Your job is to write a program to help Jimmy by solving this riddle.
Input
The input is a sequence of datasets. Each dataset consists of a line that contains two integers a and b separated by a space (2 ≤ a, b ≤ 106 ). It is guaranteed that key numbers of these integers are always different.
The input is terminated by a line with two zeros. This line is not part of any datasets and thus should not be processed.
Output
For each dataset, print in a line ‘a’ (without quotes) if the door with the integer a is connected to the treasure repository; print ‘b’ otherwise. No extra space or character is allowed.
Example
Input
10 15
30 20
0 0
Output
a
b
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Tanechka is shopping in the toy shop. There are exactly $n$ toys in the shop for sale, the cost of the $i$-th toy is $i$ burles. She wants to choose two toys in such a way that their total cost is $k$ burles. How many ways to do that does she have?
Each toy appears in the shop exactly once. Pairs $(a, b)$ and $(b, a)$ are considered equal. Pairs $(a, b)$, where $a=b$, are not allowed.
-----Input-----
The first line of the input contains two integers $n$, $k$ ($1 \le n, k \le 10^{14}$) — the number of toys and the expected total cost of the pair of toys.
-----Output-----
Print the number of ways to choose the pair of toys satisfying the condition above. Print 0, if Tanechka can choose no pair of toys in such a way that their total cost is $k$ burles.
-----Examples-----
Input
8 5
Output
2
Input
8 15
Output
1
Input
7 20
Output
0
Input
1000000000000 1000000000001
Output
500000000000
-----Note-----
In the first example Tanechka can choose the pair of toys ($1, 4$) or the pair of toys ($2, 3$).
In the second example Tanechka can choose only the pair of toys ($7, 8$).
In the third example choosing any pair of toys will lead to the total cost less than $20$. So the answer is 0.
In the fourth example she can choose the following pairs: $(1, 1000000000000)$, $(2, 999999999999)$, $(3, 999999999998)$, ..., $(500000000000, 500000000001)$. The number of such pairs is exactly $500000000000$.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to come up with an interesting problem and asked the crew to solve it.
In the beginning, uncle Bogdan wrote on a board a positive integer $x$ consisting of $n$ digits. After that, he wiped out $x$ and wrote integer $k$ instead, which was the concatenation of binary representations of digits $x$ consists of (without leading zeroes). For example, let $x = 729$, then $k = 111101001$ (since $7 = 111$, $2 = 10$, $9 = 1001$).
After some time, uncle Bogdan understood that he doesn't know what to do with $k$ and asked Denis to help. Denis decided to wipe last $n$ digits of $k$ and named the new number as $r$.
As a result, Denis proposed to find such integer $x$ of length $n$ that $r$ (as number) is maximum possible. If there are multiple valid $x$ then Denis is interested in the minimum one.
All crew members, including captain Flint himself, easily solved the task. All, except cabin boy Kostya, who was too drunk to think straight. But what about you?
Note: in this task, we compare integers ($x$ or $k$) as numbers (despite what representations they are written in), so $729 < 1999$ or $111 < 1000$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Next $t$ lines contain test cases — one per test case. The one and only line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) — the length of the integer $x$ you need to find.
It's guaranteed that the sum of $n$ from all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print the minimum integer $x$ of length $n$ such that obtained by Denis number $r$ is maximum possible.
-----Example-----
Input
2
1
3
Output
8
998
-----Note-----
In the second test case (with $n = 3$), if uncle Bogdan had $x = 998$ then $k = 100110011000$. Denis (by wiping last $n = 3$ digits) will obtain $r = 100110011$.
It can be proved that the $100110011$ is the maximum possible $r$ Denis can obtain and $998$ is the minimum $x$ to obtain it.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Monocarp has forgotten the password to his mobile phone. The password consists of $4$ digits from $0$ to $9$ (note that it can start with the digit $0$).
Monocarp remembers that his password had exactly two different digits, and each of these digits appeared exactly two times in the password. Monocarp also remembers some digits which were definitely not used in the password.
You have to calculate the number of different sequences of $4$ digits that could be the password for Monocarp's mobile phone (i. e. these sequences should meet all constraints on Monocarp's password).
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 200$) — the number of testcases.
The first line of each testcase contains a single integer $n$ ($1 \le n \le 8$) — the number of digits for which Monocarp remembers that they were not used in the password.
The second line contains $n$ different integers $a_1, a_2, \dots a_n$ ($0 \le a_i \le 9$) representing the digits that were not used in the password. Note that the digits $a_1, a_2, \dots, a_n$ are given in ascending order.
-----Output-----
For each testcase, print one integer — the number of different $4$-digit sequences that meet the constraints.
-----Examples-----
Input
2
8
0 1 2 4 5 6 8 9
1
8
Output
6
216
-----Note-----
In the first example, all possible passwords are: "3377", "3737", "3773", "7337", "7373", "7733".
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Hedgehog recently remembered one of his favorite childhood activities, — solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one.
Soon the Hedgehog came up with a brilliant idea: instead of buying ready-made puzzles, one can take his own large piece of paper with some picture and cut it into many small rectangular pieces, then mix them and solve the resulting puzzle, trying to piece together the picture. The resulting task is even more challenging than the classic puzzle: now all the fragments have the same rectangular shape, and one can assemble the puzzle only relying on the picture drawn on the pieces.
All puzzle pieces turn out to be of the same size X × Y, because the picture is cut first by horizontal cuts with the pitch of X, then with vertical cuts with the pitch of Y. If we denote the initial size of the picture as A × B, then A must be divisible by X and B must be divisible by Y (X and Y are integer numbers).
However, not every such cutting of the picture will result in a good puzzle. The Hedgehog finds a puzzle good if no two pieces in it are the same (It is allowed to rotate the pieces when comparing them, but it is forbidden to turn them over).
Your task is to count for a given picture the number of good puzzles that you can make from it, and also to find the puzzle with the minimal piece size.
Input
The first line contains two numbers A and B which are the sizes of the picture. They are positive integers not exceeding 20.
Then follow A lines containing B symbols each, describing the actual picture. The lines only contain uppercase English letters.
Output
In the first line print the number of possible good puzzles (in other words, the number of pairs (X, Y) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself.
In the second line print two numbers — the sizes X and Y of the smallest possible element among all good puzzles. The comparison is made firstly by the area XY of one element and secondly — by the length X.
Examples
Input
2 4
ABDC
ABDC
Output
3
2 1
Input
2 6
ABCCBA
ABCCBA
Output
1
2 6
Note
The picture in the first sample test has the following good puzzles: (2, 1), (2, 2), (2, 4).
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
problem
There are $ N $ propositions, named $ 1, 2, \ cdots, N $, respectively. Also, $ M $ information about the propositions is given. The $ i $ th information is "$ a_i $$". Given in the form "b_i $", which means that $ a_i $ is $ b_i $. ("If" is a logical conditional and the transition law holds.) $ For each proposition $ i $ Output all propositions that have the same value as i $ in ascending order. However, proposition $ i $ and proposition $ i $ are always the same value. Proposition $ X $ and proposition $ Y $ have the same value as "$ if $ X $". It means "Y $" and "$ X $ if $ Y $".
output
On the $ i $ line, output all propositions that have the same value as the proposition $ i $, separated by blanks in ascending order. Also, output a line break at the end of each line.
Example
Input
5 2
1 2
2 1
Output
1 2
1 2
3
4
5
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Center for Picassonian Cubism is a Spanish national museum of cubist artworks, dedicated to Pablo Picasso. The center held a competition for an artwork that will be displayed in front of the facade of the museum building. The artwork is a collection of cubes that are piled up on the ground and is intended to amuse visitors, who will be curious how the shape of the collection of cubes changes when it is seen from the front and the sides.
The artwork is a collection of cubes with edges of one foot long and is built on a flat ground that is divided into a grid of one foot by one foot squares. Due to some technical reasons, cubes of the artwork must be either put on the ground, fitting into a unit square in the grid, or put on another cube in the way that the bottom face of the upper cube exactly meets the top face of the lower cube. No other way of putting cubes is possible.
You are a member of the judging committee responsible for selecting one out of a plenty of artwork proposals submitted to the competition. The decision is made primarily based on artistic quality but the cost for installing the artwork is another important factor. Your task is to investigate the installation cost for each proposal. The cost is proportional to the number of cubes, so you have to figure out the minimum number of cubes needed for installation.
Each design proposal of an artwork consists of the front view and the side view (the view seen from the right-hand side), as shown in Figure 1.
<image>
Figure 1: An example of an artwork proposal
The front view (resp., the side view) indicates the maximum heights of piles of cubes for each column line (resp., row line) of the grid.
There are several ways to install this proposal of artwork, such as the following figures.
<image>
In these figures, the dotted lines on the ground indicate the grid lines. The left figure makes use of 16 cubes, which is not optimal. That is, the artwork can be installed with a fewer number of cubes. Actually, the right one is optimal and only uses 13 cubes. Note that, a single pile of height three in the right figure plays the roles of two such piles in the left one.
Notice that swapping columns of cubes does not change the side view. Similarly, swapping rows does not change the front view. Thus, such swaps do not change the costs of building the artworks.
For example, consider the artwork proposal given in Figure 2.
<image>
Figure 2: Another example of artwork proposal
An optimal installation of this proposal of artwork can be achieved with 13 cubes, as shown in the following figure, which can be obtained by exchanging the rightmost two columns of the optimal installation of the artwork of Figure 1.
<image>
Input
The input is a sequence of datasets. The end of the input is indicated by a line containing two zeros separated by a space. Each dataset is formatted as follows.
w d
h1 h2 ... hw
h'1 h'2 ... h'd
The integers w and d separated by a space are the numbers of columns and rows of the grid, respectively. You may assume 1 ≤ w ≤ 10 and 1 ≤ d ≤ 10. The integers separated by a space in the second and third lines specify the shape of the artwork. The integers hi (1 ≤ hi ≤ 20, 1 ≤ i ≤ w) in the second line give the front view, i.e., the maximum heights of cubes per each column line, ordered from left to right (seen from the front). The integers hi (1 ≤ hi ≤ 20, 1 ≤ i ≤ d) in the third line give the side view, i.e., the maximum heights of cubes per each row line, ordered from left to right (seen from the right-hand side).
Output
For each dataset, output a line containing the minimum number of cubes. The output should not contain any other extra characters.
You can assume that, for each dataset, there is at least one way to install the artwork.
Example
Input
5 5
1 2 3 4 5
1 2 3 4 5
5 5
2 5 4 1 3
4 1 5 3 2
5 5
1 2 3 4 5
3 3 3 4 5
3 3
7 7 7
7 7 7
3 3
4 4 4
4 3 4
4 3
4 2 2 4
4 2 1
4 4
2 8 8 8
2 3 8 3
10 10
9 9 9 9 9 9 9 9 9 9
9 9 9 9 9 9 9 9 9 9
10 9
20 1 20 20 20 20 20 18 20 20
20 20 20 20 7 20 20 20 20
0 0
Output
15
15
21
21
15
13
32
90
186
Read the inputs from stdin solve the problem and write the answer to stdout (do 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
Yamano Mifune Gakuen's 1st grade G group is a class where female students carrying misfortune gather. They face various challenges every day with the goal of being happy.
In their class, they can take practical happiness courses as part of the test of happiness.
From Monday to Friday, there are classes from 1st to Nth, and there are M courses that can be taken.
Subject i starts from the ai period of the day of the week di (di = 0, 1, 2, 3, 4 corresponds to Monday, Tuesday, Wednesday, Thursday, and Friday, respectively), and is performed in consecutive ki frames. The degree of happiness obtained when taking the course is ti.
Each student is free to choose up to L subjects so that they do not overlap each other. How do you choose the subject to get the highest level of happiness? Please find the maximum value of happiness that can be obtained from the information of the given subject.
Constraints
* 2 ≤ N ≤ 8
* 0 ≤ M ≤ 300
* 0 ≤ L ≤ min (N × 5, M)
* 0 ≤ di ≤ 4
* 1 ≤ ai ≤ N
* 1 ≤ ki
* ai + ki --1 ≤ N
* 1 ≤ ti ≤ 100
Input
The input is given in the following format.
N M L
d1 a1 k1 t1
d2 a2 k2 t2
...
dM aM kM tM
The first line is given three integers N, M, L separated by blanks.
The four integers di, ai, ki, and ti are given on the 2nd to M + 1th lines, separated by blanks.
Output
Output the maximum value of the sum of happiness on one line.
Examples
Input
3 7 3
0 1 1 1
0 1 1 2
1 1 3 4
1 1 1 1
1 2 1 2
2 1 1 3
2 2 2 1
Output
9
Input
5 10 5
0 1 1 2
0 2 1 2
0 1 2 3
1 2 1 2
1 4 2 3
2 1 1 1
2 1 1 2
3 3 2 3
4 1 1 2
4 2 1 2
Output
13
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedule to the lamp. Every day it will turn power on at moment $0$ and turn power off at moment $M$. Moreover, the lamp allows you to set a program of switching its state (states are "lights on" and "lights off"). Unfortunately, some program is already installed into the lamp.
The lamp allows only good programs. Good program can be represented as a non-empty array $a$, where $0 < a_1 < a_2 < \dots < a_{|a|} < M$. All $a_i$ must be integers. Of course, preinstalled program is a good program.
The lamp follows program $a$ in next manner: at moment $0$ turns power and light on. Then at moment $a_i$ the lamp flips its state to opposite (if it was lit, it turns off, and vice versa). The state of the lamp flips instantly: for example, if you turn the light off at moment $1$ and then do nothing, the total time when the lamp is lit will be $1$. Finally, at moment $M$ the lamp is turning its power off regardless of its state.
Since you are not among those people who read instructions, and you don't understand the language it's written in, you realize (after some testing) the only possible way to alter the preinstalled program. You can insert at most one element into the program $a$, so it still should be a good program after alteration. Insertion can be done between any pair of consecutive elements of $a$, or even at the begining or at the end of $a$.
Find such a way to alter the program that the total time when the lamp is lit is maximum possible. Maybe you should leave program untouched. If the lamp is lit from $x$ till moment $y$, then its lit for $y - x$ units of time. Segments of time when the lamp is lit are summed up.
-----Input-----
First line contains two space separated integers $n$ and $M$ ($1 \le n \le 10^5$, $2 \le M \le 10^9$) — the length of program $a$ and the moment when power turns off.
Second line contains $n$ space separated integers $a_1, a_2, \dots, a_n$ ($0 < a_1 < a_2 < \dots < a_n < M$) — initially installed program $a$.
-----Output-----
Print the only integer — maximum possible total time when the lamp is lit.
-----Examples-----
Input
3 10
4 6 7
Output
8
Input
2 12
1 10
Output
9
Input
2 7
3 4
Output
6
-----Note-----
In the first example, one of possible optimal solutions is to insert value $x = 3$ before $a_1$, so program will be $[3, 4, 6, 7]$ and time of lamp being lit equals $(3 - 0) + (6 - 4) + (10 - 7) = 8$. Other possible solution is to insert $x = 5$ in appropriate place.
In the second example, there is only one optimal solution: to insert $x = 2$ between $a_1$ and $a_2$. Program will become $[1, 2, 10]$, and answer will be $(1 - 0) + (10 - 2) = 9$.
In the third example, optimal answer is to leave program untouched, so answer will be $(3 - 0) + (7 - 4) = 6$.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Backgorund
The super popular game "Puzzle & Hexagons" has finally been released. This game is so funny that many people are addicted to it. There were a number of people who were certified as addicted by doctors because of their excessive enthusiasm. Volunteers from around the world have created a "Puzzle & Hexagons" simulator to help addicts in the game and try to encourage them to avoid playing on dangerous real machines. I want you to cooperate in making a simulator.
Problem
A board with H squares in the vertical direction and W in the horizontal direction is given. Fig.1 shows the board surface when H = 4 and W = 7, and the coordinates (x, y) of the corresponding squares.
<image>
Fig.1
In the initial state, each square has a colored block. The color of the block is expressed by one letter of the alphabet as follows.
*'R' ・ ・ ・ Red
*'G' ・ ・ ・ Green
*'B' ・ ・ ・ Blue
*'P' ・ ・ ・ Purple
*'Y' ・ ・ ・ Yellow
*'E' ・ ・ ・ Water
Then the number Q of operations is given.
Each operation is given the center coordinates of rotation (x, y), indicating that the six blocks around the square are rotated one clockwise. (See Fig.2). At this time, even if the cell does not have a block, it is considered that an empty block exists and is rotated one clockwise. However, if any one of the specified coordinates and the six squares around it does not exist on the H × W board, the rotation is not performed.
<image>
Fig.2
Next, the process is repeated until the following processing cannot be performed.
1. In Fig.3, when there is no block in any of the squares B, C, and D from the position of block A, block A falls to the position of C. If the squares B and D do not exist, it is considered that the block does not exist, and if the square C does not exist, the fall process is not performed.
2. If there is a block that can process 1, it returns to 1.
3. If three or more blocks of the same color are connected, all the blocks disappear. Connecting two blocks means sharing one side of the square.
Note: This series of processing is performed even when no operation is given (initial state).
<image>
Fig.3
Output the final board after performing all operations.
Constraints
* 3 ≤ H ≤ 50
* 3 ≤ W ≤ 50
* 0 ≤ x <W
* 0 ≤ y <H
* 1 ≤ Q ≤ 100
* Fi, j (0 ≤ i <W, 0 ≤ j <H) is one of'R',' G',' B',' P',' Y',' E'.
Input
The input is given in the following format.
H W
F0, H−1 F1, H−1… FW−1, H−1
F0, H-2 F1, H-2 ... FW-1, H-2
..
..
..
F0,0 F1,0… FW-1,0
Q
x0 y0
x1 y1
..
..
..
xQ−1 yQ−1
The first line is given two integers H and W that represent the vertical and horizontal sizes of the board. From the second line to the H + 1 line, a character string representing the color of the board corresponding to each subscript is given. The number Q of operations is given on the second line of H +. In the following Q line, x and y representing the coordinates of the cell at the center of rotation are given.
Output
Output the board surface in line H after performing all operations. However, cells without blocks should be represented by'.'.
Examples
Input
3 3
RGR
RBP
YEB
1
1 1
Output
…
YBG
EBP
Input
4 5
BYYGG
RRRRR
RRBRR
YYGGB
2
3 1
3 1
Output
.....
.....
.....
B.BGB
Input
4 4
BEEP
ERYY
BBRP
RBYP
1
1 2
Output
....
....
....
.B..
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 went to the store, selling $n$ types of chocolates. There are $a_i$ chocolates of type $i$ in stock.
You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy $x_i$ chocolates of type $i$ (clearly, $0 \le x_i \le a_i$), then for all $1 \le j < i$ at least one of the following must hold: $x_j = 0$ (you bought zero chocolates of type $j$) $x_j < x_i$ (you bought less chocolates of type $j$ than of type $i$)
For example, the array $x = [0, 0, 1, 2, 10]$ satisfies the requirement above (assuming that all $a_i \ge x_i$), while arrays $x = [0, 1, 0]$, $x = [5, 5]$ and $x = [3, 2]$ don't.
Calculate the maximum number of chocolates you can buy.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$), denoting the number of types of chocolate.
The next line contains $n$ integers $a_i$ ($1 \le a_i \le 10^9$), denoting the number of chocolates of each type.
-----Output-----
Print the maximum number of chocolates you can buy.
-----Examples-----
Input
5
1 2 1 3 6
Output
10
Input
5
3 2 5 4 10
Output
20
Input
4
1 1 1 1
Output
1
-----Note-----
In the first example, it is optimal to buy: $0 + 0 + 1 + 3 + 6$ chocolates.
In the second example, it is optimal to buy: $1 + 2 + 3 + 4 + 10$ chocolates.
In the third example, it is optimal to buy: $0 + 0 + 0 + 1$ chocolates.
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$:
$[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences.
Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if:
it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example:
$[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$.
You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$.
For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ — it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$.
-----Input-----
The first line contains an integer $n$ ($1 \le n \le 100$) — the length of the sequence $a$.
The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$).
The third line contains an integer $m$ ($1 \le m \le 100$) — the number of requests.
The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) — the requests.
-----Output-----
Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$.
-----Examples-----
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
-----Note-----
In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first.
There are $n$ books standing in a row on the shelf, the $i$-th book has color $a_i$.
You'd like to rearrange the books to make the shelf look beautiful. The shelf is considered beautiful if all books of the same color are next to each other.
In one operation you can take one book from any position on the shelf and move it to the right end of the shelf.
What is the minimum number of operations you need to make the shelf beautiful?
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 5 \cdot 10^5$) — the number of books.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the book colors.
-----Output-----
Output the minimum number of operations to make the shelf beautiful.
-----Examples-----
Input
5
1 2 2 1 3
Output
2
Input
5
1 2 2 1 1
Output
1
-----Note-----
In the first example, we have the bookshelf $[1, 2, 2, 1, 3]$ and can, for example:
take a book on position $4$ and move to the right end: we'll get $[1, 2, 2, 3, 1]$;
take a book on position $1$ and move to the right end: we'll get $[2, 2, 3, 1, 1]$.
In the second example, we can move the first book to the end of the bookshelf and get $[2,2,1,1,1]$.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer).
There are t seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than t seconds. Note, that he can choose to not use all t seconds. Moreover, he can even choose to not round the grade at all.
In this problem, classic rounding rules are used: while rounding number to the n-th digit one has to take a look at the digit n + 1. If it is less than 5 than the n-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the n + 1 digit is greater or equal to 5, the digit at the position n is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away.
For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3.
-----Input-----
The first line of the input contains two integers n and t (1 ≤ n ≤ 200 000, 1 ≤ t ≤ 10^9) — the length of Efim's grade and the number of seconds till the end of the break respectively.
The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0.
-----Output-----
Print the maximum grade that Efim can get in t seconds. Do not print trailing zeroes.
-----Examples-----
Input
6 1
10.245
Output
10.25
Input
6 2
10.245
Output
10.3
Input
3 100
9.2
Output
9.2
-----Note-----
In the first two samples Efim initially has grade 10.245.
During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect.
In the third sample the optimal strategy is to not perform any rounding at all.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
KISS stands for Keep It Simple Stupid.
It is a design principle for keeping things simple rather than complex.
You are the boss of Joe.
Joe is submitting words to you to publish to a blog. He likes to complicate things.
Define a function that determines if Joe's work is simple or complex.
Input will be non emtpy strings with no punctuation.
It is simple if:
``` the length of each word does not exceed the amount of words in the string ```
(See example test cases)
Otherwise it is complex.
If complex:
```python
return "Keep It Simple Stupid"
```
or if it was kept simple:
```python
return "Good work Joe!"
```
Note: Random test are random and nonsensical. Here is a silly example of a random test:
```python
"jump always mostly is touchy dancing choice is pineapples mostly"
```
Write your solution by modifying this code:
```python
def is_kiss(words):
```
Your solution should implemented in the function "is_kiss". The i
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 an array with some numbers. All numbers are equal except for one. Try to find it!
```python
find_uniq([ 1, 1, 1, 2, 1, 1 ]) == 2
find_uniq([ 0, 0, 0.55, 0, 0 ]) == 0.55
```
It’s guaranteed that array contains at least 3 numbers.
The tests contain some very huge arrays, so think about performance.
This is the first kata in series:
1. Find the unique number (this kata)
2. [Find the unique string](https://www.codewars.com/kata/585d8c8a28bc7403ea0000c3)
3. [Find The Unique](https://www.codewars.com/kata/5862e0db4f7ab47bed0000e5)
Write your solution by modifying this code:
```python
def find_uniq(arr):
```
Your solution should implemented in the function "find_uniq". The i
Read the inputs from stdin solve the problem and write the answer to stdout (do 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$ uppercase Latin letters 'A' and $b$ letters 'B'.
The period of the string is the smallest such positive integer $k$ that $s_i = s_{i~mod~k}$ ($0$-indexed) for each $i$. Note that this implies that $k$ won't always divide $a+b = |s|$.
For example, the period of string "ABAABAA" is $3$, the period of "AAAA" is $1$, and the period of "AABBB" is $5$.
Find the number of different periods over all possible strings with $a$ letters 'A' and $b$ letters 'B'.
-----Input-----
The first line contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$) — the number of letters 'A' and 'B', respectively.
-----Output-----
Print the number of different periods over all possible strings with $a$ letters 'A' and $b$ letters 'B'.
-----Examples-----
Input
2 4
Output
4
Input
5 3
Output
5
-----Note-----
All the possible periods for the first example: $3$ "BBABBA" $4$ "BBAABB" $5$ "BBBAAB" $6$ "AABBBB"
All the possible periods for the second example: $3$ "BAABAABA" $5$ "BAABABAA" $6$ "BABAAABA" $7$ "BAABAAAB" $8$ "AAAAABBB"
Note that these are not the only possible strings for the given periods.
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 has recently found an ancient, broken computer. The machine has only one register, which allows one to put in there one variable. Then in one operation, you can shift its bits left or right by at most three positions. The right shift is forbidden if it cuts off some ones. So, in fact, in one operation, you can multiply or divide your number by $2$, $4$ or $8$, and division is only allowed if the number is divisible by the chosen divisor.
Formally, if the register contains a positive integer $x$, in one operation it can be replaced by one of the following: $x \cdot 2$ $x \cdot 4$ $x \cdot 8$ $x / 2$, if $x$ is divisible by $2$ $x / 4$, if $x$ is divisible by $4$ $x / 8$, if $x$ is divisible by $8$
For example, if $x = 6$, in one operation it can be replaced by $12$, $24$, $48$ or $3$. Value $6$ isn't divisible by $4$ or $8$, so there're only four variants of replacement.
Now Johnny wonders how many operations he needs to perform if he puts $a$ in the register and wants to get $b$ at the end.
-----Input-----
The input consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases. The following $t$ lines contain a description of test cases.
The first and only line in each test case contains integers $a$ and $b$ ($1 \leq a, b \leq 10^{18}$) — the initial and target value of the variable, respectively.
-----Output-----
Output $t$ lines, each line should contain one integer denoting the minimum number of operations Johnny needs to perform. If Johnny cannot get $b$ at the end, then write $-1$.
-----Example-----
Input
10
10 5
11 44
17 21
1 1
96 3
2 128
1001 1100611139403776
1000000000000000000 1000000000000000000
7 1
10 8
Output
1
1
-1
0
2
2
14
0
-1
-1
-----Note-----
In the first test case, Johnny can reach $5$ from $10$ by using the shift to the right by one (i.e. divide by $2$).
In the second test case, Johnny can reach $44$ from $11$ by using the shift to the left by two (i.e. multiply by $4$).
In the third test case, it is impossible for Johnny to reach $21$ from $17$.
In the fourth test case, initial and target values are equal, so Johnny has to do $0$ operations.
In the fifth test case, Johnny can reach $3$ from $96$ by using two shifts to the right: one by $2$, and another by $3$ (i.e. divide by $4$ and by $8$).
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he's got to defeat all n dragons that live on this level. Kirito and the dragons have strength, which is represented by an integer. In the duel between two opponents the duel's outcome is determined by their strength. Initially, Kirito's strength equals s.
If Kirito starts duelling with the i-th (1 ≤ i ≤ n) dragon and Kirito's strength is not greater than the dragon's strength xi, then Kirito loses the duel and dies. But if Kirito's strength is greater than the dragon's strength, then he defeats the dragon and gets a bonus strength increase by yi.
Kirito can fight the dragons in any order. Determine whether he can move on to the next level of the game, that is, defeat all dragons without a single loss.
Input
The first line contains two space-separated integers s and n (1 ≤ s ≤ 104, 1 ≤ n ≤ 103). Then n lines follow: the i-th line contains space-separated integers xi and yi (1 ≤ xi ≤ 104, 0 ≤ yi ≤ 104) — the i-th dragon's strength and the bonus for defeating it.
Output
On a single line print "YES" (without the quotes), if Kirito can move on to the next level and print "NO" (without the quotes), if he can't.
Examples
Input
2 2
1 99
100 0
Output
YES
Input
10 1
100 100
Output
NO
Note
In the first sample Kirito's strength initially equals 2. As the first dragon's strength is less than 2, Kirito can fight it and defeat it. After that he gets the bonus and his strength increases to 2 + 99 = 101. Now he can defeat the second dragon and move on to the next level.
In the second sample Kirito's strength is too small to defeat the only dragon and win.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
You are given an array $a_1, a_2, \dots, a_n$ consisting of $n$ distinct integers. Count the number of pairs of indices $(i, j)$ such that $i < j$ and $a_i \cdot a_j = i + j$.
-----Input-----
The first line contains one integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. Then $t$ cases follow.
The first line of each test case contains one integer $n$ ($2 \leq n \leq 10^5$) — the length of array $a$.
The second line of each test case contains $n$ space separated integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 2 \cdot n$) — the array $a$. It is guaranteed that all elements are distinct.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, output the number of pairs of indices $(i, j)$ such that $i < j$ and $a_i \cdot a_j = i + j$.
-----Examples-----
Input
3
2
3 1
3
6 1 5
5
3 1 5 9 2
Output
1
1
3
-----Note-----
For the first test case, the only pair that satisfies the constraints is $(1, 2)$, as $a_1 \cdot a_2 = 1 + 2 = 3$
For the second test case, the only pair that satisfies the constraints is $(2, 3)$.
For the third test case, the pairs that satisfy the constraints are $(1, 2)$, $(1, 5)$, and $(2, 3)$.
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Our bear's forest has a checkered field. The checkered field is an n × n table, the rows are numbered from 1 to n from top to bottom, the columns are numbered from 1 to n from left to right. Let's denote a cell of the field on the intersection of row x and column y by record (x, y). Each cell of the field contains growing raspberry, at that, the cell (x, y) of the field contains x + y raspberry bushes.
The bear came out to walk across the field. At the beginning of the walk his speed is (dx, dy). Then the bear spends exactly t seconds on the field. Each second the following takes place: Let's suppose that at the current moment the bear is in cell (x, y). First the bear eats the raspberry from all the bushes he has in the current cell. After the bear eats the raspberry from k bushes, he increases each component of his speed by k. In other words, if before eating the k bushes of raspberry his speed was (dx, dy), then after eating the berry his speed equals (dx + k, dy + k). Let's denote the current speed of the bear (dx, dy) (it was increased after the previous step). Then the bear moves from cell (x, y) to cell (((x + dx - 1) mod n) + 1, ((y + dy - 1) mod n) + 1). Then one additional raspberry bush grows in each cell of the field.
You task is to predict the bear's actions. Find the cell he ends up in if he starts from cell (sx, sy). Assume that each bush has infinitely much raspberry and the bear will never eat all of it.
-----Input-----
The first line of the input contains six space-separated integers: n, sx, sy, dx, dy, t (1 ≤ n ≤ 10^9; 1 ≤ sx, sy ≤ n; - 100 ≤ dx, dy ≤ 100; 0 ≤ t ≤ 10^18).
-----Output-----
Print two integers — the coordinates of the cell the bear will end up in after t seconds.
-----Examples-----
Input
5 1 2 0 1 2
Output
3 1
Input
1 1 1 -1 -1 2
Output
1 1
-----Note-----
Operation a mod b means taking the remainder after dividing a by b. Note that the result of the operation is always non-negative. For example, ( - 1) mod 3 = 2.
In the first sample before the first move the speed vector will equal (3,4) and the bear will get to cell (4,1). Before the second move the speed vector will equal (9,10) and he bear will get to cell (3,1). Don't forget that at the second move, the number of berry bushes increased by 1.
In the second sample before the first move the speed vector will equal (1,1) and the bear will get to cell (1,1). Before the second move, the speed vector will equal (4,4) and the bear will get to cell (1,1). Don't forget that at the second move, the number of berry bushes increased by 1.
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Dormi received a histogram with $n$ bars of height $a_1, a_2, \ldots, a_n$ for Christmas. However, the more he played with his new histogram, the more he realized its imperfections, so today he wanted to modify it to his liking.
To modify the histogram, Little Dormi is able to perform the following operation an arbitrary number of times:
Select an index $i$ ($1 \le i \le n$) where $a_i>0$, and assign $a_i := a_i-1$.
Little Dormi defines the ugliness score of his histogram (after performing some number of operations) as the sum of the vertical length of its outline and the number of operations he performed on it. And to make the histogram as perfect as possible, he would like to minimize the ugliness score after modifying it with some number of operations.
However, as his histogram is very large, Little Dormi is having trouble minimizing the ugliness score, so as Little Dormi's older brother, help him find the minimal ugliness.
Consider the following example where the histogram has $4$ columns of heights $4,8,9,6$:
The blue region represents the histogram, and the red lines represent the vertical portion of the outline. Currently, the vertical length of the outline is $4+4+1+3+6 = 18$, so if Little Dormi does not modify the histogram at all, the ugliness would be $18$.
However, Little Dormi can apply the operation once on column $2$ and twice on column $3$, resulting in a histogram with heights $4,7,7,6$:
Now, as the total vertical length of the outline (red lines) is $4+3+1+6=14$, the ugliness is $14+3=17$ dollars. It can be proven that this is optimal.
-----Input-----
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n \le 4 \cdot 10^5$).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $4 \cdot 10^5$.
-----Output-----
For each test case output one integer, the minimal ugliness Little Dormi can achieve with the histogram in that test case.
-----Examples-----
Input
2
4
4 8 9 6
6
2 1 7 4 0 0
Output
17
12
-----Note-----
Example $1$ is the example described in the statement.
The initial histogram for example $2$ is given below:
The ugliness is currently $2+1+6+3+4=16$.
By applying the operation once on column $1$, six times on column $3$, and three times on column $4$, we can end up with a histogram with heights $1,1,1,1,0,0$:
The vertical length of the outline is now $1+1=2$ and Little Dormi made $1+6+3=10$ operations, so the final ugliness is $2+10=12$, which can be proven to be optimal.
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.
Let's try to make a new task. Firstly we will use the following task. There are n people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are n people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation p?
More formally, if we denote the handle of the i-th person as h_{i}, then the following condition must hold: $\forall i, j(i < j) : h_{p_{i}} < h_{p_{j}}$.
-----Input-----
The first line contains an integer n (1 ≤ n ≤ 10^5) — the number of people.
The next n lines each contains two strings. The i-th line contains strings f_{i} and s_{i} (1 ≤ |f_{i}|, |s_{i}| ≤ 50) — the first name and last name of the i-th person. Each string consists only of lowercase English letters. All of the given 2n strings will be distinct.
The next line contains n distinct integers: p_1, p_2, ..., p_{n} (1 ≤ p_{i} ≤ n).
-----Output-----
If it is possible, output "YES", otherwise output "NO".
-----Examples-----
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
1 2 3
Output
NO
Input
3
gennady korotkevich
petr mitrichev
gaoyuan chen
3 1 2
Output
YES
Input
2
galileo galilei
nicolaus copernicus
2 1
Output
YES
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
1 2 3 4 5 6 7 8 9 10
Output
NO
Input
10
rean schwarzer
fei claussell
alisa reinford
eliot craig
laura arseid
jusis albarea
machias regnitz
sara valestin
emma millstein
gaius worzel
2 4 9 6 5 7 1 3 8 10
Output
YES
-----Note-----
In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.
In example 3, if Copernicus uses "copernicus" as his handle, everything will be alright.
Read the inputs from stdin solve the problem and write the answer to stdout (do 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
I chose creature observation as the theme of my free study during the summer vacation and purchased a creature observation kit.
This creature prefers to live in a three-dimensional grid-like space. Only one animal can be placed in each cell. It repeats birth and death every day according to the surrounding environment. The conditions of birth and death depend on the number of creatures adjacent to the cell. Here, when a living thing is adjacent to a certain cell, it means that one cell and another cell inhabiting the living thing share a face, an edge, or a point. The rules of birth and death are as follows.
* In a cell where no creatures live, if there is an i such that the number of adjacent creatures is ai (1 ≤ i ≤ M1), a creature is born in that cell.
* In a cell inhabited by a creature, if there is no j such that the number of adjacent creatures is bj (1 ≤ j ≤ M2), the creature in that cell will die.
The breeding box I bought this time is a cubic breeding box with 5 * 5 * 5 cells. How does this creature behave in this breeding box ...? I'm really looking forward to it.
~A few days later~
For the time being, I tried breeding it ... It takes time to observe it every day, and I'm annoyed and unmotivated. Yes, let's simulate using a computer and a program.
Constraints
The input satisfies the following conditions.
* 1 ≤ N ≤ 100
* 0 ≤ M1, M2 ≤ 27
* 0 ≤ ai, bj ≤ 26 (1 ≤ i ≤ M1, 1 ≤ j ≤ M2)
* For any i, j (1 ≤ i <j ≤ M1), ai ≠ aj
* For any i, j (1 ≤ i <j ≤ M2), bi ≠ bj
Input
The input consists of multiple datasets. Each dataset is given in the following format.
N
(State of breeding box with z = 0)
(Blank line)
(State of breeding box with z = 1)
(Blank line)
(State of breeding box with z = 2)
(Blank line)
(State of breeding box with z = 3)
(Blank line)
(State of breeding box with z = 4)
(Blank line)
M1 a1 a2… aM1
M2 b1 b2… bM2
Given the number of days N to simulate first. Next, information on the initial state of the breeding box is given. This is given by 5 5 * 5 2D grids. Each 2D grid consists of 0s and 1s, where 1 indicates that there are living things and 0 indicates that there is nothing. For example, if the value in the 4th row and 2nd column of the 2D grid in the cell state of z = 0 is 1, it means that there is a creature at the position of the coordinates (1, 3, 0) of the breeding box.
Then the integer M1 is given, followed by the M1 number ai.
Then the integer M2 is given, followed by the M2 number bj.
The end of the input is represented by N = 0.
Output
For each dataset, output the state after N days.
The output follows the following format.
Case (test case number):
(State of breeding box with z = 0)
(Blank line)
(State of breeding box with z = 1)
(Blank line)
(State of breeding box with z = 2)
(Blank line)
(State of breeding box with z = 3)
(Blank line)
(State of breeding box with z = 4)
Output the test case number on the first line, and output 5 5 * 5 2D grids as the state after N days have passed from the next line. Print a blank line between the outputs of each test case.
Example
Input
5
00000
01010
00000
00100
00000
00000
01010
00000
01010
00000
00000
00100
00000
01010
00000
00000
01010
00000
00100
00000
00000
00000
00100
00000
00000
1 2
2 3 4
4
01110
00100
00100
00100
01110
01110
10001
10000
10001
01110
11111
10001
11111
10000
10000
01110
10001
10000
10001
01110
00000
00000
00000
00000
00000
2 3 4
1 2
100
00100
01010
01110
10001
10001
01110
00100
00100
00100
01110
00000
00000
00000
00000
00000
11111
00010
00100
01000
11111
10001
10001
10001
10001
01110
5 1 3 5 7 9
5 0 2 4 6 8
0
Output
Case 1:
00000
00000
01010
01110
10101
00000
10001
00000
00000
00000
00000
00000
11111
10001
00000
00000
00000
00000
00000
01010
00000
00000
01110
00100
11111
Case 2:
00010
10110
00000
11000
10000
00001
00000
10101
00010
00100
10001
00000
00000
00000
00010
01110
10001
00000
00001
10000
00000
00000
00111
01001
01000
Case 3:
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
00000
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
wHAT DO WE NEED cAPS LOCK FOR?
Caps lock is a computer keyboard key. Pressing it sets an input mode in which typed letters are capital by default. If it is pressed by accident, it leads to accidents like the one we had in the first passage.
Let's consider that a word has been typed with the Caps lock key accidentally switched on, if:
* either it only contains uppercase letters;
* or all letters except for the first one are uppercase.
In this case we should automatically change the case of all letters. For example, the case of the letters that form words "hELLO", "HTTP", "z" should be changed.
Write a program that applies the rule mentioned above. If the rule cannot be applied, the program should leave the word unchanged.
Input
The first line of the input data contains a word consisting of uppercase and lowercase Latin letters. The word's length is from 1 to 100 characters, inclusive.
Output
Print the result of the given word's processing.
Examples
Input
cAPS
Output
Caps
Input
Lock
Output
Lock
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 function getMean that takes as parameters an array (arr) and 2 integers (x and y). The function should return the mean between the mean of the the first x elements of the array and the mean of the last y elements of the array.
The mean should be computed if both x and y have values higher than 1 but less or equal to the array's length. Otherwise the function should return -1.
getMean([1,3,2,4], 2, 3) should return 2.5 because: the mean of the the first 2 elements of the array is (1+3)/2=2 and the mean of the last 3 elements of the array is (4+2+3)/3=3 so the mean of those 2 means is (2+3)/2=2.5.
getMean([1,3,2,4], 1, 2) should return -1 because x is not higher than 1.
getMean([1,3,2,4], 2, 8) should return -1 because 8 is higher than the array's length.
Write your solution by modifying this code:
```python
def get_mean(arr,x,y):
```
Your solution should implemented in the function "get_mean". The i
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 and Aoki will have a battle using their monsters.
The health and strength of Takahashi's monster are A and B, respectively, and those of Aoki's monster are C and D, respectively.
The two monsters will take turns attacking, in the order Takahashi's, Aoki's, Takahashi's, Aoki's, ...
Here, an attack decreases the opponent's health by the value equal to the attacker's strength.
The monsters keep attacking until the health of one monster becomes 0 or below. The person with the monster whose health becomes 0 or below loses, and the other person wins.
If Takahashi will win, print Yes; if he will lose, print No.
-----Constraints-----
- 1 \leq A,B,C,D \leq 100
- All values in input are integers.
-----Input-----
Input is given from Standard Input in the following format:
A B C D
-----Output-----
If Takahashi will win, print Yes; if he will lose, print No.
-----Sample Input-----
10 9 10 10
-----Sample Output-----
No
First, Takahashi's monster attacks Aoki's monster, whose health is now 10-9=1.
Next, Aoki's monster attacks Takahashi's monster, whose health is now 10-10=0.
Takahashi's monster is the first to have 0 or less health, so Takahashi loses.
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 stowaway and a controller play the following game.
The train is represented by n wagons which are numbered with positive integers from 1 to n from the head to the tail. The stowaway and the controller are initially in some two different wagons. Every minute the train can be in one of two conditions — moving or idle. Every minute the players move.
The controller's move is as follows. The controller has the movement direction — to the train's head or to its tail. During a move the controller moves to the neighbouring wagon correspondingly to its movement direction. If at the end of his move the controller enters the 1-st or the n-th wagon, that he changes the direction of his movement into the other one. In other words, the controller cyclically goes from the train's head to its tail and back again during all the time of a game, shifting during each move by one wagon. Note, that the controller always have exactly one possible move.
The stowaway's move depends from the state of the train. If the train is moving, then the stowaway can shift to one of neighbouring wagons or he can stay where he is without moving. If the train is at a station and is idle, then the stowaway leaves the train (i.e. he is now not present in any train wagon) and then, if it is not the terminal train station, he enters the train again into any of n wagons (not necessarily into the one he's just left and not necessarily into the neighbouring one). If the train is idle for several minutes then each such minute the stowaway leaves the train and enters it back.
Let's determine the order of the players' moves. If at the given minute the train is moving, then first the stowaway moves and then the controller does. If at this minute the train is idle, then first the stowaway leaves the train, then the controller moves and then the stowaway enters the train.
If at some point in time the stowaway and the controller happen to be in one wagon, then the controller wins: he makes the stowaway pay fine. If after a while the stowaway reaches the terminal train station, then the stowaway wins: he simply leaves the station during his move and never returns there again.
At any moment of time the players know each other's positions. The players play in the optimal way. Specifically, if the controller wins, then the stowaway plays so as to lose as late as possible. As all the possible moves for the controller are determined uniquely, then he is considered to play optimally always. Determine the winner.
Input
The first line contains three integers n, m and k. They represent the number of wagons in the train, the stowaway's and the controller's initial positions correspondingly (2 ≤ n ≤ 50, 1 ≤ m, k ≤ n, m ≠ k).
The second line contains the direction in which a controller moves. "to head" means that the controller moves to the train's head and "to tail" means that the controller moves to its tail. It is guaranteed that in the direction in which the controller is moving, there is at least one wagon. Wagon 1 is the head, and wagon n is the tail.
The third line has the length from 1 to 200 and consists of symbols "0" and "1". The i-th symbol contains information about the train's state at the i-th minute of time. "0" means that in this very minute the train moves and "1" means that the train in this very minute stands idle. The last symbol of the third line is always "1" — that's the terminal train station.
Output
If the stowaway wins, print "Stowaway" without quotes. Otherwise, print "Controller" again without quotes, then, separated by a space, print the number of a minute, at which the stowaway will be caught.
Examples
Input
5 3 2
to head
0001001
Output
Stowaway
Input
3 2 1
to tail
0001
Output
Controller 2
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Our fruit guy has a bag of fruit (represented as an array of strings) where some fruits are rotten. He wants to replace all the rotten pieces of fruit with fresh ones. For example, given `["apple","rottenBanana","apple"]` the replaced array should be `["apple","banana","apple"]`. Your task is to implement a method that accepts an array of strings containing fruits should returns an array of strings where all the rotten fruits are replaced by good ones.
### Notes
- If the array is null/nil/None or empty you should return empty array (`[]`).
- The rotten fruit name will be in this camelcase (`rottenFruit`).
- The returned array should be in lowercase.
Write your solution by modifying this code:
```python
def remove_rotten(bag_of_fruits):
```
Your solution should implemented in the function "remove_rotten". The i
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Reverse and invert all integer values in a given list.
Python:
reverse_invert([1,12,'a',3.4,87,99.9,-42,50,5.6]) = [-1,-21,-78,24,-5]
Ignore all other types than integer.
Write your solution by modifying this code:
```python
def reverse_invert(lst):
```
Your solution should implemented in the function "reverse_invert". The i
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style.
Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text.
For simplicity, let us assume that Volodya's text can be represented as a single string.
-----Input-----
Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 10^6 characters.
-----Output-----
Print exactly one number — the number of powerful substrings of the given string.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
-----Examples-----
Input
heavymetalisheavymetal
Output
3
Input
heavymetalismetal
Output
2
Input
trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou
Output
3
-----Note-----
In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful.
In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal".
Read the inputs from stdin solve the problem and write the answer to stdout (do 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 Number in Expanded Form
You will be given a number and you will need to return it as a string in [Expanded Form](https://www.mathplacementreview.com/arithmetic/whole-numbers.php#expanded-form). For example:
```python
expanded_form(12) # Should return '10 + 2'
expanded_form(42) # Should return '40 + 2'
expanded_form(70304) # Should return '70000 + 300 + 4'
```
NOTE: All numbers will be whole numbers greater than 0.
If you liked this kata, check out [part 2](https://www.codewars.com/kata/write-number-in-expanded-form-part-2)!!
Write your solution by modifying this code:
```python
def expanded_form(num):
```
Your solution should implemented in the function "expanded_form". The i
Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
|
Solve the programming task below in a Python markdown code block.
In recent years John has very successfully settled at his new job at the office. But John doesn't like to idly sit around while his code is compiling, so he immediately found himself an interesting distraction. The point of his distraction was to maintain a water level in the water cooler used by other zebras.
Originally the cooler contained exactly $k$ liters of water. John decided that the amount of water must always be at least $l$ liters of water but no more than $r$ liters. John will stay at the office for exactly $t$ days. He knows that each day exactly $x$ liters of water will be used by his colleagues. At the beginning of each day he can add exactly $y$ liters of water to the cooler, but at any point in time the amount of water in the cooler must be in the range $[l, r]$.
Now John wants to find out whether he will be able to maintain the water level at the necessary level for $t$ days. Help him answer this question!
-----Input-----
The first line of the input contains six integers $k$, $l$, $r$, $t$, $x$ and $y$ ($1 \le l \le k \le r \le 10^{18}; 1 \le t \le 10^{18}; 1 \le x \le 10^6; 1 \le y \le 10^{18}$) — initial water level, the required range, the number of days, daily water usage and the exact amount of water that can be added, respectively.
-----Output-----
Print "Yes" if John can maintain the water level for $t$ days and "No" otherwise.
-----Examples-----
Input
8 1 10 2 6 4
Output
No
Input
8 1 10 2 6 5
Output
Yes
Input
9 1 10 9 2 9
Output
No
Input
20 15 25 3 5 7
Output
Yes
-----Note-----
In the first example, John can't increase the amount of water at the beginning of the first day, since it would exceed the limit $r$. That is why after the first day the cooler will contain $2$ liters of water. The next day John adds $4$ liters to the cooler but loses $6$ liters, leaving John with $0$ liters, which is outside the range $[1, 10]$.
In the second example, after the first day John is left with $2$ liters of water. At the beginning of the next day he adds $5$ liters, then $6$ liters get used, leaving John with $1$ liter of water which is in range $[1, 10]$.
In the third example, after the first day John is left with $7$ liters, after the second day — $5$ liters, after the fourth — $1$ liter. At the beginning of the fifth day John will add $9$ liters and lose $2$ liters. Meaning, after the fifth day he will have $8$ liters left. Then each day the water level will decrease by $2$ liters and after the eighth day John will have $2$ liters and after the ninth day — $0$ liters. $0$ is outside range $[1, 10]$, so the answer is "No".
In the fourth example, after the first day John is left with $15$ liters of water. At the beginning of the second day he adds $7$ liters and loses $5$, so after the second day he is left with $17$ liters. At the beginning of the third day he adds $7$ more liters of water and loses $5$, so after the third day he is left with $19$ liters. $19$ is in range $[15, 25]$ so the answer is "Yes".
Read the inputs from 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.