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
766D
Mahmoud wants to write a new dictionary that contains _n_ words and relations between them. There are two types of relations: synonymy (i.xa0e. the two words mean the same) and antonymy (i.xa0e. the two words mean the opposite). From time to time he discovers a new relation between two words. He know that if two words have a relation between them, then each of them has relations with the words that has relations with the other. For example, if like means love and love is the opposite of hate, then like is also the opposite of hate. One more example: if love is the opposite of hate and hate is the opposite of like, then love means like, and so on. Sometimes Mahmoud discovers a wrong relation. A wrong relation is a relation that makes two words equal and opposite at the same time. For example if he knows that love means like and like is the opposite of hate, and then he figures out that hate means like, the last relation is absolutely wrong because it makes hate and like opposite and have the same meaning at the same time. After Mahmoud figured out many relations, he was worried that some of them were wrong so that they will make other relations also wrong, so he decided to tell every relation he figured out to his coder friend Ehab and for every relation he wanted to know is it correct or wrong, basing on the previously discovered relations. If it is wrong he ignores it, and doesn't check with following relations. After adding all relations, Mahmoud asked Ehab about relations between some words based on the information he had given to him. Ehab is busy making a Codeforces round so he asked you for help. Input The first line of input contains three integers _n_, _m_ and _q_ (2u2009≀u2009_n_u2009≀u2009105, 1u2009≀u2009_m_,u2009_q_u2009≀u2009105) where _n_ is the number of words in the dictionary, _m_ is the number of relations Mahmoud figured out and _q_ is the number of questions Mahmoud asked after telling all relations. The second line contains _n_ distinct words _a_1,u2009_a_2,u2009...,u2009_a__n_ consisting of small English letters with length not exceeding 20, which are the words in the dictionary. Then _m_ lines follow, each of them contains an integer _t_ (1u2009≀u2009_t_u2009≀u20092) followed by two different words _x__i_ and _y__i_ which has appeared in the dictionary words. If _t_u2009=u20091, that means _x__i_ has a synonymy relation with _y__i_, otherwise _x__i_ has an antonymy relation with _y__i_. Then _q_ lines follow, each of them contains two different words which has appeared in the dictionary. That are the pairs of words Mahmoud wants to know the relation between basing on the relations he had discovered. All words in input contain only lowercase English letters and their lengths don't exceed 20 characters. In all relations and in all questions the two words are different. Output First, print _m_ lines, one per each relation. If some relation is wrong (makes two words opposite and have the same meaning at the same time) you should print "NO" (without quotes) and ignore it, otherwise print "YES" (without quotes). After that print _q_ lines, one per each question. If the two words have the same meaning, output 1. If they are opposites, output 2. If there is no relation between them, output 3. See the samples for better understanding. Examples Input 3 3 4 hate love like 1 love like 2 love hate 1 hate like love like love hate like hate hate like Input 8 6 5 hi welcome hello ihateyou goaway dog cat rat 1 hi welcome 1 ihateyou goaway 2 hello ihateyou 2 hi goaway 2 hi hello 1 hi hello dog cat dog hi hi hello ihateyou goaway welcome ihateyou Output YES YES YES YES NO YES 3 3 1 1 2
2,000
false
false
false
true
true
false
false
false
false
true
6,720
1762A
An array $$$b$$$ is good if the sum of elements of $$$b$$$ is even. You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. In one operation, you can select an index $$$i$$$ and change $$$a_i := lfloor frac{a_i}{2} floor$$$. $$$^dagger$$$ Find the minimum number of operations (possibly $$$0$$$) needed to make $$$a$$$ good. It can be proven that it is always possible to make $$$a$$$ good. $$$^dagger$$$ $$$lfloor x floor$$$ denotes the floor function β€” the largest integer less than or equal to $$$x$$$. For example, $$$lfloor 2.7 floor = 2$$$, $$$lfloor pi floor = 3$$$ and $$$lfloor 5 floor =5$$$. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 50$$$) β€” the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ space-separated integers $$$a_1,a_2,ldots,a_n$$$ ($$$1 leq a_i leq 10^6$$$) β€” representing the array $$$a$$$. Do note that the sum of $$$n$$$ over all test cases is not bounded. Output For each test case, output the minimum number of operations needed to make $$$a$$$ good. Example Input 4 4 1 1 1 1 2 7 4 3 1 2 4 1 15 Note In the first test case, array $$$a$$$ is already good. In the second test case, we can perform on index $$$2$$$ twice. After the first operation, array $$$a$$$ becomes $$$[7,2]$$$. After performing on index $$$2$$$ again, $$$a$$$ becomes $$$[7,1]$$$, which is good. It can be proved that it is not possible to make $$$a$$$ good in less number of operations. In the third test case, $$$a$$$ becomes $$$[0,2,4]$$$ if we perform the operation on index $$$1$$$ once. As $$$[0,2,4]$$$ is good, answer is $$$1$$$. In the fourth test case, we need to perform the operation on index $$$1$$$ four times. After all operations, $$$a$$$ becomes $$$[0]$$$. It can be proved that it is not possible to make $$$a$$$ good in less number of operations.
800
true
true
false
false
false
false
false
false
false
false
1,740
1878G
After (finally) qualifying for the IOI 2023, wxhtzdy was very happy, so he decided to do what most competitive programmers do: trying to guess the problems that will be on IOI. During this process, he accidentally made a problem, which he thought was really cool. You are given a tree (a connected acyclic graph) with $$$n$$$ vertices and $$$n-1$$$ edges. Vertex $$$i$$$ ($$$1 le i le n$$$) has a value $$$a_i$$$. Lets' define $$$g(u, v)$$$ as the $$$, on the tree from the first test case in the example. On the path from $$$3$$$ to $$$4$$$ are vertices $$$3$$$, $$$1$$$, $$$4$$$. Then, $$$g(3, 4) = a_3 a_1 a_4$$$ (here, $$$$$$ represents the . Lets define the niceness of a vertex $$$z$$$ as the sum of the number of non-zero bits in $$$g(x, z)$$$ and the number of non-zero bits in $$$g(y, z)$$$. You need to find the maximum niceness among all vertices $$$z$$$ on the shortest path from $$$x$$$ to $$$y$$$. Since his brain is really tired after solving an output only problem on SIO (he had to do it to qualify for the IOI), he wants your help with this problem. Input The first line of input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β€” the number of vertices. The second line of each test case contains $$$n$$$ positive integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_v le 10^9$$$)xa0β€” the value of each vertex, the $$$i$$$-th integer in this line corresponds to the vertex $$$i$$$. Following $$$n - 1$$$ lines are the description of a tree. Each line contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n, u e v$$$)xa0β€” indicating that vertices $$$u$$$ and $$$v$$$ are connected by an edge. The next line contains a single integer $$$q$$$ ($$$1 le q le 10^5$$$)xa0β€” number of queries. Following $$$q$$$ lines contain 2 integers $$$x, y$$$ ($$$1 le x, y le n$$$)xa0β€” the vertices $$$x$$$ and $$$y$$$ for each query. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. It is guaranteed that the sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case output $$$q$$$ integers, each of which is the answer to the corresponding query. Examples Input 3 4 1 2 3 4 1 3 1 2 1 4 3 1 1 1 3 1 4 3 7 6 3 3 1 2 1 4 1 1 1 2 1 3 2 3 1 4 1 1 1 Input 3 7 4 7 7 4 10 8 10 6 1 3 1 2 1 7 4 1 5 4 2 4 7 5 2 3 4 5 2 5 6 9 5 6 2 4 6 5 1 2 1 1 6 4 3 1 3 4 6 1 1 4 4 3 3 5 7 5 1 3 7 5 1 6 2 1 5 4 2 3 3 4 7 6 6 3 2 4 2 7 7 Output 8 6 7 7 6 6 4 7 6 4 Input 1 7 6 8 7 2 5 8 7 2 1 3 2 4 3 4 6 4 5 6 7 4 1 5 6 7 4 5 1 4 Note The image below shows the tree from the second example, first test case. Tree from the second example, first test case In the first query, we have $$$x=7$$$, $$$y=5$$$. The shortest path from $$$7$$$ to $$$5$$$ is $$$7-4-2-1-5$$$. Let's calculate the niceness of vertex $$$7$$$ on this path. We have $$$g(7,7)=a_7=10=(1010)_2$$$ and $$$g(5,7)=a_5 a_1 a_2 a_4 a_7=10 4 7 4 10=15=(1111)_2$$$, so its niceness is equal to $$$2 + 4 = 6$$$. Now let's calculate the niceness of vertex $$$4$$$ on this path. We have $$$g(7,4)=a_7 a_4=10 4=14=(1110)_2$$$ and $$$g(5,4)=a_5 a_1 a_2 a_4=10 4 7 4=15=(1111)_2$$$, so its niceness is equal to $$$3 + 4 = 7$$$. Now let's calculate the niceness of vertex $$$2$$$ on this path. We have $$$g(7,2)=a_7 a_4 a_2=10 4 7=15=(1111)_2$$$ and $$$g(5,2)=a_5 a_1 a_2=10 4 7=15=(1111)_2$$$, so its niceness is equal to $$$4 + 4 = 8$$$. Now let's calculate the niceness of vertex $$$1$$$ on this path. We have $$$g(7,1)=a_7 a_4 a_2 a_1=10 4 7 4=15=(1111)_2$$$ and $$$g(5,1)=a_5 a_1=10 4=14=(1110)_2$$$, so its niceness is equal to $$$4 + 3 = 7$$$. Finally, let's calculate the niceness of vertex $$$5$$$ on this path. We have $$$g(7,5)=a_7 a_4 a_2 a_1 a_5=10 4 7 4 10=15=(1111)_2$$$ and $$$g(5,5)=a_5=10=(1010)_2$$$, so its niceness is equal to $$$4 + 2 = 6$$$. The maximum niceness on this path is at vertex $$$2$$$, and it is $$$8$$$.
2,300
false
false
true
false
true
false
true
true
false
false
1,011
2033E
Sakurako's exams are over, and she did excellently. As a reward, she received a permutation $$$p$$$. Kosuke was not entirely satisfied because he failed one exam and did not receive a gift. He decided to sneak into her room (thanks to the code for her lock) and spoil the permutation so that it becomes simple. A permutation $$$p$$$ is considered simple if for every $$$i$$$ $$$(1le i le n)$$$ one of the following conditions holds: $$$p_i=i$$$ $$$p_{p_i}=i$$$ For example, the permutations $$$[1, 2, 3, 4]$$$, $$$[5, 2, 4, 3, 1]$$$, and $$$[2, 1]$$$ are simple, while $$$[2, 3, 1]$$$ and $$$[5, 2, 1, 4, 3]$$$ are not. In one operation, Kosuke can choose indices $$$i,j$$$ $$$(1le i,jle n)$$$ and swap the elements $$$p_i$$$ and $$$p_j$$$. Sakurako is about to return home. Your task is to calculate the minimum number of operations that Kosuke needs to perform to make the permutation simple. Input The first line contains one integer $$$t$$$ ($$$1le tle 10^4$$$)xa0β€” the number of test cases. Each test case is described by two lines. The first line contains one integer $$$n$$$ ($$$1le n le 10^6$$$)xa0β€” the length of the permutation $$$p$$$. The second line contains $$$n$$$ integers $$$p_i$$$ ($$$1le p_ile n$$$)xa0β€” the elements of the permutation $$$p$$$. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$10^6$$$. It is guaranteed that $$$p$$$ is a permutation. Note In the first and second examples, the permutations are already simple. In the fourth example, it is sufficient to swap $$$p_2$$$ and $$$p_4$$$. Thus, the permutation will become $$$[2, 1, 4, 3]$$$ in $$$1$$$ operation.
1,400
true
true
false
false
true
false
true
false
false
true
64
1827D
You are given a tree (an undirected connected acyclic graph) which initially only contains vertex $$$1$$$. There will be several queries to the given tree. In the $$$i$$$-th query, vertex $$$i + 1$$$ will appear and be connected to vertex $$$p_i$$$ ($$$1 le p_i le i$$$). After each query, please find out the least number of operations required to make the current tree has two centroids. In one operation, you can add one vertex and one edge to the tree such that it remains a tree. A vertex is called a centroid if its removal splits the tree into subtrees with at most $$$lfloor frac{n}{2} floor$$$ vertices each, with $$$n$$$ as the number of vertices of the tree. For example, the centroid of the following tree is $$$3$$$ because the biggest subtree after removing the centroid has $$$2$$$ vertices. In the next tree, vertex $$$1$$$ and $$$2$$$ are both centroids. 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 a single integer $$$n$$$ ($$$2 le n le 5 cdot 10^{5}$$$)xa0β€” the number of nodes of the final tree. The second line of each test case contains $$$n - 1$$$ integers $$$p_1, p_2, ldots, p_{n - 1}$$$ ($$$1 le p_i le i$$$)xa0β€” the index of the vertex that is connected to vertex $$$i + 1$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^{5}$$$. Output For each test case, output $$$n - 1$$$ integers. The $$$i$$$-th integer is the answer to the $$$i$$$-th queryxa0β€” the least number of operations required to make the current tree have two centroids. We can show that an answer always exists. Example Input 5 2 1 3 1 1 4 1 2 3 7 1 2 3 2 5 2 10 1 2 2 4 5 5 7 8 9 Output 0 0 1 0 1 0 0 1 0 1 2 3 0 1 2 1 0 1 0 1 2 Note The illustrations below are of the fourth example test case. After the third query: The tree already has vertices $$$2$$$ and $$$3$$$ as centroids, so no operations are needed. After the fourth query: Adding vertex $$$x$$$ to the tree makes vertices $$$2$$$ and $$$3$$$ centroids. Only one operation is needed. After the fifth query: Adding vertex $$$x$$$ and $$$y$$$ to the tree makes vertices $$$5$$$ and $$$2$$$ centroids. Two operations are needed. After the sixth query: Adding vertex $$$x$$$, $$$y$$$, and $$$z$$$ to the tree makes vertices $$$5$$$ and $$$2$$$ centroids. Three operations are needed.
2,800
false
true
false
false
true
false
false
false
false
false
1,319
868B
Have you ever tried to explain to the coordinator, why it is eight hours to the contest and not a single problem has been prepared yet? Misha had. And this time he has a really strong excuse: he faced a space-time paradox! Space and time replaced each other. The entire universe turned into an enormous clock face with three handsxa0β€” hour, minute, and second. Time froze, and clocks now show the time _h_ hours, _m_ minutes, _s_ seconds. Last time Misha talked with the coordinator at _t_1 o'clock, so now he stands on the number _t_1 on the clock face. The contest should be ready by _t_2 o'clock. In the terms of paradox it means that Misha has to go to number _t_2 somehow. Note that he doesn't have to move forward only: in these circumstances time has no direction. Clock hands are very long, and Misha cannot get round them. He also cannot step over as it leads to the collapse of space-time. That is, if hour clock points 12 and Misha stands at 11 then he cannot move to 1 along the top arc. He has to follow all the way round the clock center (of course, if there are no other hands on his way). Given the hands' positions, _t_1, and _t_2, find if Misha can prepare the contest on time (or should we say on space?). That is, find if he can move from _t_1 to _t_2 by the clock face. Input Five integers _h_, _m_, _s_, _t_1, _t_2 (1u2009≀u2009_h_u2009≀u200912, 0u2009≀u2009_m_,u2009_s_u2009≀u200959, 1u2009≀u2009_t_1,u2009_t_2u2009≀u200912, _t_1u2009β‰ u2009_t_2). Misha's position and the target time do not coincide with the position of any hand. Output Print "YES" (quotes for clarity), if Misha can prepare the contest on time, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Note The three examples are shown on the pictures below from left to right. The starting position of Misha is shown with green, the ending position is shown with pink. Note that the positions of the hands on the pictures are not exact, but are close to the exact and the answer is the same.
1,400
false
false
true
false
false
false
false
false
false
false
6,272
1028D
Let's consider a simplified version of order book of some stock. The order book is a list of orders (offers) from people that want to buy or sell one unit of the stock, each order is described by direction (BUY or SELL) and price. At every moment of time, every SELL offer has higher price than every BUY offer. In this problem no two ever existed orders will have the same price. The lowest-price SELL order and the highest-price BUY order are called the best offers, marked with black frames on the picture below. The presented order book says that someone wants to sell the product at price $$$12$$$ and it's the best SELL offer because the other two have higher prices. The best BUY offer has price $$$10$$$. There are two possible actions in this orderbook: 1. Somebody adds a new order of some direction with some price. 2. Somebody accepts the best possible SELL or BUY offer (makes a deal). It's impossible to accept not the best SELL or BUY offer (to make a deal at worse price). After someone accepts the offer, it is removed from the orderbook forever. It is allowed to add new BUY order only with prices less than the best SELL offer (if you want to buy stock for higher price, then instead of adding an order you should accept the best SELL offer). Similarly, one couldn't add a new SELL order with price less or equal to the best BUY offer. For example, you can't add a new offer "SELL $$$20$$$" if there is already an offer "BUY $$$20$$$" or "BUY $$$25$$$"xa0β€” in this case you just accept the best BUY offer. You have a damaged order book log (in the beginning the are no orders in book). Every action has one of the two types: 1. "ADD $$$p$$$" denotes adding a new order with price $$$p$$$ and unknown direction. The order must not contradict with orders still not removed from the order book. 2. "ACCEPT $$$p$$$" denotes accepting an existing best offer with price $$$p$$$ and unknown direction. The directions of all actions are lost. Information from the log isn't always enough to determine these directions. Count the number of ways to correctly restore all ADD action directions so that all the described conditions are satisfied at any moment. Since the answer could be large, output it modulo $$$10^9 + 7$$$. If it is impossible to correctly restore directions, then output $$$0$$$. Input The first line contains an integer $$$n$$$ ($$$1 le n le 363,304$$$) β€” the number of actions in the log. Each of the next $$$n$$$ lines contains a string "ACCEPT" or "ADD" and an integer $$$p$$$ ($$$1 le p le 308,983,066$$$), describing an action type and price. All ADD actions have different prices. For ACCEPT action it is guaranteed that the order with the same price has already been added but has not been accepted yet. Note In the first example each of orders may be BUY or SELL. In the second example the order with price $$$1$$$ has to be BUY order, the order with the price $$$3$$$ has to be SELL order.
2,100
false
true
false
false
true
false
false
false
false
false
5,566
1625C
The Government of Mars is not only interested in optimizing space flights, but also wants to improve the road system of the planet. One of the most important highways of Mars connects Olymp City and Kstolop, the capital of Cydonia. In this problem, we only consider the way from Kstolop to Olymp City, but not the reverse path (i.xa0e. the path from Olymp City to Kstolop). The road from Kstolop to Olymp City is $$$ell$$$ kilometers long. Each point of the road has a coordinate $$$x$$$ ($$$0 le x le ell$$$), which is equal to the distance from Kstolop in kilometers. So, Kstolop is located in the point with coordinate $$$0$$$, and Olymp City is located in the point with coordinate $$$ell$$$. There are $$$n$$$ signs along the road, $$$i$$$-th of which sets a speed limit $$$a_i$$$. This limit means that the next kilometer must be passed in $$$a_i$$$ minutes and is active until you encounter the next along the road. There is a road sign at the start of the road (i.xa0e. in the point with coordinate $$$0$$$), which sets the initial speed limit. If you know the location of all the signs, it's not hard to calculate how much time it takes to drive from Kstolop to Olymp City. Consider an example: Here, you need to drive the first three kilometers in five minutes each, then one kilometer in eight minutes, then four kilometers in three minutes each, and finally the last two kilometers must be passed in six minutes each. Total time is $$$3cdot 5 + 1cdot 8 + 4cdot 3 + 2cdot 6 = 47$$$ minutes. To optimize the road traffic, the Government of Mars decided to remove no more than $$$k$$$ road signs. It cannot remove the sign at the start of the road, otherwise, there will be no limit at the start. By removing these signs, the Government also wants to make the time needed to drive from Kstolop to Olymp City as small as possible. The largest industrial enterprises are located in Cydonia, so it's the priority task to optimize the road traffic from Olymp City. So, the Government of Mars wants you to remove the signs in the way described above. Input The first line contains three integers $$$n$$$, $$$ell$$$, $$$k$$$ ($$$1 le n le 500$$$, $$$1 le ell le 10^5$$$, $$$0 le k le n-1$$$), the amount of signs on the road, the distance between the cities and the maximal number of signs you may remove. The second line contains $$$n$$$ integers $$$d_i$$$ ($$$d_1 = 0$$$, $$$d_i < d_{i+1}$$$, $$$0 le d_i le ell - 1$$$) β€” coordinates of all signs. The third line contains $$$n$$$ integers $$$a_i$$$ ($$$1 le a_i le 10^4$$$) β€” speed limits. Note In the first example, you cannot remove the signs. So the answer is $$$47$$$, as it's said in the statements above. In the second example, you may remove the second and the fourth sign. In this case, you need to drive four kilometers in $$$4cdot5 = 20$$$ minutes, and then six kilometers in $$$6cdot3 = 18$$$, so the total time is $$$4cdot5 + 6cdot3 = 38$$$ minutes.
1,700
false
false
false
true
false
false
false
false
false
false
2,491
596A
After making bad dives into swimming pools, Wilbur wants to build a swimming pool in the shape of a rectangle in his backyard. He has set up coordinate axes, and he wants the sides of the rectangle to be parallel to them. Of course, the area of the rectangle must be positive. Wilbur had all four vertices of the planned pool written on a paper, until his friend came along and erased some of the vertices. Now Wilbur is wondering, if the remaining _n_ vertices of the initial rectangle give enough information to restore the area of the planned swimming pool. Input The first line of the input contains a single integer _n_ (1u2009≀u2009_n_u2009≀u20094)xa0β€” the number of vertices that were not erased by Wilbur's friend. Each of the following _n_ lines contains two integers _x__i_ and _y__i_ (u2009-u20091000u2009≀u2009_x__i_,u2009_y__i_u2009≀u20091000)xa0β€”the coordinates of the _i_-th vertex that remains. Vertices are given in an arbitrary order. It's guaranteed that these points are distinct vertices of some rectangle, that has positive area and which sides are parallel to the coordinate axes. Output Print the area of the initial rectangle if it could be uniquely determined by the points remaining. Otherwise, print u2009-u20091. Note In the first sample, two opposite corners of the initial rectangle are given, and that gives enough information to say that the rectangle is actually a unit square. In the second sample there is only one vertex left and this is definitely not enough to uniquely define the area.
1,100
false
false
true
false
false
false
false
false
false
false
7,465
1674C
You are given a string $$$s$$$, consisting only of Latin letters 'a', and a string $$$t$$$, consisting of lowercase Latin letters. In one move, you can replace any letter 'a' in the string $$$s$$$ with a string $$$t$$$. Note that after the replacement string $$$s$$$ might contain letters other than 'a'. You can perform an arbitrary number of moves (including zero). How many different strings can you obtain? Print the number, or report that it is infinitely large. Two strings are considered different if they have different length, or they differ at some index. Input The first line contains a single integer $$$q$$$ ($$$1 le q le 10^4$$$)xa0β€” the number of testcases. The first line of each testcase contains a non-empty string $$$s$$$, consisting only of Latin letters 'a'. The length of $$$s$$$ doesn't exceed $$$50$$$. The second line contains a non-empty string $$$t$$$, consisting of lowercase Latin letters. The length of $$$t$$$ doesn't exceed $$$50$$$. Output For each testcase, print the number of different strings $$$s$$$ that can be obtained after an arbitrary amount of moves (including zero). If the number is infinitely large, print -1. Otherwise, print the number. Note In the first example, you can replace any letter 'a' with the string "a", but that won't change the string. So no matter how many moves you make, you can't obtain a string other than the initial one. In the second example, you can replace the second letter 'a' with "abc". String $$$s$$$ becomes equal to "aabc". Then the second letter 'a' again. String $$$s$$$ becomes equal to "aabcbc". And so on, generating infinitely many different strings. In the third example, you can either leave string $$$s$$$ as is, performing zero moves, or replace the only 'a' with "b". String $$$s$$$ becomes equal to "b", so you can't perform more moves on it.
1,000
false
false
true
false
false
false
false
false
false
false
2,227
1776J
An Italian data center consists of a set of servers, each colored green, white, or red, and a set of wires connecting them. Each wire connects two distinct servers and two servers are connected by at most one wire. Additionally, the data center is connected, i.e. there is a way to transmit information between any two servers through a sequence of wires. To judge all of the contestant submissions, SWERC has an Italian data center. Since every year the number of contestants doubles, the data center needs to grow to adapt to the extra load. To address this, SWERC builds a new data center based on the previous year's one by following these steps: For each server $$$s$$$ in the old data center, the new data center contains two servers $$$s_1$$$ and $$$s_2$$$ of the same color as $$$s$$$. A wire is placed connecting the two servers $$$s_1$$$ and $$$s_2$$$. For each wire connecting servers $$$s$$$ and $$$t$$$ in the old data center: if $$$s$$$ and $$$t$$$ have the same color, then a wire is placed in the new data center connecting $$$s_1$$$ and $$$t_1$$$ and another wire connecting $$$s_2$$$ and $$$t_2$$$; otherwise, a wire is placed in the new data center connecting $$$s_1$$$ and $$$t_2$$$ and another one connecting $$$s_2$$$ and $$$t_1$$$. One can show that if the old data center is connected than the new data center is also connected. You are given the Italian data center that SWERC currently has, which contains $$$n$$$ servers (indexed by $$$1, , 2, , dots, , n$$$) and $$$m$$$ wires connecting them. The organization wants to know how good their data center will be after $$$k$$$ years, so you should determine the diameter of the data center SWERC will have in $$$k$$$ years. The diameter of the data center is the largest distance between any two servers, i.e. the shortest number of wires that have to be used to transmit something between the two servers. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 leq n leq 100$$$, $$$n - 1 leq m leq n (n - 1) / 2$$$, $$$0 leq k leq 100$$$) β€” the number of servers, the number of wires, and the number of years to consider. The second line contains $$$n$$$ integers $$$c_1, , c_2, , dots, , c_n$$$ ($$$1 leq c_i leq 3$$$) β€” $$$c_i$$$ is the color of server $$$i$$$ (where $$$1$$$ stands for green, $$$2$$$ for white and $$$3$$$ for red). Each of the next $$$m$$$ lines contains two integers $$$s_i$$$ and $$$t_i$$$ ($$$1 leq s_i, t_i leq n$$$) β€” the two servers the $$$i$$$-th wire connects. It is guaranteed that the data center is connected, the endpoints of each wire are distinct, and that there are no repeated wires. Output Print the diameter of SWERC's data center after $$$k$$$ years. Examples Input 3 3 0 1 2 3 1 2 2 3 3 1 Input 3 3 1 1 2 3 1 2 2 3 3 1 Input 3 3 2 1 2 1 1 2 2 3 3 1 Input 8 14 100 3 3 2 2 1 2 2 1 2 7 1 5 7 8 4 6 2 8 1 8 2 6 6 7 1 6 1 4 3 5 1 3 4 5 5 7 Note In the first sample, the Italian data center is the following: The distance between any pair of servers is $$$1$$$ so the diameter is $$$1$$$. In the second sample, the initial Italian data center is the one from the first sample. After one year we obtain the following (where the numbers indicate which copy the server refers to): Consider the highlighted servers. The distance between them is $$$2$$$ and there is no pair of servers with greater distance, so the diameter is $$$2$$$. In the third sample, the data center after one year is the following: After one more year: Consider the highlighted servers. The distance between them is $$$3$$$ and there is no pair of servers with greater distance, so the diameter is $$$3$$$.
2,500
false
false
false
false
false
false
false
false
false
true
1,619
370C
A Christmas party in city S. had _n_ children. All children came in mittens. The mittens can be of different colors, but each child had the left and the right mitten of the same color. Let's say that the colors of the mittens are numbered with integers from 1 to _m_, and the children are numbered from 1 to _n_. Then the _i_-th child has both mittens of color _c__i_. The Party had Santa Claus ('Father Frost' in Russian), his granddaughter Snow Girl, the children danced around the richly decorated Christmas tree. In fact, everything was so bright and diverse that the children wanted to wear mittens of distinct colors. The children decided to swap the mittens so that each of them got one left and one right mitten in the end, and these two mittens were of distinct colors. All mittens are of the same size and fit all the children. The children started exchanging the mittens haphazardly, but they couldn't reach the situation when each child has a pair of mittens of distinct colors. Vasily Petrov, the dad of one of the children, noted that in the general case the children's idea may turn out impossible. Besides, he is a mathematician and he came up with such scheme of distributing mittens that the number of children that have distinct-colored mittens was maximum. You task is to repeat his discovery. Note that the left and right mittens are different: each child must end up with one left and one right mitten. Input The first line contains two integers _n_ and _m_ β€” the number of the children and the number of possible mitten colors (1u2009≀u2009_n_u2009≀u20095000, 1u2009≀u2009_m_u2009≀u2009100). The second line contains _n_ integers _c_1,u2009_c_2,u2009... _c__n_, where _c__i_ is the color of the mittens of the _i_-th child (1u2009≀u2009_c__i_u2009≀u2009_m_). Output In the first line, print the maximum number of children who can end up with a distinct-colored pair of mittens. In the next _n_ lines print the way the mittens can be distributed in this case. On the _i_-th of these lines print two space-separated integers: the color of the left and the color of the right mitten the _i_-th child will get. If there are multiple solutions, you can print any of them. Examples Output 6 2 1 1 2 2 1 1 3 1 2 3 1
1,800
false
true
false
false
false
true
false
false
true
false
8,351
2018F1
This is the easy version of the problem. In the three versions, the constraints on $$$n$$$ and the time limit are different. You can make hacks only if all the versions of the problem are solved. This is the statement of Problem D1B: There are $$$n$$$ cities in a row, numbered $$$1, 2, ldots, n$$$ left to right. At time $$$1$$$, you conquer exactly one city, called the starting city. At time $$$2, 3, ldots, n$$$, you can choose a city adjacent to the ones conquered so far and conquer it. You win if, for each $$$i$$$, you conquer city $$$i$$$ at a time no later than $$$a_i$$$. A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win? For each $$$0 leq k leq n$$$, count the number of arrays of positive integers $$$a_1, a_2, ldots, a_n$$$ such that $$$1 leq a_i leq n$$$ for each $$$1 leq i leq n$$$; the answer to Problem D1B is $$$k$$$. The answer can be very large, so you have to calculate it modulo a given prime $$$p$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 80$$$). The description of the test cases follows. The only line of each test case contains two integers $$$n$$$, $$$p$$$ ($$$1 le n le 80$$$, $$$10^8 leq p leq 10^9$$$, $$$p$$$ is prime)xa0β€” the number of cities and the modulo. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$80$$$. Output For each test case, output $$$n+1$$$ integers: the $$$i$$$-th integer should be the number of arrays that satisfy the conditions for $$$k = i-1$$$. Example Input 11 1 998244353 2 998244353 3 998244353 4 998244353 5 998244353 6 998244353 7 998244353 8 998244353 9 998244353 10 102275857 10 999662017 Output 0 1 1 2 1 14 7 4 2 183 34 19 16 4 2624 209 112 120 48 12 42605 1546 793 992 468 216 36 785910 13327 6556 9190 4672 2880 864 144 16382863 130922 61939 94992 50100 36960 14256 4608 576 382823936 1441729 657784 1086596 583344 488700 216000 96480 23040 2880 20300780 17572114 7751377 13641280 7376068 6810552 3269700 1785600 576000 144000 14400 944100756 17572114 7751377 13641280 7376068 6810552 3269700 1785600 576000 144000 14400 Note In the first test case, arrays with $$$1$$$ good starting city: $$$[1]$$$. In the second test case, arrays with $$$0$$$ good starting cities: $$$[1, 1]$$$; arrays with $$$1$$$ good starting city: $$$[1, 2]$$$, $$$[2, 1]$$$; arrays with $$$2$$$ good starting cities: $$$[2, 2]$$$. In the third test case, arrays with $$$0$$$ good starting cities: $$$[1, 1, 1]$$$, $$$[1, 1, 2]$$$, $$$[1, 1, 3]$$$, $$$[1, 2, 1]$$$, $$$[1, 2, 2]$$$, $$$[1, 3, 1]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 1]$$$, $$$[2, 1, 2]$$$, $$$[2, 2, 1]$$$, $$$[2, 2, 2]$$$, $$$[2, 3, 1]$$$, $$$[2, 3, 2]$$$, $$$[3, 1, 1]$$$; arrays with $$$1$$$ good starting city: $$$[1, 2, 3]$$$, $$$[1, 3, 3]$$$, $$$[2, 1, 3]$$$, $$$[3, 1, 2]$$$, $$$[3, 1, 3]$$$, $$$[3, 2, 1]$$$, $$$[3, 3, 1]$$$; arrays with $$$2$$$ good starting cities: $$$[2, 2, 3]$$$, $$$[2, 3, 3]$$$, $$$[3, 2, 2]$$$, $$$[3, 3, 2]$$$; arrays with $$$3$$$ good starting cities: $$$[3, 2, 3]$$$, $$$[3, 3, 3]$$$.
2,900
true
true
false
true
false
false
false
false
false
false
157
730E
All-Berland programming contest comes to an end. In total, _n_ teams participated in it. Like in ACM-ICPC, current results stopped refreshing one hour before the contest ends. So at the Award Ceremony, results are partially known. For each team the value _a__i_ is given β€” the number of points the _i_-th team has earned before the last hour of the contest. Besides that, the Jury has evaluated all submissions sent during the last hour and knows values _d__i_ β€” the number of points earned by the _i_-th team during the last hour (these values can be negative, which means that a team can lose points). Before the contest, each team got unique id from 1 to _n_. According to the contest rules, a team with more points takes a higher place. If two or more teams have equal number of points, the team with lower id will take the higher place. So no two teams can share the same place. The Award Ceremony proceeds in the following way. At the beginning of the ceremony, a large screen shows the results for the time moment "one hour before the end", which means that the _i_-th team has _a__i_ points. Then the Jury unfreezes results of the teams one by one in some order. When result of the _j_-th team is unfrozen, its score changes from _a__j_ to _a__j_u2009+u2009_d__j_. At this time the table of results is modified and the place of the team can change. The unfreezing of the _j_-th team is followed by the applause from the audience with duration of _x__j_u2009-u2009_y__j_ seconds, where _x__j_ is the place of the _j_-th team before unfreezing and _y__j_ is the place right after the unfreezing. For example, if the team does not change the place, there is no applause from the audience. As you can see, during the Award Ceremony, each team will be unfrozen exactly once. Your task is to find such an order to unfreeze all the teams that the total duration of applause is maximum possible. Input The first line of the input file contains a single integer _n_ (1u2009≀u2009_n_u2009≀u2009100) β€” the number of teams. Each of the next _n_ lines contains two integers _a__i_ and _d__i_ (1u2009≀u2009_a__i_u2009≀u2009100, u2009-u2009100u2009≀u2009_d__i_u2009≀u2009100) β€” the number of points the _i_-th team has earned before the last hour of the contest and the number of points earned by this team during the last hour. It is possible that after unfreezing a team will have a negative score. Output Print the only integer β€” maximal total applause duration in seconds if the Jury can choose any order of the teams to unfreeze. Note In the first example the initial standings are: 1. Team 2, 52 points 2. Team 1, 17 points 3. Team 4, 6 points 4. Team 3, 1 point Here any order of unfreezing the teams leads to 4 seconds of applause in total. For example, let's unfreeze teams in their order from the Team 1 to the Team 4. After the Team 1 became unfrozen the standings are: 1. Team 2, 52 points 2. Team 4, 6 points 3. Team 1, 3 points 4. Team 3, 1 point So there is 1 second of applause, because the difference between old and new places 2u2009-u20093u2009=u20091. After the Team 2 became unfrozen the standings are: 1. Team 2, 47 points 2. Team 4, 6 points 3. Team 1, 3 points 4. Team 3, 1 point The place of the Team 2 has not changed, so no applause during unfreezing. After the Team 3 became unfrozen the standings are: 1. Team 3, 53 point 2. Team 2, 47 points 3. Team 4, 6 points 4. Team 1, 3 points The place of the Team 3 has changed from 4 to 1, so the duration of applause is 4u2009-u20091u2009=u20093. The unfreezing of the Team 4 has not changed any place because _d_4u2009=u20090. Therefore, the total duration of applause is 1u2009+u20090u2009+u20093u2009+u20090u2009=u20094 seconds.
2,200
false
true
true
false
false
false
false
false
false
false
6,884
1967F
Let $$$p_1, ldots, p_n$$$ be a permutation of $$$[1, ldots, n]$$$. Let the $$$q$$$-subsequence of $$$p$$$ be a permutation of $$$[1, q]$$$, whose elements are in the same relative order as in $$$p_1, ldots, p_n$$$. That is, we extract all elements not exceeding $$$q$$$ together from $$$p$$$ in the original order, and they make the $$$q$$$-subsequence of $$$p$$$. For a given array $$$a$$$, let $$$pre(i)$$$ be the largest value satisfying $$$pre(i) < i$$$ and $$$a_{pre(i)} > a_i$$$. If it does not exist, let $$$pre(i) = -10^{100}$$$. Let $$$nxt(i)$$$ be the smallest value satisfying $$$nxt(i) > i$$$ and $$$a_{nxt(i)} > a_i$$$. If it does not exist, let $$$nxt(i) = 10^{100}$$$. For each $$$q$$$ such that $$$1 leq q leq n$$$, let $$$a_1, ldots, a_q$$$ be the $$$q$$$-subsequence of $$$p$$$. For each $$$i$$$ such that $$$1 leq i leq q$$$, $$$pre(i)$$$ and $$$nxt(i)$$$ will be calculated as defined. Then, you will be given some integer values of $$$x$$$, and for each of them you have to calculate $$$sumlimits_{i=1}^q min(nxt(i) - pre(i), x)$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1le tle 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 3cdot 10^5$$$)xa0β€” the length of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1, ldots, p_n$$$ ($$$1 leq p_i leq n$$$)xa0β€” the initial permutation. Then, for each $$$q$$$ such that $$$1 leq q leq n$$$ in ascending order, you will be given an integer $$$k$$$ ($$$0 leq k leq 10^5$$$), representing the number of queries for the $$$q$$$-subsequence. Then $$$k$$$ numbers in a line follow: each of them is the value of $$$x$$$ for a single query ($$$1 leq x leq q$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3cdot 10^5$$$ and the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, for each query, print a single line with an integer: the answer to the query. Example Input 1 7 6 1 4 3 2 5 7 1 1 0 1 3 1 2 3 1 2 3 1 3 2 2 6 Output 1 9 8 5 10 14 16 14 30 Note The $$$1$$$-subsequence is $$$[1]$$$, and $$$pre=[-10^{100}]$$$, $$$nxt=[10^{100}]$$$. $$$ans(1)=min(10^{100}-(-10^{100}),1)=1$$$. The $$$5$$$-subsequence is $$$[1,4,3,2,5]$$$, and $$$pre=[-10^{100},-10^{100},2,3,-10^{100}]$$$, $$$nxt=[2,5,5,5,10^{100}]$$$. $$$ans(1)=5,ans(2)=10,ans(3)=14$$$.
3,200
false
false
true
false
true
false
true
false
false
false
501
778E
Boris really likes numbers and even owns a small shop selling interesting numbers. He has _n_ decimal numbers _B__i_. Cost of the number in his shop is equal to the sum of costs of its digits. You are given the values _c__d_, where _c__d_ is the cost of the digit _d_. Of course, Boris is interested in that numbers he owns have the maximum cost possible. Recently Boris got hold of the magical artifact _A_, which can allow him to increase the cost of his collection. Artifact is a string, consisting of digits and '?' symbols. To use the artifact, Boris must replace all '?' with digits to get a decimal number without leading zeros (it is also not allowed to get number 0). After that, the resulting number is added to all numbers _B__i_ in Boris' collection. He uses the artifact exactly once. What is the maximum cost of the collection Boris can achieve after using the artifact? Input First line contains artifact _A_, consisting of digits '0'–'9' and '?' symbols (1u2009≀u2009_A_u2009≀u20091000). Next line contains _n_xa0β€” the amount of numbers in Boris' collection (1u2009≀u2009_n_u2009≀u20091000). Next _n_ lines contain integers _B__i_ (1u2009≀u2009_B__i_u2009<u2009101000). _A_ doesn't start with '0'. Last line contains ten integersxa0β€” costs of digits _c_0,u2009_c_1,u2009...,u2009_c_9 (0u2009≀u2009_c__i_u2009≀u20091000). Output Output one integerxa0β€” the maximum possible cost of the collection after using the artifact. Examples Input 42 3 89 1 958 0 0 1 1 2 2 3 3 4 4 Input ?5? 4 2203 5229 276 6243 2 1 6 1 1 2 5 2 2 3 Note In the second sample input, the optimal way is to compose the number 453. After adding this number, Boris will have numbers 2656, 5682, 729 and 6696. The total cost of all digits in them is equal to 18u2009+u200915u2009+u200911u2009+u200918u2009=u200962.
3,000
false
false
false
true
false
false
false
false
true
false
6,652
1182E
Problem - 1182E - 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 dp math matrices number theory *2300 No tag edit access β†’ Contest materials $$$. Input The only line contains five integers $$$n$$$, $$$f_{1}$$$, $$$f_{2}$$$, $$$f_{3}$$$, and $$$c$$$ ($$$4 le n le 10^{18}$$$, $$$1 le f_{1}$$$, $$$f_{2}$$$, $$$f_{3}$$$, $$$c le 10^{9}$$$). Output Print $$$f_{n} bmod (10^{9} + 7)$$$. Examples Input 5 1 2 5 3 Output 72900 Input 17 97 41 37 11 Output 317451037 Note In the first example, $$$f_{4} = 90$$$, $$$f_{5} = 72900$$$. In the second example, $$$f_{17} approx 2.28 imes 10^{29587}$$$.
2,300
true
false
false
true
false
false
false
false
false
false
4,804
356A
Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the message to all knights in the kingdom and they in turn agreed to participate in this grand event. As for you, you're just a simple peasant. There's no surprise that you slept in this morning and were late for the tournament (it was a weekend, after all). Now you are really curious about the results of the tournament. This time the tournament in Berland went as follows: There are _n_ knights participating in the tournament. Each knight was assigned his unique number β€” an integer from 1 to _n_. The tournament consisted of _m_ fights, in the _i_-th fight the knights that were still in the game with numbers at least _l__i_ and at most _r__i_ have fought for the right to continue taking part in the tournament. After the _i_-th fight among all participants of the fight only one knight won β€” the knight number _x__i_, he continued participating in the tournament. Other knights left the tournament. The winner of the last (the _m_-th) fight (the knight number _x__m_) became the winner of the tournament. You fished out all the information about the fights from your friends. Now for each knight you want to know the name of the knight he was conquered by. We think that the knight number _b_ was conquered by the knight number _a_, if there was a fight with both of these knights present and the winner was the knight number _a_. Write the code that calculates for each knight, the name of the knight that beat him. Input The first line contains two integers _n_, _m_ (2u2009≀u2009_n_u2009≀u20093Β·105;xa01u2009≀u2009_m_u2009≀u20093Β·105) β€” the number of knights and the number of fights. Each of the following _m_ lines contains three integers _l__i_,u2009_r__i_,u2009_x__i_ (1u2009≀u2009_l__i_u2009<u2009_r__i_u2009≀u2009_n_;xa0_l__i_u2009≀u2009_x__i_u2009≀u2009_r__i_) β€” the description of the _i_-th fight. It is guaranteed that the input is correct and matches the problem statement. It is guaranteed that at least two knights took part in each battle. Output Print _n_ integers. If the _i_-th knight lost, then the _i_-th number should equal the number of the knight that beat the knight number _i_. If the _i_-th knight is the winner, then the _i_-th number must equal 0. Examples Input 8 4 3 5 4 3 7 6 2 8 8 1 8 1 Note Consider the first test case. Knights 1 and 2 fought the first fight and knight 1 won. Knights 1 and 3 fought the second fight and knight 3 won. The last fight was between knights 3 and 4, knight 4 won.
1,500
false
false
false
false
true
false
false
false
false
false
8,411
524D
Polycarpus got an internship in one well-known social network. His test task is to count the number of unique users who have visited a social network during the day. Polycarpus was provided with information on all user requests for this time period. For each query, we know its time... and nothing else, because Polycarpus has already accidentally removed the user IDs corresponding to the requests from the database. Thus, it is now impossible to determine whether any two requests are made by the same person or by different people. But wait, something is still known, because that day a record was achieved β€” _M_ simultaneous users online! In addition, Polycarpus believes that if a user made a request at second _s_, then he was online for _T_ seconds after that, that is, at seconds _s_, _s_u2009+u20091, _s_u2009+u20092, ..., _s_u2009+u2009_T_u2009-u20091. So, the user's time online can be calculated as the union of time intervals of the form [_s_,u2009_s_u2009+u2009_T_u2009-u20091] over all times _s_ of requests from him. Guided by these thoughts, Polycarpus wants to assign a user ID to each request so that: the number of different users online did not exceed _M_ at any moment, at some second the number of distinct users online reached value _M_, the total number of users (the number of distinct identifiers) was as much as possible. Help Polycarpus cope with the test. Input The first line contains three integers _n_, _M_ and _T_ (1u2009≀u2009_n_,u2009_M_u2009≀u200920u2009000, 1u2009≀u2009_T_u2009≀u200986400) β€” the number of queries, the record number of online users and the time when the user was online after a query was sent. Next _n_ lines contain the times of the queries in the format "hh:mm:ss", where hh are hours, mm are minutes, ss are seconds. The times of the queries follow in the non-decreasing order, some of them can coincide. It is guaranteed that all the times and even all the segments of type [_s_,u2009_s_u2009+u2009_T_u2009-u20091] are within one 24-hour range (from 00:00:00 to 23:59:59). Output In the first line print number _R_ β€” the largest possible number of distinct users. The following _n_ lines should contain the user IDs for requests in the same order in which the requests are given in the input. User IDs must be integers from 1 to _R_. The requests of the same user must correspond to the same identifiers, the requests of distinct users must correspond to distinct identifiers. If there are multiple solutions, print any of them. If there is no solution, print "No solution" (without the quotes). Examples Input 4 2 10 17:05:53 17:05:58 17:06:01 22:39:47 Note Consider the first sample. The user who sent the first request was online from 17:05:53 to 17:06:02, the user who sent the second request was online from 17:05:58 to 17:06:07, the user who sent the third request, was online from 17:06:01 to 17:06:10. Thus, these IDs cannot belong to three distinct users, because in that case all these users would be online, for example, at 17:06:01. That is impossible, because _M_u2009=u20092. That means that some two of these queries belonged to the same user. One of the correct variants is given in the answer to the sample. For it user 1 was online from 17:05:53 to 17:06:02, user 2 β€” from 17:05:58 to 17:06:10 (he sent the second and third queries), user 3 β€” from 22:39:47 to 22:39:56. In the second sample there is only one query. So, only one user visited the network within the 24-hour period and there couldn't be two users online on the network simultaneously. (The time the user spent online is the union of time intervals for requests, so users who didn't send requests could not be online in the network.)
2,100
false
true
false
false
false
false
false
false
false
false
7,739
1301A
You are given three strings $$$a$$$, $$$b$$$ and $$$c$$$ of the same length $$$n$$$. The strings consist of lowercase English letters only. The $$$i$$$-th letter of $$$a$$$ is $$$a_i$$$, the $$$i$$$-th letter of $$$b$$$ is $$$b_i$$$, the $$$i$$$-th letter of $$$c$$$ is $$$c_i$$$. For every $$$i$$$ ($$$1 leq i leq n$$$) you must swap (i.e. exchange) $$$c_i$$$ with either $$$a_i$$$ or $$$b_i$$$. So in total you'll perform exactly $$$n$$$ swap operations, each of them either $$$c_i leftrightarrow a_i$$$ or $$$c_i leftrightarrow b_i$$$ ($$$i$$$ iterates over all integers between $$$1$$$ and $$$n$$$, inclusive). For example, if $$$a$$$ is "code", $$$b$$$ is "true", and $$$c$$$ is "help", you can make $$$c$$$ equal to "crue" taking the $$$1$$$-st and the $$$4$$$-th letters from $$$a$$$ and the others from $$$b$$$. In this way $$$a$$$ becomes "hodp" and $$$b$$$ becomes "tele". Is it possible that after these swaps the string $$$a$$$ becomes exactly the same as the string $$$b$$$? Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$) xa0β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a string of lowercase English letters $$$a$$$. The second line of each test case contains a string of lowercase English letters $$$b$$$. The third line of each test case contains a string of lowercase English letters $$$c$$$. It is guaranteed that in each test case these three strings are non-empty and have the same length, which is not exceeding $$$100$$$. Output Print $$$t$$$ lines with answers for all test cases. For each test case: If it is possible to make string $$$a$$$ equal to string $$$b$$$ print "YES" (without quotes), otherwise print "NO" (without quotes). You can print either lowercase or uppercase letters in the answers. Example Input 4 aaa bbb ccc abc bca bca aabb bbaa baba imi mii iim Note In the first test case, it is impossible to do the swaps so that string $$$a$$$ becomes exactly the same as string $$$b$$$. In the second test case, you should swap $$$c_i$$$ with $$$a_i$$$ for all possible $$$i$$$. After the swaps $$$a$$$ becomes "bca", $$$b$$$ becomes "bca" and $$$c$$$ becomes "abc". Here the strings $$$a$$$ and $$$b$$$ are equal. In the third test case, you should swap $$$c_1$$$ with $$$a_1$$$, $$$c_2$$$ with $$$b_2$$$, $$$c_3$$$ with $$$b_3$$$ and $$$c_4$$$ with $$$a_4$$$. Then string $$$a$$$ becomes "baba", string $$$b$$$ becomes "baba" and string $$$c$$$ becomes "abab". Here the strings $$$a$$$ and $$$b$$$ are equal. In the fourth test case, it is impossible to do the swaps so that string $$$a$$$ becomes exactly the same as string $$$b$$$.
800
false
false
true
false
false
false
false
false
false
false
4,194
359E
Simon loves neatness. So before he goes to bed, Simon wants to complete all chores in the house. Simon's house looks like a rectangular table consisting of _n_ rows and _n_ columns from above. All rows of the table are numbered from 1 to _n_ from top to bottom. All columns of the table are numbered from 1 to _n_ from left to right. Each cell of the table is a room. Pair (_x_,u2009_y_) denotes the room, located at the intersection of the _x_-th row and the _y_-th column. For each room we know if the light is on or not there. Initially Simon is in room (_x_0,u2009_y_0). He wants to turn off the lights in all the rooms in the house, and then return to room (_x_0,u2009_y_0). Suppose that at the current moment Simon is in the room (_x_,u2009_y_). To reach the desired result, he can perform the following steps: 1. The format of the action is "1". The action is to turn on the light in room (_x_,u2009_y_). Simon cannot do it if the room already has light on. 2. The format of the action is "2". The action is to turn off the light in room (_x_,u2009_y_). Simon cannot do it if the room already has light off. 3. The format of the action is "dir" (_dir_ is a character). The action is to move to a side-adjacent room in direction _dir_. The direction can be left, right, up or down (the corresponding dir is L, R, U or D). Additionally, Simon can move only if he see a light in the direction _dir_. More formally, if we represent the room, Simon wants to go, as (_nx_,u2009_ny_), there shold be an integer _k_ (_k_u2009>u20090), that room (_x_u2009+u2009(_nx_u2009-u2009_x_)_k_,u2009_y_u2009+u2009(_ny_u2009-u2009_y_)_k_) has a light. Of course, Simon cannot move out of his house. Help Simon, find the sequence of actions that lets him achieve the desired result. Input The first line contains three positive integers _n_,u2009_x_0,u2009_y_0 (2u2009≀u2009_n_u2009≀u2009500,u20091u2009≀u2009_x_0,u2009_y_0u2009≀u2009_n_). Next _n_ lines contain the description of rooms in the house. The _i_-th line contains _n_ space-separated integers _a__i_1,u2009_a__i_2,u2009...,u2009_a__in_. If number _a__ij_ equals zero, then room (_i_,u2009_j_) has light off, and if number _a__ij_ equals one, then room (_i_,u2009_j_) has light on. It is guaranteed that at least one room has light on. Output If there is no desired sequence of actions, print "NO" (without the quotes). Otherwise, print "YES" (without the quotes) and the description of the required sequence of actions as a string. Note that you do not have to minimize the length of the sequence of actions but you shouldn't use more than 3Β·106 actions. Examples Input 3 1 1 1 0 0 0 1 0 1 0 0 Input 3 1 1 1 0 0 0 1 0 0 0 1
2,400
false
false
false
false
false
true
false
false
false
false
8,395
439E
Today is Devu's birthday. For celebrating the occasion, he bought _n_ sweets from the nearby market. He has invited his _f_ friends. He would like to distribute the sweets among them. As he is a nice guy and the occasion is great, he doesn't want any friend to be sad, so he would ensure to give at least one sweet to each friend. He wants to celebrate it in a unique style, so he would like to ensure following condition for the distribution of sweets. Assume that he has distributed _n_ sweets to his friends such that _i__th_ friend is given _a__i_ sweets. He wants to make sure that there should not be any positive integer _x_u2009>u20091, which divides every _a__i_. Please find the number of ways he can distribute sweets to his friends in the required way. Note that the order of distribution is important, for example [1, 2] and [2, 1] are distinct distributions. As the answer could be very large, output answer modulo 1000000007 (109u2009+u20097). To make the problem more interesting, you are given _q_ queries. Each query contains an _n_, _f_ pair. For each query please output the required number of ways modulo 1000000007 (109u2009+u20097). Input The first line contains an integer _q_ representing the number of queries (1u2009≀u2009_q_u2009≀u2009105). Each of the next _q_ lines contains two space space-separated integers _n_, _f_ (1u2009≀u2009_f_u2009≀u2009_n_u2009≀u2009105). Output For each query, output a single integer in a line corresponding to the answer of each query. Note For first query: _n_u2009=u20096,u2009_f_u2009=u20092. Possible partitions are [1, 5] and [5, 1]. For second query: _n_u2009=u20097,u2009_f_u2009=u20092. Possible partitions are [1, 6] and [2, 5] and [3, 4] and [4, 3] and [5, 3] and [6, 1]. So in total there are 6 possible ways of partitioning.
2,100
true
false
false
true
false
false
false
false
false
false
8,080
1737D
Ela needs to send a large package from machine $$$1$$$ to machine $$$n$$$ through a network of machines. Currently, with the network condition, she complains that the network is too slow and the package can't arrive in time. Luckily, a Wiring Wizard offered her a helping hand. The network can be represented as an undirected connected graph with $$$n$$$ nodes, each node representing a machine. $$$m$$$ wires are used to connect them. Wire $$$i$$$ is used to connect machines $$$u_i$$$ and $$$v_i$$$, and has a weight $$$w_i$$$. The aforementioned large package, if going through wire $$$i$$$, will move from machine $$$u_i$$$ to machine $$$v_i$$$ (or vice versa) in exactly $$$w_i$$$ microseconds. The Wiring Wizard can use his spell an arbitrary number of times. For each spell, he will choose the wire of index $$$i$$$, connecting machine $$$u_i$$$ and $$$v_i$$$, and rewire it following these steps: Choose one machine that is connected by this wire. Without loss of generality, let's choose $$$v_i$$$. Choose a machine that is currently connecting to $$$v_i$$$ (including $$$u_i$$$), call it $$$t_i$$$. Disconnect the wire indexed $$$i$$$ from $$$v_i$$$, then using it to connect $$$u_i$$$ and $$$t_i$$$. The rewiring of wire $$$i$$$ will takes $$$w_i$$$ microseconds, and the weight of the wire will not change after this operation. After a rewiring, a machine might have some wire connect it with itself. Also, the Wiring Wizard has warned Ela that rewiring might cause temporary disconnections between some machines, but Ela just ignores it anyway. Her mission is to send the large package from machine $$$1$$$ to machine $$$n$$$ as fast as possible. Note that the Wizard can use his spell on a wire zero, one, or many times. To make sure the network works seamlessly while transferring the large package, once the package starts transferring from machine $$$1$$$, the Wiring Wizard cannot use his spell to move wires around anymore. Ela wonders, with the help of the Wiring Wizard, what is the least amount of time needed to transfer the large package from machine $$$1$$$ to $$$n$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). The description of the test cases follows. The first line contains $$$n$$$ and $$$m$$$ ($$$2 le n le 500$$$, $$$n - 1 le m le 250 000$$$), the number of nodes and number of wires, respectively. For the next $$$m$$$ lines, $$$i$$$-th line will contains $$$u_i$$$, $$$v_i$$$ and $$$w_i$$$ ($$$1 le u_i, v_i le n$$$, $$$1 le w_i le 10^9$$$) - the indices 2 machines that are connected by the $$$i$$$-th edge and the weight of it. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$250 000$$$. The graph in each test case is guaranteed to be connected, no self-loops, but it can contain multiple edges. Output For each test case, output one integer denotes the least amount of time needed to transfer the large package from machine $$$1$$$ to $$$n$$$. Example Input 3 8 9 1 2 3 6 4 5 3 5 6 6 1 3 7 4 4 3 8 4 2 3 3 7 8 5 4 5 2 4 5 1 2 1 2 4 1 3 4 1 3 1 1 1 3 2 8 8 4 6 92 7 1 65 6 5 43 6 7 96 4 3 74 4 8 54 7 4 99 2 5 22 Note Here is the graph in the first test case in the sample input: Ela can ask the Wiring Wizard to use his spell on wire with the index of $$$7$$$, which is connecting machines $$$2$$$ and $$$3$$$. Then, since the machine $$$8$$$ is connected to machine $$$3$$$, the Wiring Wizard can disconnect wire $$$7$$$ from machine $$$3$$$ and connect it to machine $$$8$$$ in $$$3$$$ microseconds (weight of wire $$$3$$$). After that, the package can be sent from machine $$$1$$$ to machine $$$8$$$ in $$$6$$$ microseconds. Therefore, the answer is $$$3 + 6 = 9$$$ microseconds. Here is the graph in the third test case in the sample input:
2,200
false
true
true
true
false
false
true
false
false
true
1,865
366B
You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong. Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete _k_u2009-u20091 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks. Overall, Dima has _n_ tasks to do, each task has a unique number from 1 to _n_. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6 tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one. Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible. Input The first line of the input contains two integers _n_,u2009_k_xa0(1u2009≀u2009_k_u2009≀u2009_n_u2009≀u2009105). The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_xa0(1u2009≀u2009_a__i_u2009≀u2009103), where _a__i_ is the power Inna tells Dima off with if she is present in the room while he is doing the _i_-th task. It is guaranteed that _n_ is divisible by _k_. Output In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do. Examples Input 10 5 1 3 5 7 9 9 4 1 8 5 Note Explanation of the first example. If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as _k_ = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12. Explanation of the second example. In the second example _k_ = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3.
1,200
false
false
true
false
false
false
true
false
false
false
8,369
465A
Sergey is testing a next-generation processor. Instead of bytes the processor works with memory cells consisting of _n_ bits. These bits are numbered from 1 to _n_. An integer is stored in the cell in the following way: the least significant bit is stored in the first bit of the cell, the next significant bit is stored in the second bit, and so on; the most significant bit is stored in the _n_-th bit. Now Sergey wants to test the following instruction: "add 1 to the value of the cell". As a result of the instruction, the integer that is written in the cell must be increased by one; if some of the most significant bits of the resulting number do not fit into the cell, they must be discarded. Sergey wrote certain values u200bu200bof the bits in the cell and is going to add one to its value. How many bits of the cell will change after the operation? Input The first line contains a single integer _n_ (1u2009≀u2009_n_u2009≀u2009100) β€” the number of bits in the cell. The second line contains a string consisting of _n_ characters β€” the initial state of the cell. The first character denotes the state of the first bit of the cell. The second character denotes the second least significant bit and so on. The last character denotes the state of the most significant bit. Output Print a single integer β€” the number of bits in the cell which change their state after we add 1 to the cell. Note In the first sample the cell ends up with value 0010, in the second sample β€” with 0000.
900
false
false
true
false
false
false
false
false
false
false
7,979
1914G1
The easy and hard versions of this problem differ only in the constraints on $$$n$$$. In the easy version, the sum of values of $$$n^2$$$ over all test cases does not exceed $$$10^6$$$. Furthermore, $$$n$$$ does not exceed $$$1000$$$ in each test case. There are $$$2n$$$ light bulbs arranged in a row. Each light bulb has a color from $$$1$$$ to $$$n$$$ (exactly two light bulbs for each color). Initially, all light bulbs are turned off. You choose a set of light bulbs $$$S$$$ that you initially turn on. After that, you can perform the following operations in any order any number of times: choose two light bulbs $$$i$$$ and $$$j$$$ of the same color, exactly one of which is on, and turn on the second one; choose three light bulbs $$$i, j, k$$$, such that both light bulbs $$$i$$$ and $$$k$$$ are on and have the same color, and the light bulb $$$j$$$ is between them ($$$i < j < k$$$), and turn on the light bulb $$$j$$$. You want to choose a set of light bulbs $$$S$$$ that you initially turn on in such a way that by performing the described operations, you can ensure that all light bulbs are turned on. Calculate two numbers: the minimum size of the set $$$S$$$ that you initially turn on; the number of sets $$$S$$$ of minimum size (taken modulo $$$998244353$$$). Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. Then follow the descriptions of the test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 1000$$$)xa0β€” the number of pairs of light bulbs. The second line of each test case contains $$$2n$$$ integers $$$c_1, c_2, dots, c_{2n}$$$ ($$$1 le c_i le n$$$), where $$$c_i$$$ is the color of the $$$i$$$-th light bulb. For each color from $$$1$$$ to $$$n$$$, exactly two light bulbs have this color. Additional constraint on the input: the sum of values of $$$n^2$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output two integers: the minimum size of the set $$$S$$$ that you initially turn on; the number of sets $$$S$$$ of minimum size (taken modulo $$$998244353$$$). Example Input 4 2 2 2 1 1 2 1 2 2 1 2 1 2 1 2 5 3 4 4 5 3 1 1 5 2 2
2,100
true
false
false
true
false
false
true
false
false
true
812
343E
Mad scientist Mike has applied for a job. His task is to manage a system of water pumping stations. The system consists of _n_ pumping stations, which are numbered by integers from 1 to _n_. Some pairs of stations are connected by bidirectional pipes through which water can flow in either direction (but only in one at a time). For each pipe you know its bandwidth β€” the maximum number of liters of water that can flow through it in one hour. Each pumping station can pump incoming water from some stations to other stations through the pipes, provided that in one hour the total influx of water to the station is equal to the total outflux of water from the station. It is Mike's responsibility to pump water between stations. From station _a_ to station _b_ through the pipes (possibly through other stations) within one hour one can transmit a certain number of liters of water according to the rules described above. During this time, water from other stations can not flow into station _a_, and can not flow out of the station _b_. However, any amount of water can flow out of station _a_ or in station _b_. If a total of _x_ litres of water flows out of the station _a_ in an hour, then Mike gets _x_ bollars more to his salary. To get paid, Mike needs to work for _n_u2009-u20091 days, according to the contract. On the first day he selects two stations _v_1 and _v_2, and within one hour he pumps a certain amount of water from _v_1 to _v_2. Next, on the _i_-th day Mike chooses a station _v__i_u2009+u20091 that has been never selected before, and pumps a certain amount of water out of the station _v__i_ to station _v__i_u2009+u20091 for one hour. The quantity of water he pumps on the _i_-th day does not depend on the amount of water pumped on the (_i_u2009-u20091)-th day. Mike needs to earn as much bollars as he can for his projects. Help Mike find such a permutation of station numbers _v_1, _v_2, ..., _v__n_ so Mike will be able to earn the highest possible salary. Input The first line of the input contains two space-separated integers _n_ and _m_ (2u2009≀u2009_n_u2009≀u2009200, 1u2009≀u2009_m_u2009≀u20091000) β€” the number of stations and pipes in the system, accordingly. The _i_-th of the next _m_ lines contains three space-separated integers _a__i_, _b__i_ and _c__i_ (1u2009≀u2009_a__i_,u2009_b__i_u2009≀u2009_n_, _a__i_u2009β‰ u2009_b__i_, 1u2009≀u2009_c__i_u2009≀u2009100) β€” the numbers of stations connected by the _i_-th pipe and the pipe's bandwidth, accordingly. It is guaranteed that any two stations are connected by at most one pipe and that there is a pipe path between any two stations. Output On the first line print a single integer β€” the maximum salary Mike can earn. On the second line print a space-separated permutation of _n_ numbers from 1 to _n_ β€” the numbers of stations in the sequence _v_1, _v_2, ..., _v__n_. If there are multiple answers, print any of them.
2,900
false
true
false
false
false
false
true
false
false
true
8,459
1733D2
This is the hard version of this problem. In this version, $$$n le 5000$$$ holds, and this version has no restriction between $$$x$$$ and $$$y$$$. You can make hacks only if both versions of the problem are solved. You are given two binary strings $$$a$$$ and $$$b$$$, both of length $$$n$$$. You can do the following operation any number of times (possibly zero). Select two indices $$$l$$$ and $$$r$$$ ($$$l < r$$$). Change $$$a_l$$$ to $$$(1 - a_l)$$$, and $$$a_r$$$ to $$$(1 - a_r)$$$. If $$$l + 1 = r$$$, the cost of the operation is $$$x$$$. Otherwise, the cost is $$$y$$$. You have to find the minimum cost needed to make $$$a$$$ equal to $$$b$$$ or say there is no way to do so. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β€” the number of test cases. Each test case consists of three lines. The first line of each test case contains three integers $$$n$$$, $$$x$$$, and $$$y$$$ ($$$5 le n le 5000$$$, $$$1 le x, y le 10^9$$$)xa0β€” the length of the strings, and the costs per operation. The second line of each test case contains the string $$$a$$$ of length $$$n$$$. The string only consists of digits $$$0$$$ and $$$1$$$. The third line of each test case contains the string $$$b$$$ of length $$$n$$$. The string only consists of digits $$$0$$$ and $$$1$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$5000$$$. Output For each test case, if there is no way to make $$$a$$$ equal to $$$b$$$, print $$$-1$$$. Otherwise, print the minimum cost needed to make $$$a$$$ equal to $$$b$$$. Example Input 6 5 8 9 01001 00101 6 2 11 000001 100000 5 7 2 01000 11011 7 8 3 0111001 0100001 6 3 4 010001 101000 5 10 1 01100 01100 Note In the first test case, selecting indices $$$2$$$ and $$$3$$$ costs $$$8$$$, which is the minimum. In the second test case, we can perform the following operations. Select indices $$$1$$$ and $$$2$$$. It costs $$$2$$$, and $$$a$$$ is 110001 now. Select indices $$$2$$$ and $$$3$$$. It costs $$$2$$$, and $$$a$$$ is 101001 now. Select indices $$$3$$$ and $$$4$$$. It costs $$$2$$$, and $$$a$$$ is 100101 now. Select indices $$$4$$$ and $$$5$$$. It costs $$$2$$$, and $$$a$$$ is 100011 now. Select indices $$$5$$$ and $$$6$$$. It costs $$$2$$$, and $$$a$$$ is 100000 now. The total cost is $$$10$$$. In the third test case, we cannot make $$$a$$$ equal to $$$b$$$ using any number of operations. In the fourth test case, we can perform the following operations. Select indices $$$3$$$ and $$$6$$$. It costs $$$3$$$, and $$$a$$$ is 0101011 now. Select indices $$$4$$$ and $$$6$$$. It costs $$$3$$$, and $$$a$$$ is 0100001 now. The total cost is $$$6$$$. In the fifth test case, we can perform the following operations. Select indices $$$1$$$ and $$$6$$$. It costs $$$4$$$, and $$$a$$$ is 110000 now. Select indices $$$2$$$ and $$$3$$$. It costs $$$3$$$, and $$$a$$$ is 101000 now. The total cost is $$$7$$$. In the sixth test case, we don't have to perform any operation.
2,000
false
true
false
true
false
false
false
false
false
false
1,888
208C
The Berland road network consists of _n_ cities and of _m_ bidirectional roads. The cities are numbered from 1 to _n_, where the main capital city has number _n_, and the culture capital β€” number 1. The road network is set up so that it is possible to reach any city from any other one by the roads. Moving on each road in any direction takes the same time. All residents of Berland are very lazy people, and so when they want to get from city _v_ to city _u_, they always choose one of the shortest paths (no matter which one). The Berland government wants to make this country's road network safer. For that, it is going to put a police station in one city. The police station has a rather strange property: when a citizen of Berland is driving along the road with a police station at one end of it, the citizen drives more carefully, so all such roads are considered safe. The roads, both ends of which differ from the city with the police station, are dangerous. Now the government wonders where to put the police station so that the average number of safe roads for all the shortest paths from the cultural capital to the main capital would take the maximum value. Input The first input line contains two integers _n_ and _m_ (2u2009≀u2009_n_u2009≀u2009100, ) β€” the number of cities and the number of roads in Berland, correspondingly. Next _m_ lines contain pairs of integers _v__i_, _u__i_ (1u2009≀u2009_v__i_,u2009_u__i_u2009≀u2009_n_, _v__i_u2009β‰ u2009_u__i_) β€” the numbers of cities that are connected by the _i_-th road. The numbers on a line are separated by a space. It is guaranteed that each pair of cities is connected with no more than one road and that it is possible to get from any city to any other one along Berland roads. Output Print the maximum possible value of the average number of safe roads among all shortest paths from the culture capital to the main one. The answer will be considered valid if its absolute or relative inaccuracy does not exceed 10u2009-u20096. Examples Input 11 14 1 2 1 3 2 4 3 4 4 5 4 6 5 11 6 11 1 8 8 9 9 7 11 7 1 10 10 4 Note In the first sample you can put a police station in one of the capitals, then each path will have exactly one safe road. If we place the station not in the capital, then the average number of safe roads will also make . In the second sample we can obtain the maximum sought value if we put the station in city 4, then 6 paths will have 2 safe roads each, and one path will have 0 safe roads, so the answer will equal .
1,900
false
false
false
true
false
false
false
false
false
true
8,999
417E
Problem - 417E - 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 constructive algorithms math probabilities *2400 No tag edit access β†’ Contest materials xa0β€” the size of the table. Output Print the table that meets the condition: _n_ lines containing _m_ integers, separated by spaces. If there are multiple possible answers, you are allowed to print anyone. It is guaranteed that there exists at least one correct answer. Examples Input 1 1 Output 1 Input 1 2 Output 3 4
2,400
true
false
false
false
false
true
false
false
false
false
8,153
761C
After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length _n_ which satisfies the following requirements: There is at least one digit in the string, There is at least one lowercase (small) letter of the Latin alphabet in the string, There is at least one of three listed symbols in the string: '#', '*', '&'. Considering that these are programming classes it is not easy to write the password. For each character of the password we have a fixed string of length _m_, on each of these _n_ strings there is a pointer on some character. The _i_-th character displayed on the screen is the pointed character in the _i_-th string. Initially, all pointers are on characters with indexes 1 in the corresponding strings (all positions are numbered starting from one). During one operation Dasha can move a pointer in one string one character to the left or to the right. Strings are cyclic, it means that when we move the pointer which is on the character with index 1 to the left, it moves to the character with the index _m_, and when we move it to the right from the position _m_ it moves to the position 1. You need to determine the minimum number of operations necessary to make the string displayed on the screen a valid password. Input The first line contains two integers _n_, _m_ (3u2009≀u2009_n_u2009≀u200950,u20091u2009≀u2009_m_u2009≀u200950) β€” the length of the password and the length of strings which are assigned to password symbols. Each of the next _n_ lines contains the string which is assigned to the _i_-th symbol of the password string. Its length is _m_, it consists of digits, lowercase English letters, and characters '#', '*' or '&'. You have such input data that you can always get a valid password. Output Print one integer β€” the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password. Examples Input 5 5 #*&# *a1c& &q2w #a3c# *&#*& Note In the first test it is necessary to move the pointer of the third string to one left to get the optimal answer. In the second test one of possible algorithms will be: to move the pointer of the second symbol once to the right. to move the pointer of the third symbol twice to the right.
1,500
false
false
true
true
false
false
true
false
false
false
6,747
1174F
This is an interactive problem. You're given a tree consisting of $$$n$$$ nodes, rooted at node $$$1$$$. A tree is a connected graph with no cycles. We chose a hidden node $$$x$$$. In order to find this node, you can ask queries of two types: d $$$u$$$ ($$$1 le u le n$$$). We will answer with the distance between nodes $$$u$$$ and $$$x$$$. The distance between two nodes is the number of edges in the shortest path between them. s $$$u$$$ ($$$1 le u le n$$$). We will answer with the second node on the path from $$$u$$$ to $$$x$$$. However, there's a plot twist. If $$$u$$$ is not an ancestor of $$$x$$$, you'll receive "Wrong answer" verdict! Node $$$a$$$ is called an ancestor of node $$$b$$$ if $$$a e b$$$ and the shortest path from node $$$1$$$ to node $$$b$$$ passes through node $$$a$$$. Note that in this problem a node is not an ancestor of itself. Can you find $$$x$$$ in no more than $$$36$$$ queries? The hidden node is fixed in each test beforehand and does not depend on your queries. Input The first line contains the integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0β€” the number of nodes in the tree. Each of the next $$$n-1$$$ lines contains two space-separated integers $$$u$$$ and $$$v$$$ ($$$1 le u,v le n$$$) that mean there's an edge between nodes $$$u$$$ and $$$v$$$. It's guaranteed that the given graph is a tree. Output To print the answer, print "! x" (without quotes). Interaction To ask a question, print it in one of the formats above: d $$$u$$$ ($$$1 le u le n$$$), or s $$$u$$$ ($$$1 le u le n$$$). After each question, you should read the answer: either the distance or the second vertex on the path, as mentioned in the legend. If we answer with $$$-1$$$ instead of a valid answer, that means you exceeded the number of queries, made an invalid query, or violated the condition in the second type of queries. Exit immediately after receiving $$$-1$$$ and you will see Wrong answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. After printing a query, do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; See the documentation for other languages. Hacks: The first line should contain two integers $$$n$$$ and $$$x$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$1 le x le n$$$). Each of the next $$$n-1$$$ lines should contain two integers $$$u$$$ and $$$v$$$ ($$$1 le u,v le n$$$) that mean there is an edge between nodes $$$u$$$ and $$$v$$$. The edges must form a tree. Example Input 5 1 2 1 3 3 4 3 5 3 5 Note In the first example, the hidden node is node $$$5$$$. We first ask about the distance between node $$$x$$$ and node $$$2$$$. The answer is $$$3$$$, so node $$$x$$$ is either $$$4$$$ or $$$5$$$. We then ask about the second node in the path from node $$$3$$$ to node $$$x$$$. Note here that node $$$3$$$ is an ancestor of node $$$5$$$. We receive node $$$5$$$ as the answer. Finally, we report that the hidden node is node $$$5$$$.
2,400
false
false
true
false
false
true
false
false
false
true
4,846
1092F
You are given a tree consisting exactly of $$$n$$$ vertices. Tree is a connected undirected graph with $$$n-1$$$ edges. Each vertex $$$v$$$ of this tree has a value $$$a_v$$$ assigned to it. Let $$$dist(x, y)$$$ be the distance between the vertices $$$x$$$ and $$$y$$$. The distance between the vertices is the number of edges on the simple path between them. Let's define the cost of the tree as the following value: firstly, let's fix some vertex of the tree. Let it be $$$v$$$. Then the cost of the tree is $$$sumlimits_{i = 1}^{n} dist(i, v) cdot a_i$$$. Your task is to calculate the maximum possible cost of the tree if you can choose $$$v$$$ arbitrarily. Input The first line contains one integer $$$n$$$, the number of vertices in the tree ($$$1 le n le 2 cdot 10^5$$$). The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 2 cdot 10^5$$$), where $$$a_i$$$ is the value of the vertex $$$i$$$. Each of the next $$$n - 1$$$ lines describes an edge of the tree. Edge $$$i$$$ is denoted by two integers $$$u_i$$$ and $$$v_i$$$, the labels of vertices it connects ($$$1 le u_i, v_i le n$$$, $$$u_i e v_i$$$). It is guaranteed that the given edges form a tree. Output Print one integer β€” the maximum possible cost of the tree if you can choose any vertex as $$$v$$$. Examples Input 8 9 4 1 7 10 1 6 5 1 2 2 3 1 4 1 5 5 6 5 7 5 8 Note Picture corresponding to the first example: You can choose the vertex $$$3$$$ as a root, then the answer will be $$$2 cdot 9 + 1 cdot 4 + 0 cdot 1 + 3 cdot 7 + 3 cdot 10 + 4 cdot 1 + 4 cdot 6 + 4 cdot 5 = 18 + 4 + 0 + 21 + 30 + 4 + 24 + 20 = 121$$$. In the second example tree consists only of one vertex so the answer is always $$$0$$$.
1,900
false
false
false
true
false
false
false
false
false
false
5,245
1552G
Andrea has come up with what he believes to be a novel sorting algorithm for arrays of length $$$n$$$. The algorithm works as follows. Initially there is an array of $$$n$$$ integers $$$a_1,, a_2,, dots,, a_n$$$. Then, $$$k$$$ steps are executed. For each $$$1le ile k$$$, during the $$$i$$$-th step the subsequence of the array $$$a$$$ with indexes $$$j_{i,1}< j_{i,2}< dots< j_{i, q_i}$$$ is sorted, without changing the values with the remaining indexes. So, the subsequence $$$a_{j_{i,1}},, a_{j_{i,2}},, dots,, a_{j_{i,q_i}}$$$ is sorted and all other elements of $$$a$$$ are left untouched. Andrea, being eager to share his discovery with the academic community, sent a short paper describing his algorithm to the journal "Annals of Sorting Algorithms" and you are the referee of the paper (that is, the person who must judge the correctness of the paper). You must decide whether Andrea's algorithm is correct, that is, if it sorts any array $$$a$$$ of $$$n$$$ integers. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1le nle 40$$$, $$$0le kle 10$$$) β€” the length of the arrays handled by Andrea's algorithm and the number of steps of Andrea's algorithm. Then $$$k$$$ lines follow, each describing the subsequence considered in a step of Andrea's algorithm. The $$$i$$$-th of these lines contains the integer $$$q_i$$$ ($$$1le q_ile n$$$) followed by $$$q_i$$$ integers $$$j_{i,1},,j_{i,2},,dots,, j_{i,q_i}$$$ ($$$1le j_{i,1}<j_{i,2}<cdots<j_{i,q_i}le n$$$) β€” the length of the subsequence considered in the $$$i$$$-th step and the indexes of the subsequence. Note Explanation of the first sample: The algorithm consists of $$$3$$$ steps. The first one sorts the subsequence $$$[a_1, a_2, a_3]$$$, the second one sorts the subsequence $$$[a_2, a_3, a_4]$$$, the third one sorts the subsequence $$$[a_1,a_2]$$$. For example, if initially $$$a=[6, 5, 6, 3]$$$, the algorithm transforms the array as follows (the subsequence that gets sorted is highlighted in red) $$$$$$ [{color{red}6},{color{red}5},{color{red}6},3] ightarrow [5, {color{red}6}, {color{red}6}, {color{red}3}] ightarrow [{color{red}5}, {color{red}3}, 6, 6] ightarrow [3, 5, 6, 6] ,.$$$$$$ One can prove that, for any initial array $$$a$$$, at the end of the algorithm the array $$$a$$$ will be sorted. Explanation of the second sample: The algorithm consists of $$$3$$$ steps. The first one sorts the subsequence $$$[a_1, a_2, a_3]$$$, the second one sorts the subsequence $$$[a_2, a_3, a_4]$$$, the third one sorts the subsequence $$$[a_1,a_3,a_4]$$$. For example, if initially $$$a=[6, 5, 6, 3]$$$, the algorithm transforms the array as follows (the subsequence that gets sorted is highlighted in red) $$$$$$ [{color{red}6},{color{red}5},{color{red}6},3] ightarrow [5, {color{red}6}, {color{red}6}, {color{red}3}] ightarrow [{color{red}5}, 3, {color{red}6}, {color{red}6}] ightarrow [5, 3, 6, 6] ,.$$$$$$ Notice that $$$a=[6,5,6,3]$$$ is an example of an array that is not sorted by the algorithm. Explanation of the third sample: The algorithm consists of $$$4$$$ steps. The first $$$3$$$ steps do nothing because they sort subsequences of length $$$1$$$, whereas the fourth step sorts the subsequence $$$[a_1,a_3]$$$. For example, if initially $$$a=[5,6,4]$$$, the algorithm transforms the array as follows (the subsequence that gets sorted is highlighted in red) $$$$$$ [{color{red}5},6,4] ightarrow [5,{color{red}6},4] ightarrow [5,{color{red}6},4] ightarrow [{color{red}5},6,{color{red}4}] ightarrow [4,6,5] ,.$$$$$$ Notice that $$$a=[5,6,4]$$$ is an example of an array that is not sorted by the algorithm. Explanation of the fourth sample: The algorithm consists of $$$2$$$ steps. The first step sorts the subsequences $$$[a_2,a_3,a_4]$$$, the second step sorts the whole array $$$[a_1,a_2,a_3,a_4,a_5]$$$. For example, if initially $$$a=[9,8,1,1,1]$$$, the algorithm transforms the array as follows (the subsequence that gets sorted is highlighted in red) $$$$$$ [9,{color{red}8},{color{red}1},{color{red}1},1] ightarrow [{color{red}9},{color{red}1},{color{red}1},{color{red}8},{color{red}1}] ightarrow [1,1,1,8,9] ,.$$$$$$ Since in the last step the whole array is sorted, it is clear that, for any initial array $$$a$$$, at the end of the algorithm the array $$$a$$$ will be sorted.
3,000
false
false
false
false
false
false
true
false
true
false
2,878
617D
Problem - 617D - 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 constructive algorithms implementation *1700 No tag edit access β†’ Contest materials xa0β€” the coordinates of the _i_-th point. It is guaranteed that all points are distinct. Output Print a single numberxa0β€” the minimum possible number of segments of the polyline. Examples Input 1 -1 1 1 1 2 Output 1 Input -1 -1 -1 3 4 3 Output 2 Input 1 1 2 3 3 2 Output 3 Note The variant of the polyline in the first sample: The variant of the polyline in the second sample: The variant of the polyline in the third sample:
1,700
false
false
true
false
false
true
false
false
false
false
7,366
255C
Gena loves sequences of numbers. Recently, he has discovered a new type of sequences which he called an almost arithmetical progression. A sequence is an almost arithmetical progression, if its elements can be represented as: _a_1u2009=u2009_p_, where _p_ is some integer; _a__i_u2009=u2009_a__i_u2009-u20091u2009+u2009(u2009-u20091)_i_u2009+u20091Β·_q_ (_i_u2009>u20091), where _q_ is some integer. Right now Gena has a piece of paper with sequence _b_, consisting of _n_ integers. Help Gena, find there the longest subsequence of integers that is an almost arithmetical progression. Sequence _s_1,u2009u2009_s_2,u2009u2009...,u2009u2009_s__k_ is a subsequence of sequence _b_1,u2009u2009_b_2,u2009u2009...,u2009u2009_b__n_, if there is such increasing sequence of indexes _i_1,u2009_i_2,u2009...,u2009_i__k_ (1u2009u2009≀u2009u2009_i_1u2009u2009<u2009u2009_i_2u2009u2009<u2009... u2009u2009<u2009u2009_i__k_u2009u2009≀u2009u2009_n_), that _b__i__j_u2009u2009=u2009u2009_s__j_. In other words, sequence _s_ can be obtained from _b_ by crossing out some elements. Input The first line contains integer _n_ (1u2009≀u2009_n_u2009≀u20094000). The next line contains _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (1u2009≀u2009_b__i_u2009≀u2009106). Output Print a single integer β€” the length of the required longest subsequence. Note In the first test the sequence actually is the suitable subsequence. In the second test the following subsequence fits: 10,u200920,u200910.
1,500
false
false
false
true
false
false
true
false
false
false
8,812
1236E
Alice is playing a game with her good friend, Marisa. There are $$$n$$$ boxes arranged in a line, numbered with integers from $$$1$$$ to $$$n$$$ from left to right. Marisa will hide a doll in one of the boxes. Then Alice will have $$$m$$$ chances to guess where the doll is. If Alice will correctly guess the number of box, where doll is now, she will win the game, otherwise, her friend will win the game. In order to win, Marisa will use some unfair tricks. After each time Alice guesses a box, she can move the doll to the neighboring box or just keep it at its place. Boxes $$$i$$$ and $$$i + 1$$$ are neighboring for all $$$1 leq i leq n - 1$$$. She can also use this trick once before the game starts. So, the game happens in this order: the game starts, Marisa makes the trick, Alice makes the first guess, Marisa makes the trick, Alice makes the second guess, Marisa makes the trick, $$$ldots$$$, Alice makes $$$m$$$-th guess, Marisa makes the trick, the game ends. Alice has come up with a sequence $$$a_1, a_2, ldots, a_m$$$. In the $$$i$$$-th guess, she will ask if the doll is in the box $$$a_i$$$. She wants to know the number of scenarios $$$(x, y)$$$ (for all $$$1 leq x, y leq n$$$), such that Marisa can win the game if she will put the doll at the $$$x$$$-th box at the beginning and at the end of the game, the doll will be at the $$$y$$$-th box. Help her and calculate this number. Input The first line contains two integers $$$n$$$ and $$$m$$$, separated by space ($$$1 leq n, m leq 10^5$$$)xa0β€” the number of boxes and the number of guesses, which Alice will make. The next line contains $$$m$$$ integers $$$a_1, a_2, ldots, a_m$$$, separated by spaces ($$$1 leq a_i leq n$$$), the number $$$a_i$$$ means the number of the box which Alice will guess in the $$$i$$$-th guess. Output Print the number of scenarios in a single line, or the number of pairs of boxes $$$(x, y)$$$ ($$$1 leq x, y leq n$$$), such that if Marisa will put the doll into the box with number $$$x$$$, she can make tricks in such way, that at the end of the game the doll will be in the box with number $$$y$$$ and she will win the game. Note In the first example, the possible scenarios are $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 1)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$. Let's take $$$(2, 2)$$$ as an example. The boxes, in which the doll will be during the game can be $$$2 o 3 o 3 o 3 o 2$$$
2,500
false
false
false
true
true
false
false
true
false
false
4,514
1808E1
This is the easy version of the problem. The only difference between the three versions is the constraints on $$$n$$$ and $$$k$$$. You can make hacks only if all versions of the problem are solved. Maxim is a minibus driver on Venus. To ride on Maxim's minibus, you need a ticket. Each ticket has a number consisting of $$$n$$$ digits. However, as we know, the residents of Venus use a numeral system with base $$$k$$$, rather than the decimal system. Therefore, the ticket number can be considered as a sequence of $$$n$$$ integers from $$$0$$$ to $$$k-1$$$, inclusive. The residents of Venus consider a ticket to be lucky if there is a digit on it that is equal to the sum of the remaining digits, modulo $$$k$$$. For example, if $$$k=10$$$, then the ticket $$$7135$$$ is lucky because $$$7 + 1 + 5 equiv 3 pmod{10}$$$. On the other hand, the ticket $$$7136$$$ is not lucky because no digit is equal to the sum of the others modulo $$$10$$$. Once, while on a trip, Maxim wondered: how many lucky tickets exist? At the same time, Maxim understands that this number can be very large, so he is interested only in the answer modulo some prime number $$$m$$$. Input The only line of the input contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 le n le 100$$$, $$$1 le k le 30$$$, $$$10^8 le m le 10^9 + 7$$$, $$$m$$$ is a prime number)xa0β€” the number of digits on the ticket, the base of the numeral system on Venus, and the module for answer calculation. Note In the first example, there are only four lucky tickets: $$$000$$$, $$$011$$$, $$$101$$$, and $$$110$$$.
2,200
false
false
false
true
false
false
false
false
false
false
1,423
1826A
There is a group of $$$n$$$ people. Some of them might be liars, who always tell lies. Other people always tell the truth. The $$$i$$$-th person says "There are at least $$$l_i$$$ liars amongst us". Determine if what people are saying is contradictory, or if it is possible. If it is possible, output the number of liars in the group. If there are multiple possible answers, output any one of them. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$) β€” the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 100$$$). The second line of each test case contains $$$n$$$ integers $$$l_i$$$ ($$$0 leq l_i leq n$$$) β€” the number said by the $$$i$$$-th person. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$10^4$$$. Output For each test case output a single integer. If what people are saying is contradictory, output $$$-1$$$. Otherwise, output the number of liars in the group. If there are multiple possible answers, output any one of them. Example Input 7 2 1 2 2 2 2 2 0 0 1 1 1 0 5 5 5 3 3 5 6 5 3 6 6 3 5 Note In the first example, the only possible answer is that the second person is a liar, so the answer is $$$1$$$ liar. In the second example, it can be proven that we can't choose the liars so that all the requirements are satisfied. In the third example, everybody tells the truth, so the answer is $$$0$$$ liars.
1,200
false
true
true
false
false
false
true
false
true
false
1,329
586A
Alena has successfully passed the entrance exams to the university and is now looking forward to start studying. One two-hour lesson at the Russian university is traditionally called a pair, it lasts for two academic hours (an academic hour is equal to 45 minutes). The University works in such a way that every day it holds exactly _n_ lessons. Depending on the schedule of a particular group of students, on a given day, some pairs may actually contain classes, but some may be empty (such pairs are called breaks). The official website of the university has already published the schedule for tomorrow for Alena's group. Thus, for each of the _n_ pairs she knows if there will be a class at that time or not. Alena's House is far from the university, so if there are breaks, she doesn't always go home. Alena has time to go home only if the break consists of at least two free pairs in a row, otherwise she waits for the next pair at the university. Of course, Alena does not want to be sleepy during pairs, so she will sleep as long as possible, and will only come to the first pair that is presented in her schedule. Similarly, if there are no more pairs, then Alena immediately goes home. Alena appreciates the time spent at home, so she always goes home when it is possible, and returns to the university only at the beginning of the next pair. Help Alena determine for how many pairs she will stay at the university. Note that during some pairs Alena may be at the university waiting for the upcoming pair. Input The first line of the input contains a positive integer _n_ (1u2009≀u2009_n_u2009≀u2009100) β€” the number of lessons at the university. The second line contains _n_ numbers _a__i_ (0u2009≀u2009_a__i_u2009≀u20091). Number _a__i_ equals 0, if Alena doesn't have the _i_-th pairs, otherwise it is equal to 1. Numbers _a_1,u2009_a_2,u2009...,u2009_a__n_ are separated by spaces. Note In the first sample Alena stays at the university from the second to the fifth pair, inclusive, during the third pair she will be it the university waiting for the next pair. In the last sample Alena doesn't have a single pair, so she spends all the time at home.
900
false
false
true
false
false
false
false
false
false
false
7,499
1523E
To monitor cryptocurrency exchange rates trader William invented a wonderful device consisting of $$$n$$$ lights arranged in a row. The device functions in the following way: Initially, all lights on William's device are turned off. At the beginning of a new iteration the device randomly, with a uniform distribution, picks a light that is turned off and turns it on, telling William which cryptocurrency he should invest in. After this iteration if any $$$k$$$ consecutive lights contain more than one turned on light, then the device finishes working. William doesn't like uncertainty, so he wants you to calculate the expected value of the number of lights that are turned on in the device after it finishes working. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10$$$). Description of the test cases follows. The only line for each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 le k le n le 10^5$$$), which are the total number of lights and the length of subsegment of lights that are being checked, respectively. Output For each test case print the answer, modulo $$$10^9+7$$$. Formally, let $$$M = 10^9+7$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output the integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 le x < M$$$ and $$$x cdot q equiv p pmod{M}$$$. Example Output 333333338 141946947 329622137 Note Explanation of the first sample test case: Let's write out all possible sequences of light toggles, which will make the device complete its operation: 1. $$$(1, 2)$$$ xa0β€” $$$2$$$ lights are turned on 2. $$$(1, 3, 2)$$$ xa0β€” $$$3$$$ lights are turned on 3. $$$(2, 1)$$$ xa0β€” $$$2$$$ lights are turned on 4. $$$(2, 3)$$$ xa0β€” $$$2$$$ lights are turned on 5. $$$(3, 2)$$$ xa0β€” $$$2$$$ lights are turned on 6. $$$(3, 1, 2)$$$ xa0β€” $$$3$$$ lights are turned on Then the final expected value will be equal to $$$frac{2}{6}$$$ + $$$frac{3}{6}$$$ + $$$frac{2}{6}$$$ + $$$frac{2}{6}$$$ + $$$frac{2}{6}$$$ + $$$frac{3}{6}$$$ = $$$frac{14}{6}$$$ = $$$frac{7}{3}$$$. Then the required output will be $$$333333338$$$, since $$$333333338 cdot 3 equiv 7 pmod{10^9+7}$$$.
2,600
true
false
false
true
false
false
false
false
false
false
3,046
2009A
Problem - 2009A - 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 math *800 No tag edit access β†’ Contest materials ") Editorial") β€” Video Tutorial") . Over all possible integer values of $$$c$$$ ($$$a leq c leq b$$$), find the minimum value of $$$(c - a) + (b - c)$$$. Input The first line contains $$$t$$$ ($$$1 leq t leq 55$$$) β€” the number of test cases. Each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 leq a leq b leq 10$$$). Output For each test case, output the minimum possible value of $$$(c - a) + (b - c)$$$ on a new line. Example Input 3 1 2 3 10 5 5 Output 1 7 0 Note In the first test case, you can choose $$$c = 1$$$ and obtain an answer of $$$(1 - 1) + (2 - 1) = 1$$$. It can be shown this is the minimum value possible. In the second test case, you can choose $$$c = 6$$$ and obtain an answer of $$$(6 - 3) + (10 - 6) = 7$$$. It can be shown this is the minimum value possible.
800
true
false
false
false
false
false
true
false
false
false
200
1845C
Monocarp finally got the courage to register on ForceCoders. He came up with a handle but is still thinking about the password. He wants his password to be as strong as possible, so he came up with the following criteria: the length of the password should be exactly $$$m$$$; the password should only consist of digits from $$$0$$$ to $$$9$$$; the password should not appear in the password database (given as a string $$$s$$$) as a subsequence (not necessarily contiguous). Monocarp also came up with two strings of length $$$m$$$: $$$l$$$ and $$$r$$$, both consisting only of digits from $$$0$$$ to $$$9$$$. He wants the $$$i$$$-th digit of his password to be between $$$l_i$$$ and $$$r_i$$$, inclusive. Does there exist a password that fits all criteria? 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 a string $$$s$$$ ($$$1 le s le 3 cdot 10^5$$$), consisting only of digits from $$$0$$$ to $$$9$$$xa0β€” the password database. The second line contains a single integer $$$m$$$ ($$$1 le m le 10$$$)xa0β€” the required length of the password. The third line contains a string $$$l$$$ ($$$l = m$$$), consisting only of digits from $$$0$$$ to $$$9$$$xa0β€” the lower restriction on each digit. The fourth line contains a string $$$r$$$ ($$$r = m$$$), consisting only of digits from $$$0$$$ to $$$9$$$xa0β€” the upper restriction on each digit. $$$l_i le r_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The sum of lengths of $$$s$$$ over all testcases doesn't exceed $$$3 cdot 10^5$$$. Output For each testcase, print "YES" if there exists a password that fits all criteria. Print "NO" otherwise. Example Input 5 88005553535123456 2 50 56 123412341234 3 111 444 1234 4 4321 4321 459 2 49 59 00010 2 10 11 Note In the first testcase, Monocarp can choose password "50". It doesn't appear in $$$s$$$ as a subsequence. In the second testcase, all combinations of three digits, each of them being from $$$1$$$ to $$$4$$$, fit the criteria on $$$l$$$ and $$$r$$$. However, all of them appear in $$$s$$$ as subsequences. For example, "314" appears at positions $$$[3, 5, 12]$$$ and "222" appears at positions $$$[2, 6, 10]$$$. In the third testcase, Monocarp can choose password "4321". Actually, that is the only password that fits the criteria on $$$l$$$ and $$$r$$$. Luckily, it doesn't appear in $$$s$$$ as a subsequence. In the fourth testcase, only "49" and "59" fit the criteria on $$$l$$$ and $$$r$$$. Both of them appear in $$$s$$$ as subsequences. In the fifth testcase, Monocarp can choose password "11".
1,400
false
true
false
true
false
false
false
true
false
false
1,212
127B
Problem - 127B - 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 , he needs four sticks of the same length. Now Nicholas wants to make from the sticks that he has as many frames as possible; to be able to paint as many canvases as possible to fill the frames. Help him in this uneasy task. Note that it is not necessary to use all the sticks Nicholas has. Input The first line contains an integer _n_ (1u2009≀u2009_n_u2009≀u2009100) β€” the number of sticks. The second line contains _n_ space-separated integers. The _i_-th integer equals the length of the _i_-th stick _a__i_ (1u2009≀u2009_a__i_u2009≀u2009100). Output Print the single number β€” the maximum number of frames Nicholas can make for his future canvases. Examples Input 5 2 4 3 2 3 Output 1 Input 13 2 2 4 4 4 4 6 6 6 7 7 9 9 Output 3 Input 4 3 3 3 5 Output 0
1,000
false
false
true
false
false
false
false
false
false
false
9,371
57C
Problem - 57C - Codeforces =============== xa0 . Output You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007. Examples Input 2 Output 4 Input 3 Output 17
1,900
true
false
false
false
false
false
false
false
false
false
9,688
1685A
You are given $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$. Is it possible to arrange them on a circle so that each number is strictly greater than both its neighbors or strictly smaller than both its neighbors? In other words, check if there exists a rearrangement $$$b_1, b_2, ldots, b_n$$$ of the integers $$$a_1, a_2, ldots, a_n$$$ such that for each $$$i$$$ from $$$1$$$ to $$$n$$$ at least one of the following conditions holds: $$$b_{i-1} < b_i > b_{i+1}$$$ $$$b_{i-1} > b_i < b_{i+1}$$$ To make sense of the previous formulas for $$$i=1$$$ and $$$i=n$$$, one shall define $$$b_0=b_n$$$ and $$$b_{n+1}=b_1$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 3cdot 10^4$$$) xa0β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 10^5$$$) xa0β€” the number of integers. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$2cdot 10^5$$$. Output For each test case, if it is not possible to arrange the numbers on the circle satisfying the conditions from the statement, output $$$ exttt{NO}$$$. You can output each letter in any case. Otherwise, output $$$ exttt{YES}$$$. In the second line, output $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$, which are a rearrangement of $$$a_1, a_2, ldots, a_n$$$ and satisfy the conditions from the statement. If there are multiple valid ways to arrange the numbers, you can output any of them. Example Input 4 3 1 1 2 4 1 9 8 4 4 2 0 2 2 6 1 1 1 11 111 1111 Output NO YES 1 8 4 9 NO YES 1 11 1 111 1 1111 Note It can be shown that there are no valid arrangements for the first and the third test cases. In the second test case, the arrangement $$$[1, 8, 4, 9]$$$ works. In this arrangement, $$$1$$$ and $$$4$$$ are both smaller than their neighbors, and $$$8, 9$$$ are larger. In the fourth test case, the arrangement $$$[1, 11, 1, 111, 1, 1111]$$$ works. In this arrangement, the three elements equal to $$$1$$$ are smaller than their neighbors, while all other elements are larger than their neighbors.
1,100
false
true
false
false
false
true
false
false
true
false
2,165
196D
In problems on strings one often has to find a string with some particular properties. The problem authors were reluctant to waste time on thinking of a name for some string so they called it good. A string is good if it doesn't have palindrome substrings longer than or equal to _d_. You are given string _s_, consisting only of lowercase English letters. Find a good string _t_ with length _s_, consisting of lowercase English letters, which is lexicographically larger than _s_. Of all such strings string _t_ must be lexicographically minimum. We will call a non-empty string _s_[_a_xa0...xa0_b_]u2009=u2009_s__a__s__a_u2009+u20091... _s__b_ (1u2009≀u2009_a_u2009≀u2009_b_u2009≀u2009_s_) a substring of string _s_u2009=u2009_s_1_s_2... _s__s_. A non-empty string _s_u2009=u2009_s_1_s_2... _s__n_ is called a palindrome if for all _i_ from 1 to _n_ the following fulfills: _s__i_u2009=u2009_s__n_u2009-u2009_i_u2009+u20091. In other words, palindrome read the same in both directions. String _x_u2009=u2009_x_1_x_2... _x__x_ is lexicographically larger than string _y_u2009=u2009_y_1_y_2... _y__y_, if either _x_u2009>u2009_y_ and _x_1u2009=u2009_y_1,u2009_x_2u2009=u2009_y_2,u2009... ,u2009_x__y_u2009=u2009_y__y_, or there exists such number _r_ (_r_u2009<u2009_x_,u2009_r_u2009<u2009_y_), that _x_1u2009=u2009_y_1,u2009_x_2u2009=u2009_y_2,u2009... ,u2009_x__r_u2009=u2009_y__r_ and _x__r_u2009+u20091u2009>u2009_y__r_u2009+u20091. Characters in such strings are compared like their ASCII codes. Input The first line contains integer _d_ (1u2009≀u2009_d_u2009≀u2009_s_). The second line contains a non-empty string _s_, its length is no more than 4Β·105 characters. The string consists of lowercase English letters. Output Print the good string that lexicographically follows _s_, has the same length and consists of only lowercase English letters. If such string does not exist, print "Impossible" (without the quotes).
2,800
false
true
false
false
true
false
false
false
false
false
9,055
1320D
In this problem, we will deal with binary strings. Each character of a binary string is either a 0 or a 1. We will also deal with substrings; recall that a substring is a contiguous subsequence of a string. We denote the substring of string $$$s$$$ starting from the $$$l$$$-th character and ending with the $$$r$$$-th character as $$$s[l dots r]$$$. The characters of each string are numbered from $$$1$$$. We can perform several operations on the strings we consider. Each operation is to choose a substring of our string and replace it with another string. There are two possible types of operations: replace 011 with 110, or replace 110 with 011. For example, if we apply exactly one operation to the string 110011110, it can be transformed into 011011110, 110110110, or 110011011. Binary string $$$a$$$ is considered reachable from binary string $$$b$$$ if there exists a sequence $$$s_1$$$, $$$s_2$$$, ..., $$$s_k$$$ such that $$$s_1 = a$$$, $$$s_k = b$$$, and for every $$$i in [1, k - 1]$$$, $$$s_i$$$ can be transformed into $$$s_{i + 1}$$$ using exactly one operation. Note that $$$k$$$ can be equal to $$$1$$$, i.u2009e., every string is reachable from itself. You are given a string $$$t$$$ and $$$q$$$ queries to it. Each query consists of three integers $$$l_1$$$, $$$l_2$$$ and $$$len$$$. To answer each query, you have to determine whether $$$t[l_1 dots l_1 + len - 1]$$$ is reachable from $$$t[l_2 dots l_2 + len - 1]$$$. Input The first line contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β€” the length of string $$$t$$$. The second line contains one string $$$t$$$ ($$$t = n$$$). Each character of $$$t$$$ is either 0 or 1. The third line contains one integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$) β€” the number of queries. Then $$$q$$$ lines follow, each line represents a query. The $$$i$$$-th line contains three integers $$$l_1$$$, $$$l_2$$$ and $$$len$$$ ($$$1 le l_1, l_2 le t$$$, $$$1 le len le t - max(l_1, l_2) + 1$$$) for the $$$i$$$-th query. Output For each query, print either YES if $$$t[l_1 dots l_1 + len - 1]$$$ is reachable from $$$t[l_2 dots l_2 + len - 1]$$$, or NO otherwise. You may print each letter in any register.
2,500
false
false
false
false
true
false
false
false
false
false
4,112
2020A
You are given two integers $$$n$$$ and $$$k$$$. In one operation, you can subtract any power of $$$k$$$ from $$$n$$$. Formally, in one operation, you can replace $$$n$$$ by $$$(n-k^x)$$$ for any non-negative integer $$$x$$$. Find the minimum number of operations required to make $$$n$$$ equal to $$$0$$$. 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 two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 10^9$$$). Output For each test case, output the minimum number of operations on a new line. Example Input 6 5 2 3 5 16 4 100 3 6492 10 10 1 Note In the first test case, $$$n = 5$$$ and $$$k = 2$$$. We can perform the following sequence of operations: 1. Subtract $$$2^0 = 1$$$ from $$$5$$$. The current value of $$$n$$$ becomes $$$5 - 1 = 4$$$. 2. Subtract $$$2^2 = 4$$$ from $$$4$$$. The current value of $$$n$$$ becomes $$$4 - 4 = 0$$$. It can be shown that there is no way to make $$$n$$$ equal to $$$0$$$ in less than $$$2$$$ operations. Thus, $$$2$$$ is the answer. In the second test case, $$$n = 3$$$ and $$$k = 5$$$. We can perform the following sequence of operations: 1. Subtract $$$5^0 = 1$$$ from $$$3$$$. The current value of $$$n$$$ becomes $$$3 - 1 = 2$$$. 2. Subtract $$$5^0 = 1$$$ from $$$2$$$. The current value of $$$n$$$ becomes $$$2 - 1 = 1$$$. 3. Subtract $$$5^0 = 1$$$ from $$$1$$$. The current value of $$$n$$$ becomes $$$1 - 1 = 0$$$. It can be shown that there is no way to make $$$n$$$ equal to $$$0$$$ in less than $$$3$$$ operations. Thus, $$$3$$$ is the answer.
800
true
true
false
false
false
false
true
false
false
false
152
1252K
Adding two numbers several times is a time-consuming task, so you want to build a robot. The robot should have a string $$$S = S_1 S_2 dots S_N$$$ of $$$N$$$ characters on its memory that represents addition instructions. Each character of the string, $$$S_i$$$, is either 'A' or 'B'. You want to be able to give $$$Q$$$ commands to the robot, each command is either of the following types: 1 $$$L$$$ $$$R$$$. The robot should toggle all the characters of $$$S_i$$$ where $$$L le i le R$$$. Toggling a character means changing it to 'A' if it was previously 'B', or changing it to 'B' if it was previously 'A'. 2 $$$L$$$ $$$R$$$ $$$A$$$ $$$B$$$. The robot should call $$$f(L, R, A, B)$$$ and return two integers as defined in the following pseudocode: ``` function f(L, R, A, B): FOR i from L to R if S[i] = 'A' A = A + B else B = A + B return (A, B) ``` You want to implement the robot's expected behavior. Input Input begins with a line containing two integers: $$$N$$$ $$$Q$$$ ($$$1 le N, Q le 100,000$$$) representing the number of characters in the robot's memory and the number of commands, respectively. The next line contains a string $$$S$$$ containing $$$N$$$ characters (each either 'A' or 'B') representing the initial string in the robot's memory. The next $$$Q$$$ lines each contains a command of the following types. 1 $$$L$$$ $$$R$$$ ($$$1 le L le R le N$$$) 2 $$$L$$$ $$$R$$$ $$$A$$$ $$$B$$$ ($$$1 le L le R le N$$$; $$$0 le A, B le 10^9$$$) There is at least one command of the second type. Output For each command of the second type in the same order as input, output in a line two integers (separated by a single space), the value of $$$A$$$ and $$$B$$$ returned by $$$f(L, R, A, B)$$$, respectively. As this output can be large, you need to modulo the output by $$$1,000,000,007$$$. Example Input 5 3 ABAAA 2 1 5 1 1 1 3 5 2 2 5 0 1000000000 Note Explanation for the sample input/output #1 For the first command, calling $$$f(L, R, A, B)$$$ causes the following: Initially, $$$A = 1$$$ and $$$B = 1$$$. At the end of $$$i = 1$$$, $$$A = 2$$$ and $$$B = 1$$$. At the end of $$$i = 2$$$, $$$A = 2$$$ and $$$B = 3$$$. At the end of $$$i = 3$$$, $$$A = 5$$$ and $$$B = 3$$$. At the end of $$$i = 4$$$, $$$A = 8$$$ and $$$B = 3$$$. At the end of $$$i = 5$$$, $$$A = 11$$$ and $$$B = 3$$$. Therefore, $$$f(L, R, A, B)$$$ will return $$$(11, 3)$$$. For the second command, string $$$S$$$ will be updated to "ABBBB". For the third command, the value of $$$A$$$ will always be $$$0$$$ and the value of $$$B$$$ will always be $$$1,000,000,000$$$. Therefore, $$$f(L, R, A, B)$$$ will return $$$(0, 1,000,000,000)$$$.
2,100
true
false
false
false
true
false
false
false
false
false
4,424
1086F
Berland forest was planted several decades ago in a formation of an infinite grid with a single tree in every cell. Now the trees are grown up and they form a pretty dense structure. So dense, actually, that the fire became a real danger for the forest. This season had been abnormally hot in Berland and some trees got caught on fire! The second fire started is considered the second $$$0$$$. Every second fire lit up all intact neightbouring trees to every currently burning tree. The tree is neighbouring if it occupies adjacent by side or by corner cell. Luckily, after $$$t$$$ seconds Berland fire department finally reached the location of fire and instantaneously extinguished it all. Now they want to calculate the destructive power of the fire. Let $$$val_{x, y}$$$ be the second the tree in cell $$$(x, y)$$$ got caught on fire. The destructive power is the sum of $$$val_{x, y}$$$ over all $$$(x, y)$$$ of burnt trees. Clearly, all the workers of fire department are firefighters, not programmers, thus they asked you to help them calculate the destructive power of the fire. The result can be rather big, so print it modulo $$$998244353$$$. Input The first line contains two integers $$$n$$$ and $$$t$$$ ($$$1 le n le 50$$$, $$$0 le t le 10^8$$$) β€” the number of trees that initially got caught on fire and the time fire department extinguished the fire, respectively. Each of the next $$$n$$$ lines contains two integers $$$x$$$ and $$$y$$$ ($$$-10^8 le x, y le 10^8$$$) β€” the positions of trees that initially got caught on fire. Obviously, the position of cell $$$(0, 0)$$$ on the grid and the directions of axes is irrelevant as the grid is infinite and the answer doesn't depend on them. It is guaranteed that all the given tree positions are pairwise distinct. The grid is infinite so the fire doesn't stop once it reaches $$$-10^8$$$ or $$$10^8$$$. It continues beyond these borders. Output Print a single integer β€” the sum of $$$val_{x, y}$$$ over all $$$(x, y)$$$ of burnt trees modulo $$$998244353$$$.
3,500
true
false
false
false
false
false
false
false
false
false
5,292
251D
Little Petya likes numbers a lot. Recently his mother has presented him a collection of _n_ non-negative integers. There's only one thing Petya likes more than numbers: playing with little Masha. He immediately decided to give a part of his new collection to her. To make the game even more interesting, Petya decided to give Masha such collection of numbers for which the following conditions fulfill: Let's introduce _x_1 to denote the _xor_ of all numbers Petya has got left; and let's introduce _x_2 to denote the _xor_ of all numbers he gave to Masha. Value (_x_1u2009+u2009_x_2) must be as large as possible. If there are multiple ways to divide the collection so that the previous condition fulfilled, then Petya minimizes the value _x_1. The _xor_ operation is a bitwise excluding "OR", that is denoted as "xor" in the Pascal language and "^" in C/C++/Java. Help Petya divide the collection as described above. If there are multiple suitable ways to divide it, find any of them. Please note that after Petya gives a part of his numbers to Masha, he may have no numbers left. The reverse situation is also possible, when Petya gives nothing to Masha. In both cases we must assume that the _xor_ of an empty set of numbers equals 0. Input The first line contains integer _n_ (1u2009≀u2009_n_u2009≀u2009105), showing how many numbers Petya's mother gave him. The second line contains the actual space-separated numbers. They are all integer, non-negative and do not exceed 1018. Output Print _n_ space-separated integers, the _i_-th of them should equal either 1, if Petya keeps the number that follows _i_-th in his collection, or it should equal 2, if Petya gives the corresponding number to Masha. The numbers are indexed in the order in which they are given in the input. Examples Input 3 1000000000000 1000000000000 1000000000000
2,700
true
false
false
false
false
false
false
false
false
false
8,828
1419F
There are $$$n$$$ detachments on the surface, numbered from $$$1$$$ to $$$n$$$, the $$$i$$$-th detachment is placed in a point with coordinates $$$(x_i, y_i)$$$. All detachments are placed in different points. Brimstone should visit each detachment at least once. You can choose the detachment where Brimstone starts. To move from one detachment to another he should first choose one of four directions of movement (up, right, left or down) and then start moving with the constant speed of one unit interval in a second until he comes to a detachment. After he reaches an arbitrary detachment, he can repeat the same process. Each $$$t$$$ seconds an orbital strike covers the whole surface, so at that moment Brimstone should be in a point where some detachment is located. He can stay with any detachment as long as needed. Brimstone is a good commander, that's why he can create at most one detachment and place it in any empty point with integer coordinates he wants before his trip. Keep in mind that Brimstone will need to visit this detachment, too. Help Brimstone and find such minimal $$$t$$$ that it is possible to check each detachment. If there is no such $$$t$$$ report about it. Input The first line contains a single integer $$$n$$$ $$$(2 le n le 1000)$$$xa0β€” the number of detachments. In each of the next $$$n$$$ lines there is a pair of integers $$$x_i$$$, $$$y_i$$$ $$$(x_i, y_i le 10^9)$$$xa0β€” the coordinates of $$$i$$$-th detachment. It is guaranteed that all points are different. Output Output such minimal integer $$$t$$$ that it is possible to check all the detachments adding at most one new detachment. If there is no such $$$t$$$, print $$$-1$$$. Examples Input 4 100 0 0 100 -100 0 0 -100 Input 7 0 2 1 0 -3 0 0 -2 -1 -1 -1 -3 -2 -3 Input 5 0 0 0 -1 3 0 -2 0 -2 1 Input 5 0 0 2 0 0 -1 -2 0 -2 1 Note In the first test it is possible to place a detachment in $$$(0, 0)$$$, so that it is possible to check all the detachments for $$$t = 100$$$. It can be proven that it is impossible to check all detachments for $$$t < 100$$$; thus the answer is $$$100$$$. In the second test, there is no such $$$t$$$ that it is possible to check all detachments, even with adding at most one new detachment, so the answer is $$$-1$$$. In the third test, it is possible to place a detachment in $$$(1, 0)$$$, so that Brimstone can check all the detachments for $$$t = 2$$$. It can be proven that it is the minimal such $$$t$$$. In the fourth test, there is no need to add any detachments, because the answer will not get better ($$$t = 2$$$). It can be proven that it is the minimal such $$$t$$$.
2,800
false
false
true
false
true
false
false
true
false
true
3,581
1105E
Hiasat registered a new account in NeckoForces and when his friends found out about that, each one of them asked to use his name as Hiasat's handle. Luckily for Hiasat, he can change his handle in some points in time. Also he knows the exact moments friends will visit his profile page. Formally, you are given a sequence of events of two types: $$$1$$$xa0β€” Hiasat can change his handle. $$$2$$$ $$$s$$$xa0β€” friend $$$s$$$ visits Hiasat's profile. The friend $$$s$$$ will be happy, if each time he visits Hiasat's profile his handle would be $$$s$$$. Hiasat asks you to help him, find the maximum possible number of happy friends he can get. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^5, 1 le m le 40$$$)xa0β€” the number of events and the number of friends. Then $$$n$$$ lines follow, each denoting an event of one of two types: $$$1$$$xa0β€” Hiasat can change his handle. $$$2$$$ $$$s$$$xa0β€” friend $$$s$$$ ($$$1 le s le 40$$$) visits Hiasat's profile. It's guaranteed, that each friend's name consists only of lowercase Latin letters. It's guaranteed, that the first event is always of the first type and each friend will visit Hiasat's profile at least once. Output Print a single integerxa0β€” the maximum number of happy friends. Examples Input 5 3 1 2 motarack 2 mike 1 2 light Input 4 3 1 2 alice 2 bob 2 tanyaromanova Note In the first example, the best way is to change the handle to the "motarack" in the first event and to the "light" in the fourth event. This way, "motarack" and "light" will be happy, but "mike" will not. In the second example, you can choose either "alice", "bob" or "tanyaromanova" and only that friend will be happy.
2,200
false
false
false
true
false
false
true
false
false
false
5,176
1520F1
This is an interactive problem. This is an easy version of the problem. The difference from the hard version is that in the easy version $$$t=1$$$ and the number of queries is limited to $$$20$$$. Polycarp is playing a computer game. In this game, an array consisting of zeros and ones is hidden. Polycarp wins if he guesses the position of the $$$k$$$-th zero from the left $$$t$$$ times. Polycarp can make no more than $$$20$$$ requests of the following type: ? $$$l$$$ $$$r$$$xa0β€” find out the sum of all elements in positions from $$$l$$$ to $$$r$$$ ($$$1 le l le r le n$$$) inclusive. In this (easy version) of the problem, this paragraph doesn't really make sense since $$$t=1$$$ always. To make the game more interesting, each guessed zero turns into one and the game continues on the changed array. More formally, if the position of the $$$k$$$-th zero was $$$x$$$, then after Polycarp guesses this position, the $$$x$$$-th element of the array will be replaced from $$$0$$$ to $$$1$$$. Of course, this feature affects something only for $$$t>1$$$. Help Polycarp win the game. Interaction First, your program must read two integers $$$n$$$ and $$$t$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$t=1$$$). Then $$$t$$$ lines follow, each of which contains one integer $$$k$$$ ($$$1 le k le n$$$). It is guaranteed that at the moment of the request the array contains at least $$$k$$$ zeros. In order to get the next value of $$$k$$$, you must output the answer for the current value of $$$k$$$. After that, you can make no more than $$$20$$$ requests. Use the following format to output the answer (it is not a request, it doesn't count in $$$20$$$): ! $$$x$$$xa0β€” position of the $$$k$$$-th zero. Positions in the array are numbered from left to right from $$$1$$$ to $$$n$$$ inclusive. After printing $$$t$$$ answers, your program should exit immediately. In this task, the interactor is not adaptive. This means that within the same test, the hidden array and the queries do not change. In case of an incorrect query, -1 will be displayed. When this value is received, your program must immediately exit normally (for example, by calling exit(0)), otherwise, the testing system may issue an arbitrary verdict. If the number of requests is exceeded, the verdict wrong answer will be displayed. Your solution may get the verdict Idleness limit exceeded if you don't print anything or forget to flush the output buffer. To flush the output buffer, you need to do the following immediately after the query output and the end-of-line character: 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. Hacks Use the following format for hacks: On the first line print the string $$$s$$$ ($$$1 le s le 2 cdot 10^5$$$), consisting of zeros and ones, and an integer $$$t$$$ ($$$t = 1$$$)xa0β€” hidden array and number of requests, respectively. In the next $$$t$$$ lines output the number $$$k$$$ ($$$1 le k le s$$$). The hacked solution will not have direct access to the hidden array. Note In the first test, the $$$[1, 0, 1, 1, 0, 1]$$$ array is hidden. In this test $$$k=2$$$.
1,600
false
false
false
false
false
false
false
true
false
false
3,058
1335B
You are given three positive integers $$$n$$$, $$$a$$$ and $$$b$$$. You have to construct a string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters such that each substring of length $$$a$$$ has exactly $$$b$$$ distinct letters. It is guaranteed that the answer exists. You have to answer $$$t$$$ independent test cases. Recall that the substring $$$s[l dots r]$$$ is the string $$$s_l, s_{l+1}, dots, s_{r}$$$ and its length is $$$r - l + 1$$$. In this problem you are only interested in substrings of length $$$a$$$. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2000$$$) β€” the number of test cases. Then $$$t$$$ test cases follow. The only line of a test case contains three space-separated integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$1 le a le n le 2000, 1 le b le min(26, a)$$$), where $$$n$$$ is the length of the required string, $$$a$$$ is the length of a substring and $$$b$$$ is the required number of distinct letters in each substring of length $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$ ($$$sum n le 2000$$$). Output For each test case, print the answer β€” such a string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters that each substring of length $$$a$$$ has exactly $$$b$$$ distinct letters. If there are multiple valid answers, print any of them. It is guaranteed that the answer exists. Example Input 4 7 5 3 6 1 1 6 6 1 5 2 2 Output tleelte qwerty vvvvvv abcde Note In the first test case of the example, consider all the substrings of length $$$5$$$: "tleel": it contains $$$3$$$ distinct (unique) letters, "leelt": it contains $$$3$$$ distinct (unique) letters, "eelte": it contains $$$3$$$ distinct (unique) letters.
900
false
false
false
false
false
true
false
false
false
false
4,029
1799G
There are $$$n$$$ people that will participate in voting. Each person has exactly one vote. $$$i$$$-th person has a team $$$t_i$$$ ($$$1 leq t_i leq n$$$) where $$$t_i = t_j$$$ means $$$i$$$, $$$j$$$ are in the same team. By the rules each person should vote for the person from the different team. Note that it automatically means that each person can't vote for himself. Each person knows the number of votes $$$c_i$$$ he wants to get. How many possible votings exists, such that each person will get the desired number of votes? Due to this number can be big, find it by modulo $$$998,244,353$$$. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 200$$$) β€” the number of people. The second line contains $$$n$$$ integers $$$c_1, c_2, ldots, c_n$$$ ($$$0 leq c_i leq n$$$) β€” desired number of votes. It is guaranteed, that $$$sumlimits_{i=1}^{n} c_i = n$$$. The third line contains $$$n$$$ integers $$$t_1, t_2, ldots, t_n$$$ ($$$1 leq t_i leq n$$$) β€” team numbers. Output Print a single integer β€” the number of possible votings by modulo $$$998,244,353$$$. Examples Input 5 2 0 1 0 2 1 2 3 4 5 Input 5 1 2 2 0 0 3 5 4 3 4 Note In the first test there are two possible votings: $$$(2, 3, 1)$$$, $$$(3, 1, 2)$$$. In the third test there are five possible votings: $$$(3, 3, 2, 2, 1)$$$, $$$(2, 3, 2, 3, 1)$$$, $$$(3, 3, 1, 2, 2)$$$, $$$(3, 1, 2, 3, 2)$$$, $$$(2, 3, 1, 3, 2)$$$.
2,600
true
false
false
true
false
false
false
false
false
false
1,477
825B
Alice and Bob play 5-in-a-row game. They have a playing field of size 10u2009Γ—u200910. In turns they put either crosses or noughts, one at a time. Alice puts crosses and Bob puts noughts. In current match they have made some turns and now it's Alice's turn. She wonders if she can put cross in such empty cell that she wins immediately. Alice wins if some crosses in the field form line of length not smaller than 5. This line can be horizontal, vertical and diagonal. Input You are given matrix 10u2009Γ—u200910 (10 lines of 10 characters each) with capital Latin letters 'X' being a cross, letters 'O' being a nought and '.' being an empty cell. The number of 'X' cells is equal to the number of 'O' cells and there is at least one of each type. There is at least one empty cell. It is guaranteed that in the current arrangement nobody has still won. Output Print 'YES' if it's possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print 'NO'. Examples Input XX.XX..... .....OOOO. .......... .......... .......... .......... .......... .......... .......... .......... Input XXOXX..... OO.O...... .......... .......... .......... .......... .......... .......... .......... ..........
1,600
false
false
true
false
false
false
true
false
false
false
6,461
1481F
Kilani and Abd are neighbors for 3000 years, but then the day came and Kilani decided to move to another house. As a farewell gift, Kilani is going to challenge Abd with a problem written by their other neighbor with the same name Abd. The problem is: You are given a connected tree rooted at node $$$1$$$. You should assign a character a or b to every node in the tree so that the total number of a's is equal to $$$x$$$ and the total number of b's is equal to $$$n - x$$$. Let's define a string for each node $$$v$$$ of the tree as follows: if $$$v$$$ is root then the string is just one character assigned to $$$v$$$: otherwise, let's take a string defined for the $$$v$$$'s parent $$$p_v$$$ and add to the end of it a character assigned to $$$v$$$. You should assign every node a character in a way that minimizes the number of distinct strings among the strings of all nodes. Input The first line contains two integers $$$n$$$ and $$$x$$$ ($$$1 leq n leq 10^5$$$; $$$0 leq x leq n$$$)xa0β€” the number of vertices in the tree the number of a's. The second line contains $$$n - 1$$$ integers $$$p_2, p_3, dots, p_{n}$$$ ($$$1 leq p_i leq n$$$; $$$p_i eq i$$$), where $$$p_i$$$ is the parent of node $$$i$$$. It is guaranteed that the input describes a connected tree. Output In the first line, print the minimum possible total number of distinct strings. In the second line, print $$$n$$$ characters, where all characters are either a or b and the $$$i$$$-th character is the character assigned to the $$$i$$$-th node. Make sure that the total number of a's is equal to $$$x$$$ and the total number of b's is equal to $$$n - x$$$. If there is more than one answer you can print any of them. Note The tree from the sample is shown below: The tree after assigning characters to every node (according to the output) is the following: Strings for all nodes are the following: string of node $$$1$$$ is: a string of node $$$2$$$ is: aa string of node $$$3$$$ is: aab string of node $$$4$$$ is: aab string of node $$$5$$$ is: aabb string of node $$$6$$$ is: aabb string of node $$$7$$$ is: aabb string of node $$$8$$$ is: aabb string of node $$$9$$$ is: aa The set of unique strings is $$${ ext{a}, ext{aa}, ext{aab}, ext{aabb}}$$$, so the number of distinct strings is $$$4$$$.
3,100
false
true
false
true
false
false
false
false
false
false
3,268
57D
Stewie the Rabbit explores a new parallel universe. This two dimensional universe has the shape of a rectangular grid, containing _n_ lines and _m_ columns. The universe is very small: one cell of the grid can only contain one particle. Each particle in this universe is either static or dynamic. Each static particle always remains in one and the same position. Due to unintelligible gravitation laws no two static particles in the parallel universe can be present in one column or row, and they also can't be present in the diagonally adjacent cells. A dynamic particle appears in a random empty cell, randomly chooses the destination cell (destination cell may coincide with the start cell, see the samples) and moves there along the shortest path through the cells, unoccupied by the static particles. All empty cells have the same probability of being selected as the beginning or end of the path. Having reached the destination cell, the particle disappears. Only one dynamic particle can exist at one moment of time. This particle can move from a cell to a cell if they have an adjacent side, and this transition takes exactly one galactic second. Stewie got interested in what is the average lifespan of one particle in the given universe. Input The first line contains two space-separated integers: _n_,u2009_m_ (2u2009≀u2009_n_,u2009_m_u2009≀u20091000) which represent the sizes of the universe. The next _n_ lines containing _m_ symbols each describe the universe without dynamic particles β€” the _j_-th symbol of the _i_-th line equals to 'X' if the cell is occupied by a static particle, and to '.' if it is empty. It is guaranteed that the described universe satisfies the properties described above, that is no two static particles can be in one column or in one row, besides, they can't be positioned in the diagonally adjacent cells. Output You have to print on a single line a single number which is the average life span of a particle with an accuracy of at least 6 decimal places. The answer will be accepted if it is within 10u2009-u20096 of absolute or relative error from the correct answer.
2,500
true
false
false
true
false
false
false
false
false
false
9,687
887A
Problem - 887A - 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 ") Editorial") if it's possible to remove digits required way and Β«noΒ» otherwise. Examples Input 100010001 Output yes Input 100 Output no Note In the first test case, you can get string 1 000 000 after removing two ones which is a representation of number 64 in the binary numerical system. You can read more about binary numeral system representation here:
1,000
false
false
true
false
false
false
false
false
false
false
6,202
1365F
# Swaps Again Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes Ayush, Ashish and Vivek are busy preparing a new problem for the next Codeforces round and need help checking if their test cases are valid. Each test case consists of an integer n and two arrays a and b, of size n. If after some (possibly zero) operations described below, array a can be transformed into array b, the input is said to be valid .Otherwise, it is invalid .An operation on array a is: β€’ select an integer k (1 ≀ k ≀ b n > 2 c) β€’ swap the prefix of length k with the suffix of length k For example, if array a initially is {1, 2, 3, 4, 5, 6}, after performing an operation with k = 2 , it is transformed into {5, 6, 3, 4, 1, 2}.Given the set of test cases, help them determine if each one is valid or invalid . # Input The first line contains one integer t (1 ≀ t ≀ 500) x16 the number of test cases. The description of each test case is as follows. The first line of each test case contains a single integer n (1 ≀ n ≀ 500) x16 the size of the arrays. The second line of each test case contains n integers a1, a2, ..., an (1 ≀ ai ≀ 10 9) x16 elements of array a.The third line of each test case contains n integers b1, b2, ..., bn (1 ≀ bi ≀ 10 9) x16 elements of array b. # Output For each test case, print β€œ Yes ” if the given input is valid . Otherwise print β€œ No ”. You may print the answer in any case. # Example standard input standard output 521 2 2 1 31 2 3 1 2 3 31 2 4 1 3 4 41 2 3 2 3 1 2 2 31 2 3 1 3 2 yes yes No yes No Page 1 of 2 Note For the first test case, we can swap prefix a[1 : 1] with suffix a[2 : 2] to get a = [2 , 1] .For the second test case, a is already equal to b.For the third test case, it is impossible since we cannot obtain 3 in a.For the fourth test case, we can first swap prefix a[1 : 1] with suffix a[4 : 4] to obtain a = [2 , 2, 3, 1] . Now we can swap prefix a[1 : 2] with suffix a[3 : 4] to obtain a = [3 , 1, 2, 2] .For the fifth test case, it is impossible to convert a to b. Page 2 of 2
2,100
false
false
true
false
false
true
false
false
true
false
3,852
113D
One day as Petya and his friend Vasya were having one of their numerous trips, they decided to visit a museum castle. The museum has a specific shape: it consists of _n_ rooms connected with _m_ corridors so that one can access any room from any other one. After the two friends had a little walk around the museum, they decided to split and watch the pieces of art each of them found interesting. They agreed to meet in one of the rooms at six p.m. However, they forgot one quite essential thing: they didn't specify the place to meet and when the time came, they started to rush about the museum looking for each other (they couldn't call each other as roaming made a call's cost skyrocket). Yet, even despite the whole rush, they couldn't get enough of the pieces of art, that's why each of them has the following strategy: each minute he make a decision where to go β€” with probability _p__i_ he doesn't move to any other place during this minute (i.e. he stays in the room). With probability 1u2009-u2009_p__i_ he equiprobably choose one of the adjacent rooms and went there along the corridor. Here _i_ is the ordinal number of the current room. Building was expensive in ancient times, that's why each corridor connected two different rooms, and any two rooms had no more than one corridor between them. The boys act simultaneously. As the corridors are dark, it is impossible to meet there; however, one can walk along the corridors in both directions (besides, the two boys can be going through the same corridor simultaneously without meeting). The boys act like that until they meet each other. More formally, the two friends meet when at some moment of time both of them decided to appear in the same room. For each room find the probability that the boys will meet there considering that at 6 p.m. they are positioned in rooms _a_ and _b_ correspondingly. Input The first line contains four integers: _n_ (1u2009≀u2009_n_u2009≀u200922), representing the numbers of rooms; _m_ , representing the number of corridors; _a_,u2009_b_ (1u2009≀u2009_a_,u2009_b_u2009≀u2009_n_), representing the numbers of Petya's and Vasya's starting rooms correspondingly. Next _m_ lines contain pairs of numbers β€” the numbers of rooms connected by a corridor. Next _n_ lines contain probabilities _p__i_ (0.01u2009≀u2009_p__i_u2009≀u20090.99) with the accuracy of up to four digits after the decimal point β€” the probability to stay in room _i_. It is guaranteed that every room can be reached from every other room by corridors.
2,700
true
false
false
false
false
false
false
false
false
false
9,432
1103C
Today is tuesday, that means there is a dispute in JOHNNY SOLVING team again: they try to understand who is Johnny and who is Solving. That's why guys asked Umnik to help them. Umnik gave guys a connected graph with $$$n$$$ vertices without loops and multiedges, such that a degree of any vertex is at least $$$3$$$, and also he gave a number $$$1 leq k leq n$$$. Because Johnny is not too smart, he promised to find a simple path with length at least $$$frac{n}{k}$$$ in the graph. In reply, Solving promised to find $$$k$$$ simple by vertices cycles with representatives, such that: Length of each cycle is at least $$$3$$$. Length of each cycle is not divisible by $$$3$$$. In each cycle must be a representative - vertex, which belongs only to this cycle among all printed cycles. You need to help guys resolve the dispute, for that you need to find a solution for Johnny: a simple path with length at least $$$frac{n}{k}$$$ ($$$n$$$ is not necessarily divided by $$$k$$$), or solution for Solving: $$$k$$$ cycles that satisfy all the conditions above. If there is no any solution - print $$$-1$$$. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 leq k leq n leq 2.5 cdot 10^5, 1 leq m leq 5 cdot 10^5$$$) Next $$$m$$$ lines describe edges of the graph in format $$$v$$$, $$$u$$$ ($$$1 leq v, u leq n$$$). It's guaranteed that $$$v eq u$$$ and all $$$m$$$ pairs are distinct. It's guaranteed that a degree of each vertex is at least $$$3$$$. Output Print PATH in the first line, if you solve problem for Johnny. In the second line print the number of vertices in the path $$$c$$$ ($$$c geq frac{n}{k}$$$). And in the third line print vertices describing the path in route order. Print CYCLES in the first line, if you solve problem for Solving. In the following lines describe exactly $$$k$$$ cycles in the following format: in the first line print the size of the cycle $$$c$$$ ($$$c geq 3$$$). In the second line print the cycle in route order. Also, the first vertex in the cycle must be a representative. Print $$$-1$$$ if there is no any solution. The total amount of printed numbers in the output must be at most $$$10^6$$$. It's guaranteed, that if exists any solution then there is a correct output satisfies this restriction. Examples Input 4 6 2 1 2 1 3 1 4 2 3 2 4 3 4 Input 10 18 2 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 2 3 3 4 2 4 5 6 6 7 5 7 8 9 9 10 8 10 Output CYCLES 4 4 1 2 3 4 7 1 5 6
2,700
true
false
false
false
false
true
false
false
false
true
5,185
258C
Problem - 258C - 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 combinatorics dp math *2000 No tag edit access β†’ Contest materials operation of a non-empty set of positive integers. The result of the LCM operation of _k_ positive integers _x_1,u2009_x_2,u2009...,u2009_x__k_ is the minimum positive integer that is divisible by each of numbers _x__i_. Let's assume that there is a sequence of integers _b_1,u2009_b_2,u2009...,u2009_b__n_. Let's denote their LCMs as _lcm_(_b_1,u2009_b_2,u2009...,u2009_b__n_) and the maximum of them as _max_(_b_1,u2009_b_2,u2009...,u2009_b__n_). The Little Elephant considers a sequence _b_ good, if _lcm_(_b_1,u2009_b_2,u2009...,u2009_b__n_)u2009=u2009_max_(_b_1,u2009_b_2,u2009...,u2009_b__n_). The Little Elephant has a sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_. Help him find the number of good sequences of integers _b_1,u2009_b_2,u2009...,u2009_b__n_, such that for all _i_ (1u2009≀u2009_i_u2009≀u2009_n_) the following condition fulfills: 1u2009≀u2009_b__i_u2009≀u2009_a__i_. As the answer can be rather large, print the remainder from dividing it by 1000000007 (109u2009+u20097). Input The first line contains a single positive integer _n_ (1u2009≀u2009_n_u2009≀u2009105) β€” the number of integers in the sequence _a_. The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≀u2009_a__i_u2009≀u2009105) β€” sequence _a_. Output In the single line print a single integer β€” the answer to the problem modulo 1000000007 (109u2009+u20097). Examples Input 4 1 4 3 2 Output 15 Input 2 6 3 Output 13
2,000
true
false
false
true
false
false
false
true
false
false
8,800
1490B
You are given a number $$$n$$$ (divisible by $$$3$$$) and an array $$$a[1 dots n]$$$. In one move, you can increase any of the array elements by one. Formally, you choose the index $$$i$$$ ($$$1 le i le n$$$) and replace $$$a_i$$$ with $$$a_i + 1$$$. You can choose the same index $$$i$$$ multiple times for different moves. Let's denote by $$$c_0$$$, $$$c_1$$$ and $$$c_2$$$ the number of numbers from the array $$$a$$$ that have remainders $$$0$$$, $$$1$$$ and $$$2$$$ when divided by the number $$$3$$$, respectively. Let's say that the array $$$a$$$ has balanced remainders if $$$c_0$$$, $$$c_1$$$ and $$$c_2$$$ are equal. For example, if $$$n = 6$$$ and $$$a = [0, 2, 5, 5, 4, 8]$$$, then the following sequence of moves is possible: initially $$$c_0 = 1$$$, $$$c_1 = 1$$$ and $$$c_2 = 4$$$, these values are not equal to each other. Let's increase $$$a_3$$$, now the array $$$a = [0, 2, 6, 5, 4, 8]$$$; $$$c_0 = 2$$$, $$$c_1 = 1$$$ and $$$c_2 = 3$$$, these values are not equal. Let's increase $$$a_6$$$, now the array $$$a = [0, 2, 6, 5, 4, 9]$$$; $$$c_0 = 3$$$, $$$c_1 = 1$$$ and $$$c_2 = 2$$$, these values are not equal. Let's increase $$$a_1$$$, now the array $$$a = [1, 2, 6, 5, 4, 9]$$$; $$$c_0 = 2$$$, $$$c_1 = 2$$$ and $$$c_2 = 2$$$, these values are equal to each other, which means that the array $$$a$$$ has balanced remainders. Find the minimum number of moves needed to make the array $$$a$$$ have balanced remainders. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$3 le n le 3 cdot 10^4$$$)xa0β€” the length of the array $$$a$$$. It is guaranteed that the number $$$n$$$ is divisible by $$$3$$$. The next line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 100$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$150,000$$$. Output For each test case, output one integerxa0β€” the minimum number of moves that must be made for the $$$a$$$ array to make it have balanced remainders. Example Input 4 6 0 2 5 5 4 8 6 2 0 2 1 0 0 9 7 1 3 4 2 10 3 9 6 6 0 1 2 3 4 5 Note The first test case is explained in the statements. In the second test case, you need to make one move for $$$i=2$$$. The third test case you need to make three moves: the first move: $$$i=9$$$; the second move: $$$i=9$$$; the third move: $$$i=2$$$. In the fourth test case, the values $$$c_0$$$, $$$c_1$$$ and $$$c_2$$$ initially equal to each other, so the array $$$a$$$ already has balanced remainders.
1,000
true
false
false
false
false
true
true
false
false
false
3,228
1500A
It was the third month of remote learning, Nastya got sick of staying at dormitory, so she decided to return to her hometown. In order to make her trip more entertaining, one of Nastya's friend presented her an integer array $$$a$$$. Several hours after starting her journey home Nastya remembered about the present. To entertain herself she decided to check, are there four different indices $$$x, y, z, w$$$ such that $$$a_x + a_y = a_z + a_w$$$. Her train has already arrived the destination, but she still hasn't found the answer. Can you help her unravel the mystery? Input The first line contains the single integer $$$n$$$ ($$$4 leq n leq 200,000$$$)xa0β€” the size of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 2.5 cdot 10^6$$$). Output Print "YES" if there are such four indices, and "NO" otherwise. If such indices exist, print these indices $$$x$$$, $$$y$$$, $$$z$$$ and $$$w$$$ ($$$1 le x, y, z, w le n$$$). If there are multiple answers, print any of them. Note In the first example $$$a_2 + a_3 = 1 + 5 = 2 + 4 = a_1 + a_6$$$. Note that there are other answer, for example, 2 3 4 6. In the second example, we can't choose four indices. The answer 1 2 2 3 is wrong, because indices should be different, despite that $$$a_1 + a_2 = 1 + 3 = 3 + 1 = a_2 + a_3$$$
1,800
true
false
true
false
false
false
true
false
false
false
3,168
155A
Vasya adores sport programming. He can't write programs but he loves to watch the contests' progress. Vasya even has a favorite coder and Vasya pays special attention to him. One day Vasya decided to collect the results of all contests where his favorite coder participated and track the progress of his coolness. For each contest where this coder participated, he wrote out a single non-negative number β€” the number of points his favorite coder earned in the contest. Vasya wrote out the points for the contest in the order, in which the contests run (naturally, no two contests ran simultaneously). Vasya considers a coder's performance in a contest amazing in two situations: he can break either his best or his worst performance record. First, it is amazing if during the contest the coder earns strictly more points that he earned on each past contest. Second, it is amazing if during the contest the coder earns strictly less points that he earned on each past contest. A coder's first contest isn't considered amazing. Now he wants to count the number of amazing performances the coder had throughout his whole history of participating in contests. But the list of earned points turned out long and Vasya can't code... That's why he asks you to help him. Input The first line contains the single integer _n_ (1u2009≀u2009_n_u2009≀u20091000) β€” the number of contests where the coder participated. The next line contains _n_ space-separated non-negative integer numbers β€” they are the points which the coder has earned. The points are given in the chronological order. All points do not exceed 10000. Output Print the single number β€” the number of amazing performances the coder has had during his whole history of participating in the contests. Examples Input 10 4664 6496 5814 7010 5762 5736 6944 4850 3698 7242 Note In the first sample the performances number 2 and 3 are amazing. In the second sample the performances number 2, 4, 9 and 10 are amazing.
800
false
false
false
false
false
false
true
false
false
false
9,254
1744F
You are given a permutation $$$p_1, p_2, ldots, p_n$$$ of length $$$n$$$ of numbers $$$0, ldots, n - 1$$$. Count the number of subsegments $$$1 leq l leq r leq n$$$ of this permutation such that $$$mex(p_l, p_{l+1}, ldots, p_r) > med(p_l, p_{l+1}, ldots, p_r)$$$. $$$mex$$$ of $$$S$$$ is the smallest non-negative integer that does not occur in $$$S$$$. For example: $$$mex({0, 1, 2, 3}) = 4$$$ $$$mex({0, 4, 1, 3}) = 2$$$ $$$mex({5, 4, 0, 1, 2}) = 3$$$ $$$med$$$ of the set $$$S$$$ is the median of the set, i.e. the element that, after sorting the elements in non-decreasing order, will be at position number $$$left lfloor{ frac{S + 1}{2} } ight floor$$$ (array elements are numbered starting from $$$1$$$ and here $$$left lfloor{v} ight floor$$$ denotes rounding $$$v$$$ down.). For example: $$$med({0, 1, 2, 3}) = 1$$$ $$$med({0, 4, 1, 3}) = 1$$$ $$$med({5, 4, 0, 1, 2}) = 2$$$ A sequence of $$$n$$$ numbers is called a permutation if it contains all the numbers from $$$0$$$ to $$$n - 1$$$ exactly once. Input The first line of the input contains a single integer $$$t$$$ $$$(1 leq t leq 10^4$$$), the number of test cases. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$), the length of the permutation $$$p$$$. The second line of each test case contains exactly $$$n$$$ integers: $$$p_1, p_2, ldots, p_n$$$ ($$$0 leq p_i leq n - 1$$$), elements of permutation $$$p$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case print the answer in a single line: the number of subsegments $$$1 leq l leq r leq n$$$ of this permutation such that $$$mex(p_l, p_{l+1}, ldots, p_r) > med(p_l, p_{l+1}, ldots, p_r)$$$. Example Input 8 1 0 2 1 0 3 1 0 2 4 0 2 1 3 5 3 1 0 2 4 6 2 0 4 1 3 5 8 3 7 2 6 0 1 5 4 4 2 0 1 3 Note The first test case contains exactly one subsegment and $$$mex({0}) = 1 > med({0}) = 0$$$ on it. In the third test case, on the following subsegments: $$$[1, 0]$$$, $$$[0]$$$, $$$[1, 0, 2]$$$ and $$$[0, 2]$$$, $$$mex$$$ is greater than $$$med$$$. In the fourth test case, on the following subsegments: $$$[0, 2]$$$, $$$[0]$$$, $$$[0, 2, 1]$$$ and $$$[0, 2, 1, 3]$$$, $$$mex$$$ greater than $$$med$$$.
2,000
true
false
false
false
false
false
false
false
false
false
1,811
1505A
Problem - 1505A - Codeforces =============== xa0
900
false
false
true
false
false
false
false
false
false
false
3,152
1970E3
Harry Potter is hiking in the Alps surrounding Lake Geneva. In this area there are $$$m$$$ cabins, numbered 1 to $$$m$$$. Each cabin is connected, with one or more trails, to a central meeting point next to the lake. Each trail is either short or long. Cabin $$$i$$$ is connected with $$$s_i$$$ short trails and $$$l_i$$$ long trails to the lake. Each day, Harry walks a trail from the cabin where he currently is to Lake Geneva, and then from there he walks a trail to any of the $$$m$$$ cabins (including the one he started in). However, as he has to finish the hike in a day, at least one of the two trails has to be short. How many possible combinations of trails can Harry take if he starts in cabin 1 and walks for $$$n$$$ days? Give the answer modulo $$$10^9 + 7$$$. Input The first line contains the integers $$$m$$$ and $$$n$$$. The second line contains $$$m$$$ integers, $$$s_1, dots, s_m$$$, where $$$s_i$$$ is the number of short trails between cabin $$$i$$$ and Lake Geneva. The third and last line contains $$$m$$$ integers, $$$l_1, dots, l_m$$$, where $$$l_i$$$ is the number of long trails between cabin $$$i$$$ and Lake Geneva. We have the following constraints: $$$0 le s_i, l_i le 10^3$$$. $$$1 le m le 10^5$$$. $$$1 le n le 10^9$$$. Output The number of possible combinations of trails, modulo $$$10^9 + 7$$$.
2,200
false
false
false
true
false
false
false
false
false
false
472
566F
As you must know, the maximum clique problem in an arbitrary graph is _NP_-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively. Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques. Let's define a divisibility graph for a set of positive integers _A_u2009=u2009{_a_1,u2009_a_2,u2009...,u2009_a__n_} as follows. The vertices of the given graph are numbers from set _A_, and two numbers _a__i_ and _a__j_ (_i_u2009β‰ u2009_j_) are connected by an edge if and only if either _a__i_ is divisible by _a__j_, or _a__j_ is divisible by _a__i_. You are given a set of non-negative integers _A_. Determine the size of a maximum clique in a divisibility graph for set _A_. Input The first line contains integer _n_ (1u2009≀u2009_n_u2009≀u2009106), that sets the size of set _A_. The second line contains _n_ distinct positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≀u2009_a__i_u2009≀u2009106) β€” elements of subset _A_. The numbers in the line follow in the ascending order. Output Print a single number β€” the maximum size of a clique in a divisibility graph for set _A_. Examples Input 8 3 4 6 8 10 18 21 24 Note In the first sample test a clique of size 3 is, for example, a subset of vertexes {3,u20096,u200918}. A clique of a larger size doesn't exist in this graph.
1,500
true
false
false
true
false
false
false
false
false
false
7,586
776C
Molly Hooper has _n_ different kinds of chemicals arranged in a line. Each of the chemicals has an affection value, The _i_-th of them has affection value _a__i_. Molly wants Sherlock to fall in love with her. She intends to do this by mixing a contiguous segment of chemicals together to make a love potion with total affection value as a non-negative integer power of _k_. Total affection value of a continuous segment of chemicals is the sum of affection values of each chemical in that segment. Help her to do so in finding the total number of such segments. Input The first line of input contains two integers, _n_ and _k_, the number of chemicals and the number, such that the total affection value is a non-negative power of this number _k_. (1u2009≀u2009_n_u2009≀u2009105, 1u2009≀u2009_k_u2009≀u200910). Next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009109u2009≀u2009_a__i_u2009≀u2009109)xa0β€” affection values of chemicals. Output Output a single integerxa0β€” the number of valid segments. Note Do keep in mind that _k_0u2009=u20091. In the first sample, Molly can get following different affection values: 2: segments [1,u20091], [2,u20092], [3,u20093], [4,u20094]; 4: segments [1,u20092], [2,u20093], [3,u20094]; 6: segments [1,u20093], [2,u20094]; 8: segments [1,u20094]. Out of these, 2, 4 and 8 are powers of _k_u2009=u20092. Therefore, the answer is 8. In the second sample, Molly can choose segments [1,u20092], [3,u20093], [3,u20094].
1,800
true
false
true
false
true
false
true
true
false
false
6,666
845A
Berland annual chess tournament is coming! Organizers have gathered 2Β·_n_ chess players who should be divided into two teams with _n_ people each. The first team is sponsored by BerOil and the second team is sponsored by BerMobile. Obviously, organizers should guarantee the win for the team of BerOil. Thus, organizers should divide all 2Β·_n_ players into two teams with _n_ people each in such a way that the first team always wins. Every chess player has its rating _r__i_. It is known that chess player with the greater rating always wins the player with the lower rating. If their ratings are equal then any of the players can win. After teams assignment there will come a drawing to form _n_ pairs of opponents: in each pair there is a player from the first team and a player from the second team. Every chess player should be in exactly one pair. Every pair plays once. The drawing is totally random. Is it possible to divide all 2Β·_n_ players into two teams with _n_ people each so that the player from the first team in every pair wins regardless of the results of the drawing? Input The first line contains one integer _n_ (1u2009≀u2009_n_u2009≀u2009100). The second line contains 2Β·_n_ integers _a_1,u2009_a_2,u2009... _a_2_n_ (1u2009≀u2009_a__i_u2009≀u20091000). Output If it's possible to divide all 2Β·_n_ players into two teams with _n_ people each so that the player from the first team in every pair wins regardless of the results of the drawing, then print "YES". Otherwise print "NO".
1,100
false
false
true
false
false
false
false
false
true
false
6,384
715B
ZS the Coder has drawn an undirected graph of _n_ vertices numbered from 0 to _n_u2009-u20091 and _m_ edges between them. Each edge of the graph is weighted, each weight is a positive integer. The next day, ZS the Coder realized that some of the weights were erased! So he wants to reassign positive integer weight to each of the edges which weights were erased, so that the length of the shortest path between vertices _s_ and _t_ in the resulting graph is exactly _L_. Can you help him? Input The first line contains five integers _n_,u2009_m_,u2009_L_,u2009_s_,u2009_t_ (2u2009≀u2009_n_u2009≀u20091000,u2009u20091u2009≀u2009_m_u2009≀u200910u2009000,u2009u20091u2009≀u2009_L_u2009≀u2009109,u2009u20090u2009≀u2009_s_,u2009_t_u2009≀u2009_n_u2009-u20091,u2009u2009_s_u2009β‰ u2009_t_)xa0β€” the number of vertices, number of edges, the desired length of shortest path, starting vertex and ending vertex respectively. Then, _m_ lines describing the edges of the graph follow. _i_-th of them contains three integers, _u__i_,u2009_v__i_,u2009_w__i_ (0u2009≀u2009_u__i_,u2009_v__i_u2009≀u2009_n_u2009-u20091,u2009u2009_u__i_u2009β‰ u2009_v__i_,u2009u20090u2009≀u2009_w__i_u2009≀u2009109). _u__i_ and _v__i_ denote the endpoints of the edge and _w__i_ denotes its weight. If _w__i_ is equal to 0 then the weight of the corresponding edge was erased. It is guaranteed that there is at most one edge between any pair of vertices. Output Print "NO" (without quotes) in the only line if it's not possible to assign the weights in a required way. Otherwise, print "YES" in the first line. Next _m_ lines should contain the edges of the resulting graph, with weights assigned to edges which weights were erased. _i_-th of them should contain three integers _u__i_, _v__i_ and _w__i_, denoting an edge between vertices _u__i_ and _v__i_ of weight _w__i_. The edges of the new graph must coincide with the ones in the graph from the input. The weights that were not erased must remain unchanged whereas the new weights can be any positive integer not exceeding 1018. The order of the edges in the output doesn't matter. The length of the shortest path between _s_ and _t_ must be equal to _L_. If there are multiple solutions, print any of them. Examples Input 5 5 13 0 4 0 1 5 2 1 2 3 2 3 1 4 0 4 3 4 Output YES 0 1 5 2 1 2 3 2 3 1 4 8 4 3 4 Input 2 1 123456789 0 1 0 1 0 Input 2 1 999999999 1 0 0 1 1000000000 Note Here's how the graph in the first sample case looks like : In the first sample case, there is only one missing edge weight. Placing the weight of 8 gives a shortest path from 0 to 4 of length 13. In the second sample case, there is only a single edge. Clearly, the only way is to replace the missing weight with 123456789. In the last sample case, there is no weights to assign but the length of the shortest path doesn't match the required value, so the answer is "NO".
2,300
false
false
false
false
false
true
false
true
false
true
6,958
1936B
There is a one-dimensional grid of length $$$n$$$. The $$$i$$$-th cell of the grid contains a character $$$s_i$$$, which is either '<' or '>'. When a pinball is placed on one of the cells, it moves according to the following rules: If the pinball is on the $$$i$$$-th cell and $$$s_i$$$ is '<', the pinball moves one cell to the left in the next second. If $$$s_i$$$ is '>', it moves one cell to the right. After the pinball has moved, the character $$$s_i$$$ is inverted (i.xa0e. if $$$s_i$$$ used to be '<', it becomes '>', and vice versa). The pinball stops moving when it leaves the grid: either from the left border or from the right one. You need to answer $$$n$$$ independent queries. In the $$$i$$$-th query, a pinball will be placed on the $$$i$$$-th cell. Note that we always place a pinball on the initial grid. For each query, calculate how many seconds it takes the pinball to leave the grid. It can be shown that the pinball will always leave the grid within a finite number of steps. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 5 cdot 10^5$$$). The second line of each test case contains a string $$$s_1s_2 ldots s_{n}$$$ of length $$$n$$$ consisting of characters '<' and '>'. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Output For each test case, for each $$$i$$$ ($$$1 le i le n$$$) output the answer if a pinball is initially placed on the $$$i$$$-th cell. Example Output 3 6 5 1 2 3 4 1 4 7 10 8 1 Note In the first test case, the movement of the pinball for $$$i=1$$$ is shown in the following pictures. It takes the pinball $$$3$$$ seconds to leave the grid. The movement of the pinball for $$$i=2$$$ is shown in the following pictures. It takes the pinball $$$6$$$ seconds to leave the grid.
2,000
true
false
true
false
true
false
false
true
false
false
667
1217A
You play your favourite game yet another time. You chose the character you didn't play before. It has $$$str$$$ points of strength and $$$int$$$ points of intelligence. Also, at start, the character has $$$exp$$$ free experience points you can invest either in strength or in intelligence (by investing one point you can either raise strength by $$$1$$$ or raise intelligence by $$$1$$$). Since you'd like to make some fun you want to create a jock character, so it has more strength than intelligence points (resulting strength is strictly greater than the resulting intelligence). Calculate the number of different character builds you can create (for the purpose of replayability) if you must invest all free points. Two character builds are different if their strength and/or intellect are different. Input The first line contains the single integer $$$T$$$ ($$$1 le T le 100$$$) β€” the number of queries. Next $$$T$$$ lines contain descriptions of queries β€” one per line. This line contains three integers $$$str$$$, $$$int$$$ and $$$exp$$$ ($$$1 le str, int le 10^8$$$, $$$0 le exp le 10^8$$$) β€” the initial strength and intelligence of the character and the number of free points, respectively. Output Print $$$T$$$ integers β€” one per query. For each query print the number of different character builds you can create. Example Input 4 5 3 4 2 1 0 3 5 5 4 10 6 Note In the first query there are only three appropriate character builds: $$$(str = 7, int = 5)$$$, $$$(8, 4)$$$ and $$$(9, 3)$$$. All other builds are either too smart or don't use all free points. In the second query there is only one possible build: $$$(2, 1)$$$. In the third query there are two appropriate builds: $$$(7, 6)$$$, $$$(8, 5)$$$. In the fourth query all builds have too much brains.
1,300
true
false
false
false
false
false
false
true
false
false
4,590
1098A
Mitya has a rooted tree with $$$n$$$ vertices indexed from $$$1$$$ to $$$n$$$, where the root has index $$$1$$$. Each vertex $$$v$$$ initially had an integer number $$$a_v ge 0$$$ written on it. For every vertex $$$v$$$ Mitya has computed $$$s_v$$$: the sum of all values written on the vertices on the path from vertex $$$v$$$ to the root, as well as $$$h_v$$$xa0β€” the depth of vertex $$$v$$$, which denotes the number of vertices on the path from vertex $$$v$$$ to the root. Clearly, $$$s_1=a_1$$$ and $$$h_1=1$$$. Then Mitya erased all numbers $$$a_v$$$, and by accident he also erased all values $$$s_v$$$ for vertices with even depth (vertices with even $$$h_v$$$). Your task is to restore the values $$$a_v$$$ for every vertex, or determine that Mitya made a mistake. In case there are multiple ways to restore the values, you're required to find one which minimizes the total sum of values $$$a_v$$$ for all vertices in the tree. Input The first line contains one integer $$$n$$$xa0β€” the number of vertices in the tree ($$$2 le n le 10^5$$$). The following line contains integers $$$p_2$$$, $$$p_3$$$, ... $$$p_n$$$, where $$$p_i$$$ stands for the parent of vertex with index $$$i$$$ in the tree ($$$1 le p_i < i$$$). The last line contains integer values $$$s_1$$$, $$$s_2$$$, ..., $$$s_n$$$ ($$$-1 le s_v le 10^9$$$), where erased values are replaced by $$$-1$$$. Output Output one integerxa0β€” the minimum total sum of all values $$$a_v$$$ in the original tree, or $$$-1$$$ if such tree does not exist. Examples Input 5 1 1 1 1 1 -1 -1 -1 -1 Input 5 1 2 3 1 1 -1 2 -1 -1
1,600
false
true
false
false
false
true
false
false
false
false
5,216
1930F
For an array $$$b$$$ of $$$m$$$ non-negative integers, define $$$f(b)$$$ as the maximum value of $$$maxlimits_{i = 1}^{m} (b_i x) - minlimits_{i = 1}^{m} (b_i x)$$$ over all possible non-negative integers $$$x$$$, where $$$$$$ is $$$. It is guaranteed that $$$0 leq v < n$$$. The queries are given in a modified way. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 2 cdot 10^5$$$)xa0β€” the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1 leq n leq 2^{22}$$$, $$$1 leq q leq 10^6$$$)xa0β€” the number of queries. The second line of each test case contains $$$q$$$ space-separated integers $$$e_1,e_2,ldots,e_q$$$ ($$$0 leq e_i < n$$$)xa0β€” the encrypted values of $$$v$$$. Let $$$mathrm{last}_i$$$ equal the output of the $$$(i-1)$$$-th query for $$$igeq 2$$$ and $$$mathrm{last}_i=0$$$ for $$$i=1$$$. Then the value of $$$v$$$ for the $$$i$$$-th query is ($$$e_i + mathrm{last}_i$$$) modulo $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2^{22}$$$ and the sum of $$$q$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, print $$$q$$$ integers. The $$$i$$$-th integer is the output of the $$$i$$$-th query. Note In the first test case, the final $$$a=[1,2]$$$. For $$$i=1$$$, the answer is always $$$0$$$, irrespective of $$$x$$$. For $$$i=2$$$, we can select $$$x=5$$$. In the second test case, the final $$$a=[3,1,0,5]$$$.
2,700
false
false
false
false
false
false
true
false
false
false
705
427E
Imagine that your city is an infinite 2D plane with Cartesian coordinate system. The only crime-affected road of your city is the _x_-axis. Currently, there are _n_ criminals along the road. No police station has been built on this road yet, so the mayor wants to build one. As you are going to be in charge of this new police station, the mayor has asked you to choose a suitable position (some integer point) for building it. You should choose the best position for the police station, so that you could minimize the total time of your criminal catching mission. Your mission of catching the criminals will operate only from this station. The new station will have only one patrol car. You will go to the criminals by this car, carry them on the car, bring them back to the police station and put them in prison. The patrol car can carry at most _m_ criminals at a time. Note that, the criminals don't know about your mission. So, they will stay where they are instead of running away. Your task is to find the position for the police station, so that total distance you need to cover to catch all the criminals will be minimum possible. Note that, you also can built the police station on the positions where one or more criminals already exist. In such a case all these criminals are arrested instantly. Input The first line of the input will have two integers _n_xa0(1u2009≀u2009_n_u2009≀u2009106) and _m_xa0(1u2009≀u2009_m_u2009≀u2009106) separated by spaces. The next line will contain _n_ integers separated by spaces. The _i__th_ integer is the position of the _i__th_ criminal on the _x_-axis. Absolute value of positions will not exceed 109. If a criminal has position _x_, he/she is located in the point (_x_,u20090) of the plane. The positions of the criminals will be given in non-decreasing order. Note, that there can be more than one criminal standing at some point of the plane. Note: since the size of the input/output could be very large, don't use slow input/output techniques in your language. For example, do not use input/output streams (cin, cout) in C++. Output Print a single integer, that means the minimum possible distance you need to cover to catch all the criminals. Examples Input 11 2 -375 -108 1336 1453 1598 1892 2804 3732 4291 4588 4822
2,000
true
true
true
false
false
false
false
false
false
false
8,127
1681E
You found a map of a weirdly shaped labyrinth. The map is a grid, consisting of $$$n$$$ rows and $$$n$$$ columns. The rows of the grid are numbered from $$$1$$$ to $$$n$$$ from bottom to top. The columns of the grid are numbered from $$$1$$$ to $$$n$$$ from left to right. The labyrinth has $$$n$$$ layers. The first layer is the bottom left corner (cell $$$(1, 1)$$$). The second layer consists of all cells that are in the grid and adjacent to the first layer by a side or a corner. The third layer consists of all cells that are in the grid and adjacent to the second layer by a side or a corner. And so on. The labyrinth with $$$5$$$ layers, for example, is shaped as follows: The layers are separated from one another with walls. However, there are doors in these walls. Each layer (except for layer $$$n$$$) has exactly two doors to the next layer. One door is placed on the top wall of the layer and another door is placed on the right wall of the layer. For each layer from $$$1$$$ to $$$n-1$$$ you are given positions of these two doors. The doors can be passed in both directions: either from layer $$$i$$$ to layer $$$i+1$$$ or from layer $$$i+1$$$ to layer $$$i$$$. If you are standing in some cell, you can move to an adjacent by a side cell if a wall doesn't block your move (e.g. you can't move to a cell in another layer if there is no door between the cells). Now you have $$$m$$$ queries of sort: what's the minimum number of moves one has to make to go from cell $$$(x_1, y_1)$$$ to cell $$$(x_2, y_2)$$$. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$)xa0β€” the number of layers in the labyrinth. The $$$i$$$-th of the next $$$n-1$$$ lines contains four integers $$$d_{1,x}, d_{1,y}, d_{2,x}$$$ and $$$d_{2,y}$$$ ($$$1 le d_{1,x}, d_{1,y}, d_{2,x}, d_{2,y} le n$$$)xa0β€” the coordinates of the doors. Both cells are on the $$$i$$$-th layer. The first cell is adjacent to the top wall of the $$$i$$$-th layer by a sidexa0β€” that side is where the door is. The second cell is adjacent to the right wall of the $$$i$$$-th layer by a sidexa0β€” that side is where the door is. The next line contains a single integer $$$m$$$ ($$$1 le m le 2 cdot 10^5$$$)xa0β€” the number of queries. The $$$j$$$-th of the next $$$m$$$ lines contains four integers $$$x_1, y_1, x_2$$$ and $$$y_2$$$ ($$$1 le x_1, y_1, x_2, y_2 le n$$$)xa0β€” the coordinates of the cells in the $$$j$$$-th query. Output For each query, print a single integerxa0β€” the minimum number of moves one has to make to go from cell $$$(x_1, y_1)$$$ to cell $$$(x_2, y_2)$$$. Examples Input 2 1 1 1 1 10 1 1 1 1 1 1 1 2 1 1 2 1 1 1 2 2 1 2 1 2 1 2 2 1 1 2 2 2 2 1 2 1 2 1 2 2 2 2 2 2 Output 0 1 1 2 0 2 1 0 1 0 Input 4 1 1 1 1 2 1 2 2 3 2 1 3 5 2 4 4 3 4 4 3 3 1 2 3 3 2 2 4 4 1 4 2 3 Note Here is the map of the labyrinth from the second example. The doors are marked red.
2,600
false
false
false
true
true
false
false
false
false
false
2,181
1269B
You are given a positive integer $$$m$$$ and two integer sequence: $$$a=[a_1, a_2, ldots, a_n]$$$ and $$$b=[b_1, b_2, ldots, b_n]$$$. Both of these sequence have a length $$$n$$$. Permutation is a sequence of $$$n$$$ different positive integers from $$$1$$$ to $$$n$$$. For example, these sequences are permutations: $$$[1]$$$, $$$[1,2]$$$, $$$[2,1]$$$, $$$[6,7,3,4,1,2,5]$$$. These are not: $$$[0]$$$, $$$[1,1]$$$, $$$[2,3]$$$. You need to find the non-negative integer $$$x$$$, and increase all elements of $$$a_i$$$ by $$$x$$$, modulo $$$m$$$ (i.e. you want to change $$$a_i$$$ to $$$(a_i + x) bmod m$$$), so it would be possible to rearrange elements of $$$a$$$ to make it equal $$$b$$$, among them you need to find the smallest possible $$$x$$$. In other words, you need to find the smallest non-negative integer $$$x$$$, for which it is possible to find some permutation $$$p=[p_1, p_2, ldots, p_n]$$$, such that for all $$$1 leq i leq n$$$, $$$(a_i + x) bmod m = b_{p_i}$$$, where $$$y bmod m$$$xa0β€” remainder of division of $$$y$$$ by $$$m$$$. For example, if $$$m=3$$$, $$$a = [0, 0, 2, 1], b = [2, 0, 1, 1]$$$, you can choose $$$x=1$$$, and $$$a$$$ will be equal to $$$[1, 1, 0, 2]$$$ and you can rearrange it to make it equal $$$[2, 0, 1, 1]$$$, which is equal to $$$b$$$. Input The first line contains two integers $$$n,m$$$ ($$$1 leq n leq 2000, 1 leq m leq 10^9$$$): number of elemens in arrays and $$$m$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i < m$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$0 leq b_i < m$$$). It is guaranteed that there exists some non-negative integer $$$x$$$, such that it would be possible to find some permutation $$$p_1, p_2, ldots, p_n$$$ such that $$$(a_i + x) bmod m = b_{p_i}$$$.
1,500
false
false
false
false
false
false
true
false
true
false
4,345
243E
Problem - 243E - 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 data structures *3000 No tag edit access β†’ Contest materials . You are given matrix _a_ of size _n_u2009Γ—u2009_n_, consisting of zeroes and ones. Your task is to determine whether you can get a good matrix _b_ from it by rearranging the columns or not. Input The first line contains integer _n_ (1u2009≀u2009_n_u2009≀u2009500) β€” the size of matrix _a_. Each of _n_ following lines contains _n_ characters "0" and "1" β€” matrix _a_. Note that the characters are written without separators. Output Print "YES" in the first line, if you can rearrange the matrix columns so as to get a good matrix _b_. In the next _n_ lines print the good matrix _b_. If there are multiple answers, you are allowed to print any of them. If it is impossible to get a good matrix, print "NO". Examples Input 6 100010 110110 011001 010010 000100 011001 Output YES 011000 111100 000111 001100 100000 000111 Input 3 110 101 011 Output NO
3,000
false
false
false
false
true
false
false
false
false
false
8,859
323A
Problem - 323A - Codeforces =============== xa0 , so that the following conditions must be satisfied: each white cube has exactly 2 neighbouring cubes of white color; each black cube has exactly 2 neighbouring cubes of black color. Input The first line contains integer _k_ (1u2009≀u2009_k_u2009≀u2009100), which is size of the cube. Output Print -1 if there is no solution. Otherwise, print the required painting of the cube consequently by layers. Print a _k_u2009Γ—u2009_k_ matrix in the first _k_ lines, showing how the first layer of the cube should be painted. In the following _k_ lines print a _k_u2009Γ—u2009_k_ matrix β€” the way the second layer should be painted. And so on to the last _k_-th layer. Note that orientation of the cube in the space does not matter. Mark a white unit cube with symbol "w" and a black one with "b". Use the format of output data, given in the test samples. You may print extra empty lines, they will be ignored. Examples Input 1 Output -1 Input 2 Output bb ww bb ww
1,600
false
false
false
false
false
true
false
false
false
false
8,544
1764E
Doremy has two arrays $$$a$$$ and $$$b$$$ of $$$n$$$ integers each, and an integer $$$k$$$. Initially, she has a number line where no integers are colored. She chooses a permutation $$$p$$$ of $$$[1,2,ldots,n]$$$ then performs $$$n$$$ moves. On the $$$i$$$-th move she does the following: Pick an uncolored integer $$$x$$$ on the number line such that either: $$$x leq a_{p_i}$$$; or there exists a colored integer $$$y$$$ such that $$$y leq a_{p_i}$$$ and $$$x leq y+b_{p_i}$$$. Color integer $$$x$$$ with color $$$p_i$$$. Determine if the integer $$$k$$$ can be colored with color $$$1$$$. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0β€” the number of test cases. The description of the test cases follows. The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 10^5$$$, $$$1 le k le 10^9$$$). Each of the following $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i,b_i le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output "YES" (without quotes) if the point $$$k$$$ can be colored with color $$$1$$$. Otherwise, output "NO" (without quotes). You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). Example Input 6 4 16 5 3 8 12 10 7 15 1 4 16 8 12 10 7 15 1 5 3 4 16 10 7 15 1 5 3 8 12 4 16 15 1 5 3 8 12 10 7 1 1000000000 500000000 500000000 2 1000000000 1 999999999 1 1 Output NO YES YES YES NO YES Note For the first test case, it is impossible to color point $$$16$$$ with color $$$1$$$. For the second test case, $$$p=[2,1,3,4]$$$ is one possible choice, the detail is shown below. On the first move, pick $$$x=8$$$ and color it with color $$$2$$$ since $$$x=8$$$ is uncolored and $$$x le a_2$$$. On the second move, pick $$$x=16$$$ and color it with color $$$1$$$ since there exists a colored point $$$y=8$$$ such that $$$yle a_1$$$ and $$$x le y + b_1$$$. On the third move, pick $$$x=0$$$ and color it with color $$$3$$$ since $$$x=0$$$ is uncolored and $$$x le a_3$$$. On the forth move, pick $$$x=-2$$$ and color it with color $$$4$$$ since $$$x=-2$$$ is uncolored and $$$x le a_4$$$. In the end, point $$$-2,0,8,16$$$ are colored with color $$$4,3,2,1$$$, respectively. For the third test case, $$$p=[2,1,4,3]$$$ is one possible choice. For the fourth test case, $$$p=[2,3,4,1]$$$ is one possible choice.
2,400
false
true
false
true
false
false
false
false
true
false
1,723
1133D
Problem - 1133D - 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 hashing math number theory *1500 No tag edit access β†’ Contest materials ") Editorial") number $$$d$$$, and then for every $$$i in
1,500
true
false
false
false
false
false
false
false
false
false
5,051
932B
Problem - 932B - 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 data structures dfs and similar *1300 No tag edit access β†’ Contest materials u2009=u2009_k_. Input The first line of the input contains an integer _Q_ (1u2009≀u2009_Q_u2009≀u20092u2009Γ—u2009105) representing the number of queries. _Q_ lines follow, each of which contains 3 integers _l_, _r_ and _k_ (1u2009≀u2009_l_u2009≀u2009_r_u2009≀u2009106,u20091u2009≀u2009_k_u2009≀u20099). Output For each query, print a single line containing the answer for that query. Examples Input 4 22 73 9 45 64 6 47 55 7 2 62 4 Output 1 4 0 8 Input 4 82 94 6 56 67 4 28 59 9 39 74 4 Output 3 1 1 5 Note In the first example: _g_(33)u2009=u20099 as _g_(33)u2009=u2009_g_(3u2009Γ—u20093)u2009=u2009_g_(9)u2009=u20099 _g_(47)u2009=u2009_g_(48)u2009=u2009_g_(60)u2009=u2009_g_(61)u2009=u20096 There are no such integers between 47 and 55. _g_(4)u2009=u2009_g_(14)u2009=u2009_g_(22)u2009=u2009_g_(27)u2009=u2009_g_(39)u2009=u2009_g_(40)u2009=u2009_g_(41)u2009=u2009_g_(58)u2009=u20094
1,300
false
false
false
false
true
false
false
true
false
false
5,977
514A
Problem - 514A - 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 implementation *1200 No tag edit access β†’ Contest materials ") Editorial") digits. The decimal representation of the final number shouldn't start with a zero. Input The first line contains a single integer _x_ (1u2009≀u2009_x_u2009≀u20091018) β€” the number that Luke Skywalker gave to Chewbacca. Output Print the minimum possible positive number that Chewbacca can obtain after inverting some digits. The number shouldn't contain leading zeroes. Examples Input 27 Output 22 Input 4545 Output 4444
1,200
false
true
true
false
false
false
false
false
false
false
7,780
464A
Problem - 464A - 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 strings *1700 No tag edit access β†’ Contest materials . The second line contains string _s_, consisting of _n_ small English letters. It is guaranteed that the string is tolerable (according to the above definition). Output If the lexicographically next tolerable string of the same length exists, print it. Otherwise, print "NO" (without the quotes). Examples Input 3 3 cba Output NO Input 3 4 cba Output cbd Input 4 4 abcd Output abda Note String _s_ is lexicographically larger (or simply larger) than string _t_ with the same length, if there is number _i_, such that _s_1u2009=u2009_t_1, ..., _s__i_u2009=u2009_t__i_, _s__i_u2009+u20091u2009>u2009_t__i_u2009+u20091. The lexicographically next tolerable string is the lexicographically minimum tolerable string which is larger than the given one. A palindrome is a string that reads the same forward or reversed.
1,700
false
true
false
false
false
false
false
false
false
false
7,984
858E
The All-Berland National Olympiad in Informatics has just ended! Now Vladimir wants to upload the contest from the Olympiad as a gym to a popular Codehorses website. Unfortunately, the archive with Olympiad's data is a mess. For example, the files with tests are named arbitrary without any logic. Vladimir wants to rename the files with tests so that their names are distinct integers starting from 1 without any gaps, namely, "1", "2", ..., "_n_', where _n_ is the total number of tests. Some of the files contain tests from statements (examples), while others contain regular tests. It is possible that there are no examples, and it is possible that all tests are examples. Vladimir wants to rename the files so that the examples are the first several tests, all all the next files contain regular tests only. The only operation Vladimir can perform is the "move" command. Vladimir wants to write a script file, each of the lines in which is "move file_1 file_2", that means that the file "file_1" is to be renamed to "file_2". If there is a file "file_2" at the moment of this line being run, then this file is to be rewritten. After the line "move file_1 file_2" the file "file_1" doesn't exist, but there is a file "file_2" with content equal to the content of "file_1" before the "move" command. Help Vladimir to write the script file with the minimum possible number of lines so that after this script is run: all examples are the first several tests having filenames "1", "2", ..., "_e_", where _e_ is the total number of examples; all other files contain regular tests with filenames "_e_u2009+u20091", "_e_u2009+u20092", ..., "_n_", where _n_ is the total number of all tests. Input The first line contains single integer _n_ (1u2009≀u2009_n_u2009≀u2009105) β€” the number of files with tests. _n_ lines follow, each describing a file with test. Each line has a form of "name_i type_i", where "name_i" is the filename, and "type_i" equals "1", if the _i_-th file contains an example test, and "0" if it contains a regular test. Filenames of each file are strings of digits and small English letters with length from 1 to 6 characters. The filenames are guaranteed to be distinct. Output In the first line print the minimum number of lines in Vladimir's script file. After that print the script file, each line should be "move file_1 file_2", where "file_1" is an existing at the moment of this line being run filename, and "file_2" β€” is a string of digits and small English letters with length from 1 to 6.
2,200
false
true
true
false
false
false
false
false
false
false
6,310
500F
Dohyun is running a grocery store. He sells _n_ items numbered by integers from 1 to _n_. The _i_-th (1u2009≀u2009_i_u2009≀u2009_n_) of them costs _c__i_ dollars, and if I buy it, my happiness increases by _h__i_. Each item can be displayed only for _p_ units of time because of freshness. As Dohyun displays the _i_-th item at time _t__i_, the customers can buy the _i_-th item only from time _t__i_ to time _t__i_u2009+u2009(_p_u2009-u20091) inclusively. Also, each customer cannot buy the same item more than once. I'd like to visit Dohyun's grocery store and buy some items for the New Year Party, and maximize my happiness. Because I am a really busy person, I can visit the store only once, and for very short period of time. In other words, if I visit the store at time _t_, I can only buy the items available at time _t_. But I can buy as many items as possible, if the budget holds. I can't buy same item several times due to store rules. It is not necessary to use the whole budget. I made a list of _q_ pairs of integers (_a__j_,u2009_b__j_), which means I may visit the store at time _a__j_, and spend at most _b__j_ dollars at the store. For each pair, I'd like to know the maximum happiness I can obtain. But there are so many pairs that I can't handle them. Can you help me? Input The first line contains two space-separated integers _n_ and _p_ (1u2009≀u2009_n_u2009≀u20094000, 1u2009≀u2009_p_u2009≀u200910u2009000) β€” the number of items, and the display time of each item. Next _n_ lines describe the items. The _i_-th (1u2009≀u2009_i_u2009≀u2009_n_) of them contains three space-separated integers _c__i_, _h__i_, _t__i_ (1u2009≀u2009_c__i_,u2009_h__i_u2009≀u20094000, 1u2009≀u2009_t__i_u2009≀u200910u2009000) β€” the cost of the _i_-th item, the happiness of the _i_-th item, and the time when the _i_-th item starts to be displayed. The next line contains an integer _q_ (1u2009≀u2009_q_u2009≀u200920u2009000)β€” the number of candidates. Next _q_ lines describe the candidates. The _j_-th (1u2009≀u2009_j_u2009≀u2009_q_) of them contains two space-separated integers _a__j_, _b__j_ (1u2009≀u2009_a__j_u2009≀u200920u2009000, 1u2009≀u2009_b__j_u2009≀u20094000) β€” the visit time and the budget for _j_-th visit of store. Output For each candidate, print a single line containing the maximum happiness that I can obtain by buying some items. Examples Input 4 4 2 3 2 3 5 1 4 7 2 11 15 5 4 1 3 2 5 2 6 5 14 Input 5 4 3 2 1 7 4 4 2 1 2 6 3 5 3 2 2 10 1 5 2 5 4 8 4 9 4 10 5 8 5 9 5 10 8 4 7 9 Note Consider the first sample. 1. At time 1, only the 2nd item is available. I can buy the 2nd item using 3 dollars and my happiness will increase by 5. 2. At time 2, the 1st, 2nd, and 3rd item is available. I can buy the 1st item using 2 dollars, and the 2nd item using 3 dollars. My happiness will increase by 3 + 5 = 8. 3. At time 2, the 1st, 2nd, and 3rd item is available. I can buy the 1st item using 2 dollars, and the 3nd item using 4 dollars. My happiness will increase by 3 + 7 = 10. 4. At time 5, the 1st, 3rd, and 4th item is available. I can buy the 1st item using 2 dollars, and the 4th item using 11 dollars. My happiness will increase by 3 + 15 = 18. Note that I don't need to use the whole budget in this case.
2,700
false
false
false
true
false
false
false
false
false
false
7,832
1680D
You are walking with your dog, and now you are at the promenade. The promenade can be represented as an infinite line. Initially, you are in the point $$$0$$$ with your dog. You decided to give some freedom to your dog, so you untied her and let her run for a while. Also, you watched what your dog is doing, so you have some writings about how she ran. During the $$$i$$$-th minute, the dog position changed from her previous position by the value $$$a_i$$$ (it means, that the dog ran for $$$a_i$$$ meters during the $$$i$$$-th minute). If $$$a_i$$$ is positive, the dog ran $$$a_i$$$ meters to the right, otherwise (if $$$a_i$$$ is negative) she ran $$$a_i$$$ meters to the left. During some minutes, you were chatting with your friend, so you don't have writings about your dog movement during these minutes. These values $$$a_i$$$ equal zero. You want your dog to return to you after the end of the walk, so the destination point of the dog after $$$n$$$ minutes should be $$$0$$$. Now you are wondering: what is the maximum possible number of different integer points of the line your dog could visit on her way, if you replace every $$$0$$$ with some integer from $$$-k$$$ to $$$k$$$ (and your dog should return to $$$0$$$ after the walk)? The dog visits an integer point if she runs through that point or reaches in it at the end of any minute. Point $$$0$$$ is always visited by the dog, since she is initially there. If the dog cannot return to the point $$$0$$$ after $$$n$$$ minutes regardless of the integers you place, print -1. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 3000; 1 le k le 10^9$$$) β€” the number of minutes and the maximum possible speed of your dog during the minutes without records. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$), where $$$a_i$$$ is the number of meters your dog ran during the $$$i$$$-th minutes (to the left if $$$a_i$$$ is negative, to the right otherwise). If $$$a_i = 0$$$ then this value is unknown and can be replaced with any integer from the range $$$[-k; k]$$$. Output If the dog cannot return to the point $$$0$$$ after $$$n$$$ minutes regardless of the set of integers you place, print -1. Otherwise, print one integer β€” the maximum number of different integer points your dog could visit if you fill all the unknown values optimally and the dog will return to the point $$$0$$$ at the end of the walk.
2,400
true
true
false
false
false
false
true
false
false
false
2,188
533C
Polycarp and Vasiliy love simple logical games. Today they play a game with infinite chessboard and one pawn for each player. Polycarp and Vasiliy move in turns, Polycarp starts. In each turn Polycarp can move his pawn from cell (_x_,u2009_y_) to (_x_u2009-u20091,u2009_y_) or (_x_,u2009_y_u2009-u20091). Vasiliy can move his pawn from (_x_,u2009_y_) to one of cells: (_x_u2009-u20091,u2009_y_),u2009(_x_u2009-u20091,u2009_y_u2009-u20091) and (_x_,u2009_y_u2009-u20091). Both players are also allowed to skip move. There are some additional restrictions β€” a player is forbidden to move his pawn to a cell with negative _x_-coordinate or _y_-coordinate or to the cell containing opponent's pawn The winner is the first person to reach cell (0,u20090). You are given the starting coordinates of both pawns. Determine who will win if both of them play optimally well. Input The first line contains four integers: _x__p_,u2009_y__p_,u2009_x__v_,u2009_y__v_ (0u2009≀u2009_x__p_,u2009_y__p_,u2009_x__v_,u2009_y__v_u2009≀u2009105) β€” Polycarp's and Vasiliy's starting coordinates. It is guaranteed that in the beginning the pawns are in different cells and none of them is in the cell (0,u20090). Output Output the name of the winner: "Polycarp" or "Vasiliy". Note In the first sample test Polycarp starts in (2,u20091) and will move to (1,u20091) in the first turn. No matter what his opponent is doing, in the second turn Polycarp can move to (1,u20090) and finally to (0,u20090) in the third turn.
1,700
true
true
true
false
false
false
false
false
false
false
7,705
2037B
To help you prepare for your upcoming Codeforces contest, Citlali set a grid problem and is trying to give you a $$$n$$$ by $$$m$$$ grid through your input stream. Specifically, your input stream should contain the following: The first line contains two integers $$$n$$$ and $$$m$$$ β€” the dimensions of the grid. The following $$$n$$$ lines contain $$$m$$$ integers each β€” the values of the grid. However, someone has intercepted your input stream, shuffled all given integers, and put them all on one line! Now, there are $$$k$$$ integers all on one line, and you don't know where each integer originally belongs. Instead of asking Citlali to resend the input, you decide to determine the values of $$$n$$$ and $$$m$$$ yourself. Output any possible value of $$$n$$$ and $$$m$$$ that Citlali could have provided. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$) β€” the number of test cases. The first line of each test case contains an integer $$$k$$$ ($$$3 leq k leq 2 cdot 10^5$$$) β€” the total number of inputs in your input stream. The following line of each test case contains $$$k$$$ integers $$$a_1, a_2, ldots, a_k$$$ ($$$1 leq a_i leq k$$$) β€” the shuffled inputs of your input stream. It is guaranteed that $$$n$$$ and $$$m$$$ are contained within the $$$k$$$ integers. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output two integers, one possible value of $$$n$$$ and $$$m$$$. If multiple possible answers exist, output any. Example Input 5 3 1 1 2 11 3 3 4 5 6 7 8 9 9 10 11 8 8 4 8 3 8 2 8 1 6 2 1 4 5 3 3 8 1 2 6 3 8 5 5 3 Output 1 1 3 3 2 3 4 1 1 6 Note In the first test case, the initial input could have been the following: 1 1 2 In the second test case, the initial input could have been the following: 3 3 4 5 6 7 8 9 9 10 11
800
false
false
true
false
false
false
true
false
false
false
44
937A
The recent All-Berland Olympiad in Informatics featured _n_ participants with each scoring a certain amount of points. As the head of the programming committee, you are to determine the set of participants to be awarded with diplomas with respect to the following criteria: At least one participant should get a diploma. None of those with score equal to zero should get awarded. When someone is awarded, all participants with score not less than his score should also be awarded. Determine the number of ways to choose a subset of participants that will receive the diplomas. Input The first line contains a single integer _n_ (1u2009≀u2009_n_u2009≀u2009100)xa0β€” the number of participants. The next line contains a sequence of _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≀u2009_a__i_u2009≀u2009600)xa0β€” participants' scores. It's guaranteed that at least one participant has non-zero score. Output Print a single integerxa0β€” the desired number of ways. Note There are three ways to choose a subset in sample case one. 1. Only participants with 3 points will get diplomas. 2. Participants with 2 or 3 points will get diplomas. 3. Everyone will get a diploma! The only option in sample case two is to award everyone. Note that in sample case three participants with zero scores cannot get anything.
800
false
false
true
false
false
false
false
false
true
false
5,953
1679B
Once upon a time Mike and Mike decided to come up with an outstanding problem for some stage of ROI (rare olympiad in informatics). One of them came up with a problem prototype but another stole the idea and proposed that problem for another stage of the same olympiad. Since then the first Mike has been waiting for an opportunity to propose the original idea for some other contest... Mike waited until this moment! You are given an array $$$a$$$ of $$$n$$$ integers. You are also given $$$q$$$ queries of two types: Replace $$$i$$$-th element in the array with integer $$$x$$$. Replace each element in the array with integer $$$x$$$. After performing each query you have to calculate the sum of all elements in the array. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 2 cdot 10^5$$$)xa0β€” the number of elements in the array and the number of queries, respectively. The second line contains $$$n$$$ integers $$$a_1, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β€” elements of the array $$$a$$$. Each of the following $$$q$$$ lines contains a description of the corresponding query. Description begins with integer $$$t$$$ ($$$t in {1, 2}$$$) which denotes a type of the query: If $$$t = 1$$$, then two integers $$$i$$$ and $$$x$$$ are following ($$$1 le i le n$$$, $$$1 le x le 10^9$$$)xa0β€” position of replaced element and it's new value. If $$$t = 2$$$, then integer $$$x$$$ is following ($$$1 le x le 10^9$$$)xa0β€” new value of each element in the array. Output Print $$$q$$$ integers, each on a separate line. In the $$$i$$$-th line print the sum of all elements in the array after performing the first $$$i$$$ queries. Example Input 5 5 1 2 3 4 5 1 1 5 2 10 1 5 11 1 4 1 2 1 Note Consider array from the example and the result of performing each query: 1. Initial array is $$$[1, 2, 3, 4, 5]$$$. 2. After performing the first query, array equals to $$$[5, 2, 3, 4, 5]$$$. The sum of all elements is $$$19$$$. 3. After performing the second query, array equals to $$$[10, 10, 10, 10, 10]$$$. The sum of all elements is $$$50$$$. 4. After performing the third query, array equals to $$$[10, 10, 10, 10, 11$$$]. The sum of all elements is $$$51$$$. 5. After performing the fourth query, array equals to $$$[10, 10, 10, 1, 11]$$$. The sum of all elements is $$$42$$$. 6. After performing the fifth query, array equals to $$$[1, 1, 1, 1, 1]$$$. The sum of all elements is $$$5$$$.
1,200
false
false
true
false
true
false
false
false
false
false
2,196
1299D
Guy-Manuel and Thomas are planning $$$144$$$ trips around the world. You are given a simple weighted undirected connected graph with $$$n$$$ vertexes and $$$m$$$ edges with the following restriction: there isn't any simple cycle (i. e. a cycle which doesn't pass through any vertex more than once) of length greater than $$$3$$$ which passes through the vertex $$$1$$$. The cost of a path (not necessarily simple) in this graph is defined as the xa0β€” the number of vertexes and edges in the graph. The $$$i$$$-th of the next $$$m$$$ lines contains three integers $$$a_i$$$, $$$b_i$$$ and $$$w_i$$$ ($$$1 le a_i, b_i le n, a_i eq b_i, 0 le w_i < 32$$$)xa0β€” the endpoints of the $$$i$$$-th edge and its weight. It's guaranteed there aren't any multiple edges, the graph is connected and there isn't any simple cycle of length greater than $$$3$$$ which passes through the vertex $$$1$$$. Output Output the answer modulo $$$10^9+7$$$. Examples Input 6 8 1 2 0 2 3 1 2 4 3 2 6 2 3 4 8 3 5 4 5 4 5 5 6 6 Input 7 9 1 2 0 1 3 1 2 3 9 2 4 3 2 5 4 4 5 7 3 6 6 3 7 7 6 7 8 Input 4 4 1 2 27 1 3 1 1 4 1 3 4 0 Note The pictures below represent the graphs from examples. In the first example, there aren't any nontrivial cycles with cost $$$0$$$, so we can either remove or keep the only edge incident to the vertex $$$1$$$. In the second example, if we don't remove the edge $$$1-2$$$, then there is a cycle $$$1-2-4-5-2-1$$$ with cost $$$0$$$; also if we don't remove the edge $$$1-3$$$, then there is a cycle $$$1-3-2-4-5-2-3-1$$$ of cost $$$0$$$. The only valid subset consists of both edges. In the third example, all subsets are valid except for those two in which both edges $$$1-3$$$ and $$$1-4$$$ are kept.
3,000
true
false
false
true
false
false
false
false
false
true
4,198
1665E
You are given an array $$$a$$$ of $$$n$$$ non-negative integers, numbered from $$$1$$$ to $$$n$$$. Let's define the cost of the array $$$a$$$ as $$$displaystyle min_{i eq j} a_i a_j$$$, where $$$$$$ denotes the . For each query you should find the cost of the subarray $$$a_{l}, a_{l + 1}, ldots, a_{r}$$$. Input Each test case consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 le n le 10^5$$$)xa0β€” the length array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i < 2^{30}$$$)xa0β€” the elements of $$$a$$$. The third line of each test case contains an integer $$$q$$$ ($$$1 le q le 10^5$$$)xa0β€” the number of queries. Each of the next $$$q$$$ lines contains two integers $$$l_j$$$, $$$r_j$$$ ($$$1 le l_j < r_j le n$$$)xa0β€” the description of the $$$j$$$-th query. It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases do not exceed $$$10^5$$$. Output For each test case print $$$q$$$ numbers, where the $$$j$$$-th number is the cost of array $$$a_{l_j}, a_{l_j + 1}, ldots, a_{r_j}$$$. Example Input 2 5 6 1 3 2 1 4 1 2 2 3 2 4 2 5 4 0 2 1 1073741823 4 1 2 2 3 1 3 3 4 Output 7 3 3 1 2 3 1 1073741823 Note In the first test case the array $$$a$$$ is $$$110_2, 001_2, 011_2, 010_2, 001_2$$$. That's why the answers for the queries are: $$$[1; 2]$$$: $$$a_1 a_2 = 110_2 001_2 = 111_2 = 7$$$; $$$[2; 3]$$$: $$$a_2 a_3 = 001_2 011_2 = 011_2 = 3$$$; $$$[2; 4]$$$: $$$a_2 a_3 = a_3 a_4 = a_2 a_4 = 011_2 = 3$$$; $$$[2; 5]$$$: $$$a_2 a_5 = 001_2 = 1$$$. In the second test case the array $$$a$$$ is $$$00_2, 10_2, 01_2, underbrace{11ldots 1_2}_{30}$$$ ($$$a_4 = 2^{30} - 1$$$). That's why the answers for the queries are: $$$[1; 2]$$$: $$$a_1 a_2 = 10_2 = 2$$$; $$$[2; 3]$$$: $$$a_2 a_3 = 11_2 = 3$$$; $$$[1; 3]$$$: $$$a_1 a_3 = 01_2 = 1$$$; $$$[3; 4]$$$: $$$a_3 a_4 = 01_2 underbrace{11ldots 1_2}_{30} = 2^{30} - 1 = 1073741823$$$.
2,500
false
true
true
false
true
false
true
false
false
false
2,286
466A
Problem - 466A - 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 *1200 No tag edit access β†’ Contest materials ") ") . It costs _b_ rubles. Ann did the math; she will need to use subway _n_ times. Help Ann, tell her what is the minimum sum of money she will have to spend to make _n_ rides? Input The single line contains four space-separated integers _n_, _m_, _a_, _b_ (1u2009≀u2009_n_,u2009_m_,u2009_a_,u2009_b_u2009≀u20091000) β€” the number of rides Ann has planned, the number of rides covered by the _m_ ride ticket, the price of a one ride ticket and the price of an _m_ ride ticket. Output Print a single integer β€” the minimum sum in rubles that Ann will need to spend. Examples Input 6 2 1 2 Output 6 Input 5 2 2 3 Output 8 Note In the first sample one of the optimal solutions is: each time buy a one ride ticket. There are other optimal solutions. For example, buy three _m_ ride tickets.
1,200
false
false
true
false
false
false
false
false
false
false
7,977
163E
The best programmers of Embezzland compete to develop a part of the project called "e-Government" β€” the system of automated statistic collecting and press analysis. We know that any of the _k_ citizens can become a member of the Embezzland government. The citizens' surnames are _a_1,u2009_a_2,u2009...,u2009_a__k_. All surnames are different. Initially all _k_ citizens from this list are members of the government. The system should support the following options: Include citizen _a__i_ to the government. Exclude citizen _a__i_ from the government. Given a newspaper article text, calculate how politicized it is. To do this, for every active government member the system counts the number of times his surname occurs in the text as a substring. All occurrences are taken into consideration, including the intersecting ones. The degree of politicization of a text is defined as the sum of these values for all active government members. Implement this system. Input The first line contains space-separated integers _n_ and _k_ (1u2009≀u2009_n_,u2009_k_u2009≀u2009105) β€” the number of queries to the system and the number of potential government members. Next _k_ lines contain the surnames _a_1,u2009_a_2,u2009...,u2009_a__k_, one per line. All surnames are pairwise different. Next _n_ lines contain queries to the system, one per line. Each query consists of a character that determines an operation and the operation argument, written consecutively without a space. Operation "include in the government" corresponds to the character "+", operation "exclude" corresponds to "-". An argument of those operations is an integer between 1 and _k_ β€” the index of the citizen involved in the operation. Any citizen can be included and excluded from the government an arbitrary number of times in any order. Including in the government a citizen who is already there or excluding the citizen who isn't there changes nothing. The operation "calculate politicization" corresponds to character "?". Its argument is a text. All strings β€” surnames and texts β€” are non-empty sequences of lowercase Latin letters. The total length of all surnames doesn't exceed 106, the total length of all texts doesn't exceed 106. Output For any "calculate politicization" operation print on a separate line the degree of the politicization of the given text. Print nothing for other operations. Examples Input 7 3 a aa ab ?aaab -2 ?aaab -3 ?aaab +2 ?aabbaa
2,800
false
false
false
true
true
false
false
false
false
false
9,211