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
1804G
Raj has a single physical network line that connects his office to the Internet. This line bandwidth is $$$b$$$ bytes per millisecond. There are $$$n$$$ users who would like to use this network line to transmit some data. The $$$i$$$-th of them will use the line from millisecond $$$s_i$$$ to millisecond $$$f_i$$$ inclusive. His initial data rate will be set to $$$d_i$$$. That means he will use data rate equal to $$$d_i$$$ for millisecond $$$s_i$$$, and then it will change according to the procedure described below. The flow control will happen as follows. Suppose there are $$$m$$$ users trying to transmit some data via the given network line during millisecond $$$x$$$. Denote as $$$t_i$$$ the data rate that the $$$i$$$-th of these $$$m$$$ users has at the beginning of this millisecond. All $$$t_i$$$ are non-negative integer values. 1. If $$$m = 0$$$, i.xa0e. there are no users trying to transmit data during this millisecond, nothing happens. 2. If the sum of all $$$t_i$$$ is less than or equal to $$$b$$$, each active user successfully completes his transmission (the $$$i$$$-th active user transmits $$$t_i$$$ bytes). After that, the data rate of each active user grows by $$$1$$$, i.xa0e. each $$$t_i$$$ is increased by $$$1$$$. 3. If the sum of all $$$t_i$$$ is greater than $$$b$$$, the congestion occurs and no data transmissions succeed this millisecond at all. If that happens, each $$$t_i$$$ decreases twice, i.xa0e. each $$$t_i$$$ is replaced with $$$lfloor frac{t_i}{2} floor$$$. Raj knows all the values $$$n$$$, $$$b$$$, $$$s_i$$$, $$$f_i$$$, and $$$d_i$$$, he wants to calculate the total number of bytes transmitted by all the users in the aggregate. Input The first line of the input contains two integers $$$n$$$ and $$$b$$$ ($$$1 leq n leq 2 cdot 10^5$$$, $$$1 leq b leq 10^9$$$), the number of users who will use the line and the line bandwidth, respectively. Each of the following $$$n$$$ lines contains three integers $$$s_i$$$, $$$f_i$$$ and $$$d_i$$$ ($$$1 leq s_i leq f_i leq 10^9$$$, $$$1 leq d_i leq 10^9$$$), denoting that the $$$i$$$-th user will try to transmit data during each millisecond between $$$s_i$$$ and $$$f_i$$$ inclusive, and the initial data rate of the $$$i$$$-th user. Output Print one integerxa0— the total number of bytes all users will successfully transmit. Examples Input 3 10 1 100 1 30 60 20 40 80 6 Note Consider the first example. Millisecond $$$1$$$: User $$$1$$$ transmits $$$2$$$ bytes. Millisecond $$$2$$$: User $$$1$$$ transmits $$$3$$$ bytes. Millisecond $$$3$$$: Congestion occurs, and no user transmits data. Millisecond $$$4$$$: User $$$1$$$ transmits $$$2$$$ bytes. Millisecond $$$5$$$: User $$$1$$$ transmits $$$3$$$ bytes. In the second example, at each millisecond from the $$$7$$$-th to the $$$11$$$-th inclusive, congestion occurs, and the only user decreases their rate twice. However, they don't decrease the speed enough before disconnecting. Consider the third example. Millisecond $$$1$$$: User $$$1$$$ transmits $$$1$$$ bytes. Millisecond $$$2$$$: User $$$1$$$ transmits $$$2$$$ bytes. Millisecond $$$3$$$: User $$$1$$$ transmits $$$3$$$ bytes. Millisecond $$$4$$$: User $$$1$$$ transmits $$$4$$$ bytes. Millisecond $$$5$$$: User $$$1$$$ transmits $$$5$$$ bytes. Millisecond $$$6$$$: User $$$1$$$ transmits $$$6$$$ bytes. Millisecond $$$7$$$: Congestion occurs, and no user transmits data. Millisecond $$$8$$$: User $$$1$$$ transmits $$$3$$$ bytes. User $$$2$$$ transmits $$$3$$$ bytes. Millisecond $$$9$$$: Congestion occurs, and no user transmits data. Millisecond $$$10$$$: User $$$1$$$ transmits $$$2$$$ bytes. User $$$2$$$ transmits $$$2$$$ bytes. Millisecond $$$11$$$: User $$$1$$$ transmits $$$3$$$ bytes. User $$$2$$$ transmits $$$3$$$ bytes. Millisecond $$$12$$$: Congestion occurs, and no user transmits data. Millisecond $$$13$$$: User $$$2$$$ transmits $$$2$$$ bytes. Millisecond $$$14$$$: User $$$2$$$ transmits $$$3$$$ bytes. Millisecond $$$15$$$: User $$$2$$$ transmits $$$4$$$ bytes. Millisecond $$$16$$$: User $$$2$$$ transmits $$$5$$$ bytes. Millisecond $$$17$$$: User $$$2$$$ transmits $$$6$$$ bytes. Millisecond $$$18$$$: Congestion occurs, and no user transmits data. Millisecond $$$19$$$: User $$$2$$$ transmits $$$3$$$ bytes. Millisecond $$$20$$$: User $$$2$$$ transmits $$$4$$$ bytes.
3,500
false
false
true
false
true
false
false
false
false
false
1,451
1088D
This is an interactive problem! Ehab plays a game with Laggy. Ehab has 2 hidden integers $$$(a,b)$$$. Laggy can ask a pair of integers $$$(c,d)$$$ and Ehab will reply with: 1 if $$$a oplus c>b oplus d$$$. 0 if $$$a oplus c=b oplus d$$$. -1 if $$$a oplus c<b oplus d$$$. Operation $$$a oplus b$$$ is the $$$ with at most 62 questions. You'll play this game. You're Laggy and the interactor is Ehab. It's guaranteed that $$$0 le a,b<2^{30}$$$. Input See the interaction section. Output To print the answer, print "! a b" (without quotes). Don't forget to flush the output after printing the answer. Interaction To ask a question, print "? c d" (without quotes). Both $$$c$$$ and $$$d$$$ must be non-negative integers less than $$$2^{30}$$$. Don't forget to flush the output after printing any question. After each question, you should read the answer as mentioned in the legend. If the interactor replies with -2, that means you asked more than 62 queries and your program should terminate. To flush the output, you can use:- fflush(stdout) in C++. System.out.flush() in Java. stdout.flush() in Python. flush(output) in Pascal. See the documentation for other languages. Hacking: To hack someone, print the 2 space-separated integers $$$a$$$ and $$$b$$$ $$$(0 le a,b<2^{30})$$$. Example Output ? 2 1 ? 1 2 ? 2 0 ! 3 1 Note In the sample: The hidden numbers are $$$a=3$$$ and $$$b=1$$$. In the first query: $$$3 oplus 2 = 1$$$ and $$$1 oplus 1 = 0$$$, so the answer is 1. In the second query: $$$3 oplus 1 = 2$$$ and $$$1 oplus 2 = 3$$$, so the answer is -1. In the third query: $$$3 oplus 2 = 1$$$ and $$$1 oplus 0 = 1$$$, so the answer is 0. Then, we printed the answer.
2,000
false
false
true
false
false
true
false
false
false
false
5,288
1776G
After the first successful edition, Gabriella has been asked to organize a second wine tasting event. There will be $$$2n - 1$$$ bottles of wine arranged in a row, each of which is either red wine or white wine. This time, Gabriella has already chosen the type and order of all the bottles. The types of the wines are represented by a string $$$s$$$ of length $$$2n - 1$$$. For each $$$1 le i le 2n - 1$$$, it holds that $$$s_i = exttt{R}$$$ if the $$$i$$$-th bottle is red wine, and $$$s_i = exttt{W}$$$ if the $$$i$$$-th bottle is white wine. Exactly $$$n$$$ critics have been invited to attend. The critics are numbered from $$$1$$$ to $$$n$$$. Just like last year, each critic $$$j$$$ wants to taste an interval of wines, that is, the bottles at positions $$$a_j, , a_j + 1, , dots, , b_j$$$ for some $$$1 le a_j le b_j le 2n - 1$$$. Moreover, they have the following additional requirements: each of them wants to taste at least $$$n$$$ wines, that is, it must hold that $$$b_j - a_j + 1 ge n$$$; no two critics must taste exactly the same wines, that is, if $$$j e k$$$ it must hold that $$$a_j e a_k$$$ or $$$b_j e b_k$$$. Gabriella knows that, since the event is held in a coastal region of Italy, critics are especially interested in the white wines, and don't care much about the red ones. (Indeed, white wine is perfect to accompany seafood.) Thus, to ensure fairness, she would like that all critics taste the same number of white wines. Help Gabriella find an integer $$$x$$$ (with $$$0 le x le 2n - 1$$$) such that there exists a valid assignment of intervals to critics where each critic tastes exactly $$$x$$$ white wines. It can be proved that at least one such $$$x$$$ always exists. Input The first line contains the integer $$$n$$$ ($$$1 le n le 10^6$$$) — where $$$2n - 1$$$ is the number of bottles, and $$$n$$$ is the number of critics. The second line contains a string $$$s$$$ of length $$$2n - 1$$$ that represents the arrangement of the wines — the $$$i$$$-th character of $$$s$$$ ($$$1 le i le 2n - 1$$$) is $$$ exttt{R}$$$ for a red wine and $$$ exttt{W}$$$ for a white wine. Output Print an integer $$$x$$$ — the number of white wines that each critic will taste. It can be proved that at least one solution exists. If multiple solutions exist, any of them will be accepted. Note In the first sample, there are $$$5$$$ critics and $$$2 cdot 5 - 1 = 9$$$ bottles of wine. A possible set of intervals that makes each critic taste $$$2$$$ white wines is the following: $$$[2, 6],$$$ $$$[1, 6],$$$ $$$[4, 8],$$$ $$$[1, 5],$$$ $$$[3, 7]$$$. Note that all intervals contain at least $$$5$$$ bottles. In the second sample, there is $$$1$$$ critic and $$$2 cdot 1 - 1 = 1$$$ bottle of wine. The only possible interval is $$$[1, 1]$$$, which gives $$$x = 0$$$.
2,100
true
false
false
false
false
true
false
false
false
false
1,622
568D
One Khanate had a lot of roads and very little wood. Riding along the roads was inconvenient, because the roads did not have road signs indicating the direction to important cities. The Han decided that it's time to fix the issue, and ordered to put signs on every road. The Minister of Transport has to do that, but he has only _k_ signs. Help the minister to solve his problem, otherwise the poor guy can lose not only his position, but also his head. More formally, every road in the Khanate is a line on the _Oxy_ plane, given by an equation of the form _Ax_u2009+u2009_By_u2009+u2009_C_u2009=u20090 (_A_ and _B_ are not equal to 0 at the same time). You are required to determine whether you can put signs in at most _k_ points so that each road had at least one sign installed. Input The input starts with two positive integers _n_, _k_ (1u2009≤u2009_n_u2009≤u2009105,u20091u2009≤u2009_k_u2009≤u20095) Next _n_ lines contain three integers each, _A__i_,u2009_B__i_,u2009_C__i_, the coefficients of the equation that determines the road (_A__i_,u2009_B__i_,u2009_C__i_u2009≤u2009105, _A__i_2u2009+u2009_B__i_2u2009≠u20090). It is guaranteed that no two roads coincide. Output If there is no solution, print "NO" in the single line (without the quotes). Otherwise, print in the first line "YES" (without the quotes). In the second line print a single number _m_ (_m_u2009≤u2009_k_) — the number of used signs. In the next _m_ lines print the descriptions of their locations. Description of a location of one sign is two integers _v_,u2009_u_. If _u_ and _v_ are two distinct integers between 1 and _n_, we assume that sign is at the point of intersection of roads number _v_ and _u_. If _u_u2009=u2009u2009-u20091, and _v_ is an integer between 1 and _n_, then the sign is on the road number _v_ in the point not lying on any other road. In any other case the description of a sign will be assumed invalid and your answer will be considered incorrect. In case if _v_u2009=u2009_u_, or if _v_ and _u_ are the numbers of two non-intersecting roads, your answer will also be considered incorrect. The roads are numbered starting from 1 in the order in which they follow in the input. Examples Input 3 1 1 0 0 0 -1 0 7 -93 0 Note Note that you do not have to minimize _m_, but it shouldn't be more than _k_. In the first test all three roads intersect at point (0,0). In the second test all three roads form a triangle and there is no way to place one sign so that it would stand on all three roads at once.
2,800
true
false
false
false
false
false
true
false
false
false
7,575
748D
Santa Claus likes palindromes very much. There was his birthday recently. _k_ of his friends came to him to congratulate him, and each of them presented to him a string _s__i_ having the same length _n_. We denote the beauty of the _i_-th string by _a__i_. It can happen that _a__i_ is negativexa0— that means that Santa doesn't find this string beautiful at all. Santa Claus is crazy about palindromes. He is thinking about the following question: what is the maximum possible total beauty of a palindrome which can be obtained by concatenating some (possibly all) of the strings he has? Each present can be used at most once. Note that all strings have the same length _n_. Recall that a palindrome is a string that doesn't change after one reverses it. Since the empty string is a palindrome too, the answer can't be negative. Even if all _a__i_'s are negative, Santa can obtain the empty string. Input The first line contains two positive integers _k_ and _n_ divided by space and denoting the number of Santa friends and the length of every string they've presented, respectively (1u2009≤u2009_k_,u2009_n_u2009≤u2009100u2009000; _n_·_k_xa0u2009≤u2009100u2009000). _k_ lines follow. The _i_-th of them contains the string _s__i_ and its beauty _a__i_ (u2009-u200910u2009000u2009≤u2009_a__i_u2009≤u200910u2009000). The string consists of _n_ lowercase English letters, and its beauty is integer. Some of strings may coincide. Also, equal strings can have different beauties. Output In the only line print the required maximum possible beauty. Examples Input 7 3 abb 2 aaa -3 bba -1 zyz -4 abb 5 aaa 7 xyx 4 Input 2 5 abcde 10000 abcde 10000 Note In the first example Santa can obtain abbaaaxyxaaabba by concatenating strings 5, 2, 7, 6 and 3 (in this order).
2,100
false
true
false
false
true
true
false
false
false
false
6,801
842E
Nikita plays a new computer game. There are _m_ levels in this game. In the beginning of each level a new class appears in the game; this class is a child-class of the class _y__i_ (and _y__i_ is called parent-class for this new class). Thus, the classes form a tree. Initially there is only one class with index 1. Changing the class to its neighbour (child-class or parent-class) in the tree costs 1 coin. You can not change the class back. The cost of changing the class _a_ to the class _b_ is equal to the total cost of class changes on the path from _a_ to _b_ in the class tree. Suppose that at _i_xa0-th level the maximum cost of changing one class to another is _x_. For each level output the number of classes such that for each of these classes there exists some other class _y_, and the distance from this class to _y_ is exactly _x_. Input First line contains one integer number _m_xa0— number of queries (1u2009≤u2009_m_u2009≤u20093·105). Next _m_ lines contain description of queries. _i_xa0-th line (1u2009≤u2009_i_u2009≤u2009_m_) describes the _i_xa0-th level and contains an integer _y__i_xa0— the index of the parent-class of class with index _i_u2009+u20091 (1u2009≤u2009_y__i_u2009≤u2009_i_). Output Suppose that at _i_xa0-th level the maximum cost of changing one class to another is _x_. For each level output the number of classes such that for each of these classes there exists some other class _y_, and the distance from this class to _y_ is exactly _x_.
2,800
false
false
false
false
false
false
false
true
false
true
6,392
1773J
You are given an undirected connected graph with $$$n$$$ vertices and $$$m$$$ edges. Each edge has an associated counter, initially equal to $$$0$$$. In one operation, you can choose an arbitrary spanning tree and add any value $$$v$$$ to all edges of this spanning tree. Determine if it's possible to make every counter equal to its target value $$$x_i$$$ modulo prime $$$p$$$, and provide a sequence of operations that achieves it. Input The first line contains three integers $$$n$$$, $$$m$$$, and $$$p$$$xa0— the number of vertices, the number of edges, and the prime modulus ($$$1 le n le 500$$$; $$$1 le m le 1000$$$; $$$2 le p le 10^9$$$, $$$p$$$ is prime). Next $$$m$$$ lines contain three integers $$$u_i$$$, $$$v_i$$$, $$$x_i$$$ eachxa0— the two endpoints of the $$$i$$$-th edge and the target value of that edge's counter ($$$1 le u_i, v_i le n$$$; $$$0 le x_i < p$$$; $$$u_i eq v_i$$$). The graph is connected. There are no loops, but there may be multiple edges between the same two vertices. Output If the target values on counters cannot be achieved, print -1. Otherwise, print $$$t$$$xa0— the number of operations, followed by $$$t$$$ lines, describing the sequence of operations. Each line starts with integer $$$v$$$ ($$$0 le v < p$$$)xa0— the counter increment for this operation. Then, in the same line, followed by $$$n - 1$$$ integers $$$e_1$$$, $$$e_2$$$, ... $$$e_{n - 1}$$$ ($$$1 le e_i le m$$$)xa0— the edges of the spanning tree. The number of operations $$$t$$$ should not exceed $$$2m$$$. You don't need to minimize $$$t$$$. Any correct answer within the $$$2m$$$ bound is accepted. You are allowed to repeat spanning trees. Examples Input 3 3 101 1 2 30 2 3 40 3 1 50 Output 3 10 1 2 20 1 3 30 2 3 Input 5 4 5 1 3 1 2 3 2 2 5 3 4 1 4
2,900
true
false
false
false
false
true
false
false
false
false
1,647
1031D
You are given a matrix of size $$$n imes n$$$ filled with lowercase English letters. You can change no more than $$$k$$$ letters in this matrix. Consider all paths from the upper left corner to the lower right corner that move from a cell to its neighboring cell to the right or down. Each path is associated with the string that is formed by all the letters in the cells the path visits. Thus, the length of each string is $$$2n - 1$$$. Find the lexicographically smallest string that can be associated with a path after changing letters in at most $$$k$$$ cells of the matrix. A string $$$a$$$ is lexicographically smaller than a string $$$b$$$, if the first different letter in $$$a$$$ and $$$b$$$ is smaller in $$$a$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2000$$$, $$$0 le k le n^2$$$) — the size of the matrix and the number of letters you can change. Each of the next $$$n$$$ lines contains a string of $$$n$$$ lowercase English letters denoting one row of the matrix. Output Output the lexicographically smallest string that can be associated with some valid path after changing no more than $$$k$$$ letters in the matrix. Examples Input 5 3 bwwwz hrhdh sepsp sqfaf ajbvw Input 7 6 ypnxnnp pnxonpm nxanpou xnnpmud nhtdudu npmuduh pmutsnz Note In the first sample test case it is possible to change letters 'b' in cells $$$(2, 1)$$$ and $$$(3, 1)$$$ to 'a', then the minimum path contains cells $$$(1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4)$$$. The first coordinate corresponds to the row and the second coordinate corresponds to the column.
1,900
false
true
false
false
false
false
false
false
false
false
5,545
61D
Problem - 61D - 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 dfs and similar graphs greedy shortest paths trees *1800 No tag edit access → Contest materials ") — the amount of cities. Next _n_u2009-u20091 lines contain 3 integer numbers each _x__i_, _y__i_ and _w__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_,u20090u2009≤u2009_w__i_u2009≤u20092u2009×u2009104). _x__i_ and _y__i_ are two ends of a road and _w__i_ is the length of that road. Output A single integer number, the minimal length of Shapur's travel. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Examples Input 3 1 2 3 2 3 4 Output 7 Input 3 1 2 3 1 3 3 Output 9
1,800
false
true
false
false
false
false
false
false
false
true
9,667
1808A
Olympus City recently launched the production of personal starships. Now everyone on Mars can buy one and fly to other planets inexpensively. Each starship has a numberxa0—some positive integer $$$x$$$. Let's define the luckiness of a number $$$x$$$ as the difference between the largest and smallest digits of that number. For example, $$$142857$$$ has $$$8$$$ as its largest digit and $$$1$$$ as its smallest digit, so its luckiness is $$$8-1=7$$$. And the number $$$111$$$ has all digits equal to $$$1$$$, so its luckiness is zero. Hateehc is a famous Martian blogger who often flies to different corners of the solar system. To release interesting videos even faster, he decided to buy himself a starship. When he came to the store, he saw starships with numbers from $$$l$$$ to $$$r$$$ inclusively. While in the store, Hateehc wanted to find a starship with the luckiest number. Since there are a lot of starships in the store, and Hateehc can't program, you have to help the blogger and write a program that answers his question. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10,000$$$)xa0—the number of test cases. Each of the following $$$t$$$ lines contains a description of the test case. The description consists of two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le 10^6$$$)xa0— the largest and smallest numbers of the starships in the store. Output Print $$$t$$$ lines, one line for each test case, containing the luckiest starship number in the store. If there are several ways to choose the luckiest number, output any of them. Example Input 5 59 63 42 49 15 15 53 57 1 100 Note Let's look at two test examples: the luckiness of the number $$$59$$$ is $$$9 - 5 = 4$$$; the luckiness of $$$60$$$ equals $$$6 - 0 = 6$$$; the luckiness of $$$61$$$ equals $$$6 - 1 = 5$$$; the luckiness of $$$62$$$ equals $$$6 - 2 = 4$$$; the luckiness of $$$63$$$ is $$$6 - 3 = 3$$$. Thus, the luckiest number is $$$60$$$. In the fifth test example, the luckiest number is $$$90$$$.
900
false
false
true
false
false
false
true
false
false
false
1,427
1304F2
The only difference between easy and hard versions is the constraint on $$$k$$$. Gildong loves observing animals, so he bought two cameras to take videos of wild animals in a forest. The color of one camera is red, and the other one's color is blue. Gildong is going to take videos for $$$n$$$ days, starting from day $$$1$$$ to day $$$n$$$. The forest can be divided into $$$m$$$ areas, numbered from $$$1$$$ to $$$m$$$. He'll use the cameras in the following way: On every odd day ($$$1$$$-st, $$$3$$$-rd, $$$5$$$-th, ...), bring the red camera to the forest and record a video for $$$2$$$ days. On every even day ($$$2$$$-nd, $$$4$$$-th, $$$6$$$-th, ...), bring the blue camera to the forest and record a video for $$$2$$$ days. If he starts recording on the $$$n$$$-th day with one of the cameras, the camera records for only one day. Each camera can observe $$$k$$$ consecutive areas of the forest. For example, if $$$m=5$$$ and $$$k=3$$$, he can put a camera to observe one of these three ranges of areas for two days: $$$[1,3]$$$, $$$[2,4]$$$, and $$$[3,5]$$$. Gildong got information about how many animals will be seen in each area on each day. Since he would like to observe as many animals as possible, he wants you to find the best way to place the two cameras for $$$n$$$ days. Note that if the two cameras are observing the same area on the same day, the animals observed in that area are counted only once. Input The first line contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 le n le 50$$$, $$$1 le m le 2 cdot 10^4$$$, $$$1 le k le m$$$) – the number of days Gildong is going to record, the number of areas of the forest, and the range of the cameras, respectively. Next $$$n$$$ lines contain $$$m$$$ integers each. The $$$j$$$-th integer in the $$$i+1$$$-st line is the number of animals that can be seen on the $$$i$$$-th day in the $$$j$$$-th area. Each number of animals is between $$$0$$$ and $$$1000$$$, inclusive.
2,400
false
true
false
true
true
false
false
false
false
false
4,165
1492D
Problem - 1492D - 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 bitmasks constructive algorithms greedy math *1900 No tag edit access → Contest materials ") such that 1. both $$$x$$$ and $$$y$$$ consist of $$$a$$$ zeroes and $$$b$$$ ones; 2. $$$x - y$$$ (also written in binary form) has exactly $$$k$$$ ones. You are not allowed to use leading zeros for $$$x$$$ and $$$y$$$. Input The only line contains three integers $$$a$$$, $$$b$$$, and $$$k$$$ ($$$0 leq a$$$; $$$1 leq b$$$; $$$0 leq k leq a + b leq 2 cdot 10^5$$$)xa0— the number of zeroes, ones, and the number of ones in the result. Output If it's possible to find two suitable integers, print "Yes" followed by $$$x$$$ and $$$y$$$ in base-2. Otherwise print "No". If there are multiple possible answers, print any of them. Examples Input 4 2 3 Output Yes 101000 100001 Input 3 2 1 Output Yes 10100 10010 Input 3 2 5 Output No Note In the first example, $$$x = 101000_2 = 2^5 + 2^3 = 40_{10}$$$, $$$y = 100001_2 = 2^5 + 2^0 = 33_{10}$$$, $$$40_{10} - 33_{10} = 7_{10} = 2^2 + 2^1 + 2^0 = 111_{2}$$$. Hence $$$x-y$$$ has $$$3$$$ ones in base-2. In the second example, $$$x = 10100_2 = 2^4 + 2^2 = 20_{10}$$$, $$$y = 10010_2 = 2^4 + 2^1 = 18$$$, $$$x - y = 20 - 18 = 2_{10} = 10_{2}$$$. This is precisely one 1. In the third example, one may show, that it's impossible to find an answer.
1,900
true
true
false
false
false
true
false
false
false
false
3,210
316F1
Smart Beaver became interested in drawing. He draws suns. However, at some point, Smart Beaver realized that simply drawing suns is boring. So he decided to design a program that will process his drawings. You are given a picture drawn by the beaver. It will have two colors: one for the background and one for the suns in the image. Your task will be to count the number of suns in the image and for each of them to count the number of rays. Sun is arbitrarily rotated ellipse with rays. Ray is a segment which connects point on boundary of the ellipse with some point outside ellipse. An image where all suns are circles. An image where all suns are ellipses, their axes are parallel to the coordinate axes. An image where all suns are rotated ellipses. It is guaranteed that: No two suns have common points. The rays’ width is 3 pixels. The lengths of the ellipsis suns’ axes will lie between 40 and 200 pixels. No two rays intersect. The lengths of all rays will lie between 10 and 30 pixels. Input The first line contains two integers _h_ and _w_ — the height and width of the image (1u2009≤u2009_h_,u2009_w_u2009≤u20091600). Next _h_ lines will contain _w_ space-separated integers each. They describe Smart Beaver’s picture. Each number equals either a 0 (the image background), or a 1 (the sun color). The input limits for scoring 30 points are (subproblem F1): All suns on the image are circles. The input limits for scoring 70 points are (subproblems F1+F2): All suns on the image are ellipses with axes parallel to the coordinate axes. The input limits for scoring 100 points are (subproblems F1+F2+F3): All suns on the image are ellipses, they can be arbitrarily rotated. Output The first line must contain a single number _k_ — the number of suns on the beaver’s image. The second line must contain exactly _k_ space-separated integers, corresponding to the number of rays on each sun. The numbers of the second line must be sorted in the increasing order. Examples Note For each complexity level you are suggested a sample in the initial data. You can download the samples at
2,200
false
false
true
false
false
false
false
false
false
false
8,571
1647E
After the most stunning success with the fifth-graders, Madoka has been trusted with teaching the sixth-graders. There's $$$n$$$ single-place desks in her classroom. At the very beginning Madoka decided that the student number $$$b_i$$$ ($$$1 le b_i le n$$$) will sit at the desk number $$$i$$$. Also there's an infinite line of students with numbers $$$n + 1, n + 2, n + 3, ldots$$$ waiting at the door with the hope of being able to learn something from the Madoka herself. Pay attention that each student has his unique number. After each lesson, the following happens in sequence. 1. The student sitting at the desk $$$i$$$ moves to the desk $$$p_i$$$. All students move simultaneously. 2. If there is more than one student at a desk, the student with the lowest number keeps the place, and the others are removed from the class forever. 3. For all empty desks in ascending order, the student from the lowest number from the outside line occupies the desk. Note that in the end there is exactly one student at each desk again. It is guaranteed that the numbers $$$p$$$ are such that at least one student is removed after each lesson. Check out the explanation to the first example for a better understanding. After several (possibly, zero) lessons the desk $$$i$$$ is occupied by student $$$a_i$$$. Given the values $$$a_1, a_2, ldots, a_n$$$ and $$$p_1, p_2, ldots, p_n$$$, find the lexicographically smallest suitable initial seating permutation $$$b_1, b_2, ldots, b_n$$$. The permutation is an array of $$$n$$$ different integers from $$$1$$$ up to $$$n$$$ in any order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not ($$$2$$$ occurs twice). $$$[1,3,4]$$$ is not a permutation either ($$$n=3$$$ but there's $$$4$$$ in the array). For two different permutations $$$a$$$ and $$$b$$$ of the same length, $$$a$$$ is lexicographically less than $$$b$$$ if in the first position where $$$a$$$ and $$$b$$$ differ, the permutation $$$a$$$ has a smaller element than the corresponding element in $$$b$$$. Input The first line of input data contains an integer $$$n$$$ ($$$2 le n le 10^5$$$)xa0— a number of desks in the classroom. The second line contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 leq p_i leq n$$$)xa0— desks where the students move. It is guaranteed that $$$p$$$ has at least two equal elements. The third line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the final seating of the students. It is guaranteed that there is an initial permutation from which the seating $$$a$$$ can be obtained. Output In the only line print $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le n$$$)xa0— lexicographically minimum permutation describing the initial seating of the sixth-graders that can lead to the final seating $$$a$$$. Note The description of the first test is below: The first picture shows the starting permutation, which is the answer. Then the students sitting at desks $$$1, 2$$$ are transferred to a $$$5$$$ desk. Also, a $$$1$$$ student moved from a $$$5$$$ desk, and a student from a $$$4$$$ disk is transferred to a $$$3$$$ desk. Thus, after all these transfers permutation shown in the second image is obtained. Then, at the desk with the number $$$5$$$, the student with the number $$$3$$$ is expelled, and at the desk with the number $$$3$$$, the student with the number $$$5$$$ is expelled. (Since their numbers are not the smallest) Then new students with numbers $$$6, 7$$$ sit at desks numbered $$$2, 4$$$. And this permutation (after the end of the first lesson) is shown in the third image. The $$$4$$$ image shows the seating arrangement, after the second lesson before all the extra ones were kicked out. And the fifth shows the final seating after $$$2$$$ lesson.
2,500
false
true
false
false
true
false
false
false
false
false
2,385
1624G
Recently, Vlad has been carried away by spanning trees, so his friends, without hesitation, gave him a connected weighted undirected graph of $$$n$$$ vertices and $$$m$$$ edges for his birthday. Vlad defined the ority of a spanning tree as the — the number of test cases in the input. An empty line is written in front of each test case. This is followed by two numbers $$$n$$$ and $$$m$$$ ($$$3 le n le 2 cdot 10^5, n - 1 le m le 2 cdot 10^5$$$) — the number of vertices and edges of the graph, respectively. The next $$$m$$$ lines contain the description of the edges. Line $$$i$$$ contains three numbers $$$v_i$$$, $$$u_i$$$ and $$$w_i$$$ ($$$1 le v_i, u_i le n$$$, $$$1 le w_i le 10^9$$$, $$$v_i eq u_i$$$) — the vertices that the edge connects and its weight. It is guaranteed that the sum $$$m$$$ and the sum $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$ and each test case contains a connected graph. Output Print $$$t$$$ lines, each of which contains the answer to the corresponding set of input dataxa0— the minimum possible spanning tree ority. Example Input 3 3 3 1 2 1 2 3 2 1 3 2 5 7 4 2 7 2 5 8 3 4 2 3 2 1 2 4 2 4 1 2 1 2 2 3 4 1 2 1 2 3 2 1 3 3 3 1 4
1,900
false
true
false
false
false
false
false
false
false
true
2,494
156D
As Sherlock Holmes was investigating another crime, he found a certain number of clues. Also, he has already found direct links between some of those clues. The direct links between the clues are mutual. That is, the direct link between clues _A_ and _B_ and the direct link between clues _B_ and _A_ is the same thing. No more than one direct link can exist between two clues. Of course Sherlock is able to find direct links between all clues. But it will take too much time and the criminals can use this extra time to hide. To solve the crime, Sherlock needs each clue to be linked to all other clues (maybe not directly, via some other clues). Clues _A_ and _B_ are considered linked either if there is a direct link between them or if there is a direct link between _A_ and some other clue _C_ which is linked to _B_. Sherlock Holmes counted the minimum number of additional direct links that he needs to find to solve the crime. As it turns out, it equals _T_. Please count the number of different ways to find exactly _T_ direct links between the clues so that the crime is solved in the end. Two ways to find direct links are considered different if there exist two clues which have a direct link in one way and do not have a direct link in the other way. As the number of different ways can turn out rather big, print it modulo _k_. Input The first line contains three space-separated integers _n_,u2009_m_,u2009_k_ (1u2009≤u2009_n_u2009≤u2009105,u20090u2009≤u2009_m_u2009≤u2009105, 1u2009≤u2009_k_u2009≤u2009109) — the number of clues, the number of direct clue links that Holmes has already found and the divisor for the modulo operation. Each of next _m_ lines contains two integers _a_ and _b_ (1u2009≤u2009_a_,u2009_b_u2009≤u2009_n_,u2009_a_u2009≠u2009_b_), that represent a direct link between clues. It is guaranteed that any two clues are linked by no more than one direct link. Note that the direct links between the clues are mutual. Output Print the single number — the answer to the problem modulo _k_. Note The first sample only has two clues and Sherlock hasn't found any direct link between them yet. The only way to solve the crime is to find the link. The second sample has three clues and Sherlock hasn't found any direct links between them. He has to find two of three possible direct links between clues to solve the crime — there are 3 ways to do it. The third sample has four clues and the detective has already found one direct link between the first and the fourth clue. There are 8 ways to find two remaining clues to solve the crime.
2,500
false
false
false
false
false
false
false
false
false
true
9,249
1538E
Polycarp came up with a new programming language. There are only two types of statements in it: "x := s": assign the variable named x the value s (where s is a string). For example, the statement var := hello assigns the variable named var the value hello. Note that s is the value of a string, not the name of a variable. Between the variable name, the := operator and the string contains exactly one space each. "x = a + b": assign the variable named x the concatenation of values of two variables a and b. For example, if the program consists of three statements a := hello, b := world, c = a + b, then the variable c will contain the string helloworld. It is guaranteed that the program is correct and the variables a and b were previously defined. There is exactly one space between the variable names and the = and + operators. All variable names and strings only consist of lowercase letters of the English alphabet and do not exceed $$$5$$$ characters. The result of the program is the number of occurrences of string haha in the string that was written to the variable in the last statement. Polycarp was very tired while inventing that language. He asks you to implement it. Your task isxa0— for given program statements calculate the number of occurrences of string haha in the last assigned variable. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^3$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 50$$$)xa0— the number of statements in the program. All variable names and strings are guaranteed to consist only of lowercase letters of the English alphabet and do not exceed $$$5$$$ characters. This is followed by $$$n$$$ lines describing the statements in the format described above. It is guaranteed that the program is correct. Output For each set of input data, output the number of occurrences of the haha substring in the string that was written to the variable in the last statement. Example Input 4 6 a := h b := aha c = a + b c = c + c e = c + c d = a + c 15 x := haha x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x x = x + x 1 haha := hah 5 haahh := aaaha ahhhh = haahh + haahh haahh = haahh + haahh ahhhh = ahhhh + haahh ahhaa = haahh + ahhhh Note In the first test case the resulting value of d is hhahahaha.
2,100
false
false
true
false
true
false
false
false
false
false
2,951
940B
Problem - 940B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp greedy *1400 No tag edit access → Contest materials ") ") . The second line contains a single integer _k_ (1u2009≤u2009_k_u2009≤u20092·109). The third line contains a single integer _A_ (1u2009≤u2009_A_u2009≤u20092·109). The fourth line contains a single integer _B_ (1u2009≤u2009_B_u2009≤u20092·109). Output Output a single integerxa0— the minimum amount of coins you have to pay to make _x_ equal to 1. Examples Input 9 2 3 1 Output 6 Input 5 5 2 20 Output 8 Input 19 3 4 2 Output 12 Note In the first testcase, the optimal strategy is as follows: Subtract 1 from _x_ (9u2009→u20098) paying 3 coins. Divide _x_ by 2 (8u2009→u20094) paying 1 coin. Divide _x_ by 2 (4u2009→u20092) paying 1 coin. Divide _x_ by 2 (2u2009→u20091) paying 1 coin. The total cost is 6 coins. In the second test case the optimal strategy is to subtract 1 from _x_ 4 times paying 8 coins in total.
1,400
false
true
false
true
false
false
false
false
false
false
5,937
2019A
You are given an array $$$a_1, a_2, ldots, a_n$$$ of positive integers. You can color some elements of the array red, but there cannot be two adjacent red elements (i.e., for $$$1 leq i leq n-1$$$, at least one of $$$a_i$$$ and $$$a_{i+1}$$$ must not be red). Your score is the maximum value of a red element plus the number of red elements. Find the maximum score you can get. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$)xa0— the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 1000$$$)xa0— the given array. Note In the first test case, you can color the array as follows: $$$[color{red}{5}, 4, color{red}{5}]$$$. Your score is $$$max([5, 5]) + ext{size}([5, 5]) = 5+2 = 7$$$. This is the maximum score you can get. In the second test case, you can color the array as follows: $$$[color{red}{4}, 5, color{red}{4}]$$$. Your score is $$$max([4, 4]) + ext{size}([4, 4]) = 4+2 = 6$$$. This is the maximum score you can get. In the third test case, you can color the array as follows: $$$[color{red}{3}, 3, color{red}{3}, 3, color{red}{4}, 1, 2, color{red}{3}, 4, color{red}{5}]$$$. Your score is $$$max([3, 3, 4, 3, 5]) + ext{size}([3, 3, 4, 3, 5]) = 5+5 = 10$$$. This is the maximum score you can get.
800
false
true
false
true
false
false
true
false
false
false
154
1535D
$$$2^k$$$ teams participate in a playoff tournament. The tournament consists of $$$2^k - 1$$$ games. They are held as follows: first of all, the teams are split into pairs: team $$$1$$$ plays against team $$$2$$$, team $$$3$$$ plays against team $$$4$$$ (exactly in this order), and so on (so, $$$2^{k-1}$$$ games are played in that phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only $$$2^{k-1}$$$ teams remain. If only one team remains, it is declared the champion; otherwise, $$$2^{k-2}$$$ games are played: in the first one of them, the winner of the game "$$$1$$$ vs $$$2$$$" plays against the winner of the game "$$$3$$$ vs $$$4$$$", then the winner of the game "$$$5$$$ vs $$$6$$$" plays against the winner of the game "$$$7$$$ vs $$$8$$$", and so on. This process repeats until only one team remains. For example, this picture describes the chronological order of games with $$$k = 3$$$: Let the string $$$s$$$ consisting of $$$2^k - 1$$$ characters describe the results of the games in chronological order as follows: if $$$s_i$$$ is 0, then the team with lower index wins the $$$i$$$-th game; if $$$s_i$$$ is 1, then the team with greater index wins the $$$i$$$-th game; if $$$s_i$$$ is ?, then the result of the $$$i$$$-th game is unknown (any team could win this game). Let $$$f(s)$$$ be the number of possible winners of the tournament described by the string $$$s$$$. A team $$$i$$$ is a possible winner of the tournament if it is possible to replace every ? with either 1 or 0 in such a way that team $$$i$$$ is the champion. You are given the initial state of the string $$$s$$$. You have to process $$$q$$$ queries of the following form: $$$p$$$ $$$c$$$xa0— replace $$$s_p$$$ with character $$$c$$$, and print $$$f(s)$$$ as the result of the query. Input The first line contains one integer $$$k$$$ ($$$1 le k le 18$$$). The second line contains a string consisting of $$$2^k - 1$$$ charactersxa0— the initial state of the string $$$s$$$. Each character is either ?, 0, or 1. The third line contains one integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$)xa0— the number of queries. Then $$$q$$$ lines follow, the $$$i$$$-th line contains an integer $$$p$$$ and a character $$$c$$$ ($$$1 le p le 2^k - 1$$$; $$$c$$$ is either ?, 0, or 1), describing the $$$i$$$-th query.
1,800
false
false
true
true
true
false
false
false
false
false
2,971
107B
As a German University in Cairo (GUC) student and a basketball player, Herr Wafa was delighted once he heard the news. GUC is finally participating in the Annual Basketball Competition (ABC). A team is to be formed of _n_ players, all of which are GUC students. However, the team might have players belonging to different departments. There are _m_ departments in GUC, numbered from 1 to _m_. Herr Wafa's department has number _h_. For each department _i_, Herr Wafa knows number _s__i_ — how many students who play basketball belong to this department. Herr Wafa was also able to guarantee a spot on the team, using his special powers. But since he hates floating-point numbers, he needs your help at finding the probability that he will have at least one teammate belonging to his department. Note that every possible team containing Herr Wafa is equally probable. Consider all the students different from each other. Input The first line contains three integers _n_, _m_ and _h_ (1u2009≤u2009_n_u2009≤u2009100,u20091u2009≤u2009_m_u2009≤u20091000,u20091u2009≤u2009_h_u2009≤u2009_m_) — the number of players on the team, the number of departments in GUC and Herr Wafa's department, correspondingly. The second line contains a single-space-separated list of _m_ integers _s__i_ (1u2009≤u2009_s__i_u2009≤u2009100), denoting the number of students in the _i_-th department. Note that _s__h_ includes Herr Wafa. Output Print the probability that Herr Wafa will have at least one teammate from his department. If there is not enough basketball players in GUC to participate in ABC, print -1. The answer will be accepted if it has absolute or relative error not exceeding 10u2009-u20096. Note In the first example all 3 players (2 from department 1 and 1 from department 2) must be chosen for the team. Both players from Wafa's departments will be chosen, so he's guaranteed to have a teammate from his department. In the second example, there are not enough players. In the third example, there are three possibilities to compose the team containing Herr Wafa. In two of them the other player from Herr Wafa's department is part of the team.
1,600
true
false
false
true
false
false
false
false
false
false
9,455
1679E
Today is a holiday in the residence hallxa0— Oleh arrived, in honor of which the girls gave him a string. Oleh liked the gift a lot, so he immediately thought up and offered you, his best friend, the following problem. You are given a string $$$s$$$ of length $$$n$$$, which consists of the first $$$17$$$ lowercase Latin letters {$$$a$$$, $$$b$$$, $$$c$$$, $$$ldots$$$, $$$p$$$, $$$q$$$} and question marks. And $$$q$$$ queries. Each query is defined by a set of pairwise distinct lowercase first $$$17$$$ letters of the Latin alphabet, which can be used to replace the question marks in the string $$$s$$$. The answer to the query is the sum of the number of distinct substrings that are palindromes over all strings that can be obtained from the original string $$$s$$$ by replacing question marks with available characters. The answer must be calculated modulo $$$998,244,353$$$. Pay attention! Two substrings are different when their start and end positions in the string are different. So, the number of different substrings that are palindromes for the string aba will be $$$4$$$: a, b, a, aba. Consider examples of replacing question marks with letters. For example, from the string aba??ee when querying {$$$a$$$, $$$b$$$} you can get the strings ababaee or abaaaee but you cannot get the strings pizza, abaee, abacaba, aba?fee, aba47ee, or abatree. Recall that a palindrome is a string that reads the same from left to right as from right to left. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 1,000$$$)xa0— the length of the string $$$s$$$. The second line contains the string $$$s$$$, which consists of $$$n$$$ lowercase Latin letters and question marks. It is guaranteed that all letters in the string belong to the set {$$$a$$$, $$$b$$$, $$$c$$$, $$$ldots$$$, $$$p$$$, $$$q$$$}. The third line contains a single integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$)xa0— the number of queries. This is followed by $$$q$$$ lines, each containing a single line $$$t$$$xa0— a set of characters that can replace question marks ($$$1 le t le 17$$$). It is guaranteed that all letters in the string belong to the set {$$$a$$$, $$$b$$$, $$$c$$$, $$$ldots$$$, $$$p$$$, $$$q$$$} and occur at most once. Output For each query print one numberxa0— the total numbers of palindromic substrings in all strings that can be obtained from the string $$$s$$$, modulo $$$998,244,353$$$. Note Consider the first example and the first query in it. We can get only one string as a result of replacing the question marks xa0— abaaaba. It has the following palindrome substrings: 1. a xa0— substring [$$$1$$$; $$$1$$$]. 2. b xa0— substring [$$$2$$$; $$$2$$$]. 3. a xa0— substring [$$$3$$$; $$$3$$$]. 4. a xa0— substring [$$$4$$$; $$$4$$$]. 5. a xa0— substring [$$$5$$$; $$$5$$$]. 6. b xa0— substring [$$$6$$$; $$$6$$$]. 7. a xa0— substring [$$$7$$$; $$$7$$$]. 8. aa xa0— substring [$$$3$$$; $$$4$$$]. 9. aa xa0— substring [$$$4$$$; $$$5$$$]. 10. aba xa0— substring [$$$1$$$; $$$3$$$]. 11. aaa xa0— substring [$$$3$$$; $$$5$$$]. 12. aba xa0— substring [$$$5$$$; $$$7$$$]. 13. baaab xa0— substring [$$$2$$$; $$$6$$$]. 14. abaaaba xa0— substring [$$$1$$$; $$$7$$$]. In the third request, we get 4 lines: abaaaba, abababa, abbaaba, abbbaba.
2,400
false
false
false
true
false
false
false
false
false
false
2,193
1340D
Denis came to Nastya and discovered that she was not happy to see him... There is only one chance that she can become happy. Denis wants to buy all things that Nastya likes so she will certainly agree to talk to him. The map of the city where they live has a lot of squares, some of which are connected by roads. There is exactly one way between each pair of squares which does not visit any vertex twice. It turns out that the graph of the city is a tree. Denis is located at vertex $$$1$$$ at the time $$$0$$$. He wants to visit every vertex at least once and get back as soon as possible. Denis can walk one road in $$$1$$$ time. Unfortunately, the city is so large that it will take a very long time to visit all squares. Therefore, Denis took a desperate step. He pulled out his pocket time machine, which he constructed in his basement. With its help, Denis can change the time to any non-negative time, which is less than the current time. But the time machine has one feature. If the hero finds himself in the same place and at the same time twice, there will be an explosion of universal proportions and Nastya will stay unhappy. Therefore, Denis asks you to find him a route using a time machine that he will get around all squares and will return to the first and at the same time the maximum time in which he visited any square will be minimal. Formally, Denis's route can be represented as a sequence of pairs: $$${v_1, t_1}, {v_2, t_2}, {v_3, t_3}, ldots, {v_k, t_k}$$$, where $$$v_i$$$ is number of square, and $$$t_i$$$ is time in which the boy is now. The following conditions must be met: The route starts on square $$$1$$$ at time $$$0$$$, i.e. $$$v_1 = 1, t_1 = 0$$$ and ends on the square $$$1$$$, i.e. $$$v_k = 1$$$. All transitions are divided into two types: 1. Being in the square change the time: $$${ v_i, t_i } o { v_{i+1}, t_{i+1} } : v_{i+1} = v_i, 0 leq t_{i+1} < t_i$$$. 2. Walk along one of the roads: $$${ v_i, t_i } o { v_{i+1}, t_{i+1} }$$$. Herewith, $$$v_i$$$ and $$$v_{i+1}$$$ are connected by road, and $$$t_{i+1} = t_i + 1$$$ All pairs $$${ v_i, t_i }$$$ must be different. All squares are among $$$v_1, v_2, ldots, v_k$$$. You need to find a route such that the maximum time in any square will be minimal, that is, the route for which $$$max{(t_1, t_2, ldots, t_k)}$$$ will be the minimum possible. Input The first line contains a single integer $$$n$$$ $$$(1 leq n leq 10^5)$$$ xa0— the number of squares in the city. The next $$$n - 1$$$ lines contain two integers $$$u$$$ and $$$v$$$ $$$(1 leq v, u leq n, u eq v)$$$ - the numbers of the squares connected by the road. It is guaranteed that the given graph is a tree. Output In the first line output the integer $$$k$$$ $$$(1 leq k leq 10^6)$$$ xa0— the length of the path of Denis. In the next $$$k$$$ lines output pairs $$$v_i, t_i$$$ xa0— pairs that describe Denis's route (as in the statement). All route requirements described in the statements must be met. It is guaranteed that under given restrictions there is at least one route and an answer whose length does not exceed $$$10^6$$$. If there are several possible answers, print any.
2,600
false
false
false
false
false
true
false
false
false
true
4,004
1646B
$$$ defmyred#1{color{red}{underline{bf{#1}}}} defmyblue#1{color{blue}{overline{bf{#1}}}} $$$ $$$defRED{myred{Red}} defBLUE{myblue{Blue}}$$$ You are given a sequence of $$$n$$$ non-negative integers $$$a_1, a_2, ldots, a_n$$$. Initially, all the elements of the sequence are unpainted. You can paint each number $$$RED$$$ or $$$BLUE$$$ (but not both), or leave it unpainted. For a color $$$c$$$, $$$ ext{Count}(c)$$$ is the number of elements in the sequence painted with that color and $$$ ext{Sum}(c)$$$ is the sum of the elements in the sequence painted with that color. For example, if the given sequence is $$$[2, 8, 6, 3, 1]$$$ and it is painted this way: $$$[myblue{2}, 8, myred{6}, myblue{3}, 1]$$$ (where $$$6$$$ is painted red, $$$2$$$ and $$$3$$$ are painted blue, $$$1$$$ and $$$8$$$ are unpainted) then $$$ ext{Sum}(RED)=6$$$, $$$ ext{Sum}(BLUE)=2+3=5$$$, $$$ ext{Count}(RED)=1$$$, and $$$ ext{Count}(BLUE)=2$$$. Determine if it is possible to paint the sequence so that $$$ ext{Sum}(RED) > ext{Sum}(BLUE)$$$ and $$$ ext{Count}(RED) < ext{Count}(BLUE)$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$3le nle 2cdot 10^5$$$)xa0— the length of the given sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$0le a_ile 10^9$$$)xa0— the given sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, print YES if it is possible to paint the given sequence satisfying the above requirements, and NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response). Example Input 4 3 1 2 3 5 2 8 6 3 1 4 3 5 4 2 5 1000000000 1000000000 1000000000 1000000000 1000000000 Note In the first test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[myblue{1},myblue{2},myred{3}]$$$ (where $$$3$$$ is painted red, $$$1$$$ and $$$2$$$ are painted blue) then $$$ ext{Count}(RED)=1 < ext{Count}(BLUE)=2$$$, but $$$ ext{Sum}(RED)=3 gtr ext{Sum}(BLUE)=3$$$. So, this is not a possible way to paint the sequence. In the second test case, a possible way to paint the sequence is described in the statement. We can see that $$$ ext{Sum}(RED)=6 > ext{Sum}(BLUE)=5$$$ and $$$ ext{Count}(RED)=1 < ext{Count}(BLUE)=2$$$. In the third test case, there is no possible way to paint the sequence. For example, if you paint the sequence this way: $$$[myred{3},myred{5},myblue{4}, myblue{2}]$$$ (where $$$3$$$ and $$$5$$$ are painted red, $$$4$$$ and $$$2$$$ are painted blue) then $$$ ext{Sum}(RED) = 8 > ext{Sum}(BLUE) = 6$$$ but $$$ ext{Count}(RED) = 2 less ext{Count}(BLUE) = 2$$$. So, this is not a possible way to paint the sequence. In the fourth test case, it can be proven that there is no possible way to paint the sequence satisfying sum and count constraints.
800
false
true
false
false
false
true
true
false
true
false
2,394
1278F
Problem - 1278F - 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 dp math number theory probabilities *2600 No tag edit access → Contest materials . Output Print one integer — the expected value of $$$x^k$$$, taken modulo $$$998244353$$$ (the answer can always be represented as an irreducible fraction $$$frac{a}{b}$$$, where $$$b mod 998244353 e 0$$$; you have to print $$$a cdot b^{-1} mod 998244353$$$). Examples Input 1 1 1 Output 1 Input 1 1 5000 Output 1 Input 2 2 2 Output 499122178 Input 998244352 1337 5000 Output 326459680
2,600
true
false
false
true
false
false
false
false
false
false
4,301
1061A
Problem - 1061A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy implementation math *800 No tag edit access → Contest materials ") ") Output Print exactly one integerxa0— the minimum number of coins required to obtain sum $$$S$$$. Examples Input 5 11 Output 3 Input 6 16 Output 3 Note In the first example, some of the possible ways to get sum $$$11$$$ with $$$3$$$ coins are: $$$(3, 4, 4)$$$ $$$(2, 4, 5)$$$ $$$(1, 5, 5)$$$ $$$(3, 3, 5)$$$ It is impossible to get sum $$$11$$$ with less than $$$3$$$ coins. In the second example, some of the possible ways to get sum $$$16$$$ with $$$3$$$ coins are: $$$(5, 5, 6)$$$ $$$(4, 6, 6)$$$ It is impossible to get sum $$$16$$$ with less than $$$3$$$ coins.
800
true
true
true
false
false
false
false
false
false
false
5,408
1070L
Berland has $$$n$$$ cities, some of which are connected by roads. Each road is bidirectional, connects two distinct cities and for each two cities there's at most one road connecting them. The president of Berland decided to split country into $$$r$$$ states in such a way that each city will belong to exactly one of these $$$r$$$ states. After this split each road will connect either cities of the same state or cities of the different states. Let's call roads that connect two cities of the same state "inner" roads. The president doesn't like odd people, odd cities and odd numbers, so he wants this split to be done in such a way that each city would have even number of "inner" roads connected to it. Please help president to find smallest possible $$$r$$$ for which such a split exists. Input The input contains one or several test cases. The first input line contains a single integer number $$$t$$$ — number of test cases. Then, $$$t$$$ test cases follow. Solve test cases separately, test cases are completely independent and do not affect each other. Then $$$t$$$ blocks of input data follow. Each block starts from empty line which separates it from the remaining input data. The second line of each block contains two space-separated integers $$$n$$$, $$$m$$$ ($$$1 le n le 2000$$$, $$$0 le m le 10000$$$) — the number of cities and number of roads in the Berland. Each of the next $$$m$$$ lines contains two space-separated integers — $$$x_i$$$, $$$y_i$$$ ($$$1 le x_i, y_i le n$$$; $$$x_i e y_i$$$), which denotes that the $$$i$$$-th road connects cities $$$x_i$$$ and $$$y_i$$$. Each pair of cities are connected by at most one road. Sum of values $$$n$$$ across all test cases doesn't exceed $$$2000$$$. Sum of values $$$m$$$ across all test cases doesn't exceed $$$10000$$$. Output For each test case first print a line containing a single integer $$$r$$$ — smallest possible number of states for which required split is possible. In the next line print $$$n$$$ space-separated integers in range from $$$1$$$ to $$$r$$$, inclusive, where the $$$j$$$-th number denotes number of state for the $$$j$$$-th city. If there are multiple solutions, print any. Example Input 25 3 1 2 2 5 1 5 6 5 1 2 2 3 3 4 4 2 4 1 Output 1 1 1 1 1 1 2 2 1 1 1 1 1
2,600
false
false
false
false
false
true
false
false
false
false
5,356
232B
John Doe has an _n_u2009×u2009_m_ table. John Doe can paint points in some table cells, not more than one point in one table cell. John Doe wants to use such operations to make each square subtable of size _n_u2009×u2009_n_ have exactly _k_ points. John Doe wondered, how many distinct ways to fill the table with points are there, provided that the condition must hold. As this number can be rather large, John Doe asks to find its remainder after dividing by 1000000007 (109u2009+u20097). You should assume that John always paints a point exactly in the center of some cell. Two ways to fill a table are considered distinct, if there exists a table cell, that has a point in one way and doesn't have it in the other. Input A single line contains space-separated integers _n_, _m_, _k_ (1u2009≤u2009_n_u2009≤u2009100;xa0_n_u2009≤u2009_m_u2009≤u20091018;xa00u2009≤u2009_k_u2009≤u2009_n_2) — the number of rows of the table, the number of columns of the table and the number of points each square must contain. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Output In a single line print a single integer — the remainder from dividing the described number of ways by 1000000007 (109u2009+u20097). Note Let's consider the first test case: The gray area belongs to both 5u2009×u20095 squares. So, if it has one point, then there shouldn't be points in any other place. If one of the white areas has a point, then the other one also must have a point. Thus, there are about 20 variants, where the point lies in the gray area and 25 variants, where each of the white areas contains a point. Overall there are 45 variants.
1,900
true
false
false
true
false
false
false
false
false
false
8,910
425D
Problem - 425D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search data structures hashing *2300 No tag edit access → Contest materials . Each of the next _n_ lines contains two integers _x__i_,u2009_y__i_ (0u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009105), the integers represent the coordinates of the _i_-th point. It is guaranteed that all the given points are distinct. Output In a single line print the required number of squares. Examples Input 5 0 0 0 2 2 0 2 2 1 1 Output 1 Input 9 0 0 1 1 2 2 0 1 1 0 0 2 2 0 1 2 2 1 Output 5
2,300
false
false
false
false
true
false
false
true
false
false
8,135
1316D
Nash designed an interesting yet simple board game where a player is simply required to follow instructions written on the cell where the player currently stands. This board game is played on the $$$n imes n$$$ board. Rows and columns of this board are numbered from $$$1$$$ to $$$n$$$. The cell on the intersection of the $$$r$$$-th row and $$$c$$$-th column is denoted by $$$(r, c)$$$. Some cells on the board are called blocked zones. On each cell of the board, there is written one of the following $$$5$$$ characters xa0— $$$U$$$, $$$D$$$, $$$L$$$, $$$R$$$ or $$$X$$$ xa0— instructions for the player. Suppose that the current cell is $$$(r, c)$$$. If the character is $$$R$$$, the player should move to the right cell $$$(r, c+1)$$$, for $$$L$$$ the player should move to the left cell $$$(r, c-1)$$$, for $$$U$$$ the player should move to the top cell $$$(r-1, c)$$$, for $$$D$$$ the player should move to the bottom cell $$$(r+1, c)$$$. Finally, if the character in the cell is $$$X$$$, then this cell is the blocked zone. The player should remain in this cell (the game for him isn't very interesting from now on). It is guaranteed that the characters are written in a way that the player will never have to step outside of the board, no matter at which cell he starts. As a player starts from a cell, he moves according to the character in the current cell. The player keeps moving until he lands in a blocked zone. It is also possible that the player will keep moving infinitely long. For every of the $$$n^2$$$ cells of the board Alice, your friend, wants to know, how will the game go, if the player starts in this cell. For each starting cell of the board, she writes down the cell that the player stops at, or that the player never stops at all. She gives you the information she has written: for each cell $$$(r, c)$$$ she wrote: a pair ($$$x$$$,$$$y$$$), meaning if a player had started at $$$(r, c)$$$, he would end up at cell ($$$x$$$,$$$y$$$). or a pair ($$$-1$$$,$$$-1$$$), meaning if a player had started at $$$(r, c)$$$, he would keep moving infinitely long and would never enter the blocked zone. It might be possible that Alice is trying to fool you and there's no possible grid that satisfies all the constraints Alice gave you. For the given information Alice provided you, you are required to decipher a possible board, or to determine that such a board doesn't exist. If there exist several different boards that satisfy the provided information, you can find any of them. Input The first line of the input contains a single integer $$$n$$$ ($$$1 leq n leq 10^{3}$$$) xa0— the side of the board. The $$$i$$$-th of the next $$$n$$$ lines of the input contains $$$2n$$$ integers $$$x_1, y_1, x_2, y_2, dots, x_n, y_n$$$, where $$$(x_j, y_j)$$$ ($$$1 leq x_j leq n, 1 leq y_j leq n$$$, or $$$(x_j,y_j)=(-1,-1)$$$) is the pair written by Alice for the cell $$$(i, j)$$$. Output If there doesn't exist a board satisfying the information that Alice gave you, print a single line containing INVALID. Otherwise, in the first line print VALID. In the $$$i$$$-th of the next $$$n$$$ lines, print the string of $$$n$$$ characters, corresponding to the characters in the $$$i$$$-th row of the suitable board you found. Each character of a string can either be $$$U$$$, $$$D$$$, $$$L$$$, $$$R$$$ or $$$X$$$. If there exist several different boards that satisfy the provided information, you can find any of them. Note For the sample test 1 : The given grid in output is a valid one. If the player starts at $$$(1,1)$$$, he doesn't move any further following $$$X$$$ and stops there. If the player starts at $$$(1,2)$$$, he moves to left following $$$L$$$ and stops at $$$(1,1)$$$. If the player starts at $$$(2,1)$$$, he moves to right following $$$R$$$ and stops at $$$(2,2)$$$. If the player starts at $$$(2,2)$$$, he doesn't move any further following $$$X$$$ and stops there. The simulation can be seen below : For the sample test 2 : The given grid in output is a valid one, as a player starting at any cell other than the one at center $$$(2,2)$$$, keeps moving in an infinitely long cycle and never stops. Had he started at $$$(2,2)$$$, he wouldn't have moved further following instruction $$$X$$$ . The simulation can be seen below :
2,000
false
false
true
false
false
true
false
false
false
true
4,118
1896H1
This is the easy version of the problem. The only difference between the two versions is the constraint on $$$k$$$. You can make hacks only if all versions of the problem are solved. In this statement, all strings are $$$0$$$-indexed. For two strings $$$a$$$, $$$b$$$ of the same length $$$p$$$, we define the following definitions: The hamming distance between $$$a$$$ and $$$b$$$, denoted as $$$h(a, b)$$$, is defined as the number of positions $$$i$$$ such that $$$0 le i < p$$$ and $$$a_i e b_i$$$. $$$b$$$ is a cyclic shift of $$$a$$$ if there exists some $$$0 leq k < p$$$ such that $$$b_{(i+k) bmod p} = a_i$$$ for all $$$0 le i < p$$$. Here $$$x bmod y$$$ denotes the remainder from dividing $$$x$$$ by $$$y$$$. You are given two binary strings $$$s$$$ and $$$t$$$ of length $$$2^{k+1}$$$ each. Both strings may contain missing characters (denoted by the character '?'). Your task is to count the number of ways to replace the missing characters in both strings with the characters '0' or '1' such that: Each string $$$s$$$ and $$$t$$$ contains exactly $$$2^k$$$ occurrences of each character '0' and '1' $$$h(s, c) ge 2^k$$$ for all strings $$$c$$$ that is a cyclic shift of $$$t$$$. As the result can be very large, you should print the value modulo $$$998,244,353$$$. Input The first line of the input contains a single integer $$$k$$$ ($$$1 le k le 7$$$). The second line of the input contains string $$$s$$$ of size $$$2^{k+1}$$$, consisting of the characters '0', '1' and '?'. The third line of the input contains string $$$t$$$ of size $$$2^{k+1}$$$, consisting of the characters '0', '1' and '?'. It is guaranteed that both strings $$$s$$$ and $$$t$$$ contains no more than $$$2^k$$$ character '0' or '1'. Output Print a single integerxa0— the answer to the problem modulo $$$998,244,353$$$. Examples Input 5 0101010101010101010101010101010101010101010101010101010101010101 ???????????????????????????????????????????????????????????????? Note In the first example, we can check that the condition $$$h(s, c) ge 2^k$$$ for all cyclic shift $$$c$$$ of $$$t$$$ is satisfied. In particular: for $$$c = mathtt{0101}$$$, $$$h(s, c) = h(mathtt{0110}, mathtt{0101}) = 2 ge 2^1$$$; for $$$c = mathtt{1010}$$$, $$$h(s, c) = h(mathtt{0110}, mathtt{1010}) = 2 ge 2^1$$$. In the second example, there exists a cycle shift $$$c$$$ of $$$t$$$ such that $$$h(s, c) < 2^k$$$ (in particular, $$$c = mathtt{0011}$$$, and $$$h(s, c) = h(mathtt{0011}, mathtt{0011}) = 0$$$). In the third example, there are $$$2$$$ possible ways to recover the missing characters: $$$s = mathtt{0101}$$$, $$$t = mathtt{0110}$$$; $$$s = mathtt{0011}$$$, $$$t = mathtt{0101}$$$. In the fourth example, there are $$$3$$$ possible ways to recover the missing characters: $$$s = mathtt{00011110}$$$, $$$t = mathtt{01010101}$$$; $$$s = mathtt{00011011}$$$, $$$t = mathtt{01010101}$$$; $$$s = mathtt{00001111}$$$, $$$t = mathtt{01010101}$$$.
3,400
true
false
false
true
false
false
true
false
false
false
930
763E
After his birthday party, Timofey went to his favorite tree alley in a park. He wants to feed there his favorite birdsxa0— crows. It's widely known that each tree is occupied by a single crow family. The trees in the alley form a row and are numbered from 1 to _n_. Some families are friends to each other. For some reasons, two families can be friends only if they live not too far from each other, more precisely, there is no more than _k_u2009-u20091 trees between any pair of friend families. Formally, the family on the _u_-th tree and the family on the _v_-th tree can be friends only if _u_u2009-u2009_v_u2009≤u2009_k_ holds. One of the friendship features is that if some family learns that Timofey is feeding crows somewhere, it notifies about this all friend families. Thus, after Timofey starts to feed crows under some tree, all the families that are friends to the family living on this tree, as well as their friends and so on, fly to the feeding place. Of course, the family living on the tree also comes to the feeding place. Today Timofey came to the alley and noticed that all the families that live on trees with numbers strictly less than _l_ or strictly greater than _r_ have flown away. Thus, it is not possible to pass the information about feeding through them. Moreover, there is no need to feed them. Help Timofey to learn what is the minimum number of trees under which he has to feed crows so that all the families that have remained will get the information about feeding. You are given several situations, described by integers _l_ and _r_, you need to calculate the answer for all of them. Input The first line contains integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_k_u2009≤u20095), where _n_ is the number of trees, and _k_ is the maximum possible distance between friend families. The next line contains single integer _m_ (0u2009≤u2009_m_u2009≤u2009_n_·_k_)xa0— the number of pair of friend families. Each of the next _m_ lines contains two integers _u_ and _v_ (1u2009≤u2009_u_,u2009_v_u2009≤u2009105), that means that the families on trees _u_ and _v_ are friends. It is guaranteed that _u_u2009≠u2009_v_ and _u_u2009-u2009_v_u2009≤u2009_k_. All the given pairs are distinct. The next line contains single integer _q_ (1u2009≤u2009_q_u2009≤u2009105)xa0— the number of situations you need to calculate the answer in. Each of the next _q_ lines contains two integers _l_ and _r_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009105), that means that in this situation families that have flown away lived on such trees _x_, so that either _x_u2009<u2009_l_ or _x_u2009>u2009_r_. Note In the first example the following family pairs are friends: (1,u20093), (2,u20093) and (4,u20095). In the first situation only the first family has remained, so the answer is 1. In the second situation the first two families have remained, and they aren't friends, so the answer is 2. In the third situation the families 2 and 3 are friends, so it is enough to feed any of them, the answer is 1. In the fourth situation we can feed the first family, then the third family will get the information from the first family, and the second family will get the information from the third. The answer is 1. In the fifth situation we can feed the first and the fifth families, so the answer is 2.
2,900
false
false
false
false
true
false
false
false
false
false
6,733
659B
Very soon Berland will hold a School Team Programming Olympiad. From each of the _m_ Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by _n_ Berland students. There were at least two schoolboys participating from each of the _m_ regions of Berland. The result of each of the participants of the qualifying competition is an integer score from 0 to 800 inclusive. The team of each region is formed from two such members of the qualifying competition of the region, that none of them can be replaced by a schoolboy of the same region, not included in the team and who received a greater number of points. There may be a situation where a team of some region can not be formed uniquely, that is, there is more than one school team that meets the properties described above. In this case, the region needs to undertake an additional contest. The two teams in the region are considered to be different if there is at least one schoolboy who is included in one team and is not included in the other team. It is guaranteed that for each region at least two its representatives participated in the qualifying contest. Your task is, given the results of the qualifying competition, to identify the team from each region, or to announce that in this region its formation requires additional contests. Input The first line of the input contains two integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u2009100u2009000, 1u2009≤u2009_m_u2009≤u200910u2009000, _n_u2009≥u20092_m_)xa0— the number of participants of the qualifying contest and the number of regions in Berland. Next _n_ lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of length from 1 to 10 characters and consisting of large and small English letters), region number (integer from 1 to _m_) and the number of points scored by the participant (integer from 0 to 800, inclusive). It is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the _m_ regions. The surnames that only differ in letter cases, should be considered distinct. Output Print _m_ lines. On the _i_-th line print the team of the _i_-th regionxa0— the surnames of the two team members in an arbitrary order, or a single character "?" (without the quotes) if you need to spend further qualifying contests in the region. Examples Input 5 2 Ivanov 1 763 Andreev 2 800 Petrov 1 595 Sidorov 1 790 Semenov 2 503 Output Sidorov Ivanov Andreev Semenov Input 5 2 Ivanov 1 800 Andreev 2 763 Petrov 1 800 Sidorov 1 800 Semenov 2 503 Note In the first sample region teams are uniquely determined. In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: "Petrov"-"Sidorov", "Ivanov"-"Sidorov", "Ivanov" -"Petrov", so it is impossible to determine a team uniquely.
1,300
false
false
false
false
false
true
false
false
true
false
7,188
799A
In some game by Playrix it takes _t_ minutes for an oven to bake _k_ carrot cakes, all cakes are ready at the same moment _t_ minutes after they started baking. Arkady needs at least _n_ cakes to complete a task, but he currently don't have any. However, he has infinitely many ingredients and one oven. Moreover, Arkady can build one more similar oven to make the process faster, it would take _d_ minutes to build the oven. While the new oven is being built, only old one can bake cakes, after the new oven is built, both ovens bake simultaneously. Arkady can't build more than one oven. Determine if it is reasonable to build the second oven, i.e. will it decrease the minimum time needed to get _n_ cakes or not. If the time needed with the second oven is the same as with one oven, then it is unreasonable. Input The only line contains four integers _n_, _t_, _k_, _d_ (1u2009≤u2009_n_,u2009_t_,u2009_k_,u2009_d_u2009≤u20091u2009000)xa0— the number of cakes needed, the time needed for one oven to bake _k_ cakes, the number of cakes baked at the same time, the time needed to build the second oven. Output If it is reasonable to build the second oven, print "YES". Otherwise print "NO". Note In the first example it is possible to get 8 cakes in 12 minutes using one oven. The second oven can be built in 5 minutes, so after 6 minutes the first oven bakes 4 cakes, the second oven bakes 4 more ovens after 11 minutes. Thus, it is reasonable to build the second oven. In the second example it doesn't matter whether we build the second oven or not, thus it takes 12 minutes to bake 8 cakes in both cases. Thus, it is unreasonable to build the second oven. In the third example the first oven bakes 11 cakes in 3 minutes, that is more than needed 10. It is unreasonable to build the second oven, because its building takes more time that baking the needed number of cakes using the only oven.
1,100
false
false
true
false
false
false
true
false
false
false
6,576
456B
Problem - 456B - 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 number theory *1200 No tag edit access → Contest materials xa0_mod_xa05 for given value of _n_. Fedya managed to complete the task. Can you? Note that given number _n_ can be extremely large (e.g. it can exceed any integer type of your programming language). Input The single line contains a single integer _n_ (0u2009≤u2009_n_u2009≤u200910105). The number doesn't contain any leading zeroes. Output Print the value of the expression without leading zeros. Examples Input 4 Output 4 Input 124356983594583453458888889 Output 0 Note Operation _x_xa0_mod_xa0_y_ means taking remainder after division _x_ by _y_. Note to the first sample:
1,200
true
false
false
false
false
false
false
false
false
false
8,013
442C
Problem - 442C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures greedy *2500 No tag edit access → Contest materials points, where _a_ and _b_ are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points. After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20095·105) — the number of elements in the array. The next line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009106) — the values of the array elements. Output In a single line print a single integer — the maximum number of points Artem can get. Examples Input 5 3 1 5 2 6 Output 11 Input 5 1 2 3 4 5 Output 6 Input 5 1 100 101 100 1 Output 102
2,500
false
true
false
false
true
false
false
false
false
false
8,068
1182C
You are given $$$n$$$ words, each of which consists of lowercase alphabet letters. Each word contains at least one vowel. You are going to choose some of the given words and make as many beautiful lyrics as possible. Each lyric consists of two lines. Each line consists of two words separated by whitespace. A lyric is beautiful if and only if it satisfies all conditions below. The number of vowels in the first word of the first line is the same as the number of vowels in the first word of the second line. The number of vowels in the second word of the first line is the same as the number of vowels in the second word of the second line. The last vowel of the first line is the same as the last vowel of the second line. Note that there may be consonants after the vowel. Also, letters "a", "e", "o", "i", and "u" are vowels. Note that "y" is never vowel. For example of a beautiful lyric, "hello hellooowww""whatsup yowowowow" is a beautiful lyric because there are two vowels each in "hello" and "whatsup", four vowels each in "hellooowww" and "yowowowow" (keep in mind that "y" is not a vowel), and the last vowel of each line is "o". For example of a not beautiful lyric, "hey man""iam mcdic" is not a beautiful lyric because "hey" and "iam" don't have same number of vowels and the last vowels of two lines are different ("a" in the first and "i" in the second). How many beautiful lyrics can you write from given words? Note that you cannot use a word more times than it is given to you. For example, if a word is given three times, you can use it at most three times. Input The first line contains single integer $$$n$$$ ($$$1 le n le 10^{5}$$$)xa0— the number of words. The $$$i$$$-th of the next $$$n$$$ lines contains string $$$s_{i}$$$ consisting lowercase alphabet lettersxa0— the $$$i$$$-th word. It is guaranteed that the sum of the total word length is equal or less than $$$10^{6}$$$. Each word contains at least one vowel. Output In the first line, print $$$m$$$xa0— the number of maximum possible beautiful lyrics. In next $$$2m$$$ lines, print $$$m$$$ beautiful lyrics (two lines per lyric). If there are multiple answers, print any. Note In the first example, those beautiful lyrics are one of the possible answers. Let's look at the first lyric on the sample output of the first example. "about proud hooray round" forms a beautiful lyric because "about" and "hooray" have same number of vowels, "proud" and "round" have same number of vowels, and both lines have same last vowel. On the other hand, you cannot form any beautiful lyric with the word "codeforces". In the second example, you cannot form any beautiful lyric from given words. In the third example, you can use the word "same" up to three times.
1,700
false
true
false
false
true
false
false
false
false
false
4,806
189B
Problem - 189B - 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 *1300 No tag edit access → Contest materials , (_w_,u20090), (_w_,u2009_h_), (0,u2009_h_). In other words, for all vertices (_x__i_,u2009_y__i_) of the rhombus the following conditions should fulfill: 0u2009≤u2009_x__i_u2009≤u2009_w_ and 0u2009≤u2009_y__i_u2009≤u2009_h_. Its diagonals are parallel to the axis. Count the number of such rhombi. Let us remind you that a rhombus is a quadrilateral whose four sides all have the same length. Input The first line contains two integers _w_ and _h_ (1u2009≤u2009_w_,u2009_h_u2009≤u20094000) — the rectangle's sizes. Output Print a single number — the number of sought rhombi. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier. Examples Input 2 2 Output 1 Input 1 2 Output 0 Note In the first example there exists only one such rhombus. Its vertices are located at points (1,u20090), (2,u20091), (1,u20092), (0,u20091).
1,300
true
false
false
false
false
false
true
false
false
false
9,083
1734E
You are given a prime number $$$n$$$, and an array of $$$n$$$ integers $$$b_1,b_2,ldots, b_n$$$, where $$$0 leq b_i < n$$$ for each $$$1 le i leq n$$$. You have to find a matrix $$$a$$$ of size $$$n imes n$$$ such that all of the following requirements hold: $$$0 le a_{i,j} < n$$$ for all $$$1 le i, j le n$$$. $$$a_{r_1, c_1} + a_{r_2, c_2} otequiv a_{r_1, c_2} + a_{r_2, c_1} pmod n$$$ for all positive integers $$$r_1$$$, $$$r_2$$$, $$$c_1$$$, and $$$c_2$$$ such that $$$1 le r_1 < r_2 le n$$$ and $$$1 le c_1 < c_2 le n$$$. $$$a_{i,i} = b_i$$$ for all $$$1 le i leq n$$$. Here $$$x ot equiv y pmod m$$$ denotes that $$$x$$$ and $$$y$$$ give different remainders when divided by $$$m$$$. If there are multiple solutions, output any. It can be shown that such a matrix always exists under the given constraints. Input The first line contains a single positive integer $$$n$$$ ($$$2 le n < 350$$$). The second line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$0 le b_i < n$$$)xa0— the required values on the main diagonal of the matrix. It is guaranteed that $$$n$$$ is prime. Output Print $$$n$$$ lines. On the $$$i$$$-th line, print $$$n$$$ integers $$$a_{i, 1}, a_{i, 2}, ldots, a_{i, n}$$$, each separated with a space. If there are multiple solutions, output any. Examples Output 1 0 1 3 4 1 4 3 1 0 2 4 1 0 2 1 2 2 2 2 2 2 0 1 4 Note In the first example, the answer is valid because all entries are non-negative integers less than $$$n = 2$$$, and $$$a_{1,1}+a_{2,2} otequiv a_{1,2}+a_{2,1} pmod 2$$$ (because $$$a_{1,1}+a_{2,2} = 0 + 0 equiv 0 pmod 2$$$ and $$$a_{1,2}+a_{2,1} = 1 + 0 equiv 1 pmod 2 $$$). Moreover, the values on the main diagonals are equal to $$$0,0$$$ as required. In the second example, the answer is correct because all entries are non-negative integers less than $$$n = 3$$$, and the second condition is satisfied for all quadruplets $$$(r_1, r_2, c_1, c_2)$$$. For example: When $$$r_1=1$$$, $$$r_2=2$$$, $$$c_1=1$$$ and $$$c_2=2$$$, $$$a_{1,1}+a_{2,2} otequiv a_{1,2}+a_{2,1} pmod 3$$$ because $$$a_{1,1}+a_{2,2} = 1 + 1 equiv 2 pmod 3$$$ and $$$a_{1,2}+a_{2,1} = 2 + 1 equiv 0 pmod 3 $$$. When $$$r_1=2$$$, $$$r_2=3$$$, $$$c_1=1$$$, and $$$c_2=3$$$, $$$a_{2,1}+a_{3,3} otequiv a_{2,3}+a_{3,1} pmod 3$$$ because $$$a_{2,1}+a_{3,3} = 1 + 1 equiv 2 pmod 3$$$ and $$$a_{2,3}+a_{3,1} = 0 + 1 equiv 1 pmod 3 $$$. Moreover, the values on the main diagonal are equal to $$$1,1,1$$$ as required.
2,100
false
false
false
false
false
true
false
false
false
false
1,882
1105D
Kilani is playing a game with his friends. This game can be represented as a grid of size $$$n imes m$$$, where each cell is either empty or blocked, and every player has one or more castles in some cells (there are no two castles in one cell). The game is played in rounds. In each round players expand turn by turn: firstly, the first player expands, then the second player expands and so on. The expansion happens as follows: for each castle the player owns now, he tries to expand into the empty cells nearby. The player $$$i$$$ can expand from a cell with his castle to the empty cell if it's possible to reach it in at most $$$s_i$$$ (where $$$s_i$$$ is player's expansion speed) moves to the left, up, right or down without going through blocked cells or cells occupied by some other player's castle. The player examines the set of cells he can expand to and builds a castle in each of them at once. The turned is passed to the next player after that. The game ends when no player can make a move. You are given the game field and speed of the expansion for each player. Kilani wants to know for each player how many cells he will control (have a castle their) after the game ends. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$p$$$ ($$$1 le n, m le 1000$$$, $$$1 le p le 9$$$)xa0— the size of the grid and the number of players. The second line contains $$$p$$$ integers $$$s_i$$$ ($$$1 le s le 10^9$$$)xa0— the speed of the expansion for every player. The following $$$n$$$ lines describe the game grid. Each of them consists of $$$m$$$ symbols, where '.' denotes an empty cell, '#' denotes a blocked cell and digit $$$x$$$ ($$$1 le x le p$$$) denotes the castle owned by player $$$x$$$. It is guaranteed, that each player has at least one castle on the grid. Output Print $$$p$$$ integersxa0— the number of cells controlled by each player after the game ends. Examples Input 3 3 2 1 1 1.. ... ..2 Input 3 4 4 1 1 1 1 .... #... 1234 Note The picture below show the game before it started, the game after the first round and game after the second round in the first example: In the second example, the first player is "blocked" so he will not capture new cells for the entire game. All other player will expand up during the first two rounds and in the third round only the second player will move to the left.
1,900
false
false
true
false
false
false
false
false
false
true
5,177
547D
Problem - 547D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms dfs and similar graphs *2600 No tag edit access → Contest materials . He wants to put exactly one fish in each of these points such that the difference between the number of red fish and the blue fish on each horizontal or vertical line is at most 1. He can't find a way to perform that! Please help him. Input The first line of input contains integer _n_ (1u2009≤u2009_n_u2009≤u20092u2009×u2009105). The next _n_ lines contain the information about the points, _i_-th line contains two integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u20092u2009×u2009105), the _i_-th point coordinates. It is guaranteed that there is at least one valid answer. Output Print the answer as a sequence of _n_ characters 'r' (for red) or 'b' (for blue) where _i_-th character denotes the color of the fish in the _i_-th point. Examples Input 4 1 1 1 2 2 1 2 2 Output brrb Input 3 1 1 1 2 2 1 Output brr
2,600
false
false
false
false
false
true
false
false
false
true
7,649
1728D
Alice and Bob are playing a game. Initially, they are given a non-empty string $$$s$$$, consisting of lowercase Latin letters. The length of the string is even. Each player also has a string of their own, initially empty. Alice starts, then they alternate moves. In one move, a player takes either the first or the last letter of the string $$$s$$$, removes it from $$$s$$$ and prepends (adds to the beginning) it to their own string. The game ends when the string $$$s$$$ becomes empty. The winner is the player with a lexicographically smaller string. If the players' strings are equal, then it's a draw. A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if there exists such position $$$i$$$ that $$$a_j = b_j$$$ for all $$$j < i$$$ and $$$a_i < b_i$$$. What is the result of the game if both players play optimally (e.u2009g. both players try to win; if they can't, then try to draw)? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of testcases. Each testcase consists of a single linexa0— a non-empty string $$$s$$$, consisting of lowercase Latin letters. The length of the string $$$s$$$ is even. The total length of the strings over all testcases doesn't exceed $$$2000$$$. Output For each testcase, print the result of the game if both players play optimally. If Alice wins, print "Alice". If Bob wins, print "Bob". If it's a draw, print "Draw". Note One of the possible games Alice and Bob can play in the first testcase: 1. Alice picks the first letter in $$$s$$$: $$$s=$$$"orces", $$$a=$$$"f", $$$b=$$$""; 2. Bob picks the last letter in $$$s$$$: $$$s=$$$"orce", $$$a=$$$"f", $$$b=$$$"s"; 3. Alice picks the last letter in $$$s$$$: $$$s=$$$"orc", $$$a=$$$"ef", $$$b=$$$"s"; 4. Bob picks the first letter in $$$s$$$: $$$s=$$$"rc", $$$a=$$$"ef", $$$b=$$$"os"; 5. Alice picks the last letter in $$$s$$$: $$$s=$$$"r", $$$a=$$$"cef", $$$b=$$$"os"; 6. Bob picks the remaining letter in $$$s$$$: $$$s=$$$"", $$$a=$$$"cef", $$$b=$$$"ros". Alice wins because "cef" < "ros". Neither of the players follows any strategy in this particular example game, so it doesn't show that Alice wins if both play optimally.
1,800
false
false
false
true
false
true
false
false
false
false
1,922
724D
You are given a string _s_, consisting of lowercase English letters, and the integer _m_. One should choose some symbols from the given string so that any contiguous subsegment of length _m_ has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves. Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order. Formally, we choose a subsequence of indices 1u2009≤u2009_i_1u2009<u2009_i_2u2009<u2009...u2009<u2009_i__t_u2009≤u2009_s_. The selected sequence must meet the following condition: for every _j_ such that 1u2009≤u2009_j_u2009≤u2009_s_u2009-u2009_m_u2009+u20091, there must be at least one selected index that belongs to the segment [_j_,u2009xa0_j_u2009+u2009_m_u2009-u20091], i.e. there should exist a _k_ from 1 to _t_, such that _j_u2009≤u2009_i__k_u2009≤u2009_j_u2009+u2009_m_u2009-u20091. Then we take any permutation _p_ of the selected indices and form a new string _s__i__p_1_s__i__p_2... _s__i__p__t_. Find the lexicographically smallest string, that can be obtained using this procedure. Input The first line of the input contains a single integer _m_ (1u2009≤u2009_m_u2009≤u2009100u2009000). The second line contains the string _s_ consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn't exceed 100u2009000. It is also guaranteed that the number _m_ doesn't exceed the length of the string _s_. Output Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above. Note In the first sample, one can choose the subsequence {3} and form a string "a". In the second sample, one can choose the subsequence {1,u20092,u20094} (symbols on this positions are 'a', 'b' and 'a') and rearrange the chosen symbols to form a string "aab".
1,900
false
true
false
false
true
false
false
false
false
false
6,911
1553E
An identity permutation of length $$$n$$$ is an array $$$[1, 2, 3, dots, n]$$$. We performed the following operations to an identity permutation of length $$$n$$$: firstly, we cyclically shifted it to the right by $$$k$$$ positions, where $$$k$$$ is unknown to you (the only thing you know is that $$$0 le k le n - 1$$$). When an array is cyclically shifted to the right by $$$k$$$ positions, the resulting array is formed by taking $$$k$$$ last elements of the original array (without changing their relative order), and then appending $$$n - k$$$ first elements to the right of them (without changing relative order of the first $$$n - k$$$ elements as well). For example, if we cyclically shift the identity permutation of length $$$6$$$ by $$$2$$$ positions, we get the array $$$[5, 6, 1, 2, 3, 4]$$$; secondly, we performed the following operation at most $$$m$$$ times: pick any two elements of the array and swap them. You are given the values of $$$n$$$ and $$$m$$$, and the resulting array. Your task is to find all possible values of $$$k$$$ in the cyclic shift operation. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^5$$$) — the number of test cases. Each test case consists of two lines. The first line contains two integers $$$n$$$ and $$$m$$$ ($$$3 le n le 3 cdot 10^5$$$; $$$0 le m le frac{n}{3}$$$). The second line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$, each integer from $$$1$$$ to $$$n$$$ appears in this sequence exactly once) — the resulting array. The sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, print the answer in the following way: firstly, print one integer $$$r$$$ ($$$0 le r le n$$$) — the number of possible values of $$$k$$$ for the cyclic shift operation; secondly, print $$$r$$$ integers $$$k_1, k_2, dots, k_r$$$ ($$$0 le k_i le n - 1$$$) — all possible values of $$$k$$$ in increasing order. Example Input 4 4 1 2 3 1 4 3 1 1 2 3 3 1 3 2 1 6 0 1 2 3 4 6 5 Note Consider the example: in the first test case, the only possible value for the cyclic shift is $$$3$$$. If we shift $$$[1, 2, 3, 4]$$$ by $$$3$$$ positions, we get $$$[2, 3, 4, 1]$$$. Then we can swap the $$$3$$$-rd and the $$$4$$$-th elements to get the array $$$[2, 3, 1, 4]$$$; in the second test case, the only possible value for the cyclic shift is $$$0$$$. If we shift $$$[1, 2, 3]$$$ by $$$0$$$ positions, we get $$$[1, 2, 3]$$$. Then we don't change the array at all (we stated that we made at most $$$1$$$ swap), so the resulting array stays $$$[1, 2, 3]$$$; in the third test case, all values from $$$0$$$ to $$$2$$$ are possible for the cyclic shift: if we shift $$$[1, 2, 3]$$$ by $$$0$$$ positions, we get $$$[1, 2, 3]$$$. Then we can swap the $$$1$$$-st and the $$$3$$$-rd elements to get $$$[3, 2, 1]$$$; if we shift $$$[1, 2, 3]$$$ by $$$1$$$ position, we get $$$[3, 1, 2]$$$. Then we can swap the $$$2$$$-nd and the $$$3$$$-rd elements to get $$$[3, 2, 1]$$$; if we shift $$$[1, 2, 3]$$$ by $$$2$$$ positions, we get $$$[2, 3, 1]$$$. Then we can swap the $$$1$$$-st and the $$$2$$$-nd elements to get $$$[3, 2, 1]$$$; in the fourth test case, we stated that we didn't do any swaps after the cyclic shift, but no value of cyclic shift could produce the array $$$[1, 2, 3, 4, 6, 5]$$$.
2,100
true
false
false
false
false
true
true
false
false
true
2,871
2010C1
This is a simplified version of the problem. It differs from the difficult one only in its constraints. At the Berland State University, the local network between servers does not always operate without errors. When transmitting two identical messages consecutively, an error may occur, resulting in the two messages merging into one. In this merging, the end of the first message coincides with the beginning of the second. Of course, the merging can only occur at identical characters. The length of the merging must be a positive number less than the length of the message text. For example, when transmitting two messages "abrakadabra" consecutively, it is possible that it will be transmitted with the described type of error, resulting in a message like "abrakadabrabrakadabra" or "abrakadabrakadabra' (in the first case, the merging occurred at one character, and in the second case, at four). Given the received message _t_, determine if it is possible that this is the result of an error of the described type in the operation of the local network, and if so, determine a possible value of _s_. A situation where two messages completely overlap each other should not be considered an error. For example, if the received message is "abcd", it should be considered that there is no error in it. Similarly, simply appending one message after another is not a sign of an error. For instance, if the received message is "abcabc", it should also be considered that there is no error in it. Output If the message _t_ cannot contain an error, output "NO" (without quotes) in a single line of output. Otherwise, in the first line, output "YES" (without quotes), and in the next line, output the string _s_ — a possible message that could have led to the error. If there are multiple possible answers, any of them is acceptable.
1,400
false
false
false
false
false
false
true
false
false
false
189
1678A
Tokitsukaze has a sequence $$$a$$$ of length $$$n$$$. For each operation, she selects two numbers $$$a_i$$$ and $$$a_j$$$ ($$$i e j$$$; $$$1 leq i,j leq n$$$). If $$$a_i = a_j$$$, change one of them to $$$0$$$. Otherwise change both of them to $$$min(a_i, a_j)$$$. Tokitsukaze wants to know the minimum number of operations to change all numbers in the sequence to $$$0$$$. It can be proved that the answer always exists. Input The first line contains a single positive integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 leq n leq 100$$$)xa0— the length of the sequence $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 100$$$)xa0— the sequence $$$a$$$. Note In the first test case, one of the possible ways to change all numbers in the sequence to $$$0$$$: In the $$$1$$$-st operation, $$$a_1 < a_2$$$, after the operation, $$$a_2 = a_1 = 1$$$. Now the sequence $$$a$$$ is $$$[1,1,3]$$$. In the $$$2$$$-nd operation, $$$a_1 = a_2 = 1$$$, after the operation, $$$a_1 = 0$$$. Now the sequence $$$a$$$ is $$$[0,1,3]$$$. In the $$$3$$$-rd operation, $$$a_1 < a_2$$$, after the operation, $$$a_2 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,3]$$$. In the $$$4$$$-th operation, $$$a_2 < a_3$$$, after the operation, $$$a_3 = 0$$$. Now the sequence $$$a$$$ is $$$[0,0,0]$$$. So the minimum number of operations is $$$4$$$.
800
false
false
true
false
false
false
false
false
false
false
2,200
487C
Problem - 487C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms math number theory *2300 No tag edit access → Contest materials . Output In the first output line, print "YES" if such sequence exists, or print "NO" if no such sequence exists. If any solution exists, you should output _n_ more lines. _i_-th line contains only an integer _a__i_. The elements of the sequence should be different positive integers no larger than _n_. If there are multiple solutions, you are allowed to print any of them. Examples Input 7 Output YES 1 4 3 6 5 2 7 Input 6 Output NO Note For the second sample, there are no valid sequences.
2,300
true
false
false
false
false
true
false
false
false
false
7,888
1392F
Omkar is standing at the foot of Celeste mountain. The summit is $$$n$$$ meters away from him, and he can see all of the mountains up to the summit, so for all $$$1 leq j leq n$$$ he knows that the height of the mountain at the point $$$j$$$ meters away from himself is $$$h_j$$$ meters. It turns out that for all $$$j$$$ satisfying $$$1 leq j leq n - 1$$$, $$$h_j < h_{j + 1}$$$ (meaning that heights are strictly increasing). Suddenly, a landslide occurs! While the landslide is occurring, the following occurs: every minute, if $$$h_j + 2 leq h_{j + 1}$$$, then one square meter of dirt will slide from position $$$j + 1$$$ to position $$$j$$$, so that $$$h_{j + 1}$$$ is decreased by $$$1$$$ and $$$h_j$$$ is increased by $$$1$$$. These changes occur simultaneously, so for example, if $$$h_j + 2 leq h_{j + 1}$$$ and $$$h_{j + 1} + 2 leq h_{j + 2}$$$ for some $$$j$$$, then $$$h_j$$$ will be increased by $$$1$$$, $$$h_{j + 2}$$$ will be decreased by $$$1$$$, and $$$h_{j + 1}$$$ will be both increased and decreased by $$$1$$$, meaning that in effect $$$h_{j + 1}$$$ is unchanged during that minute. The landslide ends when there is no $$$j$$$ such that $$$h_j + 2 leq h_{j + 1}$$$. Help Omkar figure out what the values of $$$h_1, dots, h_n$$$ will be after the landslide ends. It can be proven that under the given constraints, the landslide will always end in finitely many minutes. Note that because of the large amount of input, it is recommended that your code uses fast IO. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 10^6$$$). The second line contains $$$n$$$ integers $$$h_1, h_2, dots, h_n$$$ satisfying $$$0 leq h_1 < h_2 < dots < h_n leq 10^{12}$$$xa0— the heights. Note Initially, the mountain has heights $$$2, 6, 7, 8$$$. In the first minute, we have $$$2 + 2 leq 6$$$, so $$$2$$$ increases to $$$3$$$ and $$$6$$$ decreases to $$$5$$$, leaving $$$3, 5, 7, 8$$$. In the second minute, we have $$$3 + 2 leq 5$$$ and $$$5 + 2 leq 7$$$, so $$$3$$$ increases to $$$4$$$, $$$5$$$ is unchanged, and $$$7$$$ decreases to $$$6$$$, leaving $$$4, 5, 6, 8$$$. In the third minute, we have $$$6 + 2 leq 8$$$, so $$$6$$$ increases to $$$7$$$ and $$$8$$$ decreases to $$$7$$$, leaving $$$4, 5, 7, 7$$$. In the fourth minute, we have $$$5 + 2 leq 7$$$, so $$$5$$$ increases to $$$6$$$ and $$$7$$$ decreases to $$$6$$$, leaving $$$4, 6, 6, 7$$$. In the fifth minute, we have $$$4 + 2 leq 6$$$, so $$$4$$$ increases to $$$5$$$ and $$$6$$$ decreases to $$$5$$$, leaving $$$5, 5, 6, 7$$$. In the sixth minute, nothing else can change so the landslide stops and our answer is $$$5, 5, 6, 7$$$.
2,400
true
true
false
false
true
true
false
true
false
false
3,711
273E
Problem - 273E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp games *2600 No tag edit access → Contest materials (1u2009≤u2009_l__i_u2009<u2009_r__i_u2009≤u2009_p_). Then players take turns. On his turn the player can do the following actions: 1. choose the number of the pair _i_ (1u2009≤u2009_i_u2009≤u2009_n_), such that _r__i_u2009-u2009_l__i_u2009>u20092; 2. replace pair number _i_ by pair or by pair . Notation ⌊_x_⌋ means rounding down to the closest integer. The player who can't make a move loses. Of course, Dima wants Anya, who will move first, to win. That's why Dima should write out such _n_ pairs of integers (_l__i_,u2009_r__i_) (1u2009≤u2009_l__i_u2009<u2009_r__i_u2009≤u2009_p_), that if both players play optimally well, the first one wins. Count the number of ways in which Dima can do it. Print the remainder after dividing the answer by number 1000000007xa0(109u2009+u20097). Two ways are considered distinct, if the ordered sequences of the written pairs are distinct. Input The first line contains two integers _n_, _p_ (1u2009≤u2009_n_u2009≤u20091000,u20091u2009≤u2009_p_u2009≤u2009109). The numbers are separated by a single space. Output In a single line print the remainder after dividing the answer to the problem by number 1000000007xa0(109u2009+u20097). Examples Input 2 2 Output 0 Input 4 4 Output 520 Input 100 1000 Output 269568947
2,600
false
false
false
true
false
false
false
false
false
false
8,740
2030C
Alice and Bob are playing a game. There is a list of $$$n$$$ booleans, each of which is either true or false, given as a binary string $$$^{ ext{∗}}$$$ of length $$$n$$$ (where $$$ exttt{1}$$$ represents true, and $$$ exttt{0}$$$ represents false). Initially, there are no operators between the booleans. Alice and Bob will take alternate turns placing and or or between the booleans, with Alice going first. Thus, the game will consist of $$$n-1$$$ turns since there are $$$n$$$ booleans. Alice aims for the final statement to evaluate to true, while Bob aims for it to evaluate to false. Given the list of boolean values, determine whether Alice will win if both players play optimally. To evaluate the final expression, repeatedly perform the following steps until the statement consists of a single true or false: If the statement contains an and operator, choose any one and replace the subexpression surrounding it with its evaluation. Otherwise, the statement contains an or operator. Choose any one and replace the subexpression surrounding the or with its evaluation. For example, the expression truexa0orxa0falsexa0andxa0false is evaluated as truexa0orxa0(falsexa0andxa0false) $$$=$$$ truexa0orxa0false $$$=$$$ true. It can be shown that the result of any compound statement is unique. $$$^{ ext{∗}}$$$A binary string is a string that only consists of characters $$$ exttt{0}$$$ and $$$ exttt{1}$$$ Input The first line contains $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 leq n leq 2 cdot 10^5$$$)xa0— the length of the string. The second line contains a binary string of length $$$n$$$, consisting of characters $$$ exttt{0}$$$ and $$$ exttt{1}$$$xa0— the list of boolean values. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each testcase, output "YES" (without quotes) if Alice wins, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response). Example Input 5 2 11 3 010 12 101111111100 10 0111111011 8 01000010 Note In the first testcase, Alice can place and between the two booleans. The game ends as there are no other places to place operators, and Alice wins because truexa0andxa0true is true. In the second testcase, Alice can place or between the middle true and the left false. Bob can place and between the middle true and the right false. The statement falsexa0orxa0truexa0andxa0false is false. Note that these examples may not be the best strategies for either Alice or Bob.
1,100
false
true
false
false
false
false
true
false
false
false
86
1904D1
This is the easy version of the problem. The only differences between the two versions of this problem are the constraints on $$$n$$$ and the time limit. You can make hacks only if all versions of the problem are solved. You are given two arrays $$$a$$$ and $$$b$$$ of length $$$n$$$. You can perform the following operation some (possibly zero) times: 1. choose $$$l$$$ and $$$r$$$ such that $$$1 leq l leq r leq n$$$. 2. let $$$x=max(a_l,a_{l+1},ldots,a_r)$$$. 3. for all $$$l leq i leq r$$$, set $$$a_i := x$$$. Determine if you can make array $$$a$$$ equal to array $$$b$$$. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 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 1000$$$)xa0— the length of the arrays. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the elements of array $$$a$$$. The third line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le n$$$)xa0— the elements of array $$$b$$$. It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output "YES" (without quotes) if you can make $$$a$$$ into $$$b$$$ using any number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response). Example Input 5 5 1 2 3 2 4 1 3 3 2 4 5 3 4 2 2 4 3 4 3 4 4 5 3 2 1 1 1 3 3 3 2 2 2 1 1 1 2 3 1 1 2 2 1 2 Note In the first test case, we can achieve array $$$b$$$ by applying a single operation: $$$(l,r)=(2,3)$$$. In the second test case, it can be shown we cannot achieve array $$$b$$$ in any amount of operations. In the third test case, we can achieve array $$$b$$$ by applying two operations: $$$(l,r)=(2,5)$$$. followed by $$$(l,r)=(1,3)$$$. In the fourth and fifth test cases, it can be shown we cannot achieve array $$$b$$$ in any amount of operations.
1,600
false
true
false
false
false
true
true
false
false
false
887
810A
Noora is a student of one famous high school. It's her final year in schoolxa0— she is going to study in university next year. However, she has to get an «A» graduation certificate in order to apply to a prestigious one. In school, where Noora is studying, teachers are putting down marks to the online class register, which are integers from 1 to _k_. The worst mark is 1, the best is _k_. Mark that is going to the certificate, is calculated as an average of all the marks, rounded to the closest integer. If several answers are possible, rounding up is produced. For example, 7.3 is rounded to 7, but 7.5 and 7.8784xa0— to 8. For instance, if Noora has marks [8,u20099], then the mark to the certificate is 9, because the average is equal to 8.5 and rounded to 9, but if the marks are [8,u20098,u20099], Noora will have graduation certificate with 8. To graduate with «A» certificate, Noora has to have mark _k_. Noora got _n_ marks in register this year. However, she is afraid that her marks are not enough to get final mark _k_. Noora decided to ask for help in the internet, where hacker Leha immediately responded to her request. He is ready to hack class register for Noora and to add Noora any number of additional marks from 1 to _k_. At the same time, Leha want his hack be unseen to everyone, so he decided to add as less as possible additional marks. Please help Leha to calculate the minimal number of marks he has to add, so that final Noora's mark will become equal to _k_. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009100,u20091u2009≤u2009_k_u2009≤u2009100) denoting the number of marks, received by Noora and the value of highest possible mark. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009_k_) denoting marks received by Noora before Leha's hack. Note Consider the first example testcase. Maximal mark is 10, Noora received two marksxa0— 8 and 9, so current final mark is 9. To fix it, Leha can add marks [10,u200910,u200910,u200910] (4 marks in total) to the registry, achieving Noora having average mark equal to . Consequently, new final mark is 10. Less number of marks won't fix the situation. In the second example Leha can add [5,u20095,u20095] to the registry, so that making average mark equal to 4.5, which is enough to have 5 in the certificate.
900
true
false
true
false
false
false
false
false
false
false
6,523
528E
# Triangles 3000 Input file: standard input Output file: standard output Time limit: 5 seconds Memory limit: 512 megabytes You are given a set L = {l1, l 2, . . . , l n} of n pairwise non-parallel lines on the Euclidean plane. The i-th line is given by an equation in the form of aix + biy = ci. L doesn’t contain three lines coming through the same point. A subset of three distinct lines is chosen equiprobably. Determine the expected value of the area of the triangle formed by the three lines. ## Input The first line of the input contains integer n (3 ≤ n ≤ 3000 ). Each of the next lines contains three integers ai, b i, c i (−100 ≤ ai, b i ≤ 100 , a2 > i + b2 > i > 0, −10 000 ≤ ci ≤ 10 000 ) — the coefficients defining the i-th line. It is guaranteed that no two lines are parallel. Besides, any two lines intersect at angle at least 10 −4 radians. If we assume that I is a set of points of pairwise intersection of the lines (i. e. I = {li ∩ lj i < j }), then for any point a ∈ I it is true that the coordinates of a do not exceed 10 6 by their absolute values. Also, for any two distinct points a, b ∈ I the distance between a and b is no less than 10 −5. ## Output Print a single real number equal to the sought expected value. Your answer will be checked with the absolute or relative error 10 −4. ## Examples standard input standard output 41 0 0 0 1 0 1 1 2 -1 1 -1 1.25 ## Note A sample from the statement is shown below. There are four triangles on the plane, their areas are 0.25 , 0.5, 2, 2.25 . Page 1 of 2 yx 210.5 0-1 1 1.5 2Page 2 of 2
3,100
false
false
false
false
false
false
false
false
true
false
7,718
923F
There are _N_ cities in Bob's country connected by roads. Some pairs of cities are connected by public transport. There are two competing transport companiesxa0— Boblines operating buses and Bobrail running trains. When traveling from _A_ to _B_, a passenger always first selects the mode of transport (either bus or train), and then embarks on a journey. For every pair of cities, there are exactly two ways of how to travel between them without visiting any city more than oncexa0— one using only bus routes, and the second using only train routes. Furthermore, there is no pair of cities that is directly connected by both a bus route and a train route. You obtained the plans of each of the networks. Unfortunately, each of the companies uses different names for the same cities. More precisely, the bus company numbers the cities using integers from 1 to _N_, while the train company uses integers between _N_u2009+u20091 and 2_N_. Find one possible mapping between those two numbering schemes, such that no pair of cities is connected directly by both a bus route and a train route. Note that this mapping has to map different cities to different cities. Input The first line contains an integer _N_ (2u2009≤u2009_N_u2009≤u200910000), the number of cities. _N_u2009-u20091 lines follow, representing the network plan of Boblines. Each contains two integers _u_ and _v_ (1u2009≤u2009_u_,u2009_v_u2009≤u2009_N_), meaning that there is a bus route between cities _u_ and _v_. _N_u2009-u20091 lines follow, representing the network plan of Bobrail. Each contains two integers _u_ and _v_ (_N_u2009+u20091u2009≤u2009_u_,u2009_v_u2009≤u20092_N_), meaning that there is a train route between cities _u_ and _v_. Output If there is no solution, output a single line with the word "No". If a solution exists, output two lines. On the first line, there should be the word "Yes". On the second line, there should be _N_ integers _P_1,u2009_P_2,u2009...,u2009_P__N_ (_N_u2009+u20091u2009≤u2009_P__i_u2009≤u20092_N_)xa0— the mapping between the two numbering schemes. More precisely, for _i_u2009≠u2009_j_ it should be _P__i_u2009≠u2009_P__j_, and for every direct bus route (_i_,u2009_j_), there is no direct train route between (_P__i_,u2009_P__j_). If there are multiple solutions, you may print any of them. Examples Input 7 1 2 1 3 1 4 1 5 5 6 6 7 8 9 9 10 10 11 11 12 12 13 13 14 Output Yes 9 14 11 12 13 10 8 Note The first sample (bus lines in red and rail lines in blue):
3,200
false
false
false
false
false
true
false
false
false
true
6,018
1994H
This is an interactive problem! Timofey is writing a competition called Capture the Flag (or CTF for short). He has one task left, which involves hacking a security system. The entire system is based on polynomial hashes$$$^{ ext{∗}}$$$. Timofey can input a string consisting of lowercase Latin letters into the system, and the system will return its polynomial hash. To hack the system, Timofey needs to find the polynomial hash parameters ($$$p$$$ and $$$m$$$) that the system uses. Timofey doesn't have much time left, so he will only be able to make $$$3$$$ queries. Help him solve the task. $$$^{ ext{∗}}$$$ The polynomial hash of a string $$$s$$$, consisting of lowercase Latin letters of length $$$n$$$, based on $$$p$$$ and modulo $$$m$$$ is $$$(mathrm{ord}(s_1) cdot p ^ 0 + mathrm{ord}(s_2) cdot p ^ 1 + mathrm{ord}(s_3) cdot p ^ 2 + ldots + mathrm{ord}(s_n) cdot p ^ {n - 1}) bmod m$$$. Where $$$s_i$$$ denotes the $$$i$$$-th character of the string $$$s$$$, $$$mathrm{ord}(mathrm{chr})$$$ denotes the ordinal number of the character $$$mathrm{chr}$$$ in the English alphabet, and $$$x bmod m$$$ is the remainder of $$$x$$$ when divided by $$$m$$$. Input Each test consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^3$$$)xa0— the number of test cases. It is guaranteed that the $$$p$$$ and $$$m$$$ used by the system satisfy the conditions: $$$26 < p leq 50$$$ and $$$p + 1 < m leq 2 cdot 10^9$$$. Interaction To make a query to the system, output ? $$$s$$$, where $$$s$$$ is a string of no more than $$$50$$$ characters in length, the hash of which you want to know. In response to this query, you will receive the polynomial hash of the string $$$s$$$. To output the answer, output ! $$$p$$$ $$$m$$$, where $$$p$$$ is the base of the hash, and $$$m$$$ is the modulus. After that, immediately proceed to the next test case. You have to make not more than $$$3$$$ queries ?, otherwise you will get verdict Wrong Answer. After outputting a query, do not forget to output a newline and flush the output buffer. Otherwise, you will receive 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; see the documentation for other languages. Note Answer for the first query is $$$(ord(a) cdot 31^0 + ord(a) cdot 31^1) mod 59 = (1 + 1 cdot 31) mod 59 = 32$$$. Answer for the second query is $$$(ord(y) cdot 31^0 + ord(b) cdot 31^1) mod 59 = (25 + 2 cdot 31) mod 59 = 28$$$.
3,500
true
true
false
false
false
true
false
false
false
false
298
317C
A system of _n_ vessels with water is given. Several pairs of vessels are connected by tubes with transfusion mechanisms. One may transfer an integer amount of liters of water between two vessels connected by such tube (tube works in both directions). There might be multiple tubes between two vessels. Total number of tubes equals _e_. Volume of each vessel equals _v_ liters. Of course, the amount of the water in any vessel cannot exceed _v_ liters in the process of transfusions. Given the initial amounts _a__i_ of water in the vessels and the desired amounts _b__i_ find a sequence of transfusions that deals with the task. Total number of transfusions must not exceed 2·_n_2. Input First line of the input contains integers _n_, _v_, _e_ (1u2009≤u2009_n_u2009≤u2009300, 1u2009≤u2009_v_u2009≤u2009109, 0u2009≤u2009_e_u2009≤u200950000). Next two lines contain _n_ integers each: initial _a__i_ and the desired amounts _b__i_ of water in corresponding vessels (0u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_v_). Next _e_ lines describe one tube each in the format _x_ _y_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_,u2009_x_u2009≠u2009_y_) for a tube between vessels number _x_ and _y_. There might be multiple tubes between two vessels. You may assume that vessels are numbered from 1 to _n_ in some way. Output Print "NO" (without quotes), if such sequence of transfusions does not exist. Otherwise print any suitable sequence in the following format. On the first line print the total number of transfusions _k_ (_k_ should not exceed 2·_n_2). In the following _k_ lines print transfusions in the format _x_xa0_y_xa0_d_ (transfusion of _d_ liters from the vessel number _x_ to the vessel number _y_, _x_ and _y_ must be distinct). For all transfusions _d_ must be a non-negative integer.
2,500
false
false
false
false
false
true
false
false
false
true
8,563
786B
Rick and his co-workers have made a new radioactive formula and a lot of bad guys are after them. So Rick wants to give his legacy to Morty before bad guys catch them. There are _n_ planets in their universe numbered from 1 to _n_. Rick is in planet number _s_ (the earth) and he doesn't know where Morty is. As we all know, Rick owns a portal gun. With this gun he can open one-way portal from a planet he is in to any other planet (including that planet). But there are limits on this gun because he's still using its free trial. By default he can not open any portal by this gun. There are _q_ plans in the website that sells these guns. Every time you purchase a plan you can only use it once but you can purchase it again if you want to use it more. Plans on the website have three types: 1. With a plan of this type you can open a portal from planet _v_ to planet _u_. 2. With a plan of this type you can open a portal from planet _v_ to any planet with index in range [_l_,u2009_r_]. 3. With a plan of this type you can open a portal from any planet with index in range [_l_,u2009_r_] to planet _v_. Rick doesn't known where Morty is, but Unity is going to inform him and he wants to be prepared for when he finds and start his journey immediately. So for each planet (including earth itself) he wants to know the minimum amount of money he needs to get from earth to that planet. Input The first line of input contains three integers _n_, _q_ and _s_ (1u2009≤u2009_n_,u2009_q_u2009≤u2009105, 1u2009≤u2009_s_u2009≤u2009_n_) — number of planets, number of plans and index of earth respectively. The next _q_ lines contain the plans. Each line starts with a number _t_, type of that plan (1u2009≤u2009_t_u2009≤u20093). If _t_u2009=u20091 then it is followed by three integers _v_, _u_ and _w_ where _w_ is the cost of that plan (1u2009≤u2009_v_,u2009_u_u2009≤u2009_n_, 1u2009≤u2009_w_u2009≤u2009109). Otherwise it is followed by four integers _v_, _l_, _r_ and _w_ where _w_ is the cost of that plan (1u2009≤u2009_v_u2009≤u2009_n_, 1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_, 1u2009≤u2009_w_u2009≤u2009109). Output In the first and only line of output print _n_ integers separated by spaces. _i_-th of them should be minimum money to get from earth to _i_-th planet, or u2009-u20091 if it's impossible to get to that planet. Examples Input 3 5 1 2 3 2 3 17 2 3 2 2 16 2 2 2 3 3 3 3 1 1 12 1 3 3 17 Input 4 3 1 3 4 1 3 12 2 2 3 4 10 1 2 4 16 Note In the first sample testcase, Rick can purchase 4th plan once and then 2nd plan in order to get to get to planet number 2.
2,300
false
false
false
false
true
false
false
false
false
true
6,627
131D
A subway scheme, classic for all Berland cities is represented by a set of _n_ stations connected by _n_ passages, each of which connects exactly two stations and does not pass through any others. Besides, in the classic scheme one can get from any station to any other one along the passages. The passages can be used to move in both directions. Between each pair of stations there is no more than one passage. Berland mathematicians have recently proved a theorem that states that any classic scheme has a ringroad. There can be only one ringroad. In other words, in any classic scheme one can find the only scheme consisting of stations (where any two neighbouring ones are linked by a passage) and this cycle doesn't contain any station more than once. This invention had a powerful social impact as now the stations could be compared according to their distance from the ringroad. For example, a citizen could say "I live in three passages from the ringroad" and another one could reply "you loser, I live in one passage from the ringroad". The Internet soon got filled with applications that promised to count the distance from the station to the ringroad (send a text message to a short number...). The Berland government decided to put an end to these disturbances and start to control the situation. You are requested to write a program that can determine the remoteness from the ringroad for each station by the city subway scheme. Input The first line contains an integer _n_ (3u2009≤u2009_n_u2009≤u20093000), _n_ is the number of stations (and trains at the same time) in the subway scheme. Then _n_ lines contain descriptions of the trains, one per line. Each line contains a pair of integers _x__i_,u2009_y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_) and represents the presence of a passage from station _x__i_ to station _y__i_. The stations are numbered from 1 to _n_ in an arbitrary order. It is guaranteed that _x__i_u2009≠u2009_y__i_ and that no pair of stations contain more than one passage. The passages can be used to travel both ways. It is guaranteed that the given description represents a classic subway scheme. Output Print _n_ numbers. Separate the numbers by spaces, the _i_-th one should be equal to the distance of the _i_-th station from the ringroad. For the ringroad stations print number 0.
1,600
false
false
false
false
false
false
false
false
false
true
9,350
1512C
You are given a string $$$s$$$ consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string $$$s$$$ by '0' or '1' so that the string becomes a palindrome and has exactly $$$a$$$ characters '0' and exactly $$$b$$$ characters '1'. Note that each of the characters '?' is replaced independently from the others. A string $$$t$$$ of length $$$n$$$ is called a palindrome if the equality $$$t[i] = t[n-i+1]$$$ is true for all $$$i$$$ ($$$1 le i le n$$$). For example, if $$$s=$$$"01?????0", $$$a=4$$$ and $$$b=4$$$, then you can replace the characters '?' in the following ways: "01011010"; "01100110". For the given string $$$s$$$ and the numbers $$$a$$$ and $$$b$$$, replace all the characters with '?' in the string $$$s$$$ by '0' or '1' so that the string becomes a palindrome and has exactly $$$a$$$ characters '0' and exactly $$$b$$$ characters '1'. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$0 le a, b le 2 cdot 10^5$$$, $$$a + b ge 1$$$). The second line of each test case contains the string $$$s$$$ of length $$$a+b$$$, consisting of the characters '0', '1', and '?'. It is guaranteed that the sum of the string lengths of $$$s$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output: "-1", if you can't replace all the characters '?' in the string $$$s$$$ by '0' or '1' so that the string becomes a palindrome and that it contains exactly $$$a$$$ characters '0' and exactly $$$b$$$ characters '1'; the string that is obtained as a result of the replacement, otherwise. If there are several suitable ways to replace characters, you can output any. Example Input 9 4 4 01?????0 3 3 ?????? 1 0 ? 2 2 0101 2 2 01?0 0 1 0 0 3 1?1 2 2 ?00? 4 3 ??010?0 Output 01011010 -1 0 -1 0110 -1 111 1001 0101010
1,200
false
false
true
false
false
true
false
false
false
false
3,107
1729B
Polycarp has a string $$$s$$$ consisting of lowercase Latin letters. He encodes it using the following algorithm. He goes through the letters of the string $$$s$$$ from left to right and for each letter Polycarp considers its number in the alphabet: if the letter number is single-digit number (less than $$$10$$$), then just writes it out; if the letter number is a two-digit number (greater than or equal to $$$10$$$), then it writes it out and adds the number 0 after. For example, if the string $$$s$$$ is code, then Polycarp will encode this string as follows: 'c'xa0— is the $$$3$$$-rd letter of the alphabet. Consequently, Polycarp adds 3 to the code (the code becomes equal to 3); 'o'xa0— is the $$$15$$$-th letter of the alphabet. Consequently, Polycarp adds 15 to the code and also 0 (the code becomes 3150); 'd'xa0— is the $$$4$$$-th letter of the alphabet. Consequently, Polycarp adds 4 to the code (the code becomes 31504); 'e'xa0— is the $$$5$$$-th letter of the alphabet. Therefore, Polycarp adds 5 to the code (the code becomes 315045). Thus, code of string code is 315045. You are given a string $$$t$$$ resulting from encoding the string $$$s$$$. Your task is to decode it (get the original string $$$s$$$ by $$$t$$$). Input The first line of the input contains an integer $$$q$$$ ($$$1 le q le 10^4$$$) — the number of test cases in the input. The descriptions of the test cases follow. The first line of description of each test case contains one integer $$$n$$$ ($$$1 le n le 50$$$) — the length of the given code. The second line of the description of each test case contains a string $$$t$$$ of length $$$n$$$ — the given code. It is guaranteed that there exists such a string of lowercase Latin letters, as a result of encoding which the string $$$t$$$ is obtained. Output For each test case output the required string $$$s$$$ — the string that gives string $$$t$$$ as the result of encoding. It is guaranteed that such a string always exists. It can be shown that such a string is always unique. Example Input 9 6 315045 4 1100 7 1213121 6 120120 18 315045615018035190 7 1111110 7 1111100 5 11111 4 2606 Output code aj abacaba ll codeforces aaaak aaaaj aaaaa zf Note The first test case is explained above. In the second test case, the answer is aj. Indeed, the number of the letter a is equal to $$$1$$$, so 1 will be appended to the code. The number of the letter j is $$$10$$$, so 100 will be appended to the code. The resulting code is 1100. There are no zeros in the third test case, which means that the numbers of all letters are less than $$$10$$$ and are encoded as one digit. The original string is abacaba. In the fourth test case, the string $$$s$$$ is equal to ll. The letter l has the number $$$12$$$ and is encoded as 120. So ll is indeed 120120.
800
false
true
false
false
false
false
false
false
false
false
1,917
1822B
Karina has an array of $$$n$$$ integers $$$a_1, a_2, a_3, dots, a_n$$$. She loves multiplying numbers, so she decided that the beauty of a pair of numbers is their product. And the beauty of an array is the maximum beauty of a pair of adjacent elements in the array. For example, for $$$n = 4$$$, $$$a=[3, 5, 7, 4]$$$, the beauty of the array is $$$max$$$($$$3 cdot 5$$$, $$$5 cdot 7$$$, $$$7 cdot 4$$$) = $$$max$$$($$$15$$$, $$$35$$$, $$$28$$$) = $$$35$$$. Karina wants her array to be as beautiful as possible. In order to achieve her goal, she can remove some elements (possibly zero) from the array. After Karina removes all elements she wants to, the array must contain at least two elements. Unfortunately, Karina doesn't have enough time to do all her tasks, so she asks you to calculate the maximum beauty of the array that she can get by removing any number of elements (possibly zero). Input The first line of the input contains an 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 a test case contains an integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0— the length of the array $$$a$$$. The second line of a test case contains $$$n$$$ integers $$$a_1, a_2, a_3, dots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$)xa0— the elements of the array $$$a$$$. The sum of all values of $$$n$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. Output Output $$$t$$$ integers, each of which is the answer to the corresponding test casexa0— the maximum beauty of the array that Karina can get. Example Input 7 4 5 0 2 1 3 -1 1 0 5 2 0 -1 -4 0 6 -8 4 3 7 1 -9 6 0 3 -2 5 -4 -4 2 1000000000 910000000 7 -1 -7 -2 -5 -4 -6 -3 Output 10 0 4 72 16 910000000000000000 42 Note In the first test case of the example, to get the maximum beauty, you need to remove the $$$2$$$-nd element. In the second and third test cases of the example, there is no need to remove any elements to achieve maximum beauty. In the fourth test case of the example, you need to leave only the first and last elements.
800
true
true
false
false
false
false
false
false
true
false
1,350
1096E
Hasan loves playing games and has recently discovered a game called TopScore. In this soccer-like game there are $$$p$$$ players doing penalty shoot-outs. Winner is the one who scores the most. In case of ties, one of the top-scorers will be declared as the winner randomly with equal probability. They have just finished the game and now are waiting for the result. But there's a tiny problem! The judges have lost the paper of scores! Fortunately they have calculated sum of the scores before they get lost and also for some of the players they have remembered a lower bound on how much they scored. However, the information about the bounds is private, so Hasan only got to know his bound. According to the available data, he knows that his score is at least $$$r$$$ and sum of the scores is $$$s$$$. Thus the final state of the game can be represented in form of sequence of $$$p$$$ integers $$$a_1, a_2, dots, a_p$$$ ($$$0 le a_i$$$) — player's scores. Hasan is player number $$$1$$$, so $$$a_1 ge r$$$. Also $$$a_1 + a_2 + dots + a_p = s$$$. Two states are considered different if there exists some position $$$i$$$ such that the value of $$$a_i$$$ differs in these states. Once again, Hasan doesn't know the exact scores (he doesn't know his exact score as well). So he considers each of the final states to be equally probable to achieve. Help Hasan find the probability of him winning. It can be shown that it is in the form of $$$frac{P}{Q}$$$ where $$$P$$$ and $$$Q$$$ are non-negative integers and $$$Q e 0$$$, $$$P le Q$$$. Report the value of $$$P cdot Q^{-1} pmod {998244353}$$$. Input The only line contains three integers $$$p$$$, $$$s$$$ and $$$r$$$ ($$$1 le p le 100$$$, $$$0 le r le s le 5000$$$) — the number of players, the sum of scores of all players and Hasan's score, respectively. Output Print a single integer — the probability of Hasan winning. It can be shown that it is in the form of $$$frac{P}{Q}$$$ where $$$P$$$ and $$$Q$$$ are non-negative integers and $$$Q e 0$$$, $$$P le Q$$$. Report the value of $$$P cdot Q^{-1} pmod {998244353}$$$. Note In the first example Hasan can score $$$3$$$, $$$4$$$, $$$5$$$ or $$$6$$$ goals. If he scores $$$4$$$ goals or more than he scores strictly more than his only opponent. If he scores $$$3$$$ then his opponent also scores $$$3$$$ and Hasan has a probability of $$$frac 1 2$$$ to win the game. Thus, overall he has the probability of $$$frac 7 8$$$ to win. In the second example even Hasan's lower bound on goal implies him scoring more than any of his opponents. Thus, the resulting probability is $$$1$$$.
2,500
true
false
false
true
false
false
false
false
false
false
5,227
78D
A breakthrough among computer games, "Civilization XIII", is striking in its scale and elaborate details. Let's take a closer look at one of them. The playing area in the game is split into congruent cells that are regular hexagons. The side of each cell is equal to 1. Each unit occupies exactly one cell of the playing field. The field can be considered infinite. Let's take a look at the battle unit called an "Archer". Each archer has a parameter "shot range". It's a positive integer that determines the radius of the circle in which the archer can hit a target. The center of the circle coincides with the center of the cell in which the archer stays. A cell is considered to be under the archer’s fire if and only if all points of this cell, including border points are located inside the circle or on its border. The picture below shows the borders for shot ranges equal to 3, 4 and 5. The archer is depicted as _A_. Find the number of cells that are under fire for some archer. Input The first and only line of input contains a single positive integer _k_ — the archer's shot range (1u2009≤u2009_k_u2009≤u2009106). Output Print the single number, the number of cells that are under fire. Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cout stream (also you may use the %I64d specificator).
2,300
true
false
false
false
false
false
false
true
false
false
9,572
439C
Devu being a small kid, likes to play a lot, but he only likes to play with arrays. While playing he came up with an interesting question which he could not solve, can you please solve it for him? Given an array consisting of distinct integers. Is it possible to partition the whole array into _k_ disjoint non-empty parts such that _p_ of the parts have even sum (each of them must have even sum) and remaining _k_ - _p_ have odd sum? (note that parts need not to be continuous). If it is possible to partition the array, also give any possible way of valid partitioning. Input The first line will contain three space separated integers _n_, _k_, _p_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009105;xa00u2009≤u2009_p_u2009≤u2009_k_). The next line will contain _n_ space-separated distinct integers representing the content of array _a_: _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). Output In the first line print "YES" (without the quotes) if it is possible to partition the array in the required way. Otherwise print "NO" (without the quotes). If the required partition exists, print _k_ lines after the first line. The _i__th_ of them should contain the content of the _i__th_ part. Print the content of the part in the line in the following way: firstly print the number of elements of the part, then print all the elements of the part in arbitrary order. There must be exactly _p_ parts with even sum, each of the remaining _k_ - _p_ parts must have odd sum. As there can be multiple partitions, you are allowed to print any valid partition. Examples Output YES 1 9 1 5 1 10 1 6 1 2
1,700
false
false
true
false
false
true
true
false
false
false
8,082
809E
Tired of boring dates, Leha and Noora decided to play a game. Leha found a tree with _n_ vertices numbered from 1 to _n_. We remind you that tree is an undirected graph without cycles. Each vertex _v_ of a tree has a number _a__v_ written on it. Quite by accident it turned out that all values written on vertices are distinct and are natural numbers between 1 and _n_. The game goes in the following way. Noora chooses some vertex _u_ of a tree uniformly at random and passes a move to Leha. Leha, in his turn, chooses (also uniformly at random) some vertex _v_ from remaining vertices of a tree (_v_u2009≠u2009_u_). As you could guess there are _n_(_n_u2009-u20091) variants of choosing vertices by players. After that players calculate the value of a function _f_(_u_,u2009_v_)u2009=u2009φ(_a__u_·_a__v_) · _d_(_u_,u2009_v_) of the chosen vertices where φ(_x_) is Euler's totient function and _d_(_x_,u2009_y_) is the shortest distance between vertices _x_ and _y_ in a tree. Soon the game became boring for Noora, so Leha decided to defuse the situation and calculate expected value of function _f_ over all variants of choosing vertices _u_ and _v_, hoping of at least somehow surprise the girl. Leha asks for your help in calculating this expected value. Let this value be representable in the form of an irreducible fraction . To further surprise Noora, he wants to name her the value . Help Leha! Input The first line of input contains one integer number _n_ (2u2009≤u2009_n_u2009≤u20092·105) xa0— number of vertices in a tree. The second line contains _n_ different numbers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009_n_) separated by spaces, denoting the values written on a tree vertices. Each of the next _n_u2009-u20091 lines contains two integer numbers _x_ and _y_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_), describing the next edge of a tree. It is guaranteed that this set of edges describes a tree. Output In a single line print a number equal to _P_·_Q_u2009-u20091 modulo 109u2009+u20097. Examples Input 5 5 4 3 1 2 3 5 1 2 4 3 2 5 Note Euler's totient function φ(_n_) is the number of such _i_ that 1u2009≤u2009_i_u2009≤u2009_n_,and _gcd_(_i_,u2009_n_)u2009=u20091, where _gcd_(_x_,u2009_y_) is the greatest common divisor of numbers _x_ and _y_. There are 6 variants of choosing vertices by Leha and Noora in the first testcase: _u_u2009=u20091, _v_u2009=u20092, _f_(1,u20092)u2009=u2009φ(_a_1·_a_2)·_d_(1,u20092)u2009=u2009φ(1·2)·1u2009=u2009φ(2)u2009=u20091 _u_u2009=u20092, _v_u2009=u20091, _f_(2,u20091)u2009=u2009_f_(1,u20092)u2009=u20091 _u_u2009=u20091, _v_u2009=u20093, _f_(1,u20093)u2009=u2009φ(_a_1·_a_3)·_d_(1,u20093)u2009=u2009φ(1·3)·2u2009=u20092φ(3)u2009=u20094 _u_u2009=u20093, _v_u2009=u20091, _f_(3,u20091)u2009=u2009_f_(1,u20093)u2009=u20094 _u_u2009=u20092, _v_u2009=u20093, _f_(2,u20093)u2009=u2009φ(_a_2·_a_3)·_d_(2,u20093)u2009=u2009φ(2·3)·1u2009=u2009φ(6)u2009=u20092 _u_u2009=u20093, _v_u2009=u20092, _f_(3,u20092)u2009=u2009_f_(2,u20093)u2009=u20092 Expected value equals to . The value Leha wants to name Noora is 7·3u2009-u20091u2009=u20097·333333336u2009=u2009333333338 . In the second testcase expected value equals to , so Leha will have to surprise Hoora by number 8·1u2009-u20091u2009=u20098 .
3,100
true
false
false
false
false
false
false
false
false
false
6,524
1566E
A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex $$$v$$$ (different from root) is the previous to $$$v$$$ vertex on the shortest path from the root to the vertex $$$v$$$. Children of the vertex $$$v$$$ are all vertices for which $$$v$$$ is the parent. A vertex is a leaf if it has no children. We call a vertex a bud, if the following three conditions are satisfied: it is not a root, it has at least one child, and all its children are leaves. You are given a rooted tree with $$$n$$$ vertices. The vertex $$$1$$$ is the root. In one operation you can choose any bud with all its children (they are leaves) and re-hang them to any other vertex of the tree. By doing that you delete the edge connecting the bud and its parent and add an edge between the bud and the chosen vertex of the tree. The chosen vertex cannot be the bud itself or any of its children. All children of the bud stay connected to the bud. What is the minimum number of leaves it is possible to get if you can make any number of the above-mentioned operations (possibly zero)? Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0— the number of the vertices in the given tree. Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u eq v$$$) meaning that there is an edge between 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 $$$2 cdot 10^5$$$. Output For each test case print a single integerxa0— the minimal number of leaves that is possible to get after some operations. Example Input 5 7 1 2 1 3 1 4 2 5 2 6 4 7 6 1 2 1 3 2 4 2 5 3 6 2 1 2 7 7 3 1 5 1 3 4 6 4 7 2 1 6 2 1 2 3 4 5 3 4 3 6 Note In the first test case the tree looks as follows: Firstly you can choose a bud vertex $$$4$$$ and re-hang it to vertex $$$3$$$. After that you can choose a bud vertex $$$2$$$ and re-hang it to vertex $$$7$$$. As a result, you will have the following tree with $$$2$$$ leaves: It can be proved that it is the minimal number of leaves possible to get. In the second test case the tree looks as follows: You can choose a bud vertex $$$3$$$ and re-hang it to vertex $$$5$$$. As a result, you will have the following tree with $$$2$$$ leaves: It can be proved that it is the minimal number of leaves possible to get.
2,000
false
true
false
true
false
true
false
false
false
false
2,808
426B
Let's assume that we are given a matrix _b_ of size _x_u2009×u2009_y_, let's determine the operation of mirroring matrix _b_. The mirroring of matrix _b_ is a 2_x_u2009×u2009_y_ matrix _c_ which has the following properties: the upper half of matrix _c_ (rows with numbers from 1 to _x_) exactly matches _b_; the lower half of matrix _c_ (rows with numbers from _x_u2009+u20091 to 2_x_) is symmetric to the upper one; the symmetry line is the line that separates two halves (the line that goes in the middle, between rows _x_ and _x_u2009+u20091). Sereja has an _n_u2009×u2009_m_ matrix _a_. He wants to find such matrix _b_, that it can be transformed into matrix _a_, if we'll perform on it several (possibly zero) mirrorings. What minimum number of rows can such matrix contain? Input The first line contains two integers, _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100). Each of the next _n_ lines contains _m_ integers — the elements of matrix _a_. The _i_-th line contains integers _a__i_1,u2009_a__i_2,u2009...,u2009_a__im_ (0u2009≤u2009_a__ij_u2009≤u20091) — the _i_-th row of the matrix _a_. Output In the single line, print the answer to the problem — the minimum number of rows of matrix _b_. Examples Input 4 3 0 0 1 1 1 0 1 1 0 0 0 1 Note In the first test sample the answer is a 2u2009×u20093 matrix _b_: 001 110 If we perform a mirroring operation with this matrix, we get the matrix _a_ that is given in the input: 001 110 110 001
1,300
false
false
true
false
false
false
false
false
false
false
8,132
1173A
Nauuo is a girl who loves writing comments. One day, she posted a comment on Codeforces, wondering whether she would get upvotes or downvotes. It's known that there were $$$x$$$ persons who would upvote, $$$y$$$ persons who would downvote, and there were also another $$$z$$$ persons who would vote, but you don't know whether they would upvote or downvote. Note that each of the $$$x+y+z$$$ people would vote exactly one time. There are three different results: if there are more people upvote than downvote, the result will be "+"; if there are more people downvote than upvote, the result will be "-"; otherwise the result will be "0". Because of the $$$z$$$ unknown persons, the result may be uncertain (i.e. there are more than one possible results). More formally, the result is uncertain if and only if there exist two different situations of how the $$$z$$$ persons vote, that the results are different in the two situations. Tell Nauuo the result or report that the result is uncertain. Input The only line contains three integers $$$x$$$, $$$y$$$, $$$z$$$ ($$$0le x,y,zle100$$$), corresponding to the number of persons who would upvote, downvote or unknown. Output If there is only one possible result, print the result : "+", "-" or "0". Otherwise, print "?" to report that the result is uncertain. Note In the first example, Nauuo would definitely get three upvotes and seven downvotes, so the only possible result is "-". In the second example, no matter the person unknown downvotes or upvotes, Nauuo would get more upvotes than downvotes. So the only possible result is "+". In the third example, Nauuo would definitely get one upvote and one downvote, so the only possible result is "0". In the fourth example, if the only one person upvoted, the result would be "+", otherwise, the result would be "-". There are two possible results, so the result is uncertain.
800
false
true
false
false
false
false
false
false
false
false
4,853
1632E2
This version of the problem differs from the previous one only in the constraint on $$$n$$$. A tree is a connected undirected graph without cycles. A weighted tree has a weight assigned to each edge. The distance between two vertices is the minimum sum of weights on the path connecting them. You are given a weighted tree with $$$n$$$ vertices, each edge has a weight of $$$1$$$. Denote $$$d(v)$$$ as the distance between vertex $$$1$$$ and vertex $$$v$$$. Let $$$f(x)$$$ be the minimum possible value of $$$maxlimits_{1 leq v leq n} {d(v)}$$$ if you can temporarily add an edge with weight $$$x$$$ between any two vertices $$$a$$$ and $$$b$$$ $$$(1 le a, b le n)$$$. Note that after this operation, the graph is no longer a tree. For each integer $$$x$$$ from $$$1$$$ to $$$n$$$, find $$$f(x)$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 3 cdot 10^5$$$). Each of the next $$$n−1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u,v le n$$$) indicating that there is an edge between vertices $$$u$$$ and $$$v$$$. It is guaranteed that the given edges form a tree. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3 cdot 10^5$$$. Output For each test case, print $$$n$$$ integers in a single line, $$$x$$$-th of which is equal to $$$f(x)$$$ for all $$$x$$$ from $$$1$$$ to $$$n$$$. Example Input 3 4 1 2 2 3 1 4 2 1 2 7 1 2 1 3 3 4 3 5 3 6 5 7 Output 1 2 2 2 1 1 2 2 3 3 3 3 3 Note In the first testcase: For $$$x = 1$$$, we can an edge between vertices $$$1$$$ and $$$3$$$, then $$$d(1) = 0$$$ and $$$d(2) = d(3) = d(4) = 1$$$, so $$$f(1) = 1$$$. For $$$x ge 2$$$, no matter which edge we add, $$$d(1) = 0$$$, $$$d(2) = d(4) = 1$$$ and $$$d(3) = 2$$$, so $$$f(x) = 2$$$.
2,700
false
false
false
false
false
false
false
true
false
false
2,453
986D
Problem - 986D - 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 fft math *3100 No tag edit access → Contest materials ")
3,100
true
false
false
false
false
false
false
false
false
false
5,769
1236C
In order to do some research, $$$n^2$$$ labs are built on different heights of a mountain. Let's enumerate them with integers from $$$1$$$ to $$$n^2$$$, such that the lab with the number $$$1$$$ is at the lowest place, the lab with the number $$$2$$$ is at the second-lowest place, $$$ldots$$$, the lab with the number $$$n^2$$$ is at the highest place. To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number $$$u$$$ to the lab with the number $$$v$$$ if $$$u > v$$$. Now the labs need to be divided into $$$n$$$ groups, each group should contain exactly $$$n$$$ labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group $$$A$$$ to a group $$$B$$$ is equal to the number of pairs of labs ($$$u, v$$$) such that the lab with the number $$$u$$$ is from the group $$$A$$$, the lab with the number $$$v$$$ is from the group $$$B$$$ and $$$u > v$$$. Let's denote this value as $$$f(A,B)$$$ (i.e. $$$f(A,B)$$$ is the sum of units of water that can be sent from a group $$$A$$$ to a group $$$B$$$). For example, if $$$n=3$$$ and there are $$$3$$$ groups $$$X$$$, $$$Y$$$ and $$$Z$$$: $$$X = {1, 5, 6}, Y = {2, 4, 9}$$$ and $$$Z = {3, 7, 8}$$$. In this case, the values of $$$f$$$ are equal to: $$$f(X,Y)=4$$$ because of $$$5 ightarrow 2$$$, $$$5 ightarrow 4$$$, $$$6 ightarrow 2$$$, $$$6 ightarrow 4$$$, $$$f(X,Z)=2$$$ because of $$$5 ightarrow 3$$$, $$$6 ightarrow 3$$$, $$$f(Y,X)=5$$$ because of $$$2 ightarrow 1$$$, $$$4 ightarrow 1$$$, $$$9 ightarrow 1$$$, $$$9 ightarrow 5$$$, $$$9 ightarrow 6$$$, $$$f(Y,Z)=4$$$ because of $$$4 ightarrow 3$$$, $$$9 ightarrow 3$$$, $$$9 ightarrow 7$$$, $$$9 ightarrow 8$$$, $$$f(Z,X)=7$$$ because of $$$3 ightarrow 1$$$, $$$7 ightarrow 1$$$, $$$7 ightarrow 5$$$, $$$7 ightarrow 6$$$, $$$8 ightarrow 1$$$, $$$8 ightarrow 5$$$, $$$8 ightarrow 6$$$, $$$f(Z,Y)=5$$$ because of $$$3 ightarrow 2$$$, $$$7 ightarrow 2$$$, $$$7 ightarrow 4$$$, $$$8 ightarrow 2$$$, $$$8 ightarrow 4$$$. Please, divide labs into $$$n$$$ groups with size $$$n$$$, such that the value $$$min f(A,B)$$$ over all possible pairs of groups $$$A$$$ and $$$B$$$ ($$$A eq B$$$) is maximal. In other words, divide labs into $$$n$$$ groups with size $$$n$$$, such that minimum number of the sum of units of water that can be transported from a group $$$A$$$ to a group $$$B$$$ for every pair of different groups $$$A$$$ and $$$B$$$ ($$$A eq B$$$) as big as possible. Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values $$$f$$$ for some division. If there are many optimal divisions, you can find any. Input The only line contains one number $$$n$$$ ($$$2 leq n leq 300$$$). Output Output $$$n$$$ lines: In the $$$i$$$-th line print $$$n$$$ numbers, the numbers of labs of the $$$i$$$-th group, in any order you want. If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any. Note In the first test we can divide $$$9$$$ labs into groups $$${2, 8, 5}, {9, 3, 4}, {7, 6, 1}$$$. From the first group to the second group we can transport $$$4$$$ units of water ($$$8 ightarrow 3, 8 ightarrow 4, 5 ightarrow 3, 5 ightarrow 4$$$). From the first group to the third group we can transport $$$5$$$ units of water ($$$2 ightarrow 1, 8 ightarrow 7, 8 ightarrow 6, 8 ightarrow 1, 5 ightarrow 1$$$). From the second group to the first group we can transport $$$5$$$ units of water ($$$9 ightarrow 2, 9 ightarrow 8, 9 ightarrow 5, 3 ightarrow 2, 4 ightarrow 2$$$). From the second group to the third group we can transport $$$5$$$ units of water ($$$9 ightarrow 7, 9 ightarrow 6, 9 ightarrow 1, 3 ightarrow 1, 4 ightarrow 1$$$). From the third group to the first group we can transport $$$4$$$ units of water ($$$7 ightarrow 2, 7 ightarrow 5, 6 ightarrow 2, 6 ightarrow 5$$$). From the third group to the second group we can transport $$$4$$$ units of water ($$$7 ightarrow 3, 7 ightarrow 4, 6 ightarrow 3, 6 ightarrow 4$$$). The minimal number of the sum of units of water, that can be transported from one group to another is equal to $$$4$$$. It can be proved, that it is impossible to make a better division.
1,300
false
true
true
false
false
true
false
false
false
false
4,516
1140F
For a given set of two-dimensional points $$$S$$$, let's denote its extension $$$E(S)$$$ as the result of the following algorithm: Create another set of two-dimensional points $$$R$$$, which is initially equal to $$$S$$$. Then, while there exist four numbers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ such that $$$(x_1, y_1) in R$$$, $$$(x_1, y_2) in R$$$, $$$(x_2, y_1) in R$$$ and $$$(x_2, y_2) otin R$$$, add $$$(x_2, y_2)$$$ to $$$R$$$. When it is impossible to find such four integers, let $$$R$$$ be the result of the algorithm. Now for the problem itself. You are given a set of two-dimensional points $$$S$$$, which is initially empty. You have to process two types of queries: add some point to $$$S$$$, or remove some point from it. After each query you have to compute the size of $$$E(S)$$$. Input The first line contains one integer $$$q$$$ ($$$1 le q le 3 cdot 10^5$$$) — the number of queries. Then $$$q$$$ lines follow, each containing two integers $$$x_i$$$, $$$y_i$$$ ($$$1 le x_i, y_i le 3 cdot 10^5$$$), denoting $$$i$$$-th query as follows: if $$$(x_i, y_i) in S$$$, erase it from $$$S$$$, otherwise insert $$$(x_i, y_i)$$$ into $$$S$$$. Output Print $$$q$$$ integers. $$$i$$$-th integer should be equal to the size of $$$E(S)$$$ after processing first $$$i$$$ queries. Example Input 7 1 1 1 2 2 1 2 2 1 2 1 3 2 1
2,600
false
false
false
false
true
false
false
false
false
false
5,023
645B
It is a balmy spring afternoon, and Farmer John's _n_ cows are ruminating about link-cut cacti in their stalls. The cows, labeled 1 through _n_, are arranged so that the _i_-th cow occupies the _i_-th stall from the left. However, Elsie, after realizing that she will forever live in the shadows beyond Bessie's limelight, has formed the Mischievous Mess Makers and is plotting to disrupt this beautiful pastoral rhythm. While Farmer John takes his _k_ minute long nap, Elsie and the Mess Makers plan to repeatedly choose two distinct stalls and swap the cows occupying those stalls, making no more than one swap each minute. Being the meticulous pranksters that they are, the Mischievous Mess Makers would like to know the maximum messiness attainable in the _k_ minutes that they have. We denote as _p__i_ the label of the cow in the _i_-th stall. The messiness of an arrangement of cows is defined as the number of pairs (_i_,u2009_j_) such that _i_u2009<u2009_j_ and _p__i_u2009>u2009_p__j_. Input The first line of the input contains two integers _n_ and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u2009100u2009000)xa0— the number of cows and the length of Farmer John's nap, respectively. Output Output a single integer, the maximum messiness that the Mischievous Mess Makers can achieve by performing no more than _k_ swaps. Note In the first sample, the Mischievous Mess Makers can swap the cows in the stalls 1 and 5 during the first minute, then the cows in stalls 2 and 4 during the second minute. This reverses the arrangement of cows, giving us a total messiness of 10. In the second sample, there is only one cow, so the maximum possible messiness is 0.
1,200
true
true
false
false
false
false
false
false
false
false
7,233
1242A
Ujan has been lazy lately, but now has decided to bring his yard to good shape. First, he decided to paint the path from his house to the gate. The path consists of $$$n$$$ consecutive tiles, numbered from $$$1$$$ to $$$n$$$. Ujan will paint each tile in some color. He will consider the path aesthetic if for any two different tiles with numbers $$$i$$$ and $$$j$$$, such that $$$j - i$$$ is a divisor of $$$n$$$ greater than $$$1$$$, they have the same color. Formally, the colors of two tiles with numbers $$$i$$$ and $$$j$$$ should be the same if $$$i-j > 1$$$ and $$$n bmod i-j = 0$$$ (where $$$x bmod y$$$ is the remainder when dividing $$$x$$$ by $$$y$$$). Ujan wants to brighten up space. What is the maximum number of different colors that Ujan can use, so that the path is aesthetic? Input The first line of input contains a single integer $$$n$$$ ($$$1 leq n leq 10^{12}$$$), the length of the path. Output Output a single integer, the maximum possible number of colors that the path can be painted in. Note In the first sample, two colors is the maximum number. Tiles $$$1$$$ and $$$3$$$ should have the same color since $$$4 bmod 3-1 = 0$$$. Also, tiles $$$2$$$ and $$$4$$$ should have the same color since $$$4 bmod 4-2 = 0$$$. In the second sample, all five colors can be used.
1,500
true
false
false
false
false
true
false
false
false
false
4,489
1320E
There are $$$n$$$ cities in Treeland connected with $$$n - 1$$$ bidirectional roads in such that a way that any city is reachable from any other; in other words, the graph of cities and roads is a tree. Treeland is preparing for a seasonal virus epidemic, and currently, they are trying to evaluate different infection scenarios. In each scenario, several cities are initially infected with different virus species. Suppose that there are $$$k_i$$$ virus species in the $$$i$$$-th scenario. Let us denote $$$v_j$$$ the initial city for the virus $$$j$$$, and $$$s_j$$$ the propagation speed of the virus $$$j$$$. The spread of the viruses happens in turns: first virus $$$1$$$ spreads, followed by virus $$$2$$$, and so on. After virus $$$k_i$$$ spreads, the process starts again from virus $$$1$$$. A spread turn of virus $$$j$$$ proceeds as follows. For each city $$$x$$$ not infected with any virus at the start of the turn, at the end of the turn it becomes infected with virus $$$j$$$ if and only if there is such a city $$$y$$$ that: city $$$y$$$ was infected with virus $$$j$$$ at the start of the turn; the path between cities $$$x$$$ and $$$y$$$ contains at most $$$s_j$$$ edges; all cities on the path between cities $$$x$$$ and $$$y$$$ (excluding $$$y$$$) were uninfected with any virus at the start of the turn. Once a city is infected with a virus, it stays infected indefinitely and can not be infected with any other virus. The spread stops once all cities are infected. You need to process $$$q$$$ independent scenarios. Each scenario is described by $$$k_i$$$ virus species and $$$m_i$$$ important cities. For each important city determine which the virus it will be infected by in the end. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0— the number of cities in Treeland. The following $$$n - 1$$$ lines describe the roads. The $$$i$$$-th of these lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 leq x_i, y_i leq n$$$)xa0— indices of cities connecting by the $$$i$$$-th road. It is guaranteed that the given graph of cities and roads is a tree. The next line contains a single integer $$$q$$$ ($$$1 leq q leq 2 cdot 10^5$$$)xa0— the number of infection scenarios. $$$q$$$ scenario descriptions follow. The description of the $$$i$$$-th scenario starts with a line containing two integers $$$k_i$$$ and $$$m_i$$$ ($$$1 leq k_i, m_i leq n$$$)xa0— the number of virus species and the number of important cities in this scenario respectively. It is guaranteed that $$$sum_{i = 1}^ q k_i$$$ and $$$sum_{i = 1}^ q m_i$$$ do not exceed $$$2 cdot 10^5$$$. The following $$$k_i$$$ lines describe the virus species. The $$$j$$$-th of these lines contains two integers $$$v_j$$$ and $$$s_j$$$ ($$$1 leq v_j leq n$$$, $$$1 leq s_j leq 10^6$$$)xa0– the initial city and the propagation speed of the virus species $$$j$$$. It is guaranteed that the initial cities of all virus species within a scenario are distinct. The following line contains $$$m_i$$$ distinct integers $$$u_1, ldots, u_{m_i}$$$ ($$$1 leq u_j leq n$$$)xa0— indices of important cities. Output Print $$$q$$$ lines. The $$$i$$$-th line should contain $$$m_i$$$ integersxa0— indices of virus species that cities $$$u_1, ldots, u_{m_i}$$$ are infected with at the end of the $$$i$$$-th scenario. Example Input 7 1 2 1 3 2 4 2 5 3 6 3 7 3 2 2 4 1 7 1 1 3 2 2 4 3 7 1 1 3 3 3 1 1 4 100 7 100 1 2 3
3,000
false
false
false
true
true
false
false
false
false
false
4,111
32D
A star map in Berland is a checked field _n_u2009×u2009_m_ squares. In each square there is or there is not a star. The favourite constellation of all Berland's astronomers is the constellation of the Cross. This constellation can be formed by any 5 stars so, that for some integer _x_ (radius of the constellation) the following is true: the 2nd is on the same vertical line as the 1st, but _x_ squares up the 3rd is on the same vertical line as the 1st, but _x_ squares down the 4th is on the same horizontal line as the 1st, but _x_ squares left the 5th is on the same horizontal line as the 1st, but _x_ squares right Such constellations can be very numerous, that's why they are numbered with integers from 1 on the following principle: when two constellations are compared, the one with a smaller radius gets a smaller index; if their radii are equal — the one, whose central star if higher than the central star of the other one; if their central stars are at the same level — the one, whose central star is to the left of the central star of the other one. Your task is to find the constellation with index _k_ by the given Berland's star map. Input The first line contains three integers _n_, _m_ and _k_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009300,u20091u2009≤u2009_k_u2009≤u20093·107) — height and width of the map and index of the required constellation respectively. The upper-left corner has coordinates (1,u20091), and the lower-right — (_n_,u2009_m_). Then there follow _n_ lines, _m_ characters each — description of the map. _j_-th character in _i_-th line is «*», if there is a star in the corresponding square, and «.» if this square is empty. Output If the number of the constellations is less than _k_, output -1. Otherwise output 5 lines, two integers each — coordinates of the required constellation. Output the stars in the following order: central, upper, lower, left, right. Examples Input 5 6 1 ....*. ...** ....*. ..*... .***.. Input 5 6 2 ....*. ...** ....*. ..*... .***.. Input 7 7 2 ...*... ....... ...*... *.***. ...*... ....... ...*...
1,600
false
false
true
false
false
false
false
false
false
false
9,835
543B
In some country there are exactly _n_ cities and _m_ bidirectional roads connecting the cities. Cities are numbered with integers from 1 to _n_. If cities _a_ and _b_ are connected by a road, then in an hour you can go along this road either from city _a_ to city _b_, or from city _b_ to city _a_. The road network is such that from any city you can get to any other one by moving along the roads. You want to destroy the largest possible number of roads in the country so that the remaining roads would allow you to get from city _s_1 to city _t_1 in at most _l_1 hours and get from city _s_2 to city _t_2 in at most _l_2 hours. Determine what maximum number of roads you need to destroy in order to meet the condition of your plan. If it is impossible to reach the desired result, print -1. Input The first line contains two integers _n_, _m_ (1u2009≤u2009_n_u2009≤u20093000, )xa0— the number of cities and roads in the country, respectively. Next _m_ lines contain the descriptions of the roads as pairs of integers _a__i_, _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_). It is guaranteed that the roads that are given in the description can transport you from any city to any other one. It is guaranteed that each pair of cities has at most one road between them. The last two lines contains three integers each, _s_1, _t_1, _l_1 and _s_2, _t_2, _l_2, respectively (1u2009≤u2009_s__i_,u2009_t__i_u2009≤u2009_n_, 0u2009≤u2009_l__i_u2009≤u2009_n_). Output Print a single number — the answer to the problem. If the it is impossible to meet the conditions, print -1. Examples Input 5 4 1 2 2 3 3 4 4 5 1 3 2 3 5 2 Input 5 4 1 2 2 3 3 4 4 5 1 3 2 2 4 2 Input 5 4 1 2 2 3 3 4 4 5 1 3 2 3 5 1
2,100
false
false
false
false
false
true
false
false
false
true
7,668
710A
Problem - 710A - Codeforces =============== xa0
800
false
false
true
false
false
false
false
false
false
false
6,982
1615C
There are $$$n$$$ candles on a Hanukkah menorah, and some of its candles are initially lit. We can describe which candles are lit with a binary string $$$s$$$, where the $$$i$$$-th candle is lit if and only if $$$s_i=1$$$. Initially, the candle lights are described by a string $$$a$$$. In an operation, you select a candle that is currently lit. By doing so, the candle you selected will remain lit, and every other candle will change (if it was lit, it will become unlit and if it was unlit, it will become lit). You would like to make the candles look the same as string $$$b$$$. Your task is to determine if it is possible, and if it is, find the minimum number of operations required. Input The first line contains an integer $$$t$$$ ($$$1le tle 10^4$$$)xa0— the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1le nle 10^5$$$)xa0— the number of candles. The second line contains a string $$$a$$$ of length $$$n$$$ consisting of symbols 0 and 1xa0— the initial pattern of lights. The third line contains a string $$$b$$$ of length $$$n$$$ consisting of symbols 0 and 1xa0— the desired pattern of lights. It is guaranteed that the sum of $$$n$$$ does not exceed $$$10^5$$$. Output For each test case, output the minimum number of operations required to transform $$$a$$$ to $$$b$$$, or $$$-1$$$ if it's impossible. Example Input 5 5 11010 11010 2 01 11 3 000 101 9 100010111 101101100 9 001011011 011010101 Note In the first test case, the two strings are already equal, so we don't have to perform any operations. In the second test case, we can perform a single operation selecting the second candle to transform $$$01$$$ into $$$11$$$. In the third test case, it's impossible to perform any operations because there are no lit candles to select. In the fourth test case, we can perform the following operations to transform $$$a$$$ into $$$b$$$: 1. Select the $$$7$$$-th candle: $$$100010{color{red}1}11 o 011101{color{red} 1}00$$$. 2. Select the $$$2$$$-nd candle: $$$0{color{red} 1}1101100 o 1{color{red} 1}0010011$$$. 3. Select the $$$1$$$-st candle: $$${color{red}1}10010011 o {color{red}1}01101100$$$. In the fifth test case, we can perform the following operations to transform $$$a$$$ into $$$b$$$: 1. Select the $$$6$$$-th candle: $$$00101{color{red}1}011 o 11010{color{red}1}100$$$ 2. Select the $$$2$$$-nd candle: $$$1{color{red}1}0101100 o 0{color{red}1}1010011$$$ 3. Select the $$$8$$$-th candle: $$$0110100{color{red}1}1 o 1001011{color{red}1}0$$$ 4. Select the $$$7$$$-th candle: $$$100101{color{red}1}10 o 011010{color{red}1}01$$$
1,600
true
true
false
false
false
false
true
false
false
true
2,562
1379D
There are many freight trains departing from Kirnes planet every day. One day on that planet consists of $$$h$$$ hours, and each hour consists of $$$m$$$ minutes, where $$$m$$$ is an even number. Currently, there are $$$n$$$ freight trains, and they depart every day at the same time: $$$i$$$-th train departs at $$$h_i$$$ hours and $$$m_i$$$ minutes. The government decided to add passenger trams as well: they plan to add a regular tram service with half-hour intervals. It means that the first tram of the day must depart at $$$0$$$ hours and $$$t$$$ minutes, where $$$0 le t < {m over 2}$$$, the second tram departs $$$m over 2$$$ minutes after the first one and so on. This schedule allows exactly two passenger trams per hour, which is a great improvement. To allow passengers to board the tram safely, the tram must arrive $$$k$$$ minutes before. During the time when passengers are boarding the tram, no freight train can depart from the planet. However, freight trains are allowed to depart at the very moment when the boarding starts, as well as at the moment when the passenger tram departs. Note that, if the first passenger tram departs at $$$0$$$ hours and $$$t$$$ minutes, where $$$t < k$$$, then the freight trains can not depart during the last $$$k - t$$$ minutes of the day. A schematic picture of the correct way to run passenger trams. Here $$$h=2$$$ (therefore, the number of passenger trams is $$$2h=4$$$), the number of freight trains is $$$n=6$$$. The passenger trams are marked in red (note that the spaces between them are the same). The freight trains are marked in blue. Time segments of length $$$k$$$ before each passenger tram are highlighted in red. Note that there are no freight trains inside these segments. Unfortunately, it might not be possible to satisfy the requirements of the government without canceling some of the freight trains. Please help the government find the optimal value of $$$t$$$ to minimize the number of canceled freight trains in case all passenger trams depart according to schedule. Input The first line of input contains four integers $$$n$$$, $$$h$$$, $$$m$$$, $$$k$$$ ($$$1 le n le 100,000$$$, $$$1 le h le 10^9$$$, $$$2 le m le 10^9$$$, $$$m$$$ is even, $$$1 le k le {m over 2}$$$)xa0— the number of freight trains per day, the number of hours and minutes on the planet, and the boarding time for each passenger tram. $$$n$$$ lines follow, each contains two integers $$$h_i$$$ and $$$m_i$$$ ($$$0 le h_i < h$$$, $$$0 le m_i < m$$$)xa0— the time when $$$i$$$-th freight train departs. It is guaranteed that no freight trains depart at the same time. Output The first line of output should contain two integers: the minimum number of trains that need to be canceled, and the optimal starting time $$$t$$$. Second line of output should contain freight trains that need to be canceled. Examples Input 2 24 60 15 16 0 17 15 Input 2 24 60 16 16 0 17 15 Note In the first test case of the example the first tram can depart at 0 hours and 0 minutes. Then the freight train at 16 hours and 0 minutes can depart at the same time as the passenger tram, and the freight train at 17 hours and 15 minutes can depart at the same time as the boarding starts for the upcoming passenger tram. In the second test case of the example it is not possible to design the passenger tram schedule without cancelling any of the freight trains: if $$$t in [1, 15]$$$, then the freight train at 16 hours and 0 minutes is not able to depart (since boarding time is 16 minutes). If $$$t = 0$$$ or $$$t in [16, 29]$$$, then the freight train departing at 17 hours 15 minutes is not able to depart. However, if the second freight train is canceled, one can choose $$$t = 0$$$. Another possible option is to cancel the first train and choose $$$t = 13$$$.
2,300
false
false
false
false
true
false
true
true
true
false
3,775
1132C
Problem - 1132C - 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 *1700 No tag edit access → Contest materials — the number of sections and the number of painters availible for hire, respectively. Then $$$q$$$ lines follow, each describing one of the painters: $$$i$$$-th line contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$). Output Print one integer — maximum number of painted sections if you hire $$$q - 2$$$ painters. Examples Input 7 5 1 4 4 5 5 6 6 7 3 5 Output 7 Input 4 3 1 1 2 2 3 4 Output 2 Input 4 4 1 1 2 2 2 3 3 4 Output 3
1,700
false
false
false
false
false
false
true
false
false
false
5,059
1175C
At first, there was a legend related to the name of the problem, but now it's just a formal statement. You are given $$$n$$$ points $$$a_1, a_2, dots, a_n$$$ on the $$$OX$$$ axis. Now you are asked to find such an integer point $$$x$$$ on $$$OX$$$ axis that $$$f_k(x)$$$ is minimal possible. The function $$$f_k(x)$$$ can be described in the following way: form a list of distances $$$d_1, d_2, dots, d_n$$$ where $$$d_i = a_i - x$$$ (distance between $$$a_i$$$ and $$$x$$$); sort list $$$d$$$ in non-descending order; take $$$d_{k + 1}$$$ as a result. If there are multiple optimal answers you can print any of them. Input The first line contains single integer $$$T$$$ ($$$ 1 le T le 2 cdot 10^5$$$) — number of queries. Next $$$2 cdot T$$$ lines contain descriptions of queries. All queries are independent. The first line of each query contains two integers $$$n$$$, $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$0 le k < n$$$) — the number of points and constant $$$k$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_1 < a_2 < dots < a_n le 10^9$$$) — points in ascending order. It's guaranteed that $$$sum{n}$$$ doesn't exceed $$$2 cdot 10^5$$$. Output Print $$$T$$$ integers — corresponding points $$$x$$$ which have minimal possible value of $$$f_k(x)$$$. If there are multiple answers you can print any of them. Example Input 3 3 2 1 2 5 2 1 1 1000000000 1 0 4
1,600
false
true
false
false
false
false
true
true
false
false
4,843
1659D
Suppose you had an array $$$A$$$ of $$$n$$$ elements, each of which is $$$0$$$ or $$$1$$$. Let us define a function $$$f(k,A)$$$ which returns another array $$$B$$$, the result of sorting the first $$$k$$$ elements of $$$A$$$ in non-decreasing order. For example, $$$f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$$$. Note that the first $$$4$$$ elements were sorted. Now consider the arrays $$$B_1, B_2,ldots, B_n$$$ generated by $$$f(1,A), f(2,A),ldots,f(n,A)$$$. Let $$$C$$$ be the array obtained by taking the element-wise sum of $$$B_1, B_2,ldots, B_n$$$. For example, let $$$A=[0,1,0,1]$$$. Then we have $$$B_1=[0,1,0,1]$$$, $$$B_2=[0,1,0,1]$$$, $$$B_3=[0,0,1,1]$$$, $$$B_4=[0,0,1,1]$$$. Then $$$C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]$$$. You are given $$$C$$$. Determine a binary array $$$A$$$ that would give $$$C$$$ when processed as above. It is guaranteed that an array $$$A$$$ exists for given $$$C$$$ in the input. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$) xa0— the number of test cases. Each test case has two lines. The first line contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$c_1, c_2, ldots, c_n$$$ ($$$0 leq c_i leq n$$$). It is guaranteed that a valid array $$$A$$$ exists for the given $$$C$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Note Here's the explanation for the first test case. Given that $$$A=[1,1,0,1]$$$, we can construct each $$$B_i$$$: $$$B_1=[color{blue}{1},1,0,1]$$$; $$$B_2=[color{blue}{1},color{blue}{1},0,1]$$$; $$$B_3=[color{blue}{0},color{blue}{1},color{blue}{1},1]$$$; $$$B_4=[color{blue}{0},color{blue}{1},color{blue}{1},color{blue}{1}]$$$ And then, we can sum up each column above to get $$$C=[1+1+0+0,1+1+1+1,0+0+1+1,1+1+1+1]=[2,4,2,4]$$$.
1,900
true
true
true
false
true
true
false
false
false
false
2,329
1575F
Mr. Chanek opened a letter from his fellow, who is currently studying at Singanesia. Here is what it says. Define an array $$$b$$$ ($$$0 leq b_i < k$$$) with $$$n$$$ integers. While there exists a pair $$$(i, j)$$$ such that $$$b_i e b_j$$$, do the following operation: Randomly pick a number $$$i$$$ satisfying $$$0 leq i < n$$$. Note that each number $$$i$$$ has a probability of $$$frac{1}{n}$$$ to be picked. Randomly Pick a number $$$j$$$ satisfying $$$0 leq j < k$$$. Change the value of $$$b_i$$$ to $$$j$$$. It is possible for $$$b_i$$$ to be changed to the same value. Denote $$$f(b)$$$ as the expected number of operations done to $$$b$$$ until all elements of $$$b$$$ are equal. You are given two integers $$$n$$$ and $$$k$$$, and an array $$$a$$$ ($$$-1 leq a_i < k$$$) of $$$n$$$ integers. For every index $$$i$$$ with $$$a_i = -1$$$, replace $$$a_i$$$ with a random number $$$j$$$ satisfying $$$0 leq j < k$$$. Let $$$c$$$ be the number of occurrences of $$$-1$$$ in $$$a$$$. There are $$$k^c$$$ possibilites of $$$a$$$ after the replacement, each with equal probability of being the final array. Find the expected value of $$$f(a)$$$ modulo $$$10^9 + 7$$$. Formally, let $$$M = 10^9 + 7$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output the integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 le x < M$$$ and $$$x cdot q equiv p pmod{M}$$$. After reading the letter, Mr. Chanek gave the task to you. Solve it for the sake of their friendship! Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 leq n leq 10^5$$$, $$$2 leq k leq 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-1 leq a_i < k$$$).
2,900
true
false
false
false
false
false
false
false
false
false
2,763
290D
Problem - 290D - Codeforces =============== xa0 . Each character will be a letter of English alphabet, lowercase or uppercase. The second line of the input is an integer between 0 and 26, inclusive. Output Output the required string. Examples Input AprilFool 14 Output AprILFooL
1,400
false
false
true
false
false
false
false
false
false
false
8,674
1209D
The legendary Farmer John is throwing a huge party, and animals from all over the world are hanging out at his house. His guests are hungry, so he instructs his cow Bessie to bring out the snacks! Moo! There are $$$n$$$ snacks flavors, numbered with integers $$$1, 2, ldots, n$$$. Bessie has $$$n$$$ snacks, one snack of each flavor. Every guest has exactly two favorite flavors. The procedure for eating snacks will go as follows: First, Bessie will line up the guests in some way. Then in this order, guests will approach the snacks one by one. Each guest in their turn will eat all remaining snacks of their favorite flavor. In case no favorite flavors are present when a guest goes up, they become very sad. Help Bessie to minimize the number of sad guests by lining the guests in an optimal way. Input The first line contains integers $$$n$$$ and $$$k$$$ ($$$2 le n le 10^5$$$, $$$1 le k le 10^5$$$), the number of snacks and the number of guests. The $$$i$$$-th of the following $$$k$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$, $$$x_i e y_i$$$), favorite snack flavors of the $$$i$$$-th guest. Output Output one integer, the smallest possible number of sad guests. Examples Input 6 5 2 3 2 1 3 4 6 5 4 5 Note In the first example, Bessie can order the guests like this: $$$3, 1, 2, 4$$$. Guest $$$3$$$ goes first and eats snacks $$$1$$$ and $$$4$$$. Then the guest $$$1$$$ goes and eats the snack $$$2$$$ only, because the snack $$$1$$$ has already been eaten. Similarly, the guest $$$2$$$ goes up and eats the snack $$$3$$$ only. All the snacks are gone, so the guest $$$4$$$ will be sad. In the second example, one optimal ordering is $$$2, 1, 3, 5, 4$$$. All the guests will be satisfied.
1,700
false
false
false
false
false
false
false
false
false
true
4,643
1419D1
This is the easy version of the problem. The difference between the versions is that in the easy version all prices $$$a_i$$$ are different. You can make hacks if and only if you solved both versions of the problem. Today is Sage's birthday, and she will go shopping to buy ice spheres. All $$$n$$$ ice spheres are placed in a row and they are numbered from $$$1$$$ to $$$n$$$ from left to right. Each ice sphere has a positive integer price. In this version all prices are different. An ice sphere is cheap if it costs strictly less than two neighboring ice spheres: the nearest to the left and the nearest to the right. The leftmost and the rightmost ice spheres are not cheap. Sage will choose all cheap ice spheres and then buy only them. You can visit the shop before Sage and reorder the ice spheres as you wish. Find out the maximum number of ice spheres that Sage can buy, and show how the ice spheres should be reordered. Input The first line contains a single integer $$$n$$$ $$$(1 le n le 10^5)$$$xa0— the number of ice spheres in the shop. The second line contains $$$n$$$ different integers $$$a_1, a_2, dots, a_n$$$ $$$(1 le a_i le 10^9)$$$xa0— the prices of ice spheres. Output In the first line print the maximum number of ice spheres that Sage can buy. In the second line print the prices of ice spheres in the optimal order. If there are several correct answers, you can print any of them. Note In the example it's not possible to place ice spheres in any order so that Sage would buy $$$3$$$ of them. If the ice spheres are placed like this $$$(3, 1, 4, 2, 5)$$$, then Sage will buy two spheres: one for $$$1$$$ and one for $$$2$$$, because they are cheap.
1,000
false
true
false
false
false
true
false
true
true
false
3,584
1070H
Polycarp is working on a new operating system called BerOS. He asks you to help with implementation of a file suggestion feature. There are $$$n$$$ files on hard drive and their names are $$$f_1, f_2, dots, f_n$$$. Any file name contains between $$$1$$$ and $$$8$$$ characters, inclusive. All file names are unique. The file suggestion feature handles queries, each represented by a string $$$s$$$. For each query $$$s$$$ it should count number of files containing $$$s$$$ as a substring (i.e. some continuous segment of characters in a file name equals $$$s$$$) and suggest any such file name. For example, if file names are "read.me", "hosts", "ops", and "beros.18", and the query is "os", the number of matched files is $$$2$$$ (two file names contain "os" as a substring) and suggested file name can be either "hosts" or "beros.18". Input The first line of the input contains integer $$$n$$$ ($$$1 le n le 10000$$$) — the total number of files. The following $$$n$$$ lines contain file names, one per line. The $$$i$$$-th line contains $$$f_i$$$ — the name of the $$$i$$$-th file. Each file name contains between $$$1$$$ and $$$8$$$ characters, inclusive. File names contain only lowercase Latin letters, digits and dot characters ('.'). Any sequence of valid characters can be a file name (for example, in BerOS ".", ".." and "..." are valid file names). All file names are unique. The following line contains integer $$$q$$$ ($$$1 le q le 50000$$$) — the total number of queries. The following $$$q$$$ lines contain queries $$$s_1, s_2, dots, s_q$$$, one per line. Each $$$s_j$$$ has length between $$$1$$$ and $$$8$$$ characters, inclusive. It contains only lowercase Latin letters, digits and dot characters ('.'). Output Print $$$q$$$ lines, one per query. The $$$j$$$-th line should contain the response on the $$$j$$$-th query — two values $$$c_j$$$ and $$$t_j$$$, where $$$c_j$$$ is the number of matched files for the $$$j$$$-th query, $$$t_j$$$ is the name of any file matched by the $$$j$$$-th query. If there is no such file, print a single character '-' instead. If there are multiple matched files, print any. Example Input 4 test contests test. .test 6 ts . st. .test contes. st Output 1 contests 2 .test 1 test. 1 .test 0 - 4 test.
1,500
false
false
true
false
false
false
true
false
false
false
5,360
964A
Problem - 964A - 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 *800 No tag edit access → Contest materials ") . Output Output one integerxa0— the answer to the problem. Examples Input 7 Output 4 Input 8 Output 5 Input 9 Output 5 Note In the first sample, there are following possible weights of splits of $$$7$$$: Weight 1:
800
true
false
false
false
false
false
false
false
false
false
5,844
1567F
Alice has an empty grid with $$$n$$$ rows and $$$m$$$ columns. Some of the cells are marked, and no marked cells are adjacent to the edge of the grid. (Two squares are adjacent if they share a side.) Alice wants to fill each cell with a number such that the following statements are true: every unmarked cell contains either the number $$$1$$$ or $$$4$$$; every marked cell contains the sum of the numbers in all unmarked cells adjacent to it (if a marked cell is not adjacent to any unmarked cell, this sum is $$$0$$$); every marked cell contains a multiple of $$$5$$$. Alice couldn't figure it out, so she asks Bob to help her. Help Bob find any such grid, or state that no such grid exists. Input The first line of input contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 500$$$)xa0— the number of rows and the number of columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or 'X'xa0— an unmarked and a marked cell, respectively. No marked cells are adjacent to the edge of the grid. Output Output "'NO" if no suitable grid exists. Otherwise, output "'YES"'. Then output $$$n$$$ lines of $$$m$$$ space-separated integersxa0— the integers in the grid. Examples Input 5 5 ..... .XXX. .X.X. .XXX. ..... Output YES 4 1 4 4 1 4 5 5 5 1 4 5 1 5 4 1 5 5 5 4 1 4 4 1 4 Input 5 5 ..... .XXX. .XXX. .XXX. ..... Input 9 9 ......... .XXXXX.X. .X...X... .X.XXXXX. .X.X.X.X. .X.XXX.X. .X.....X. .XXXXXXX. ......... Output YES 4 4 4 1 4 1 4 1 4 1 5 5 5 5 5 4 10 1 4 5 1 4 1 5 4 4 4 4 5 1 5 5 0 5 5 1 4 5 1 5 4 5 1 5 4 4 5 1 5 5 5 4 5 1 1 5 4 4 1 1 4 5 1 4 5 5 5 5 5 5 5 4 1 1 1 1 4 4 1 1 4 Note It can be shown that no such grid exists for the second test.
2,700
false
false
true
false
false
true
false
false
false
true
2,799
633A
Problem - 633A - Codeforces =============== xa0 ]( "Announcement") xa0— the number of units of damage dealt by Ebony gun and Ivory gun, and the total number of damage required to break the shield, respectively. Output Print "Yes" (without quotes) if Dante can deal exactly _c_ damage to the shield and "No" (without quotes) otherwise. Examples Input 4 6 15 Output No Input 3 2 7 Output Yes Input 6 11 6 Output Yes Note In the second sample, Dante can fire 1 bullet from Ebony and 2 from Ivory to deal exactly 1·3u2009+u20092·2u2009=u20097 damage. In the third sample, Dante can fire 1 bullet from ebony and no bullets from ivory to do 1·6u2009+u20090·11u2009=u20096 damage.
1,100
true
false
false
false
false
false
true
false
false
false
7,275
1612A
Let's denote the Manhattan distance between two points $$$p_1$$$ (with coordinates $$$(x_1, y_1)$$$) and $$$p_2$$$ (with coordinates $$$(x_2, y_2)$$$) as $$$d(p_1, p_2) = x_1 - x_2 + y_1 - y_2$$$. For example, the distance between two points with coordinates $$$(1, 3)$$$ and $$$(4, 2)$$$ is $$$1 - 4 + 3 - 2 = 4$$$. You are given two points, $$$A$$$ and $$$B$$$. The point $$$A$$$ has coordinates $$$(0, 0)$$$, the point $$$B$$$ has coordinates $$$(x, y)$$$. Your goal is to find a point $$$C$$$ such that: both coordinates of $$$C$$$ are non-negative integers; $$$d(A, C) = dfrac{d(A, B)}{2}$$$ (without any rounding); $$$d(B, C) = dfrac{d(A, B)}{2}$$$ (without any rounding). Find any point $$$C$$$ that meets these constraints, or report that no such point exists. Input The first line contains one integer $$$t$$$ ($$$1 le t le 3000$$$) — the number of test cases. Each test case consists of one line containing two integers $$$x$$$ and $$$y$$$ ($$$0 le x, y le 50$$$) — the coordinates of the point $$$B$$$. Output For each test case, print the answer on a separate line as follows: if it is impossible to find a point $$$C$$$ meeting the constraints, print "-1 -1" (without quotes); otherwise, print two non-negative integers not exceeding $$$10^6$$$ — the coordinates of point $$$C$$$ meeting the constraints. If there are multiple answers, print any of them. It can be shown that if any such point exists, it's possible to find a point with coordinates not exceeding $$$10^6$$$ that meets the constraints. Example Input 10 49 3 2 50 13 0 0 41 42 0 0 36 13 37 42 16 42 13 0 0 Output 23 3 1 25 -1 -1 -1 -1 21 0 0 18 13 12 25 4 -1 -1 0 0 Note Explanations for some of the test cases from the example: In the first test case, the point $$$B$$$ has coordinates $$$(49, 3)$$$. If the point $$$C$$$ has coordinates $$$(23, 3)$$$, then the distance from $$$A$$$ to $$$B$$$ is $$$49 - 0 + 3 - 0 = 52$$$, the distance from $$$A$$$ to $$$C$$$ is $$$23 - 0 + 3 - 0 = 26$$$, and the distance from $$$B$$$ to $$$C$$$ is $$$23 - 49 + 3 - 3 = 26$$$. In the second test case, the point $$$B$$$ has coordinates $$$(2, 50)$$$. If the point $$$C$$$ has coordinates $$$(1, 25)$$$, then the distance from $$$A$$$ to $$$B$$$ is $$$2 - 0 + 50 - 0 = 52$$$, the distance from $$$A$$$ to $$$C$$$ is $$$1 - 0 + 25 - 0 = 26$$$, and the distance from $$$B$$$ to $$$C$$$ is $$$1 - 2 + 25 - 50 = 26$$$. In the third and the fourth test cases, it can be shown that no point with integer coordinates meets the constraints. In the fifth test case, the point $$$B$$$ has coordinates $$$(42, 0)$$$. If the point $$$C$$$ has coordinates $$$(21, 0)$$$, then the distance from $$$A$$$ to $$$B$$$ is $$$42 - 0 + 0 - 0 = 42$$$, the distance from $$$A$$$ to $$$C$$$ is $$$21 - 0 + 0 - 0 = 21$$$, and the distance from $$$B$$$ to $$$C$$$ is $$$21 - 42 + 0 - 0 = 21$$$.
800
false
false
false
false
false
true
true
false
false
false
2,583
605B
Student Vladislav came to his programming exam completely unprepared as usual. He got a question about some strange algorithm on a graphxa0— something that will definitely never be useful in real life. He asked a girl sitting next to him to lend him some cheat papers for this questions and found there the following definition: The minimum spanning tree _T_ of graph _G_ is such a tree that it contains all the vertices of the original graph _G_, and the sum of the weights of its edges is the minimum possible among all such trees. Vladislav drew a graph with _n_ vertices and _m_ edges containing no loops and multiple edges. He found one of its minimum spanning trees and then wrote for each edge its weight and whether it is included in the found tree or not. Unfortunately, the piece of paper where the graph was painted is gone and the teacher is getting very angry and demands to see the original graph. Help Vladislav come up with a graph so that the information about the minimum spanning tree remains correct. Input The first line of the input contains two integers _n_ and _m_ ()xa0— the number of vertices and the number of edges in the graph. Each of the next _m_ lines describes an edge of the graph and consists of two integers _a__j_ and _b__j_ (1u2009≤u2009_a__j_u2009≤u2009109,u2009_b__j_u2009=u2009{0,u20091}). The first of these numbers is the weight of the edge and the second number is equal to 1 if this edge was included in the minimum spanning tree found by Vladislav, or 0 if it was not. It is guaranteed that exactly _n_u2009-u20091 number {_b__j_} are equal to one and exactly _m_u2009-u2009_n_u2009+u20091 of them are equal to zero. Output If Vladislav has made a mistake and such graph doesn't exist, print u2009-u20091. Otherwise print _m_ lines. On the _j_-th line print a pair of vertices (_u__j_,u2009_v__j_) (1u2009≤u2009_u__j_,u2009_v__j_u2009≤u2009_n_,u2009_u__j_u2009≠u2009_v__j_), that should be connected by the _j_-th edge. The edges are numbered in the same order as in the input. The graph, determined by these edges, must be connected, contain no loops or multiple edges and its edges with _b__j_u2009=u20091 must define the minimum spanning tree. In case there are multiple possible solutions, print any of them.
1,700
false
false
false
false
true
true
false
false
false
true
7,425
925C
Vitya has learned that the answer for The Ultimate Question of Life, the Universe, and Everything is not the integer 54 42, but an increasing integer sequence $$$a_1, ldots, a_n$$$. In order to not reveal the secret earlier than needed, Vitya encrypted the answer and obtained the sequence $$$b_1, ldots, b_n$$$ using the following rules: $$$b_1 = a_1$$$; $$$b_i = a_i oplus a_{i - 1}$$$ for all $$$i$$$ from 2 to $$$n$$$, where $$$x oplus y$$$ is the . The second line contains $$$n$$$ integers $$$b_1, ldots, b_n$$$ ($$$1 leq b_i < 2^{60}$$$). Output If there are no valid permutations, print a single line containing "No". Otherwise in the first line print the word "Yes", and in the second line print integers $$$b'_1, ldots, b'_n$$$xa0— a valid permutation of integers $$$b_i$$$. The unordered multisets $$${b_1, ldots, b_n}$$$ and $$${b'_1, ldots, b'_n}$$$ should be equal, i.xa0e. for each integer $$$x$$$ the number of occurrences of $$$x$$$ in the first multiset should be equal to the number of occurrences of $$$x$$$ in the second multiset. Apart from this, the sequence $$$a_i = b'_1 oplus ldots oplus b'_i$$$ should be strictly increasing. If there are multiple answers, print any of them. Note In the first example no permutation is valid. In the second example the given answer lead to the sequence $$$a_1 = 4$$$, $$$a_2 = 8$$$, $$$a_3 = 15$$$, $$$a_4 = 16$$$, $$$a_5 = 23$$$, $$$a_6 = 42$$$.
2,200
true
false
false
false
false
true
false
false
false
false
6,009
960A
A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend B who appends some number of letters 'b' to the end of this string. Since both A and B like the characters 'a' and 'b', they have made sure that at this point, at least one 'a' and one 'b' exist in the string. B now gives this string to C and he appends some number of letters 'c' to the end of the string. However, since C is a good friend of A and B, the number of letters 'c' he appends is equal to the number of 'a' or to the number of 'b' in the string. It is also possible that the number of letters 'c' equals both to the number of letters 'a' and to the number of letters 'b' at the same time. You have a string in your hands, and you want to check if it is possible to obtain the string in this way or not. If it is possible to obtain the string, print "YES", otherwise print "NO" (without the quotes). Input The first and only line consists of a string $$$S$$$ ($$$ 1 le S le 5,000 $$$). It is guaranteed that the string will only consist of the lowercase English letters 'a', 'b', 'c'. Output Print "YES" or "NO", according to the condition. Note Consider first example: the number of 'c' is equal to the number of 'a'. Consider second example: although the number of 'c' is equal to the number of the 'b', the order is not correct. Consider third example: the number of 'c' is equal to the number of 'b'.
1,200
false
false
true
false
false
false
false
false
false
false
5,871
1168C
Toad Pimple has an array of integers $$$a_1, a_2, ldots, a_n$$$. We say that $$$y$$$ is reachable from $$$x$$$ if $$$x<y$$$ and there exists an integer array $$$p$$$ such that $$$x = p_1 < p_2 < ldots < p_k=y$$$, and $$$a_{p_i}, &, a_{p_{i+1}} > 0$$$ for all integers $$$i$$$ such that $$$1 leq i < k$$$. Here $$$&$$$ denotes the xa0— the number of integers in the array and the number of queries you need to answer. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 300,000$$$)xa0— the given array. The next $$$q$$$ lines contain two integers each. The $$$i$$$-th of them contains two space-separated integers $$$x_i$$$ and $$$y_i$$$ ($$$1 leq x_i < y_i leq n$$$). You need to check if $$$y_i$$$ is reachable from $$$x_i$$$. Output Output $$$q$$$ lines. In the $$$i$$$-th of them print "Shi" if $$$y_i$$$ is reachable from $$$x_i$$$, otherwise, print "Fou". Example Input 5 3 1 3 0 2 1 1 3 2 4 1 4 Note In the first example, $$$a_3 = 0$$$. You can't reach it, because AND with it is always zero. $$$a_2, &, a_4 > 0$$$, so $$$4$$$ is reachable from $$$2$$$, and to go from $$$1$$$ to $$$4$$$ you can use $$$p = [1, 2, 4]$$$.
2,200
false
false
false
true
false
false
false
false
false
false
4,874
1380F
Let $$$a$$$ and $$$b$$$ be some non-negative integers. Let's define strange addition of $$$a$$$ and $$$b$$$ as following: 1. write down the numbers one under another and align them by their least significant digit; 2. add them up digit by digit and concatenate the respective sums together. Assume that both numbers have an infinite number of leading zeros. For example, let's take a look at a strange addition of numbers $$$3248$$$ and $$$908$$$: You are given a string $$$c$$$, consisting of $$$n$$$ digits from $$$0$$$ to $$$9$$$. You are also given $$$m$$$ updates of form: $$$x~d$$$xa0— replace the digit at the $$$x$$$-th position of $$$c$$$ with a digit $$$d$$$. Note that string $$$c$$$ might have leading zeros at any point of time. After each update print the number of pairs $$$(a, b)$$$ such that both $$$a$$$ and $$$b$$$ are non-negative integers and the result of a strange addition of $$$a$$$ and $$$b$$$ is equal to $$$c$$$. Note that the numbers of pairs can be quite large, so print them modulo $$$998244353$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 5 cdot 10^5$$$)xa0— the length of the number $$$c$$$ and the number of updates. The second line contains a string $$$c$$$, consisting of exactly $$$n$$$ digits from $$$0$$$ to $$$9$$$. Each of the next $$$m$$$ lines contains two integers $$$x$$$ and $$$d$$$ ($$$1 le x le n$$$, $$$0 le d le 9$$$)xa0— the descriptions of updates. Output Print $$$m$$$ integersxa0— the $$$i$$$-th value should be equal to the number of pairs $$$(a, b)$$$ such that both $$$a$$$ and $$$b$$$ are non-negative integers and the result of a strange addition of $$$a$$$ and $$$b$$$ is equal to $$$c$$$ after $$$i$$$ updates are applied. Note that the numbers of pairs can be quite large, so print them modulo $$$998244353$$$. Note After the first update $$$c$$$ is equal to $$$14$$$. The pairs that sum up to $$$14$$$ are: $$$(0, 14)$$$, $$$(1, 13)$$$, $$$(2, 12)$$$, $$$(3, 11)$$$, $$$(4, 10)$$$, $$$(5, 9)$$$, $$$(6, 8)$$$, $$$(7, 7)$$$, $$$(8, 6)$$$, $$$(9, 5)$$$, $$$(10, 4)$$$, $$$(11, 3)$$$, $$$(12, 2)$$$, $$$(13, 1)$$$, $$$(14, 0)$$$. After the second update $$$c$$$ is equal to $$$11$$$. After the third update $$$c$$$ is equal to $$$01$$$.
2,600
false
false
false
true
true
false
false
false
false
false
3,766
1090F
, # Problem A. How to Learn You Score Time limit: 1 second Memory limit: 512 megabytes This is an interactive problem. Petya participates in such contest that the participants only learn results of judging their submissions after the end of the contest. There are n problems numbered from 1 to n in the contest. For each problem he has made a submission. After the contest he was very eager to learn his points and his place. But the results would only be published at the closing ceremony, which is a few hours away. Petya decided to find out his result before that: to get his points for each problem from the jury, he would ask weird questions. Let Petya’s points be c1, c 2, . . . , c n for problems 1, 2, . . . , n , respectively. Petya doesn’t know these numbers and wants to find them out. Petya chooses three distinct numbers of the problems and asks the jury to tell him the sum of the minimum and the maximum points he received for these three problems. Fortunately, the jury answers such queries. Formally, if he queries about i, j, k, he gets min (ci, c j , c k) + max (ci, c j , c k).Petya still doesn’t know how to find out all his points. Help him find out c1, c 2, . . . , c n, by making no more than 4n queries to the jury. # Interaction Protocol First, your program must read a single integer n — the number of the problems in the contest ( 5 x14 n x14 1000 ). Next, your program can make queries about the sum of minimum and maximum points among problems i, j, k. In order to do that, output a separate line « ? i j k » ( 1 x14 i, j, k x14 n, i̸ = j, j̸ = k, i̸ = k). As the answer to this query, you will get a single integer min (ci, c j , c k) + max (ci, c j , c k) in the input. If your program makes more than 4n queries or asks an invalid query, you get « Wrong Answer ». After finding Petya’s points output « ! c1 c2 . . . c n» — the points for each problem. It is guaranteed that Petya’s points for each problem are integer and satisfy 0 x14 ci x14 10 9 for all 1 x14 i x14 n.Also, all numbers ci are fixed in advance and won’t change during the testing of your program. # Example standard input standard output 52441? 1 2 3 ? 1 3 5 ? 5 4 3 ? 1 4 2 ! 1 0 2 1 3 # Note Print new line character after each line. After each action your program must flush standard output. If you use “ writeln ” in Pascal, “ cout << ... << endl ” in C++, “ System.out.println ” in Java, “ print ”in Python, “ Console.WriteLine ” in C#, standard output is automatically flushed, no additional action is required. If you are using other way to output data, you should flush standard output. Note that even if you flush output, you absolutely must print new line character. # Explanation In this sample, there are 5 problems, and Petya got 1, 0, 2, 1, 3 points for the problems. If he queries about problems 1, 2, 3, his minimum points among them is 0 (for the 2-nd problem), and his maximum is 2 (for the 3-rd problem). Thus, the jury will answer 2 = 0 + 2 to that query. Page 1 of 1
2,600
false
false
false
false
false
true
false
false
false
false
5,267
325C
Piegirl has found a monster and a book about monsters and pies. When she is reading the book, she found out that there are _n_ types of monsters, each with an ID between 1 and _n_. If you feed a pie to a monster, the monster will split into some number of monsters (possibly zero), and at least one colorful diamond. Monsters may be able to split in multiple ways. At the begining Piegirl has exactly one monster. She begins by feeding the monster a pie. She continues feeding pies to monsters until no more monsters are left. Then she collects all the diamonds that were created. You will be given a list of split rules describing the way in which the various monsters can split. Every monster can split in at least one way, and if a monster can split in multiple ways then each time when it splits Piegirl can choose the way it splits. For each monster, determine the smallest and the largest number of diamonds Piegirl can possibly collect, if initially she has a single instance of that monster. Piegirl has an unlimited supply of pies. Input The first line contains two integers: _m_ and _n_ (1u2009≤u2009_m_,u2009_n_u2009≤u2009105), the number of possible splits and the number of different monster types. Each of the following _m_ lines contains a split rule. Each split rule starts with an integer (a monster ID) _m__i_ (1u2009≤u2009_m__i_u2009≤u2009_n_), and a positive integer _l__i_ indicating the number of monsters and diamonds the current monster can split into. This is followed by _l__i_ integers, with positive integers representing a monster ID and -1 representing a diamond. Each monster will have at least one split rule. Each split rule will have at least one diamond. The sum of _l__i_ across all split rules will be at most 105. Output For each monster, in order of their IDs, print a line with two integers: the smallest and the largest number of diamonds that can possibly be collected by starting with that monster. If Piegirl cannot possibly end up in a state without monsters, print -1 for both smallest and the largest value. If she can collect an arbitrarily large number of diamonds, print -2 as the largest number of diamonds. If any number in output exceeds 314000000 (but is finite), print 314000000 instead of that number. Examples Input 6 4 1 3 -1 1 -1 1 2 -1 -1 2 3 -1 3 -1 2 3 -1 -1 -1 3 2 -1 -1 4 2 4 -1 Input 3 2 1 2 1 -1 2 2 -1 -1 2 3 2 1 -1
2,600
false
false
false
false
false
false
false
false
false
true
8,539
1955G
Not long ago, Egor learned about the Euclidean algorithm for finding the greatest common divisor of two numbers. The greatest common divisor of two numbers $$$a$$$ and $$$b$$$ is the largest number that divides both $$$a$$$ and $$$b$$$ without leaving a remainder. With this knowledge, Egor can solve a problem that he once couldn't. Vasily has a grid with $$$n$$$ rows and $$$m$$$ columns, and the integer $$${a_i}_j$$$ is located at the intersection of the $$$i$$$-th row and the $$$j$$$-th column. Egor wants to go from the top left corner (at the intersection of the first row and the first column) to the bottom right corner (at the intersection of the last row and the last column) and find the greatest common divisor of all the numbers along the path. He is only allowed to move down and to the right. Egor has written down several paths and obtained different GCD values. He became interested in finding the maximum possible GCD. Unfortunately, Egor is tired of calculating GCDs, so he asks for your help in finding the maximum GCD of the integers along the path from the top left corner to the bottom right corner of the grid. Input The first line contains an integer $$$t$$$ ($$$1 le t le {10}^{4}$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 100$$$)xa0— the number of rows and columns of the grid. Then, there are $$$n$$$ lines, where the $$$i$$$-th line contains $$$m$$$ integers $$$(1 le a_{i,j} le {10}^{6}$$$)xa0— the integers written in the $$$i$$$-th row and the $$$j$$$-th column of the grid. It is guaranteed that the sum of $$$n cdot m$$$ does not exceed $$$2 cdot {10}^{5}$$$ over all test cases. Output For each test case, output the maximum possible GCD along the path from the top left cell to the bottom right cell in a separate line. Example Input 3 2 3 30 20 30 15 25 40 3 3 12 4 9 3 12 2 8 3 12 2 4 2 4 6 8 1 3 6 9
1,900
true
false
true
true
false
false
true
false
false
false
542