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
828A
In a small restaurant there are _a_ tables for one person and _b_ tables for two persons. It it known that _n_ groups of people come today, each consisting of one or two people. If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group. If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group. You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to. Input The first line contains three integers _n_, _a_ and _b_ (1u2009≤u2009_n_u2009≤u20092·105, 1u2009≤u2009_a_,u2009_b_u2009≤u20092·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables. The second line contains a sequence of integers _t_1,u2009_t_2,u2009...,u2009_t__n_ (1u2009≤u2009_t__i_u2009≤u20092) — the description of clients in chronological order. If _t__i_ is equal to one, then the _i_-th group consists of one person, otherwise the _i_-th group consists of two people. Output Print the total number of people the restaurant denies service to. Note In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served. In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
1,200
false
false
true
false
false
false
false
false
false
false
6,449
1712F
— To count graph diameters. You and Your Submission A tree is a connected undirected graph without cycles. A weighted tree has a weight assigned to each edge. The degree of a vertex is the number of edges connected to this vertex. You are given a weighted tree with $$$n$$$ vertices, each edge has a weight of $$$1$$$. Let $$$L$$$ be the set of vertices with degree equal to $$$1$$$. You have to answer $$$q$$$ independent queries. In the $$$i$$$-th query: 1. You are given a positive integer $$$x_i$$$. 2. For all $$$u,v in L$$$ such that $$$u < v$$$, add edge $$$(u, v)$$$ with weight $$$x_i$$$ to the graph (initially the given tree). 3. Find the diameter of the resulting graph. The diameter of a graph is equal to $$$maxlimits_{1 le u < v le n}{operatorname{d}(u, v)}$$$, where $$$operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$. Input The first line contains a single integer $$$n$$$ ($$$3 le n le 10^6$$$). The second line contains $$$n - 1$$$ integers $$$p_2,p_3,ldots,p_n$$$ ($$$1 le p_i < i$$$) indicating that there is an edge between vertices $$$i$$$ and $$$p_i$$$. It is guaranteed that the given edges form a tree. The third line contains a single integer $$$q$$$ ($$$1 le q le 10$$$). The fourth line contains $$$q$$$ integers $$$x_1,x_2,ldots,x_q$$$ ($$$1 le x_i le n$$$). All $$$x_i$$$ are distinct. Output Print $$$q$$$ integers in a single line — the answers to the queries. Examples Input 7 1 2 3 4 2 1 7 2 1 3 7 5 6 4 Note The graph in the first test after adding the edges:
3,200
false
false
false
false
true
false
false
true
false
false
2,007
1545E2
Note that the differences between easy and hard versions are the constraints on $$$n$$$ and the time limit. You can make hacks only if both versions are solved. AquaMoon knew through foresight that some ghosts wanted to curse tourists on a pedestrian street. But unfortunately, this time, these ghosts were hiding in a barrier, and she couldn't enter this barrier in a short time and destroy them. Therefore, all that can be done is to save any unfortunate person on the street from the ghosts. The pedestrian street can be represented as a one-dimensional coordinate system. There is one person hanging out on the pedestrian street. At the time $$$0$$$ he is at coordinate $$$x$$$, moving with a speed of $$$1$$$ unit per second. In particular, at time $$$i$$$ the person will be at coordinate $$$x+i$$$. The ghosts are going to cast $$$n$$$ curses on the street. The $$$i$$$-th curse will last from time $$$tl_i-1+10^{-18}$$$ to time $$$tr_i+1-10^{-18}$$$ (exclusively) and will kill people with coordinates from $$$l_i-1+10^{-18}$$$ to $$$r_i+1-10^{-18}$$$ (exclusively). Formally that means, that the person, whose coordinate is between $$$(l_i-1+10^{-18},r_i+1-10^{-18})$$$ in the time range $$$(tl_i-1+10^{-18},tr_i+1-10^{-18})$$$ will die. To save the person on the street, AquaMoon can stop time at any moment $$$t$$$, and then move the person from his current coordinate $$$x$$$ to any coordinate $$$y$$$ ($$$t$$$, $$$x$$$ and $$$y$$$ are not necessarily integers). The movement costs AquaMoon $$$x-y$$$ energy. The movement is continuous, so if there exists some cursed area between points $$$x$$$ and $$$y$$$ at time $$$t$$$, the person will die too. AquaMoon wants to know what is the minimum amount of energy she needs to spend in order to save the person on the street from all $$$n$$$ curses. But she is not good at programming. As her friend, can you help her? Input The first line contains a single integer $$$n$$$ ($$$1le nle 2 cdot 10^5$$$) — the number of curses. The next line contains a single integer $$$x$$$ ($$$1le xle 10^6$$$) — the initial coordinate of the person. The following $$$n$$$ lines contain four integers $$$tl_i$$$, $$$tr_i$$$, $$$l_i$$$, $$$r_i$$$ each ($$$1le tl_ile tr_ile 10^6$$$, $$$1le l_ile r_ile 10^6$$$). Output Print a single integer — the minimum energy which AquaMoon needs to spent, rounded up to the nearest integer (in case there are two nearest integers you should round the answer to the highest of them).
3,500
false
false
false
true
true
false
false
false
false
false
2,917
1425C
Problem - 1425C - 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 math *3100 No tag edit access → Contest materials ") ") $$$, which denotes the minimum number of moves to move a knight from square $$$(1, 1)$$$ to square $$$(X, Y)$$$. It turns out finding $$$F(X, Y)$$$ is too simple, so Mr. Chanek defines: $$$G(X, Y) = sum_{i=X}^{N} sum_{j=Y}^{M} F(i, j)$$$ Given X and Y, you are tasked to find $$$G(X, Y)$$$. A knight can move from square $$$(a, b)$$$ to square $$$(a', b')$$$ if and only if $$$a - a' > 0$$$, $$$b - b' > 0$$$, and $$$a - a' + b - b' = 3$$$. Of course, the knight cannot leave the chessboard. Input The first line contains an integer $$$T$$$ $$$(1 le T le 100)$$$, the number of test cases. Each test case contains a line with four integers $$$X$$$ $$$Y$$$ $$$N$$$ $$$M$$$ $$$(3 leq X leq N leq 10^9, 3 leq Y leq M leq 10^9)$$$. Output For each test case, print a line with the value of $$$G(X, Y)$$$ modulo $$$10^9 + 7$$$. Example Input 2 3 4 5 6 5 5 8 8 Output 27 70
3,100
true
false
false
false
false
false
false
false
false
false
3,545
1674D
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 786 (Div. 3) 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 sortings *1200 No tag edit access → Contest materials Announcement Tutorial PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST D. A-B-C Sort time limit per test2 seconds memory limit per test256 megabytes You are given three arrays $$$a$$$, $$$b$$$ and $$$c$$$. Initially, array $$$a$$$ consists of $$$n$$$ elements, arrays $$$b$$$ and $$$c$$$ are empty. You are performing the following algorithm that consists of two steps: Step $$$1$$$: while $$$a$$$ is not empty, you take the last element from $$$a$$$ and move it in the middle of array $$$b$$$. If $$$b$$$ currently has odd length, you can choose: place the element from $$$a$$$ to the left or to the right of the middle element of $$$b$$$. As a result, $$$a$$$ becomes empty and $$$b$$$ consists of $$$n$$$ elements. Step $$$2$$$: while $$$b$$$ is not empty, you take the middle element from $$$b$$$ and move it to the end of array $$$c$$$. If $$$b$$$ currently has even length, you can choose which of two middle elements to take. As a result, $$$b$$$ becomes empty and $$$c$$$ now consists of $$$n$$$ elements. Refer to the Note section for examples. Can you make array $$$c$$$ sorted in non-decreasing order? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$)xa0— the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^6$$$)xa0— the array $$$a$$$ itself. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 cdot 10^5$$$. Output For each test, print YES (case-insensitive), if you can make array $$$c$$$ sorted in non-decreasing order. Otherwise, print NO (case-insensitive). Example input 3 4 3 1 5 3 3 3 2 1 1 7331 output YES NO YES Note In the first test case, we can do the following for $$$a = [3, 1, 5, 3]$$$: Step $$$1$$$: $$$a$$$ $$$[3, 1, 5, 3]$$$ $$$Rightarrow$$$ $$$[3, 1, 5]$$$ $$$Rightarrow$$$ $$$[3, 1]$$$ $$$Rightarrow$$$ $$$[3]$$$ $$$Rightarrow$$$ $$$[]$$$ $$$b$$$ $$$[]$$$ $$$[underline{3}]$$$ $$$[3, underline{5}]$$$ $$$[3, underline{1}, 5]$$$ $$$[3, underline{3}, 1, 5]$$$ Step $$$2$$$: $$$b$$$ $$$[3, 3, underline{1}, 5]$$$ $$$Rightarrow$$$ $$$[3, underline{3}, 5]$$$ $$$Rightarrow$$$ $$$[underline{3}, 5]$$$ $$$Rightarrow$$$ $$$[underline{5}]$$$ $$$Rightarrow$$$ $$$[]$$$ $$$c$$$ $$$[]$$$ $$$[1]$$$ $$$[1, 3]$$$ $$$[1, 3, 3]$$$ $$$[1, 3, 3, 5]$$$ As a result, array $$$c = [1, 3, 3, 5]$$$ and it's sorted. Codeforces (c)
1,200
false
false
true
false
false
true
false
false
true
false
2,226
457C
Problem - 457C - Codeforces =============== xa0 — number of voters in the city. Each of the next _n_ lines describes one voter and contains two integers _a__i_ and _b__i_ (0u2009≤u2009_a__i_u2009≤u2009105;xa00u2009≤u2009_b__i_u2009≤u2009104) — number of the candidate that voter is going to vote for and amount of money you need to pay him to change his mind. You are the candidate 0 (so if a voter wants to vote for you, _a__i_ is equal to zero, in which case _b__i_ will also be equal to zero). Output Print one integer — smallest amount of money you need to spend to win the elections. Examples Input 5 1 2 1 2 1 2 2 1 0 0 Output 3 Input 4 1 2 1 2 2 1 0 0 Output 2 Input 1 100000 0 Output 0
2,100
false
false
false
false
false
false
true
false
false
false
8,010
279D
You've got a positive integer sequence _a_1,u2009_a_2,u2009...,u2009_a__n_. All numbers in the sequence are distinct. Let's fix the set of variables _b_1,u2009_b_2,u2009...,u2009_b__m_. Initially each variable _b__i_ (1u2009≤u2009_i_u2009≤u2009_m_) contains the value of zero. Consider the following sequence, consisting of _n_ operations. The first operation is assigning the value of _a_1 to some variable _b__x_ (1u2009≤u2009_x_u2009≤u2009_m_). Each of the following _n_u2009-u20091 operations is assigning to some variable _b__y_ the value that is equal to the sum of values that are stored in the variables _b__i_ and _b__j_ (1u2009≤u2009_i_,u2009_j_,u2009_y_u2009≤u2009_m_). At that, the value that is assigned on the _t_-th operation, must equal _a__t_. For each operation numbers _y_,u2009_i_,u2009_j_ are chosen anew. Your task is to find the minimum number of variables _m_, such that those variables can help you perform the described sequence of operations. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u200923). The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__k_u2009≤u2009109). It is guaranteed that all numbers in the sequence are distinct. Output In a single line print a single number — the minimum number of variables _m_, such that those variables can help you perform the described sequence of operations. If you cannot perform the sequence of operations at any _m_, print -1. Note In the first sample, you can use two variables _b_1 and _b_2 to perform the following sequence of operations. 1. _b_1 := 1; 2. _b_2 := _b_1u2009+u2009_b_1; 3. _b_1 := _b_1u2009+u2009_b_2; 4. _b_1 := _b_1u2009+u2009_b_1; 5. _b_1 := _b_1u2009+u2009_b_2.
2,200
false
false
false
true
false
false
false
false
false
false
8,717
1093B
You are given a string $$$s$$$ consisting only of lowercase Latin letters. You can rearrange all letters of this string as you wish. Your task is to obtain a good string by rearranging the letters of the given string or report that it is impossible to do it. Let's call a string good if it is not a palindrome. Palindrome is a string which is read from left to right the same as from right to left. For example, strings "abacaba", "aa" and "z" are palindromes and strings "bba", "xd" are not. You have to answer $$$t$$$ independent queries. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 100$$$) — number of queries. Each of the next $$$t$$$ lines contains one string. The $$$i$$$-th line contains a string $$$s_i$$$ consisting only of lowercase Latin letter. It is guaranteed that the length of $$$s_i$$$ is from $$$1$$$ to $$$1000$$$ (inclusive). Output Print $$$t$$$ lines. In the $$$i$$$-th line print the answer to the $$$i$$$-th query: -1 if it is impossible to obtain a good string by rearranging the letters of $$$s_i$$$ and any good string which can be obtained from the given one (by rearranging the letters) otherwise. Note In the first query we cannot rearrange letters to obtain a good string. Other examples (not all) of correct answers to the second query: "ababaca", "abcabaa", "baacaba". In the third query we can do nothing to obtain a good string.
900
false
true
false
false
false
true
false
false
true
false
5,243
1556A
William has two numbers $$$a$$$ and $$$b$$$ initially both equal to zero. William mastered performing three different operations with them quickly. Before performing each operation some positive integer $$$k$$$ is picked, which is then used to perform one of the following operations: (note, that for each operation you can choose a new positive integer $$$k$$$) 1. add number $$$k$$$ to both $$$a$$$ and $$$b$$$, or 2. add number $$$k$$$ to $$$a$$$ and subtract $$$k$$$ from $$$b$$$, or 3. add number $$$k$$$ to $$$b$$$ and subtract $$$k$$$ from $$$a$$$. Note that after performing operations, numbers $$$a$$$ and $$$b$$$ may become negative as well. William wants to find out the minimal number of operations he would have to perform to make $$$a$$$ equal to his favorite number $$$c$$$ and $$$b$$$ equal to his second favorite number $$$d$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). Description of the test cases follows. The only line of each test case contains two integers $$$c$$$ and $$$d$$$ $$$(0 le c, d le 10^9)$$$, which are William's favorite numbers and which he wants $$$a$$$ and $$$b$$$ to be transformed into. Output For each test case output a single number, which is the minimal number of operations which William would have to perform to make $$$a$$$ equal to $$$c$$$ and $$$b$$$ equal to $$$d$$$, or $$$-1$$$ if it is impossible to achieve this using the described operations. Example Input 6 1 2 3 5 5 3 6 6 8 0 0 0 Note Let us demonstrate one of the suboptimal ways of getting a pair $$$(3, 5)$$$: Using an operation of the first type with $$$k=1$$$, the current pair would be equal to $$$(1, 1)$$$. Using an operation of the third type with $$$k=8$$$, the current pair would be equal to $$$(-7, 9)$$$. Using an operation of the second type with $$$k=7$$$, the current pair would be equal to $$$(0, 2)$$$. Using an operation of the first type with $$$k=3$$$, the current pair would be equal to $$$(3, 5)$$$.
800
true
false
false
false
false
false
false
false
false
false
2,855
825F
Ivan wants to write a letter to his friend. The letter is a string _s_ consisting of lowercase Latin letters. Unfortunately, when Ivan started writing the letter, he realised that it is very long and writing the whole letter may take extremely long time. So he wants to write the compressed version of string _s_ instead of the string itself. The compressed version of string _s_ is a sequence of strings _c_1,u2009_s_1,u2009_c_2,u2009_s_2,u2009...,u2009_c__k_,u2009_s__k_, where _c__i_ is the decimal representation of number _a__i_ (without any leading zeroes) and _s__i_ is some string consisting of lowercase Latin letters. If Ivan writes string _s_1 exactly _a_1 times, then string _s_2 exactly _a_2 times, and so on, the result will be string _s_. The length of a compressed version is _c_1u2009+u2009_s_1u2009+u2009_c_2u2009+u2009_s_2... _c__k_u2009+u2009_s__k_. Among all compressed versions Ivan wants to choose a version such that its length is minimum possible. Help Ivan to determine minimum possible length. Input The only line of input contains one string _s_ consisting of lowercase Latin letters (1u2009≤u2009_s_u2009≤u20098000). Output Output one integer number — the minimum possible length of a compressed version of _s_. Note In the first example Ivan will choose this compressed version: _c_1 is 10, _s_1 is a. In the second example Ivan will choose this compressed version: _c_1 is 1, _s_1 is abcab. In the third example Ivan will choose this compressed version: _c_1 is 2, _s_1 is c, _c_2 is 1, _s_2 is z, _c_3 is 4, _s_3 is ab.
2,400
false
false
false
true
false
false
false
false
false
false
6,457
1205C
This is an interactive problem You are given a grid $$$n imes n$$$, where $$$n$$$ is odd. Rows are enumerated from $$$1$$$ to $$$n$$$ from up to down, columns are enumerated from $$$1$$$ to $$$n$$$ from left to right. Cell, standing on the intersection of row $$$x$$$ and column $$$y$$$, is denoted by $$$(x, y)$$$. Every cell contains $$$0$$$ or $$$1$$$. It is known that the top-left cell contains $$$1$$$, and the bottom-right cell contains $$$0$$$. We want to know numbers in all cells of the grid. To do so we can ask the following questions: "$$$?$$$ $$$x_1$$$ $$$y_1$$$ $$$x_2$$$ $$$y_2$$$", where $$$1 le x_1 le x_2 le n$$$, $$$1 le y_1 le y_2 le n$$$, and $$$x_1 + y_1 + 2 le x_2 + y_2$$$. In other words, we output two different cells $$$(x_1, y_1)$$$, $$$(x_2, y_2)$$$ of the grid such that we can get from the first to the second by moving only to the right and down, and they aren't adjacent. As a response to such question you will be told if there exists a path between $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$, going only to the right or down, numbers in cells of which form a palindrome. For example, paths, shown in green, are palindromic, so answer for "$$$?$$$ $$$1$$$ $$$1$$$ $$$2$$$ $$$3$$$" and "$$$?$$$ $$$1$$$ $$$2$$$ $$$3$$$ $$$3$$$" would be that there exists such path. However, there is no palindromic path between $$$(1, 1)$$$ and $$$(3, 1)$$$. Determine all cells of the grid by asking not more than $$$n^2$$$ questions. It can be shown that the answer always exists. Input The first line contains odd integer ($$$3 le n < 50$$$)xa0— the side of the grid. Interaction You begin the interaction by reading $$$n$$$. To ask a question about cells $$$(x_1, y_1), (x_2, y_2)$$$, in a separate line output "$$$?$$$ $$$x_1$$$ $$$y_1$$$ $$$x_2$$$ $$$y_2$$$". Numbers in the query have to satisfy $$$1 le x_1 le x_2 le n$$$, $$$1 le y_1 le y_2 le n$$$, and $$$x_1 + y_1 + 2 le x_2 + y_2$$$. Don't forget to 'flush', to get the answer. In response, you will receive $$$1$$$, if there exists a path going from $$$(x_1, y_1)$$$ to $$$(x_2, y_2)$$$ only to the right or down, numbers in cells of which form a palindrome, and $$$0$$$ otherwise. In case your query is invalid or you asked more than $$$n^2$$$ queries, program will print $$$-1$$$ and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts. When you determine numbers in all cells, output "!". Then output $$$n$$$ lines, the $$$i$$$-th of which is a string of length $$$n$$$, corresponding to numbers in the $$$i$$$-th row of the grid. After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hack Format To hack, use the following format. The first line should contain a single odd integer $$$n$$$ (side of your grid). The $$$i$$$-th of $$$n$$$ following lines should contain a string of length $$$n$$$ corresponding to the $$$i$$$-th row of the grid. Top left element of the grid has to be equal to $$$1$$$, bottom right has to be equal to $$$0$$$. Example Output ? 1 1 1 3 ? 1 1 2 3 ? 2 1 2 3 ? 3 1 3 3 ? 2 2 3 3 ? 1 2 3 2 ? 1 2 3 3 ! 100 001 000
2,400
false
false
true
false
false
false
false
false
false
false
4,667
1708A
You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. You are allowed to perform this operation any number of times (possibly, zero): choose an index $$$i$$$ ($$$2 le i le n$$$), and change $$$a_i$$$ to $$$a_i - a_{i-1}$$$. Is it possible to make $$$a_i=0$$$ for all $$$2le ile n$$$? Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 100$$$) xa0— the number of test cases. The description of the test cases follows. The first line contains one integer $$$n$$$ ($$$2 le n le 100$$$)xa0— the length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1 le a_i le 10^9$$$). Output For each test case, print "YES" (without quotes), if it is possible to change $$$a_i$$$ to $$$0$$$ for all $$$2 le i le n$$$, and "NO" (without quotes) otherwise. You can print letters in any case (upper or lower). Example Input 4 2 5 10 3 1 2 3 4 1 1 1 1 9 9 9 8 2 4 4 3 5 3 Note In the first test case, the initial array is $$$[5,10]$$$. You can perform $$$2$$$ operations to reach the goal: 1. Choose $$$i=2$$$, and the array becomes $$$[5,5]$$$. 2. Choose $$$i=2$$$, and the array becomes $$$[5,0]$$$. In the second test case, the initial array is $$$[1,2,3]$$$. You can perform $$$4$$$ operations to reach the goal: 1. Choose $$$i=3$$$, and the array becomes $$$[1,2,1]$$$. 2. Choose $$$i=2$$$, and the array becomes $$$[1,1,1]$$$. 3. Choose $$$i=3$$$, and the array becomes $$$[1,1,0]$$$. 4. Choose $$$i=2$$$, and the array becomes $$$[1,0,0]$$$. In the third test case, you can choose indices in the order $$$4$$$, $$$3$$$, $$$2$$$.
800
true
true
false
false
false
false
false
false
false
false
2,028
331D1
Don't put up with what you're sick of! The Smart Beaver decided to escape from the campus of Beaver Science Academy (BSA). BSA is a _b_u2009×u2009_b_ square on a plane. Each point _x_,u2009_y_ (0u2009≤u2009_x_,u2009_y_u2009≤u2009_b_) belongs to BSA. To make the path quick and funny, the Beaver constructed a Beaveractor, an effective and comfortable types of transport. The campus obeys traffic rules: there are _n_ arrows, parallel to the coordinate axes. The arrows do not intersect and do not touch each other. When the Beaveractor reaches some arrow, it turns in the arrow's direction and moves on until it either reaches the next arrow or gets outside the campus. The Beaveractor covers exactly one unit of space per one unit of time. You can assume that there are no obstacles to the Beaveractor. The BSA scientists want to transport the brand new Beaveractor to the "Academic Tractor" research institute and send the Smart Beaver to do his postgraduate studies and sharpen pencils. They have _q_ plans, representing the Beaveractor's initial position (_x__i_,u2009_y__i_), the initial motion vector _w__i_ and the time _t__i_ that have passed after the escape started. Your task is for each of the _q_ plans to determine the Smart Beaver's position after the given time. Input The first line contains two integers: the number of traffic rules _n_ and the size of the campus _b_, 0u2009≤u2009_n_, 1u2009≤u2009_b_. Next _n_ lines contain the rules. Each line of the rules contains four space-separated integers _x_0, _y_0, _x_1, _y_1 — the beginning and the end of the arrow. It is guaranteed that all arrows are parallel to the coordinate axes and have no common points. All arrows are located inside the campus, that is, 0u2009≤u2009_x_0,u2009_y_0,u2009_x_1,u2009_y_1u2009≤u2009_b_ holds. Next line contains integer _q_ — the number of plans the scientists have, 1u2009≤u2009_q_u2009≤u2009105. The _i_-th plan is represented by two integers, _x__i_, _y__i_ are the Beaveractor's coordinates at the initial time, 0u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_b_, character _w__i_, that takes value U, D, L, R and sets the initial direction up, down, to the left or to the right correspondingly (the Y axis is directed upwards), and _t__i_ — the time passed after the escape started, 0u2009≤u2009_t__i_u2009≤u20091015. to get 30 points you need to solve the problem with constraints _n_,u2009_b_u2009≤u200930 (subproblem D1); to get 60 points you need to solve the problem with constraints _n_,u2009_b_u2009≤u20091000 (subproblems D1+D2); to get 100 points you need to solve the problem with constraints _n_,u2009_b_u2009≤u2009105 (subproblems D1+D2+D3). Output Print _q_ lines. Each line should contain two integers — the Beaveractor's coordinates at the final moment of time for each plan. If the Smart Beaver manages to leave the campus in time _t__i_, print the coordinates of the last point in the campus he visited. Examples Input 3 3 0 0 0 1 0 2 2 2 3 3 2 3 12 0 0 L 0 0 0 L 1 0 0 L 2 0 0 L 3 0 0 L 4 0 0 L 5 0 0 L 6 2 0 U 2 2 0 U 3 3 0 U 5 1 3 D 2 1 3 R 2 Output 0 0 0 1 0 2 1 2 2 2 3 2 3 2 2 2 3 2 1 3 2 2 1 3
2,400
false
false
true
false
false
false
false
false
false
false
8,515
2031B
Penchick and his friend Kohane are touring Indonesia, and their next stop is in Surabaya! In the bustling food stalls of Surabaya, Kohane bought $$$n$$$ satay sticks and arranged them in a line, with the $$$i$$$-th satay stick having length $$$p_i$$$. It is given that $$$p$$$ is a permutation$$$^{ ext{∗}}$$$ of length $$$n$$$. Penchick wants to sort the satay sticks in increasing order of length, so that $$$p_i=i$$$ for each $$$1le ile n$$$. For fun, they created a rule: they can only swap neighboring satay sticks whose lengths differ by exactly $$$1$$$. Formally, they can perform the following operation any number of times (including zero): Select an index $$$i$$$ ($$$1le ile n-1$$$) such that $$$p_{i+1}-p_i=1$$$; Swap $$$p_i$$$ and $$$p_{i+1}$$$. Determine whether it is possible to sort the permutation $$$p$$$, thus the satay sticks, by performing the above operation. $$$^{ ext{∗}}$$$A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 2cdot 10^5$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2cdot 10^5$$$)xa0— the number of satay sticks. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$)xa0— the permutation $$$p$$$ representing the length of the satay sticks. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, output "YES" if it is possible to sort permutation $$$p$$$ by performing the operation. Otherwise, output "NO". You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Note In the first test case, we can sort permutation $$$p = [2, 1, 3, 4]$$$ by performing an operation on index $$$1$$$ ($$$p_2 - p_1 = 1 - 2 = 1$$$), resulting in $$$p = [1, 2, 3, 4]$$$. In the second test case, it can be proven that it is impossible to sort permutation $$$p = [4, 2, 3, 1]$$$ by performing the operation. Here is an example of a sequence of operations that can be performed on the permutation: Select $$$i = 2$$$ ($$$p_3 - p_2 = 3 - 2 = 1$$$). This results in $$$p = [4, 3, 2, 1]$$$. Select $$$i = 1$$$ ($$$p_2 - p_1 = 3 - 4 = 1$$$). This results in $$$p = [3, 4, 2, 1]$$$. Select $$$i = 3$$$ ($$$p_4 - p_3 = 1 - 2 = 1$$$). This results in $$$p = [3, 4, 1, 2]$$$. Unfortunately, permutation $$$p$$$ remains unsorted after performing the operations.
900
false
true
false
false
false
false
true
false
true
false
79
1625A
Martian scientists explore Ganymede, one of Jupiter's numerous moons. Recently, they have found ruins of an ancient civilization. The scientists brought to Mars some tablets with writings in a language unknown to science. They found out that the inhabitants of Ganymede used an alphabet consisting of two letters, and each word was exactly $$$ell$$$ letters long. So, the scientists decided to write each word of this language as an integer from $$$0$$$ to $$$2^{ell} - 1$$$ inclusively. The first letter of the alphabet corresponds to zero bit in this integer, and the second letter corresponds to one bit. The same word may have various forms in this language. Then, you need to restore the initial form. The process of doing it is described below. Denote the distance between two words as the amount of positions, in which these words differ. For example, the distance between $$$1001_2$$$ and $$$1100_2$$$ (in binary) is equal to two, as these words have different letters in the second and the fourth positions, counting from left to right. Further, denote the distance between words $$$x$$$ and $$$y$$$ as $$$d(x, y)$$$. Let the word have $$$n$$$ forms, the $$$i$$$-th of which is described with an integer $$$x_i$$$. All the $$$x_i$$$ are not necessarily different, as two various forms of the word can be written the same. Consider some word $$$y$$$. Then, closeness of the word $$$y$$$ is equal to the sum of distances to each of the word forms, i.xa0e. the sum $$$d(x_i, y)$$$ over all $$$1 le i le n$$$. The initial form is the word $$$y$$$ with minimal possible nearness. You need to help the scientists and write the program which finds the initial form of the word given all its known forms. Note that the initial form is not necessarily equal to any of the $$$n$$$ given forms. Input The first line contains an integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. The following are descriptions of the test cases. The first line contains two integers $$$n$$$ and $$$ell$$$ ($$$1 le n le 100$$$, $$$1 le ell le 30$$$) — the amount of word forms, and the number of letters in one word. The second line contains $$$n$$$ integers $$$x_i$$$ ($$$0 le x_i le 2^ell - 1$$$) — word forms. The integers are not necessarily different. Output For each test, print a single integer, the initial form of the word, i.xa0e. such $$$y$$$ ($$$0 le y le 2^ell - 1$$$) that the sum $$$d(x_i, y)$$$ over all $$$1 le i le n$$$ is minimal possible. Note that $$$y$$$ can differ from all the integers $$$x_i$$$. If there are multiple ways to restore the initial form, print any. Note In the first test case, the words can be written as $$$x_1 = 10010_2$$$, $$$x_2 = 01001_2$$$ and $$$x_3 = 10101_2$$$ in binary. Let $$$y = 10001_2$$$. Then, $$$d(x_1, y) = 2$$$ (the difference is in the fourth and the fifth positions), $$$d(x_2, y) = 2$$$ (the difference is in the first and the second positions), $$$d(x_3, y) = 1$$$ (the difference is in the third position). So, the closeness is $$$2 + 2 + 1 = 5$$$. It can be shown that you cannot achieve smaller closeness. In the second test case, all the forms are equal to $$$18$$$ ($$$10010_2$$$ in binary), so the initial form is also $$$18$$$. It's easy to see that closeness is equal to zero in this case.
800
true
true
false
false
false
false
false
false
false
false
2,493
1624B
Polycarp has $$$3$$$ positive integers $$$a$$$, $$$b$$$ and $$$c$$$. He can perform the following operation exactly once. Choose a positive integer $$$m$$$ and multiply exactly one of the integers $$$a$$$, $$$b$$$ or $$$c$$$ by $$$m$$$. Can Polycarp make it so that after performing the operation, the sequence of three numbers $$$a$$$, $$$b$$$, $$$c$$$ (in this order) forms an if there exists a number $$$d$$$ (called "common difference") such that $$$x_{i+1}=x_i+d$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$. In this problem, $$$n=3$$$. For example, the following sequences are AP: $$$[5, 10, 15]$$$, $$$[3, 2, 1]$$$, $$$[1, 1, 1]$$$, and $$$[13, 10, 7]$$$. The following sequences are not AP: $$$[1, 2, 4]$$$, $$$[0, 1, 0]$$$ and $$$[1, 3, 2]$$$. You need to answer $$$t$$$ independent test cases. Input The first line contains the number $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Each of the following $$$t$$$ lines contains $$$3$$$ integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 le a, b, c le 10^8$$$). Output For each test case print "YES" (without quotes) if Polycarp can choose a positive integer $$$m$$$ and multiply exactly one of the integers $$$a$$$, $$$b$$$ or $$$c$$$ by $$$m$$$ to make $$$[a, b, c]$$$ be an arithmetic progression. Print "NO" (without quotes) otherwise. You can print YES and NO in any (upper or lower) case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive answer). Example Input 11 10 5 30 30 5 10 1 2 3 1 6 3 2 6 3 1 1 1 1 1 2 1 1 3 1 100000000 1 2 1 1 1 2 2 Output YES YES YES YES NO YES NO YES YES NO YES Note In the first and second test cases, you can choose the number $$$m=4$$$ and multiply the second number ($$$b=5$$$) by $$$4$$$. In the first test case the resulting sequence will be $$$[10, 20, 30]$$$. This is an AP with a difference $$$d=10$$$. In the second test case the resulting sequence will be $$$[30, 20, 10]$$$. This is an AP with a difference $$$d=-10$$$. In the third test case, you can choose $$$m=1$$$ and multiply any number by $$$1$$$. The resulting sequence will be $$$[1, 2, 3]$$$. This is an AP with a difference $$$d=1$$$. In the fourth test case, you can choose $$$m=9$$$ and multiply the first number ($$$a=1$$$) by $$$9$$$. The resulting sequence will be $$$[9, 6, 3]$$$. This is an AP with a difference $$$d=-3$$$. In the fifth test case, it is impossible to make an AP.
900
true
false
true
false
false
false
false
false
false
false
2,499
1178C
Bob is decorating his kitchen, more precisely, the floor. He has found a prime candidate for the tiles he will use. They come in a simple form factorxa0—xa0a square tile that is diagonally split into white and black part as depicted in the figure below. The dimension of this tile is perfect for this kitchen, as he will need exactly $$$w imes h$$$ tiles without any scraps. That is, the width of the kitchen is $$$w$$$ tiles, and the height is $$$h$$$ tiles. As each tile can be rotated in one of four ways, he still needs to decide on how exactly he will tile the floor. There is a single aesthetic criterion that he wants to fulfil: two adjacent tiles must not share a colour on the edgexa0—xa0i.e. one of the tiles must have a white colour on the shared border, and the second one must be black. The picture on the left shows one valid tiling of a $$$3 imes 2$$$ kitchen. The picture on the right shows an invalid arrangement, as the bottom two tiles touch with their white parts. Find the number of possible tilings. As this number may be large, output its remainder when divided by $$$998244353$$$ (a prime number). Input The only line contains two space separated integers $$$w$$$, $$$h$$$xa0($$$1 leq w,h leq 1,000$$$)xa0—xa0the width and height of the kitchen, measured in tiles. Output Output a single integer $$$n$$$xa0—xa0the remainder of the number of tilings when divided by $$$998244353$$$.
1,300
true
true
false
false
false
false
false
false
false
false
4,828
1974D
Let's imagine the surface of Mars as an infinite coordinate plane. Initially, the rover Perseverance-2 and the helicopter Ingenuity-2 are located at the point with coordinates $$$(0, 0)$$$. A set of instructions $$$s$$$ consisting of $$$n$$$ instructions of the following types was specially developed for them: N: move one meter north (from point $$$(x, y)$$$ to $$$(x, y + 1)$$$); S: move one meter south (from point $$$(x, y)$$$ to $$$(x, y - 1)$$$); E: move one meter east (from point $$$(x, y)$$$ to $$$(x + 1, y)$$$); W: move one meter west (from point $$$(x, y)$$$ to $$$(x - 1, y)$$$). Each instruction must be executed either by the rover or by the helicopter. Moreover, each device must execute at least one instruction. Your task is to distribute the instructions in such a way that after executing all $$$n$$$ instructions, the helicopter and the rover end up at the same point, or determine that this is impossible. Input The first line of input contains $$$t$$$ ($$$1 leq t leq 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 instructions. The second line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of the characters 'N', 'S', 'E', 'W'xa0— the sequence of instructions. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10 ^ 5$$$. Output For each test case, if the required distribution of instructions exists, output a string $$$p$$$ of length $$$n$$$ consisting of the characters 'R', 'H'. If the $$$i$$$-th operation should be executed by the rover, then $$$p_i= ext{R}$$$, if the $$$i$$$-th operation should be executed by the helicopter, then $$$p_i= ext{H}$$$. If there are multiple solutions, output any of them. Otherwise, output NO. Example Input 10 6 NENSNE 3 WWW 6 NESSWS 2 SN 2 WE 4 SSNN 4 WESN 2 SS 4 EWNN 4 WEWE Output RRHRRH NO HRRHRH NO NO RHRH RRHH RH RRRH RRHH Note Let's consider the first example: the string $$$S = exttt{NENSNE}$$$. One of the possible solutions, shown in the figure below, is $$$p = exttt{RRHRRH}$$$, using which both the rover and the helicopter will end up one meter north and one meter east. For WWW, the solution is impossible.
1,400
false
true
true
false
false
true
false
false
false
false
446
1039C
The Metropolis computer network consists of $$$n$$$ servers, each has an encryption key in the range from $$$0$$$ to $$$2^k - 1$$$ assigned to it. Let $$$c_i$$$ be the encryption key assigned to the $$$i$$$-th server. Additionally, $$$m$$$ pairs of servers are directly connected via a data communication channel. Because of the encryption algorithms specifics, a data communication channel can only be considered safe if the two servers it connects have distinct encryption keys. The initial assignment of encryption keys is guaranteed to keep all data communication channels safe. You have been informed that a new virus is actively spreading across the internet, and it is capable to change the encryption key of any server it infects. More specifically, the virus body contains some unknown number $$$x$$$ in the same aforementioned range, and when server $$$i$$$ is infected, its encryption key changes from $$$c_i$$$ to $$$c_i oplus x$$$, where $$$oplus$$$ denotes the $$$, where $$$A$$$ is some (possibly empty) subset of the set of servers and $$$x$$$ is some number in the range from $$$0$$$ to $$$2^k - 1$$$, such that when all servers from the chosen subset $$$A$$$ and none of the others are infected by a virus containing the number $$$x$$$, all data communication channels remain safe. Since this number can be quite big, you are asked to find its remainder modulo $$$10^9 + 7$$$. Input The first line of input contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 leq n leq 500,000$$$, $$$0 leq m leq min(frac{n(n - 1)}{2}, 500,000)$$$, $$$0 leq k leq 60$$$)xa0— the number of servers, the number of pairs of servers directly connected by a data communication channel, and the parameter $$$k$$$, which defines the range of possible values for encryption keys. The next line contains $$$n$$$ integers $$$c_i$$$ ($$$0 leq c_i leq 2^k - 1$$$), the $$$i$$$-th of which is the encryption key used by the $$$i$$$-th server. The next $$$m$$$ lines contain two integers $$$u_i$$$ and $$$v_i$$$ each ($$$1 leq u_i, v_i leq n$$$, $$$u_i e v_i$$$) denoting that those servers are connected by a data communication channel. It is guaranteed that each pair of servers appears in this list at most once. Output The only output line should contain a single integerxa0— the number of safe infections of some subset of servers by a virus with some parameter, modulo $$$10^9 + 7$$$. Examples Input 4 4 2 0 1 0 1 1 2 2 3 3 4 4 1 Input 4 5 3 7 1 7 2 1 2 2 3 3 4 4 1 2 4 Note Consider the first example. Possible values for the number $$$x$$$ contained by the virus are $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$. For values $$$0$$$, $$$2$$$ and $$$3$$$ the virus can infect any subset of the set of servers, which gives us $$$16$$$ pairs for each values. A virus containing the number $$$1$$$ can infect either all of the servers, or none. This gives us $$$16 + 2 + 16 + 16 = 50$$$ pairs in total.
2,200
true
false
false
false
false
false
false
false
true
true
5,500
1183F
One important contest will take place on the most famous programming platform (Topforces) very soon! The authors have a pool of $$$n$$$ problems and should choose at most three of them into this contest. The prettiness of the $$$i$$$-th problem is $$$a_i$$$. The authors have to compose the most pretty contest (in other words, the cumulative prettinesses of chosen problems should be maximum possible). But there is one important thing in the contest preparation: because of some superstitions of authors, the prettinesses of problems cannot divide each other. In other words, if the prettinesses of chosen problems are $$$x, y, z$$$, then $$$x$$$ should be divisible by neither $$$y$$$, nor $$$z$$$, $$$y$$$ should be divisible by neither $$$x$$$, nor $$$z$$$ and $$$z$$$ should be divisible by neither $$$x$$$, nor $$$y$$$. If the prettinesses of chosen problems are $$$x$$$ and $$$y$$$ then neither $$$x$$$ should be divisible by $$$y$$$ nor $$$y$$$ should be divisible by $$$x$$$. Any contest composed from one problem is considered good. Your task is to find out the maximum possible total prettiness of the contest composed of at most three problems from the given pool. You have to answer $$$q$$$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$) — the number of queries. The first line of the query contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of problems. The second line of the query contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$2 le a_i le 2 cdot 10^5$$$), where $$$a_i$$$ is the prettiness of the $$$i$$$-th problem. It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 cdot 10^5$$$.
2,100
true
false
false
false
false
false
true
false
true
false
4,797
1286F
To defeat Lord Voldemort, Harry needs to destroy all horcruxes first. The last horcrux is an array $$$a$$$ of $$$n$$$ integers, which also needs to be destroyed. The array is considered destroyed if all its elements are zeroes. To destroy the array, Harry can perform two types of operations: 1. choose an index $$$i$$$ ($$$1 le i le n$$$), an integer $$$x$$$, and subtract $$$x$$$ from $$$a_i$$$. 2. choose two indices $$$i$$$ and $$$j$$$ ($$$1 le i, j le n; i e j$$$), an integer $$$x$$$, and subtract $$$x$$$ from $$$a_i$$$ and $$$x + 1$$$ from $$$a_j$$$. Note that $$$x$$$ does not have to be positive. Harry is in a hurry, please help him to find the minimum number of operations required to destroy the array and exterminate Lord Voldemort. Input The first line contains a single integer $$$n$$$xa0— the size of the array $$$a$$$ ($$$1 le n le 20$$$). The following line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$xa0— array elements ($$$-10^{15} le a_i le 10^{15}$$$). Output Output a single integerxa0— the minimum number of operations required to destroy the array $$$a$$$. Note In the first example one can just apply the operation of the first kind three times. In the second example, one can apply the operation of the second kind two times: first, choose $$$i = 2, j = 1, x = 4$$$, it transforms the array into $$$(0, -1, -2)$$$, and then choose $$$i = 3, j = 2, x = -2$$$ to destroy the array. In the third example, there is nothing to be done, since the array is already destroyed.
3,100
true
false
true
true
false
true
true
false
false
false
4,255
930B
Vasya and Kolya play a game with a string, using the following rules. Initially, Kolya creates a string _s_, consisting of small English letters, and uniformly at random chooses an integer _k_ from a segment [0,u2009_len_(_s_)u2009-u20091]. He tells Vasya this string _s_, and then shifts it _k_ letters to the left, i.xa0e. creates a new string _t_u2009=u2009_s__k_u2009+u20091_s__k_u2009+u20092... _s__n__s_1_s_2... _s__k_. Vasya does not know the integer _k_ nor the string _t_, but he wants to guess the integer _k_. To do this, he asks Kolya to tell him the first letter of the new string, and then, after he sees it, open one more letter on some position, which Vasya can choose. Vasya understands, that he can't guarantee that he will win, but he wants to know the probability of winning, if he plays optimally. He wants you to compute this probability. Note that Vasya wants to know the value of _k_ uniquely, it means, that if there are at least two cyclic shifts of _s_ that fit the information Vasya knowns, Vasya loses. Of course, at any moment of the game Vasya wants to maximize the probability of his win. Output Print the only numberxa0— the answer for the problem. You answer is considered correct, if its absolute or relative error does not exceed 10u2009-u20096. Formally, let your answer be _a_, and the jury's answer be _b_. Your answer is considered correct if Note In the first example Vasya can always open the second letter after opening the first letter, and the cyclic shift is always determined uniquely. In the second example if the first opened letter of _t_ is "t" or "c", then Vasya can't guess the shift by opening only one other letter. On the other hand, if the first letter is "i" or "a", then he can open the fourth letter and determine the shift uniquely.
1,600
false
false
true
false
false
false
false
false
false
false
5,985
125C
Problem - 125C - Codeforces =============== xa0 that would last for several days. Next day the hobbits wrote a guest list, some non-empty set containing all the inhabitants of the Shire. To ensure that everybody enjoy themselves and nobody gets bored, for any two days (say, days A and B) of the GP there existed at least one hobbit, invited to come on day A and on day B. However, to ensure that nobody has a row, for any three different days A, B, C there shouldn't be a hobbit invited on days A, B and C. The Shire inhabitants are keen on keeping the GP going for as long as possible. Your task is given number _n_, to indicate the GP's maximum duration and the guest lists for each day. Input The first line contains an integer _n_ (3u2009≤u2009_n_u2009≤u200910000), representing the number of hobbits. Output In the first output line print a number _k_ — the maximum duration of GP in days. Then on _k_ lines print the guest lists, (the guests should be separated by spaces). Print each guest list on the single line. Each list can contain an arbitrary positive number of hobbits. The hobbits are numbered with integers from 1 to _n_. Examples Input 4 Output 3 1 2 1 3 2 3 Input 5 Output 3 1 2 1 3 2 3
1,600
false
true
false
false
false
true
false
false
false
false
9,380
1254C
This is an interactive problem. Khanh has $$$n$$$ points on the Cartesian plane, denoted by $$$a_1, a_2, ldots, a_n$$$. All points' coordinates are integers between $$$-10^9$$$ and $$$10^9$$$, inclusive. No three points are collinear. He says that these points are vertices of a convex polygon; in other words, there exists a permutation $$$p_1, p_2, ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$ such that the polygon $$$a_{p_1} a_{p_2} ldots a_{p_n}$$$ is convex and vertices are listed in counter-clockwise order. Khanh gives you the number $$$n$$$, but hides the coordinates of his points. Your task is to guess the above permutation by asking multiple queries. In each query, you give Khanh $$$4$$$ integers $$$t$$$, $$$i$$$, $$$j$$$, $$$k$$$; where either $$$t = 1$$$ or $$$t = 2$$$; and $$$i$$$, $$$j$$$, $$$k$$$ are three distinct indices from $$$1$$$ to $$$n$$$, inclusive. In response, Khanh tells you: if $$$t = 1$$$, the area of the triangle $$$a_ia_ja_k$$$ multiplied by $$$2$$$. if $$$t = 2$$$, the sign of the cross product of two vectors $$$overrightarrow{a_ia_j}$$$ and $$$overrightarrow{a_ia_k}$$$. Recall that the cross product of vector $$$overrightarrow{a} = (x_a, y_a)$$$ and vector $$$overrightarrow{b} = (x_b, y_b)$$$ is the integer $$$x_a cdot y_b - x_b cdot y_a$$$. The sign of a number is $$$1$$$ it it is positive, and $$$-1$$$ otherwise. It can be proven that the cross product obtained in the above queries can not be $$$0$$$. You can ask at most $$$3 cdot n$$$ queries. Please note that Khanh fixes the coordinates of his points and does not change it while answering your queries. You do not need to guess the coordinates. In your permutation $$$a_{p_1}a_{p_2}ldots a_{p_n}$$$, $$$p_1$$$ should be equal to $$$1$$$ and the indices of vertices should be listed in counter-clockwise order. Interaction You start the interaction by reading $$$n$$$ ($$$3 leq n leq 1,000$$$)xa0— the number of vertices. To ask a query, write $$$4$$$ integers $$$t$$$, $$$i$$$, $$$j$$$, $$$k$$$ ($$$1 leq t leq 2$$$, $$$1 leq i, j, k leq n$$$) in a separate line. $$$i$$$, $$$j$$$ and $$$k$$$ should be distinct. Then read a single integer to get the answer to this query, as explained above. It can be proven that the answer of a query is always an integer. When you find the permutation, write a number $$$0$$$. Then write $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ in the same line. After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hack format To hack, use the following format: The first line contains an integer $$$n$$$ ($$$3 leq n leq 1,000$$$)xa0— the number of vertices. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^9 le x_i, y_i le 10^9$$$)xa0— the coordinate of the point $$$a_i$$$.
2,300
true
false
false
false
false
true
false
false
false
false
4,413
178A3
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below. The playing field is a sequence of _n_ non-negative integers _a__i_ numbered from 1 to _n_. The goal of the game is to make numbers _a_1,u2009_a_2,u2009...,u2009_a__k_ (i.e. some prefix of the sequence) equal to zero for some fixed _k_ (_k_u2009<u2009_n_), and this should be done in the smallest possible number of moves. One move is choosing an integer _i_ (1u2009≤u2009_i_u2009≤u2009_n_) such that _a__i_u2009>u20090 and an integer _t_ (_t_u2009≥u20090) such that _i_u2009+u20092_t_u2009≤u2009_n_. After the values of _i_ and _t_ have been selected, the value of _a__i_ is decreased by 1, and the value of _a__i_u2009+u20092_t_ is increased by 1. For example, let _n_u2009=u20094 and _a_u2009=u2009(1,u20090,u20091,u20092), then it is possible to make move _i_u2009=u20093, _t_u2009=u20090 and get _a_u2009=u2009(1,u20090,u20090,u20093) or to make move _i_u2009=u20091, _t_u2009=u20091 and get _a_u2009=u2009(0,u20090,u20092,u20092) (the only possible other move is _i_u2009=u20091, _t_u2009=u20090). You are given _n_ and the initial sequence _a__i_. The task is to calculate the minimum number of moves needed to make the first _k_ elements of the original sequence equal to zero for each possible _k_ (1u2009≤u2009_k_u2009<u2009_n_). Input The first input line contains a single integer _n_. The second line contains _n_ integers _a__i_ (0u2009≤u2009_a__i_u2009≤u2009104), separated by single spaces. The input limitations for getting 20 points are: 1u2009≤u2009_n_u2009≤u2009300 The input limitations for getting 50 points are: 1u2009≤u2009_n_u2009≤u20092000 The input limitations for getting 100 points are: 1u2009≤u2009_n_u2009≤u2009105 Output Print exactly _n_u2009-u20091 lines: the _k_-th output line must contain the minimum number of moves needed to make the first _k_ elements of the original sequence _a__i_ equal to zero. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
1,100
false
true
false
false
false
false
false
false
false
false
9,138
643E
Limak is a little grizzly bear. He will once attack Deerland but now he can only destroy trees in role-playing games. Limak starts with a tree with one vertex. The only vertex has index 1 and is a root of the tree. Sometimes, a game chooses a subtree and allows Limak to attack it. When a subtree is attacked then each of its edges is destroyed with probability , independently of other edges. Then, Limak gets the penaltyxa0— an integer equal to the height of the subtree after the attack. The height is defined as the maximum number of edges on the path between the root of the subtree and any vertex in the subtree. You must handle queries of two types. 1 v denotes a query of the first type. A new vertex appears and its parent is _v_. A new vertex has the next available index (so, new vertices will be numbered 2,u20093,u2009...). 2 v denotes a query of the second type. For a moment let's assume that the game allows Limak to attack a subtree rooted in _v_. Then, what would be the expected value of the penalty Limak gets after the attack? In a query of the second type, Limak doesn't actually attack the subtree and thus the query doesn't affect next queries. Input The first line of the input contains one integer _q_ (1u2009≤u2009_q_u2009≤u2009500u2009000)xa0— the number of queries. Then, _q_ lines follow. The _i_-th of them contains two integers _type__i_ and _v__i_ (1u2009≤u2009_type__i_u2009≤u20092). If _type__i_u2009=u20091 then _v__i_ denotes a parent of a new vertex, while if _type__i_u2009=u20092 then you should print the answer for a subtree rooted in _v__i_. It's guaranteed that there will be at least 1 query of the second type, that is, the output won't be empty. It's guaranteed that just before the _i_-th query a vertex _v__i_ already exists. Output For each query of the second type print one real numberxa0—the expected value of the penalty if Limak attacks the given subtree. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct if . Examples Input 7 1 1 1 1 2 1 1 2 1 3 2 2 2 1 Output 0.7500000000 0.5000000000 1.1875000000 Input 8 2 1 1 1 1 2 1 3 1 4 2 1 1 4 2 1 Output 0.0000000000 0.9375000000 0.9687500000 Note Below, you can see the drawing for the first sample. Red circles denote queries of the second type.
2,700
true
false
false
true
false
false
false
false
false
false
7,240
616F
Problem - 616F - Codeforces =============== xa0 over all strings. Note that the string _s_ is not necessarily some string from _t_. Input The first line contains the only integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of strings in _t_. Each of the next _n_ lines contains contains a non-empty string _t__i_. _t__i_ contains only lowercase English letters. It is guaranteed that the sum of lengths of all strings in _t_ is not greater than 5·105. The last line contains _n_ integers _c__i_ (u2009-u2009107u2009≤u2009_c__i_u2009≤u2009107) — the cost of the _i_-th string. Output Print the only integer _a_ — the maximal value of the function _f_(_s_) over all strings _s_. Note one more time that the string _s_ is not necessarily from _t_. Examples Input 2 aa bb 2 1 Output 4 Input 2 aa ab 2 1 Output 5
2,700
false
false
false
false
true
false
false
false
true
false
7,370
1213F
Authors have come up with the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. You are given two permutations of its indices (not necessary equal) $$$p$$$ and $$$q$$$ (both of length $$$n$$$). Recall that the permutation is the array of length $$$n$$$ which contains each integer from $$$1$$$ to $$$n$$$ exactly once. For all $$$i$$$ from $$$1$$$ to $$$n-1$$$ the following properties hold: $$$s[p_i] le s[p_{i + 1}]$$$ and $$$s[q_i] le s[q_{i + 1}]$$$. It means that if you will write down all characters of $$$s$$$ in order of permutation indices, the resulting string will be sorted in the non-decreasing order. Your task is to restore any such string $$$s$$$ of length $$$n$$$ consisting of at least $$$k$$$ distinct lowercase Latin letters which suits the given permutations. If there are multiple answers, you can print any of them. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5, 1 le k le 26$$$) — the length of the string and the number of distinct characters required. The second line of the input contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$, all $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$) — the permutation $$$p$$$. The third line of the input contains $$$n$$$ integers $$$q_1, q_2, dots, q_n$$$ ($$$1 le q_i le n$$$, all $$$q_i$$$ are distinct integers from $$$1$$$ to $$$n$$$) — the permutation $$$q$$$. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string $$$s$$$ on the second line. It should consist of $$$n$$$ lowercase Latin letters, contain at least $$$k$$$ distinct characters and suit the given permutations. If there are multiple answers, you can print any of them.
2,100
false
true
true
false
true
false
false
false
false
true
4,613
755C
PolandBall lives in a forest with his family. There are some trees in the forest. Trees are undirected acyclic graphs with _k_ vertices and _k_u2009-u20091 edges, where _k_ is some integer. Note that one vertex is a valid tree. There is exactly one relative living in each vertex of each tree, they have unique ids from 1 to _n_. For each Ball _i_ we know the id of its most distant relative living on the same tree. If there are several such vertices, we only know the value of the one with smallest id among those. How many trees are there in the forest? Input The first line contains single integer _n_ (1u2009≤u2009_n_u2009≤u2009104)xa0— the number of Balls living in the forest. The second line contains a sequence _p_1,u2009_p_2,u2009...,u2009_p__n_ of length _n_, where (1u2009≤u2009_p__i_u2009≤u2009_n_) holds and _p__i_ denotes the most distant from Ball _i_ relative living on the same tree. If there are several most distant relatives living on the same tree, _p__i_ is the id of one with the smallest id. It's guaranteed that the sequence _p_ corresponds to some valid forest. Hacking: To hack someone, you should provide a correct forest as a test. The sequence _p_ will be calculated according to the forest and given to the solution you try to hack as input. Use the following format: In the first line, output the integer _n_ (1u2009≤u2009_n_u2009≤u2009104)xa0— the number of Balls and the integer _m_ (0u2009≤u2009_m_u2009<u2009_n_)xa0— the total number of edges in the forest. Then _m_ lines should follow. The _i_-th of them should contain two integers _a__i_ and _b__i_ and represent an edge between vertices in which relatives _a__i_ and _b__i_ live. For example, the first sample is written as follows: 5 3 1 2 3 4 4 5 Output You should output the number of trees in the forest where PolandBall lives. Interaction From the technical side, this problem is interactive. However, it should not affect you (except hacking) since there is no interaction. Note In the first sample testcase, possible forest is: 1-2 3-4-5. There are 2 trees overall. In the second sample testcase, the only possible graph is one vertex and no edges. Therefore, there is only one tree.
1,300
false
false
false
false
false
false
false
false
false
true
6,775
2001B
There is an integer sequence $$$a$$$ of length $$$n$$$, where each element is initially $$$-1$$$. Misuki has two typewriters where the first one writes letters from left to right, with a pointer initially pointing to $$$1$$$, and another writes letters from right to left with a pointer initially pointing to $$$n$$$. Misuki would choose one of the typewriters and use it to perform the following operations until $$$a$$$ becomes a permutation of $$$[1, 2, ldots, n]$$$ write number: write the minimum positive integer that isn't present in the array $$$a$$$ to the element $$$a_i$$$, $$$i$$$ is the position where the pointer points at. Such operation can be performed only when $$$a_i = -1$$$. carriage return: return the pointer to its initial position (i.e. $$$1$$$ for the first typewriter, $$$n$$$ for the second) move pointer: move the pointer to the next position, let $$$i$$$ be the position the pointer points at before this operation, if Misuki is using the first typewriter, $$$i := i + 1$$$ would happen, and $$$i := i - 1$$$ otherwise. Such operation can be performed only if after the operation, $$$1 le i le n$$$ holds. Your task is to construct any permutation $$$p$$$ of length $$$n$$$, such that the minimum number of carriage return operations needed to make $$$a = p$$$ is the same no matter which typewriter Misuki is using. Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 le t le 500$$$) — 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 le n le 2 cdot 10^5$$$) — the length of the permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a line of $$$n$$$ integers, representing the permutation $$$p$$$ of length $$$n$$$ such that the minimum number of carriage return operations needed to make $$$a = p$$$ is the same no matter which typewriter Misuki is using, or $$$-1$$$ if it is impossible to do so. If there are multiple valid permutations, you can output any of them. Note In the first testcase, it's possible to make $$$a = p = [1]$$$ using $$$0$$$ carriage return operations. In the second testcase, it is possible to make $$$a = p = [1, 2]$$$ with the minimal number of carriage returns as follows: If Misuki is using the first typewriter: Write number: write $$$1$$$ to $$$a_1$$$, $$$a$$$ becomes $$$[1, -1]$$$ Move pointer: move the pointer to the next position. (i.e. $$$2$$$) Write number: write $$$2$$$ to $$$a_2$$$, $$$a$$$ becomes $$$[1, 2]$$$ If Misuki is using the second typewriter: Move pointer: move the pointer to the next position. (i.e. $$$1$$$) Write number: write $$$1$$$ to $$$a_1$$$, $$$a$$$ becomes $$$[1, -1]$$$ Carriage return: return the pointer to $$$2$$$. Write number: write $$$2$$$ to $$$a_2$$$, $$$a$$$ becomes $$$[1, 2]$$$ It can be proven that the minimum number of carriage returns needed to transform $$$a$$$ into $$$p$$$ when using the first typewriter is $$$0$$$ and it is $$$1$$$ when using the second one, so this permutation is not valid. Similarly, $$$p = [2, 1]$$$ is also not valid, so there is no solution for $$$n = 2$$$. In the third testcase, it is possibile to make $$$a = p = [3, 1, 2]$$$ with $$$1$$$ carriage return with both the first and the second typewriter. It can be proven that, for both typewriters, it is impossible to write $$$p$$$ with $$$0$$$ carriage returns. With the first typewriter it is possible to: Move pointer: move the pointer to the next position. (i.e. $$$2$$$) Write number: write $$$1$$$ to $$$a_2$$$, $$$a$$$ becomes $$$[-1, 1, -1]$$$ Move pointer: move the pointer to the next position. (i.e. $$$3$$$) Write number: write $$$2$$$ to $$$a_3$$$, $$$a$$$ becomes $$$[-1, 1, 2]$$$ Carriage return: return the pointer to $$$1$$$. Write number: write $$$3$$$ to $$$a_1$$$, $$$a$$$ becomes $$$[3,1,2]$$$ With the second typewriter it is possible to: Move pointer: move the pointer to the next position. (i.e. $$$2$$$) Write number: write $$$1$$$ to $$$a_2$$$, $$$a$$$ becomes $$$[-1, 1, -1]$$$ Carriage return: return the pointer to $$$3$$$. Write number: write $$$2$$$ to $$$a_3$$$, $$$a$$$ becomes $$$[-1, 1, 2]$$$ Move pointer: move the pointer to the next position. (i.e. $$$2$$$) Move pointer: move the pointer to the next position. (i.e. $$$1$$$) Write number: write $$$3$$$ to $$$a_1$$$, $$$a$$$ becomes $$$[3, 1, 2]$$$
800
false
false
false
false
false
true
false
false
false
false
254
1909F2
In the easy version, the $$$a_i$$$ are in the range $$$[0, n]$$$; in the hard version, the $$$a_i$$$ are in the range $$$[-1, n]$$$ and the definition of good permutation is slightly different. You can make hacks only if all versions of the problem are solved. You are given an integer $$$n$$$ and an array $$$a_1, a_2, dots, a_n$$$ of integers in the range $$$[-1, n]$$$. A permutation $$$p_1, p_2, dots, p_n$$$ of $$$[1, 2, dots, n]$$$ is good if, for each $$$i$$$, the following condition is true: if $$$a_i eq -1$$$, the number of values $$$leq i$$$ in $$$[p_1, p_2, dots, p_i]$$$ is exactly $$$a_i$$$. Count the good permutations of $$$[1, 2, dots, n]$$$, modulo $$$998,244,353$$$. 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$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-1 le a_i le n$$$), which describe the conditions for a good permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single line containing the number of good permutations, modulo $$$998,244,353$$$. Example Input 10 5 -1 -1 -1 -1 -1 5 1 2 3 4 5 6 0 2 2 2 -1 -1 6 -1 -1 -1 -1 -1 5 6 -1 -1 3 2 -1 -1 15 0 0 -1 -1 -1 2 2 -1 -1 -1 -1 9 11 13 15 6 0 2 2 2 4 6 6 0 1 3 4 5 5 6 1 2 3 2 4 6 15 0 0 1 1 1 2 3 4 5 6 7 9 11 13 15 Output 120 1 4 0 0 494403526 4 0 0 532305727 Note In the first test case, all the permutations of length $$$5$$$ are good, so there are $$$120$$$ good permutations. In the second test case, the only good permutation is $$$[1, 2, 3, 4, 5]$$$. In the third test case, there are $$$4$$$ good permutations: $$$[2, 1, 5, 6, 3, 4]$$$, $$$[2, 1, 5, 6, 4, 3]$$$, $$$[2, 1, 6, 5, 3, 4]$$$, $$$[2, 1, 6, 5, 4, 3]$$$. For example, $$$[2, 1, 5, 6, 3, 4]$$$ is good because: $$$a_1 = 0$$$, and there are $$$0$$$ values $$$leq 1$$$ in $$$[p_1] = [2]$$$; $$$a_2 = 2$$$, and there are $$$2$$$ values $$$leq 2$$$ in $$$[p_1, p_2] = [2, 1]$$$; $$$a_3 = 2$$$, and there are $$$2$$$ values $$$leq 3$$$ in $$$[p_1, p_2, p_3] = [2, 1, 5]$$$; $$$a_4 = 2$$$, and there are $$$2$$$ values $$$leq 4$$$ in $$$[p_1, p_2, p_3, p_4] = [2, 1, 5, 6]$$$; $$$a_5 = -1$$$, so there are no restrictions on $$$[p_1, p_2, p_3, p_4, p_5]$$$; $$$a_6 = -1$$$, so there are no restrictions on $$$[p_1, p_2, p_3, p_4, p_5, p_6]$$$.
2,500
true
false
false
true
false
false
false
false
false
false
851
1805F1
This is the easy version of the problem. It differs from the hard one only in constraints on $$$n$$$. You can make hacks only if you lock both versions. Let $$$a_1, a_2, ldots, a_n$$$ be an array of non-negative integers. Let $$$F(a_1, a_2, ldots, a_n)$$$ be the sorted in the non-decreasing order array of $$$n - 1$$$ smallest numbers of the form $$$a_i + a_j$$$, where $$$1 le i < j le n$$$. In other words, $$$F(a_1, a_2, ldots, a_n)$$$ is the sorted in the non-decreasing order array of $$$n - 1$$$ smallest sums of all possible pairs of elements of the array $$$a_1, a_2, ldots, a_n$$$. For example, $$$F(1, 2, 5, 7) = [1 + 2, 1 + 5, 2 + 5] = [3, 6, 7]$$$. You are given an array of non-negative integers $$$a_1, a_2, ldots, a_n$$$. Determine the single element of the array $$$underbrace{F(F(Fldots F}_{n-1}(a_1, a_2, ldots, a_n)ldots))$$$. Since the answer can be quite large, output it modulo $$$10^9+7$$$. Input The first line contains one integer $$$n$$$ ($$$2 le n le 3000$$$)xa0— the initial length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$)xa0— the array elements. Output Output a single numberxa0— the answer modulo $$$10^9 + 7$$$. Examples Input 3 1000000000 1000000000 777 Note In the first test, the array is transformed as follows: $$$[1, 2, 4, 5, 6] o [3, 5, 6, 6] o [8, 9, 9] o [17, 17] o [34]$$$. The only element of the final array is $$$34$$$. In the second test, $$$F(a_1, a_2, ldots, a_n)$$$ is $$$[2, 2, 2, 8, 8, 8, 8, 8]$$$. This array is made up of $$$3$$$ numbers of the form $$$1 + 1$$$ and $$$5$$$ numbers of the form $$$1 + 7$$$. In the fourth test, the array is transformed as follows: $$$[10^9, 10^9, 777] o [10^9+777, 10^9+777] o [2 cdot 10^9 + 1554]$$$. $$$2 cdot 10^9 + 1554$$$ modulo $$$10^9+7$$$ equals $$$1540$$$.
2,600
true
false
true
false
false
false
true
true
true
false
1,444
1482A
Michael is accused of violating the social distancing rules and creating a risk of spreading coronavirus. He is now sent to prison. Luckily, Michael knows exactly what the prison looks like from the inside, especially since it's very simple. The prison can be represented as a rectangle $$$a imes b$$$ which is divided into $$$ab$$$ cells, each representing a prison cell, common sides being the walls between cells, and sides on the perimeter being the walls leading to freedom. Before sentencing, Michael can ask his friends among the prison employees to make (very well hidden) holes in some of the walls (including walls between cells and the outermost walls). Michael wants to be able to get out of the prison after this, no matter which cell he is placed in. However, he also wants to break as few walls as possible. Your task is to find out the smallest number of walls to be broken so that there is a path to the outside from every cell after this. Input The first line contains a single integer $$$t$$$ ($$$1leq tleq 100$$$)xa0— the number of test cases. Each of the following $$$t$$$ lines contains two integers $$$a$$$ and $$$b$$$ ($$$1leq a, bleq 100$$$), representing a corresponding test case. Output For each test case print the single integer on a separate linexa0— the answer to the problem. Note Some possible escape plans for the example test cases are shown below. Broken walls are shown in gray, not broken walls are shown in black.
800
true
false
false
false
false
false
false
false
false
false
3,267
1140E
Problem - 1140E - 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 combinatorics divide and conquer dp *2200 No tag edit access → Contest materials such that $$$forall i in {0, 1, dots, r - l}$$$ $$$b_{l + i} = b_{r - i}$$$. If an array is not bad, it is good. Now you are given an array $$$a_1, a_2, dots, a_n$$$. Some elements are replaced by $$$-1$$$. Calculate the number of good arrays you can obtain by replacing each $$$-1$$$ with some integer from $$$1$$$ to $$$k$$$. Since the answer can be large, print it modulo $$$998244353$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n, k le 2 cdot 10^5$$$) — the length of array $$$a$$$ and the size of "alphabet", i.u2009e., the upper bound on the numbers you may use to replace $$$-1$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$a_i = -1$$$ or $$$1 le a_i le k$$$) — the array $$$a$$$. Output Print one integer — the number of good arrays you can get, modulo $$$998244353$$$. Examples Input 2 3 -1 -1 Output 9 Input 5 2 1 -1 -1 1 2 Output 0 Input 5 3 1 -1 -1 1 2 Output 2 Input 4 200000 -1 -1 12345 -1 Output 735945883
2,200
false
false
false
true
false
false
false
false
false
false
5,024
1872A
You have two vessels with water. The first vessel contains $$$a$$$ grams of water, and the second vessel contains $$$b$$$ grams of water. Both vessels are very large and can hold any amount of water. You also have an empty cup that can hold up to $$$c$$$ grams of water. In one move, you can scoop up to $$$c$$$ grams of water from any vessel and pour it into the other vessel. Note that the mass of water poured in one move does not have to be an integer. What is the minimum number of moves required to make the masses of water in the vessels equal? Note that you cannot perform any actions other than the described moves. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). The description of the test cases follows. Each test case consists of a single line containing three integers $$$a$$$, $$$b$$$, and $$$c$$$ ($$$1 le a, b, c le 100$$$)xa0— the mass of water in the vessels and the capacity of the cup, respectively. Output For each test case, output a single numberxa0— the minimum number of moves required to make the masses of water in the vessels equal. It can be shown, that it is always possible. Example Input 6 3 7 2 17 4 3 17 17 1 17 21 100 1 100 1 97 4 3 Note In the first test case, only one move is enough: if we pour $$$2$$$ grams of water from the second vessel into the first one, both vessels will contain $$$5$$$ grams of water. In the second example test case, three moves are enough: Pour $$$3$$$ grams of water from the first vessel into the second one. After this move, the first vessel will contain $$$17 - 3 = 14$$$ grams of water, and the second vessel will contain $$$4 + 3 = 7$$$ grams. Pour $$$2$$$ grams of water from the first vessel into the second one. After this move, the first vessel will contain $$$14 - 2 = 12$$$ grams of water, and the second vessel will contain $$$7 + 2 = 9$$$ grams. Finally, pour $$$1.5$$$ grams of water from the first vessel into the second one. After this move, the first vessel will contain $$$12 - 1.5 = 10.5$$$ grams of water, and the second vessel will contain $$$9 + 1.5 = 10.5$$$ grams. Note that this is not the only way to equalize the vessels in $$$3$$$ moves, but there is no way to do it in $$$2$$$ moves. In the third example test case, the vessels initially contain the same amount of water, so no moves are needed. The answer is $$$0$$$.
800
true
true
false
false
false
false
true
false
false
false
1,051
1905D
For an array $$$a$$$, define its cost as $$$sum_{i=1}^{n} operatorname{mex} ^dagger ([a_1,a_2,ldots,a_i])$$$. You are given a permutation$$$^ddagger$$$ $$$p$$$ of the set $$${0,1,2,ldots,n-1}$$$. Find the maximum cost across all cyclic shifts of $$$p$$$. $$$^daggeroperatorname{mex}([b_1,b_2,ldots,b_m])$$$ is the smallest non-negative integer $$$x$$$ such that $$$x$$$ does not occur among $$$b_1,b_2,ldots,b_m$$$. $$$^ddagger$$$A permutation of the set $$${0,1,2,...,n-1}$$$ is an array consisting of $$$n$$$ distinct integers from $$$0$$$ to $$$n-1$$$ in arbitrary order. For example, $$$[1,2,0,4,3]$$$ is a permutation, but $$$[0,1,1]$$$ is not a permutation ($$$1$$$ appears twice in the array), and $$$[0,2,3]$$$ is also not a permutation ($$$n=3$$$ but there is $$$3$$$ in the array). Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$)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$$$ ($$$1 le n le 10^6$$$)xa0— the length of the permutation $$$p$$$. The second line of each test case contain $$$n$$$ distinct integers $$$p_1, p_2, ldots, p_n$$$ ($$$0 le p_i < n$$$) — the elements of the permutation $$$p$$$. It is guaranteed that sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Note In the first test case, the cyclic shift that yields the maximum cost is $$$[2,1,0,5,4,3]$$$ with cost $$$0+0+3+3+3+6=15$$$. In the second test case, the cyclic shift that yields the maximum cost is $$$[0,2,1]$$$ with cost $$$1+1+3=5$$$.
2,000
true
false
true
false
true
false
false
false
false
false
880
1666J
Julia's $$$n$$$ friends want to organize a startup in a new country they moved to. They assigned each other numbers from 1 to $$$n$$$ according to the jobs they have, from the most front-end tasks to the most back-end ones. They also estimated a matrix $$$c$$$, where $$$c_{ij} = c_{ji}$$$ is the average number of messages per month between people doing jobs $$$i$$$ and $$$j$$$. Now they want to make a hierarchy tree. It will be a binary tree with each node containing one member of the team. Some member will be selected as a leader of the team and will be contained in the root node. In order for the leader to be able to easily reach any subordinate, for each node $$$v$$$ of the tree, the following should apply: all members in its left subtree must have smaller numbers than $$$v$$$, and all members in its right subtree must have larger numbers than $$$v$$$. After the hierarchy tree is settled, people doing jobs $$$i$$$ and $$$j$$$ will be communicating via the shortest path in the tree between their nodes. Let's denote the length of this path as $$$d_{ij}$$$. Thus, the cost of their communication is $$$c_{ij} cdot d_{ij}$$$. Your task is to find a hierarchy tree that minimizes the total cost of communication over all pairs: $$$sum_{1 le i < j le n} c_{ij} cdot d_{ij}$$$. Input The first line contains an integer $$$n$$$ ($$$1 le n le 200$$$)xa0– the number of team members organizing a startup. The next $$$n$$$ lines contain $$$n$$$ integers each, $$$j$$$-th number in $$$i$$$-th line is $$$c_{ij}$$$xa0— the estimated number of messages per month between team members $$$i$$$ and $$$j$$$ ($$$0 le c_{ij} le 10^9; c_{ij} = c_{ji}; c_{ii} = 0$$$). Output Output a description of a hierarchy tree that minimizes the total cost of communication. To do so, for each team member from 1 to $$$n$$$ output the number of the member in its parent node, or 0 for the leader. If there are many optimal trees, output a description of any one of them. Example Input 4 0 566 1 0 566 0 239 30 1 239 0 1 0 30 1 0 Note The minimal possible total cost is $$$566 cdot 1+239 cdot 1+30 cdot 1+1 cdot 2+1 cdot 2=839$$$:
2,100
false
false
false
true
false
true
false
false
false
false
2,276
1149A
We're giving away nice huge bags containing number tiles! A bag we want to present to you contains $$$n$$$ tiles. Each of them has a single number written on itxa0— either $$$1$$$ or $$$2$$$. However, there is one condition you must fulfill in order to receive the prize. You will need to put all the tiles from the bag in a sequence, in any order you wish. We will then compute the sums of all prefixes in the sequence, and then count how many of these sums are prime numbers. If you want to keep the prize, you will need to maximize the number of primes you get. Can you win the prize? Hurry up, the bags are waiting! Input The first line of the input contains a single integer $$$n$$$ ($$$1 leq n leq 200,000$$$) — the number of number tiles in the bag. The following line contains $$$n$$$ space-separated integers $$$a_1, a_2, dots, a_n$$$ ($$$a_i in {1, 2}$$$) — the values written on the tiles. Output Output a permutation $$$b_1, b_2, dots, b_n$$$ of the input sequence $$$(a_1, a_2, dots, a_n)$$$ maximizing the number of the prefix sums being prime numbers. If there are multiple optimal permutations, output any. Note The first solution produces the prefix sums $$$1, mathbf{color{blue}{2}}, mathbf{color{blue}{3}}, mathbf{color{blue}{5}}, mathbf{color{blue}{7}}$$$ (four primes constructed), while the prefix sums in the second solution are $$$1, mathbf{color{blue}{2}}, mathbf{color{blue}{3}}, mathbf{color{blue}{5}}, 6, mathbf{color{blue}{7}}, 8, 10, mathbf{color{blue}{11}}$$$ (five primes). Primes are marked bold and blue. In each of these cases, the number of produced primes is maximum possible.
1,200
true
true
false
false
false
true
false
false
false
false
4,969
1133C
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$. You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than $$$5$$$. Your task is to report the maximum possible number of students in a balanced team. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of students. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student. Output Print one integer — the maximum possible number of students in a balanced team. Examples Input 10 1337 1337 1337 1337 1337 1337 1337 1337 1337 1337 Input 6 1 1000 10000 10 100 1000000000 Note In the first example you can create a team with skills $$$[12, 17, 15]$$$. In the second example you can take all students in a team because their programming skills are equal. In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).
1,200
false
false
false
false
false
false
false
false
true
false
5,052
1766E
For a sequence of integers $$$[x_1, x_2, dots, x_k]$$$, let's define its decomposition as follows: Process the sequence from the first element to the last one, maintaining the list of its subsequences. When you process the element $$$x_i$$$, append it to the end of the first subsequence in the list such that the bitwise AND of its last element and $$$x_i$$$ is greater than $$$0$$$. If there is no such subsequence in the list, create a new subsequence with only one element $$$x_i$$$ and append it to the end of the list of subsequences. For example, let's analyze the decomposition of the sequence $$$[1, 3, 2, 0, 1, 3, 2, 1]$$$: processing element $$$1$$$, the list of subsequences is empty. There is no subsequence to append $$$1$$$ to, so we create a new subsequence $$$[1]$$$; processing element $$$3$$$, the list of subsequences is $$$[[1]]$$$. Since the bitwise AND of $$$3$$$ and $$$1$$$ is $$$1$$$, the element is appended to the first subsequence; processing element $$$2$$$, the list of subsequences is $$$[[1, 3]]$$$. Since the bitwise AND of $$$2$$$ and $$$3$$$ is $$$2$$$, the element is appended to the first subsequence; processing element $$$0$$$, the list of subsequences is $$$[[1, 3, 2]]$$$. There is no subsequence to append $$$0$$$ to, so we create a new subsequence $$$[0]$$$; processing element $$$1$$$, the list of subsequences is $$$[[1, 3, 2], [0]]$$$. There is no subsequence to append $$$1$$$ to, so we create a new subsequence $$$[1]$$$; processing element $$$3$$$, the list of subsequences is $$$[[1, 3, 2], [0], [1]]$$$. Since the bitwise AND of $$$3$$$ and $$$2$$$ is $$$2$$$, the element is appended to the first subsequence; processing element $$$2$$$, the list of subsequences is $$$[[1, 3, 2, 3], [0], [1]]$$$. Since the bitwise AND of $$$2$$$ and $$$3$$$ is $$$2$$$, the element is appended to the first subsequence; processing element $$$1$$$, the list of subsequences is $$$[[1, 3, 2, 3, 2], [0], [1]]$$$. The element $$$1$$$ cannot be appended to any of the first two subsequences, but can be appended to the third one. The resulting list of subsequences is $$$[[1, 3, 2, 3, 2], [0], [1, 1]]$$$. Let $$$f([x_1, x_2, dots, x_k])$$$ be the number of subsequences the sequence $$$[x_1, x_2, dots, x_k]$$$ is decomposed into. Now, for the problem itself. You are given a sequence $$$[a_1, a_2, dots, a_n]$$$, where each element is an integer from $$$0$$$ to $$$3$$$. Let $$$a[i..j]$$$ be the sequence $$$[a_i, a_{i+1}, dots, a_j]$$$. You have to calculate $$$sum limits_{i=1}^n sum limits_{j=i}^n f(a[i..j])$$$.
2,300
false
false
false
true
true
false
true
true
false
false
1,699
1506G
You are given a string $$$s$$$, consisting of lowercase Latin letters. While there is at least one character in the string $$$s$$$ that is repeated at least twice, you perform the following operation: you choose the index $$$i$$$ ($$$1 le i le s$$$) such that the character at position $$$i$$$ occurs at least two times in the string $$$s$$$, and delete the character at position $$$i$$$, that is, replace $$$s$$$ with $$$s_1 s_2 ldots s_{i-1} s_{i+1} s_{i+2} ldots s_n$$$. For example, if $$$s=$$$"codeforces", then you can apply the following sequence of operations: $$$i=6 Rightarrow s=$$$"codefrces"; $$$i=1 Rightarrow s=$$$"odefrces"; $$$i=7 Rightarrow s=$$$"odefrcs"; Given a given string $$$s$$$, find the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique. A string $$$a$$$ of length $$$n$$$ is lexicographically less than a string $$$b$$$ of length $$$m$$$, if: there is an index $$$i$$$ ($$$1 le i le min(n, m)$$$) such that the first $$$i-1$$$ characters of the strings $$$a$$$ and $$$b$$$ are the same, and the $$$i$$$-th character of the string $$$a$$$ is less than $$$i$$$-th character of string $$$b$$$; or the first $$$min(n, m)$$$ characters in the strings $$$a$$$ and $$$b$$$ are the same and $$$n < m$$$. For example, the string $$$a=$$$"aezakmi" is lexicographically less than the string $$$b=$$$"aezus". Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$). Then $$$t$$$ test cases follow. Each test case is characterized by a string $$$s$$$, consisting of lowercase Latin letters ($$$1 le s le 2 cdot 10^5$$$). It is guaranteed that the sum of the lengths of the strings in all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output the lexicographically maximum string that can be obtained after applying a certain sequence of operations after which all characters in the string become unique. Example Input 6 codeforces aezakmi abacaba convexhull swflldjgpaxs myneeocktxpqjpz Output odfrces ezakmi cba convexhul wfldjgpaxs myneocktxqjpz
2,000
false
true
false
true
true
false
true
false
false
false
3,137
1361A
Today Johnny wants to increase his contribution. His plan assumes writing $$$n$$$ blogs. One blog covers one topic, but one topic can be covered by many blogs. Moreover, some blogs have references to each other. Each pair of blogs that are connected by a reference has to cover different topics because otherwise, the readers can notice that they are split just for more contribution. Set of blogs and bidirectional references between some pairs of them is called blogs network. There are $$$n$$$ different topics, numbered from $$$1$$$ to $$$n$$$ sorted by Johnny's knowledge. The structure of the blogs network is already prepared. Now Johnny has to write the blogs in some order. He is lazy, so each time before writing a blog, he looks at it's already written neighbors (the blogs referenced to current one) and chooses the topic with the smallest number which is not covered by neighbors. It's easy to see that this strategy will always allow him to choose a topic because there are at most $$$n - 1$$$ neighbors. For example, if already written neighbors of the current blog have topics number $$$1$$$, $$$3$$$, $$$1$$$, $$$5$$$, and $$$2$$$, Johnny will choose the topic number $$$4$$$ for the current blog, because topics number $$$1$$$, $$$2$$$ and $$$3$$$ are already covered by neighbors and topic number $$$4$$$ isn't covered. As a good friend, you have done some research and predicted the best topic for each blog. Can you tell Johnny, in which order he has to write the blogs, so that his strategy produces the topic assignment chosen by you? Input The first line contains two integers $$$n$$$ $$$(1 leq n leq 5 cdot 10^5)$$$ and $$$m$$$ $$$(0 leq m leq 5 cdot 10^5)$$$xa0— the number of blogs and references, respectively. Each of the following $$$m$$$ lines contains two integers $$$a$$$ and $$$b$$$ ($$$a eq b$$$; $$$1 leq a, b leq n$$$), which mean that there is a reference between blogs $$$a$$$ and $$$b$$$. It's guaranteed that the graph doesn't contain multiple edges. The last line contains $$$n$$$ integers $$$t_1, t_2, ldots, t_n$$$, $$$i$$$-th of them denotes desired topic number of the $$$i$$$-th blog ($$$1 le t_i le n$$$). Output If the solution does not exist, then write $$$-1$$$. Otherwise, output $$$n$$$ distinct integers $$$p_1, p_2, ldots, p_n$$$ $$$(1 leq p_i leq n)$$$, which describe the numbers of blogs in order which Johnny should write them. If there are multiple answers, print any. Examples Input 3 3 1 2 2 3 3 1 2 1 3 Input 3 3 1 2 2 3 3 1 1 1 1 Input 5 3 1 2 2 3 4 5 2 1 2 2 1 Note In the first example, Johnny starts with writing blog number $$$2$$$, there are no already written neighbors yet, so it receives the first topic. Later he writes blog number $$$1$$$, it has reference to the already written second blog, so it receives the second topic. In the end, he writes blog number $$$3$$$, it has references to blogs number $$$1$$$ and $$$2$$$ so it receives the third topic. Second example: There does not exist any permutation fulfilling given conditions. Third example: First Johnny writes blog $$$2$$$, it receives the topic $$$1$$$. Then he writes blog $$$5$$$, it receives the topic $$$1$$$ too because it doesn't have reference to single already written blog $$$2$$$. Then he writes blog number $$$1$$$, it has reference to blog number $$$2$$$ with topic $$$1$$$, so it receives the topic $$$2$$$. Then he writes blog number $$$3$$$ which has reference to blog $$$2$$$, so it receives the topic $$$2$$$. Then he ends with writing blog number $$$4$$$ which has reference to blog $$$5$$$ and receives the topic $$$2$$$.
1,700
false
true
false
false
false
true
false
false
true
true
3,877
100A
Problem - 100A - Codeforces =============== xa0 . Output Write a single YES or NO. Write YES if and only if Sorush can carpet his room completely. Examples Input 10 4 6 Output YES Input 10 2 5 Output NO
1,100
false
false
true
false
false
false
false
false
false
false
9,489
1155F
The whole delivery market of Berland is controlled by two rival companies: BerEx and BerPS. They both provide fast and reliable delivery services across all the cities of Berland. The map of Berland can be represented as an undirected graph. The cities are vertices and the roads are edges between them. Each pair of cities has no more than one road between them. Each road connects different cities. BerEx and BerPS are so competitive that for each pair of cities $$$(v, u)$$$ they have set up their paths from $$$v$$$ to $$$u$$$ in such a way that these two paths don't share a single road. It is guaranteed that it was possible. Now Berland government decided to cut down the road maintenance cost by abandoning some roads. Obviously, they want to maintain as little roads as possible. However, they don't want to break the entire delivery system. So BerEx and BerPS should still be able to have their paths between every pair of cities non-intersecting. What is the minimal number of roads Berland government can maintain? More formally, given a 2-edge connected undirected graph, what is the minimum number of edges that can be left in it so that the resulting graph is also 2-edge connected? Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$3 le n le 14$$$, $$$n le m le frac{n(n - 1)}{2}$$$) — the number of cities and the number of roads between them. Each of the next $$$m$$$ lines contains two integers $$$v$$$ and $$$u$$$ ($$$1 le v, u le n$$$, $$$v e u$$$) — the cities connected by the next road. It is guaranteed that each pair of cities has no more than one road between them. It is guaranteed that each pair of cities have at least two paths between them that don't share a single road. Output The first line should contain a single integer $$$k$$$ — the minimum number of roads Berland government can maintain so that BerEx and BerPS are still able to have their paths between every pair of cities non-intersecting. The next $$$k$$$ lines should contain the list of roads which are being maintained. Each line of form "$$$v~u$$$", where $$$v$$$ and $$$u$$$ are cities connected by the next road. If there are multiple lists of minimum size, print any of them. The order of roads in the list doesn't matter. Examples Input 4 5 1 2 1 4 2 3 4 3 1 3 Input 6 10 1 2 2 3 3 1 3 4 4 5 5 6 4 6 2 5 1 6 3 5 Output 6 1 6 6 5 5 4 4 3 3 2 1 2 Note Here are graphs from the examples, red edges are the maintained ones.
2,800
false
false
false
true
false
false
true
false
false
true
4,931
1924B
There are $$$n$$$ points numbered $$$1$$$ to $$$n$$$ on a straight line. Initially, there are $$$m$$$ harbours. The $$$i$$$-th harbour is at point $$$X_i$$$ and has a value $$$V_i$$$. It is guaranteed that there are harbours at the points $$$1$$$ and $$$n$$$. There is exactly one ship on each of the $$$n$$$ points. The cost of moving a ship from its current location to the next harbour is the product of the value of the nearest harbour to its left and the distance from the nearest harbour to its right. Specifically, if a ship is already at a harbour, the cost of moving it to the next harbour is $$$0$$$. Additionally, there are $$$q$$$ queries, each of which is either of the following $$$2$$$ types: $$$1$$$ $$$x$$$ $$$v$$$ — Add a harbour at point $$$x$$$ with value $$$v$$$. It is guaranteed that before adding the harbour, there is no harbour at point $$$x$$$. $$$2$$$ $$$l$$$ $$$r$$$ — Print the sum of the cost of moving all ships at points from $$$l$$$ to $$$r$$$ to their next harbours. Note that you just need to calculate the cost of moving the ships but not actually move them. Input The first line contains three integers $$$n$$$, $$$m$$$, and $$$q$$$ ($$$2 le m le n le 3 cdot 10^5$$$, $$$1 le q le 3 cdot 10^5$$$) — the number of points, harbours, and queries, respectively. The second line contains $$$m$$$ distinct integers $$$X_1, X_2, ldots, X_m(1 le X_i le n)$$$ — the position at which the $$$i$$$-th harbour is located. The third line contains $$$m$$$ integers $$$V_1, V_2, ldots, V_m(1 le V_i le 10^7)$$$ — the value of the $$$i$$$-th harbour. Each of the next $$$q$$$ lines contains three integers. The first integer is $$$t$$$ ($$$1le t le 2$$$) — type of query. If $$$t=1$$$, then the next two integers are $$$x$$$ and $$$v$$$ ($$$2 le x le n - 1$$$, $$$1 le v le 10^7$$$) — first-type query. If $$$t=2$$$, then the next two integers are $$$l$$$ and $$$r$$$ ($$$1 le l le r le n$$$) — second-type query. It is guaranteed that there is at least one second-type query. Output For every second-type query, print one integer in a new line — answer to this query. Example Input 8 3 4 1 3 8 3 24 10 2 2 5 1 5 15 2 5 5 2 7 8 Note For the first type $$$2$$$ query, the cost for ships at positions $$$2$$$, $$$3$$$, $$$4$$$ and $$$5$$$ are $$$3(3 imes 1)$$$, $$$0$$$, $$$96(24 imes 4)$$$ and $$$72(24 imes 3)$$$ respectively. For the second type $$$2$$$ query, since the ship at position $$$5$$$ is already at a harbour, so the cost is $$$0$$$. For the third type $$$2$$$ query, the cost for ships at position $$$7$$$ and $$$8$$$ are $$$15(15 imes 1)$$$ and $$$0$$$ respectively.
2,100
true
false
true
false
true
false
false
false
true
false
745
1032E
You have a set of $$$n$$$ weights. You know that their masses are $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ grams, but you don't know which of them has which mass. You can't distinguish the weights. However, your friend does know the mass of each weight. You can ask your friend to give you exactly $$$k$$$ weights with the total mass $$$m$$$ (both parameters $$$k$$$ and $$$m$$$ are chosen by you), and your friend will point to any valid subset of weights, if it is possible. You are allowed to make this query only once. Find the maximum possible number of weights you can reveal after this query. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 100$$$)xa0— the number of weights. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 100$$$)xa0— the masses of the weights. Output Print the maximum number of weights you can learn the masses for after making a single query. Note In the first example we can ask for a subset of two weights with total mass being equal to $$$4$$$, and the only option is to get $$${2, 2}$$$. Another way to obtain the same result is to ask for a subset of two weights with the total mass of $$$5$$$ and get $$${1, 4}$$$. It is easy to see that the two remaining weights have mass of $$$2$$$ grams each. In the second example we can ask for a subset of two weights with total mass being $$$8$$$, and the only answer is $$${4, 4}$$$. We can prove it is not possible to learn masses for three weights in one query, but we won't put the proof here.
2,100
true
false
false
true
false
false
false
false
false
false
5,538
439A
Devu is a renowned classical singer. He is invited to many big functions/festivals. Recently he was invited to "All World Classical Singing Festival". Other than Devu, comedian Churu was also invited. Devu has provided organizers a list of the songs and required time for singing them. He will sing _n_ songs, _i__th_ song will take _t__i_ minutes exactly. The Comedian, Churu will crack jokes. All his jokes are of 5 minutes exactly. People have mainly come to listen Devu. But you know that he needs rest of 10 minutes after each song. On the other hand, Churu being a very active person, doesn't need any rest. You as one of the organizers should make an optimal sсhedule for the event. For some reasons you must follow the conditions: The duration of the event must be no more than _d_ minutes; Devu must complete all his songs; With satisfying the two previous conditions the number of jokes cracked by Churu should be as many as possible. If it is not possible to find a way to conduct all the songs of the Devu, output -1. Otherwise find out maximum number of jokes that Churu can crack in the grand event. Input The first line contains two space separated integers _n_, _d_ (1u2009≤u2009_n_u2009≤u2009100;xa01u2009≤u2009_d_u2009≤u200910000). The second line contains _n_ space-separated integers: _t_1,u2009_t_2,u2009...,u2009_t__n_ (1u2009≤u2009_t__i_u2009≤u2009100). Output If there is no way to conduct all the songs of Devu, output -1. Otherwise output the maximum number of jokes that Churu can crack in the grand event. Note Consider the first example. The duration of the event is 30 minutes. There could be maximum 5 jokes in the following way: First Churu cracks a joke in 5 minutes. Then Devu performs the first song for 2 minutes. Then Churu cracks 2 jokes in 10 minutes. Now Devu performs second song for 2 minutes. Then Churu cracks 2 jokes in 10 minutes. Now finally Devu will perform his last song in 1 minutes. Total time spent is 5u2009+u20092u2009+u200910u2009+u20092u2009+u200910u2009+u20091u2009=u200930 minutes. Consider the second example. There is no way of organizing Devu's all songs. Hence the answer is -1.
900
false
true
true
false
false
false
false
false
false
false
8,084
1764G1
The only difference between this problem and the other two versions is the maximum number of queries. In this version, you are allowed to ask at most $$$mathbf{30}$$$ queries. You can make hacks only if all versions of the problem are solved. This is an interactive problem. "Everybody! Doremy's Perfect Data Structure Class is about to start! Come and do your best if you want to have as much IQ as me!" In today's Data Structure class, Doremy is teaching everyone a powerful data structurexa0— Doremy tree! Now she gives you a quiz to prove that you are paying attention in class. Given an array $$$a$$$ of length $$$m$$$, Doremy tree supports the query $$$Q(l,r,k)$$$, where $$$1 leq l leq r leq m$$$ and $$$1 leq k leq m$$$, which returns the number of distinct integers in the array $$$left[lfloorfrac{a_l}{k} floor, lfloorfrac{a_{l+1}}{k} floor, ldots, lfloorfrac{a_r}{k} floor ight]$$$. Doremy has a secret permutation $$$p$$$ of integers from $$$1$$$ to $$$n$$$. You can make queries, in one query, you give $$$3$$$ integers $$$l,r,k$$$ ($$$1 leq l leq r leq n$$$, $$$1 leq k leq n$$$) and receive the value of $$$Q(l,r,k)$$$ for the array $$$p$$$. Can you find the index $$$y$$$ ($$$1 leq y leq n$$$) such that $$$p_y=1$$$ in at most $$$mathbf{30}$$$ queries? Note that the permutation $$$p$$$ is fixed before any queries are made. Interaction You begin the interaction by reading an integer $$$n$$$ ($$$3 le n le 1024$$$) in the first linexa0— the length of the permutation. To make a query, you should output "? $$$l r k$$$" ($$$1 leq l leq r leq n$$$, $$$1 leq k leq n$$$) in a separate line. After each query, you should read an integer $$$x$$$xa0— the value of $$$Q(l,r,k)$$$ for $$$p$$$. In this version of the problem, you can make at most $$$30$$$ such queries. To give the answer, you should output "! $$$y$$$" ($$$1 leq y leq n$$$) in a separate line, where $$$p_y=1$$$. After printing a query or the answer, do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hacks Format The first line of the hack contains an integer $$$n$$$ ($$$3 le n le 1024$$$)xa0— the length of the permutation. The second line of the hack contains $$$n$$$ distinct integers $$$p_1,p_2,ldots,p_n$$$ ($$$1 le p_ile n$$$)xa0— the permutation. Example Output ? 1 3 4 ? 3 5 3 ? 3 4 5 ? 3 5 2 ! 4 Note The permutation in the example is $$$[3,5,2,1,4]$$$. The input and output for example illustrate possible interaction on that test (empty lines are inserted only for clarity). In this interaction process: For the first query, $$$lfloorfrac{3}{4} floor=0,lfloorfrac{5}{4} floor=1,lfloorfrac{2}{4} floor=0$$$, so the answer is $$$2$$$. For the second query, $$$lfloorfrac{2}{3} floor=0,lfloorfrac{1}{3} floor=0,lfloorfrac{4}{3} floor=1$$$, so the answer is still $$$2$$$. For the third query, $$$lfloorfrac{2}{5} floor=0,lfloorfrac{1}{5} floor=0$$$, so the answer is $$$1$$$. For the fourth query, $$$lfloorfrac{2}{2} floor=1,lfloorfrac{1}{2} floor=0,lfloorfrac{4}{2} floor=2$$$, so the answer is $$$3$$$. The correct answer is got after $$$4$$$ queries, so this process will be judged correct.
2,900
false
false
false
false
false
false
false
true
false
false
1,721
1236B
Alice got many presents these days. So she decided to pack them into boxes and send them to her friends. There are $$$n$$$ kinds of presents. Presents of one kind are identical (i.e. there is no way to distinguish two gifts of the same kind). Presents of different kinds are different (i.e. that is, two gifts of different kinds are distinguishable). The number of presents of each kind, that Alice has is very big, so we can consider Alice has an infinite number of gifts of each kind. Also, there are $$$m$$$ boxes. All of them are for different people, so they are pairwise distinct (consider that the names of $$$m$$$ friends are written on the boxes). For example, putting the first kind of present into the first box but not into the second box, is different from putting the first kind of present into the second box but not into the first box. Alice wants to pack presents with the following rules: She won't pack more than one present of each kind into the same box, so each box should contain presents of different kinds (i.e. each box contains a subset of $$$n$$$ kinds, empty boxes are allowed); For each kind at least one present should be packed into some box. Now Alice wants to know how many different ways to pack the presents exists. Please, help her and calculate this number. Since the answer can be huge, output it by modulo $$$10^9+7$$$. See examples and their notes for clarification. Input The first line contains two integers $$$n$$$ and $$$m$$$, separated by spaces ($$$1 leq n,m leq 10^9$$$)xa0— the number of kinds of presents and the number of boxes that Alice has. Note In the first example, there are seven ways to pack presents: $$${1}{}{}$$$ $$${}{1}{}$$$ $$${}{}{1}$$$ $$${1}{1}{}$$$ $$${}{1}{1}$$$ $$${1}{}{1}$$$ $$${1}{1}{1}$$$ In the second example there are nine ways to pack presents: $$${}{1,2}$$$ $$${1}{2}$$$ $$${1}{1,2}$$$ $$${2}{1}$$$ $$${2}{1,2}$$$ $$${1,2}{}$$$ $$${1,2}{1}$$$ $$${1,2}{2}$$$ $$${1,2}{1,2}$$$ For example, the way $$${2}{2}$$$ is wrong, because presents of the first kind should be used in the least one box.
1,500
true
false
false
false
false
false
false
false
false
false
4,517
1783E
Monocarp and Polycarp are playing a computer game. This game features $$$n$$$ bosses for the playing to kill, numbered from $$$1$$$ to $$$n$$$. They will fight each boss the following way: Monocarp makes $$$k$$$ attempts to kill the boss; Polycarp makes $$$k$$$ attempts to kill the boss; Monocarp makes $$$k$$$ attempts to kill the boss; Polycarp makes $$$k$$$ attempts to kill the boss; ... Monocarp kills the $$$i$$$-th boss on his $$$a_i$$$-th attempt. Polycarp kills the $$$i$$$-th boss on his $$$b_i$$$-th attempt. After one of them kills the $$$i$$$-th boss, they move on to the $$$(i+1)$$$-st boss. The attempt counters reset for both of them. Once one of them kills the $$$n$$$-th boss, the game ends. Find all values of $$$k$$$ from $$$1$$$ to $$$n$$$ such that Monocarp kills all bosses. 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 single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the number of bosses. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the index of attempt Monocarp kills each boss on. The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le n$$$)xa0— the index of attempt Polycarp kills each boss on. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print two lines. The first line should contain a single integer $$$mathit{cnt}$$$xa0— the number of values of $$$k$$$ from $$$1$$$ to $$$n$$$ such that Monocarp kills all bosses. The second line should contain $$$mathit{cnt}$$$ distinct integersxa0— the values of $$$k$$$ themselves. Example Input 3 3 1 1 1 2 3 1 1 1 1 4 1 4 3 2 3 3 4 1 Note Consider the last testcase of the example. Let $$$k = 1$$$. First, Monocarp makes one attempt to kill the first boss. It's successful, since $$$a_1 = 1$$$. Then, Monocarp makes one attempt to kill the second boss. It's unsuccessful, since $$$a_2 > 1$$$. So, Polycarp makes an attempt then. It's also unsuccessful, since $$$b_2 > 1$$$. Then, Monocarp makes another attempt. It's still unsuccessful, since $$$a_2 > 2$$$. This goes on until Polycarp finally kills the boss on his third attempt. Monocarp didn't kill this boss, thus, $$$k = 1$$$ isn't the answer. Let $$$k = 2$$$. Monocarp still kills the first boss on his first attempt. Then, he makes two unsuccessful attempts for the second boss. Then, Polycarp makes two unsuccessful attempts. Then, Monocarp makes two more attempts and kills the boss on his fourth attempt. The third boss is similar. First, two unsuccessful attempts by Monocarp. Then, two unsuccessful attempts by Polycarp. Then, Monocarp has two more attempts, but even his first one is successful, since $$$a_3 = 3$$$. The fourth boss is also killed by Monocarp. Thus, $$$k = 2$$$ is the answer.
2,300
true
false
false
false
true
false
true
false
false
false
1,575
1670E
After the last regional contest, Hemose and his teammates finally qualified to the ICPC World Finals, so for this great achievement and his love of trees, he gave you this problem as the name of his team "Hemose 3al shagra" (Hemose on the tree). You are given a tree of $$$n$$$ vertices where $$$n$$$ is a power of $$$2$$$. You have to give each node and edge an integer value in the range $$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 5cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$p$$$ ($$$1 le p le 17$$$), where $$$n$$$ (the number of vertices in the tree) is equal to $$$2^p$$$. Each of the next $$$n−1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u,v le n$$$) meaning that there is an edge between the vertices $$$u$$$ and $$$v$$$ in the tree. It is guaranteed that the given graph is a tree. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3cdot 10^5$$$. Output For each test case on the first line print the chosen root. On the second line, print $$$n$$$ integers separated by spaces, where the $$$i$$$-th integer represents the chosen value for the $$$i$$$-th node. On the third line, print $$$n-1$$$ integers separated by spaces, where the $$$i$$$-th integer represents the chosen value for the $$$i$$$-th edge. The edges are numerated in the order of their appearance in the input data. If there are multiple solutions, you may output any. Example Input 2 2 1 2 2 3 3 4 3 1 2 2 3 3 4 1 5 1 6 5 7 5 8 Output 3 5 1 3 6 4 2 7 5 1 2 8 11 4 13 9 15 6 14 3 7 10 5 12 Note The tree in the first test case with the weights of all nodes and edges is shown in the picture. The costs of all paths are: $$$3$$$; $$$3oplus 7=4$$$; $$$3oplus 7oplus 6=2$$$; $$$3oplus 2=1$$$; $$$3oplus 2oplus 1=0$$$; $$$3oplus 2oplus 1oplus 4=4$$$; $$$3oplus 2oplus 1oplus 4oplus 5=1$$$. The maximum cost of all these paths is $$$4$$$. We can show that it is impossible to assign the values and choose the root differently to achieve a smaller maximum cost of all paths. The tree in the second test case:
2,200
false
false
false
false
false
true
false
false
false
false
2,253
1731B
Demodogs from the Upside-down have attacked Hawkins again. El wants to reach Mike and also kill as many Demodogs in the way as possible. Hawkins can be represented as an $$$n imes n$$$ grid. The number of Demodogs in a cell at the $$$i$$$-th row and the $$$j$$$-th column is $$$i cdot j$$$. El is at position $$$(1, 1)$$$ of the grid, and she has to reach $$$(n, n)$$$ where she can find Mike. The only directions she can move are the right (from $$$(i, j)$$$ to $$$(i, j + 1)$$$) and the down (from $$$(i, j)$$$ to $$$(i + 1, j)$$$). She can't go out of the grid, as there are doors to the Upside-down at the boundaries. Calculate the maximum possible number of Demodogs $$$mathrm{ans}$$$ she can kill on the way, considering that she kills all Demodogs in cells she visits (including starting and finishing cells). Print $$$2022 cdot mathrm{ans}$$$ modulo $$$10^9 + 7$$$. Modulo $$$10^9 + 7$$$ because the result can be too large and multiplied by $$$2022$$$ because we are never gonna see it again! (Note, you firstly multiply by $$$2022$$$ and only after that take the remainder.) Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 10^4$$$). Description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 leq n leq 10^9$$$)xa0— the size of the grid. Output For each test case, print a single integerxa0— the maximum number of Demodogs that can be killed multiplied by $$$2022$$$, modulo $$$10^9 + 7$$$. Note In the first test case, for any path chosen by her the number of Demodogs to be killed would be $$$7$$$, so the answer would be $$$2022 cdot 7 = 14154$$$.
1,100
true
true
false
false
false
false
false
false
false
false
1,904
1677E
Tokitsukaze has a permutation $$$p$$$ of length $$$n$$$. Let's call a segment $$$[l,r]$$$ beautiful if there exist $$$i$$$ and $$$j$$$ satisfying $$$p_i cdot p_j = max{p_l, p_{l+1}, ldots, p_r }$$$, where $$$l leq i < j leq r$$$. Now Tokitsukaze has $$$q$$$ queries, in the $$$i$$$-th query she wants to know how many beautiful subsegments $$$[x,y]$$$ there are in the segment $$$[l_i,r_i]$$$ (i.xa0e. $$$l_i leq x leq y leq r_i$$$). Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1leq n leq 2 cdot 10^5$$$; $$$1 leq q leq 10^6$$$)xa0— the length of permutation $$$p$$$ and the number of queries. The second line contains $$$n$$$ distinct integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 leq p_i leq n$$$)xa0— the permutation $$$p$$$. Each of the next $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 leq l_i leq r_i leq n$$$)xa0— the segment $$$[l_i,r_i]$$$ of this query. Output For each query, print one integerxa0— the numbers of beautiful subsegments in the segment $$$[l_i,r_i]$$$. Examples Input 8 3 1 3 5 2 4 7 6 8 1 3 1 1 1 8 Input 10 10 6 1 3 2 5 8 4 10 7 9 1 8 1 10 1 2 1 4 2 4 5 8 4 10 4 7 8 10 5 9 Output 17 25 1 5 2 0 4 1 0 0 Note In the first example, for the first query, there are $$$2$$$ beautiful subsegmentsxa0— $$$[1,2]$$$ and $$$[1,3]$$$.
2,900
false
false
false
false
true
false
false
false
false
false
2,202
1068C
Ivan is a novice painter. He has $$$n$$$ dyes of different colors. He also knows exactly $$$m$$$ pairs of colors which harmonize with each other. Ivan also enjoy playing chess. He has $$$5000$$$ rooks. He wants to take $$$k$$$ rooks, paint each of them in one of $$$n$$$ colors and then place this $$$k$$$ rooks on a chessboard of size $$$10^{9} imes 10^{9}$$$. Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal. Ivan wants his arrangement of rooks to have following properties: For any color there is a rook of this color on a board; For any color the set of rooks of this color is connected; For any two different colors $$$a$$$ $$$b$$$ union of set of rooks of color $$$a$$$ and set of rooks of color $$$b$$$ is connected if and only if this two colors harmonize with each other. Please help Ivan find such an arrangement. Input The first line of input contains $$$2$$$ integers $$$n$$$, $$$m$$$ ($$$1 le n le 100$$$, $$$0 le m le min(1000, ,, frac{n(n-1)}{2})$$$)xa0— number of colors and number of pairs of colors which harmonize with each other. In next $$$m$$$ lines pairs of colors which harmonize with each other are listed. Colors are numbered from $$$1$$$ to $$$n$$$. It is guaranteed that no pair occurs twice in this list. Output Print $$$n$$$ blocks, $$$i$$$-th of them describes rooks of $$$i$$$-th color. In the first line of block print one number $$$a_{i}$$$ ($$$1 le a_{i} le 5000$$$)xa0— number of rooks of color $$$i$$$. In each of next $$$a_{i}$$$ lines print two integers $$$x$$$ and $$$y$$$ ($$$1 le x, ,, y le 10^{9}$$$)xa0— coordinates of the next rook. All rooks must be on different cells. Total number of rooks must not exceed $$$5000$$$. It is guaranteed that the solution exists. Examples Output 2 3 4 1 4 4 1 2 2 2 2 4 5 4 1 5 1 Note Rooks arrangements for all three examples (red is color $$$1$$$, green is color $$$2$$$ and blue is color $$$3$$$).
1,700
false
false
false
false
false
true
false
false
false
true
5,368
1214C
Petya's friends made him a birthday present — a bracket sequence. Petya was quite disappointed with his gift, because he dreamed of correct bracket sequence, yet he told his friends nothing about his dreams and decided to fix present himself. To make everything right, Petya is going to move at most one bracket from its original place in the sequence to any other position. Reversing the bracket (e.g. turning "(" into ")" or vice versa) isn't allowed. We remind that bracket sequence $$$s$$$ is called correct if: $$$s$$$ is empty; $$$s$$$ is equal to "($$$t$$$)", where $$$t$$$ is correct bracket sequence; $$$s$$$ is equal to $$$t_1 t_2$$$, i.e. concatenation of $$$t_1$$$ and $$$t_2$$$, where $$$t_1$$$ and $$$t_2$$$ are correct bracket sequences. For example, "(()())", "()" are correct, while ")(" and "())" are not. Help Petya to fix his birthday present and understand whether he can move one bracket so that the sequence becomes correct. Input First of line of input contains a single number $$$n$$$ ($$$1 leq n leq 200,000$$$)xa0— length of the sequence which Petya received for his birthday. Second line of the input contains bracket sequence of length $$$n$$$, containing symbols "(" and ")". Output Print "Yes" if Petya can make his sequence correct moving at most one bracket. Otherwise print "No". Note In the first example, Petya can move first bracket to the end, thus turning the sequence into "()", which is correct bracket sequence. In the second example, there is no way to move at most one bracket so that the sequence becomes correct. In the third example, the sequence is already correct and there's no need to move brackets.
1,200
false
true
false
false
true
false
false
false
false
false
4,609
1805C
The universe is a coordinate plane. There are $$$n$$$ space highways, each of which is a straight line $$$y=kx$$$ passing through the origin $$$(0, 0)$$$. Also, there are $$$m$$$ asteroid belts on the plane, which we represent as open upwards parabolas, i.xa0e. graphs of functions $$$y=ax^2+bx+c$$$, where $$$a > 0$$$. You want to photograph each parabola. To do this, for each parabola you need to choose a line that does not intersect this parabola and does not touch it. You can select the same line for different parabolas. Please find such a line for each parabola, or determine that there is no such line. 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 $$$2$$$ integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 10^5$$$)xa0—the number of lines and parabolas, respectively. Each of the next $$$n$$$ lines contains one integer $$$k$$$ ($$$k le 10^8$$$), denoting a line that is described with the equation $$$y=kx$$$. The lines are not necessarily distinct, $$$k$$$ can be equal to $$$0$$$. Each of the next $$$m$$$ lines contains $$$3$$$ integers $$$a$$$, $$$b$$$, and $$$c$$$ ($$$a, b, c le 10^8$$$, $$$a > 0$$$)xa0— coefficients of equations of the parabolas $$$ax^2+bx+c$$$. The parabolas are not necessarily distinct. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$10^5$$$, and the sum $$$m$$$ over all test cases also does not exceed $$$10^5$$$. Output For each test case, output the answers for each parabola in the given order. If there is a line that does not intersect the given parabola and doesn't touch it, print on a separate line the word "YES", and then on a separate line the number $$$k$$$xa0— the coefficient of this line. If there are several answers, print any of them. If the line does not exist, print one word "NO". You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. The empty lines in the output in the example are given only for illustration, you do not need to output them (but you can). Example Input 5 1 2 1 1 -1 2 1 -1 3 2 2 1 4 1 2 1 2 5 1 1 1 0 1 0 0 1 1 100000000 100000000 100000000 100000000 2 3 0 2 2 2 1 1 -2 1 1 -2 -1 Output YES 1 YES 1 YES 1 YES 4 NO YES 100000000 YES 0 NO NO Note In the first test case, both parabolas do not intersect the only given line $$$y=1cdot x$$$, so the answer is two numbers $$$1$$$. In the second test case, the line $$$y=x$$$ and the parabola $$$2x^2+5x+1$$$ intersect, and also the line $$$y=4x$$$ and the parabola $$$x^2+2x+1$$$ touch, so these pairs do not satisfy the condition. So for the first parabola, the answer is $$$1$$$ ($$$y=1x$$$), and for the second parabolaxa0— $$$4$$$. In the third test set, the line and the parabola intersect, so the answer is "NO".
1,400
true
false
false
false
true
false
false
true
false
false
1,447
666B
A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wide. But not everyone should have the opportunity to see works of sculptor, shouldn't he? Otherwise there will be no any exclusivity. So Cicasso will entirely hold the world tour in his native country — Berland. Cicasso is very devoted to his work and he wants to be distracted as little as possible. Therefore he will visit only four cities. These cities will be different, so no one could think that he has "favourites". Of course, to save money, he will chose the shortest paths between these cities. But as you have probably guessed, Cicasso is a weird person. Although he doesn't like to organize exhibitions, he likes to travel around the country and enjoy its scenery. So he wants the total distance which he will travel to be as large as possible. However, the sculptor is bad in planning, so he asks you for help. There are _n_ cities and _m_ one-way roads in Berland. You have to choose four different cities, which Cicasso will visit and also determine the order in which he will visit them. So that the total distance he will travel, if he visits cities in your order, starting from the first city in your list, and ending in the last, choosing each time the shortest route between a pair of cities — will be the largest. Note that intermediate routes may pass through the cities, which are assigned to the tour, as well as pass twice through the same city. For example, the tour can look like that: . Four cities in the order of visiting marked as overlines: [1,u20095,u20092,u20094]. Note that Berland is a high-tech country. So using nanotechnologies all roads were altered so that they have the same length. For the same reason moving using regular cars is not very popular in the country, and it can happen that there are such pairs of cities, one of which generally can not be reached by car from the other one. However, Cicasso is very conservative and cannot travel without the car. Choose cities so that the sculptor can make the tour using only the automobile. It is guaranteed that it is always possible to do. Input In the first line there is a pair of integers _n_ and _m_ (4u2009≤u2009_n_u2009≤u20093000,u20093u2009≤u2009_m_u2009≤u20095000) — a number of cities and one-way roads in Berland. Each of the next _m_ lines contains a pair of integers _u__i_,u2009_v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_) — a one-way road from the city _u__i_ to the city _v__i_. Note that _u__i_ and _v__i_ are not required to be distinct. Moreover, it can be several one-way roads between the same pair of cities. Note Let _d_(_x_,u2009_y_) be the shortest distance between cities _x_ and _y_. Then in the example _d_(2,u20091)u2009=u20093,u2009_d_(1,u20098)u2009=u20097,u2009_d_(8,u20097)u2009=u20093. The total distance equals 13.
2,000
false
false
false
false
false
false
false
false
false
true
7,154
1265B
You are given a permutation $$$p=[p_1, p_2, ldots, p_n]$$$ of integers from $$$1$$$ to $$$n$$$. Let's call the number $$$m$$$ ($$$1 le m le n$$$) beautiful, if there exists two indices $$$l, r$$$ ($$$1 le l le r le n$$$), such that the numbers $$$[p_l, p_{l+1}, ldots, p_r]$$$ is a permutation of numbers $$$1, 2, ldots, m$$$. For example, let $$$p = [4, 5, 1, 3, 2, 6]$$$. In this case, the numbers $$$1, 3, 5, 6$$$ are beautiful and $$$2, 4$$$ are not. It is because: if $$$l = 3$$$ and $$$r = 3$$$ we will have a permutation $$$[1]$$$ for $$$m = 1$$$; if $$$l = 3$$$ and $$$r = 5$$$ we will have a permutation $$$[1, 3, 2]$$$ for $$$m = 3$$$; if $$$l = 1$$$ and $$$r = 5$$$ we will have a permutation $$$[4, 5, 1, 3, 2]$$$ for $$$m = 5$$$; if $$$l = 1$$$ and $$$r = 6$$$ we will have a permutation $$$[4, 5, 1, 3, 2, 6]$$$ for $$$m = 6$$$; it is impossible to take some $$$l$$$ and $$$r$$$, such that $$$[p_l, p_{l+1}, ldots, p_r]$$$ is a permutation of numbers $$$1, 2, ldots, m$$$ for $$$m = 2$$$ and for $$$m = 4$$$. You are given a permutation $$$p=[p_1, p_2, ldots, p_n]$$$. For all $$$m$$$ ($$$1 le m le n$$$) determine if it is a beautiful number or not. Input The first line contains the only integer $$$t$$$ ($$$1 le t le 1000$$$) xa0— the number of test cases in the input. The next lines contain the description of test cases. The first line of a test case contains a number $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the length of the given permutation $$$p$$$. The next line contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$, all $$$p_i$$$ are different)xa0— the given permutation $$$p$$$. It is guaranteed, that the sum of $$$n$$$ from all test cases in the input doesn't exceed $$$2 cdot 10^5$$$. Output Print $$$t$$$ linesxa0— the answers to test cases in the order they are given in the input. The answer to a test case is the string of length $$$n$$$, there the $$$i$$$-th character is equal to $$$1$$$ if $$$i$$$ is a beautiful number and is equal to $$$0$$$ if $$$i$$$ is not a beautiful number.
1,300
true
false
true
false
true
false
false
false
false
false
4,373
513E1
You are given an array of length _n_ and a number _k_. Let's pick _k_ non-overlapping non-empty subarrays of the initial array. Let _s__i_ be the sum of the _i_-th subarray in order from left to right. Compute the maximum value of the following expression: _s_1u2009-u2009_s_2u2009+u2009_s_2u2009-u2009_s_3u2009+u2009...u2009+u2009_s__k_u2009-u20091u2009-u2009_s__k_ Here subarray is a contiguous part of an array. Input The first line of input contains two integers _n_ and _k_. The second line contains _n_ integers — the elements of the array. The absolute values of elements do not exceed 104. The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. In subproblem E1 (9 points), constraints 2u2009≤u2009_n_u2009≤u2009400, 2u2009≤u2009_k_u2009≤u2009_min_(_n_,u200950) will hold. In subproblem E2 (12 points), constraints 2u2009≤u2009_n_u2009≤u200930000, 2u2009≤u2009_k_u2009≤u2009_min_(_n_,u2009200) will hold. Output Output a single integer — the maximum possible value. Note Consider the first sample test. The optimal solution is obtained if the first subarray contains the first element only, the second subarray spans the next three elements and the last subarray contains the last element only. The sums of these subarrays are 5, 9 and 1, correspondingly. Consider the second sample test. In the optimal solution, the first subarray consists of the first two elements and the second subarray consists of the third element only. Note that the last element does not belong to any subarray in this solution.
2,600
false
false
false
true
false
false
false
false
false
false
7,787
1223C
You are an environmental activist at heart but the reality is harsh and you are just a cashier in a cinema. But you can still do something! You have $$$n$$$ tickets to sell. The price of the $$$i$$$-th ticket is $$$p_i$$$. As a teller, you have a possibility to select the order in which the tickets will be sold (i.e. a permutation of the tickets). You know that the cinema participates in two ecological restoration programs applying them to the order you chose: The $$$x%$$$ of the price of each the $$$a$$$-th sold ticket ($$$a$$$-th, $$$2a$$$-th, $$$3a$$$-th and so on) in the order you chose is aimed for research and spreading of renewable energy sources. The $$$y%$$$ of the price of each the $$$b$$$-th sold ticket ($$$b$$$-th, $$$2b$$$-th, $$$3b$$$-th and so on) in the order you chose is aimed for pollution abatement. If the ticket is in both programs then the $$$(x + y) %$$$ are used for environmental activities. Also, it's known that all prices are multiples of $$$100$$$, so there is no need in any rounding. For example, if you'd like to sell tickets with prices $$$[400, 100, 300, 200]$$$ and the cinema pays $$$10%$$$ of each $$$2$$$-nd sold ticket and $$$20%$$$ of each $$$3$$$-rd sold ticket, then arranging them in order $$$[100, 200, 300, 400]$$$ will lead to contribution equal to $$$100 cdot 0 + 200 cdot 0.1 + 300 cdot 0.2 + 400 cdot 0.1 = 120$$$. But arranging them in order $$$[100, 300, 400, 200]$$$ will lead to $$$100 cdot 0 + 300 cdot 0.1 + 400 cdot 0.2 + 200 cdot 0.1 = 130$$$. Nature can't wait, so you decided to change the order of tickets in such a way, so that the total contribution to programs will reach at least $$$k$$$ in minimum number of sold tickets. Or say that it's impossible to do so. In other words, find the minimum number of tickets which are needed to be sold in order to earn at least $$$k$$$. Input The first line contains a single integer $$$q$$$ ($$$1 le q le 100$$$)xa0— the number of independent queries. Each query consists of $$$5$$$ lines. The first line of each query contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the number of tickets. The second line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$100 le p_i le 10^9$$$, $$$p_i bmod 100 = 0$$$)xa0— the corresponding prices of tickets. The third line contains two integers $$$x$$$ and $$$a$$$ ($$$1 le x le 100$$$, $$$x + y le 100$$$, $$$1 le a le n$$$)xa0— the parameters of the first program. The fourth line contains two integers $$$y$$$ and $$$b$$$ ($$$1 le y le 100$$$, $$$x + y le 100$$$, $$$1 le b le n$$$)xa0— the parameters of the second program. The fifth line contains single integer $$$k$$$ ($$$1 le k le 10^{14}$$$)xa0— the required total contribution. It's guaranteed that the total number of tickets per test doesn't exceed $$$2 cdot 10^5$$$. Output Print $$$q$$$ integersxa0— one per query. For each query, print the minimum number of tickets you need to sell to make the total ecological contribution of at least $$$k$$$ if you can sell tickets in any order. If the total contribution can not be achieved selling all the tickets, print $$$-1$$$. Example Input 4 1 100 50 1 49 1 100 8 100 200 100 200 100 200 100 100 10 2 15 3 107 3 1000000000 1000000000 1000000000 50 1 50 1 3000000000 5 200 100 100 100 100 69 5 31 2 90 Note In the first query the total contribution is equal to $$$50 + 49 = 99 < 100$$$, so it's impossible to gather enough money. In the second query you can rearrange tickets in a following way: $$$[100, 100, 200, 200, 100, 200, 100, 100]$$$ and the total contribution from the first $$$6$$$ tickets is equal to $$$100 cdot 0 + 100 cdot 0.1 + 200 cdot 0.15 + 200 cdot 0.1 + 100 cdot 0 + 200 cdot 0.25 = 10 + 30 + 20 + 50 = 110$$$. In the third query the full price of each ticket goes to the environmental activities. In the fourth query you can rearrange tickets as $$$[100, 200, 100, 100, 100]$$$ and the total contribution from the first $$$4$$$ tickets is $$$100 cdot 0 + 200 cdot 0.31 + 100 cdot 0 + 100 cdot 0.31 = 62 + 31 = 93$$$.
1,600
false
true
false
false
false
false
false
true
false
false
4,557
1345B
A card pyramid of height $$$1$$$ is constructed by resting two cards against each other. For $$$h>1$$$, a card pyramid of height $$$h$$$ is constructed by placing a card pyramid of height $$$h-1$$$ onto a base. A base consists of $$$h$$$ pyramids of height $$$1$$$, and $$$h-1$$$ cards on top. For example, card pyramids of heights $$$1$$$, $$$2$$$, and $$$3$$$ look as follows: You start with $$$n$$$ cards and build the tallest pyramid that you can. If there are some cards remaining, you build the tallest pyramid possible with the remaining cards. You repeat this process until it is impossible to build another pyramid. In the end, how many pyramids will you have constructed? Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 1000$$$)xa0— the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. Each test case contains a single integer $$$n$$$ ($$$1le nle 10^9$$$)xa0— the number of cards. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^9$$$. Output For each test case output a single integerxa0— the number of pyramids you will have constructed in the end. Note In the first test, you construct a pyramid of height $$$1$$$ with $$$2$$$ cards. There is $$$1$$$ card remaining, which is not enough to build a pyramid. In the second test, you build two pyramids, each of height $$$2$$$, with no cards remaining. In the third test, you build one pyramid of height $$$3$$$, with no cards remaining. In the fourth test, you build one pyramid of height $$$3$$$ with $$$9$$$ cards remaining. Then you build a pyramid of height $$$2$$$ with $$$2$$$ cards remaining. Then you build a final pyramid of height $$$1$$$ with no cards remaining. In the fifth test, one card is not enough to build any pyramids.
1,100
true
false
false
true
false
false
true
true
false
false
3,980
1146H
Problem - 1146H - Codeforces =============== xa0 ]( "Forethought Future Cup") xa0— the number of points. Each of the next $$$n$$$ lines contains two integers $$$x_i, y_i$$$ ($$$-10^6 leq x_i,y_i leq 10^6$$$)xa0— the coordinates of the $$$i$$$-th point. It is guaranteed that no three points are collinear. Output Print a single integer, the number of sets of $$$5$$$ points that form a pentagram. Examples Input 5 0 0 0 2 2 0 2 2 1 3 Output 1 Input 5 0 0 4 0 0 4 4 4 2 3 Output 0 Input 10 841746 527518 595261 331297 -946901 129987 670374 -140388 -684770 309555 -302589 415564 -387435 613331 -624940 -95922 945847 -199224 24636 -565799 Output 85 Note A picture of the first sample: A picture of the second sample: A picture of the third sample:
2,900
false
false
false
true
false
false
false
false
false
false
4,984
386D
You are playing the following game. There are _n_ points on a plane. They are the vertices of a regular _n_-polygon. Points are labeled with integer numbers from 1 to _n_. Each pair of distinct points is connected by a diagonal, which is colored in one of 26 colors. Points are denoted by lowercase English letters. There are three stones positioned on three distinct vertices. All stones are the same. With one move you can move the stone to another free vertex along some diagonal. The color of this diagonal must be the same as the color of the diagonal, connecting another two stones. Your goal is to move stones in such way that the only vertices occupied by stones are 1, 2 and 3. You must achieve such position using minimal number of moves. Write a program which plays this game in an optimal way. Input In the first line there is one integer _n_ (3u2009≤u2009_n_u2009≤u200970) — the number of points. In the second line there are three space-separated integer from 1 to _n_ — numbers of vertices, where stones are initially located. Each of the following _n_ lines contains _n_ symbols — the matrix denoting the colors of the diagonals. Colors are denoted by lowercase English letters. The symbol _j_ of line _i_ denotes the color of diagonal between points _i_ and _j_. Matrix is symmetric, so _j_-th symbol of _i_-th line is equal to _i_-th symbol of _j_-th line. Main diagonal is filled with '*' symbols because there is no diagonal, connecting point to itself. Output If there is no way to put stones on vertices 1, 2 and 3, print -1 on a single line. Otherwise, on the first line print minimal required number of moves and in the next lines print the description of each move, one move per line. To describe a move print two integers. The point from which to remove the stone, and the point to which move the stone. If there are several optimal solutions, print any of them. Examples Input 4 2 3 4 *aba a*ab ba*b abb Input 4 2 3 4 *abc a*ab ba*b cbb Note In the first example we can move stone from point 4 to point 1 because this points are connected by the diagonal of color 'a' and the diagonal connection point 2 and 3, where the other stones are located, are connected by the diagonal of the same color. After that stones will be on the points 1, 2 and 3.
2,100
false
false
true
true
false
false
false
false
false
true
8,283
1184A1
Problem - 1184A1 - 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 number theory *1200 No tag edit access → Contest materials time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor. Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques. The first hashing function she designed is as follows. Given two positive integers $$$(x, y)$$$ she defines $$$H(x,y):=x^2+2xy+x+1$$$. Now, Heidi wonders if the function is reversible. That is, given a positive integer $$$r$$$, can you find a pair $$$(x, y)$$$ (of positive integers) such that $$$H(x, y) = r$$$? If multiple such pairs exist, output the one with smallest possible $$$x$$$. If there is no such pair, output "NO". Input The first and only line contains an integer $$$r$$$ ($$$1 le r le 10^{12}$$$). Output Output integers $$$x, y$$$ such that $$$H(x,y) = r$$$ and $$$x$$$ is smallest possible, or "NO" if no such pair exists. Examples Input 19 Output 1 8 Input 16 Output NO
1,200
true
false
false
false
false
false
true
false
false
false
4,794
1765I
The black king lives on a chess board with an infinite number of columns (files) and $$$8$$$ rows (ranks). The columns are numbered with all integer numbers (including negative). The rows are numbered from $$$1$$$ toxa0$$$8$$$. Initially, the black king is located on the starting square $$$(x_s, y_s)$$$, and he needs to reach some target square $$$(x_t, y_t)$$$. Unfortunately, there are also white pieces on the board, and they threaten the black king. After negotiations, the white pieces agreed to let the black king pass to the target square on the following conditions: each turn, the black king makes a move according to the movement rules; the black king cannot move to a square occupied by a white piece; the black king cannot move to a square which is under attack by any white piece. A square is under attack if a white piece can reach it in one move according to the movement rules; the white pieces never move. Help the black king find the minimum number of moves needed to reach the target square while not violating the conditions. The black king cannot leave the board at any time. The black king moves according to the movement rules below. Even though the white pieces never move, squares which they can reach in one move are considered to be under attack, so the black king cannot move into those squares. Below are the movement rules. Note that the pieces (except for the knight) cannot jump over other pieces. a king moves exactly one square horizontally, vertically, or diagonally. a rook moves any number of vacant squares horizontally or vertically. a bishop moves any number of vacant squares diagonally. a queen moves any number of vacant squares horizontally, vertically, or diagonally. a knight moves to one of the nearest squares not on the same rank, file, or diagonal (this can be thought of as moving two squares horizontally then one square vertically, or moving one square horizontally then two squares vertically — i.u2009e. in an "L" pattern). Knights are not blocked by other pieces, they can simply jump over them. There are no pawns on the board. King and knight possible moves, respectively. Dotted line shows that knight can jump over other pieces. Queen, bishop, and rook possible moves, respectively. Input The first line contains two integers $$$x_s$$$ and $$$y_s$$$ ($$$1 le x_s le 10^8$$$; $$$1 le y_s le 8$$$) — the starting coordinates of the black king. The second line contains two integers $$$x_t$$$ and $$$y_t$$$ ($$$1 le x_t le 10^8$$$; $$$1 le y_t le 8$$$) — the coordinates of the target square for the black king. The third line contains one integer $$$n$$$ ($$$0 le n le 2000$$$) — the number of white pieces on the board. Then $$$n$$$ lines follow, the $$$i$$$-th line contains one character $$$t_i$$$ and two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i le 10^8$$$; $$$1 le y_i le 8$$$) — the type and the coordinates of the $$$i$$$-th white piece. The types of pieces are represented by the following uppercase Latin letters: K — king Q — queen R — rook B — bishop N — knight There can be any number of white pieces of any type listed above on the board, for example, $$$3$$$ white kings or $$$4$$$ white queens. There are no pawns on the board. Additional constrains on the input: no square is occupied by more than one white piece; the starting square for the black king is different from the square he wants to reach, and neither of these two squares is occupied or is under attack by any white piece. Output Print one integer — the minimum number of moves needed for the black king to reach the target square while not violating the conditions, or $$$-1$$$ if it is impossible. Examples Input 1 8 7 8 2 N 4 8 B 4 6 Input 1 1 1 5 2 K 1 3 R 2 3 Note The image below demonstrates the solution for the second example. Here, the letters K, R, s, and t represent the white king, the white rook, the starting square, and the target square, respectively. Bold crosses mark the squares which are under attack by the white pieces. Bold dots show the shortest path for the black king.
2,800
false
false
true
false
false
false
false
false
false
false
1,709
1332D
Bob is playing a game named "Walk on Matrix". In this game, player is given an $$$n imes m$$$ matrix $$$A=(a_{i,j})$$$, i.e. the element in the $$$i$$$-th row in the $$$j$$$-th column is $$$a_{i,j}$$$. Initially, player is located at position $$$(1,1)$$$ with score $$$a_{1,1}$$$. To reach the goal, position $$$(n,m)$$$, player can move right or down, i.e. move from $$$(x,y)$$$ to $$$(x,y+1)$$$ or $$$(x+1,y)$$$, as long as player is still on the matrix. However, each move changes player's score to the $$$ such that $$$1 le n,m le 500$$$ (as Bob hates large matrix); $$$0 le a_{i,j} le 3 cdot 10^5$$$ for all $$$1 le ile n,1 le jle m$$$ (as Bob hates large numbers); the difference between the maximum score he can get and the output of his algorithm is exactly $$$k$$$. It can be shown that for any given integer $$$k$$$ such that $$$0 le k le 10^5$$$, there exists a matrix satisfying the above constraints. Please help him with it! Output Output two integers $$$n$$$, $$$m$$$ ($$$1 le n,m le 500$$$) in the first line, representing the size of the matrix. Then output $$$n$$$ lines with $$$m$$$ integers in each line, $$$a_{i,j}$$$ in the $$$(i+1)$$$-th row, $$$j$$$-th column. Note In the first example, the maximum score Bob can achieve is $$$300000$$$, while the output of his algorithm is $$$300000$$$. In the second example, the maximum score Bob can achieve is $$$7&3&3&3&7&3=3$$$, while the output of his algorithm is $$$2$$$.
1,700
true
false
false
false
false
true
false
false
false
false
4,047
1965C
You have a strip of paper with a binary string $$$s$$$ of length $$$n$$$. You can fold the paper in between any pair of adjacent digits. A set of folds is considered valid if after the folds, all characters that are on top of or below each other match. Note that all folds are made at the same time, so the characters don't have to match in between folds. For example, these are valid foldings of $$$s = mathtt{110110110011}$$$ and $$$s = mathtt{01110}$$$: The length of the folded strip is the length seen from above after all folds are made. So for the two above examples, after the folds shown above, the lengths would be $$$7$$$ and $$$3$$$, respectively. Notice that for the above folding of $$$s = mathtt{01110}$$$, if we made either of the two folds on their own, that would not be a valid folding. However, because we don't check for validity until all folds are made, this folding is valid. After performing a set of valid folds, what is the minimum length strip you can form? Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2cdot 10^5$$$)xa0— the size of the strip. The second line of each test case contains a string $$$s$$$ of $$$n$$$ characters '0' and '1'xa0— a description of the digits on the strip. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Note For the first example case, one optimal folding is to fold the strip in the middle, which produces a strip of length 3. The third and fourth example cases correspond to the images above. Note that the folding shown above for $$$s = mathtt{110110110011}$$$ is not of minimal length.
2,300
false
true
false
false
false
true
false
false
false
false
514
1326F1
This is the easy version of the problem. The difference is constraints on the number of wise men and the time limit. You can make hacks only if all versions of this task are solved. $$$n$$$ wise men live in a beautiful city. Some of them know each other. For each of the $$$n!$$$ possible permutations $$$p_1, p_2, ldots, p_n$$$ of the wise men, let's generate a binary string of length $$$n-1$$$: for each $$$1 leq i < n$$$ set $$$s_i=1$$$ if $$$p_i$$$ and $$$p_{i+1}$$$ know each other, and $$$s_i=0$$$ otherwise. For all possible $$$2^{n-1}$$$ binary strings, find the number of permutations that produce this binary string. Input The first line of input contains one integer $$$n$$$ ($$$2 leq n leq 14)$$$ xa0— the number of wise men in the city. The next $$$n$$$ lines contain a binary string of length $$$n$$$ each, such that the $$$j$$$-th character of the $$$i$$$-th string is equal to '1' if wise man $$$i$$$ knows wise man $$$j$$$, and equals '0' otherwise. It is guaranteed that if the $$$i$$$-th man knows the $$$j$$$-th man, then the $$$j$$$-th man knows $$$i$$$-th man and no man knows himself. Output Print $$$2^{n-1}$$$ space-separated integers. For each $$$0 leq x < 2^{n-1}$$$: Let's consider a string $$$s$$$ of length $$$n-1$$$, such that $$$s_i = lfloor frac{x}{2^{i-1}} floor bmod 2$$$ for all $$$1 leq i leq n - 1$$$. The $$$(x+1)$$$-th number should be equal to the required answer for $$$s$$$. Examples Input 4 0101 1000 0001 1010 Note In the first test, each wise man knows each other, so every permutation will produce the string $$$11$$$. In the second test: If $$$p = {1, 2, 3, 4}$$$, the produced string is $$$101$$$, because wise men $$$1$$$ and $$$2$$$ know each other, $$$2$$$ and $$$3$$$ don't know each other, and $$$3$$$ and $$$4$$$ know each other; If $$$p = {4, 1, 2, 3}$$$, the produced string is $$$110$$$, because wise men $$$1$$$ and $$$4$$$ know each other, $$$1$$$ and $$$2$$$ know each other and $$$2$$$, and $$$3$$$ don't know each other; If $$$p = {1, 3, 2, 4}$$$, the produced string is $$$000$$$, because wise men $$$1$$$ and $$$3$$$ don't know each other, $$$3$$$ and $$$2$$$ don't know each other, and $$$2$$$ and $$$4$$$ don't know each other.
2,600
false
false
false
true
false
false
true
false
false
false
4,081
1427F
When they are bored, Federico and Giada often play the following card game with a deck containing $$$6n$$$ cards. Each card contains one number between $$$1$$$ and $$$6n$$$ and each number appears on exactly one card. Initially the deck is sorted, so the first card contains the number $$$1$$$, the second card contains the number $$$2$$$, $$$dots$$$, and the last one contains the number $$$6n$$$. Federico and Giada take turns, alternating; Federico starts. In his turn, the player takes $$$3$$$ contiguous cards from the deck and puts them in his pocket. The order of the cards remaining in the deck is not changed. They play until the deck is empty (after exactly $$$2n$$$ turns). At the end of the game both Federico and Giada have $$$3n$$$ cards in their pockets. You are given the cards in Federico's pocket at the end of the game. Describe a sequence of moves that produces that set of cards in Federico's pocket. Input The first line of the input contains one integer $$$n$$$ ($$$1le n le 200$$$). The second line of the input contains $$$3n$$$ numbers $$$x_1, x_2,ldots, x_{3n}$$$ ($$$1 le x_1 < x_2 <ldots < x_{3n} le 6n$$$) – the cards in Federico's pocket at the end of the game. It is guaranteed that for each test there is at least one sequence of moves that produces such set of cards in Federico's pocket. Output Print $$$2n$$$ lines, each containing $$$3$$$ integers. The $$$i$$$-th line should contain, in increasing order, the integers $$$a_i<b_i<c_i$$$ written on the three cards taken by the player during the $$$i$$$-th turn (so taken by Federico if $$$i$$$ is odd and by Giada if $$$i$$$ is even). If there is more than one possible sequence of moves, you can print any. Examples Output 9 10 11 6 7 8 2 3 4 1 5 12 Input 5 1 2 3 4 5 9 11 12 13 18 19 20 21 22 23 Output 19 20 21 24 25 26 11 12 13 27 28 29 1 2 3 14 15 16 18 22 23 6 7 8 4 5 9 10 17 30 Note Explanation of the first testcase: Initially the deck has $$$12 = 2cdot 6$$$ sorted cards, so the deck is $$$[1 2 3 4 5 6 7 8 9 10 11 12]$$$. During turn $$$1$$$, Federico takes the three cards $$$[9 10 11]$$$. After his move, the deck is $$$[1 2 3 4 5 6 7 8 12]$$$. During turn $$$2$$$, Giada takes the three cards $$$[6 7 8]$$$. After her move, the deck is $$$[1 2 3 4 5 12]$$$. During turn $$$3$$$, Federico takes the three cards $$$[2 3 4]$$$. After his move, the deck is $$$[1 5 12]$$$. During turn $$$4$$$, Giada takes the three cards $$$[1 5 12]$$$. After her move, the deck is empty. At the end of the game, the cards in Federico's pocket are $$$[2 3 4 9 10 11]$$$ and the cards in Giada's pocket are $$$[1 5 6 7 8 12]$$$.
3,200
false
true
false
false
true
false
false
false
false
false
3,528
901E
Senor Vorpal Kickass'o invented an innovative method to encrypt integer sequences of length _n_. To encrypt a sequence, one has to choose a secret sequence , that acts as a key. Vorpal is very selective, so the key should be such a sequence _b__i_, that its cyclic shifts are linearly independent, that is, there is no non-zero set of coefficients _x_0,u2009_x_1,u2009...,u2009_x__n_u2009-u20091, such that for all _k_ at the same time. After that for a sequence you should build the following cipher: In other words, you are to compute the quadratic deviation between each cyclic shift of _b__i_ and the sequence _a__i_. The resulting sequence is the Kickass's cipher. The cipher is in development right now and Vorpal wants to decipher a sequence after it has been encrypted. You are to solve this problem for him. You are given sequences _c__i_ and _b__i_. You are to find all suitable sequences _a__i_. Input The first line contains a single integer _n_ (). The second line contains _n_ integers _b_0,u2009_b_1,u2009...,u2009_b__n_u2009-u20091 (). The third line contains _n_ integers _c_0,u2009_c_1,u2009...,u2009_c__n_u2009-u20091 (). It is guaranteed that all cyclic shifts of sequence _b__i_ are linearly independent. Output In the first line print a single integer _k_xa0— the number of sequences _a__i_, such that after encrypting them with key _b__i_ you get the sequence _c__i_. After that in each of _k_ next lines print _n_ integers _a_0,u2009_a_1,u2009...,u2009_a__n_u2009-u20091. Print the sequences in lexicographical order. Note that _k_ could be equal to 0.
3,300
true
false
false
false
false
false
false
false
false
false
6,137
1692G
Given an array $$$a$$$ of length $$$n$$$ and an integer $$$k$$$, find the number of indices $$$1 leq i leq n - k$$$ such that the subarray $$$[a_i, dots, a_{i+k}]$$$ with length $$$k+1$$$ (not with length $$$k$$$) has the following property: If you multiply the first element by $$$2^0$$$, the second element by $$$2^1$$$, ..., and the ($$$k+1$$$)-st element by $$$2^k$$$, then this subarray is sorted in strictly increasing order. More formally, count the number of indices $$$1 leq i leq n - k$$$ such that $$$$$$2^0 cdot a_i < 2^1 cdot a_{i+1} < 2^2 cdot a_{i+2} < dots < 2^k cdot a_{i+k}.$$$$$$ Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$3 leq n leq 2 cdot 10^5$$$, $$$1 leq k < n$$$)xa0— the length of the array and the number of inequalities. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the elements of the array. The sum of $$$n$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0— the number of indices satisfying the condition in the statement. Example Input 6 4 2 20 22 19 84 5 1 9 5 3 2 1 5 2 9 5 3 2 1 7 2 22 12 16 4 3 22 12 7 3 22 12 16 4 3 22 12 9 3 3 9 12 3 9 12 3 9 12 Note In the first test case, both subarrays satisfy the condition: $$$i=1$$$: the subarray $$$[a_1,a_2,a_3] = [20,22,19]$$$, and $$$1 cdot 20 < 2 cdot 22 < 4 cdot 19$$$. $$$i=2$$$: the subarray $$$[a_2,a_3,a_4] = [22,19,84]$$$, and $$$1 cdot 22 < 2 cdot 19 < 4 cdot 84$$$. In the second test case, three subarrays satisfy the condition: $$$i=1$$$: the subarray $$$[a_1,a_2] = [9,5]$$$, and $$$1 cdot 9 < 2 cdot 5$$$. $$$i=2$$$: the subarray $$$[a_2,a_3] = [5,3]$$$, and $$$1 cdot 5 < 2 cdot 3$$$. $$$i=3$$$: the subarray $$$[a_3,a_4] = [3,2]$$$, and $$$1 cdot 3 < 2 cdot 2$$$. $$$i=4$$$: the subarray $$$[a_4,a_5] = [2,1]$$$, but $$$1 cdot 2 = 2 cdot 1$$$, so this subarray doesn't satisfy the condition.
1,400
false
false
false
true
true
false
false
false
true
false
2,124
1081E
Chouti is working on a strange math problem. There was a sequence of $$$n$$$ positive integers $$$x_1, x_2, ldots, x_n$$$, where $$$n$$$ is even. The sequence was very special, namely for every integer $$$t$$$ from $$$1$$$ to $$$n$$$, $$$x_1+x_2+...+x_t$$$ is a square of some integer number (that is, a . The second line contains $$$frac{n}{2}$$$ positive integers $$$x_2, x_4, ldots, x_n$$$ ($$$1 le x_i le 2 cdot 10^5$$$). Output If there are no possible sequence, print "No". Otherwise, print "Yes" and then $$$n$$$ positive integers $$$x_1, x_2, ldots, x_n$$$ ($$$1 le x_i le 10^{13}$$$), where $$$x_2, x_4, ldots, x_n$$$ should be same as in input data. If there are multiple answers, print any. Note, that the limit for $$$x_i$$$ is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying $$$1 le x_i le 10^{13}$$$. Note In the first example $$$x_1=4$$$ $$$x_1+x_2=9$$$ $$$x_1+x_2+x_3=25$$$ $$$x_1+x_2+x_3+x_4=36$$$ $$$x_1+x_2+x_3+x_4+x_5=100$$$ $$$x_1+x_2+x_3+x_4+x_5+x_6=144$$$ All these numbers are perfect squares. In the second example, $$$x_1=100$$$, $$$x_1+x_2=10000$$$. They are all perfect squares. There're other answers possible. For example, $$$x_1=22500$$$ is another answer. In the third example, it is possible to show, that no such sequence exists.
1,900
true
true
false
false
false
true
false
true
false
false
5,319
1627F
There is a $$$k imes k$$$ grid, where $$$k$$$ is even. The square in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r,c)$$$. Two squares $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ are considered adjacent if $$$lvert r_1 - r_2 vert + lvert c_1 - c_2 vert = 1$$$. An array of adjacent pairs of squares is called strong if it is possible to cut the grid along grid lines into two connected, elements. Input The input consists of multiple test cases. The first line contains an 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 two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 10^5$$$; $$$2 leq k leq 500$$$, $$$k$$$ is even) — the length of $$$a$$$ and the size of the grid, respectively. Then $$$n$$$ lines follow. The $$$i$$$-th of these lines contains four space-separated integers $$$r_{i,1}$$$, $$$c_{i,1}$$$, $$$r_{i,2}$$$, and $$$c_{i,2}$$$ ($$$1 leq r_{i,1}, c_{i,1}, r_{i,2}, c_{i,2} leq k$$$) — the $$$i$$$-th element of $$$a$$$, represented by the row and column of the first square $$$(r_{i,1}, c_{i,1})$$$ and the row and column of the second square $$$(r_{i,2}, c_{i,2})$$$. These squares are adjacent. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$k$$$ over all test cases does not exceed $$$500$$$. Output For each test case, output a single integer — the size of the largest strong subsequence of $$$a$$$. Example Input 3 8 4 1 2 1 3 2 2 2 3 3 2 3 3 4 2 4 3 1 4 2 4 2 1 3 1 2 2 3 2 4 1 4 2 7 2 1 1 1 2 2 1 2 2 1 1 1 2 1 1 2 1 1 2 2 2 1 1 2 1 1 2 2 2 1 6 3 3 3 4 Note In the first test case, the array $$$a$$$ is not good, but if we take the subsequence $$$[a_1, a_2, a_3, a_4, a_5, a_6, a_8]$$$, then the square can be split as shown in the statement. In the second test case, we can take the subsequence consisting of the last four elements of $$$a$$$ and cut the square with a horizontal line through its center.
2,700
false
true
true
false
false
false
false
false
false
true
2,476
2038F
Suppose you are working in the Ministry of Digital Development of Berland, and your task is to monitor the industry of video blogging. There are $$$n$$$ bloggers in Berland. Recently, due to the poor state of the main video platform in Berland, two alternative platforms were introduced. That's why bloggers started to reupload their videos to these alternative platforms. You've got the statistics that the $$$i$$$-th blogger uploaded $$$v_i$$$ videos to the first alternative platform and $$$r_i$$$ videos to the second alternative platform. You think that a potential user will be upset if even at least one of his favorite bloggers doesn't upload anything. However, if a blogger uploads videos to both platforms, the user will watch that blogger on the platform where more videos are available. So, you've come up with the following function to estimate user experience. Suppose a user watches $$$k$$$ bloggers $$$b_1, b_2, dots, b_k$$$; then, let user experience be $$$$$$E(b_1, dots, b_k) = maxleft(min_{i=1..k}{v[b_i]}, min_{i=1..k}{r[b_i]} ight).$$$$$$ In order to get some statistics, you want to calculate the value $$$mathit{avg}_k$$$ that is equal to an average experience among all subsets of bloggers of size $$$k$$$. Also, you have to calculate $$$mathit{avg}_k$$$ for each $$$k$$$ from $$$1$$$ to $$$n$$$. Since answers may be too large, print them modulo $$$998,244,353$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the number of bloggers. The second line contains $$$n$$$ integers $$$v_1, v_2, dots, v_n$$$ ($$$0 le v_i le 10^6$$$), where $$$v_i$$$ is the number of videos of the $$$i$$$-th blogger on the first alternative platform. The third line contains $$$n$$$ integers $$$r_1, r_2, dots, r_n$$$ ($$$0 le r_i le 10^6$$$), where $$$r_i$$$ is the number of videos of the $$$i$$$-th blogger on the second alternative platform. Output Print $$$n$$$ integers $$$mathit{avg}_1, mathit{avg}_2, dots, mathit{avg}_n$$$. It can be proven that $$$mathit{avg}_k$$$ may be represented as an irreducible fraction $$$dfrac{x}{y}$$$ where $$$y otequiv 0 pmod{998,244,353}$$$. So, print $$$mathit{avg}_k$$$ in a form $$$x cdot y^{-1} bmod 998,244,353$$$. Note In the first example, $$$332748119$$$ is $$$frac{4}{3}$$$. In the third example, $$$199648873$$$ is $$$frac{12}{5}$$$.
2,500
true
false
false
false
true
false
false
false
true
false
33
854B
Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has _n_ apartments that are numbered from 1 to _n_ and are arranged in a row. Two apartments are adjacent if their indices differ by 1. Some of the apartments can already be inhabited, others are available for sale. Maxim often visits his neighbors, so apartment is good for him if it is available for sale and there is at least one already inhabited apartment adjacent to it. Maxim knows that there are exactly _k_ already inhabited apartments, but he doesn't know their indices yet. Find out what could be the minimum possible and the maximum possible number of apartments that are good for Maxim. Input The only line of the input contains two integers: _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009109, 0u2009≤u2009_k_u2009≤u2009_n_). Output Print the minimum possible and the maximum possible number of apartments good for Maxim. Note In the sample test, the number of good apartments could be minimum possible if, for example, apartments with indices 1, 2 and 3 were inhabited. In this case only apartment 4 is good. The maximum possible number could be, for example, if apartments with indices 1, 3 and 5 were inhabited. In this case all other apartments: 2, 4 and 6 are good.
1,200
true
false
false
false
false
true
false
false
false
false
6,328
649E
Вдоль дороги стоят _n_ путешественников. Дорога представляет собой прямую, размерами путешественников можно пренебречь, считая их точками. Водитель автобуса Василий, благодаря мобильному приложению, знает для каждого путешественника точку _x__i_, в которой тот стоит. Кроме того, он знает расстояние _d__i_, которое _i_-й путешественник хочет проехать на автобусе. Таким образом, _i_-й путешественник планирует выйти из автобуса в точке _x__i_u2009+u2009_d__i_. Теперь Василий хочет решить, кого из путешественников он подвезёт, а кого оставит пылиться у дороги. Василий решил, что сегодня он должен хорошо заработать, поэтому решил перевезти в точности _a_ путешественников. В автопарке есть автобусы любых видов. Чем больше мест в автобусе, тем дороже стоит его аренда. Помогите Василию определить минимальное количество пассажирских мест в автобусе, которых будет достаточно для перевозки ровно _a_ путешественников. Ни в какой момент времени в автобусе не может быть путешественников больше, чем количество пассажирских мест в автобусе. Василий сам может решить какое конкретно подмножество из _a_ путешественников он перевезёт на автобусе. Считайте, что автобус всегда едет слева направо (от меньших координат к большим) и начинает свой путь левее самого левостоящего путешественника. Если в одной точке какой-то путешественник должен выйти из автобуса, а другой войти, считайте, что сначала произойдет выход одного путешественника из автобуса, а затем другой путешественник сможет зайти в автобус. Входные данные В первой строке входных данных следует два целых положительных числа _n_ и _a_ (1u2009≤u2009_a_u2009≤u2009_n_u2009≤u2009200u2009000)xa0— количество путешественников, стоящих вдоль дороги, и минимальное количество путешественников, которых Василий хочет подвезти. В каждой из следующих _n_ строк содержится по два целых числа _x__i_ и _d__i_ (1u2009≤u2009_x__i_,u2009_d__i_u2009≤u2009109)xa0— координата, в которой находится _i_-й путешественник, а также расстояние, на которое он хочет переместиться на автобусе. Координаты путешественников заданы в произвольном порядке. В одной точке могут находиться несколько путешественников. Выходные данные Сначала выведите одно целое числоxa0— минимальное количество пассажирских мест в автобусе, которых будет достаточно для перевозки хотя бы _a_ путешественников. Затем выведите _a_ целых чиселxa0— номера путешественников, которых подвезёт Василий. Путешественники нумеруются, начиная с единицы, в том порядке, в котором заданы во входных данных. Номера можно выводить в произвольном порядке. Если ответов несколько, разрешается вывести любой из них. Примечание В первом тестовом примере достаточно одноместного автобуса. К примеру, Василий может подвезти третьего и первого путешественников, либо второго и первого путешественников.
2,100
false
true
false
false
true
false
false
true
true
false
7,218
40D
Berland scientists noticed long ago that the world around them depends on Berland population. Due to persistent research in this area the scientists managed to find out that the Berland chronology starts from the moment when the first two people came to that land (it is considered to have happened in the first year). After one Berland year after the start of the chronology the population had already equaled 13 people (the second year). However, tracing the population number during the following years was an ultimately difficult task, still it was found out that if _d__i_ — the number of people in Berland in the year of _i_, then either _d__i_u2009=u200912_d__i_u2009-u20092, or _d__i_u2009=u200913_d__i_u2009-u20091u2009-u200912_d__i_u2009-u20092. Of course no one knows how many people are living in Berland at the moment, but now we can tell if there could possibly be a year in which the country population equaled _A_. That's what we ask you to determine. Also, if possible, you have to find out in which years it could be (from the beginning of Berland chronology). Let's suppose that it could be in the years of _a_1,u2009_a_2,u2009...,u2009_a__k_. Then you have to define how many residents could be in the country during those years apart from the _A_ variant. Look at the examples for further explanation. Input The first line contains integer _A_ (1u2009≤u2009_A_u2009<u200910300). It is guaranteed that the number doesn't contain leading zeros. Output On the first output line print YES, if there could be a year in which the total population of the country equaled _A_, otherwise print NO. If the answer is YES, then you also have to print number _k_ — the number of years in which the population could equal _A_. On the next line you have to output precisely _k_ space-separated numbers — _a_1,u2009_a_2,u2009...,u2009_a__k_. Those numbers have to be output in the increasing order. On the next line you should output number _p_ — how many variants of the number of people could be in the years of _a_1,u2009_a_2,u2009...,u2009_a__k_, apart from the _A_ variant. On each of the next _p_ lines you have to print one number — the sought number of residents. Those number also have to go in the increasing order. If any number (or both of them) _k_ or _p_ exceeds 1000, then you have to print 1000 instead of it and only the first 1000 possible answers in the increasing order. The numbers should have no leading zeros.
2,600
true
false
false
false
false
false
false
false
false
false
9,786
1472F
Every year Santa Claus gives gifts to all children. However, each country has its own traditions, and this process takes place in different ways. For example, in Berland you need to solve the New Year's puzzle. Polycarp got the following problem: given a grid strip of size $$$2 imes n$$$, some cells of it are blocked. You need to check if it is possible to tile all free cells using the $$$2 imes 1$$$ and $$$1 imes 2$$$ tiles (dominoes). For example, if $$$n = 5$$$ and the strip looks like this (black cells are blocked): Then it can be tiled, for example, using two vertical and two horizontal tiles, as in the picture below (different tiles are marked by different colors). And if $$$n = 3$$$ and the strip looks like this: It is impossible to tile free cells. Polycarp easily solved this task and received his New Year's gift. Can you solve it? Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. Then $$$t$$$ test cases follow. Each test case is preceded by an empty line. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^9$$$, $$$1 le m le 2 cdot 10^5$$$)xa0— the length of the strip and the number of blocked cells on it. Each of the next $$$m$$$ lines contains two integers $$$r_i, c_i$$$ ($$$1 le r_i le 2, 1 le c_i le n$$$)xa0— numbers of rows and columns of blocked cells. It is guaranteed that all blocked cells are different, i.e. $$$(r_i, c_i) e (r_j, c_j), i e j$$$. It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print on a separate line: "YES", if it is possible to tile all unblocked squares with the $$$2 imes 1$$$ and $$$1 imes 2$$$ tiles; "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 3 5 2 2 2 1 4 3 2 2 1 2 3 6 4 2 1 2 3 2 4 2 6 Note The first two test cases are explained in the statement. In the third test case the strip looks like this: It is easy to check that the unblocked squares on it can not be tiled.
2,100
false
true
false
true
false
false
true
false
true
false
3,319
1851G
Vlad decided to go on a trip to the mountains. He plans to move between $$$n$$$ mountains, some of which are connected by roads. The $$$i$$$-th mountain has a height of $$$h_i$$$. If there is a road between mountains $$$i$$$ and $$$j$$$, Vlad can move from mountain $$$i$$$ to mountain $$$j$$$ by spending $$$h_j - h_i$$$ units of energy. If his energy drops below zero during the transition, he will not be able to move from mountain $$$i$$$ to mountain $$$j$$$. Note that $$$h_j - h_i$$$ can be negative and then the energy will be restored. Vlad wants to consider different route options, so he asks you to answer the following queries: is it possible to construct some route starting at mountain $$$a$$$ and ending at mountain $$$b$$$, given that he initially has $$$e$$$ units of energy? Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two numbers $$$n$$$ and $$$m$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$1 le m le min(frac{ncdot(n - 1)}{2}, 2 cdot 10^5)$$$)xa0— the number of mountains and the number of roads between them, respectively. The second line contains $$$n$$$ integers $$$h_1, h_2, h_3, dots, h_n$$$ ($$$1 le h_i le 10^9$$$)xa0— the heights of the mountains. The next $$$m$$$ lines contain two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u e v$$$)xa0— the numbers of the mountains connected by a road. It is guaranteed that no road appears twice. The next line contains an integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$)xa0— the number of queries. The following $$$q$$$ lines contain three numbers $$$a$$$, $$$b$$$, and $$$e$$$ ($$$1 le a, b le n$$$, $$$0 le e le 10^9$$$)xa0— the initial and final mountains of the route, and the amount of energy, respectively. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. The same guarantee applies to $$$m$$$ and $$$q$$$. Output For each query, output "YES" if Vlad can construct a route from mountain $$$a$$$ to mountain $$$b$$$, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer). In the examples below, the answers for different test cases are separated by an empty line, which you do not need to output.
2,000
false
false
true
false
true
false
false
true
true
true
1,168
1445A
You are given two arrays $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ positive integers, and an integer $$$x$$$. Please determine if one can rearrange the elements of $$$b$$$ so that $$$a_i + b_i leq x$$$ holds for each $$$i$$$ ($$$1 le i le n$$$). Input The first line of input contains one integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. $$$t$$$ blocks follow, each describing an individual test case. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 leq n leq 50$$$; $$$1 leq x leq 1000$$$)xa0— the length of arrays $$$a$$$ and $$$b$$$, and the parameter $$$x$$$, described in the problem statement. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_1 le a_2 le dots le a_n leq x$$$)xa0— the elements of array $$$a$$$ in non-descending order. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 leq b_1 le b_2 le dots le b_n leq x$$$)xa0— the elements of array $$$b$$$ in non-descending order. Test cases are separated by a blank line. Output For each test case print Yes if one can rearrange the corresponding array $$$b$$$ so that $$$a_i + b_i leq x$$$ holds for each $$$i$$$ ($$$1 le i le n$$$) or No otherwise. Each character can be printed in any case. Example Input 4 3 4 1 2 3 1 1 2 2 6 1 4 2 5 4 4 1 2 3 4 1 2 3 4 1 5 5 5 Note In the first test case, one can rearrange $$$b$$$ so it'll look like $$$[1, 2, 1]$$$. In this case, $$$1 + 1 leq 4$$$; $$$2 + 2 leq 4$$$; $$$3 + 1 leq 4$$$. In the second test case, one can set $$$b$$$ to $$$[5, 2]$$$, then $$$1 + 5 leq 6$$$; $$$4 + 2 leq 6$$$. In the third test case, no matter how one shuffles array $$$b$$$, $$$a_4 + b_4 = 4 + b_4 > 4$$$. In the fourth test case, there is only one rearrangement of array $$$b$$$ and it doesn't satisfy the condition since $$$5 + 5 > 5$$$.
800
false
true
false
false
false
false
false
false
true
false
3,449
2023D
Recently, you received a rare ticket to the only casino in the world where you can actually earn something, and you want to take full advantage of this opportunity. The conditions in this casino are as follows: There are a total of $$$n$$$ games in the casino. You can play each game at most once. Each game is characterized by two parameters: $$$p_i$$$ ($$$1 le p_i le 100$$$) and $$$w_i$$$ — the probability of winning the game in percentage and the winnings for a win. If you lose in any game you decide to play, you will receive nothing at all (even for the games you won). You need to choose a set of games in advance that you will play in such a way as to maximize the expected value of your winnings. In this case, if you choose to play the games with indices $$$i_1 < i_2 < ldots < i_k$$$, you will win in all of them with a probability of $$$prodlimits_{j=1}^k frac{p_{i_j}}{100}$$$, and in that case, your winnings will be equal to $$$sumlimits_{j=1}^k w_{i_j}$$$. That is, the expected value of your winnings will be $$$left(prodlimits_{j=1}^k frac{p_{i_j}}{100} ight) cdot left(sumlimits_{j=1}^k w_{i_j} ight)$$$. To avoid going bankrupt, the casino owners have limited the expected value of winnings for each individual game. Thus, for all $$$i$$$ ($$$1 le i le n$$$), it holds that $$$w_i cdot p_i le 2 cdot 10^5$$$. Your task is to find the maximum expected value of winnings that can be obtained by choosing some set of games in the casino. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the number of games offered to play. The $$$i$$$-th of the following $$$n$$$ lines contains two integers $$$p_i$$$ and $$$w_i$$$ ($$$1 leq p_i leq 100$$$, $$$1 leq w_i, p_i cdot w_i leq 2 cdot 10^5$$$)xa0— the probability of winning and the size of the winnings in the $$$i$$$-th game. Output Output a single number — the maximum expected value of winnings in the casino that can be obtained by choosing some subset of games. Your answer will be accepted if the relative or absolute error does not exceed $$$10^{-6}$$$. Formally, if $$$a$$$ is your answer and $$$b$$$ is the jury's answer, it will be accepted if $$$frac{a-b}{max(b, 1)} le 10^{-6}$$$. Examples Input 4 1 100 2 1000 2 100 3 1 Input 5 34 804 78 209 99 191 61 439 90 79 Note In the first example, you can choose the first and third games. In this case, the expected value of winnings will be $$$left(frac{p_1}{100}cdot frac{p_3}{100} ight) cdot (w_1 + w_3) = left(frac{80}{100}cdot frac{50}{100} ight) cdot (80 + 200) = 112$$$. In the second example, you can choose the first and second games. In this case, the expected value of winnings will be $$$left(frac{p_1}{100}cdot frac{p_2}{100} ight) cdot (w_1 + w_2) = left(frac{100}{100}cdot frac{100}{100} ight) cdot (1 + 1) = 2$$$. In the third example, you can choose only the second game. In this case, the expected value of winnings will be $$$frac{p_2}{100} cdot w_2 = frac{2}{100} cdot 1000 = 20$$$.
2,900
true
true
false
true
false
false
true
false
false
false
128
587F
Duff is mad at her friends. That's why she sometimes makes Malek to take candy from one of her friends for no reason! She has _n_ friends. Her _i_-th friend's name is _s__i_ (their names are not necessarily unique). _q_ times, she asks Malek to take candy from her friends. She's angry, but also she acts with rules. When she wants to ask Malek to take candy from one of her friends, like _k_, she chooses two numbers _l_ and _r_ and tells Malek to take exactly candies from him/her, where _occur_(_t_,u2009_s_) is the number of occurrences of string _t_ in _s_. Malek is not able to calculate how many candies to take in each request from Duff. That's why she asked for your help. Please tell him how many candies to take in each request. Input The first line of input contains two integers _n_ and _q_ (1u2009≤u2009_n_,u2009_q_u2009≤u2009105). The next _n_ lines contain the names. _i_-th of them contains an string _s__i_, consisting of lowercase English letters (). The next _q_ lines contain the requests. Each of them contains three integers, _l_,u2009_r_ and _k_ (says that Malek should take candies from Duff's _k_-th friend). Output Print the answer to each request in one line. Examples Input 5 5 a ab abab ababab b 1 5 4 3 5 4 1 5 2 1 5 3 1 4 1
3,000
false
false
false
false
true
false
false
false
false
false
7,492
477E
Dreamoon has just created a document of hard problems using notepad.exe. The document consists of _n_ lines of text, _a__i_ denotes the length of the _i_-th line. He now wants to know what is the fastest way to move the cursor around because the document is really long. Let (_r_,u2009_c_) be a current cursor position, where _r_ is row number and _c_ is position of cursor in the row. We have 1u2009≤u2009_r_u2009≤u2009_n_ and 0u2009≤u2009_c_u2009≤u2009_a__r_. We can use following six operations in notepad.exe to move our cursor assuming the current cursor position is at (_r_,u2009_c_): 1. up key: the new cursor position (_nr_,u2009_nc_)u2009=u2009(_max_(_r_u2009-u20091,u20091),u2009_min_(_a__nr_,u2009_c_)) 2. down key: the new cursor position (_nr_,u2009_nc_)u2009=u2009(_min_(_r_u2009+u20091,u2009_n_),u2009_min_(_a__nr_,u2009_c_)) 3. left key: the new cursor position (_nr_,u2009_nc_)u2009=u2009(_r_,u2009_max_(0,u2009_c_u2009-u20091)) 4. right key: the new cursor position (_nr_,u2009_nc_)u2009=u2009(_r_,u2009_min_(_a__nr_,u2009_c_u2009+u20091)) 5. HOME key: the new cursor position (_nr_,u2009_nc_)u2009=u2009(_r_,u20090) 6. END key: the new cursor position (_nr_,u2009_nc_)u2009=u2009(_r_,u2009_a__r_) You're given the document description (_n_ and sequence _a__i_) and _q_ queries from Dreamoon. Each query asks what minimal number of key presses is needed to move the cursor from (_r_1,u2009_c_1) to (_r_2,u2009_c_2). Input The first line contains an integer _n_(1u2009≤u2009_n_u2009≤u2009400,u2009000) — the number of lines of text. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_(1u2009≤u2009_a__i_u2009≤u2009108). The third line contains an integer _q_(1u2009≤u2009_q_u2009≤u2009400,u2009000). Each of the next _q_ lines contains four integers _r_1,u2009_c_1,u2009_r_2,u2009_c_2 representing a query (1u2009≤u2009_r_1,u2009_r_2u2009≤u2009_n_,u20090u2009≤u2009_c_1u2009≤u2009_a__r_1,u20090u2009≤u2009_c_2u2009≤u2009_a__r_2). Output For each query print the result of the query. Examples Input 9 1 3 5 3 1 3 5 3 1 4 3 5 3 1 3 3 7 3 1 0 3 3 6 0 7 3 Note In the first sample, the first query can be solved with keys: HOME, right. The second query can be solved with keys: down, down, down, END, down. The third query can be solved with keys: down, END, down. The fourth query can be solved with keys: END, down.
3,100
false
false
false
false
true
false
false
false
false
false
7,922
1206B
You are given $$$n$$$ numbers $$$a_1, a_2, dots, a_n$$$. With a cost of one coin you can perform the following operation: Choose one of these numbers and add or subtract $$$1$$$ from it. In particular, we can apply this operation to the same number several times. We want to make the product of all these numbers equal to $$$1$$$, in other words, we want $$$a_1 cdot a_2$$$ $$$dots$$$ $$$cdot a_n = 1$$$. For example, for $$$n = 3$$$ and numbers $$$[1, -3, 0]$$$ we can make product equal to $$$1$$$ in $$$3$$$ coins: add $$$1$$$ to second element, add $$$1$$$ to second element again, subtract $$$1$$$ from third element, so that array becomes $$$[1, -1, -1]$$$. And $$$1cdot (-1) cdot (-1) = 1$$$. What is the minimum cost we will have to pay to do that? Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$)xa0— the numbers. Note In the first example, you can change $$$1$$$ to $$$-1$$$ or $$$-1$$$ to $$$1$$$ in $$$2$$$ coins. In the second example, you have to apply at least $$$4$$$ operations for the product not to be $$$0$$$. In the third example, you can change $$$-5$$$ to $$$-1$$$ in $$$4$$$ coins, $$$-3$$$ to $$$-1$$$ in $$$2$$$ coins, $$$5$$$ to $$$1$$$ in $$$4$$$ coins, $$$3$$$ to $$$1$$$ in $$$2$$$ coins, $$$0$$$ to $$$1$$$ in $$$1$$$ coin.
900
false
false
true
true
false
false
false
false
false
false
4,662
567C
Polycarp loves geometric progressions very much. Since he was only three years old, he loves only the progressions of length three. He also has a favorite integer _k_ and a sequence _a_, consisting of _n_ integers. He wants to know how many subsequences of length three can be selected from _a_, so that they form a geometric progression with common ratio _k_. A subsequence of length three is a combination of three such indexes _i_1,u2009_i_2,u2009_i_3, that 1u2009≤u2009_i_1u2009<u2009_i_2u2009<u2009_i_3u2009≤u2009_n_. That is, a subsequence of length three are such groups of three elements that are not necessarily consecutive in the sequence, but their indexes are strictly increasing. A geometric progression with common ratio _k_ is a sequence of numbers of the form _b_·_k_0,u2009_b_·_k_1,u2009...,u2009_b_·_k__r_u2009-u20091. Polycarp is only three years old, so he can not calculate this number himself. Help him to do it. Input The first line of the input contains two integers, _n_ and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u20092·105), showing how many numbers Polycarp's sequence has and his favorite number. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009109u2009≤u2009_a__i_u2009≤u2009109) — elements of the sequence. Output Output a single number — the number of ways to choose a subsequence of length three, such that it forms a geometric progression with a common ratio _k_. Examples Input 10 3 1 2 6 2 3 6 9 18 3 9 Note In the first sample test the answer is four, as any of the two 1s can be chosen as the first element, the second element can be any of the 2s, and the third element of the subsequence must be equal to 4.
1,700
false
false
false
true
true
false
false
true
false
false
7,582
35E
No Great Victory anniversary in Berland has ever passed without the war parade. This year is not an exception. That’s why the preparations are on in full strength. Tanks are building a line, artillery mounts are ready to fire, soldiers are marching on the main square... And the air forces general Mr. Generalov is in trouble again. This year a lot of sky-scrapers have been built which makes it difficult for the airplanes to fly above the city. It was decided that the planes should fly strictly from south to north. Moreover, there must be no sky scraper on a plane’s route, otherwise the anniversary will become a tragedy. The Ministry of Building gave the data on _n_ sky scrapers (the rest of the buildings are rather small and will not be a problem to the planes). When looking at the city from south to north as a geometrical plane, the _i_-th building is a rectangle of height _h__i_. Its westernmost point has the x-coordinate of _l__i_ and the easternmost — of _r__i_. The terrain of the area is plain so that all the buildings stand on one level. Your task as the Ministry of Defence’s head programmer is to find an enveloping polyline using the data on the sky-scrapers. The polyline’s properties are as follows: If you look at the city from south to north as a plane, then any part of any building will be inside or on the boarder of the area that the polyline encloses together with the land surface. The polyline starts and ends on the land level, i.e. at the height equal to 0. The segments of the polyline are parallel to the coordinate axes, i.e. they can only be vertical or horizontal. The polyline’s vertices should have integer coordinates. If you look at the city from south to north the polyline (together with the land surface) must enclose the minimum possible area. The polyline must have the smallest length among all the polylines, enclosing the minimum possible area with the land. The consecutive segments of the polyline must be perpendicular. Picture to the second sample test (the enveloping polyline is marked on the right). Input The first input line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009100000). Then follow _n_ lines, each containing three integers _h__i_, _l__i_, _r__i_ (1u2009≤u2009_h__i_u2009≤u2009109,u2009u2009-u2009109u2009≤u2009_l__i_u2009<u2009_r__i_u2009≤u2009109). Output In the first line output integer _m_ — amount of vertices of the enveloping polyline. The next _m_ lines should contain 2 integers each — the position and the height of the polyline’s vertex. Output the coordinates of each vertex in the order of traversing the polyline from west to east. Remember that the first and the last vertices of the polyline should have the height of 0. Examples Output 6 0 0 0 3 1 3 1 4 3 4 3 0 Input 5 3 -3 0 2 -1 1 4 2 4 2 3 7 3 6 8 Output 14 -3 0 -3 3 0 3 0 2 1 2 1 0 2 0 2 4 4 4 4 2 6 2 6 3 8 3 8 0
2,100
false
false
false
false
true
false
false
false
true
false
9,819
1634A
Real stupidity beats artificial intelligence every time. — Terry Pratchett, Hogfather, Discworld You are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$ How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$? In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$)xa0— number of test cases. Next $$$2 cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 100$$$, $$$0 le k le 1000$$$)xa0— the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters. Output For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints. Example Input 4 3 2 aab 3 3 aab 7 1 abacaba 2 0 ab Note In the first test case of the example: After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.
800
false
true
false
false
false
false
false
false
false
false
2,446
731E
Once upon a time Petya and Gena gathered after another programming competition and decided to play some game. As they consider most modern games to be boring, they always try to invent their own games. They have only stickers and markers, but that won't stop them. The game they came up with has the following rules. Initially, there are _n_ stickers on the wall arranged in a row. Each sticker has some number written on it. Now they alternate turn, Petya moves first. One move happens as follows. Lets say there are _m_u2009≥u20092 stickers on the wall. The player, who makes the current move, picks some integer _k_ from 2 to _m_ and takes _k_ leftmost stickers (removes them from the wall). After that he makes the new sticker, puts it to the left end of the row, and writes on it the new integer, equal to the sum of all stickers he took on this move. Game ends when there is only one sticker left on the wall. The score of the player is equal to the sum of integers written on all stickers he took during all his moves. The goal of each player is to maximize the difference between his score and the score of his opponent. Given the integer _n_ and the initial sequence of stickers on the wall, define the result of the game, i.e. the difference between the Petya's and Gena's score if both players play optimally. Input The first line of input contains a single integer _n_ (2u2009≤u2009_n_u2009≤u2009200u2009000)xa0— the number of stickers, initially located on the wall. The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (u2009-u200910u2009000u2009≤u2009_a__i_u2009≤u200910u2009000)xa0— the numbers on stickers in order from left to right. Note In the first sample, the optimal move for Petya is to take all the stickers. As a result, his score will be equal to 14 and Gena's score will be equal to 0. In the second sample, the optimal sequence of moves is the following. On the first move Petya will take first three sticker and will put the new sticker with value u2009-u20098. On the second move Gena will take the remaining two stickers. The Petya's score is 1u2009+u2009(u2009-u20097)u2009+u2009(u2009-u20092)u2009=u2009u2009-u20098, Gena's score is (u2009-u20098)u2009+u20093u2009=u2009u2009-u20095, i.e. the score difference will be u2009-u20093.
2,200
false
false
false
true
false
false
false
false
false
false
6,872
1773H
This is an interactive problem. After emigrating to another country, little Hanna came across the fact that playing "Hot and cold" became more difficult: she does not understand the hint phrases! You are to write a helping program for her. The playground where the game happens is a rectangle, and the treasure is hidden in some point with integer coordinates between 0xa0andxa0$$$10^6$$$, inclusive. Hanna visits several points with valid coordinates. Ifxa0the visited point contains the hidden treasure, she receives the phrase "Found!" in the local language. Fortunately, the intonations allow her to recognize this phrasexa0— in this problem it will be denoted by an exclamation mark. Otherwise, for each visited point except for the first one, Hanna receives a phrase in the local language stating whether she is now "Closer", "Further", or "At the same distance" from the treasure, compared to the previous point. The distances are measured in Euclidean metric. After the first visited point, in case of no treasure there, Hanna receives the phrase "Not found" in the local language. Your program must help Hanna find the treasure by visiting at most 64 points. Interaction Your program should print each visited point in a line with two coordinates between 0xa0andxa0$$$10^6$$$, inclusive, and flush the output after each one. For each visited point, the testing system writes one of the phrases: "Found!", "Closer", "Further", "At the same distance", or "Not found" in some language, consistent throughout the game. The first phrase ends with an exclamation mark, all other phrases do not contain exclamation marks. All phrases contain only Latin letters, spaces and exclamation marks, have lengths between 2 and 30, inclusive, don't start or end with a space, and are pairwise distinct. The phrases are not necessarily coming from any real human language. After receiving a phrase with an exclamation mark, your program must halt without printing anything else. Visiting points more than once is allowed, and each such visit counts towards the limit of 64 queries. Examples Input Tabilmadi Daha yakin Daha yakin Sama distanco Dalej Znaydeno! Output 500 200 560 230 566 240 566 238 30 239 566 239 Input You are lucky today!
2,600
false
false
false
false
false
false
false
true
false
false
1,649
188B
Problem - 188B - Codeforces =============== xa0 , separated by a single space. The numbers are given without leading zeros. Output Output the sum of _a_ and reverse of _b_. Examples Input 5 15 Output 56 Input 73 9180 Output 892
1,300
false
false
true
false
false
false
false
false
false
false
9,091
1423H
In Bubbleland a group of special programming forces gets a top secret job to calculate the number of potentially infected people by a new unknown virus. The state has a population of $$$n$$$ people and every day there is new information about new contacts between people. The job of special programming forces is to calculate how many contacts in the last $$$k$$$ days a given person had. The new virus has an incubation period of $$$k$$$ days, and after that time people consider as non-infectious. Because the new virus is an extremely dangerous, government mark as suspicious everybody who had direct or indirect contact in the last $$$k$$$ days, independently of the order of contacts. This virus is very strange, and people can't get durable immunity. You need to help special programming forces to calculate the number of suspicious people for a given person (number of people who had contact with a given person). There are 3 given inputs on beginning $$$n$$$ where $$$n$$$ is population, $$$q$$$ number of queries, $$$k$$$ virus incubation time in days. Each query is one of three types: 1. ($$$x$$$, $$$y$$$) person $$$x$$$ and person $$$y$$$ met that day ($$$x eq y$$$). 2. ($$$z$$$) return the number of people in contact with $$$z$$$, counting himself. 3. The end of the current day moves on to the next day. Input The first line of input contains three integers $$$n$$$ ($$$1 le nle 10^5$$$) the number of people in the state, $$$q$$$ ($$$1 le qle 5×10^5$$$) number of queries and $$$k$$$ ($$$1 le kle 10^5$$$) virus incubation time in days. Each of the next $$$q$$$ lines starts with an integer $$$t$$$ ($$$1 le tle 3$$$) the type of the query. A pair of integers $$$x$$$ and $$$y$$$ ($$$1 le x, y le n$$$) follows in the query of the first type ($$$x eq y$$$). An integer $$$i$$$ ($$$1 le ile n$$$) follows in the query of the second type. Query of third type does not have the following number. Output For the queries of the second type print on a separate line the current number of people in contact with a given person. Examples Input 5 12 1 1 1 2 1 1 3 1 3 4 2 4 2 5 3 2 1 1 1 2 1 3 2 2 1 3 2 1 Input 5 12 2 1 1 2 1 1 3 1 3 4 2 4 2 5 3 2 1 1 1 2 1 3 2 2 1 3 2 1 Input 10 25 2 1 9 3 2 5 1 1 3 1 3 1 2 2 1 8 3 1 5 6 3 1 9 2 1 8 3 2 9 1 3 1 2 5 1 6 4 3 3 2 4 3 1 10 9 1 1 7 3 2 2 3 1 5 6 1 1 4 Note Pay attention if persons $$$1$$$ and $$$2$$$ had contact first day and next day persons $$$1$$$ and $$$3$$$ had contact, for $$$k$$$>$$$1$$$ number of contacts of person $$$3$$$ is $$$3$$$(persons:1,2,3).
2,500
false
false
false
false
true
false
false
false
false
true
3,556
1941E
Bernard loves visiting Rudolf, but he is always running late. The problem is that Bernard has to cross the river on a ferry. Rudolf decided to help his friend solve this problem. The river is a grid of $$$n$$$ rows and $$$m$$$ columns. The intersection of the $$$i$$$-th row and the $$$j$$$-th column contains the number $$$a_{i,j}$$$xa0— the depth in the corresponding cell. All cells in the first and last columns correspond to the river banks, so the depth for them is $$$0$$$. The river may look like this. Rudolf can choose the row $$$(i,1), (i,2), ldots, (i,m)$$$ and build a bridge over it. In each cell of the row, he can install a support for the bridge. The cost of installing a support in the cell $$$(i,j)$$$ is $$$a_{i,j}+1$$$. Supports must be installed so that the following conditions are met: 1. A support must be installed in cell $$$(i,1)$$$; 2. A support must be installed in cell $$$(i,m)$$$; 3. The distance between any pair of adjacent supports must be no more than $$$d$$$. The distance between supports $$$(i, j_1)$$$ and $$$(i, j_2)$$$ is $$$j_1 - j_2 - 1$$$. Building just one bridge is boring. Therefore, Rudolf decided to build $$$k$$$ bridges on consecutive rows of the river, that is, to choose some $$$i$$$ ($$$1 le i le n - k + 1$$$) and independently build a bridge on each of the rows $$$i, i + 1, ldots, i + k - 1$$$. Help Rudolf minimize the total cost of installing supports. Input The first line contains a single integer $$$t$$$ $$$(1 le t le 10^3)$$$ — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$k$$$, and $$$d$$$ ($$$1 le k le n le 100$$$, $$$3 le m le 2 cdot 10^5$$$, $$$1 le d le m$$$) — the number of rows and columns of the field, the number of bridges, and the maximum distance between supports. Then follow $$$n$$$ lines, $$$i$$$-th line contains $$$m$$$ positive integers $$$a_{i, j}$$$ ($$$0 le a_{i, j} le 10^6$$$, $$$a_{i, 1} = a_{i, m} = 0$$$) — the depths of the river cells. It is guaranteed that the sum of $$$n cdot m$$$ for all sets of input data does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single number — the minimum total cost of supports installation. Example Input 5 3 11 1 4 0 1 2 3 4 5 4 3 2 1 0 0 1 2 3 2 1 2 3 3 2 0 0 1 2 3 5 5 5 5 5 2 0 4 4 2 1 0 3 3 0 0 2 1 0 0 1 2 0 0 3 3 0 4 5 2 5 0 1 1 1 0 0 2 2 2 0 0 2 1 1 0 0 3 2 1 0 1 8 1 1 0 10 4 8 4 4 2 0 4 5 3 2 0 8 4 4 0 0 3 4 8 0 0 8 1 10 0 0 10 1 5 0 Note In the first test case, it is most profitable to build a bridge on the second row. It is not a top view, but side view: gray cellsxa0— bridge itself, white cells are empty, black cellsxa0— supports, blue cellsxa0— water, brown cellsxa0— river bottom. In the second test case, it is most profitable to build bridges on the second and third rows. The supports will be placed in cells $$$(2, 3)$$$, $$$(3, 2)$$$, and on the river banks. In the third test case the supports can be placed along the river banks.
1,600
false
false
false
true
true
false
false
true
false
false
635
1673F
This is an interactive problem. A city has $$$n^2$$$ buildings divided into a grid of $$$n$$$ rows and $$$n$$$ columns. You need to build a road of some length $$$D(A,B)$$$ of your choice between each pair of adjacent by side buildings $$$A$$$ and $$$B$$$. Due to budget limitations and legal restrictions, the length of each road must be a positive integer and the total length of all roads should not exceed $$$48,000$$$. There is a thief in the city who will start from the topmost, leftmost building (in the first row and the first column) and roam around the city, occasionally stealing artifacts from some of the buildings. He can move from one building to another adjacent building by travelling through the road which connects them. You are unable to track down what buildings he visits and what path he follows to reach them. But there is one tracking mechanism in the city. The tracker is capable of storing a single integer $$$x$$$ which is initially $$$0$$$. Each time the thief travels from a building $$$A$$$ to another adjacent building $$$B$$$ through a road of length $$$D(A,B)$$$, the tracker changes $$$x$$$ to $$$xoplus D(A,B)$$$. Each time the thief steals from a building, the tracker reports the value $$$x$$$ stored in it and resets it back to $$$0$$$. It is known beforehand that the thief will steal in exactly $$$k$$$ buildings but you will know the values returned by the tracker only after the thefts actually happen. Your task is to choose the lengths of roads in such a way that no matter what strategy or routes the thief follows, you will be able to exactly tell the location of all the buildings where the thefts occurred from the values returned by the tracker. Interaction First read a single line containing two integers $$$n$$$ $$$(2leq nleq 32)$$$ and $$$k$$$ $$$(1leq kleq 1024)$$$ denoting the number of rows and number of thefts respectively. Let's denote the $$$j$$$-th building in the $$$i$$$-th row by $$$B_{i,j}$$$. Then print $$$n$$$ lines each containing $$$n-1$$$ integers. The $$$j$$$-th integer of the $$$i$$$-th line must be the value of $$$D(B_{i,j},B_{i,j+1})$$$. Then print $$$n-1$$$ lines each containing $$$n$$$ integers. The $$$j$$$-th integer of the $$$i$$$-th line must be the value of $$$D(B_{i,j},B_{i+1,j})$$$. Remember that the total length of the roads must not exceed $$$48,000$$$. Then answer $$$k$$$ queries. First read the value $$$x$$$ returned by the tracker. Then print two integers denoting the row number and column number of the building where the theft occurred. After that you will be able to answer the next query (if such exists). After printing the answers do not forget to output end of line and flush the output buffer. Otherwise you will get the verdict Idleness limit exceeded. To flush the buffer, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; Read documentation for other languages. Hacks You cannot make hacks in this problem. Example Output 1 8 2 4 1 2 1 1 1 2 2 1 Note For the sample test, $$$n=2$$$ and $$$k=4$$$. You choose to build the roads of the following lengths: The thief follows the following strategy: 1. Start at $$$B_{1,1}$$$. 2. Move Right to $$$B_{1,2}$$$. 3. Move Down to $$$B_{2,2}$$$. 4. Move Left to $$$B_{2,1}$$$. 5. Move Up to $$$B_{1,1}$$$. 6. Move Right to $$$B_{1,2}$$$. 7. Steal from $$$B_{1,2}$$$. 8. Move Left to $$$B_{1,1}$$$. 9. Steal from $$$B_{1,1}$$$. 10. Move Down to $$$B_{2,1}$$$. 11. Move Right to $$$B_{2,2}$$$. 12. Move Up to $$$B_{1,2}$$$. 13. Steal from $$$B_{1,2}$$$. 14. Move Left to $$$B_{1,1}$$$. 15. Move Down to $$$B_{2,1}$$$. 16. Steal from $$$B_{2,1}$$$. The tracker responds in the following way: 1. Initialize $$$x=0$$$. 2. Change $$$x$$$ to $$$xoplus 1=0oplus1=1$$$. 3. Change $$$x$$$ to $$$xoplus 4=1oplus4=5$$$. 4. Change $$$x$$$ to $$$xoplus 8=5oplus8=13$$$. 5. Change $$$x$$$ to $$$xoplus 2=13oplus2=15$$$. 6. Change $$$x$$$ to $$$xoplus 1=15oplus1=14$$$. 7. Return $$$x=14$$$ and re-initialize $$$x=0$$$. 8. Change $$$x$$$ to $$$xoplus 1=0oplus1=1$$$. 9. Return $$$x=1$$$ and re-initialize $$$x=0$$$. 10. Change $$$x$$$ to $$$xoplus 2=0oplus2=2$$$. 11. Change $$$x$$$ to $$$xoplus 8=2oplus8=10$$$. 12. Change $$$x$$$ to $$$xoplus 4=10oplus4=14$$$. 13. Return $$$x=14$$$ and re-initialize $$$x=0$$$. 14. Change $$$x$$$ to $$$xoplus 1=0oplus1=1$$$. 15. Change $$$x$$$ to $$$xoplus 2=1oplus2=3$$$. 16. Return $$$x=3$$$ and re-initialize $$$x=0$$$.
2,400
true
true
false
false
false
true
false
false
false
false
2,230
200A
The capital of Berland has the only movie theater in the country. Besides, it consists of only one room. The room is divided into _n_ rows, each row consists of _m_ seats. There are _k_ people lined up to the box office, each person wants to buy exactly one ticket for his own entertainment. Before the box office started selling tickets, each person found the seat that seemed best for him and remembered it as a pair of coordinates (_x__i_,u2009_y__i_), where _x__i_ is the row number, and _y__i_ is the seat number in this row. It is possible that some people have chosen the same place, then when some people see their favorite seat taken in the plan of empty seats in the theater, they choose and buy a ticket to another place. Each of them has the following logic: let's assume that he originally wanted to buy a ticket to seat (_x_1,u2009_y_1), then when he comes to the box office, he chooses such empty seat (_x_2,u2009_y_2), which satisfies the following conditions: the value of _x_1u2009-u2009_x_2u2009+u2009_y_1u2009-u2009_y_2 is minimum if the choice is not unique, then among the seats that satisfy the first condition, this person selects the one for which the value of _x_2 is minimum if the choice is still not unique, among the seats that satisfy the first and second conditions, this person selects the one for which the value of _y_2 is minimum Your task is to find the coordinates of a seat for each person. Input The first input line contains three integers _n_, _m_, _k_ (1u2009≤u2009_n_,u2009_m_u2009≤u20092000, 1u2009≤u2009_k_u2009≤u2009_min_(_n_·_m_,u2009105) — the number of rows in the room, the number of seats in each row and the number of people in the line, correspondingly. Each of the next _k_ lines contains two integers _x__i_, _y__i_ (1u2009≤u2009_x__i_u2009≤u2009_n_, 1u2009≤u2009_y__i_u2009≤u2009_m_) — the coordinates of the seat each person has chosen. Numbers on the same line are separated by a space. The pairs of coordinates are located in the order, in which people stand in the line, starting from the head (the first person in the line who stands in front of the box office) to the tail (the last person in the line). Output Print _k_ lines, each containing a pair of integers. Print on the _i_-th line _x__i_,u2009_y__i_ — the coordinates of the seat, for which the person who stands _i_-th in the line will buy the ticket. Examples Input 3 4 6 1 1 1 1 1 1 1 2 1 3 1 3 Input 4 3 12 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 Output 2 2 1 2 2 1 2 3 3 2 1 1 1 3 3 1 3 3 4 2 4 1 4 3
2,400
false
false
false
false
true
false
true
false
false
false
9,044
1795C
A tea manufacturer decided to conduct a massive tea tasting. $$$n$$$ sorts of tea will be tasted by $$$n$$$ tasters. Both the sorts of tea and the tasters are numbered from $$$1$$$ to $$$n$$$. The manufacturer prepared $$$a_i$$$ milliliters of the $$$i$$$-th sort of tea. The $$$j$$$-th taster can drink $$$b_j$$$ milliliters of tea at once. The tasting will be conducted in steps. During the first step, the $$$i$$$-th taster tastes the $$$i$$$-th sort of tea. The $$$i$$$-th taster drinks $$$min(a_i, b_i)$$$ tea (how much is available of the $$$i$$$-th sort and how much the $$$i$$$-th taster can drink). $$$a_i$$$ also decreases by this amount. Then all tasters move to the previous sort of tea. Thus, during the second step, the $$$i$$$-th taster tastes the $$$(i-1)$$$-st sort of tea. The $$$i$$$-th taster drinks $$$min(a_{i-1}, b_i)$$$ tea. The $$$1$$$-st person ends the tasting. During the third step, the $$$i$$$-th taster tastes the $$$(i-2)$$$-nd sort of tea. The $$$2$$$-nd taster ends the tasting. This goes on until everyone ends the tasting. Take a look at the tasting process for $$$n = 3$$$, $$$a = [10, 20, 15]$$$, $$$b = [9, 8, 6]$$$. In the left row, there are the current amounts of each sort of tea. In the right column, there are current amounts of tea each taster has drunk in total. The arrow tells which taster each tea goes to on the current step. The number on the arrow is the amountxa0— minimum of how much is available of the sort of tea and how much the taster can drink. For each taster, print how many milliliters of tea he/she will drink in total. 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 single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the number of sorts of tea and the number of tasters. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the amount of each sort of tea. The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le 10^9$$$)xa0— the amount of tea each taster can drink at once. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print $$$n$$$ integersxa0— the $$$i$$$-th value should be equal to the total amount of tea the $$$i$$$-th taster will drink. Example Input 4 3 10 20 15 9 8 6 1 5 7 4 13 8 5 4 3 4 2 1 3 1000000000 1000000000 1000000000 1 1 1000000000 Output 9 9 12 5 3 8 6 4 1 2 2999999997 Note The first testcase is described in the statement. Here are the remaining amounts of each sort of tea after each step and the total amount of tea each taster has drunk: $$$a = [1, 12, 9]$$$, $$$mathit{ans} = [9, 8, 6]$$$ $$$a = [0, 6, 9]$$$, $$$mathit{ans} = [9, 9, 12]$$$ $$$a = [0, 6, 9]$$$, $$$mathit{ans} = [9, 9, 12]$$$ In the second testcase, the only taster drinks $$$min(5, 7)$$$ milliliters of tea of the only sort. Here are the remaining amounts of each sort of tea after each step and the total amount of tea each taster has drunk for the third testcase: $$$a = [10, 4, 3, 3]$$$, $$$mathit{ans} = [3, 4, 2, 1]$$$; $$$a = [6, 2, 2, 3]$$$, $$$mathit{ans} = [3, 8, 4, 2]$$$; $$$a = [4, 1, 2, 3]$$$, $$$mathit{ans} = [3, 8, 6, 3]$$$; $$$a = [3, 1, 2, 3]$$$, $$$mathit{ans} = [3, 8, 6, 4]$$$. Here are the remaining amounts of each sort of tea after each step and the total amount of tea each taster has drunk for the fourth testcase: $$$a = [999999999, 999999999, 0]$$$, $$$mathit{ans} = [1, 1, 1000000000]$$$; $$$a = [999999998, 0, 0]$$$, $$$mathit{ans} = [1, 2, 1999999999]$$$; $$$a = [0, 0, 0]$$$, $$$mathit{ans} = [1, 2, 2999999997]$$$.
1,500
false
false
true
false
true
false
false
true
false
false
1,507
1101D
You are given a tree consisting of $$$n$$$ vertices. A number is written on each vertex; the number on vertex $$$i$$$ is equal to $$$a_i$$$. Let's denote the function $$$g(x, y)$$$ as the greatest common divisor of the numbers written on the vertices belonging to the simple path from vertex $$$x$$$ to vertex $$$y$$$ (including these two vertices). Also let's denote $$$dist(x, y)$$$ as the number of vertices on the simple path between vertices $$$x$$$ and $$$y$$$, including the endpoints. $$$dist(x, x) = 1$$$ for every vertex $$$x$$$. Your task is calculate the maximum value of $$$dist(x, y)$$$ among such pairs of vertices that $$$g(x, y) > 1$$$. Input The first line contains one integer $$$n$$$ — the number of vertices $$$(1 le n le 2 cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 le a_i le 2 cdot 10^5)$$$ — the numbers written on vertices. Then $$$n - 1$$$ lines follow, each containing two integers $$$x$$$ and $$$y$$$ $$$(1 le x, y le n, x e y)$$$ denoting an edge connecting vertex $$$x$$$ with vertex $$$y$$$. It is guaranteed that these edges form a tree. Output If there is no pair of vertices $$$x, y$$$ such that $$$g(x, y) > 1$$$, print $$$0$$$. Otherwise print the maximum value of $$$dist(x, y)$$$ among such pairs.
2,000
false
false
false
true
true
false
false
false
false
false
5,197
1320B
The map of Bertown can be represented as a set of $$$n$$$ intersections, numbered from $$$1$$$ to $$$n$$$ and connected by $$$m$$$ one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection $$$v$$$ to another intersection $$$u$$$ is the path that starts in $$$v$$$, ends in $$$u$$$ and has the minimum length among all such paths. Polycarp lives near the intersection $$$s$$$ and works in a building near the intersection $$$t$$$. Every day he gets from $$$s$$$ to $$$t$$$ by car. Today he has chosen the following path to his workplace: $$$p_1$$$, $$$p_2$$$, ..., $$$p_k$$$, where $$$p_1 = s$$$, $$$p_k = t$$$, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from $$$s$$$ to $$$t$$$. Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection $$$s$$$, the system chooses some shortest path from $$$s$$$ to $$$t$$$ and shows it to Polycarp. Let's denote the next intersection in the chosen path as $$$v$$$. If Polycarp chooses to drive along the road from $$$s$$$ to $$$v$$$, then the navigator shows him the same shortest path (obviously, starting from $$$v$$$ as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection $$$w$$$ instead, the navigator rebuilds the path: as soon as Polycarp arrives at $$$w$$$, the navigation system chooses some shortest path from $$$w$$$ to $$$t$$$ and shows it to Polycarp. The same process continues until Polycarp arrives at $$$t$$$: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules. Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path $$$[1, 2, 3, 4]$$$ ($$$s = 1$$$, $$$t = 4$$$): Check the picture by the link [ When Polycarp starts at $$$1$$$, the system chooses some shortest path from $$$1$$$ to $$$4$$$. There is only one such path, it is $$$[1, 5, 4]$$$; 2. Polycarp chooses to drive to $$$2$$$, which is not along the path chosen by the system. When Polycarp arrives at $$$2$$$, the navigator rebuilds the path by choosing some shortest path from $$$2$$$ to $$$4$$$, for example, $$$[2, 6, 4]$$$ (note that it could choose $$$[2, 3, 4]$$$); 3. Polycarp chooses to drive to $$$3$$$, which is not along the path chosen by the system. When Polycarp arrives at $$$3$$$, the navigator rebuilds the path by choosing the only shortest path from $$$3$$$ to $$$4$$$, which is $$$[3, 4]$$$; 4. Polycarp arrives at $$$4$$$ along the road chosen by the navigator, so the system does not have to rebuild anything. Overall, we get $$$2$$$ rebuilds in this scenario. Note that if the system chose $$$[2, 3, 4]$$$ instead of $$$[2, 6, 4]$$$ during the second step, there would be only $$$1$$$ rebuild (since Polycarp goes along the path, so the system maintains the path $$$[3, 4]$$$ during the third step). The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey? Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le m le 2 cdot 10^5$$$) — the number of intersections and one-way roads in Bertown, respectively. Then $$$m$$$ lines follow, each describing a road. Each line contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u e v$$$) denoting a road from intersection $$$u$$$ to intersection $$$v$$$. All roads in Bertown are pairwise distinct, which means that each ordered pair $$$(u, v)$$$ appears at most once in these $$$m$$$ lines (but if there is a road $$$(u, v)$$$, the road $$$(v, u)$$$ can also appear). The following line contains one integer $$$k$$$ ($$$2 le k le n$$$) — the number of intersections in Polycarp's path from home to his workplace. The last line contains $$$k$$$ integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_k$$$ ($$$1 le p_i le n$$$, all these integers are pairwise distinct) — the intersections along Polycarp's path in the order he arrived at them. $$$p_1$$$ is the intersection where Polycarp lives ($$$s = p_1$$$), and $$$p_k$$$ is the intersection where Polycarp's workplace is situated ($$$t = p_k$$$). It is guaranteed that for every $$$i in [1, k - 1]$$$ the road from $$$p_i$$$ to $$$p_{i + 1}$$$ exists, so the path goes along the roads of Bertown.
1,700
false
false
false
false
false
false
false
false
false
true
4,114
700B
Treeland is a country in which there are _n_ towns connected by _n_u2009-u20091 two-way road such that it's possible to get from any town to any other town. In Treeland there are 2_k_ universities which are located in different towns. Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done! To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in _k_ pairs should be as large as possible. Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1. Input The first line of the input contains two integers _n_ and _k_ (2u2009≤u2009_n_u2009≤u2009200u2009000, 1u2009≤u2009_k_u2009≤u2009_n_u2009/u20092)xa0— the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to _n_. The second line contains 2_k_ distinct integers _u_1,u2009_u_2,u2009...,u2009_u_2_k_ (1u2009≤u2009_u__i_u2009≤u2009_n_)xa0— indices of towns in which universities are located. The next _n_u2009-u20091 line contains the description of roads. Each line contains the pair of integers _x__j_ and _y__j_ (1u2009≤u2009_x__j_,u2009_y__j_u2009≤u2009_n_), which means that the _j_-th road connects towns _x__j_ and _y__j_. All of them are two-way roads. You can move from any town to any other using only these roads. Output Print the maximum possible sum of distances in the division of universities into _k_ pairs. Examples Input 7 2 1 5 6 2 1 3 3 2 4 5 3 7 4 3 4 6 Input 9 3 3 2 1 6 5 9 8 9 3 2 2 7 3 4 7 6 4 5 2 1 2 8 Note The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example.
1,800
false
false
false
true
false
false
false
false
false
true
7,024
162A
Problem - 162A - Codeforces =============== xa0 u2009/u20092 (always integer). You are given _n_; calculate _n_-th pentagonal number. Input The only line of input contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009100). Output Output the _n_-th pentagonal number. Examples Input 2 Output 5 Input 5 Output 35
1,100
false
false
true
false
false
false
false
false
false
false
9,225
868A
As technologies develop, manufacturers are making the process of unlocking a phone as user-friendly as possible. To unlock its new phone, Arkady's pet dog Mu-mu has to bark the password once. The phone represents a password as a string of two lowercase English letters. Mu-mu's enemy Kashtanka wants to unlock Mu-mu's phone to steal some sensible information, but it can only bark _n_ distinct words, each of which can be represented as a string of two lowercase English letters. Kashtanka wants to bark several words (not necessarily distinct) one after another to pronounce a string containing the password as a substring. Tell if it's possible to unlock the phone in this way, or not. Input The first line contains two lowercase English lettersxa0— the password on the phone. The second line contains single integer _n_ (1u2009≤u2009_n_u2009≤u2009100)xa0— the number of words Kashtanka knows. The next _n_ lines contain two lowercase English letters each, representing the words Kashtanka knows. The words are guaranteed to be distinct. Output Print "YES" if Kashtanka can bark several words in a line forming a string containing the password, and "NO" otherwise. You can print each letter in arbitrary case (upper or lower). Note In the first example the password is "ya", and Kashtanka can bark "oy" and then "ah", and then "ha" to form the string "oyahha" which contains the password. So, the answer is "YES". In the second example Kashtanka can't produce a string containing password as a substring. Note that it can bark "ht" and then "tp" producing "http", but it doesn't contain the password "hp" as a substring. In the third example the string "hahahaha" contains "ah" as a substring.
900
false
false
true
false
false
false
true
false
false
false
6,273