Problem ID
stringlengths 2
6
| Problem Description
stringlengths 0
7.52k
| Rating
float64 800
3.5k
| math
bool 2
classes | greedy
bool 2
classes | implementation
bool 2
classes | dp
bool 2
classes | data structures
bool 2
classes | constructive algorithms
bool 2
classes | brute force
bool 2
classes | binary search
bool 2
classes | sortings
bool 2
classes | graphs
bool 2
classes | __index_level_0__
int64 3
9.98k
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1211C | Summer in Berland lasts $$$n$$$ days, the price of one portion of ice cream on the $$$i$$$-th day is $$$c_i$$$. Over the summer, Tanya wants to eat exactly $$$k$$$ portions of ice cream. At the same time, on the $$$i$$$-th day, she decided that she would eat at least $$$a_i$$$ portions, but not more than $$$b_i$$$ ($$$a_i le b_i$$$) portions. In other words, let $$$d_i$$$ be equal to the number of portions that she eats on the $$$i$$$-th day. Then $$$d_1+d_2+dots+d_n=k$$$ and $$$a_i le d_i le b_i$$$ for each $$$i$$$. Given that portions of ice cream can only be eaten on the day of purchase, find the minimum amount of money that Tanya can spend on ice cream in the summer. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2cdot10^5$$$, $$$0 le k le 10^9$$$) — the number of days and the total number of servings of ice cream that Tanya will eat in the summer. The following $$$n$$$ lines contain descriptions of the days, one description per line. Each description consists of three integers $$$a_i, b_i, c_i$$$ ($$$0 le a_i le b_i le 10^9$$$, $$$1 le c_i le 10^6$$$). Output Print the minimum amount of money that Tanya can spend on ice cream in the summer. If there is no way for Tanya to buy and satisfy all the requirements, then print -1. Examples Input 3 7 3 5 6 0 3 4 3 3 3 Input 1 45000 40000 50000 100000 Input 3 100 2 10 50 50 60 16 20 21 25 Input 4 12 2 5 1 1 2 2 2 3 7 3 10 4 Note In the first example, Tanya needs to eat $$$3$$$ portions of ice cream on the first day, $$$1$$$ portions of ice cream on the second day and $$$3$$$ portions of ice cream on the third day. In this case, the amount of money spent is $$$3cdot6+1cdot4+3cdot3=31$$$. It can be shown that any other valid way to eat exactly $$$7$$$ portions of ice cream costs more. | 1,700 | false | true | false | false | false | false | false | false | true | false | 4,626 |
2014G | What is done is done, and the spoilt milk cannot be helped. Little John is as little as night is day — he was known to be a giant, at possibly $$$2.1$$$ metres tall. It has everything to do with his love for milk. His dairy diary has $$$n$$$ entries, showing that he acquired $$$a_i$$$ pints of fresh milk on day $$$d_i$$$. Milk declines in freshness with time and stays drinkable for a maximum of $$$k$$$ days. In other words, fresh milk acquired on day $$$d_i$$$ will be drinkable between days $$$d_i$$$ and $$$d_i+k-1$$$ inclusive. Every day, Little John drinks drinkable milk, up to a maximum of $$$m$$$ pints. In other words, if there are less than $$$m$$$ pints of milk, he will drink them all and not be satisfied; if there are at least $$$m$$$ pints of milk, he will drink exactly $$$m$$$ pints and be satisfied, and it's a milk satisfaction day. Little John always drinks the freshest drinkable milk first. Determine the number of milk satisfaction days for Little John. Input The first line of the input contains a single integer $$$t$$$ ($$$1leq t leq 10^4$$$), the number of test cases. The first line of each test case consists of three integers $$$n$$$, $$$m$$$, $$$k$$$ ($$$1le n$$$, $$$m$$$, $$$k le 10^5$$$), the number of diary entries, the maximum pints needed for a milk satisfaction day, and the duration of milk's freshness. Then follow $$$n$$$ lines of each test case, each with two integers $$$d_i$$$ and $$$a_i$$$ ($$$1le d_i$$$, $$$a_i le 10^6$$$), the day on which the milk was acquired and the number of pints acquired. They are sorted in increasing values of $$$d_i$$$, and all values of $$$d_i$$$ 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 a single integer, the number of milk satisfaction days. Example Input 6 1 1 3 1 5 2 3 3 1 5 2 7 4 5 2 1 9 2 6 4 9 5 6 5 2 4 4 7 5 3 7 1 11 2 12 1 4 1 3 5 10 9 4 14 8 15 3 5 5 5 8 9 10 7 16 10 21 5 28 9 Note In the first test case, $$$5$$$ pints of milk are good for $$$3$$$ days before spoiling. In the second test case, the following will happen: On day $$$1$$$, he will receive $$$5$$$ pints of milk and drink $$$3$$$ of them (leaving $$$2$$$ pints from day $$$1$$$); On day $$$2$$$, he will receive $$$7$$$ pints of milk and drink $$$3$$$ of them (leaving $$$2$$$ pints from day $$$1$$$ and $$$4$$$ pints from day $$$2$$$); On day $$$3$$$, he will drink $$$3$$$ pints from day $$$2$$$ (leaving $$$2$$$ pints from day $$$1$$$ and $$$1$$$ pint from day $$$2$$$); On day $$$4$$$, the milk acquired on day $$$1$$$ will spoil, and he will drink $$$1$$$ pint from day $$$2$$$ (no more milk is left). | 2,200 | false | true | true | false | true | false | true | false | false | false | 165 |
893A | Alex, Bob and Carl will soon participate in a team chess tournament. Since they are all in the same team, they have decided to practise really hard before the tournament. But it's a bit difficult for them because chess is a game for two players, not three. So they play with each other according to following rules: Alex and Bob play the first game, and Carl is spectating; When the game ends, the one who lost the game becomes the spectator in the next game, and the one who was spectating plays against the winner. Alex, Bob and Carl play in such a way that there are no draws. Today they have played _n_ games, and for each of these games they remember who was the winner. They decided to make up a log of games describing who won each game. But now they doubt if the information in the log is correct, and they want to know if the situation described in the log they made up was possible (that is, no game is won by someone who is spectating if Alex, Bob and Carl play according to the rules). Help them to check it! Input The first line contains one integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of games Alex, Bob and Carl played. Then _n_ lines follow, describing the game log. _i_-th line contains one integer _a__i_ (1u2009≤u2009_a__i_u2009≤u20093) which is equal to 1 if Alex won _i_-th game, to 2 if Bob won _i_-th game and 3 if Carl won _i_-th game. Output Print YES if the situation described in the log was possible. Otherwise print NO. Note In the first example the possible situation is: 1. Alex wins, Carl starts playing instead of Bob; 2. Alex wins, Bob replaces Carl; 3. Bob wins. The situation in the second example is impossible because Bob loses the first game, so he cannot win the second one. | 900 | false | false | true | false | false | false | false | false | false | false | 6,181 |
591A | Harry Potter and He-Who-Must-Not-Be-Named engaged in a fight to the death once again. This time they are located at opposite ends of the corridor of length _l_. Two opponents simultaneously charge a deadly spell in the enemy. We know that the impulse of Harry's magic spell flies at a speed of _p_ meters per second, and the impulse of You-Know-Who's magic spell flies at a speed of _q_ meters per second. The impulses are moving through the corridor toward each other, and at the time of the collision they turn round and fly back to those who cast them without changing their original speeds. Then, as soon as the impulse gets back to it's caster, the wizard reflects it and sends again towards the enemy, without changing the original speed of the impulse. Since Harry has perfectly mastered the basics of magic, he knows that after the second collision both impulses will disappear, and a powerful explosion will occur exactly in the place of their collision. However, the young wizard isn't good at math, so he asks you to calculate the distance from his position to the place of the second meeting of the spell impulses, provided that the opponents do not change positions during the whole fight. Input The first line of the input contains a single integer _l_ (1u2009≤u2009_l_u2009≤u20091u2009000)xa0— the length of the corridor where the fight takes place. The second line contains integer _p_, the third line contains integer _q_ (1u2009≤u2009_p_,u2009_q_u2009≤u2009500)xa0— the speeds of magical impulses for Harry Potter and He-Who-Must-Not-Be-Named, respectively. Output Print a single real numberxa0— the distance from the end of the corridor, where Harry is located, to the place of the second meeting of the spell impulses. Your answer will be considered correct if its absolute or relative error will not exceed 10u2009-u20094. Namely: let's assume that your answer equals _a_, and the answer of the jury is _b_. The checker program will consider your answer correct if . Note In the first sample the speeds of the impulses are equal, so both of their meetings occur exactly in the middle of the corridor. | 900 | true | false | true | false | false | false | false | false | false | false | 7,484 |
270A | Problem - 270A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags geometry implementation math *1100 No tag edit access → Contest materials — the number of tests. Each of the following _t_ lines contains a single integer _a_ (0u2009<u2009_a_u2009<u2009180) — the angle the robot can make corners at measured in degrees. Output For each test, output on a single line "YES" (without quotes), if the robot can build a fence Emuskald wants, and "NO" (without quotes), if it is impossible. Examples Input 3 30 60 90 Output NO YES YES Note In the first test case, it is impossible to build the fence, since there is no regular polygon with angle . In the second test case, the fence is a regular triangle, and in the last test case — a square. | 1,100 | true | false | true | false | false | false | false | false | false | false | 8,753 |
1322C | Wu got hungry after an intense training session, and came to a nearby store to buy his favourite instant noodles. After Wu paid for his purchase, the cashier gave him an interesting task. You are given a bipartite graph with positive integers in all vertices of the right half. For a subset $$$S$$$ of vertices of the left half we define $$$N(S)$$$ as the set of all vertices of the right half adjacent to at least one vertex in $$$S$$$, and $$$f(S)$$$ as the sum of all numbers in vertices of $$$N(S)$$$. Find the greatest common divisor of $$$f(S)$$$ for all possible non-empty subsets $$$S$$$ (assume that GCD of empty set is $$$0$$$). Wu is too tired after his training to solve this problem. Help him! Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 500,000$$$)xa0— the number of test cases in the given test set. Test case descriptions follow. The first line of each case description contains two integers $$$n$$$ and $$$m$$$ ($$$1~leq~n,~m~leq~500,000$$$)xa0— the number of vertices in either half of the graph, and the number of edges respectively. The second line contains $$$n$$$ integers $$$c_i$$$ ($$$1 leq c_i leq 10^{12}$$$). The $$$i$$$-th number describes the integer in the vertex $$$i$$$ of the right half of the graph. Each of the following $$$m$$$ lines contains a pair of integers $$$u_i$$$ and $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$), describing an edge between the vertex $$$u_i$$$ of the left half and the vertex $$$v_i$$$ of the right half. It is guaranteed that the graph does not contain multiple edges. Test case descriptions are separated with empty lines. The total value of $$$n$$$ across all test cases does not exceed $$$500,000$$$, and the total value of $$$m$$$ across all test cases does not exceed $$$500,000$$$ as well. Output For each test case print a single integerxa0— the required greatest common divisor. Example Input 3 2 4 1 1 1 1 1 2 2 1 2 2 3 4 1 1 1 1 1 1 2 2 2 2 3 4 7 36 31 96 29 1 2 1 3 1 4 2 2 2 4 3 1 4 3 Note The greatest common divisor of a set of integers is the largest integer $$$g$$$ such that all elements of the set are divisible by $$$g$$$. In the first sample case vertices of the left half and vertices of the right half are pairwise connected, and $$$f(S)$$$ for any non-empty subset is $$$2$$$, thus the greatest common divisor of these values if also equal to $$$2$$$. In the second sample case the subset $$${1}$$$ in the left half is connected to vertices $$${1, 2}$$$ of the right half, with the sum of numbers equal to $$$2$$$, and the subset $$${1, 2}$$$ in the left half is connected to vertices $$${1, 2, 3}$$$ of the right half, with the sum of numbers equal to $$$3$$$. Thus, $$$f({1}) = 2$$$, $$$f({1, 2}) = 3$$$, which means that the greatest common divisor of all values of $$$f(S)$$$ is $$$1$$$. | 2,300 | true | false | false | false | false | false | false | false | false | true | 4,105 |
1864B | You are given a string $$$s$$$ of length $$$n$$$ consisting of lowercase English letters, and an integer $$$k$$$. In one step you can perform any one of the two operations below: Pick an index $$$i$$$ ($$$1 le i le n - 2$$$) and swap $$$s_{i}$$$ and $$$s_{i+2}$$$. Pick an index $$$i$$$ ($$$1 le i le n-k+1$$$) and reverse the order of letters formed by the range $$$[i,i+k-1]$$$ of the string. Formally, if the string currently is equal to $$$s_1ldots s_{i-1}s_is_{i+1}ldots s_{i+k-2}s_{i+k-1}s_{i+k}ldots s_{n-1}s_n$$$, change it to $$$s_1ldots s_{i-1}s_{i+k-1}s_{i+k-2}ldots s_{i+1}s_is_{i+k}ldots s_{n-1}s_n$$$. You can make as many steps as you want (possibly, zero). Your task is to find the lexicographically smallest string you can obtain after some number of steps. A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ of the same length if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k < n le 10^5$$$). The second line of each test case contains the string $$$s$$$ of length $$$n$$$ consisting of lowercase English letters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print the lexicographically smallest string after doing some (possibly, zero) steps. Example Input 5 4 2 nima 5 3 panda 9 2 theforces 7 3 amirfar 6 4 rounds Output aimn aandp ceefhorst aafmirr dnorsu Note In the first test case, we can obtain the string "aimn" using the following operations: 1. Reverse the range $$$[3,4]$$$. The string turns into "niam". 2. Swap $$$s_1$$$ and $$$s_3$$$. The string turns into "ainm". 3. Reverse the range $$$[3,4]$$$. The string turns into "aimn". It can be proven that we cannot obtain any string lexicographically smaller than "aimn". Therefore, "aimn" is the answer. In the second test case, we can obtain the string "aandp" using the following operations: 1. Swap $$$s_3$$$ and $$$s_5$$$. The string turns into "paadn". 2. Swap $$$s_1$$$ and $$$s_3$$$. The string turns into "aapdn". 3. Swap $$$s_3$$$ and $$$s_5$$$. The string turns into "aandp". It can be proven that we cannot obtain any string lexicographically smaller than "aandp". Therefore, "aandp" is the answer. | 1,100 | false | true | false | false | false | true | false | false | true | false | 1,096 |
1480B | The great hero guards the country where Homer lives. The hero has attack power $$$A$$$ and initial health value $$$B$$$. There are $$$n$$$ monsters in front of the hero. The $$$i$$$-th monster has attack power $$$a_i$$$ and initial health value $$$b_i$$$. The hero or a monster is said to be living, if his or its health value is positive (greater than or equal to $$$1$$$); and he or it is said to be dead, if his or its health value is non-positive (less than or equal to $$$0$$$). In order to protect people in the country, the hero will fight with monsters until either the hero is dead or all the monsters are dead. In each fight, the hero can select an arbitrary living monster and fight with it. Suppose the $$$i$$$-th monster is selected, and the health values of the hero and the $$$i$$$-th monster are $$$x$$$ and $$$y$$$ before the fight, respectively. After the fight, the health values of the hero and the $$$i$$$-th monster become $$$x-a_i$$$ and $$$y-A$$$, respectively. Note that the hero can fight the same monster more than once. For the safety of the people in the country, please tell them whether the great hero can kill all the monsters (even if the great hero himself is dead after killing the last monster). Input Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 le t le 10^5$$$)xa0— the number of test cases. Description of the test cases follows. The first line of each test case contains three integers $$$A$$$ ($$$1 leq A leq 10^6$$$), $$$B$$$ ($$$1 leq B leq 10^6$$$) and $$$n$$$ ($$$1 leq n leq 10^5$$$) — the attack power of the great hero, the initial health value of the great hero, and the number of monsters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq 10^6$$$), where $$$a_i$$$ denotes the attack power of the $$$i$$$-th monster. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 leq b_i leq 10^6$$$), where $$$b_i$$$ denotes the initial health value of the $$$i$$$-th monster. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print the answer: "YES" (without quotes) if the great hero can kill all the monsters. Otherwise, print "NO" (without quotes). Example Input 5 3 17 1 2 16 10 999 3 10 20 30 100 50 30 1000 1000 4 200 300 400 500 1000 1000 1000 1000 999 999 1 1000 1000 999 999 1 1000000 999 Note In the first example: There will be $$$6$$$ fights between the hero and the only monster. After that, the monster is dead and the health value of the hero becomes $$$17 - 6 imes 2 = 5 > 0$$$. So the answer is "YES", and moreover, the hero is still living. In the second example: After all monsters are dead, the health value of the hero will become $$$709$$$, regardless of the order of all fights. So the answer is "YES". In the third example: A possible order is to fight with the $$$1$$$-st, $$$2$$$-nd, $$$3$$$-rd and $$$4$$$-th monsters. After all fights, the health value of the hero becomes $$$-400$$$. Unfortunately, the hero is dead, but all monsters are also dead. So the answer is "YES". In the fourth example: The hero becomes dead but the monster is still living with health value $$$1000 - 999 = 1$$$. So the answer is "NO". | 900 | false | true | true | false | false | false | false | false | true | false | 3,274 |
386A | Problem - 386A - Codeforces =============== xa0 . Write a program that reads prices offered by bidders and finds the winner and the price he will pay. Consider that all of the offered prices are different. Input The first line of the input contains _n_ (2u2009≤u2009_n_u2009≤u20091000) — number of bidders. The second line contains _n_ distinct integer numbers _p_1,u2009_p_2,u2009... _p__n_, separated by single spaces (1u2009≤u2009_p__i_u2009≤u200910000), where _p__i_ stands for the price offered by the _i_-th bidder. Output The single output line should contain two integers: index of the winner and the price he will pay. Indices are 1-based. Examples Input 2 5 7 Output 2 5 Input 3 10 2 8 Output 1 8 Input 6 3 8 2 9 4 14 Output 6 9 | 800 | false | false | true | false | false | false | false | false | false | false | 8,286 |
1294C | Problem - 1294C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy math number theory *1300 No tag edit access → Contest materials ") Editorial") — the number of test cases. The next $$$n$$$ lines describe test cases. The $$$i$$$-th test case is given on a new line as one integer $$$n$$$ ($$$2 le n le 10^9$$$). Output For each test case, print the answer on it. Print "NO" if it is impossible to represent $$$n$$$ as $$$a cdot b cdot c$$$ for some distinct integers $$$a, b, c$$$ such that $$$2 le a, b, c$$$. Otherwise, print "YES" and any possible such representation. Example Input 5 64 32 97 2 12345 Output YES 2 4 8 NO NO NO YES 3 5 823 | 1,300 | true | true | false | false | false | false | false | false | false | false | 4,227 |
1769B1 | Statement is not available on English language В этой версии задачи размеры копируемых файлов не превышают $$$1000$$$ байт. Вы копируете с одного сервера на другой $$$n$$$ файлов размером $$$a_1, a_2, ldots, a_n$$$ байт. Файлы копируются последовательно в заданном порядке. При копировании вы видите два прогресс-бара: первый показывает процент скопированных данных в текущем файле, а второйxa0— общий процент скопированных данных по всем $$$n$$$ файлам. Оба процента отображаются округлёнными вниз до целого числа. Значения на прогресс-барах обновляются после копирования каждого байта. Формально, после копирования байта номер $$$x$$$ из файла номер $$$i$$$ первый прогресс-бар показывает $$$lfloor frac{100 cdot x}{a_i} floor$$$ процентов, а второйxa0— $$$lfloor frac{100 cdot (a_1 + a_2 + ldots + a_{i - 1} + x)}{a_1 + a_2 + ldots + a_n} floor$$$ процентов. В самом начале копирования оба прогресс-бара показывают $$$0$$$ процентов. Найдите все такие целые числа от $$$0$$$ до $$$100$$$ включительно, что существует момент времени, в который оба прогресс-бара одновременно показывают это число. Выведите эти числа в порядке возрастания. Входные данные В первой строке задано одно целое число $$$n$$$ ($$$1 le n le 100$$$)xa0— число копируемых файлов. Во второй строке заданы $$$n$$$ целых чисел $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 1000$$$)xa0— размеры файлов в байтах в том порядке, в котором они будут копироваться. Выходные данные Выведите в возрастающем порядке все числа от $$$0$$$ до $$$100$$$ включительно такие, что существует момент времени, в который на обоих прогресс-барах одновременно показывается это число. Примеры Выходные данные 0 16 33 50 66 83 100 Выходные данные 0 95 96 97 98 99 100 Входные данные 4 1000 2 2 998 Выходные данные 0 50 99 100 Входные данные 6 170 130 400 256 30 100 Выходные данные 0 17 43 44 84 90 99 100 Примечание В первом тесте копируется всего один файл, поэтому оба прогресс-бара всегда показывают одинаковые значения. Во втором тесте первый прогресс-бар сразу же уйдёт вперёд, потом сбросится в ноль и начнёт догонять второй прогресс-бар заново. В конце копирования прогресс-бары некоторое время будут показывать одно и то же число. | 1,000 | true | false | true | false | false | false | true | false | false | false | 1,684 |
1467C | You are given three bags. Each bag contains a non-empty multiset of numbers. You can perform a number of operations on these bags. In one operation, you can choose any two non-empty bags, and choose one number from each of the bags. Let's say that you choose number $$$a$$$ from the first bag and number $$$b$$$ from the second bag. Then, you remove $$$b$$$ from the second bag and replace $$$a$$$ with $$$a-b$$$ in the first bag. Note that if there are multiple occurrences of these numbers, then you shall only remove/replace exactly one occurrence. You have to perform these operations in such a way that you have exactly one number remaining in exactly one of the bags (the other two bags being empty). It can be shown that you can always apply these operations to receive such a configuration in the end. Among all these configurations, find the one which has the maximum number left in the end. Input The first line of the input contains three space-separated integers $$$n_1$$$, $$$n_2$$$ and $$$n_3$$$ ($$$1 le n_1, n_2, n_3 le 3cdot10^5$$$, $$$1 le n_1+n_2+n_3 le 3cdot10^5$$$) — the number of numbers in the three bags. The $$$i$$$-th of the next three lines contain $$$n_i$$$ space-separated integers $$$a_{{i,1}}$$$, $$$a_{{i,2}}$$$, ..., $$$a_{{i,{{n_i}}}}$$$ ($$$1 le a_{{i,j}} le 10^9$$$) — the numbers in the $$$i$$$-th bag. Note In the first example input, let us perform the following operations: $$$[1, 2], [6, 3, 4, 5], [5]$$$ $$$[-5, 2], [3, 4, 5], [5]$$$ (Applying an operation to $$$(1, 6)$$$) $$$[-10, 2], [3, 4], [5]$$$ (Applying an operation to $$$(-5, 5)$$$) $$$[2], [3, 4], [15]$$$ (Applying an operation to $$$(5, -10)$$$) $$$[-1], [4], [15]$$$ (Applying an operation to $$$(2, 3)$$$) $$$[-5], [], [15]$$$ (Applying an operation to $$$(-1, 4)$$$) $$$[], [], [20]$$$ (Applying an operation to $$$(15, -5)$$$) You can verify that you cannot achieve a bigger number. Hence, the answer is $$$20$$$. | 1,900 | false | true | false | false | false | true | false | false | false | false | 3,355 |
209B | Flatland is inhabited by pixels of three colors: red, green and blue. We know that if two pixels of different colors meet in a violent fight, only one of them survives the fight (that is, the total number of pixels decreases by one). Besides, if pixels of colors _x_ and _y_ (_x_u2009≠u2009_y_) meet in a violent fight, then the pixel that survives the fight immediately changes its color to _z_ (_z_u2009≠u2009_x_;xa0_z_u2009≠u2009_y_). Pixels of the same color are friends, so they don't fight. The King of Flatland knows that his land will be peaceful and prosperous when the pixels are of the same color. For each of the three colors you know the number of pixels of this color that inhabit Flatland. Help the king and determine whether fights can bring peace and prosperity to the country and if it is possible, find the minimum number of fights needed to make the land peaceful and prosperous. Input The first line contains three space-separated integers _a_, _b_ and _c_ (0u2009≤u2009_a_,u2009_b_,u2009_c_u2009≤u2009231;xa0_a_u2009+u2009_b_u2009+u2009_c_u2009>u20090) — the number of red, green and blue pixels, correspondingly. Output Print a single number — the minimum number of pixel fights before the country becomes peaceful and prosperous. If making the country peaceful and prosperous is impossible, print -1. Note In the first test sample the country needs only one fight to achieve peace and prosperity. Besides, it can be any fight whatsoever. For example, let's assume that the green and the blue pixels fight, then the surviving pixel will be red. As a result, after the fight there are two red pixels. There won't be other pixels. In the second sample the following sequence of fights is possible: red and blue, green and red, red and blue. As a result, after all fights there is one green pixel left. | 2,100 | true | false | false | false | false | true | false | false | false | false | 8,995 |
1910F | Monocarp is playing a computer game where he's controlling an empire. An empire consists of $$$n$$$ cities, connected by $$$n - 1$$$ roads. The cities are numbered from $$$1$$$ to $$$n$$$. It's possible to reach every city from every other one using the roads. Traversing every road takes $$$2$$$ hours. However, that can be improved. Monocarp can build railway stations in no more than $$$k$$$ cities. After they are built, all existing roads that connect two cities with railway stations get converted to railroads and become $$$1$$$ hour to traverse. Let $$$f(x, y)$$$ be the total time it takes to traverse the roads on the shortest path between cities $$$x$$$ and $$$y$$$. Monocarp wants to build at most $$$k$$$ railway stations in such a way that the following value is minimized: $$$sum limits_{v=1}^{n} sum limits_{u=1}^{v-1} f(v, u)$$$ (the total time it takes to travel from every city to every other one). What the smallest value he can achieve? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The first line of each testcase contains two integers $$$n$$$ and $$$k$$$ ($$$2 le k le n le 2 cdot 10^5$$$)xa0— the number of cities and the maximum number of railway stations Monocarp can build. Each of the following $$$n-1$$$ lines contains two integers $$$v$$$ and $$$u$$$ ($$$1 le v, u le n$$$; $$$v eq u$$$)xa0— a road that connects cities $$$v$$$ and $$$u$$$. It's possible to reach every city from every other one using the roads. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print a single integerxa0— the smallest total time it takes to travel from every city to every other one that Monocarp can achieve after building at most $$$k$$$ railway stations. Example Input 3 5 2 1 2 2 3 3 4 4 5 4 4 1 2 1 3 1 4 5 3 1 2 1 3 2 4 2 5 | 2,000 | false | true | false | false | false | false | false | false | false | false | 842 |
524F | Polycarpus has a finite sequence of opening and closing brackets. In order not to fall asleep in a lecture, Polycarpus is having fun with his sequence. He is able to perform two operations: adding any bracket in any position (in the beginning, the end, or between any two existing brackets); cyclic shift — moving the last bracket from the end of the sequence to the beginning. Polycarpus can apply any number of operations to his sequence and adding a cyclic shift in any order. As a result, he wants to get the correct bracket sequence of the minimum possible length. If there are several such sequences, Polycarpus is interested in the lexicographically smallest one. Help him find such a sequence. Acorrect bracket sequence is a sequence of opening and closing brackets, from which you can get a correct arithmetic expression by adding characters "1" and "+" . Each opening bracket must correspond to a closed one. For example, the sequences "(())()", "()", "(()(()))" are correct and ")(", "(()" and "(()))(" are not. The sequence _a_1 _a_2... _a__n_ is lexicographically smaller than sequence _b_1 _b_2... _b__n_, if there is such number _i_ from 1 to _n_, that_a__k_u2009=u2009_b__k_ for 1u2009≤u2009_k_u2009<u2009_i_ and _a__i_u2009<u2009_b__i_. Consider that "(" u2009<u2009 ")". Input The first line contains Polycarpus's sequence consisting of characters "(" and ")". The length of a line is from 1 to 1u2009000u2009000. Output Print a correct bracket sequence of the minimum length that Polycarpus can obtain by his operations. If there are multiple such sequences, print the lexicographically minimum one. Note The sequence in the first example is already correct, but to get the lexicographically minimum answer, you need to perform four cyclic shift operations. In the second example you need to add a closing parenthesis between the second and third brackets and make a cyclic shift. You can first make the shift, and then add the bracket at the end. | 2,700 | false | true | false | false | true | false | false | false | false | false | 7,737 |
934A | Nian is a monster which lives deep in the oceans. Once a year, it shows up on the land, devouring livestock and even people. In order to keep the monster away, people fill their villages with red colour, light, and cracking noise, all of which frighten the monster out of coming. Little Tommy has _n_ lanterns and Big Banban has _m_ lanterns. Tommy's lanterns have brightness _a_1,u2009_a_2,u2009...,u2009_a__n_, and Banban's have brightness _b_1,u2009_b_2,u2009...,u2009_b__m_ respectively. Tommy intends to hide one of his lanterns, then Banban picks one of Tommy's non-hidden lanterns and one of his own lanterns to form a pair. The pair's brightness will be the product of the brightness of two lanterns. Tommy wants to make the product as small as possible, while Banban tries to make it as large as possible. You are asked to find the brightness of the chosen pair if both of them choose optimally. Input The first line contains two space-separated integers _n_ and _m_ (2u2009≤u2009_n_,u2009_m_u2009≤u200950). The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_. The third line contains _m_ space-separated integers _b_1,u2009_b_2,u2009...,u2009_b__m_. All the integers range from u2009-u2009109 to 109. Note In the first example, Tommy will hide 20 and Banban will choose 18 from Tommy and 14 from himself. In the second example, Tommy will hide 3 and Banban will choose 2 from Tommy and 1 from himself. | 1,400 | false | false | false | false | false | false | true | false | false | false | 5,966 |
756A | Pavel cooks barbecue. There are _n_ skewers, they lay on a brazier in a row, each on one of _n_ positions. Pavel wants each skewer to be cooked some time in every of _n_ positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation _p_ and a sequence _b_1,u2009_b_2,u2009...,u2009_b__n_, consisting of zeros and ones. Each second Pavel move skewer on position _i_ to position _p__i_, and if _b__i_ equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation _p_ and sequence _b_ suits Pavel. What is the minimum total number of elements in the given permutation _p_ and the given sequence _b_ he needs to change so that every skewer will visit each of 2_n_ placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation _p_ and a sequence _b_ suit him if there is an integer _k_ (_k_u2009≥u20092_n_), so that after _k_ seconds each skewer visits each of the 2_n_ placements. It can be shown that some suitable pair of permutation _p_ and sequence _b_ exists for any _n_. Input The first line contain the integer _n_ (1u2009≤u2009_n_u2009≤u20092·105)xa0— the number of skewers. The second line contains a sequence of integers _p_1,u2009_p_2,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009≤u2009_n_)xa0— the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence _b_1,u2009_b_2,u2009...,u2009_b__n_ consisting of zeros and ones, according to which Pavel wants to reverse the skewers. Note In the first example Pavel can change the permutation to 4,u20093,u20091,u20092. In the second example Pavel can change any element of _b_ to 1. | 1,700 | false | false | false | false | false | true | false | false | false | false | 6,770 |
543C | You have multiset of _n_ strings of the same length, consisting of lowercase English letters. We will say that those strings are easy to remember if for each string there is some position _i_ and some letter _c_ of the English alphabet, such that this string is the only string in the multiset that has letter _c_ in position _i_. For example, a multiset of strings {"abc", "aba", "adc", "ada"} are not easy to remember. And multiset {"abc", "ada", "ssa"} is easy to remember because: the first string is the only string that has character _c_ in position 3; the second string is the only string that has character _d_ in position 2; the third string is the only string that has character _s_ in position 2. You want to change your multiset a little so that it is easy to remember. For _a__ij_ coins, you can change character in the _j_-th position of the _i_-th string into any other lowercase letter of the English alphabet. Find what is the minimum sum you should pay in order to make the multiset of strings easy to remember. Input The first line contains two integers _n_, _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u200920)xa0— the number of strings in the multiset and the length of the strings respectively. Next _n_ lines contain the strings of the multiset, consisting only of lowercase English letters, each string's length is _m_. Next _n_ lines contain _m_ integers each, the _i_-th of them contains integers _a__i_1,u2009_a__i_2,u2009...,u2009_a__im_ (0u2009≤u2009_a__ij_u2009≤u2009106). Output Print a single number — the answer to the problem. Examples Input 4 5 abcde abcde abcde abcde 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Input 4 3 abc aba adc ada 10 10 10 10 1 10 10 10 10 10 1 10 Input 3 3 abc ada ssa 1 1 1 1 1 1 1 1 1 | 2,500 | false | false | false | true | false | false | false | false | false | false | 7,667 |
1573B | You are given two arrays $$$a$$$ and $$$b$$$ of length $$$n$$$. Array $$$a$$$ contains each odd integer from $$$1$$$ to $$$2n$$$ in an arbitrary order, and array $$$b$$$ contains each even integer from $$$1$$$ to $$$2n$$$ in an arbitrary order. You can perform the following operation on those arrays: choose one of the two arrays pick an index $$$i$$$ from $$$1$$$ to $$$n-1$$$ swap the $$$i$$$-th and the $$$(i+1)$$$-th elements of the chosen array Compute the minimum number of operations needed to make array $$$a$$$ lexicographically smaller than array $$$b$$$. For two different arrays $$$x$$$ and $$$y$$$ of the same length $$$n$$$, we say that $$$x$$$ is lexicographically smaller than $$$y$$$ if in the first position where $$$x$$$ and $$$y$$$ differ, the array $$$x$$$ has a smaller element than the corresponding element in $$$y$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) — the length of the arrays. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 2n$$$, all $$$a_i$$$ are odd and pairwise distinct) — array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le 2n$$$, all $$$b_i$$$ are even and pairwise distinct) — array $$$b$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print one integer: the minimum number of operations needed to make array $$$a$$$ lexicographically smaller than array $$$b$$$. We can show that an answer always exists. Example Input 3 2 3 1 4 2 3 5 3 1 2 4 6 5 7 5 9 1 3 2 4 6 10 8 Note In the first example, the array $$$a$$$ is already lexicographically smaller than array $$$b$$$, so no operations are required. In the second example, we can swap $$$5$$$ and $$$3$$$ and then swap $$$2$$$ and $$$4$$$, which results in $$$[3, 5, 1]$$$ and $$$[4, 2, 6]$$$. Another correct way is to swap $$$3$$$ and $$$1$$$ and then swap $$$5$$$ and $$$1$$$, which results in $$$[1, 5, 3]$$$ and $$$[2, 4, 6]$$$. Yet another correct way is to swap $$$4$$$ and $$$6$$$ and then swap $$$2$$$ and $$$6$$$, which results in $$$[5, 3, 1]$$$ and $$$[6, 2, 4]$$$. | 1,400 | true | true | false | false | false | false | false | false | true | false | 2,775 |
407B | One day, little Vasya found himself in a maze consisting of (_n_u2009+u20091) rooms, numbered from 1 to (_n_u2009+u20091). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (_n_u2009+u20091)-th one. The maze is organized as follows. Each room of the maze has two one-way portals. Let's consider room number _i_ (1u2009≤u2009_i_u2009≤u2009_n_), someone can use the first portal to move from it to room number (_i_u2009+u20091), also someone can use the second portal to move from it to room number _p__i_, where 1u2009≤u2009_p__i_u2009≤u2009_i_. In order not to get lost, Vasya decided to act as follows. Each time Vasya enters some room, he paints a cross on its ceiling. Initially, Vasya paints a cross at the ceiling of room 1. Let's assume that Vasya is in room _i_ and has already painted a cross on its ceiling. Then, if the ceiling now contains an odd number of crosses, Vasya uses the second portal (it leads to room _p__i_), otherwise Vasya uses the first portal. Help Vasya determine the number of times he needs to use portals to get to room (_n_u2009+u20091) in the end. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009103)xa0— the number of rooms. The second line contains _n_ integers _p__i_ (1u2009≤u2009_p__i_u2009≤u2009_i_). Each _p__i_ denotes the number of the room, that someone can reach, if he will use the second portal in the _i_-th room. Output Print a single number — the number of portal moves the boy needs to go out of the maze. As the number can be rather large, print it modulo 1000000007 (109u2009+u20097). | 1,600 | false | false | true | true | false | false | false | false | false | false | 8,197 |
150E | This winter is so... well, you've got the idea :-) The Nvodsk road system can be represented as _n_ junctions connected with _n_u2009-u20091 bidirectional roads so that there is a path between any two junctions. The organizers of some event want to choose a place to accommodate the participants (junction _v_), and the place to set up the contests (junction _u_). Besides, at the one hand, they want the participants to walk about the city and see the neighbourhood (that's why the distance between _v_ and _u_ should be no less than _l_). On the other hand, they don't want the participants to freeze (so the distance between _v_ and _u_ should be no more than _r_). Besides, for every street we know its beauty — some integer from 0 to 109. Your task is to choose the path that fits in the length limits and has the largest average beauty. We shall define the average beauty as a median of sequence of the beauties of all roads along the path. We can put it more formally like that: let there be a path with the length _k_. Let _a__i_ be a non-decreasing sequence that contains exactly _k_ elements. Each number occurs there exactly the number of times a road with such beauty occurs along on path. We will represent the path median as number _a_⌊_k_u2009/u20092⌋, assuming that indexation starting from zero is used. ⌊_x_⌋ — is number _х_, rounded down to the nearest integer. For example, if _a_u2009=u2009{0,u20095,u200912}, then the median equals to 5, and if _a_u2009=u2009{0,u20095,u20097,u200912}, then the median is number 7. It is guaranteed that there will be at least one path with the suitable quantity of roads. Input The first line contains three integers _n_, _l_, _r_ (1u2009≤u2009_l_u2009≤u2009_r_u2009<u2009_n_u2009≤u2009105). Next _n_u2009-u20091 lines contain descriptions of roads of the Nvodsk, each line contains three integers _a__i_, _b__i_, _c__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, 0u2009≤u2009_c__i_u2009≤u2009109, _a__i_u2009≠u2009_b__i_) — junctions _a__i_ and _b__i_ are connected with a street whose beauty equals _c__i_. Output Print two integers — numbers of the junctions, where to accommodate the participants and set up the contests, correspondingly. If there are multiple optimal variants, print any of them. Note In the first sample all roads have the same beauty. That means that all paths of the positive length have the same median. Thus, any path with length from 3 to 4, inclusive will be valid for us. In the second sample the city looks like that: 1 - 2 - 3 - 4 - 5 - 6. Two last roads are more valuable and we should choose any path that contains both of them and has the suitable length. It is either the path between 2 and 6 or the path between 3 and 6. | 3,000 | false | false | false | false | true | false | false | true | false | false | 9,272 |
61A | Shapur was an extremely gifted student. He was great at everything including Combinatorics, Algebra, Number Theory, Geometry, Calculus, etc. He was not only smart but extraordinarily fast! He could manage to sum 1018 numbers in a single second. One day in 230 AD Shapur was trying to find out if any one can possibly do calculations faster than him. As a result he made a very great contest and asked every one to come and take part. In his contest he gave the contestants many different pairs of numbers. Each number is made from digits 0 or 1. The contestants should write a new number corresponding to the given pair of numbers. The rule is simple: The _i_-th digit of the answer is 1 if and only if the _i_-th digit of the two given numbers differ. In the other case the _i_-th digit of the answer is 0. Shapur made many numbers and first tried his own speed. He saw that he can perform these operations on numbers of length ∞ (length of a number is number of digits in it) in a glance! He always gives correct answers so he expects the contestants to give correct answers, too. He is a good fellow so he won't give anyone very big numbers and he always gives one person numbers of same length. Now you are going to take part in Shapur's contest. See if you are faster and more accurate. Input There are two lines in each input. Each of them contains a single number. It is guaranteed that the numbers are made from 0 and 1 only and that their length is same. The numbers may start with 0. The length of each number doesn't exceed 100. Output Write one line — the corresponding answer. Do not omit the leading 0s. | 800 | false | false | true | false | false | false | false | false | false | false | 9,670 |
961C | Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size _n_ by _n_, _n_ is always odd. And what's even worse, some squares were of wrong color. _j_-th square of the _i_-th row of _k_-th piece of the board has color _a__k_,u2009_i_,u2009_j_; 1 being black and 0 being white. Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2_n_ by 2_n_. You are allowed to move pieces but not allowed to rotate or flip them. Input The first line contains odd integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the size of all pieces of the board. Then 4 segments follow, each describes one piece of the board. Each consists of _n_ lines of _n_ characters; _j_-th one of _i_-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line. Output Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard. Examples Input 3 101 010 101101 000 101 010 101 011 010 101 010 | 1,400 | false | false | true | false | false | false | true | false | false | false | 5,861 |
49E | The DNA sequence for every living creature in Berland can be represented as a non-empty line consisting of lowercase Latin letters. Berland scientists found out that all the creatures evolve by stages. During one stage exactly one symbol of the DNA line is replaced by exactly two other ones. At that overall there are _n_ permissible substitutions. The substitution _a__i_->_b__i__c__i_ means that any one symbol _a__i_ can be replaced with two symbols _b__i__c__i_. Every substitution could happen an unlimited number of times. They say that two creatures with DNA sequences _s_1 and _s_2 can have a common ancestor if there exists such a DNA sequence _s_3 that throughout evolution it can result in _s_1 and _s_2, perhaps after a different number of stages. Your task is to find out by the given _s_1 and _s_2 whether the creatures possessing such DNA sequences can have a common ancestor. If the answer is positive, you have to find the length of the shortest sequence of the common ancestor’s DNA. Input The first line contains a non-empty DNA sequence _s_1, the second line contains a non-empty DNA sequence _s_2. The lengths of these lines do not exceed 50, the lines contain only lowercase Latin letters. The third line contains an integer _n_ (0u2009≤u2009_n_u2009≤u200950) — the number of permissible substitutions. Then follow _n_ lines each of which describes a substitution in the format _a__i_->_b__i__c__i_. The characters _a__i_, _b__i_, and _c__i_ are lowercase Latin letters. Lines _s_1 and _s_2 can coincide, the list of substitutions can contain similar substitutions. Output If _s_1 and _s_2 cannot have a common ancestor, print -1. Otherwise print the length of the shortest sequence _s_3, from which _s_1 and _s_2 could have evolved. Examples Input ababa aba 7 c->ba c->cc e->ab z->ea b->ba d->dd d->ab | 2,300 | false | false | false | true | false | false | false | false | false | false | 9,725 |
1307C | Bessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However, Bessie is sure that there is a secret message hidden inside. The text is a string $$$s$$$ of lowercase Latin letters. She considers a string $$$t$$$ as hidden in string $$$s$$$ if $$$t$$$ exists as a subsequence of $$$s$$$ whose indices form an arithmetic progression. For example, the string aab is hidden in string aaabb because it occurs at indices $$$1$$$, $$$3$$$, and $$$5$$$, which form an arithmetic progression with a common difference of $$$2$$$. Bessie thinks that any hidden string that occurs the most times is the secret message. Two occurrences of a subsequence of $$$S$$$ are distinct if the sets of indices are different. Help her find the number of occurrences of the secret message! For example, in the string aaabb, a is hidden $$$3$$$ times, b is hidden $$$2$$$ times, ab is hidden $$$6$$$ times, aa is hidden $$$3$$$ times, bb is hidden $$$1$$$ time, aab is hidden $$$2$$$ times, aaa is hidden $$$1$$$ time, abb is hidden $$$1$$$ time, aaab is hidden $$$1$$$ time, aabb is hidden $$$1$$$ time, and aaabb is hidden $$$1$$$ time. The number of occurrences of the secret message is $$$6$$$. Note In the first example, these are all the hidden strings and their indice sets: a occurs at $$$(1)$$$, $$$(2)$$$, $$$(3)$$$ b occurs at $$$(4)$$$, $$$(5)$$$ ab occurs at $$$(1,4)$$$, $$$(1,5)$$$, $$$(2,4)$$$, $$$(2,5)$$$, $$$(3,4)$$$, $$$(3,5)$$$ aa occurs at $$$(1,2)$$$, $$$(1,3)$$$, $$$(2,3)$$$ bb occurs at $$$(4,5)$$$ aab occurs at $$$(1,3,5)$$$, $$$(2,3,4)$$$ aaa occurs at $$$(1,2,3)$$$ abb occurs at $$$(3,4,5)$$$ aaab occurs at $$$(1,2,3,4)$$$ aabb occurs at $$$(2,3,4,5)$$$ aaabb occurs at $$$(1,2,3,4,5)$$$ Note that all the sets of indices are arithmetic progressions. In the second example, no hidden string occurs more than once. In the third example, the hidden string is the letter l. | 1,500 | true | false | false | true | false | false | true | false | false | false | 4,154 |
1379B | Pasha loves to send strictly positive integers to his friends. Pasha cares about security, therefore when he wants to send an integer $$$n$$$, he encrypts it in the following way: he picks three integers $$$a$$$, $$$b$$$ and $$$c$$$ such that $$$l leq a,b,c leq r$$$, and then he computes the encrypted value $$$m = n cdot a + b - c$$$. Unfortunately, an adversary intercepted the values $$$l$$$, $$$r$$$ and $$$m$$$. Is it possible to recover the original values of $$$a$$$, $$$b$$$ and $$$c$$$ from this information? More formally, you are asked to find any values of $$$a$$$, $$$b$$$ and $$$c$$$ such that $$$a$$$, $$$b$$$ and $$$c$$$ are integers, $$$l leq a, b, c leq r$$$, there exists a strictly positive integer $$$n$$$, such that $$$n cdot a + b - c = m$$$. Input The first line contains the only integer $$$t$$$ ($$$1 leq t leq 20$$$)xa0— the number of test cases. The following $$$t$$$ lines describe one test case each. Each test case consists of three integers $$$l$$$, $$$r$$$ and $$$m$$$ ($$$1 leq l leq r leq 500,000$$$, $$$1 leq m leq 10^{10}$$$). The numbers are such that the answer to the problem exists. Output For each test case output three integers $$$a$$$, $$$b$$$ and $$$c$$$ such that, $$$l leq a, b, c leq r$$$ and there exists a strictly positive integer $$$n$$$ such that $$$n cdot a + b - c = m$$$. It is guaranteed that there is at least one possible solution, and you can output any possible combination if there are multiple solutions. Note In the first example $$$n = 3$$$ is possible, then $$$n cdot 4 + 6 - 5 = 13 = m$$$. Other possible solutions include: $$$a = 4$$$, $$$b = 5$$$, $$$c = 4$$$ (when $$$n = 3$$$); $$$a = 5$$$, $$$b = 4$$$, $$$c = 6$$$ (when $$$n = 3$$$); $$$a = 6$$$, $$$b = 6$$$, $$$c = 5$$$ (when $$$n = 2$$$); $$$a = 6$$$, $$$b = 5$$$, $$$c = 4$$$ (when $$$n = 2$$$). In the second example the only possible case is $$$n = 1$$$: in this case $$$n cdot 2 + 2 - 3 = 1 = m$$$. Note that, $$$n = 0$$$ is not possible, since in that case $$$n$$$ is not a strictly positive integer. | 1,500 | true | false | false | false | false | false | true | true | false | false | 3,777 |
1776E | Isona is in a train station. This station has two platforms, and between them there are $$$m$$$ parallel railways that can be viewed as infinite straight lines. Each railway is identified with an integer from $$$1$$$ to $$$m$$$, railway $$$1$$$ being the closest to the first platform and railway $$$m$$$ being the farthest. There is a $$$1$$$ meter distance between consecutive railways, as well as between each platform and its closest railway. Isona is standing on the inner border of the first platform, when she realizes that she forgot to validate her ticket! There is a validating machine on the second platform, exactly opposite her current position (thus, the distance between Isona and the validating machine is $$$m + 1$$$ meters). There are only $$$s$$$ seconds left to validate the ticket and the bridge designated to cross the railways is too far from the validating machine. Therefore, Isona (who is very brave and a little bit careless) will cross the railways running in a straight line perpendicular to the railways themselves. Isona can only run forward (not backward) and she can stay still. When she runs at maximum speed, she needs $$$v$$$ seconds to traverse $$$1$$$ meter. She can run at any speed less than or equal to her maximum speed. There is only one problem: $$$n$$$ trains are programmed to transit through the railways. The $$$i$$$-th train will use the railway $$$r_i$$$. It will start crossing the straight line between Isona and the validating machine $$$a_i$$$ seconds from now and it will end $$$b_i$$$ seconds from now. Of course, Isona cannot cross a railway when a train is passing. Formally, for every $$$i = 1, , 2, , dots, , n$$$, Isona is not allowed to be on railway $$$r_i$$$ at any time $$$t$$$ with $$$a_i < t < b_i$$$ (but she is allowed to cross at times $$$a_i$$$ or $$$b_i$$$). The following picture summarizes the situation. In the picture there are $$$m = 4$$$ railways and two trains are visible; the train going through railway $$$3$$$ is currently crossing the line between Isona and the validating machine. Isona is a really good runner, but she gets tired every time she has to change her running speed. What is the minimum number of speed changes she has to perform to get to the validating machine on the other platform within $$$s$$$ seconds from now? Note that at the beginning Isona is not running. She can start to run anytime. The instant she starts to run (i.e. her speed becomes positive) is not counted as a speed change. Input The first line of the input contains four integers $$$n$$$, $$$m$$$, $$$s$$$, $$$v$$$ ($$$1 leq n leq 500$$$, $$$1 leq m leq 10$$$, $$$1 leq s, v leq 10^9$$$) — the number of trains, the number of railways, the maximum time in seconds Isona can spend crossing the railways, and the number of seconds she needs to traverse $$$1$$$ meter at maximum speed. Each of the next $$$n$$$ lines contains three integers $$$a_i$$$, $$$b_i$$$, $$$r_i$$$ ($$$1 leq a_i < b_i leq 10^9$$$, $$$1 leq r_i leq m$$$) — the start and end times of the $$$i$$$-th train crossing the straight line between Isona and the validating machine, and the railway it will be using. It is guaranteed that, for any two trains $$$i$$$ and $$$j$$$ that go through the same railway (i.e. $$$r_i = r_j$$$), there is at least $$$1$$$ second between them (that is, either $$$a_j ge b_i + 1$$$ or $$$a_i ge b_j + 1$$$). Output Print the minimum number of speed changes Isona has to perform to get to the validating machine in time. If this is impossible, print $$$-1$$$. Examples Input 4 3 5 1 1 2 1 3 4 1 2 3 2 3 4 3 Input 3 3 12 2 2 10 1 1 6 2 8 12 3 Input 8 4 13 2 1 4 1 5 13 1 1 5 2 6 13 2 1 9 3 10 13 3 1 10 4 11 13 4 Note In the first sample, if Isona starts running at time $$$t=0$$$ at maximum speed ($$$1$$$ m/s), she will cross each railway just when a train is about to traverse it, and she will arrive at the other platform at time $$$4 = s - 1$$$ without changing speed. In the second sample, a possible solution with $$$2$$$ speed changes is the following: for the first $$$2$$$ seconds Isona goes at maximum speed ($$$0.5$$$ m/s), then she slows down to $$$0.25$$$ m/s for $$$4$$$ seconds until she reaches the second railway. At that point, she goes at maximum speed again until she reaches the other platform. In the third sample, Isona can wait $$$2$$$ seconds before starting running. She then runs for $$$5$$$ seconds at maximum speed ($$$0.5$$$ m/s). After that, she waits for $$$1$$$ second not running (or running at $$$0$$$ m/s), and finally she runs again at maximum speed for the last $$$5$$$ seconds. Overall, she changes speed twice. | 3,500 | false | false | false | true | true | false | false | false | false | false | 1,624 |
1822F | Two friends, Alisa and Yuki, planted a tree with $$$n$$$ vertices in their garden. A tree is an undirected graph without cycles, loops, or multiple edges. Each edge in this tree has a length of $$$k$$$. Initially, vertex $$$1$$$ is the root of the tree. Alisa and Yuki are growing the tree not just for fun, they want to sell it. The cost of the tree is defined as the maximum distance from the root to a vertex among all vertices of the tree. The distance between two vertices $$$u$$$ and $$$v$$$ is the sum of the lengths of the edges on the path from $$$u$$$ to $$$v$$$. The girls took a course in gardening, so they know how to modify the tree. Alisa and Yuki can spend $$$c$$$ coins to shift the root of the tree to one of the neighbors of the current root. This operation can be performed any number of times (possibly zero). Note that the structure of the tree is left unchanged; the only change is which vertex is the root. The friends want to sell the tree with the maximum profit. The profit is defined as the difference between the cost of the tree and the total cost of operations. The profit is cost of the tree minus the total cost of operations. Help the girls and find the maximum profit they can get by applying operations to the tree any number of times (possibly zero). Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains integers $$$n$$$, $$$k$$$, $$$c$$$ ($$$2 le n le 2 cdot 10^5$$$; $$$1 le k, c le 10^9$$$)xa0— the number of vertices in the tree, the length of each edge, and the cost of the operation. The next $$$n - 1$$$ lines of the test case contain pairs of integers $$$u_i$$$, $$$v_i$$$ ($$$1 le u_i, v_i le n$$$)xa0— the edges of the graph. These edges form a tree. The sum of the values of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0— the maximum profit that Yuki and Alisa can get. Example Input 4 3 2 3 2 1 3 1 5 4 1 2 1 4 2 5 4 3 4 6 5 3 4 1 6 1 2 6 5 1 3 2 10 6 4 1 3 1 9 9 7 7 6 6 4 9 2 2 8 8 5 5 10 | 1,700 | false | false | false | true | false | false | true | false | false | true | 1,346 |
339B | Problem - 339B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *1000 No tag edit access → Contest materials . The second line contains _m_ integers _a_1,u2009_a_2,u2009...,u2009_a__m_ (1u2009≤u2009_a__i_u2009≤u2009_n_). Note that Xenia can have multiple consecutive tasks in one house. Output Print a single integer — the time Xenia needs to complete all tasks. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 3 3 2 3 Output 6 Input 4 3 2 3 3 Output 2 Note In the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1u2009→u20092u2009→u20093u2009→u20094u2009→u20091u2009→u20092u2009→u20093. This is optimal sequence. So, she needs 6 time units. | 1,000 | false | false | true | false | false | false | false | false | false | false | 8,479 |
1713C | A $$$mathbf{0}$$$-indexed array $$$a$$$ of size $$$n$$$ is called good if for all valid indices $$$i$$$ ($$$0 le i le n-1$$$), $$$a_i + i$$$ is a perfect square$$$^dagger$$$. Given an integer $$$n$$$. Find a permutation$$$^ddagger$$$ $$$p$$$ of $$$[0,1,2,ldots,n-1]$$$ that is good or determine that no such permutation exists. $$$^dagger$$$ An integer $$$x$$$ is said to be a perfect square if there exists an integer $$$y$$$ such that $$$x = y^2$$$. $$$^ddagger$$$ An array $$$b$$$ is a permutation of an array $$$a$$$ if $$$b$$$ consists of the elements of $$$a$$$ in arbitrary order. For example, $$$[4,2,3,4]$$$ is a permutation of $$$[3,2,4,4]$$$ while $$$[1,2,2]$$$ is not a permutation of $$$[1,2,3]$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) — the length of the permutation $$$p$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output $$$n$$$ distinct integers $$$p_0, p_1, dots, p_{n-1}$$$ ($$$0 le p_i le n-1$$$) — the permutation $$$p$$$ — if the answer exists, and $$$-1$$$ otherwise. Example Output 1 0 2 0 3 2 1 1 0 2 6 5 4 3 Note In the first test case, we have $$$n=3$$$. The array $$$p = [1, 0, 2]$$$ is good since $$$1 + 0 = 1^2$$$, $$$0 + 1 = 1^2$$$, and $$$2 + 2 = 2^2$$$ In the second test case, we have $$$n=4$$$. The array $$$p = [0, 3, 2, 1]$$$ is good since $$$0 + 0 = 0^2$$$, $$$3 + 1 = 2^2$$$, $$$2+2 = 2^2$$$, and $$$1+3 = 2^2$$$. | 1,200 | true | false | false | true | false | true | false | false | false | false | 2,004 |
1140C | You have a playlist consisting of $$$n$$$ songs. The $$$i$$$-th song is characterized by two numbers $$$t_i$$$ and $$$b_i$$$ — its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of $$$3$$$ songs having lengths $$$[5, 7, 4]$$$ and beauty values $$$[11, 14, 6]$$$ is equal to $$$(5 + 7 + 4) cdot 6 = 96$$$. You need to choose at most $$$k$$$ songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 3 cdot 10^5$$$) – the number of songs in the playlist and the maximum number of songs you can choose, respectively. Each of the next $$$n$$$ lines contains two integers $$$t_i$$$ and $$$b_i$$$ ($$$1 le t_i, b_i le 10^6$$$) — the length and beauty of $$$i$$$-th song. Output Print one integer — the maximum pleasure you can get. Examples Input 4 3 4 7 15 1 3 6 6 8 Input 5 3 12 31 112 4 100 100 13 55 55 50 Note In the first test case we can choose songs $$${1, 3, 4}$$$, so the total pleasure is $$$(4 + 3 + 6) cdot 6 = 78$$$. In the second test case we can choose song $$$3$$$. The total pleasure will be equal to $$$100 cdot 100 = 10000$$$. | 1,600 | false | false | false | false | true | false | true | false | true | false | 5,026 |
1493D | Problem - 1493D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force data structures hashing implementation math number theory sortings two pointers *2100 No tag edit access → Contest materials ") Editorial") ]( of all elements of the array $$$a$$$. Since the answer can be too large, you are asked to output it modulo $$$10^9+7$$$. Input The first line contains two integers — $$$n$$$ and $$$q$$$ ($$$1 le n, q le 2 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 2 cdot 10^5$$$) — the elements of the array $$$a$$$ before the changes. The next $$$q$$$ lines contain queries in the following format: each line contains two integers $$$i$$$ and $$$x$$$ ($$$1 le i le n$$$, $$$1 le x le 2 cdot 10^5$$$). Output Print $$$q$$$ lines: after processing each query output the GCD of all elements modulo $$$10^9+7$$$ on a separate line. Example Input 4 3 1 6 8 12 1 12 2 3 3 3 Output 2 2 6 Note After the first query the array is $$$ | 2,100 | true | false | true | false | true | false | true | false | true | false | 3,205 |
1548E | Two painters, Amin and Benj, are repainting Gregor's living room ceiling! The ceiling can be modeled as an $$$n imes m$$$ grid. For each $$$i$$$ between $$$1$$$ and $$$n$$$, inclusive, painter Amin applies $$$a_i$$$ layers of paint to the entire $$$i$$$-th row. For each $$$j$$$ between $$$1$$$ and $$$m$$$, inclusive, painter Benj applies $$$b_j$$$ layers of paint to the entire $$$j$$$-th column. Therefore, the cell $$$(i,j)$$$ ends up with $$$a_i+b_j$$$ layers of paint. Gregor considers the cell $$$(i,j)$$$ to be badly painted if $$$a_i+b_j le x$$$. Define a badly painted region to be a maximal connected component of badly painted cells, i.xa0e. a connected component of badly painted cells such that all adjacent to the component cells are not badly painted. Two cells are considered adjacent if they share a side. Gregor is appalled by the state of the finished ceiling, and wants to know the number of badly painted regions. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$x$$$ ($$$1 le n,m le 2cdot 10^5$$$, $$$1 le x le 2cdot 10^5$$$) — the dimensions of Gregor's ceiling, and the maximum number of paint layers in a badly painted cell. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 2cdot 10^5$$$), the number of paint layers Amin applies to each row. The third line contains $$$m$$$ integers $$$b_1, b_2, ldots, b_m$$$ ($$$1 le b_j le 2cdot 10^5$$$), the number of paint layers Benj applies to each column. Output Print a single integer, the number of badly painted regions. Examples Input 3 4 11 9 8 5 10 6 7 2 Input 3 4 12 9 8 5 10 6 7 2 Input 5 23 6 1 4 3 5 2 2 3 1 6 1 5 5 6 1 3 2 6 2 3 1 6 1 4 1 6 1 5 5 Note The diagram below represents the first example. The numbers to the left of each row represent the list $$$a$$$, and the numbers above each column represent the list $$$b$$$. The numbers inside each cell represent the number of paint layers in that cell. The colored cells correspond to badly painted cells. The red and blue cells respectively form $$$2$$$ badly painted regions. | 3,400 | true | true | false | false | true | false | false | false | false | true | 2,901 |
2008F | Sakurako has a box with $$$n$$$ balls. Each ball has it's value. She wants to bet with her friend that if the friend randomly picks two balls from the box (it could be two distinct balls, but they may have the same value), the product of their values will be the same as the number that Sakurako guessed. Since Sakurako has a PhD in probability, she knows that the best number to pick is $$$. Input The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$) xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2le nle 2cdot 10^5$$$) xa0— the number of elements in the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0le a_ile 10^9$$$) xa0— the elements of the array. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2cdot 10^5$$$. Note For the first test, Sakurako's friend can pick these pairs of balls: $$$(a_1,a_2)$$$, $$$(a_1,a_3)$$$, $$$(a_2,a_3)$$$. Their products equal to $$$3cdot 2=6$$$ , $$$3cdot 3=9$$$ , $$$3cdot 2=6$$$ respectively, so the expected value is $$$frac{6+9+6}{3}=7$$$. For the second test, Sakurako's friend can pick these pairs of balls: $$$(a_1,a_2)$$$, $$$(a_1,a_3)$$$, $$$(a_1,a_4)$$$, $$$(a_2,a_3)$$$, $$$(a_2,a_4)$$$, $$$(a_3,a_4)$$$. Their products equal to $$$2cdot 2=4$$$ , $$$2cdot 2=4$$$ , $$$2cdot 4=8$$$, $$$2cdot 2=4$$$, $$$2cdot 4=8$$$, $$$2cdot 4=8$$$ respectively, so the expected value is $$$frac{4+4+8+4+8+8}{6}=frac{36}{6}=6$$$. | 1,400 | true | false | false | false | false | false | false | false | false | false | 203 |
1546B | AquaMoon had $$$n$$$ strings of length $$$m$$$ each. $$$n$$$ is an odd number. When AquaMoon was gone, Cirno tried to pair these $$$n$$$ strings together. After making $$$frac{n-1}{2}$$$ pairs, she found out that there was exactly one string without the pair! In her rage, she disrupted each pair of strings. For each pair, she selected some positions (at least $$$1$$$ and at most $$$m$$$) and swapped the letters in the two strings of this pair at the selected positions. For example, if $$$m = 6$$$ and two strings "abcdef" and "xyzklm" are in one pair and Cirno selected positions $$$2$$$, $$$3$$$ and $$$6$$$ she will swap 'b' with 'y', 'c' with 'z' and 'f' with 'm'. The resulting strings will be "ayzdem" and "xbcklf". Cirno then stole away the string without pair and shuffled all remaining strings in arbitrary order. AquaMoon found the remaining $$$n-1$$$ strings in complete disarray. Also, she remembers the initial $$$n$$$ strings. She wants to know which string was stolen, but she is not good at programming. Can you help her? Input This problem is made as interactive. It means, that your solution will read the input, given by the interactor. But the interactor will give you the full input at the beginning and after that, you should print the answer. So you should solve the problem, like as you solve the usual, non-interactive problem because you won't have any interaction process. The only thing you should not forget is to flush the output buffer, after printing the answer. Otherwise, you can get an "Idleness limit exceeded" verdict. Refer to the — the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 leq n leq 10^5$$$, $$$1 leq m leq 10^5$$$) — the number of strings and the length of each string, respectively. The next $$$n$$$ lines each contain a string with length $$$m$$$, describing the original $$$n$$$ strings. All string consists of lowercase Latin letters. The next $$$n-1$$$ lines each contain a string with length $$$m$$$, describing the strings after Cirno exchanged and reordered them. It is guaranteed that $$$n$$$ is odd and that the sum of $$$n cdot m$$$ over all test cases does not exceed $$$10^5$$$. Hack format: The first line should contain a single integer $$$t$$$. After that $$$t$$$ test cases should follow in the following format: The first line should contain two integers $$$n$$$ and $$$m$$$. The following $$$n$$$ lines should contain $$$n$$$ strings of length $$$m$$$, describing the original strings. The following $$$frac{n-1}{2}$$$ lines should describe the pairs. They should contain, in the following order: the index of the first string $$$i$$$ ($$$1 leq i leq n$$$), the index of the second string $$$j$$$ ($$$1 leq j leq n$$$, $$$i eq j$$$), the number of exchanged positions $$$k$$$ ($$$1 leq k leq m$$$), and the list of $$$k$$$ positions that are exchanged ($$$k$$$ distinct indices from $$$1$$$ to $$$m$$$ in any order). The final line should contain a permutation of integers from $$$1$$$ to $$$n$$$, describing the way the strings should be reordered. The strings will be placed in the order indices placed in this permutation, the stolen string index will be ignored. Note In the first test case, "aaaaa" and "bbbbb" exchanged all positions, and "ccccc" is the stolen string. In the second test case, "aaaa" and "bbbb" exchanged two first positions, and "cccc" is the stolen string. This is the first test in the hack format: 3 3 5 aaaaa bbbbb ccccc 1 2 5 1 2 3 4 5 2 1 3 3 4 aaaa bbbb cccc 1 2 2 1 2 2 1 3 5 6 abcdef uuuuuu kekeke ekekek xyzklm 1 5 3 2 3 6 2 4 3 2 4 6 5 4 1 2 3 | 1,200 | true | false | false | false | false | false | false | false | false | false | 2,914 |
1687D | Problem - 1687D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search brute force data structures dsu implementation math *2900 No tag edit access → Contest materials $$$ be the minimal $$$ be the maximal square number less than or equal to $$$x$$$. For example, $$$f(1)=f(2)=g(4)=g(8)=4$$$. A positive integer $$$x$$$ is cute if $$$x-g(x)<f(x)-x$$$. For example, $$$1,5,11$$$ are cute integers, while $$$3,8,15$$$ are not. Ran gives you an array $$$a$$$ of length $$$n$$$. She wants you to find the smallest non-negative integer $$$k$$$ such that $$$a_i + k$$$ is a cute number for any element of $$$a$$$. Input The first line contains one integer $$$n$$$ ($$$1 leq n leq 10^6$$$) — the length of $$$a$$$. The second line contains $$$n$$$ intergers $$$a_1,a_2,ldots,a_n$$$ ($$$1 leq a_1 leq a_2 leq ldots leq a_n leq 2cdot 10^6$$$) — the array $$$a$$$. Output Print a single interger $$$k$$$ — the answer. Examples Input 4 1 3 8 10 Output 1 Input 5 2 3 8 9 11 Output 8 Input 8 1 2 3 4 5 6 7 8 Output 48 Note Test case 1: $$$3$$$ is not cute integer, so $$$k e 0$$$. $$$2,4,9,11$$$ are cute integers, so $$$k=1$$$. | 2,900 | true | false | true | false | true | false | true | true | false | false | 2,154 |
309D | Professional sport is more than hard work. It also is the equipment, designed by top engineers. As an example, let's take tennis. Not only should you be in great shape, you also need an excellent racket! In this problem your task is to contribute to the development of tennis and to help to design a revolutionary new concept of a racket! The concept is a triangular racket. Ant it should be not just any triangle, but a regular one. As soon as you've chosen the shape, you need to stretch the net. By the time you came the rocket had _n_ holes drilled on each of its sides. The holes divide each side into equal _n_u2009+u20091 parts. At that, the _m_ closest to each apex holes on each side are made for better ventilation only and you cannot stretch the net through them. The next revolutionary idea as to stretch the net as obtuse triangles through the holes, so that for each triangle all apexes lay on different sides. Moreover, you need the net to be stretched along every possible obtuse triangle. That's where we need your help — help us to count the number of triangles the net is going to consist of. Two triangles are considered to be different if their pictures on the fixed at some position racket are different. Input The first and the only input line contains two integers _n_, _m_ . Output Print a single number — the answer to the problem. Note For the following picture _n_u2009=u20098, _m_u2009=u20092. White circles are the holes for ventilation, red circles — holes for net stretching. One of the possible obtuse triangles is painted red. | 2,700 | false | false | false | false | false | false | true | false | false | false | 8,604 |
1583B | Lord Omkar would like to have a tree with $$$n$$$ nodes ($$$3 le n le 10^5$$$) and has asked his disciples to construct the tree. However, Lord Omkar has created $$$m$$$ ($$$mathbf{1 le m < n}$$$) restrictions to ensure that the tree will be as heavenly as possible. A tree with $$$n$$$ nodes is an connected undirected graph with $$$n$$$ nodes and $$$n-1$$$ edges. Note that for any two nodes, there is exactly one simple path between them, where a simple path is a path between two nodes that does not contain any node more than once. Here is an example of a tree: A restriction consists of $$$3$$$ pairwise distinct integers, $$$a$$$, $$$b$$$, and $$$c$$$ ($$$1 le a,b,c le n$$$). It signifies that node $$$b$$$ cannot lie on the simple path between node $$$a$$$ and node $$$c$$$. Can you help Lord Omkar and become his most trusted disciple? You will need to find heavenly trees for multiple sets of restrictions. It can be shown that a heavenly tree will always exist for any set of restrictions under the given constraints. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 10^4$$$). Description of the test cases follows. The first line of each test case contains two integers, $$$n$$$ and $$$m$$$ ($$$3 leq n leq 10^5$$$, $$$mathbf{1 leq m < n}$$$), representing the size of the tree and the number of restrictions. The $$$i$$$-th of the next $$$m$$$ lines contains three integers $$$a_i$$$, $$$b_i$$$, $$$c_i$$$ ($$$1 le a_i, b_i, c_i le n$$$, $$$a$$$, $$$b$$$, $$$c$$$ are distinct), signifying that node $$$b_i$$$ cannot lie on the simple path between nodes $$$a_i$$$ and $$$c_i$$$. It is guaranteed that the sum of $$$n$$$ across all test cases will not exceed $$$10^5$$$. Output For each test case, output $$$n-1$$$ lines representing the $$$n-1$$$ edges in the tree. On each line, output two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u eq v$$$) signifying that there is an edge between nodes $$$u$$$ and $$$v$$$. Given edges have to form a tree that satisfies Omkar's restrictions. Example Input 2 7 4 1 2 3 3 4 5 5 6 7 6 5 4 5 3 1 2 3 2 3 4 3 4 5 Output 1 2 1 3 3 5 3 4 2 7 7 6 5 1 1 3 3 2 2 4 Note The output of the first sample case corresponds to the following tree: For the first restriction, the simple path between $$$1$$$ and $$$3$$$ is $$$1, 3$$$, which doesn't contain $$$2$$$. The simple path between $$$3$$$ and $$$5$$$ is $$$3, 5$$$, which doesn't contain $$$4$$$. The simple path between $$$5$$$ and $$$7$$$ is $$$5, 3, 1, 2, 7$$$, which doesn't contain $$$6$$$. The simple path between $$$6$$$ and $$$4$$$ is $$$6, 7, 2, 1, 3, 4$$$, which doesn't contain $$$5$$$. Thus, this tree meets all of the restrictions. The output of the second sample case corresponds to the following tree: | 1,200 | false | false | false | false | false | true | false | false | false | false | 2,716 |
87D | A long time ago in some country in Asia were civil wars. Each of _n_ cities wanted to seize power. That's why sometimes one city gathered an army and sent it to campaign against another city. Road making was difficult, so the country had few roads, exactly _n_u2009-u20091. Also you could reach any city from any other city going on those roads. Even during the war the Oriental people remain spiritually rich and appreciate the beauty of nature. And to keep the memory of this great crusade for the centuries to come, they planted one beautiful tree by the road on which the army spent most time. The Oriental people love nature, that's why if there were several such roads, then one tree was planted by each of them. Recently, when the records of the war were found, it became clear that each city attacked each other one exactly once. There were exactly _n_(_n_u2009-u20091) attacks in total. Everyone has been wondering what road after those wars became the most beautiful, that is, by which road they planted the largest number of beautiful trees. Input The first line contains an integer _n_ (2u2009≤u2009_n_u2009≤u2009105), which represents the number of cities. Next _n_u2009-u20091 lines contain three integers each: the numbers of cities _a__i_,u2009_b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_), connected by the _i_-th road and the number of days _d__i_ the army spends to go on it (1u2009≤u2009_d__i_u2009≤u2009109). The lengths of several roads may coincide. Output Print on the first line two integers — the number of beautiful trees on the most beautiful road and the number of the most beautiful roads. Print on the second line the list of the most beautiful roads in the sorted order by the numbers' increasing. The roads are numbered from 1 to _n_u2009-u20091 in the order in which they are given in the input data. Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use the cout stream (also you may use the %I64d specificator). Examples Input 6 1 2 1 1 3 5 3 4 2 3 5 3 3 6 4 | 2,300 | false | false | true | true | false | false | false | false | true | true | 9,533 |
719B | Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and red. There are _n_ cockroaches living in Anatoly's room. Anatoly just made all his cockroaches to form a single line. As he is a perfectionist, he would like the colors of cockroaches in the line to alternate. He has a can of black paint and a can of red paint. In one turn he can either swap any two cockroaches, or take any single cockroach and change it's color. Help Anatoly find out the minimum number of turns he needs to make the colors of cockroaches in the line alternate. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the number of cockroaches. The second line contains a string of length _n_, consisting of characters 'b' and 'r' that denote black cockroach and red cockroach respectively. Output Print one integerxa0— the minimum number of moves Anatoly has to perform in order to make the colors of cockroaches in the line to alternate. Note In the first sample, Anatoly has to swap third and fourth cockroaches. He needs 1 turn to do this. In the second sample, the optimum answer is to paint the second and the fourth cockroaches red. This requires 2 turns. In the third sample, the colors of cockroaches in the line are alternating already, thus the answer is 0. | 1,400 | false | true | false | false | false | false | false | false | false | false | 6,938 |
1415F | You live on a number line. You are initially (at time moment $$$t = 0$$$) located at point $$$x = 0$$$. There are $$$n$$$ events of the following type: at time $$$t_i$$$ a small cake appears at coordinate $$$x_i$$$. To collect this cake, you have to be at this coordinate at this point, otherwise the cake spoils immediately. No two cakes appear at the same time and no two cakes appear at the same coordinate. You can move with the speed of $$$1$$$ length unit per one time unit. Also, at any moment you can create a clone of yourself at the same point where you are located. The clone can't move, but it will collect the cakes appearing at this position for you. The clone disappears when you create another clone. If the new clone is created at time moment $$$t$$$, the old clone can collect the cakes that appear before or at the time moment $$$t$$$, and the new clone can collect the cakes that appear at or after time moment $$$t$$$. Can you collect all the cakes (by yourself or with the help of clones)? Input The first line contains a single integer $$$n$$$ ($$$1 le n le 5000$$$)xa0— the number of cakes. Each of the next $$$n$$$ lines contains two integers $$$t_i$$$ and $$$x_i$$$ ($$$1 le t_i le 10^9$$$, $$$-10^9 le x_i le 10^9$$$)xa0— the time and coordinate of a cake's appearance. All times are distinct and are given in increasing order, all the coordinates are distinct. Output Print "YES" if you can collect all cakes, otherwise print "NO". Note In the first example you should start moving towards $$$5$$$ right away, leaving a clone at position $$$1$$$ at time moment $$$1$$$, and collecting the cake at position $$$2$$$ at time moment $$$2$$$. At time moment $$$5$$$ you are at the position $$$5$$$ and collect a cake there, your clone collects the last cake at time moment $$$6$$$. In the second example you have to leave a clone at position $$$0$$$ and start moving towards position $$$5$$$. At time moment $$$1$$$ the clone collects a cake. At time moment $$$2$$$ you should create a new clone at the current position $$$2$$$, it will collect the last cake in future. You will collect the second cake at position $$$5$$$. In the third example there is no way to collect all cakes. | 2,900 | false | false | false | true | false | false | false | false | false | false | 3,603 |
1158F | Let $$$c$$$ be some positive integer. Let's call an array $$$a_1, a_2, ldots, a_n$$$ of positive integers $$$c$$$-array, if for all $$$i$$$ condition $$$1 leq a_i leq c$$$ is satisfied. Let's call $$$c$$$-array $$$b_1, b_2, ldots, b_k$$$ a subarray of $$$c$$$-array $$$a_1, a_2, ldots, a_n$$$, if there exists such set of $$$k$$$ indices $$$1 leq i_1 < i_2 < ldots < i_k leq n$$$ that $$$b_j = a_{i_j}$$$ for all $$$1 leq j leq k$$$. Let's define density of $$$c$$$-array $$$a_1, a_2, ldots, a_n$$$ as maximal non-negative integer $$$p$$$, such that any $$$c$$$-array, that contains $$$p$$$ numbers is a subarray of $$$a_1, a_2, ldots, a_n$$$. You are given a number $$$c$$$ and some $$$c$$$-array $$$a_1, a_2, ldots, a_n$$$. For all $$$0 leq p leq n$$$ find the number of sequences of indices $$$1 leq i_1 < i_2 < ldots < i_k leq n$$$ for all $$$1 leq k leq n$$$, such that density of array $$$a_{i_1}, a_{i_2}, ldots, a_{i_k}$$$ is equal to $$$p$$$. Find these numbers by modulo $$$998,244,353$$$, because they can be too large. Input The first line contains two integers $$$n$$$ and $$$c$$$, separated by spaces ($$$1 leq n, c leq 3,000$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$, separated by spaces ($$$1 leq a_i leq c$$$). Output Print $$$n + 1$$$ numbers $$$s_0, s_1, ldots, s_n$$$. $$$s_p$$$ should be equal to the number of sequences of indices $$$1 leq i_1 < i_2 < ldots < i_k leq n$$$ for all $$$1 leq k leq n$$$ by modulo $$$998,244,353$$$, such that the density of array $$$a_{i_1}, a_{i_2}, ldots, a_{i_k}$$$ is equal to $$$p$$$. Note In the first example, it's easy to see that the density of array will always be equal to its length. There exists $$$4$$$ sequences with one index, $$$6$$$ with two indices, $$$4$$$ with three and $$$1$$$ with four. In the second example, the only sequence of indices, such that the array will have non-zero density is all indices because in other cases there won't be at least one number from $$$1$$$ to $$$3$$$ in the array, so it won't satisfy the condition of density for $$$p geq 1$$$. | 3,500 | true | false | false | true | false | false | false | false | false | false | 4,910 |
1838D | There is a string $$$s$$$ of length $$$n$$$ consisting of the characters '(' and ')'. You are walking on this string. You start by standing on top of the first character of $$$s$$$, and you want to make a sequence of moves such that you end on the $$$n$$$-th character. In one step, you can move one space to the left (if you are not standing on the first character), or one space to the right (if you are not standing on the last character). You may not stay in the same place, however you may visit any character, including the first and last character, any number of times. At each point in time, you write down the character you are currently standing on. We say the string is walkable if there exists some sequence of moves that take you from the first character to the last character, such that the string you write down is a regular bracket sequence. A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. One possible valid walk on $$$s=mathtt{(())()))}$$$. The red dot indicates your current position, and the red string indicates the string you have written down. Note that the red string is a regular bracket sequence at the end of the process. You are given $$$q$$$ queries. Each query flips the value of a character from '(' to ')' or vice versa. After each query, determine whether the string is walkable. Queries are cumulative, so the effects of each query carry on to future queries. Input The first line of the input contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 2cdot 10^5$$$)xa0— the size of the string and the number of queries, respectively. The second line of the input contains a string $$$s$$$ of size $$$n$$$, consisting of the characters '(' and ')'xa0— the initial bracket string. Each of the next $$$q$$$ lines contains a single integer $$$i$$$ ($$$1le i le n$$$)xa0— the index of the character to flip for that query. Output For each query, print "YES" if the string is walkable after that query, and "NO" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Note In the first example: After the first query, the string is (())()()(). This string is a regular bracket sequence, so it is walkable by simply moving to the right. After the second query, the string is (())()))(). If you move right once, then left once, then walk right until you hit the end of the string, you produce the string (((())()))(), which is a regular bracket sequence. After the third query, the string is ()))()))(). We can show that this string is not walkable. In the second example, the strings after the queries are ()) and ()(, neither of which are walkable. | 2,100 | false | true | false | false | true | false | false | false | false | false | 1,261 |
712E | There are _n_ casinos lined in a row. If Memory plays at casino _i_, he has probability _p__i_ to win and move to the casino on the right (_i_u2009+u20091) or exit the row (if _i_u2009=u2009_n_), and a probability 1u2009-u2009_p__i_ to lose and move to the casino on the left (_i_u2009-u20091) or also exit the row (if _i_u2009=u20091). We say that Memory dominates on the interval _i_... _j_ if he completes a walk such that, He starts on casino _i_. He never looses in casino _i_. He finishes his walk by winning in casino _j_. Note that Memory can still walk left of the 1-st casino and right of the casino _n_ and that always finishes the process. Now Memory has some requests, in one of the following forms: 1 _i_ _a_ _b_: Set . 2 _l_ _r_: Print the probability that Memory will dominate on the interval _l_... _r_, i.e. compute the probability that Memory will first leave the segment _l_... _r_ after winning at casino _r_, if she starts in casino _l_. It is guaranteed that at any moment of time _p_ is a non-decreasing sequence, i.e. _p__i_u2009≤u2009_p__i_u2009+u20091 for all _i_ from 1 to _n_u2009-u20091. Please help Memory by answering all his requests! Input The first line of the input contains two integers _n_ and _q_(1u2009≤u2009_n_,u2009_q_u2009≤u2009100u2009000), xa0— number of casinos and number of requests respectively. The next _n_ lines each contain integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_u2009<u2009_b__i_u2009≤u2009109) xa0— is the probability _p__i_ of winning in casino _i_. The next _q_ lines each contain queries of one of the types specified above (1u2009≤u2009_a_u2009<u2009_b_u2009≤u2009109, 1u2009≤u2009_i_u2009≤u2009_n_, 1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_). It's guaranteed that there will be at least one query of type 2, i.e. the output will be non-empty. Additionally, it is guaranteed that _p_ forms a non-decreasing sequence at all times. Output Print a real number for every request of type 2xa0— the probability that boy will "dominate" on that interval. Your answer will be considered correct if its absolute error does not exceed 10u2009-u20094. Namely: let's assume that one of your answers is _a_, and the corresponding answer of the jury is _b_. The checker program will consider your answer correct if _a_u2009-u2009_b_u2009≤u200910u2009-u20094. Example Input 3 13 1 3 1 2 2 3 2 1 1 2 1 2 2 1 3 2 2 2 2 2 3 2 3 3 1 2 2 3 2 1 1 2 1 2 2 1 3 2 2 2 2 2 3 2 3 3 Output 0.3333333333 0.2000000000 0.1666666667 0.5000000000 0.4000000000 0.6666666667 0.3333333333 0.2500000000 0.2222222222 0.6666666667 0.5714285714 0.6666666667 | 2,500 | true | false | false | false | true | false | false | false | false | false | 6,967 |
1045C | In an unspecified solar system, there are $$$N$$$ planets. A space government company has recently hired space contractors to build $$$M$$$ bidirectional Hyperspace™ highways, each connecting two different planets. The primary objective, which was to make sure that every planet can be reached from any other planet taking only Hyperspace™ highways, has been completely fulfilled. Unfortunately, lots of space contractors had friends and cousins in the Space Board of Directors of the company, so the company decided to do much more than just connecting all planets. In order to make spending enormous amounts of space money for Hyperspace™ highways look neccessary, they decided to enforce a strict rule on the Hyperspace™ highway network: whenever there is a way to travel through some planets and return to the starting point without travelling through any planet twice, every pair of planets on the itinerary should be directly connected by a Hyperspace™ highway. In other words, the set of planets in every simple cycle induces a complete subgraph. You are designing a Hyperspace™ navigational app, and the key technical problem you are facing is finding the minimal number of Hyperspace™ highways one needs to use to travel from planet $$$A$$$ to planet $$$B$$$. As this problem is too easy for Bubble Cup, here is a harder task: your program needs to do it for $$$Q$$$ pairs of planets. Input The first line contains three positive integers $$$N$$$ ($$$1leq Nleq 100,000$$$), $$$M$$$ ($$$1leq Mleq 500,000$$$) and $$$Q$$$ ($$$1leq Qleq 200,000$$$), denoting the number of planets, the number of Hyperspace™ highways, and the number of queries, respectively. Each of the following M lines contains a highway: highway $$$i$$$ is given by two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 leq u_i < v_i leq N$$$), meaning the planets $$$u_i$$$ and $$$v_i$$$ are connected by a Hyperspace™ highway. It is guaranteed that the network of planets and Hyperspace™ highways forms a simple connected graph. Each of the following $$$Q$$$ lines contains a query: query $$$j$$$ is given by two integers $$$a_j$$$ and $$$b_j$$$ $$$(1 leq a_j < b_j leq N )$$$, meaning we are interested in the minimal number of Hyperspace™ highways one needs to take to travel from planet $$$a_j$$$ to planet $$$b_j$$$. Output Output $$$Q$$$ lines: the $$$j$$$-th line of output should contain the minimal number of Hyperspace™ highways one needs to take to travel from planet $$$a_j$$$ to planet $$$b_j$$$. Examples Input 5 7 2 1 2 1 3 1 4 2 3 2 4 3 4 1 5 1 4 2 5 Input 8 11 4 1 2 2 3 3 4 4 5 1 3 1 6 3 5 3 7 4 7 5 7 6 8 1 5 2 4 6 7 3 8 Note The graph from the second sample: | 2,300 | false | false | false | false | false | false | false | false | false | true | 5,468 |
986B | Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from $$$1$$$ to $$$n$$$ and then $$$3n$$$ times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements $$$7n+1$$$ times instead of $$$3n$$$ times. Because it is more random, OK?! You somehow get a test from one of these problems and now you want to know from which one. Input In the first line of input there is one integer $$$n$$$ ($$$10^{3} le n le 10^{6}$$$). In the second line there are $$$n$$$ distinct integers between $$$1$$$ and $$$n$$$xa0— the permutation of size $$$n$$$ from the test. It is guaranteed that all tests except for sample are generated this way: First we choose $$$n$$$xa0— the size of the permutation. Then we randomly choose a method to generate a permutationxa0— the one of Petr or the one of Alex. Then we generate a permutation using chosen method. Output If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes). Note Please note that the sample is not a valid test (because of limitations for $$$n$$$) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC. Due to randomness of input hacks in this problem are forbidden. | 1,800 | true | false | false | false | false | false | false | false | false | false | 5,771 |
1612B | A permutation of length $$$n$$$ is an array $$$p=[p_1,p_2,dots, p_n]$$$ which contains every integer from $$$1$$$ to $$$n$$$ (inclusive) exactly once. For example, $$$p=[4, 2, 6, 5, 3, 1]$$$ is a permutation of length $$$6$$$. You are given three integers $$$n$$$, $$$a$$$ and $$$b$$$, where $$$n$$$ is an even number. Print any permutation of length $$$n$$$ that the minimum among all its elements of the left half equals $$$a$$$ and the maximum among all its elements of the right half equals $$$b$$$. Print -1 if no such permutation exists. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 1000$$$), the number of test cases in the test. The following $$$t$$$ lines contain test case descriptions. Each test case description contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$2 le n le 100$$$; $$$1 le a,b le n$$$; $$$a e b$$$), where $$$n$$$ is an even number (i.e. $$$n bmod 2 = 0$$$). Output For each test case, print a single line containing any suitable permutation. Print -1 no such permutation exists. If there are multiple answers, print any of them. Example Input 7 6 2 5 6 1 3 6 4 3 4 2 4 10 5 3 2 1 2 2 2 1 Output 4 2 6 5 3 1 -1 6 4 5 1 3 2 3 2 4 1 -1 1 2 2 1 | 900 | false | true | false | false | false | true | false | false | false | false | 2,582 |
570B | One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from 1 to _n_. Let's assume that Misha chose number _m_, and Andrew chose number _a_. Then, by using a random generator they choose a random integer _c_ in the range between 1 and _n_ (any integer from 1 to _n_ is chosen with the same probability), after which the winner is the player, whose number was closer to _c_. The boys agreed that if _m_ and _a_ are located on the same distance from _c_, Misha wins. Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and number _n_. You need to determine which value of _a_ Andrew must choose, so that the probability of his victory is the highest possible. More formally, you need to find such integer _a_ (1u2009≤u2009_a_u2009≤u2009_n_), that the probability that is maximal, where _c_ is the equiprobably chosen integer from 1 to _n_ (inclusive). Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_m_u2009≤u2009_n_u2009≤u2009109) — the range of numbers in the game, and the number selected by Misha respectively. Output Print a single number — such value _a_, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them. Note In the first sample test: Andrew wins if _c_ is equal to 2 or 3. The probability that Andrew wins is 2u2009/u20093. If Andrew chooses _a_u2009=u20093, the probability of winning will be 1u2009/u20093. If _a_u2009=u20091, the probability of winning is 0. In the second sample test: Andrew wins if _c_ is equal to 1 and 2. The probability that Andrew wins is 1u2009/u20092. For other choices of _a_ the probability of winning is less. | 1,300 | true | true | true | false | false | true | false | false | false | false | 7,570 |
1912D | Daisy has recently learned divisibility rules for integers and she is fascinated by them. One of the tests she learned is a divisibility test by 3. You can find a sum of all digits of a decimal number and check if the resulting sum is divisible by 3. Moreover, the resulting sum of digits is congruent modulo 3 to the original numberxa0— the remainder modulo 3 is preserved. For example, $$$75 equiv 7 + 5 pmod 3$$$. Daisy is specifically interested in such remainder preserving divisibility tests. There are more examples like that that she learned for decimal integers (integers base 10): To test divisibility modulo 11, find an alternating sum of digits. Counting digits from the last (least significant) digit, add digits on odd positions (the last, 3rd to the last, etc) and subtract digits on even positions (2nd to the last, 4th to the last, etc) to get the sum that is congruent modulo 11 to the original number. For example, $$$123 equiv 1 - 2 + 3 pmod {11}$$$. To test divisibility modulo 4, keep the last two digits. Their value is congruent modulo 4 to the original number. For example, $$$876543 equiv 43 pmod 4$$$. To test divisibility modulo 7, find an alternating sum of groups of three digits. For example, $$$4389328 equiv 4 - 389 + 328 pmod 7$$$. Similar tests can be found in other bases. For example, to test divisibility modulo 5 for octal numbers (base 8), find an alternating sum of groups of two digits. For example, $$$1234_8 equiv -12_8 + 34_8 pmod 5$$$. Daisy wants to find such rules for a given base $$$b$$$. She is interested in three kinds of divisibility rules: Kind 1xa0— take the last $$$k$$$ digits of an integer in base $$$b$$$. Kind 2xa0— take a sum of groups of $$$k$$$ digits of an integer in base $$$b$$$. Kind 3xa0— take an alternating sum of groups of $$$k$$$ digits of an integer in base $$$b$$$. It is not always possible to find such a divisibility rule. For example, in base 10 there is no such test for divisibility modulo 6, even though different approaches to testing divisibility by 6 exist. Given base $$$b$$$ and modulo $$$n$$$, Daisy wants to know the smallest group size $$$k$$$ for which such a divisibility test exits. Input There are several tests in the input. The first line of the input contains an integer $$$t$$$xa0— the number of tests. The next $$$t$$$ lines describe the tests. Each test consists of a line with two integers $$$b$$$ and $$$n$$$xa0— the base and the modulo ($$$b, n ge 2$$$). The sum of all $$$b$$$ values in the input does not exceed $$$10^6$$$, and the sum of all $$$n$$$ values in the input does not exceed $$$10^6$$$. Output Write $$$t$$$ linesxa0— a line for each test in the input. On a line for a test write a single integer $$$0$$$ if the divisibility test for a given $$$b$$$ and $$$n$$$ does not exist. Otherwise, write two integers $$$a$$$ and $$$k$$$, where $$$a$$$ is the kind of the divisibility test (1, 2, or 3) and $$$k$$$ is the number of digits in a group for the test, such that $$$k$$$ is the lowest among all possible divisiblity tests for the given $$$b$$$ and $$$n$$$. | 1,900 | true | false | false | false | false | false | false | false | false | false | 834 |
1749E | Monocarp is playing Minecraft and wants to build a wall of cacti. He wants to build it on a field of sand of the size of $$$n imes m$$$ cells. Initially, there are cacti in some cells of the field. Note that, in Minecraft, cacti cannot grow on cells adjacent to each other by side — and the initial field meets this restriction. Monocarp can plant new cacti (they must also fulfil the aforementioned condition). He can't chop down any of the cacti that are already growing on the field — he doesn't have an axe, and the cacti are too prickly for his hands. Monocarp believes that the wall is complete if there is no path from the top row of the field to the bottom row, such that: each two consecutive cells in the path are adjacent by side; no cell belonging to the path contains a cactus. Your task is to plant the minimum number of cacti to build a wall (or to report that this is impossible). Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^3$$$)xa0— number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n, m le 2 cdot 10^5$$$; $$$n imes m le 4 cdot 10^5$$$)xa0— the number of rows and columns, respectively. Then $$$n$$$ rows follow, $$$i$$$-th row contains a string $$$s_i$$$ of length $$$m$$$, where $$$s_{i, j}$$$ is '#', if a cactus grows at the intersection of the $$$i$$$-th row and the $$$j$$$-th column. Otherwise, $$$s_{i, j}$$$ is '.'. The sum of $$$n imes m$$$ over all test cases does not exceed $$$4 cdot 10^5$$$. Output For each test case, print NO in the first line if it is impossible to build a cactus wall without breaking the rules. Otherwise, print YES in the first line, then print $$$n$$$ lines of $$$m$$$ characters eachxa0— the field itself, where the $$$j$$$-th character of the $$$i$$$-th line is equal to '#', if there is a cactus on the intersection of the $$$i$$$-th row and the $$$j$$$-th column, otherwise it is '.'. If there are multiple optimal answers, print any of them. Example Input 4 2 4 .#.. ..#. 3 3 #.# ... .#. 5 5 ..... ..... ..... ..... ..... 4 3 #.. .#. #.# ... Output YES .#.# #.#. NO YES ....# ...#. ..#.. .#... #.... YES #.. .#. #.# ... | 2,400 | false | false | false | false | false | true | false | false | false | true | 1,787 |
181B | Problem - 181B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search brute force *1300 No tag edit access → Contest materials such that point _B_ is the middle of segment _AC_. The groups of three points are considered unordered, that is, if point _B_ is the middle of segment _AC_, then groups (_A_,u2009_B_,u2009_C_) and (_C_,u2009_B_,u2009_A_) are considered the same. Input The first line contains a single integer _n_ (3u2009≤u2009_n_u2009≤u20093000) — the number of points. Next _n_ lines contain the points. The _i_-th line contains coordinates of the _i_-th point: two space-separated integers _x__i_,u2009_y__i_ (u2009-u20091000u2009≤u2009_x__i_,u2009_y__i_u2009≤u20091000). It is guaranteed that all given points are different. Output Print the single number — the answer to the problem. Examples Input 3 1 1 2 2 3 3 Output 1 Input 3 0 0 -1 0 0 1 Output 0 | 1,300 | false | false | false | false | false | false | true | true | false | false | 9,115 |
914B | Edogawa Conan got tired of solving cases, and invited his friend, Professor Agasa, over. They decided to play a game of cards. Conan has _n_ cards, and the _i_-th card has a number _a__i_ written on it. They take turns playing, starting with Conan. In each turn, the player chooses a card and removes it. Also, he removes all cards having a number strictly lesser than the number on the chosen card. Formally, if the player chooses the _i_-th card, he removes that card and removes the _j_-th card for all _j_ such that _a__j_u2009<u2009_a__i_. A player loses if he cannot make a move on his turn, that is, he loses if there are no cards left. Predict the outcome of the game, assuming both players play optimally. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009105)xa0— the number of cards Conan has. The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009105), where _a__i_ is the number on the _i_-th card. Output If Conan wins, print "Conan" (without quotes), otherwise print "Agasa" (without quotes). Note In the first example, Conan can just choose the card having number 7 on it and hence remove all the cards. After that, there are no cards left on Agasa's turn. In the second example, no matter which card Conan chooses, there will be one one card left, which Agasa can choose. After that, there are no cards left when it becomes Conan's turn again. | 1,200 | false | true | true | false | false | false | false | false | false | false | 6,082 |
1186E | Vus the Cossack has a field with dimensions $$$n imes m$$$, which consists of "0" and "1". He is building an infinite field from this field. He is doing this in this way: 1. He takes the current field and finds a new inverted field. In other words, the new field will contain "1" only there, where "0" was in the current field, and "0" there, where "1" was. 2. To the current field, he adds the inverted field to the right. 3. To the current field, he adds the inverted field to the bottom. 4. To the current field, he adds the current field to the bottom right. 5. He repeats it. For example, if the initial field was: $$$begin{matrix} 1 & 0 & 1 & 1 & end{matrix}$$$ After the first iteration, the field will be like this: $$$begin{matrix} 1 & 0 & 0 & 1 1 & 1 & 0 & 0 0 & 1 & 1 & 0 0 & 0 & 1 & 1 end{matrix}$$$ After the second iteration, the field will be like this: $$$begin{matrix} 1 & 0 & 0 & 1 & 0 & 1 & 1 & 0 1 & 1 & 0 & 0 & 0 & 0 & 1 & 1 0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 0 & 0 & 1 & 1 & 1 & 1 & 0 & 0 0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 0 & 0 & 1 & 1 & 1 & 1 & 0 & 0 1 & 0 & 0 & 1 & 0 & 1 & 1 & 0 1 & 1 & 0& 0 & 0 & 0 & 1 & 1 end{matrix}$$$ And so on... Let's numerate lines from top to bottom from $$$1$$$ to infinity, and columns from left to right from $$$1$$$ to infinity. We call the submatrix $$$(x_1, y_1, x_2, y_2)$$$ all numbers that have coordinates $$$(x, y)$$$ such that $$$x_1 leq x leq x_2$$$ and $$$y_1 leq y leq y_2$$$. The Cossack needs sometimes to find the sum of all the numbers in submatrices. Since he is pretty busy right now, he is asking you to find the answers! Input The first line contains three integers $$$n$$$, $$$m$$$, $$$q$$$ ($$$1 leq n, m leq 1,000$$$, $$$1 leq q leq 10^5$$$)xa0— the dimensions of the initial matrix and the number of queries. Each of the next $$$n$$$ lines contains $$$m$$$ characters $$$c_{ij}$$$ ($$$0 leq c_{ij} leq 1$$$)xa0— the characters in the matrix. Each of the next $$$q$$$ lines contains four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ ($$$1 leq x_1 leq x_2 leq 10^9$$$, $$$1 leq y_1 leq y_2 leq 10^9$$$)xa0— the coordinates of the upper left cell and bottom right cell, between which you need to find the sum of all numbers. Output For each query, print the answer. Examples Input 2 2 5 10 11 1 1 8 8 2 4 5 6 1 2 7 8 3 3 6 8 5 6 7 8 Input 2 3 7 100 101 4 12 5 17 5 4 9 4 1 4 13 18 12 1 14 9 3 10 7 18 3 15 12 17 8 6 8 12 Note The first example is explained in the legend. | 2,500 | true | false | true | false | false | false | false | false | false | false | 4,768 |
1970A1 | Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Helvetic Coding Contest 2024 online mirror (teams allowed, unrated) Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation sortings *1000 No tag edit access → Contest materials Announcement (en) Statements PDF (en) Tutorial PDF (en) Tutorial #2 (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST A1. Balanced Shuffle (Easy) time limit per test2 seconds memory limit per test256 megabytes A parentheses sequence is a string consisting of characters "(" and ")", for example "(()((". A balanced parentheses sequence is a parentheses sequence which can become a valid mathematical expression after inserting numbers and operations into it, for example "(()(()))". The balance of a parentheses sequence is defined as the number of opening parentheses "(" minus the number of closing parentheses ")". For example, the balance of the sequence "(()((" is 3. A balanced parentheses sequence can also be defined as a parentheses sequence with balance 0 such that each of its prefixes has a non-negative balance. We define the balanced shuffle operation that takes a parentheses sequence and returns a parentheses sequence as follows: first, for every character of the input sequence, we compute the balance of the prefix of the sequence before that character and write those down in a table together with the positions of the characters in the input sequence, for example: Prefix balance 0 1 2 1 2 3 2 1 Position 1 2 3 4 5 6 7 8 Character ( ( ) ( ( ) ) ) Then, we sort the columns of this table in increasing order of prefix balance, breaking ties in decreasing order of position. In the above example, we get: Prefix balance 0 1 1 1 2 2 2 3 Position 1 8 4 2 7 5 3 6 Character ( ) ( ( ) ( ) ) The last row of this table forms another parentheses sequence, in this case "()(()())". This sequence is called the result of applying the balanced shuffle operation to the input sequence, or in short just the balanced shuffle of the input sequence. You are given a balanced parentheses sequence. Print its balanced shuffle. Input The only line of input contains a string $$$s$$$ consisting only of characters "(" and ")". This string is guaranteed to be a non-empty balanced parentheses sequence with its length not exceeding $$$500,000$$$. Output Print the parentheses sequence $$$t$$$xa0— the balanced shuffle of $$$s$$$. Example input (()(())) output ()(()()) Codeforces (c) | 1,000 | false | false | true | false | false | false | false | false | true | false | 486 |
748E | Santa Claus has _n_ tangerines, and the _i_-th of them consists of exactly _a__i_ slices. Santa Claus came to a school which has _k_ pupils. Santa decided to treat them with tangerines. However, there can be too few tangerines to present at least one tangerine to each pupil. So Santa decided to divide tangerines into parts so that no one will be offended. In order to do this, he can divide a tangerine or any existing part into two smaller equal parts. If the number of slices in the part he wants to split is odd, then one of the resulting parts will have one slice more than the other. It's forbidden to divide a part consisting of only one slice. Santa Claus wants to present to everyone either a whole tangerine or exactly one part of it (that also means that everyone must get a positive number of slices). One or several tangerines or their parts may stay with Santa. Let _b__i_ be the number of slices the _i_-th pupil has in the end. Let Santa's joy be the minimum among all _b__i_'s. Your task is to find the maximum possible joy Santa can have after he treats everyone with tangerines (or their parts). Input The first line contains two positive integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009106, 1u2009≤u2009_k_u2009≤u20092·109) denoting the number of tangerines and the number of pupils, respectively. The second line consists of _n_ positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009107), where _a__i_ stands for the number of slices the _i_-th tangerine consists of. Output If there's no way to present a tangerine or a part of tangerine to everyone, print -1. Otherwise, print the maximum possible joy that Santa can have. Note In the first example Santa should divide the second tangerine into two parts with 5 and 4 slices. After that he can present the part with 5 slices to the first pupil and the whole first tangerine (with 5 slices, too) to the second pupil. In the second example Santa should divide both tangerines, so that he'll be able to present two parts with 6 slices and two parts with 7 slices. In the third example Santa Claus can't present 2 slices to 3 pupils in such a way that everyone will have anything. | 2,100 | false | true | false | false | true | false | false | true | false | false | 6,800 |
875D | Disclaimer: there are lots of untranslateable puns in the Russian version of the statement, so there is one more reason for you to learn Russian :) Rick and Morty like to go to the ridge High Cry for crying loudlyxa0— there is an extraordinary echo. Recently they discovered an interesting acoustic characteristic of this ridge: if Rick and Morty begin crying simultaneously from different mountains, their cry would be heard between these mountains up to the height equal the bitwise OR of mountains they've climbed and all the mountains between them. Bitwise OR is a binary operation which is determined the following way. Consider representation of numbers _x_ and _y_ in binary numeric system (probably with leading zeroes) _x_u2009=u2009_x__k_... _x_1_x_0 and _y_u2009=u2009_y__k_... _y_1_y_0. Then _z_u2009=u2009_x_xa0xa0_y_ is defined following way: _z_u2009=u2009_z__k_... _z_1_z_0, where _z__i_u2009=u20091, if _x__i_u2009=u20091 or _y__i_u2009=u20091, and _z__i_u2009=u20090 otherwise. In the other words, digit of bitwise OR of two numbers equals zero if and only if digits at corresponding positions is both numbers equals zero. For example bitwise OR of numbers 10u2009=u200910102 and 9u2009=u200910012 equals 11u2009=u200910112. In programming languages C/C++/Java/Python this operation is defined as «», and in Pascal as «or». Help Rick and Morty calculate the number of ways they can select two mountains in such a way that if they start crying from these mountains their cry will be heard above these mountains and all mountains between them. More formally you should find number of pairs _l_ and _r_ (1u2009≤u2009_l_u2009<u2009_r_u2009≤u2009_n_) such that bitwise OR of heights of all mountains between _l_ and _r_ (inclusive) is larger than the height of any mountain at this interval. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000), the number of mountains in the ridge. Second line contains _n_ integers _a__i_ (0u2009≤u2009_a__i_u2009≤u2009109), the heights of mountains in order they are located in the ridge. Output Print the only integer, the number of ways to choose two different mountains. Note In the first test case all the ways are pairs of mountains with the numbers (numbering from one): (1,u20094),u2009(1,u20095),u2009(2,u20093),u2009(2,u20094),u2009(2,u20095),u2009(3,u20094),u2009(3,u20095),u2009(4,u20095) In the second test case there are no such pairs because for any pair of mountains the height of cry from them is 3, and this height is equal to the height of any mountain. | 2,200 | false | false | false | false | true | false | false | true | false | false | 6,245 |
1012E | You are given an array of $$$n$$$ positive integers $$$a_1, a_2, dots, a_n$$$. You can perform the following operation any number of times: select several distinct indices $$$i_1, i_2, dots, i_k$$$ ($$$1 le i_j le n$$$) and move the number standing at the position $$$i_1$$$ to the position $$$i_2$$$, the number at the position $$$i_2$$$ to the position $$$i_3$$$, ..., the number at the position $$$i_k$$$ to the position $$$i_1$$$. In other words, the operation cyclically shifts elements: $$$i_1 o i_2 o ldots i_k o i_1$$$. For example, if you have $$$n=4$$$, an array $$$a_1=10, a_2=20, a_3=30, a_4=40$$$, and you choose three indices $$$i_1=2$$$, $$$i_2=1$$$, $$$i_3=4$$$, then the resulting array would become $$$a_1=20, a_2=40, a_3=30, a_4=10$$$. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number $$$s$$$. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers $$$n$$$ and $$$s$$$ ($$$1 leq n leq 200,000$$$, $$$0 leq s leq 200,000$$$)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$—elements of the array ($$$1 leq a_i leq 10^9$$$). Output If it's impossible to sort the array using cycles of total length not exceeding $$$s$$$, print a single number "-1" (quotes for clarity). Otherwise, print a single number $$$q$$$— the minimum number of operations required to sort the array. On the next $$$2 cdot q$$$ lines print descriptions of operations in the order they are applied to the array. The description of $$$i$$$-th operation begins with a single line containing one integer $$$k$$$ ($$$1 le k le n$$$)—the length of the cycle (that is, the number of selected indices). The next line should contain $$$k$$$ distinct integers $$$i_1, i_2, dots, i_k$$$ ($$$1 le i_j le n$$$)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to $$$s$$$, and the array should be sorted after applying these $$$q$$$ operations. If there are several possible answers with the optimal $$$q$$$, print any of them. Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle $$$1 o 4 o 1$$$ (of length 2), then apply the cycle $$$2 o 3 o 5 o 2$$$ (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 ($$$1 o 2 o 1$$$ and $$$3 o 4 o 3$$$). However, it's impossible to achieve the same using shorter cycles, which is required by $$$s=3$$$. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero. | 3,100 | true | false | false | false | false | false | false | false | false | false | 5,623 |
961G | You are given a set of _n_ elements indexed from 1 to _n_. The weight of _i_-th element is _w__i_. The weight of some subset of a given set is denoted as . The weight of some partition _R_ of a given set into _k_ subsets is (recall that a partition of a given set is a set of its subsets such that every element of the given set belongs to exactly one subset in partition). Calculate the sum of weights of all partitions of a given set into exactly _k_ non-empty subsets, and print it modulo 109u2009+u20097. Two partitions are considered different iff there exist two elements _x_ and _y_ such that they belong to the same set in one of the partitions, and to different sets in another partition. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u20092·105) — the number of elements and the number of subsets in each partition, respectively. The second line contains _n_ integers _w__i_ (1u2009≤u2009_w__i_u2009≤u2009109)— weights of elements of the set. Output Print one integer — the sum of weights of all partitions of a given set into _k_ non-empty subsets, taken modulo 109u2009+u20097. Note Possible partitions in the first sample: 1. {{1,u20092,u20093},u2009{4}}, _W_(_R_)u2009=u20093·(_w_1u2009+u2009_w_2u2009+u2009_w_3)u2009+u20091·_w_4u2009=u200924; 2. {{1,u20092,u20094},u2009{3}}, _W_(_R_)u2009=u200926; 3. {{1,u20093,u20094},u2009{2}}, _W_(_R_)u2009=u200924; 4. {{1,u20092},u2009{3,u20094}}, _W_(_R_)u2009=u20092·(_w_1u2009+u2009_w_2)u2009+u20092·(_w_3u2009+u2009_w_4)u2009=u200920; 5. {{1,u20093},u2009{2,u20094}}, _W_(_R_)u2009=u200920; 6. {{1,u20094},u2009{2,u20093}}, _W_(_R_)u2009=u200920; 7. {{1},u2009{2,u20093,u20094}}, _W_(_R_)u2009=u200926; Possible partitions in the second sample: 1. {{1,u20092,u20093,u20094},u2009{5}}, _W_(_R_)u2009=u200945; 2. {{1,u20092,u20093,u20095},u2009{4}}, _W_(_R_)u2009=u200948; 3. {{1,u20092,u20094,u20095},u2009{3}}, _W_(_R_)u2009=u200951; 4. {{1,u20093,u20094,u20095},u2009{2}}, _W_(_R_)u2009=u200954; 5. {{2,u20093,u20094,u20095},u2009{1}}, _W_(_R_)u2009=u200957; 6. {{1,u20092,u20093},u2009{4,u20095}}, _W_(_R_)u2009=u200936; 7. {{1,u20092,u20094},u2009{3,u20095}}, _W_(_R_)u2009=u200937; 8. {{1,u20092,u20095},u2009{3,u20094}}, _W_(_R_)u2009=u200938; 9. {{1,u20093,u20094},u2009{2,u20095}}, _W_(_R_)u2009=u200938; 10. {{1,u20093,u20095},u2009{2,u20094}}, _W_(_R_)u2009=u200939; 11. {{1,u20094,u20095},u2009{2,u20093}}, _W_(_R_)u2009=u200940; 12. {{2,u20093,u20094},u2009{1,u20095}}, _W_(_R_)u2009=u200939; 13. {{2,u20093,u20095},u2009{1,u20094}}, _W_(_R_)u2009=u200940; 14. {{2,u20094,u20095},u2009{1,u20093}}, _W_(_R_)u2009=u200941; 15. {{3,u20094,u20095},u2009{1,u20092}}, _W_(_R_)u2009=u200942. | 2,700 | true | false | false | false | false | false | false | false | false | false | 5,857 |
1896G | This is an interactive problem. There are $$$n^2$$$ pepes labeled $$$1, 2, ldots, n^2$$$ with pairwise distinct speeds. You would like to set up some races to find out the relative speed of these pepes. In one race, you can choose exactly $$$n$$$ distinct pepes and make them race against each other. After each race, you will only know the fastest pepe of these $$$n$$$ pepes. Can you order the $$$n^2-n+1$$$ fastest pepes in at most $$$2n^2 - 2n + 1$$$ races? Note that the slowest $$$n - 1$$$ pepes are indistinguishable from each other. Note that the interactor is adaptive. That is, the relative speeds of the pepes are not fixed in the beginning and may depend on your queries. But it is guaranteed that at any moment there is at least one initial configuration of pepes such that all the answers to the queries are consistent. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$2 le n le 20$$$)xa0— the number of pepes in one race. After reading the integer $$$n$$$ for each test case, you should begin the interaction. It is guaranteed that the sum of $$$n^3$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Interaction To set up a race, print a line with the following format: $$$mathtt{?},x_1,x_2 ldots x_n$$$ ($$$1 le x_i le n^2$$$, $$$x_i$$$ are pairwise distinct)xa0— the labels of the pepes in the race. After each race, you should read a line containing a single integer $$$p$$$ ($$$1le ple n^2$$$) — the label of the fastest pepe in the race. When you know the $$$n^2-n+1$$$ fastest pepes, print one line in the following format: $$$mathtt{!},p_1,p_2 ldots p_{n^2 - n + 1}$$$ ($$$1 le p_i le n^2$$$, $$$p_i$$$ are pairwise distinct) where $$$p$$$ is the sequence of these pepe's labels in descending order of speed. After that, move on to the next test case, or terminate the program if no more test cases are remaining. If your program performs more than $$$2n^2 - 2n + 1$$$ races for one test case or makes an invalid race, you may receive a Wrong Answer verdict. After printing a query do not forget to output the end of the 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 the documentation for other languages. Hack format For hacks, use the following format. The first line should contain $$$t$$$xa0— the number of test cases. The first line of each test case should contain an integer $$$n$$$ followed by the string manual. The second line of each test case should contain a permutation $$$a_1,a_2,ldots,a_{n^2}$$$ of the integers from $$$1$$$ to $$$n^2$$$. $$$a_i > a_j$$$ if and only if pepe $$$i$$$ has a faster speed than pepe $$$j$$$. As an example, the hack format for the example input is: $$$mathtt{1}mathtt{2}~mathtt{manual}mathtt{1}~mathtt{2}~mathtt{3}~mathtt{4}$$$ | 3,200 | false | false | true | false | false | true | false | false | true | false | 931 |
478A | There are five people playing a game called "Generosity". Each person gives some non-zero number of coins _b_ as an initial bet. After all players make their bets of _b_ coins, the following operation is repeated for several times: a coin is passed from one player to some other player. Your task is to write a program that can, given the number of coins each player has at the end of the game, determine the size _b_ of the initial bet or find out that such outcome of the game cannot be obtained for any positive number of coins _b_ in the initial bet. Input The input consists of a single line containing five integers _c_1,u2009_c_2,u2009_c_3,u2009_c_4 and _c_5 — the number of coins that the first, second, third, fourth and fifth players respectively have at the end of the game (0u2009≤u2009_c_1,u2009_c_2,u2009_c_3,u2009_c_4,u2009_c_5u2009≤u2009100). Output Print the only line containing a single positive integer _b_ — the number of coins in the initial bet of each player. If there is no such value of _b_, then print the only value "-1" (quotes for clarity). Note In the first sample the following sequence of operations is possible: 1. One coin is passed from the fourth player to the second player; 2. One coin is passed from the fourth player to the fifth player; 3. One coin is passed from the first player to the third player; 4. One coin is passed from the fourth player to the second player. | 1,100 | false | false | true | false | false | false | false | false | false | false | 7,921 |