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
222E
Recently a top secret mission to Mars has taken place. As a result, scientists managed to obtain some information about the Martian DNA. Now we know that any Martian DNA contains at most _m_ different nucleotides, numbered from 1 to _m_. Special characteristics of the Martian DNA prevent some nucleotide pairs from following consecutively in this chain. For example, if the nucleotide 1 and nucleotide 2 can not follow consecutively in the Martian DNA, then the chain of nucleotides [1, 2] is not a valid chain of Martian DNA, but the chain of nucleotides [2, 1] can be a valid chain (if there is no corresponding restriction). The number of nucleotide pairs that can't follow in the DNA chain consecutively, is _k_. The needs of gene research required information about the quantity of correct _n_-long chains of the Martian DNA. Your task is to write a program that will calculate this value. Input The first line contains three space-separated integers _n_,u2009_m_,u2009_k_ (1u2009≤u2009_n_u2009≤u20091015, 1u2009≤u2009_m_u2009≤u200952, 0u2009≤u2009_k_u2009≤u2009_m_2). Next _k_ lines contain two characters each, without a space between them, representing a forbidden nucleotide pair. The first character represents the first nucleotide in the forbidden pair, the second character represents the second nucleotide. The nucleotides with assigned numbers from 1 to 26 are represented by English alphabet letters from "a" to "z" (1 is an "a", 2 is a "b", ..., 26 is a "z"). Nucleotides with assigned numbers from 27 to 52 are represented by English alphabet letters from "A" to "Z" (27 is an "A", 28 is a "B", ..., 52 is a "Z"). It is guaranteed that each forbidden pair occurs at most once in the input. It is guaranteed that nucleotide's numbers in all forbidden pairs cannot be more than _m_. Note that order is important in nucleotide pairs. 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. Note In the second test case all possible three-nucleotide DNAs are permitted. Each nucleotide can take one of three values, thus in total there are 27 distinct three nucleotide DNAs. In the third test sample we cannot make any DNA of two nucleotides — the only possible nucleotide "a" cannot occur two times consecutively.
1,900
false
false
false
true
false
false
false
false
false
false
8,948
1988B
You are given a sequence $$$[a_1,ldots,a_n]$$$ where each element $$$a_i$$$ is either $$$0$$$ or $$$1$$$. You can apply several (possibly zero) operations to the sequence. In each operation, you select two integers $$$1le lle rle a$$$ (where $$$a$$$ is the current length of $$$a$$$) and replace $$$[a_l,ldots,a_r]$$$ with a single element $$$x$$$, where $$$x$$$ is the majority of $$$[a_l,ldots,a_r]$$$. Here, the majority of a sequence consisting of $$$0$$$ and $$$1$$$ is defined as follows: suppose there are $$$c_0$$$ zeros and $$$c_1$$$ ones in the sequence, respectively. If $$$c_0ge c_1$$$, the majority is $$$0$$$. If $$$c_0<c_1$$$, the majority is $$$1$$$. For example, suppose $$$a=[1,0,0,0,1,1]$$$. If we select $$$l=1,r=2$$$, the resulting sequence will be $$$[0,0,0,1,1]$$$. If we select $$$l=4,r=6$$$, the resulting sequence will be $$$[1,0,0,1]$$$. Determine if you can make $$$a=[1]$$$ with a finite number of operations. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 4cdot 10^4$$$). Description of the test cases follows. The first line of each testcase contains one integer $$$n$$$ ($$$1le nle 2cdot 10^5$$$). The second line of each testcase contains a string consisting of $$$0$$$ and $$$1$$$, describing the sequence $$$a$$$. It's guaranteed that the sum of $$$n$$$ over all testcases does not exceed $$$2cdot 10^5$$$. Output For each testcase, if it's possible to make $$$a=[1]$$$, print YES. Otherwise, print NO. You can output the answer in any case (upper or lower). For example, the strings yEs, yes, Yes, and YES will be recognized as positive responses. Note In the fourth testcase of the example, initially $$$a=[1,0,0,0,0,0,0,0,1]$$$. A valid sequence of operations is: 1. Select $$$l=2,r=8$$$ and apply the operation. $$$a$$$ becomes $$$[1,0,1]$$$. 2. Select $$$l=1,r=3$$$ and apply the operation. $$$a$$$ becomes $$$[1]$$$.
900
false
true
true
false
false
false
false
false
false
false
346
850B
Arpa has found a list containing _n_ numbers. He calls a list bad if and only if it is not empty and gcd (see notes section for more information) of numbers in the list is 1. Arpa can perform two types of operations: Choose a number and delete it with cost _x_. Choose a number and increase it by 1 with cost _y_. Arpa can apply these operations to as many numbers as he wishes, and he is allowed to apply the second operation arbitrarily many times on the same number. Help Arpa to find the minimum possible cost to make the list good. Input First line contains three integers _n_, _x_ and _y_ (1u2009≤u2009_n_u2009≤u20095·105, 1u2009≤u2009_x_,u2009_y_u2009≤u2009109)xa0— the number of elements in the list and the integers _x_ and _y_. Second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009106)xa0— the elements of the list. Output Print a single integer: the minimum possible cost to make the list good. Examples Input 10 6 2 100 49 71 73 66 96 8 60 41 63 Note In example, number 1 must be deleted (with cost 23) and number 16 must increased by 1 (with cost 17). A gcd (greatest common divisor) of a set of numbers is the maximum integer that divides all integers in the set. Read more about gcd [here](
2,100
false
false
true
false
false
false
false
false
false
false
6,350
246C
General Payne has a battalion of _n_ soldiers. The soldiers' beauty contest is coming up, it will last for _k_ days. Payne decided that his battalion will participate in the pageant. Now he has choose the participants. All soldiers in the battalion have different beauty that is represented by a positive integer. The value _a__i_ represents the beauty of the _i_-th soldier. On each of _k_ days Generals has to send a detachment of soldiers to the pageant. The beauty of the detachment is the sum of the beauties of the soldiers, who are part of this detachment. Payne wants to surprise the jury of the beauty pageant, so each of _k_ days the beauty of the sent detachment should be unique. In other words, all _k_ beauties of the sent detachments must be distinct numbers. Help Payne choose _k_ detachments of different beauties for the pageant. Please note that Payne cannot just forget to send soldiers on one day, that is, the detachment of soldiers he sends to the pageant should never be empty. Input The first line contains two integers _n_, _k_ (1u2009≤u2009_n_u2009≤u200950; 1u2009≤u2009_k_u2009≤u2009 ) — the number of soldiers and the number of days in the pageant, correspondingly. The second line contains space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009107) — the beauties of the battalion soldiers. It is guaranteed that Payne's battalion doesn't have two soldiers with the same beauty. Output Print _k_ lines: in the _i_-th line print the description of the detachment that will participate in the pageant on the _i_-th day. The description consists of integer _c__i_ (1u2009≤u2009_c__i_u2009≤u2009_n_) — the number of soldiers in the detachment on the _i_-th day of the pageant and _c__i_ distinct integers _p_1,u2009_i_,u2009_p_2,u2009_i_,u2009...,u2009_p__c__i_,u2009_i_ — the beauties of the soldiers in the detachment on the _i_-th day of the pageant. The beauties of the soldiers are allowed to print in any order. Separate numbers on the lines by spaces. It is guaranteed that there is the solution that meets the problem conditions. If there are multiple solutions, print any of them.
1,600
false
true
false
false
false
true
true
false
false
false
8,846
538A
A large banner with word CODEFORCES was ordered for the 1000-th onsite round of Codeforcesω that takes place on the Miami beach. Unfortunately, the company that made the banner mixed up two orders and delivered somebody else's banner that contains someone else's word. The word on the banner consists only of upper-case English letters. There is very little time to correct the mistake. All that we can manage to do is to cut out some substring from the banner, i.e. several consecutive letters. After that all the resulting parts of the banner will be glued into a single piece (if the beginning or the end of the original banner was cut out, only one part remains); it is not allowed change the relative order of parts of the banner (i.e. after a substring is cut, several first and last letters are left, it is allowed only to glue the last letters to the right of the first letters). Thus, for example, for example, you can cut a substring out from string 'TEMPLATE' and get string 'TEMPLE' (if you cut out string AT), 'PLATE' (if you cut out TEM), 'T' (if you cut out EMPLATE), etc. Help the organizers of the round determine whether it is possible to cut out of the banner some substring in such a way that the remaining parts formed word CODEFORCES. Input The single line of the input contains the word written on the banner. The word only consists of upper-case English letters. The word is non-empty and its length doesn't exceed 100 characters. It is guaranteed that the word isn't word CODEFORCES. Output Print 'YES', if there exists a way to cut out the substring, and 'NO' otherwise (without the quotes).
1,400
false
false
true
false
false
false
true
false
false
false
7,688
845G
Problem - 845G - Codeforces =============== xa0 . You have to find the minimum length of path between vertex 1 and vertex _n_. Note that graph can contain multiple edges and loops. It is guaranteed that the graph is connected. Input The first line contains two numbers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009100000, _n_u2009-u20091u2009≤u2009_m_u2009≤u2009100000) — the number of vertices and the number of edges, respectively. Then _m_ lines follow, each line containing three integer numbers _x_, _y_ and _w_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_, 0u2009≤u2009_w_u2009≤u2009108). These numbers denote an edge that connects vertices _x_ and _y_ and has weight _w_. Output Print one number — the minimum length of path between vertices 1 and _n_. Examples Input 3 3 1 2 3 1 3 2 3 2 0 Output 2 Input 2 2 1 1 3 1 2 3 Output 0
2,300
true
false
false
false
false
false
false
false
false
true
6,378
1608F
Problem - 1608F - 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 implementation *3200 No tag edit access → Contest materials ") ") $$$ denotes the smallest nonnegative integer that doesn't appear in it. For example, $$$MEX(
3,200
false
false
true
true
false
false
false
false
false
false
2,610
1735D
You like the card board game "Set". Each card contains $$$k$$$ features, each of which is equal to a value from the set $$${0, 1, 2}$$$. The deck contains all possible variants of cards, that is, there are $$$3^k$$$ different cards in total. A feature for three cards is called good if it is the same for these cards or pairwise distinct. Three cards are called a set if all $$$k$$$ features are good for them. For example, the cards $$$(0, 0, 0)$$$, $$$(0, 2, 1)$$$, and $$$(0, 1, 2)$$$ form a set, but the cards $$$(0, 2, 2)$$$, $$$(2, 1, 2)$$$, and $$$(1, 2, 0)$$$ do not, as, for example, the last feature is not good. A group of five cards is called a meta-set, if there is strictly more than one set among them. How many meta-sets there are among given $$$n$$$ distinct cards? Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 10^3$$$, $$$1 le k le 20$$$) — the number of cards on a table and the number of card features. The description of the cards follows in the next $$$n$$$ lines. Each line describing a card contains $$$k$$$ integers $$$c_{i, 1}, c_{i, 2}, ldots, c_{i, k}$$$ ($$$0 le c_{i, j} le 2$$$)xa0— card features. It is guaranteed that all cards are distinct. Output Output one integer — the number of meta-sets. Examples Input 8 4 0 0 0 0 0 0 0 1 0 0 0 2 0 0 1 0 0 0 2 0 0 1 0 0 1 0 0 0 2 2 0 0 Input 7 4 0 0 0 0 0 0 0 1 0 0 0 2 0 0 1 0 0 0 2 0 0 1 0 0 0 2 0 0 Input 9 2 0 0 0 1 0 2 1 0 1 1 1 2 2 0 2 1 2 2 Input 20 4 0 2 0 0 0 2 2 2 0 2 2 1 0 2 0 1 1 2 2 0 1 2 1 0 1 2 2 1 1 2 0 1 1 1 2 2 1 1 0 2 1 1 2 1 1 1 1 1 2 1 2 0 2 1 1 2 2 1 2 1 2 1 1 1 0 1 1 2 0 0 1 0 2 2 0 0 2 0 0 2 Note Let's draw the cards indicating the first four features. The first feature will indicate the number of objects on a card: $$$1$$$, $$$2$$$, $$$3$$$. The second one is the color: red, green, purple. The third is the shape: oval, diamond, squiggle. The fourth is filling: open, striped, solid. You can see the first three tests below. For the first two tests, the meta-sets are highlighted. In the first test, the only meta-set is the five cards $$$(0000, 0001, 0002, 0010, 0020)$$$. The sets in it are the triples $$$(0000, 0001, 0002)$$$ and $$$(0000, 0010, 0020)$$$. Also, a set is the triple $$$(0100, 1000, 2200)$$$ which does not belong to any meta-set. In the second test, the following groups of five cards are meta-sets: $$$(0000, 0001, 0002, 0010, 0020)$$$, $$$(0000, 0001, 0002, 0100, 0200)$$$, $$$(0000, 0010, 0020, 0100, 0200)$$$. In there third test, there are $$$54$$$ meta-sets.
1,700
true
false
false
false
true
false
true
false
false
false
1,877
763A
Each New Year Timofey and his friends cut down a tree of _n_ vertices and bring it home. After that they paint all the _n_ its vertices, so that the _i_-th vertex gets color _c__i_. Now it's time for Timofey birthday, and his mother asked him to remove the tree. Timofey removes the tree in the following way: he takes some vertex in hands, while all the other vertices move down so that the tree becomes rooted at the chosen vertex. After that Timofey brings the tree to a trash can. Timofey doesn't like it when many colors are mixing together. A subtree annoys him if there are vertices of different color in it. Timofey wants to find a vertex which he should take in hands so that there are no subtrees that annoy him. He doesn't consider the whole tree as a subtree since he can't see the color of the root vertex. A subtree of some vertex is a subgraph containing that vertex and all its descendants. Your task is to determine if there is a vertex, taking which in hands Timofey wouldn't be annoyed. Input The first line contains single integer _n_ (2u2009≤u2009_n_u2009≤u2009105)xa0— the number of vertices in the tree. Each of the next _n_u2009-u20091 lines contains two integers _u_ and _v_ (1u2009≤u2009_u_,u2009_v_u2009≤u2009_n_, _u_u2009≠u2009_v_), denoting there is an edge between vertices _u_ and _v_. It is guaranteed that the given graph is a tree. The next line contains _n_ integers _c_1,u2009_c_2,u2009...,u2009_c__n_ (1u2009≤u2009_c__i_u2009≤u2009105), denoting the colors of the vertices. Output Print "NO" in a single line, if Timofey can't take the tree in such a way that it doesn't annoy him. Otherwise print "YES" in the first line. In the second line print the index of the vertex which Timofey should take in hands. If there are multiple answers, print any of them.
1,600
false
false
true
true
false
false
false
false
false
true
6,737
685B
After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes. Once upon a time, he found a huge snowflake that has a form of the tree (connected acyclic graph) consisting of _n_ nodes. The root of tree has index 1. Kay is very interested in the structure of this tree. After doing some research he formed _q_ queries he is interested in. The _i_-th query asks to find a centroid of the subtree of the node _v__i_. Your goal is to answer all queries. Subtree of a node is a part of tree consisting of this node and all it's descendants (direct or not). In other words, subtree of node _v_ is formed by nodes _u_, such that node _v_ is present on the path from _u_ to root. Centroid of a tree (or a subtree) is a node, such that if we erase it from the tree, the maximum size of the connected component will be at least two times smaller than the size of the initial tree (or a subtree). Input The first line of the input contains two integers _n_ and _q_ (2u2009≤u2009_n_u2009≤u2009300u2009000, 1u2009≤u2009_q_u2009≤u2009300u2009000)xa0— the size of the initial tree and the number of queries respectively. The second line contains _n_u2009-u20091 integer _p_2,u2009_p_3,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009≤u2009_n_)xa0— the indices of the parents of the nodes from 2 to _n_. Node 1 is a root of the tree. It's guaranteed that _p__i_ define a correct tree. Each of the following _q_ lines contain a single integer _v__i_ (1u2009≤u2009_v__i_u2009≤u2009_n_)xa0— the index of the node, that define the subtree, for which we want to find a centroid. Output For each query print the index of a centroid of the corresponding subtree. If there are many suitable nodes, print any of them. It's guaranteed, that each subtree has at least one centroid. Note The first query asks for a centroid of the whole treexa0— this is node 3. If we delete node 3 the tree will split in four components, two of size 1 and two of size 2. The subtree of the second node consists of this node only, so the answer is 2. Node 3 is centroid of its own subtree. The centroids of the subtree of the node 5 are nodes 5 and 6xa0— both answers are considered correct.
1,900
false
false
false
true
true
false
false
false
false
false
7,082
1556D
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2) 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 interactive math *1800 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST D. Take a Guess time limit per test2 seconds memory limit per test256 megabytes This is an interactive task William has a certain sequence of integers $$$a_1, a_2, dots, a_n$$$ in his mind, but due to security concerns, he does not want to reveal it to you completely. William is ready to respond to no more than $$$2 cdot n$$$ of the following questions: What is the result of a bitwise AND of two items with indices $$$i$$$ and $$$j$$$ ($$$i eq j$$$) What is the result of a bitwise OR of two items with indices $$$i$$$ and $$$j$$$ ($$$i eq j$$$) You can ask William these questions and you need to find the $$$k$$$-th smallest number of the sequence. Formally the $$$k$$$-th smallest number is equal to the number at the $$$k$$$-th place in a 1-indexed array sorted in non-decreasing order. For example in array $$$[5, 3, 3, 10, 1]$$$ $$$4$$$th smallest number is equal to $$$5$$$, and $$$2$$$nd and $$$3$$$rd are $$$3$$$. Input It is guaranteed that for each element in a sequence the condition $$$0 le a_i le 10^9$$$ is satisfied. Interaction In the first line you will be given two integers $$$n$$$ and $$$k$$$ $$$(3 le n le 10^4, 1 le k le n)$$$, which are the number of items in the sequence $$$a$$$ and the number $$$k$$$. After that, you can ask no more than $$$2 cdot n$$$ questions (not including the "finish" operation). Each line of your output may be of one of the following types: "or i j" $$$(1 le i, j le n, i eq j)$$$, where $$$i$$$ and $$$j$$$ are indices of items for which you want to calculate the bitwise OR. "and i j" $$$(1 le i, j le n, i eq j)$$$, where $$$i$$$ and $$$j$$$ are indices of items for which you want to calculate the bitwise AND. "finish res", where $$$res$$$ is the $$$k$$$th smallest number in the sequence. After outputting this line the program execution must conclude. In response to the first two types of queries, you will get an integer $$$x$$$, the result of the operation for the numbers you have selected. After outputting a line do not forget to output a new line character and flush the output buffer. Otherwise you will get the "Idleness limit exceeded". To flush the buffer use: fflush(stdout) in C++ System.out.flush() in Java stdout.flush() in Python flush(output) in Pascal for other languages refer to documentation If you perform an incorrect query the response will be $$$-1$$$. After receiving response $$$-1$$$ you must immediately halt your program in order to receive an "Incorrect answer" verdict. Hacking To perform a hack you will need to use the following format: The first line must contain two integers $$$n$$$ and $$$k$$$ $$$(3 le n le 10^4, 1 le k le n)$$$, which are the number of items in the sequence $$$a$$$ and the number $$$k$$$. The second line must contain $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ $$$(0 le a_i le 10^9)$$$, the sequence $$$a$$$. Example input 7 6 2 7 output and 2 5 or 5 6 finish 5 Note In the example, the hidden sequence is $$$[1, 6, 4, 2, 3, 5, 4]$$$. Below is the interaction in the example. Query (contestant's program) Response (interactor) Notes and 2 5 2 $$$a_2=6$$$, $$$a_5=3$$$. Interactor returns bitwise AND of the given numbers. or 5 6 7 $$$a_5=3$$$, $$$a_6=5$$$. Interactor returns bitwise OR of the given numbers. finish 5 $$$5$$$ is the correct answer. Note that you must find the value and not the index of the kth smallest number. Codeforces (c)
1,800
true
false
false
false
false
true
false
false
false
false
2,852
190E
Berland has managed to repel the flatlanders' attack and is now starting the counter attack. Flatland has _n_ cities, numbered from 1 to _n_, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them. The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once! Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u20095·105,u20090u2009≤u2009_m_u2009≤u2009106) — the number of cities and the number of roads marked on the flatland map, correspondingly. Next _m_ lines contain descriptions of the cities on the map. The _i_-th line contains two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_) — the numbers of cities that are connected by the _i_-th road on the flatland map. It is guaranteed that each pair of cities occurs in the input no more than once. Output On the first line print number _k_ — the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads. On each of the following _k_ lines first print _t__i_ (1u2009≤u2009_t__i_u2009≤u2009_n_) — the number of vertexes in the _i_-th group. Then print space-separated numbers of cities in the _i_-th group. The order of printing groups and the order of printing numbers in the groups does not matter. The total sum _t__i_ for all _k_ groups must equal _n_. Note In the first sample there are roads only between pairs of cities 1-4 and 2-3. In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3.
2,100
false
false
false
false
true
false
false
false
true
true
9,078
245H
Problem - 245H - 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 hashing strings *1800 No tag edit access → Contest materials ") . The answer to the query is the number of substrings of string _s_
1,800
false
false
false
true
false
false
false
false
false
false
8,849
870A
Problem - 870A - Codeforces =============== xa0 ") ") representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer? Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20099) — the lengths of the first and the second lists, respectively. The second line contains _n_ distinct digits _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20099) — the elements of the first list. The third line contains _m_ distinct digits _b_1,u2009_b_2,u2009...,u2009_b__m_ (1u2009≤u2009_b__i_u2009≤u20099) — the elements of the second list. Output Print the smallest pretty integer. Examples Input 2 3 4 2 5 7 6 Output 25 Input 8 8 1 2 3 4 5 6 7 8 8 7 6 5 4 3 2 1 Output 1 Note In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list. In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer.
900
false
false
true
false
false
false
true
false
false
false
6,261
174A
A group of _n_ merry programmers celebrate Robert Floyd's birthday. Polucarpus has got an honourable task of pouring Ber-Cola to everybody. Pouring the same amount of Ber-Cola to everybody is really important. In other words, the drink's volume in each of the _n_ mugs must be the same. Polycarpus has already began the process and he partially emptied the Ber-Cola bottle. Now the first mug has _a_1 milliliters of the drink, the second one has _a_2 milliliters and so on. The bottle has _b_ milliliters left and Polycarpus plans to pour them into the mugs so that the main equation was fulfilled. Write a program that would determine what volume of the drink Polycarpus needs to add into each mug to ensure that the following two conditions were fulfilled simultaneously: there were _b_ milliliters poured in total. That is, the bottle need to be emptied; after the process is over, the volumes of the drink in the mugs should be equal. Input The first line contains a pair of integers _n_, _b_ (2u2009≤u2009_n_u2009≤u2009100,u20091u2009≤u2009_b_u2009≤u2009100), where _n_ is the total number of friends in the group and _b_ is the current volume of drink in the bottle. The second line contains a sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009100), where _a__i_ is the current volume of drink in the _i_-th mug. Output Print a single number "-1" (without the quotes), if there is no solution. Otherwise, print _n_ float numbers _c_1,u2009_c_2,u2009...,u2009_c__n_, where _c__i_ is the volume of the drink to add in the _i_-th mug. Print the numbers with no less than 6 digits after the decimal point, print each _c__i_ on a single line. Polycarpus proved that if a solution exists then it is unique. Russian locale is installed by default on the testing computer. Make sure that your solution use the point to separate the integer part of a real number from the decimal, not a comma. Examples Output 12.000000 11.000000 10.000000 9.000000 8.000000
1,100
true
false
false
false
false
false
false
false
false
false
9,168
463D
Problem - 463D - 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 dp graphs implementation *1900 No tag edit access → Contest materials ") . Each of the next _k_ lines contains integers 1,u20092,u2009...,u2009_n_ in some order — description of the current permutation. Output Print the length of the longest common subsequence. Examples Input 4 3 1 4 2 3 4 1 2 3 1 2 4 3 Output 3 Note The answer for the first test sample is subsequence
1,900
false
false
true
true
false
false
false
false
false
true
7,986
358B
Seryozha has a very changeable character. This time he refused to leave the room to Dima and his girlfriend (her hame is Inna, by the way). However, the two lovebirds can always find a way to communicate. Today they are writing text messages to each other. Dima and Inna are using a secret code in their text messages. When Dima wants to send Inna some sentence, he writes out all words, inserting a heart before each word and after the last word. A heart is a sequence of two characters: the "less" characters (<) and the digit three (3). After applying the code, a test message looks like that: <3_word_1<3_word_2<3 ... _word__n_<3. Encoding doesn't end here. Then Dima inserts a random number of small English characters, digits, signs "more" and "less" into any places of the message. Inna knows Dima perfectly well, so she knows what phrase Dima is going to send her beforehand. Inna has just got a text message. Help her find out if Dima encoded the message correctly. In other words, find out if a text message could have been received by encoding in the manner that is described above. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of words in Dima's message. Next _n_ lines contain non-empty words, one word per line. The words only consist of small English letters. The total length of all words doesn't exceed 105. The last line contains non-empty text message that Inna has got. The number of characters in the text message doesn't exceed 105. A text message can contain only small English letters, digits and signs more and less. Output In a single line, print "yes" (without the quotes), if Dima decoded the text message correctly, and "no" (without the quotes) otherwise. Examples Input 3 i love you <3i<3love<23you<3 Input 7 i am not main in the family <3i<>3am<3the<3<main<3in<3the<3><3family<3 Note Please note that Dima got a good old kick in the pants for the second sample from the statement.
1,500
false
false
false
false
false
false
true
false
false
false
8,403
373A
Cucumber boy is fan of Kyubeat, a famous music game. Kyubeat has 16 panels for playing arranged in 4u2009×u20094 table. When a panel lights up, he has to press that panel. Each panel has a timing to press (the preffered time when a player should press it), and Cucumber boy is able to press at most _k_ panels in a time with his one hand. Cucumber boy is trying to press all panels in perfect timing, that is he wants to press each panel exactly in its preffered time. If he cannot press the panels with his two hands in perfect timing, his challenge to press all the panels in perfect timing will fail. You are given one scene of Kyubeat's panel from the music Cucumber boy is trying. Tell him is he able to press all the panels in perfect timing. Input The first line contains a single integer _k_ (1u2009≤u2009_k_u2009≤u20095) — the number of panels Cucumber boy can press with his one hand. Next 4 lines contain 4 characters each (digits from 1 to 9, or period) — table of panels. If a digit _i_ was written on the panel, it means the boy has to press that panel in time _i_. If period was written on the panel, he doesn't have to press that panel. Output Output "YES" (without quotes), if he is able to press all the panels in perfect timing. If not, output "NO" (without quotes). Note In the third sample boy cannot press all panels in perfect timing. He can press all the panels in timing in time 1, but he cannot press the panels in time 2 in timing with his two hands.
900
false
false
true
false
false
false
false
false
false
false
8,338
522D
Problem - 522D - Codeforces =============== xa0 ]( "VK Cup 2015 — Квалификация 1") . For each query you need to print the minimum distance between such pair of elements _a__x_ and _a__y_ (_x_u2009≠u2009_y_), that: both indexes of the elements lie within range
2,000
false
false
false
false
true
false
false
false
false
false
7,747
414E
After a lot of trying, Mashmokh designed a problem and it's your job to solve it. You have a tree _T_ with _n_ vertices. Each vertex has a unique index from 1 to _n_. The root of _T_ has index 1. For each vertex of this tree _v_, you are given a list of its children in a specific order. You must perform three types of query on this tree: 1. find distance (the number of edges in the shortest path) between _u_ and _v_; 2. given _v_ and _h_, disconnect _v_ from its father and connect it to its _h_-th ancestor; more formally, let's denote the path from _v_ to the root by _x_1,u2009_x_2,u2009...,u2009_x__l_xa0(_h_u2009<u2009_l_), so that _x_1u2009=u2009_v_ and _x__l_ is root; disconnect _v_ from its father (_x_2) and connect it to _x__h_u2009+u20091; vertex _v_ must be added to the end of the child-list of vertex _x__h_u2009+u20091; 3. in the vertex sequence produced by calling function dfs(root) find the latest vertex that has distance _k_ from the root. The pseudo-code of function dfs(v): // ls[v]: list of children of vertex v // its i-th element is ls[v][i] // its size is size(ls[v]) sequence result = empty sequence; void dfs(vertex now) { add now to end of result; for(int i = 1; i <= size(ls[v]); i = i + 1) //loop from i = 1 to i = size(ls[v]) dfs(ls[v][i]); } Input The first line of input contains two space-separated integers _n_,u2009_m_xa0(2u2009≤u2009_n_u2009≤u2009105;xa01u2009≤u2009_m_u2009≤u2009105), the number of vertices of _T_ and number of queries to perform. The _i_-th of the following _n_ lines contains an integer _l__i_xa0(0u2009≤u2009_l__i_u2009≤u2009_n_), number of _i_-th vertex's children. Then _l__i_ space-separated integers follow, the _j_-th of them is the index of _j_-th child of _i_-th vertex. Note that the order of these vertices is important. Each of the following _m_ lines has one of the following format: "1 _v_ _u_", "2 _v_ _h_", or "3 _k_". The first number in the line is the type of query to perform according to the problem statement. The next numbers are description of the query. It's guaranteed that all the queries are correct. For example, in the second-type query _h_ is at least 2 and at most distance of _v_ from root. Also in the third-type query there is at least one vertex with distance _k_ from the root at the time the query is given. Output For each query of the first or third type output one line containing the result of the query. Examples Input 4 9 1 2 1 3 1 4 0 1 1 4 2 4 2 1 3 4 3 1 3 2 2 3 2 1 1 2 3 1 3 2
3,200
false
false
false
false
true
false
false
false
false
false
8,165
939E
Problem - 939E - 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 greedy ternary search two pointers *1800 No tag edit access → Contest materials ") . There are two kind of queries: 1. Add a positive integer to _S_, the newly added integer is not less than any number in it. 2. Find a subset _s_ of the set _S_ such that the value is maximum possible. Here _max_(_s_) means maximum value of elements in _s_, xa0— the average value of numbers in _s_. Output this maximum possible value of . Input The first line contains a single integer _Q_ (1u2009≤u2009_Q_u2009≤u20095·105)xa0— the number of queries. Each of the next _Q_ lines contains a description of query. For queries of type 1 two integers 1 and _x_ are given, where _x_ (1u2009≤u2009_x_u2009≤u2009109) is a number that you should add to _S_. It's guaranteed that _x_ is not less than any number in _S_. For queries of type 2, a single integer 2 is given. It's guaranteed that the first query has type 1, i.xa0e. _S_ is not empty when a query of type 2 comes. Output Output the answer for each query of the second type in the order these queries are given in input. Each number should be printed in separate line. Your answer is considered correct, if each of your answers has absolute or relative error not greater than 10u2009-u20096. Formally, let your answer be _a_, and the jury's answer be _b_. Your answer is considered correct if . Examples Input 6 1 3 2 1 4 2 1 8 2 Output 0.0000000000 0.5000000000 3.0000000000 Input 4 1 1 1 4 1 5 2 Output 2.0000000000
1,800
false
true
false
false
false
false
false
true
false
false
5,940
2013B
Eralim, being the mafia boss, manages a group of $$$n$$$ fighters. Fighter $$$i$$$ has a rating of $$$a_i$$$. Eralim arranges a tournament of $$$n - 1$$$ battles, in each of which two not yet eliminated fighters $$$i$$$ and $$$j$$$ ($$$1 le i < j le n$$$) are chosen, and as a result of the battle, fighter $$$i$$$ is eliminated from the tournament, and the rating of fighter $$$j$$$ is reduced by the rating of fighter $$$i$$$. That is, $$$a_j$$$ is decreased by $$$a_i$$$. Note that fighter $$$j$$$'s rating can become negative. The fighters indexes do not change. Eralim wants to know what maximum rating the last remaining fighter can preserve if he chooses the battles optimally. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0— the number of fighters. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the ratings of the fighters. The sum of $$$n$$$ over all testcases does not exceed $$$2 cdot 10^5$$$. Output For each testcase, output a single integerxa0— the maximum rating that the last remaining fighter can preserve. Example Input 5 2 2 1 3 2 2 8 4 1 2 4 3 5 1 2 3 4 5 5 3 2 4 5 4 Note In the first example, you can arrange a fight between fighters with indices $$$1$$$ and $$$2$$$, where the fighter with index $$$2$$$ will win. The rating of the last fighter, that is, the fighter with index $$$2$$$, will be $$$1 - 2 = -1$$$. In the second example, you can first conduct a fight between fighters with indices $$$1$$$ and $$$2$$$, where the fighter with index $$$2$$$ will win, and then conduct a fight between fighters with indices $$$2$$$ and $$$3$$$, where the fighter with index $$$3$$$ will win. The rating of the fighter with index $$$2$$$ after the first fight will be $$$2 - 2 = 0$$$. The rating of the fighter with index $$$3$$$ after the second fight will be $$$8 - 0 = 8$$$.
900
true
true
false
false
false
true
false
false
false
false
177
235D
In computer science, there is a method called "Divide And Conquer By Node" to solve some hard problems about paths on a tree. Let's desribe how this method works by function: _solve_(_t_) (_t_ is a tree): 1. Chose a node _x_ (it's common to chose weight-center) in tree _t_. Let's call this step "Line A". 2. Deal with all paths that pass _x_. 3. Then delete _x_ from tree _t_. 4. After that _t_ becomes some subtrees. 5. Apply _solve_ on each subtree. This ends when _t_ has only one node because after deleting it, there's nothing. Now, WJMZBMR has mistakenly believed that it's ok to chose any node in "Line A". So he'll chose a node at random. To make the situation worse, he thinks a "tree" should have the same number of edges and nodes! So this procedure becomes like that. Let's define the variable _totalCost_. Initially the value of _totalCost_ equal to 0. So, _solve_(_t_) (now _t_ is a graph): 1. _totalCost_u2009=u2009_totalCost_u2009+u2009(_size_xa0_of_xa0_t_). The operation "=" means assignment. (_Size_xa0_of_xa0_t_) means the number of nodes in _t_. 2. Choose a node _x_ in graph _t_ at random (uniformly among all nodes of _t_). 3. Then delete _x_ from graph _t_. 4. After that _t_ becomes some connected components. 5. Apply _solve_ on each component. He'll apply _solve_ on a connected graph with _n_ nodes and _n_ edges. He thinks it will work quickly, but it's very slow. So he wants to know the expectation of _totalCost_ of this procedure. Can you help him? Input The first line contains an integer _n_ (3u2009≤u2009_n_u2009≤u20093000) — the number of nodes and edges in the graph. Each of the next _n_ lines contains two space-separated integers _a__i_,u2009_b__i_ (0u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_u2009-u20091) indicating an edge between nodes _a__i_ and _b__i_. Consider that the graph nodes are numbered from 0 to (_n_u2009-u20091). It's guaranteed that there are no self-loops, no multiple edges in that graph. It's guaranteed that the graph is connected. Output Print a single real number — the expectation of _totalCost_. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Note Consider the second example. No matter what we choose first, the _totalCost_ will always be 3u2009+u20092u2009+u20091u2009=u20096.
3,000
false
false
false
false
false
false
false
false
false
true
8,893
1148G
Consider an undirected graph $$$G$$$ with $$$n$$$ vertices. There is a value $$$a_i$$$ in each vertex. Two vertices $$$i$$$ and $$$j$$$ are connected with an edge if and only if $$$gcd(a_i, a_j) > 1$$$, where $$$gcd(x, y)$$$ denotes the where all vertices are fair or all vertices are not fair. One can show that such a set always exists. Input The first line contains integers $$$n$$$ and $$$k$$$ ($$$6 leq 2 cdot k leq n leq 10^5$$$)xa0— the number of vertices and parameter $$$k$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$2 le a_i le 10^7$$$)xa0— the values in the vertices. Output Print exactly $$$k$$$ distinct integersxa0— the indices of the vertices in the chosen set in any order. Examples Input 8 4 11 15 10 6 21 15 10 6 Input 10 5 3003 17017 3230 49742 546 41990 17765 570 21945 36465 Note In the first test case, set $$${2, 4, 5}$$$ is an example of set where no vertices are fair. The vertex $$$2$$$ does not share an edge with vertex $$$4$$$ since $$$gcd(15, 8) = 1$$$. The vertex $$$4$$$ does not share an edge with vertex $$$2$$$. The vertex $$$5$$$ does not share an edge with vertex $$$2$$$. In the second test case, set $$${8, 5, 6, 4}$$$ is an example of a set where all vertices are fair.
3,300
true
false
false
false
false
true
false
false
false
true
4,971
1427D
You are given a deck of $$$n$$$ cards numbered from $$$1$$$ to $$$n$$$ (not necessarily in this order in the deck). You have to sort the deck by repeating the following operation. Choose $$$2 le k le n$$$ and split the deck in $$$k$$$ nonempty contiguous parts $$$D_1, D_2,dots, D_k$$$ ($$$D_1$$$ contains the first $$$D_1$$$ cards of the deck, $$$D_2$$$ contains the following $$$D_2$$$ cards and so on). Then reverse the order of the parts, transforming the deck into $$$D_k, D_{k-1}, dots, D_2, D_1$$$ (so, the first $$$D_k$$$ cards of the new deck are $$$D_k$$$, the following $$$D_{k-1}$$$ cards are $$$D_{k-1}$$$ and so on). The internal order of each packet of cards $$$D_i$$$ is unchanged by the operation. You have to obtain a sorted deck (i.e., a deck where the first card is $$$1$$$, the second is $$$2$$$ and so on) performing at most $$$n$$$ operations. It can be proven that it is always possible to sort the deck performing at most $$$n$$$ operations. Examples of operation: The following are three examples of valid operations (on three decks with different sizes). If the deck is [3 6 2 1 4 5 7] (so $$$3$$$ is the first card and $$$7$$$ is the last card), we may apply the operation with $$$k=4$$$ and $$$D_1=$$$[3 6], $$$D_2=$$$[2 1 4], $$$D_3=$$$[5], $$$D_4=$$$[7]. Doing so, the deck becomes [7 5 2 1 4 3 6]. If the deck is [3 1 2], we may apply the operation with $$$k=3$$$ and $$$D_1=$$$[3], $$$D_2=$$$[1], $$$D_3=$$$[2]. Doing so, the deck becomes [2 1 3]. If the deck is [5 1 2 4 3 6], we may apply the operation with $$$k=2$$$ and $$$D_1=$$$[5 1], $$$D_2=$$$[2 4 3 6]. Doing so, the deck becomes [2 4 3 6 5 1]. Input The first line of the input contains one integer $$$n$$$ ($$$1le nle 52$$$) xa0— the number of cards in the deck. The second line contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ xa0— the cards in the deck. The first card is $$$c_1$$$, the second is $$$c_2$$$ and so on. It is guaranteed that for all $$$i=1,dots,n$$$ there is exactly one $$$jin{1,dots,n}$$$ such that $$$c_j = i$$$. Output On the first line, print the number $$$q$$$ of operations you perform (it must hold $$$0le qle n$$$). Then, print $$$q$$$ lines, each describing one operation. To describe an operation, print on a single line the number $$$k$$$ of parts you are going to split the deck in, followed by the size of the $$$k$$$ parts: $$$D_1, D_2, dots , D_k$$$. It must hold $$$2le kle n$$$, and $$$D_ige 1$$$ for all $$$i=1,dots,k$$$, and $$$D_1+D_2+cdots + D_k = n$$$. It can be proven that it is always possible to sort the deck performing at most $$$n$$$ operations. If there are several ways to sort the deck you can output any of them. Note Explanation of the first testcase: Initially the deck is [3 1 2 4]. The first operation splits the deck as [(3) (1 2) (4)] and then transforms it into [4 1 2 3]. The second operation splits the deck as [(4) (1 2 3)] and then transforms it into [1 2 3 4]. Explanation of the second testcase: Initially the deck is [6 5 4 3 2 1]. The first (and only) operation splits the deck as [(6) (5) (4) (3) (2) (1)] and then transforms it into [1 2 3 4 5 6].
2,000
false
false
true
false
false
true
false
false
false
false
3,530
747F
Problem - 747F - 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 combinatorics dp math *2500 No tag edit access → Contest materials — the number of the required integer and the maximum number of times some integer or letter can appear in interesting integer. It can be shown that the answer always exists for such constraints. Output Print in the hexadecimal notation the only integer that is the _k_-th smallest interesting integer for Igor. Examples Input 17 1 Output 12 Input 1000000 2 Output fca2c Note The first 20 interesting integers if _t_u2009=u20091: 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, 10, 12, 13, 14, 15. So the answer for the first example equals 12.
2,500
true
false
false
true
false
false
true
false
false
false
6,805
1418E
You are playing a computer game. In this game, you have to fight $$$n$$$ monsters. To defend from monsters, you need a shield. Each shield has two parameters: its current durability $$$a$$$ and its defence rating $$$b$$$. Each monster has only one parameter: its strength $$$d$$$. When you fight a monster with strength $$$d$$$ while having a shield with current durability $$$a$$$ and defence $$$b$$$, there are three possible outcomes: if $$$a = 0$$$, then you receive $$$d$$$ damage; if $$$a > 0$$$ and $$$d ge b$$$, you receive no damage, but the current durability of the shield decreases by $$$1$$$; if $$$a > 0$$$ and $$$d < b$$$, nothing happens. The $$$i$$$-th monster has strength $$$d_i$$$, and you will fight each of the monsters exactly once, in some random order (all $$$n!$$$ orders are equiprobable). You have to consider $$$m$$$ different shields, the $$$i$$$-th shield has initial durability $$$a_i$$$ and defence rating $$$b_i$$$. For each shield, calculate the expected amount of damage you will receive if you take this shield and fight the given $$$n$$$ monsters in random order. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2 cdot 10^5$$$) — the number of monsters and the number of shields, respectively. The second line contains $$$n$$$ integers $$$d_1$$$, $$$d_2$$$, ..., $$$d_n$$$ ($$$1 le d_i le 10^9$$$), where $$$d_i$$$ is the strength of the $$$i$$$-th monster. Then $$$m$$$ lines follow, the $$$i$$$-th of them contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i le n$$$; $$$1 le b_i le 10^9$$$) — the description of the $$$i$$$-th shield. Output Print $$$m$$$ integers, where the $$$i$$$-th integer represents the expected damage you receive with the $$$i$$$-th shield as follows: it can be proven that, for each shield, the expected damage is an irreducible fraction $$$dfrac{x}{y}$$$, where $$$y$$$ is coprime with $$$998244353$$$. You have to print the value of $$$x cdot y^{-1} bmod 998244353$$$, where $$$y^{-1}$$$ is the inverse element for $$$y$$$ ($$$y cdot y^{-1} bmod 998244353 = 1$$$). Examples Input 3 3 4 2 6 3 1 1 2 2 3
2,400
false
false
false
false
false
false
false
true
false
false
3,590
1428F
Zookeeper is buying a carton of fruit to feed his pet wabbit. The fruits are a sequence of apples and oranges, which is represented by a binary string $$$s_1s_2ldots s_n$$$ of length $$$n$$$. $$$1$$$ represents an apple and $$$0$$$ represents an orange. Since wabbit is allergic to eating oranges, Zookeeper would like to find the longest contiguous sequence of apples. Let $$$f(l,r)$$$ be the longest contiguous sequence of apples in the substring $$$s_{l}s_{l+1}ldots s_{r}$$$. Help Zookeeper find $$$sum_{l=1}^{n} sum_{r=l}^{n} f(l,r)$$$, or the sum of $$$f$$$ across all substrings. Input The first line contains a single integer $$$n$$$ $$$(1 leq n leq 5 cdot 10^5)$$$. The next line contains a binary string $$$s$$$ of length $$$n$$$ $$$(s_i in {0,1})$$$ Output Print a single integer: $$$sum_{l=1}^{n} sum_{r=l}^{n} f(l,r)$$$. Note In the first test, there are ten substrings. The list of them (we let $$$[l,r]$$$ be the substring $$$s_l s_{l+1} ldots s_r$$$): $$$[1,1]$$$: 0 $$$[1,2]$$$: 01 $$$[1,3]$$$: 011 $$$[1,4]$$$: 0110 $$$[2,2]$$$: 1 $$$[2,3]$$$: 11 $$$[2,4]$$$: 110 $$$[3,3]$$$: 1 $$$[3,4]$$$: 10 $$$[4,4]$$$: 0 The lengths of the longest contiguous sequence of ones in each of these ten substrings are $$$0,1,2,2,1,2,2,1,1,0$$$ respectively. Hence, the answer is $$$0+1+2+2+1+2+2+1+1+0 = 12$$$.
2,400
false
false
false
true
true
false
false
true
false
false
3,520
819E
Problem - 819E - 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 graphs *2800 No tag edit access → Contest materials . Output If there is no answer, print -1. Otherwise, in the first line print _k_ (1u2009≤u2009_k_u2009≤u2009_n_2)xa0— the number of cycles in your solution. In each of the next _k_ lines print description of one cycle in the following format: first print integer _m_ (3u2009≤u2009_m_u2009≤u20094)xa0— the length of the cycle, then print _m_ integers _v_1,u2009_v_2,u2009...,u2009_v__m_ (1u2009≤u2009_v__i_u2009≤u2009_n_)xa0— the vertices in the cycle in the traverse order. Each edge should be in exactly two cycles. Examples Input 3 Output 2 3 1 2 3 3 1 2 3 Input 5 Output 6 3 5 4 2 3 3 1 5 4 4 5 2 3 4 4 3 2 1 3 4 2 1 3 3 1 5
2,800
false
false
false
false
false
true
false
false
false
true
6,476
765D
Problem - 765D - 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 dsu math *1700 No tag edit access → Contest materials ") )u2009=u2009_x_ for all , and _h_(_g_(_x_))u2009=u2009_f_(_x_) for all , or determine that finding these is impossible. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009105). The second line contains _n_ space-separated integersxa0— values _f_(1),u2009...,u2009_f_(_n_) (1u2009≤u2009_f_(_i_)u2009≤u2009_n_). Output If there is no answer, print one integer -1. Otherwise, on the first line print the number _m_ (1u2009≤u2009_m_u2009≤u2009106). On the second line print _n_ numbers _g_(1),u2009...,u2009_g_(_n_). On the third line print _m_ numbers _h_(1),u2009...,u2009_h_(_m_). If there are several correct answers, you may output any of them. It is guaranteed that if a valid answer exists, then there is an answer satisfying the above restrictions. Examples Input 3 1 2 3 Output 3 1 2 3 1 2 3 Input 3 2 2 2 Output 1 1 1 1 2 Input 2 2 1 Output -1
1,700
true
false
false
false
false
true
false
false
false
false
6,727
322A
Problem - 322A - 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 *1000 No tag edit access → Contest materials ; or the girl in the dancing pair must dance for the first time. Help Fox Ciel to make a schedule that they can dance as many songs as possible. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100) — the number of boys and girls in the dancing room. Output In the first line print _k_ — the number of songs during which they can dance. Then in the following _k_ lines, print the indexes of boys and girls dancing during songs chronologically. You can assume that the boys are indexed from 1 to _n_, and the girls are indexed from 1 to _m_. Examples Input 2 1 Output 2 1 1 2 1 Input 2 2 Output 3 1 1 1 2 2 2 Note In test case 1, there are 2 boys and 1 girl. We can have 2 dances: the 1st boy and 1st girl (during the first song), the 2nd boy and 1st girl (during the second song). And in test case 2, we have 2 boys with 2 girls, the answer is 3.
1,000
false
true
false
false
false
false
false
false
false
false
8,546
641E
Little Artem has invented a time machine! He could go anywhere in time, but all his thoughts of course are with computer science. He wants to apply this time machine to a well-known data structure: multiset. Artem wants to create a basic multiset of integers. He wants these structure to support operations of three types: 1. Add integer to the multiset. Note that the difference between set and multiset is that multiset may store several instances of one integer. 2. Remove integer from the multiset. Only one instance of this integer is removed. Artem doesn't want to handle any exceptions, so he assumes that every time remove operation is called, that integer is presented in the multiset. 3. Count the number of instances of the given integer that are stored in the multiset. But what about time machine? Artem doesn't simply apply operations to the multiset one by one, he now travels to different moments of time and apply his operation there. Consider the following example. First Artem adds integer 5 to the multiset at the 1-st moment of time. Then Artem adds integer 3 to the multiset at the moment 5. Then Artem asks how many 5 are there in the multiset at moment 6. The answer is 1. Then Artem returns back in time and asks how many integers 3 are there in the set at moment 4. Since 3 was added only at moment 5, the number of integers 3 at moment 4 equals to 0. Then Artem goes back in time again and removes 5 from the multiset at moment 3. Finally Artyom asks at moment 7 how many integers 5 are there in the set. The result is 0, since we have removed 5 at the moment 3. Note that Artem dislikes exceptions so much that he assures that after each change he makes all delete operations are applied only to element that is present in the multiset. The answer to the query of the third type is computed at the moment Artem makes the corresponding query and are not affected in any way by future changes he makes. Help Artem implement time travellers multiset. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the number of Artem's queries. Then follow _n_ lines with queries descriptions. Each of them contains three integers _a__i_, _t__i_ and _x__i_ (1u2009≤u2009_a__i_u2009≤u20093, 1u2009≤u2009_t__i_,u2009_x__i_u2009≤u2009109)xa0— type of the query, moment of time Artem travels to in order to execute this query and the value of the query itself, respectively. It's guaranteed that all moments of time are distinct and that after each operation is applied all operations of the first and second types are consistent. Output For each ask operation output the number of instances of integer being queried at the given moment of time. Examples Input 6 1 1 5 3 5 5 1 2 5 3 6 5 2 3 5 3 7 5
2,000
false
false
false
false
true
false
false
false
false
false
7,247
1136A
After lessons Nastya decided to read a book. The book contains $$$n$$$ chapters, going one after another, so that one page of the book belongs to exactly one chapter and each chapter contains at least one page. Yesterday evening Nastya did not manage to finish reading the book, so she marked the page with number $$$k$$$ as the first page which was not read (i.e. she read all pages from the $$$1$$$-st to the $$$(k-1)$$$-th). The next day Nastya's friend Igor came and asked her, how many chapters remain to be read by Nastya? Nastya is too busy now, so she asks you to compute the number of chapters she has not completely read yet (i.e. the number of chapters she has not started to read or has finished reading somewhere in the middle). Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 100$$$)xa0— the number of chapters in the book. There are $$$n$$$ lines then. The $$$i$$$-th of these lines contains two integers $$$l_i$$$, $$$r_i$$$ separated by space ($$$l_1 = 1$$$, $$$l_i leq r_i$$$)xa0— numbers of the first and the last pages of the $$$i$$$-th chapter. It's guaranteed that $$$l_{i+1} = r_i + 1$$$ for all $$$1 leq i leq n-1$$$, and also that every chapter contains at most $$$100$$$ pages. The $$$(n+2)$$$-th line contains a single integer $$$k$$$ ($$$1 leq k leq r_n$$$)xa0— the index of the marked page. Output Print a single integerxa0— the number of chapters which has not been completely read so far. Note In the first example the book contains $$$11$$$ pages and $$$3$$$ chaptersxa0— $$$[1;3]$$$, $$$[4;7]$$$ and $$$[8;11]$$$. Nastya marked the $$$2$$$-nd page, so she finished in the middle of the $$$1$$$-st chapter. So, all chapters has not been read so far, so the answer is $$$3$$$. The book in the second example contains $$$12$$$ pages and $$$3$$$ chapters too, but Nastya finished reading in the middle of the $$$2$$$-nd chapter, so that the answer is $$$2$$$.
800
false
false
true
false
false
false
false
false
false
false
5,047
1409F
You are given two strings $$$s$$$ and $$$t$$$ consisting of lowercase Latin letters. The length of $$$t$$$ is $$$2$$$ (i.e. this string consists only of two characters). In one move, you can choose any character of $$$s$$$ and replace it with any lowercase Latin letter. More formally, you choose some $$$i$$$ and replace $$$s_i$$$ (the character at the position $$$i$$$) with some character from 'a' to 'z'. You want to do no more than $$$k$$$ replacements in such a way that maximizes the number of occurrences of $$$t$$$ in $$$s$$$ as a subsequence. Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 200$$$; $$$0 le k le n$$$) — the length of $$$s$$$ and the maximum number of moves you can make. The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. The third line of the input contains the string $$$t$$$ consisting of two lowercase Latin letters. Output Print one integer — the maximum possible number of occurrences of $$$t$$$ in $$$s$$$ as a subsequence if you replace no more than $$$k$$$ characters in $$$s$$$ optimally. Examples Input 15 6 qwertyhgfdsazxc qa Note In the first example, you can obtain the string "abab" replacing $$$s_1$$$ with 'a' and $$$s_4$$$ with 'b'. Then the answer is $$$3$$$. In the second example, you can obtain the string "ssddsdd" and get the answer $$$10$$$. In the fourth example, you can obtain the string "aaacaaa" and get the answer $$$15$$$.
2,100
false
false
false
true
false
false
false
false
false
false
3,622
2039C2
This is the hard version of the problem. The differences between the two versions are highlighted in bold. You can only make hacks if both versions of the problem are solved. Shohag has two integers $$$x$$$ and $$$m$$$. Help him count the number of integers $$$1 le y le m$$$ such that $$$x oplus y$$$ is divisible$$$^{ ext{∗}}$$$ by either $$$x$$$, $$$y$$$, or both. Here $$$oplus$$$ is the xa0— the number of test cases. The first and only line of each test case contains two space-separated integers $$$x$$$ and $$$m$$$ ($$$1 le x le 10^6$$$, $$$1 le m le 10^{18}$$$). It is guaranteed that the sum of $$$x$$$ over all test cases does not exceed $$$10^7$$$. Output For each test case, print a single integerxa0— the number of suitable $$$y$$$. Note In the first test case, for $$$x = 7$$$, there are $$$3$$$ valid values for $$$y$$$ among the integers from $$$1$$$ to $$$m = 10$$$, and they are $$$1$$$, $$$7$$$, and $$$9$$$. $$$y = 1$$$ is valid because $$$x oplus y = 7 oplus 1 = 6$$$ and $$$6$$$ is divisible by $$$y = 1$$$. $$$y = 7$$$ is valid because $$$x oplus y = 7 oplus 7 = 0$$$ and $$$0$$$ is divisible by both $$$x = 7$$$ and $$$y = 7$$$. $$$y = 9$$$ is valid because $$$x oplus y = 7 oplus 9 = 14$$$ and $$$14$$$ is divisible by $$$x = 7$$$.
1,800
true
false
false
false
false
false
true
false
false
false
21
1353C
You are given a board of size $$$n imes n$$$, where $$$n$$$ is odd (not divisible by $$$2$$$). Initially, each cell of the board contains one figure. In one move, you can select exactly one figure presented in some cell and move it to one of the cells sharing a side or a corner with the current cell, i.e. from the cell $$$(i, j)$$$ you can move the figure to cells: $$$(i - 1, j - 1)$$$; $$$(i - 1, j)$$$; $$$(i - 1, j + 1)$$$; $$$(i, j - 1)$$$; $$$(i, j + 1)$$$; $$$(i + 1, j - 1)$$$; $$$(i + 1, j)$$$; $$$(i + 1, j + 1)$$$; Of course, you can not move figures to cells out of the board. It is allowed that after a move there will be several figures in one cell. Your task is to find the minimum number of moves needed to get all the figures into one cell (i.e. $$$n^2-1$$$ cells should contain $$$0$$$ figures and one cell should contain $$$n^2$$$ figures). You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 200$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 le n < 5 cdot 10^5$$$) — the size of the board. It is guaranteed that $$$n$$$ is odd (not divisible by $$$2$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^5$$$ ($$$sum n le 5 cdot 10^5$$$). Output For each test case print the answer — the minimum number of moves needed to get all the figures into one cell. Example Output 0 40 41664916690999888
1,000
true
false
false
false
false
false
false
false
false
false
3,943
405C
Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix. The dot product of two integer number vectors _x_ and _y_ of size _n_ is the sum of the products of the corresponding components of the vectors. The unusual square of an _n_u2009×u2009_n_ square matrix _A_ is defined as the sum of _n_ dot products. The _i_-th of them is the dot product of the _i_-th row vector and the _i_-th column vector in the matrix _A_. Fortunately for Chris, he has to work only in _GF_(2)! This means that all operations (addition, multiplication) are calculated modulo 2. In fact, the matrix _A_ is binary: each element of _A_ is either 0 or 1. For example, consider the following matrix _A_: The unusual square of _A_ is equal to (1·1u2009+u20091·0u2009+u20091·1)u2009+u2009(0·1u2009+u20091·1u2009+u20091·0)u2009+u2009(1·1u2009+u20090·1u2009+u20090·0)u2009=u20090u2009+u20091u2009+u20091u2009=u20090. However, there is much more to the homework. Chris has to process _q_ queries; each query can be one of the following: 1. given a row index _i_, flip all the values in the _i_-th row in _A_; 2. given a column index _i_, flip all the values in the _i_-th column in _A_; 3. find the unusual square of _A_. To flip a bit value _w_ means to change it to 1u2009-u2009_w_, i.e., 1 changes to 0 and 0 changes to 1. Given the initial matrix _A_, output the answers for each query of the third type! Can you solve Chris's homework? Input The first line of input contains an integer _n_ (1u2009≤u2009_n_u2009≤u20091000), the number of rows and the number of columns in the matrix _A_. The next _n_ lines describe the matrix: the _i_-th line contains _n_ space-separated bits and describes the _i_-th row of _A_. The _j_-th number of the _i_-th line _a__ij_ (0u2009≤u2009_a__ij_u2009≤u20091) is the element on the intersection of the _i_-th row and the _j_-th column of _A_. The next line of input contains an integer _q_ (1u2009≤u2009_q_u2009≤u2009106), the number of queries. Each of the next _q_ lines describes a single query, which can be one of the following: 1 _i_ — flip the values of the _i_-th row; 2 _i_ — flip the values of the _i_-th column; 3 — output the unusual square of _A_. Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++. Output Let the number of the 3rd type queries in the input be _m_. Output a single string _s_ of length _m_, where the _i_-th symbol of _s_ is the value of the unusual square of _A_ for the _i_-th query of the 3rd type as it appears in the input.
1,600
true
false
true
false
false
false
false
false
false
false
8,203
1260B
You are given two integers $$$a$$$ and $$$b$$$. You may perform any number of operations on them (possibly zero). During each operation you should choose any positive integer $$$x$$$ and set $$$a := a - x$$$, $$$b := b - 2x$$$ or $$$a := a - 2x$$$, $$$b := b - x$$$. Note that you may choose different values of $$$x$$$ in different operations. Is it possible to make $$$a$$$ and $$$b$$$ equal to $$$0$$$ simultaneously? Your program should answer $$$t$$$ independent test cases. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. Then the test cases follow, each test case is represented by one line containing two integers $$$a$$$ and $$$b$$$ for this test case ($$$0 le a, b le 10^9$$$). Output For each test case print the answer to it — YES if it is possible to make $$$a$$$ and $$$b$$$ equal to $$$0$$$ simultaneously, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Note In the first test case of the example two operations can be used to make both $$$a$$$ and $$$b$$$ equal to zero: 1. choose $$$x = 4$$$ and set $$$a := a - x$$$, $$$b := b - 2x$$$. Then $$$a = 6 - 4 = 2$$$, $$$b = 9 - 8 = 1$$$; 2. choose $$$x = 1$$$ and set $$$a := a - 2x$$$, $$$b := b - x$$$. Then $$$a = 2 - 2 = 0$$$, $$$b = 1 - 1 = 0$$$.
1,300
true
false
false
false
false
false
false
true
false
false
4,393
1365G
2,800
true
false
false
false
false
true
false
false
false
false
3,851
300B
A programming coach has _n_ students to teach. We know that _n_ is divisible by 3. Let's assume that all students are numbered from 1 to _n_, inclusive. Before the university programming championship the coach wants to split all students into groups of three. For some pairs of students we know that they want to be on the same team. Besides, if the _i_-th student wants to be on the same team with the _j_-th one, then the _j_-th student wants to be on the same team with the _i_-th one. The coach wants the teams to show good results, so he wants the following condition to hold: if the _i_-th student wants to be on the same team with the _j_-th, then the _i_-th and the _j_-th students must be on the same team. Also, it is obvious that each student must be on exactly one team. Help the coach and divide the teams the way he wants. Input The first line of the input contains integers _n_ and _m_ (3u2009≤u2009_n_u2009≤u200948, . Then follow _m_ lines, each contains a pair of integers _a__i_,u2009_b__i_ (1u2009≤u2009_a__i_u2009<u2009_b__i_u2009≤u2009_n_) — the pair _a__i_,u2009_b__i_ means that students with numbers _a__i_ and _b__i_ want to be on the same team. It is guaranteed that _n_ is divisible by 3. It is guaranteed that each pair _a__i_,u2009_b__i_ occurs in the input at most once. Output If the required division into teams doesn't exist, print number -1. Otherwise, print lines. In each line print three integers _x__i_, _y__i_, _z__i_ (1u2009≤u2009_x__i_,u2009_y__i_,u2009_z__i_u2009≤u2009_n_) — the _i_-th team. If there are multiple answers, you are allowed to print any of them.
1,500
false
false
false
false
false
false
true
false
false
true
8,634
1666C
Problem - 1666C - 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 constructive algorithms geometry *1800 No tag edit access → Contest materials ") ") . The points are pairwise distinct. Output On the first line output $$$n$$$xa0— the number of segments, at most 100. The next $$$n$$$ lines should contain descriptions of segments. Output four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ on a linexa0— the coordinates of the endpoints of the corresponding segment ($$$-10^9 le x_1, y_1, x_2, y_2 le 10^9$$$). Each segment should be either horizontal or vertical. It is guaranteed that the solution with the given constraints exists. Example Input 1 1 3 5 8 6 Output 3 1 1 1 5 1 5 8 5 8 5 8 6 Note The points and the segments from the example are shown below.
1,800
false
false
false
false
false
true
true
false
false
false
2,283
1805D
You are given a tree (a connected graph without cycles) with $$$n$$$ vertices. Consider a fixed integer $$$k$$$. Then, the graph $$$G_k$$$ is an undirected graph with $$$n$$$ vertices, where an edge between vertices $$$u$$$ and $$$v$$$ exists if and only if the distance between vertices $$$u$$$ and $$$v$$$ in the given tree is at least $$$k$$$. For each $$$k$$$ from $$$1$$$ to $$$n$$$, print the number of connected components in the graph $$$G_k$$$. Input The first line contains the integer $$$n$$$ ($$$2 le n le 10^5$$$)xa0— the number of vertices in the graph. Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$), denoting an edge between vertices $$$u$$$ and $$$v$$$ in the tree. It is guaranteed that these edges form a valid tree. Output Output $$$n$$$ integers: the number of connected components in the graph $$$G_k$$$ for each $$$k$$$ from $$$1$$$ to $$$n$$$. Examples Input 6 1 2 1 3 2 4 2 5 3 6 Note In the first example: If $$$k=1$$$, the graph has an edge between each pair of vertices, so it has one component. If $$$k=4$$$, the graph has only edges $$$4 leftrightarrow 6$$$ and $$$5 leftrightarrow 6$$$, so the graph has $$$4$$$ components. In the second example: when $$$k=1$$$ or $$$k=2$$$ the graph has one component. When $$$k=3$$$ the graph $$$G_k$$$ splits into $$$3$$$ components: one component has vertices $$$1$$$, $$$4$$$ and $$$5$$$, and two more components contain one vertex each. When $$$k=4$$$ or $$$k=5$$$ each vertex is a separate component.
1,800
false
true
false
true
false
false
false
false
false
true
1,446
1060D
You invited $$$n$$$ guests to dinner! You plan to arrange one or more circles of chairs. Each chair is going to be either occupied by one guest, or be empty. You can make any number of circles. Your guests happen to be a little bit shy, so the $$$i$$$-th guest wants to have a least $$$l_i$$$ free chairs to the left of his chair, and at least $$$r_i$$$ free chairs to the right. The "left" and "right" directions are chosen assuming all guests are going to be seated towards the center of the circle. Note that when a guest is the only one in his circle, the $$$l_i$$$ chairs to his left and $$$r_i$$$ chairs to his right may overlap. What is smallest total number of chairs you have to use? Input First line contains one integer $$$n$$$ xa0— number of guests, ($$$1 leqslant n leqslant 10^5$$$). Next $$$n$$$ lines contain $$$n$$$ pairs of space-separated integers $$$l_i$$$ and $$$r_i$$$ ($$$0 leqslant l_i, r_i leqslant 10^9$$$). Output Output a single integerxa0— the smallest number of chairs you have to use. Note In the second sample the only optimal answer is to use two circles: a circle with $$$5$$$ chairs accomodating guests $$$1$$$ and $$$2$$$, and another one with $$$10$$$ chairs accomodationg guests $$$3$$$ and $$$4$$$. In the third sample, you have only one circle with one person. The guest should have at least five free chairs to his left, and at least six free chairs to his right to the next person, which is in this case the guest herself. So, overall number of chairs should be at least 6+1=7.
1,900
true
true
false
false
false
false
false
false
false
false
5,413
175B
Vasya has been playing Plane of Tanks with his friends the whole year. Now it is time to divide the participants into several categories depending on their results. A player is given a non-negative integer number of points in each round of the Plane of Tanks. Vasya wrote results for each round of the last year. He has _n_ records in total. In order to determine a player's category consider the best result obtained by the player and the best results of other players. The player belongs to category: "noob" — if more than 50% of players have better results; "random" — if his result is not worse than the result that 50% of players have, but more than 20% of players have better results; "average" — if his result is not worse than the result that 80% of players have, but more than 10% of players have better results; "hardcore" — if his result is not worse than the result that 90% of players have, but more than 1% of players have better results; "pro" — if his result is not worse than the result that 99% of players have. When the percentage is calculated the player himself is taken into account. That means that if two players played the game and the first one gained 100 points and the second one 1000 points, then the first player's result is not worse than the result that 50% of players have, and the second one is not worse than the result that 100% of players have. Vasya gave you the last year Plane of Tanks results. Help Vasya determine each player's category. Input The first line contains the only integer number _n_ (1u2009≤u2009_n_u2009≤u20091000) — a number of records with the players' results. Each of the next _n_ lines contains a player's name and the amount of points, obtained by the player for the round, separated with a space. The name contains not less than 1 and no more than 10 characters. The name consists of lowercase Latin letters only. It is guaranteed that any two different players have different names. The amount of points, obtained by the player for the round, is a non-negative integer number and does not exceed 1000. Output Print on the first line the number _m_ — the number of players, who participated in one round at least. Each one of the next _m_ lines should contain a player name and a category he belongs to, separated with space. Category can be one of the following: "noob", "random", "average", "hardcore" or "pro" (without quotes). The name of each player should be printed only once. Player names with respective categories can be printed in an arbitrary order. Examples Input 5 vasya 100 vasya 200 artem 100 kolya 200 igor 250 Output 4 artem noob igor pro kolya random vasya random Input 3 vasya 200 kolya 1000 vasya 1000 Output 2 kolya pro vasya pro Note In the first example the best result, obtained by artem is not worse than the result that 25% of players have (his own result), so he belongs to category "noob". vasya and kolya have best results not worse than the results that 75% players have (both of them and artem), so they belong to category "random". igor has best result not worse than the result that 100% of players have (all other players and himself), so he belongs to category "pro". In the second example both players have the same amount of points, so they have results not worse than 100% players have, so they belong to category "pro".
1,400
false
false
true
false
false
false
false
false
false
false
9,164
100I
Problem - 100I - Codeforces =============== xa0 , around the origin. What is the result of this rotation? Input A single integer _k_ (0u2009≤u2009_k_u2009<u2009360) is given in the first line. Two integer numbers _x_ and _y_ are given in the second line (u2009-u20091390u2009≤u2009_x_,u2009_y_u2009≤u20091390). Output Write two numbers. The result of the rotation. Your answer must have a relative error less than 10u2009-u20091. Examples Input 90 1 1 Output -1.00000000 1.00000000 Input 180 1 1390 Output -1.00000000 -1390.00000000
1,500
true
false
false
false
false
false
false
false
false
false
9,481
1473E
You are given a weighted undirected connected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. It is guaranteed that there are no self-loops or multiple edges in the given graph. Let's define the weight of the path consisting of $$$k$$$ edges with indices $$$e_1, e_2, dots, e_k$$$ as $$$sumlimits_{i=1}^{k}{w_{e_i}} - maxlimits_{i=1}^{k}{w_{e_i}} + minlimits_{i=1}^{k}{w_{e_i}}$$$, where $$$w_i$$$ — weight of the $$$i$$$-th edge in the graph. Your task is to find the minimum weight of the path from the $$$1$$$-st vertex to the $$$i$$$-th vertex for each $$$i$$$ ($$$2 le i le n$$$). Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 2 cdot 10^5$$$; $$$1 le m le 2 cdot 10^5$$$) — the number of vertices and the number of edges in the graph. Following $$$m$$$ lines contains three integers $$$v_i, u_i, w_i$$$ ($$$1 le v_i, u_i le n$$$; $$$1 le w_i le 10^9$$$; $$$v_i eq u_i$$$) — endpoints of the $$$i$$$-th edge and its weight respectively. Output Print $$$n-1$$$ integers — the minimum weight of the path from $$$1$$$-st vertex to the $$$i$$$-th vertex for each $$$i$$$ ($$$2 le i le n$$$). Examples Input 5 4 5 3 4 2 1 1 3 2 2 2 4 2 Input 6 8 3 1 1 3 6 2 5 4 2 4 2 2 6 1 1 5 2 1 3 2 3 1 5 4 Input 7 10 7 5 5 2 3 3 4 7 1 5 3 6 2 7 6 6 2 6 3 7 6 4 2 1 3 1 4 1 7 4
2,400
false
false
false
false
false
false
false
false
false
true
3,313
2009E
Problem - 2009E - 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 math ternary search *1400 No tag edit access → Contest materials ") Editorial") — Video Tutorial") such that $$$x = a_1 + a_2 + dots + a_i - a_{i+1} - dots - a_n$$$ is minimized. Note that for an arbitrary integer $$$z$$$, $$$z$$$ represents the absolute value of $$$z$$$. Output the minimum possible value of $$$x$$$. Input The first line contains $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. Each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 leq n, k leq 10^9$$$) — the length of the array and the starting element of the array. Output For each test case, output the minimum value of $$$x$$$ on a new line. Example Input 4 2 2 7 2 5 3 1000000000 1000000000 Output 1 5 1 347369930 Note In the first sample, $$$a =
1,400
true
false
false
false
false
false
false
true
false
false
196
331C1
Yet another Armageddon is coming! This time the culprit is the Julya tribe calendar. The beavers in this tribe knew math very well. Smart Beaver, an archaeologist, got a sacred plate with a magic integer on it. The translation from Old Beaverish is as follows: "May the Great Beaver bless you! May your chacres open and may your third eye never turn blind from beholding the Truth! Take the magic number, subtract a digit from it (the digit must occur in the number) and get a new magic number. Repeat this operation until a magic number equals zero. The Earth will stand on Three Beavers for the time, equal to the number of subtractions you perform!" Distinct subtraction sequences can obviously get you different number of operations. But the Smart Beaver is ready to face the worst and is asking you to count the minimum number of operations he needs to reduce the magic number to zero. Input The single line contains the magic integer _n_, 0u2009≤u2009_n_. to get 20 points, you need to solve the problem with constraints: _n_u2009≤u2009106 (subproblem C1); to get 40 points, you need to solve the problem with constraints: _n_u2009≤u20091012 (subproblems C1+C2); to get 100 points, you need to solve the problem with constraints: _n_u2009≤u20091018 (subproblems C1+C2+C3). Output Print a single integer — the minimum number of subtractions that turns the magic number to a zero. Note In the first test sample the minimum number of operations can be reached by the following sequence of subtractions: 24u2009→u200920u2009→u200918u2009→u200910u2009→u20099u2009→u20090
1,100
false
false
false
true
false
false
false
false
false
false
8,518
301D
Problem - 301D - 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 *2200 No tag edit access → Contest materials , consisting of _n_ distinct integers. Also, he has _m_ queries: Query number _i_ is represented as a pair of integers _l__i_, _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_). The answer to the query _l__i_,u2009_r__i_ is the number of pairs of integers _q_, _w_ (_l__i_u2009≤u2009_q_,u2009_w_u2009≤u2009_r__i_) such that _p__q_ is the divisor of _p__w_. Help Yaroslav, answer all his queries. Input The first line contains the integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20092·105). The second line contains _n_ distinct integers _p_1,u2009_p_2,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009≤u2009_n_). The following _m_ lines contain Yaroslav's queries. The _i_-th line contains integers _l__i_,u2009_r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_). Output Print _m_ integers — the answers to Yaroslav's queries in the order they appear in the input. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1 1 1 1 Output 1 Input 10 9 1 2 3 4 5 6 7 8 9 10 1 10 2 9 3 8 4 7 5 6 2 2 9 10 5 10 4 10 Output 27 14 8 4 2 1 2 7 9
2,200
false
false
false
false
true
false
false
false
false
false
8,627
1729C
Polycarp was given a row of tiles. Each tile contains one lowercase letter of the Latin alphabet. The entire sequence of tiles forms the string $$$s$$$. In other words, you are given a string $$$s$$$ consisting of lowercase Latin letters. Initially, Polycarp is on the first tile of the row and wants to get to the last tile by jumping on the tiles. Jumping from $$$i$$$-th tile to $$$j$$$-th tile has a cost equal to $$$index(s_i) - index(s_j)$$$, where $$$index(c)$$$ is the index of the letter $$$c$$$ in the alphabet (for example, $$$index($$$'a'$$$)=1$$$, $$$index($$$'b'$$$)=2$$$, ..., $$$index($$$'z'$$$)=26$$$) . Polycarp wants to get to the $$$n$$$-th tile for the minimum total cost, but at the same time make maximum number of jumps. In other words, among all possible ways to get to the last tile for the minimum total cost, he will choose the one with the maximum number of jumps. Polycarp can visit each tile at most once. Polycarp asks you to helpxa0— print the sequence of indices of string $$$s$$$ on which he should jump. Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases in the test. Each test case is given by the string $$$s$$$ ($$$2 le s le 2 cdot 10^5$$$), where $$$s$$$xa0— is the length of string $$$s$$$. The string $$$s$$$ consists of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output The answer to each test case consists of two lines. In the first line print two integers $$$cost$$$, $$$m$$$, where $$$cost$$$ is the minimum total cost of the path, and $$$m$$$ is the maximum number of visited tiles Polycarp can make to get to $$$n$$$-th tiles for the minimum total cost $$$cost$$$ (i.e. the number of jumps is $$$m-1$$$). In the next line print $$$m$$$ different numbers $$$j_1, j_2, dots, j_m$$$ ($$$1 le j_i le s$$$)xa0— the sequence of indices of the tiles Polycarp will jump on. The first number in the sequence must be $$$1$$$ (that is, $$$j_1=1$$$) and the last number must be the value of $$$s$$$ (that is, $$$j_m=s$$$). If there are multiple answers, print any of them. Example Input 6 logic codeforces bca aaaaaaaaaaa adbaadabad to Output 9 4 1 4 3 5 16 10 1 8 3 4 9 5 2 6 7 10 1 2 1 3 0 11 1 8 10 4 3 5 7 2 9 6 11 3 10 1 9 5 4 7 3 8 6 2 10 5 2 1 2 Note In the first test case, the required path corresponds to the picture: In this case, the minimum possible total cost of the path is achieved. Since $$$index($$$'l'$$$)=12$$$, $$$index($$$'o'$$$)=15$$$, $$$index($$$'g'$$$)=7$$$, $$$index($$$'i'$$$)=9$$$, $$$index($$$'c'$$$)=3$$$, then the total cost of the path is $$$12-9+9-7+7-3=3+2+4=9$$$.
1,100
false
false
false
false
false
true
false
false
false
false
1,916
711A
ZS the Coder and Chris the Baboon are travelling to Udayland! To get there, they have to get on the special IOI bus. The IOI bus has _n_ rows of seats. There are 4 seats in each row, and the seats are separated into pairs by a walkway. When ZS and Chris came, some places in the bus was already occupied. ZS and Chris are good friends. They insist to get a pair of neighbouring empty seats. Two seats are considered neighbouring if they are in the same row and in the same pair. Given the configuration of the bus, can you help ZS and Chris determine where they should sit? Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20091000)xa0— the number of rows of seats in the bus. Then, _n_ lines follow. Each line contains exactly 5 characters, the first two of them denote the first pair of seats in the row, the third character denotes the walkway (it always equals '') and the last two of them denote the second pair of seats in the row. Each character, except the walkway, equals to 'O' or to 'X'. 'O' denotes an empty seat, 'X' denotes an occupied seat. See the sample cases for more details. Output If it is possible for Chris and ZS to sit at neighbouring empty seats, print "YES" (without quotes) in the first line. In the next _n_ lines print the bus configuration, where the characters in the pair of seats for Chris and ZS is changed with characters '+'. Thus the configuration should differ from the input one by exactly two charaters (they should be equal to 'O' in the input and to '+' in the output). If there is no pair of seats for Chris and ZS, print "NO" (without quotes) in a single line. If there are multiple solutions, you may print any of them. Examples Input 6 OOOX XOXX OXOO XXOX OOOO OOXX Output YES ++OX XOXX OXOO XXOX OOOO OOXX Input 4 XOOX XOXX OXOX XXOX Input 5 XXXX XXXX XOOX XOOO OXXO Output YES XXXX XXXX XOOX XO++ OXXO Note Note that the following is an incorrect configuration for the first sample case because the seats must be in the same pair. O++X XOXX OXOO XXOX OOOO OOXX
800
false
false
true
false
false
false
true
false
false
false
6,976
523A
Polycarp is writing the prototype of a graphic editor. He has already made up his mind that the basic image transformations in his editor will be: rotate the image 90 degrees clockwise, flip the image horizontally (symmetry relative to the vertical line, that is, the right part of the image moves to the left, and vice versa) and zooming on the image. He is sure that that there is a large number of transformations that can be expressed through these three. He has recently stopped implementing all three transformations for monochrome images. To test this feature, he asked you to write a code that will consecutively perform three actions with a monochrome image: first it will rotate the image 90 degrees clockwise, then it will flip the image horizontally and finally, it will zoom in twice on the image (that is, it will double all the linear sizes). Implement this feature to help Polycarp test his editor. Input The first line contains two integers, _w_ and _h_ (1u2009≤u2009_w_,u2009_h_u2009≤u2009100) — the width and height of an image in pixels. The picture is given in _h_ lines, each line contains _w_ characters — each character encodes the color of the corresponding pixel of the image. The line consists only of characters "." and "*", as the image is monochrome. Output Print 2_w_ lines, each containing 2_h_ characters — the result of consecutive implementing of the three transformations, described above. Examples Output .... .... *** *** .... .... Input 9 20 **....... ****..... ******... *******.. ..******. ....****. ......** *.....** ******** ******** ******** ******** ....**... ...****.. ..******. .******* ****..** ***...** **.....* *....... Output ********......**********........******* ********......**********........******* ********........********......********.. ********........********......********.. ..********......********....********.... ..********......********....********.... ..********......********..********...... ..********......********..********...... ....********....****************........ ....********....****************........ ....********....****************........ ....********....****************........ ......******************..**********.... ......******************..**********.... ........****************....**********.. ........****************....**********.. ............************......********* ............************......**********
1,200
false
false
true
false
false
false
false
false
false
false
7,746
1081G
Chouti thought about his very first days in competitive programming. When he had just learned to write merge sort, he thought that the merge sort is too slow, so he restricted the maximum depth of recursion and modified the merge sort to the following: Chouti found his idea dumb since obviously, this "merge sort" sometimes cannot sort the array correctly. However, Chouti is now starting to think of how good this "merge sort" is. Particularly, Chouti wants to know for a random permutation $$$a$$$ of $$$1, 2, ldots, n$$$ the expected number of inversions after calling MergeSort(a, 1, n, k). It can be proved that the expected number is rational. For the given prime $$$q$$$, suppose the answer can be denoted by $$$frac{u}{d}$$$ where $$$gcd(u,d)=1$$$, you need to output an integer $$$r$$$ satisfying $$$0 le r<q$$$ and $$$rd equiv u pmod q$$$. It can be proved that such $$$r$$$ exists and is unique. Note In the first example, all possible permutations are $$$[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]$$$. With $$$k=1$$$, MergeSort(a, 1, n, k) will only return the original permutation. Thus the answer is $$$9/6=3/2$$$, and you should output $$$499122178$$$ because $$$499122178 imes 2 equiv 3 pmod {998244353}$$$. In the second example, all possible permutations are $$$[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]$$$ and the corresponding outputs of MergeSort(a, 1, n, k) are $$$[1,2,3],[1,2,3],[2,1,3],[1,2,3],[2,3,1],[1,3,2]$$$ respectively. Thus the answer is $$$4/6=2/3$$$, and you should output $$$665496236$$$ because $$$665496236 imes 3 equiv 2 pmod {998244353}$$$.
3,200
true
false
false
false
false
false
false
false
false
false
5,317
894B
Problem - 894B - 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 constructive algorithms math number theory *1800 No tag edit access → Contest materials ") Editorial") . Output Print a single number denoting the answer modulo 1000000007. Examples Input 1 1 -1 Output 1 Input 1 3 1 Output 1 Input 3 3 -1 Output 16 Note In the first example the only way is to put -1 into the only block. In the second example the only way is to put 1 into every block.
1,800
true
false
false
false
false
true
false
false
false
false
6,174
234E
In the autumn of this year, two Russian teams came into the group stage of the most prestigious football club competition in the world — the UEFA Champions League. Now, these teams have already started to play in the group stage and are fighting for advancing to the playoffs. In this problem we are interested in the draw stage, the process of sorting teams into groups. The process of the draw goes as follows (the rules that are described in this problem, are somehow simplified compared to the real life). Suppose _n_ teams will take part in the group stage (_n_ is divisible by four). The teams should be divided into groups of four. Let's denote the number of groups as _m_ (). Each team has a rating — an integer characterizing the team's previous achievements. The teams are sorted by the rating's decreasing (no two teams have the same rating). After that four "baskets" are formed, each of which will contain _m_ teams: the first _m_ teams with the highest rating go to the first basket, the following _m_ teams go to the second one, and so on. Then the following procedure repeats _m_u2009-u20091 times. A team is randomly taken from each basket, first from the first basket, then from the second, then from the third, and at last, from the fourth. The taken teams form another group. After that, they are removed from their baskets. The four teams remaining in the baskets after (_m_u2009-u20091) such procedures are performed, form the last group. In the real draw the random selection of teams from the basket is performed by people — as a rule, the well-known players of the past. As we have none, we will use a random number generator, which is constructed as follows. Its parameters are four positive integers _x_,u2009_a_,u2009_b_,u2009_c_. Every time there is a call to the random number generator, it produces the following actions: Operation means taking the remainder after division: , . A random number generator will be used in the draw as follows: each time we need to randomly choose a team from the basket, it will generate a random number _k_. The teams that yet remain in the basket are considered numbered with consecutive integers from 0 to _s_u2009-u20091, in the order of decreasing rating, where _s_ is the current size of the basket. Then a team number is taken from the basket. Given a list of teams and the parameters of the random number generator, determine the result of the draw. Input The first input line contains integer _n_ (4u2009≤u2009_n_u2009≤u200964, _n_ is divisible by four) — the number of teams that take part in the sorting. The second line contains four space-separated integers _x_,u2009_a_,u2009_b_,u2009_c_ (1u2009≤u2009_x_,u2009_a_,u2009_b_,u2009_c_u2009≤u20091000) — the parameters of the random number generator. Each of the following _n_ lines describes one team. The description consists of the name of the team and its rating, separated by a single space. The name of a team consists of uppercase and lowercase English letters and has length from 1 to 20 characters. A team's rating is an integer from 0 to 1000. All teams' names are distinct. All team's ratings are also distinct.
1,600
false
false
true
false
false
false
false
false
false
false
8,900
1731C
You are given an integer array $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$). Find the number of subarrays of $$$a$$$ whose $$$operatorname{XOR}$$$ has an even number of divisors. In other words, find all pairs of indices $$$(i, j)$$$ ($$$i le j$$$) such that $$$a_i oplus a_{i + 1} oplus dots oplus a_j$$$ has an even number of divisors. For example, numbers $$$2$$$, $$$3$$$, $$$5$$$ or $$$6$$$ have an even number of divisors, while $$$1$$$ and $$$4$$$xa0— odd. Consider that $$$0$$$ has an odd number of divisors in this task. Here $$$operatorname{XOR}$$$ (or $$$oplus$$$) denotes the . Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 leq n leq 2 cdot 10^5$$$)xa0— the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print the number of subarrays, whose $$$operatorname{XOR}$$$ has an even number of divisors. Note In the first test case, there are $$$4$$$ subarrays whose $$$operatorname{XOR}$$$ has an even number of divisors: $$$[3]$$$, $$$[3,1]$$$, $$$[1,2]$$$, $$$[2]$$$. In the second test case, there are $$$11$$$ subarrays whose $$$operatorname{XOR}$$$ has an even number of divisors: $$$[4,2]$$$, $$$[4,2,1]$$$, $$$[4,2,1,5]$$$, $$$[2]$$$, $$$[2,1]$$$, $$$[2,1,5]$$$, $$$[2,1,5,3]$$$, $$$[1,5,3]$$$, $$$[5]$$$, $$$[5,3]$$$, $$$[3]$$$. In the third test case, there is no subarray whose $$$operatorname{XOR}$$$ has an even number of divisors since $$$operatorname{XOR}$$$ of any subarray is either $$$4$$$ or $$$0$$$.
1,700
true
false
false
false
false
false
true
false
false
false
1,903
1612F
Monocarp plays a computer game. There are $$$n$$$ different sets of armor and $$$m$$$ different weapons in this game. If a character equips the $$$i$$$-th set of armor and wields the $$$j$$$-th weapon, their power is usually equal to $$$i + j$$$; but some combinations of armor and weapons synergize well. Formally, there is a list of $$$q$$$ ordered pairs, and if the pair $$$(i, j)$$$ belongs to this list, the power of the character equipped with the $$$i$$$-th set of armor and wielding the $$$j$$$-th weapon is not $$$i + j$$$, but $$$i + j + 1$$$. Initially, Monocarp's character has got only the $$$1$$$-st armor set and the $$$1$$$-st weapon. Monocarp can obtain a new weapon or a new set of armor in one hour. If he wants to obtain the $$$k$$$-th armor set or the $$$k$$$-th weapon, he must possess a combination of an armor set and a weapon that gets his power to $$$k$$$ or greater. Of course, after Monocarp obtains a weapon or an armor set, he can use it to obtain new armor sets or weapons, but he can go with any of the older armor sets and/or weapons as well. Monocarp wants to obtain the $$$n$$$-th armor set and the $$$m$$$-th weapon. What is the minimum number of hours he has to spend on it? Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n, m le 2 cdot 10^5$$$) — the number of armor sets and the number of weapons, respectively. The second line contains one integer $$$q$$$ ($$$0 le q le min(2 cdot 10^5, nm)$$$) — the number of combinations that synergize well. Then $$$q$$$ lines follow, the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i le n$$$; $$$1 le b_i le m$$$) meaning that the $$$a_i$$$-th armor set synergizes well with the $$$b_i$$$-th weapon. All pairs $$$(a_i, b_i)$$$ are distinct. Output Print one integer — the minimum number of hours Monocarp has to spend to obtain both the $$$n$$$-th armor set and the $$$m$$$-th weapon. Note In the first example, Monocarp can obtain the strongest armor set and the strongest weapon as follows: 1. Obtain the $$$2$$$-nd weapon using the $$$1$$$-st armor set and the $$$1$$$-st weapon; 2. Obtain the $$$3$$$-rd armor set using the $$$1$$$-st armor set and the $$$2$$$-nd weapon; 3. Obtain the $$$4$$$-th weapon using the $$$3$$$-rd armor set and the $$$2$$$-nd weapon. In the second example, Monocarp can obtain the strongest armor set and the strongest weapon as follows: 1. Obtain the $$$3$$$-rd armor set using the $$$1$$$-st armor set and the $$$1$$$-st weapon (they synergize well, so Monocarp's power is not $$$2$$$ but $$$3$$$); 2. Obtain the $$$4$$$-th weapon using the $$$3$$$-rd armor set and the $$$1$$$-st weapon.
2,800
false
true
false
true
false
false
true
false
false
false
2,578
935D
Ancient Egyptians are known to have used a large set of symbols to write on the walls of the temples. Fafa and Fifa went to one of the temples and found two non-empty words _S_1 and _S_2 of equal lengths on the wall of temple written one below the other. Since this temple is very ancient, some symbols from the words were erased. The symbols in the set have equal probability for being in the position of any erased symbol. Fifa challenged Fafa to calculate the probability that _S_1 is lexicographically greater than _S_2. Can you help Fafa with this task? You know that , i.xa0e. there were _m_ distinct characters in Egyptians' alphabet, in this problem these characters are denoted by integers from 1 to _m_ in alphabet order. A word _x_ is lexicographically greater than a word _y_ of the same length, if the words are same up to some position, and then the word _x_ has a larger character, than the word _y_. We can prove that the probability equals to some fraction , where _P_ and _Q_ are coprime integers, and . Print as the answer the value , i.xa0e. such a non-negative integer less than 109u2009+u20097, such that , where means that _a_ and _b_ give the same remainders when divided by _m_. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009u2009_m_u2009≤u2009105) — the length of each of the two words and the size of the alphabet , respectively. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009_m_) — the symbols of _S_1. If _a__i_u2009=u20090, then the symbol at position _i_ was erased. The third line contains _n_ integers representing _S_2 with the same format as _S_1. Output Print the value , where _P_ and _Q_ are coprime and is the answer to the problem. Examples Input 7 26 0 15 12 9 13 0 14 11 1 0 13 15 12 0 Note In the first sample, the first word can be converted into (1) or (2). The second option is the only one that will make it lexicographically larger than the second word. So, the answer to the problem will be , that is 500000004, because . In the second example, there is no replacement for the zero in the second word that will make the first one lexicographically larger. So, the answer to the problem is , that is 0.
1,900
true
false
false
false
false
false
false
false
false
false
5,961
353E
You have a directed acyclic graph _G_, consisting of _n_ vertexes, numbered from 0 to _n_u2009-u20091. The graph contains _n_ edges numbered from 0 to _n_u2009-u20091. An edge with number _i_ connects vertexes _i_ and (_i_u2009+u20091)xa0_mod_xa0_n_, and it can be directed in either direction (from _i_ to (_i_u2009+u20091)xa0_mod_xa0_n_, or vise versa). Operation _x_xa0_mod_xa0_y_ means taking the remainder after dividing number _x_ by number _y_. Let's call two vertexes _u_ and _v_ in graph _G_ comparable if the graph contains a path either from _u_ to _v_ or from _v_ to _u_. We'll assume that an antichain is a set of vertexes of graph _G_, where any two distinct vertexes are not comparable. The size of an antichain is the number of vertexes in the corresponding set. An antichain is maximum if the graph doesn't have antichains of a larger size. Your task is to find the size of the maximum antichain in graph _G_. Input The first line contains the sequence of characters _s_0_s_1... _s__n_u2009-u20091 (2u2009≤u2009_n_u2009≤u2009106), consisting of numbers zero and one. The length of the line (number _n_) corresponds to the number of vertexes and edges in graph _G_. If character _s__i_ (_i_u2009≥u20090) equals 0, then the edge between vertexes _i_ and (_i_u2009+u20091)xa0_mod_xa0_n_ is directed from the _i_-th vertex to the (_i_u2009+u20091)xa0_mod_xa0_n_-th one, otherwise — to the opposite point. It is guaranteed that the given graph is acyclic. Output Print a single integer — the size of the maximum antichain of graph _G_. Note Consider the first test sample. The graph's _G_ edges are: 0u2009→u20091, 1u2009→u20092, 0u2009→u20092. We can choose the set of vertexes [0] as the maximum antichain. We cannot choose an antichain of larger size.
2,200
false
true
false
true
false
false
false
false
false
false
8,419
776A
Our beloved detective, Sherlock is currently trying to catch a serial killer who kills a person each day. Using his powers of deduction, he came to know that the killer has a strategy for selecting his next victim. The killer starts with two potential victims on his first day, selects one of these two, kills selected victim and replaces him with a new person. He repeats this procedure each day. This way, each day he has two potential victims to choose from. Sherlock knows the initial two potential victims. Also, he knows the murder that happened on a particular day and the new person who replaced this victim. You need to help him get all the pairs of potential victims at each day so that Sherlock can observe some pattern. Input First line of input contains two names (length of each of them doesn't exceed 10), the two initials potential victims. Next line contains integer _n_ (1u2009≤u2009_n_u2009≤u20091000), the number of days. Next _n_ lines contains two names (length of each of them doesn't exceed 10), first being the person murdered on this day and the second being the one who replaced that person. The input format is consistent, that is, a person murdered is guaranteed to be from the two potential victims at that time. Also, all the names are guaranteed to be distinct and consists of lowercase English letters. Output Output _n_u2009+u20091 lines, the _i_-th line should contain the two persons from which the killer selects for the _i_-th murder. The (_n_u2009+u20091)-th line should contain the two persons from which the next victim is selected. In each line, the two names can be printed in any order. Examples Input ross rachel 4 ross joey rachel phoebe phoebe monica monica chandler Output ross rachel joey rachel joey phoebe joey monica joey chandler Input icm codeforces 1 codeforces technex Output icm codeforces icm technex Note In first example, the killer starts with ross and rachel. After day 1, ross is killed and joey appears. After day 2, rachel is killed and phoebe appears. After day 3, phoebe is killed and monica appears. After day 4, monica is killed and chandler appears.
900
false
false
true
false
false
false
true
false
false
false
6,668
1701C
There are $$$n$$$ workers and $$$m$$$ tasks. The workers are numbered from $$$1$$$ to $$$n$$$. Each task $$$i$$$ has a value $$$a_i$$$xa0— the index of worker who is proficient in this task. Every task should have a worker assigned to it. If a worker is proficient in the task, they complete it in $$$1$$$ hour. Otherwise, it takes them $$$2$$$ hours. The workers work in parallel, independently of each other. Each worker can only work on one task at once. Assign the workers to all tasks in such a way that the tasks are completed as early as possible. The work starts at time $$$0$$$. What's the minimum time all tasks can be completed by? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le m le 2 cdot 10^5$$$)xa0— the number of workers and the number of tasks. The second line contains $$$m$$$ integers $$$a_1, a_2, dots, a_m$$$ ($$$1 le a_i le n$$$)xa0— the index of the worker proficient in the $$$i$$$-th task. The sum of $$$m$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print a single integerxa0— the minimum time all tasks can be completed by. Example Input 4 2 4 1 2 1 2 2 4 1 1 1 1 5 5 5 1 3 2 4 1 1 1 Note In the first testcase, the first worker works on tasks $$$1$$$ and $$$3$$$, and the second worker works on tasks $$$2$$$ and $$$4$$$. Since they both are proficient in the corresponding tasks, they take $$$1$$$ hour on each. Both of them complete $$$2$$$ tasks in $$$2$$$ hours. Thus, all tasks are completed by $$$2$$$ hours. In the second testcase, it's optimal to assign the first worker to tasks $$$1, 2$$$ and $$$3$$$ and the second worker to task $$$4$$$. The first worker spends $$$3$$$ hours, the second worker spends $$$2$$$ hours (since they are not proficient in the taken task). In the third example, each worker can be assigned to the task they are proficient at. Thus, each of them complete their task in $$$1$$$ hour.
1,400
false
true
true
false
false
false
false
true
false
false
2,074
572B
In this task you need to process a set of stock exchange orders and use them to create order book. An order is an instruction of some participant to buy or sell stocks on stock exchange. The order number _i_ has price _p__i_, direction _d__i_ — buy or sell, and integer _q__i_. This means that the participant is ready to buy or sell _q__i_ stocks at price _p__i_ for one stock. A value _q__i_ is also known as a volume of an order. All orders with the same price _p_ and direction _d_ are merged into one aggregated order with price _p_ and direction _d_. The volume of such order is a sum of volumes of the initial orders. An order book is a list of aggregated orders, the first part of which contains sell orders sorted by price in descending order, the second contains buy orders also sorted by price in descending order. An order book of depth _s_ contains _s_ best aggregated orders for each direction. A buy order is better if it has higher price and a sell order is better if it has lower price. If there are less than _s_ aggregated orders for some direction then all of them will be in the final order book. You are given _n_ stock exhange orders. Your task is to print order book of depth _s_ for these orders. Input The input starts with two positive integers _n_ and _s_ (1u2009≤u2009_n_u2009≤u20091000,u20091u2009≤u2009_s_u2009≤u200950), the number of orders and the book depth. Next _n_ lines contains a letter _d__i_ (either 'B' or 'S'), an integer _p__i_ (0u2009≤u2009_p__i_u2009≤u2009105) and an integer _q__i_ (1u2009≤u2009_q__i_u2009≤u2009104) — direction, price and volume respectively. The letter 'B' means buy, 'S' means sell. The price of any sell order is higher than the price of any buy order. Output Print no more than 2_s_ lines with aggregated orders from order book of depth _s_. The output format for orders should be the same as in input. Examples Input 6 2 B 10 3 S 50 2 S 40 1 S 50 6 B 20 4 B 25 10 Output S 50 8 S 40 1 B 25 10 B 20 4 Note Denote (x, y) an order with price _x_ and volume _y_. There are 3 aggregated buy orders (10, 3), (20, 4), (25, 10) and two sell orders (50, 8), (40, 1) in the sample. You need to print no more than two best orders for each direction, so you shouldn't print the order (10 3) having the worst price among buy orders.
1,300
false
true
true
false
true
false
false
false
true
false
7,560
645G
After a drawn-out mooclear arms race, Farmer John and the Mischievous Mess Makers have finally agreed to establish peace. They plan to divide the territory of Bovinia with a line passing through at least two of the _n_ outposts scattered throughout the land. These outposts, remnants of the conflict, are located at the points (_x_1,u2009_y_1),u2009(_x_2,u2009_y_2),u2009...,u2009(_x__n_,u2009_y__n_). In order to find the optimal dividing line, Farmer John and Elsie have plotted a map of Bovinia on the coordinate plane. Farmer John's farm and the Mischievous Mess Makers' base are located at the points _P_u2009=u2009(_a_,u20090) and _Q_u2009=u2009(u2009-u2009_a_,u20090), respectively. Because they seek a lasting peace, Farmer John and Elsie would like to minimize the maximum difference between the distances from any point on the line to _P_ and _Q_. Formally, define the difference of a line relative to two points _P_ and _Q_ as the smallest real number _d_ so that for all points _X_ on line , _PX_u2009-u2009_QX_u2009≤u2009_d_. (It is guaranteed that _d_ exists and is unique.) They wish to find the line passing through two distinct outposts (_x__i_,u2009_y__i_) and (_x__j_,u2009_y__j_) such that the difference of relative to _P_ and _Q_ is minimized. Input The first line of the input contains two integers _n_ and _a_ (2u2009≤u2009_n_u2009≤u2009100u2009000, 1u2009≤u2009_a_u2009≤u200910u2009000)xa0— the number of outposts and the coordinates of the farm and the base, respectively. The following _n_ lines describe the locations of the outposts as pairs of integers (_x__i_,u2009_y__i_) (_x__i_,u2009_y__i_u2009≤u200910u2009000). These points are distinct from each other as well as from _P_ and _Q_. Output Print a single real number—the difference of the optimal dividing line. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct, if . Note In the first sample case, the only possible line is _y_u2009=u2009_x_u2009-u20091. It can be shown that the point _X_ which maximizes _PX_u2009-u2009_QX_ is (13,u200912), with , which is . In the second sample case, if we pick the points (0,u20091) and (0,u2009u2009-u20093), we get as _x_u2009=u20090. Because _PX_u2009=u2009_QX_ on this line, the minimum possible difference is 0.
3,200
false
false
false
false
false
false
false
true
false
false
7,228
773D
In the country of Never, there are _n_ cities and a well-developed road system. There is exactly one bidirectional road between every pair of cities, thus, there are as many as roads! No two roads intersect, and no road passes through intermediate cities. The art of building tunnels and bridges has been mastered by Neverians. An independent committee has evaluated each road of Never with a positive integer called the perishability of the road. The lower the road's perishability is, the more pleasant it is to drive through this road. It's the year of transport in Never. It has been decided to build a museum of transport in one of the cities, and to set a single signpost directing to some city (not necessarily the one with the museum) in each of the other cities. The signposts must satisfy the following important condition: if any Neverian living in a city without the museum starts travelling from that city following the directions of the signposts, then this person will eventually arrive in the city with the museum. Neverians are incredibly positive-minded. If a Neverian travels by a route consisting of several roads, he considers the perishability of the route to be equal to the smallest perishability of all the roads in this route. The government of Never has not yet decided where to build the museum, so they consider all _n_ possible options. The most important is the sum of perishabilities of the routes to the museum city from all the other cities of Never, if the travelers strictly follow the directions of the signposts. The government of Never cares about their citizens, so they want to set the signposts in a way which minimizes this sum. Help them determine the minimum possible sum for all _n_ possible options of the city where the museum can be built. Input The first line contains a single integer _n_ (2u2009≤u2009_n_u2009≤u20092000)xa0— the number of cities in Never. The following _n_u2009-u20091 lines contain the description of the road network. The _i_-th of these lines contains _n_u2009-u2009_i_ integers. The _j_-th integer in the _i_-th line denotes the perishability of the road between cities _i_ and _i_u2009+u2009_j_. All road perishabilities are between 1 and 109, inclusive. Output For each city in order from 1 to _n_, output the minimum possible sum of perishabilities of the routes to this city from all the other cities of Never if the signposts are set in a way which minimizes this sum. Note The first example is explained by the picture below. From left to right, there is the initial road network and the optimal directions of the signposts in case the museum is built in city 1, 2 and 3, respectively. The museum city is represented by a blue circle, the directions of the signposts are represented by green arrows. For instance, if the museum is built in city 3, then the signpost in city 1 must be directed to city 3, while the signpost in city 2 must be directed to city 1. Then the route from city 1 to city 3 will have perishability 2, while the route from city 2 to city 3 will have perishability 1. The sum of perishabilities of these routes is 3.
2,700
false
false
false
true
false
false
false
false
false
true
6,684
1062F
There are $$$n$$$ cities in the kingdom $$$X$$$, numbered from $$$1$$$ through $$$n$$$. People travel between cities by some one-way roads. As a passenger, JATC finds it weird that from any city $$$u$$$, he can't start a trip in it and then return back to it using the roads of the kingdom. That is, the kingdom can be viewed as an acyclic graph. Being annoyed by the traveling system, JATC decides to meet the king and ask him to do something. In response, the king says that he will upgrade some cities to make it easier to travel. Because of the budget, the king will only upgrade those cities that are important or semi-important. A city $$$u$$$ is called important if for every city $$$v eq u$$$, there is either a path from $$$u$$$ to $$$v$$$ or a path from $$$v$$$ to $$$u$$$. A city $$$u$$$ is called semi-important if it is not important and we can destroy exactly one city $$$v eq u$$$ so that $$$u$$$ becomes important. The king will start to act as soon as he finds out all those cities. Please help him to speed up the process. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 300,000$$$, $$$1 le m le 300,000$$$)xa0— the number of cities and the number of one-way roads. Next $$$m$$$ lines describe the road system of the kingdom. Each of them contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i eq v_i$$$), denoting one-way road from $$$u_i$$$ to $$$v_i$$$. It is guaranteed, that the kingdoms' roads make an acyclic graph, which doesn't contain multiple edges and self-loops. Output Print a single integerxa0— the number of cities that the king has to upgrade. Examples Input 7 7 1 2 2 3 3 4 4 7 2 5 5 4 6 4 Input 6 7 1 2 2 3 3 4 1 5 5 3 2 6 6 4 Note In the first example: Starting at the city $$$1$$$ we can reach all the other cities, except for the city $$$6$$$. Also, from the city $$$6$$$ we cannot reach the city $$$1$$$. Therefore, if we destroy the city $$$6$$$ then the city $$$1$$$ will become important. So $$$1$$$ is a semi-important city. For city $$$2$$$, the set of cities that cannot reach $$$2$$$ and cannot be reached by $$$2$$$ is $$${6}$$$. Therefore, destroying city $$$6$$$ will make the city $$$2$$$ important. So city $$$2$$$ is also semi-important. For city $$$3$$$, the set is $$${5, 6}$$$. As you can see, destroying either city $$$5$$$ or $$$6$$$ will not make the city $$$3$$$ important. Therefore, it is neither important nor semi-important. For city $$$4$$$, the set is empty. So $$$4$$$ is an important city. The set for city $$$5$$$ is $$${3, 6}$$$ and the set for city $$$6$$$ is $$${3, 5}$$$. Similarly to city $$$3$$$, both of them are not important nor semi-important. The city $$$7$$$ is important since we can reach it from all other cities. So we have two important cities ($$$4$$$ and $$$7$$$) and two semi-important cities ($$$1$$$ and $$$2$$$). In the second example, the important cities are $$$1$$$ and $$$4$$$. The semi-important cities are $$$2$$$ and $$$3$$$.
2,900
false
false
false
false
false
false
false
false
false
true
5,397
47E
Bertown is under siege! The attackers have blocked all the ways out and their cannon is bombarding the city. Fortunately, Berland intelligence managed to intercept the enemies' shooting plan. Let's introduce the Cartesian system of coordinates, the origin of which coincides with the cannon's position, the _Ox_ axis is directed rightwards in the city's direction, the _Oy_ axis is directed upwards (to the sky). The cannon will make _n_ more shots. The cannon balls' initial speeds are the same in all the shots and are equal to _V_, so that every shot is characterized by only one number _alpha__i_ which represents the angle at which the cannon fires. Due to the cannon's technical peculiarities this angle does not exceed 45 angles (πu2009/u20094). We disregard the cannon sizes and consider the firing made from the point (0,u20090). The balls fly according to the known physical laws of a body thrown towards the horizon at an angle: _v__x_(_t_)u2009=u2009_V_·_cos_(_alpha_) _v__y_(_t_)u2009=u2009_V_·_sin_(_alpha_)xa0u2009–u2009xa0_g_·_t_ _x_(_t_)u2009=u2009_V_·_cos_(_alpha_)·_t_ _y_(_t_)u2009=u2009_V_·_sin_(_alpha_)·_t_xa0u2009–u2009xa0_g_·_t_2u2009/u20092 Think of the acceleration of gravity _g_ as equal to 9.8. Bertown defends _m_ walls. The _i_-th wall is represented as a vertical segment (_x__i_,u20090)u2009-u2009(_x__i_,u2009_y__i_). When a ball hits a wall, it gets stuck in it and doesn't fly on. If a ball doesn't hit any wall it falls on the ground (_y_u2009=u20090) and stops. If the ball exactly hits the point (_x__i_,u2009_y__i_), it is considered stuck. Your task is to find for each ball the coordinates of the point where it will be located in the end. Input The first line contains integers _n_ and _V_ (1u2009≤u2009_n_u2009≤u2009104,u20091u2009≤u2009_V_u2009≤u20091000) which represent the number of shots and the initial speed of every ball. The second line contains _n_ space-separated real numbers _alpha__i_ (0u2009<u2009_alpha__i_u2009<u2009πu2009/u20094) which represent the angles in radians at which the cannon will fire. The third line contains integer _m_ (1u2009≤u2009_m_u2009≤u2009105) which represents the number of walls. Then follow _m_ lines, each containing two real numbers _x__i_ and _y__i_ (1u2009≤u2009_x__i_u2009≤u20091000,u20090u2009≤u2009_y__i_u2009≤u20091000) which represent the wall’s coordinates. All the real numbers have no more than 4 decimal digits. The walls may partially overlap or even coincide.
2,200
false
false
false
false
true
false
false
false
true
false
9,738
1141D
There are $$$n$$$ left boots and $$$n$$$ right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings $$$l$$$ and $$$r$$$, both of length $$$n$$$. The character $$$l_i$$$ stands for the color of the $$$i$$$-th left boot and the character $$$r_i$$$ stands for the color of the $$$i$$$-th right boot. A lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color. For example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are not compatible: ('f', 'g') and ('a', 'z'). Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible. Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair. Input The first line contains $$$n$$$ ($$$1 le n le 150000$$$), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots). The second line contains the string $$$l$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th left boot. The third line contains the string $$$r$$$ of length $$$n$$$. It contains only lowercase Latin letters or question marks. The $$$i$$$-th character stands for the color of the $$$i$$$-th right boot. Output Print $$$k$$$ — the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors. The following $$$k$$$ lines should contain pairs $$$a_j, b_j$$$ ($$$1 le a_j, b_j le n$$$). The $$$j$$$-th of these lines should contain the index $$$a_j$$$ of the left boot in the $$$j$$$-th pair and index $$$b_j$$$ of the right boot in the $$$j$$$-th pair. All the numbers $$$a_j$$$ should be distinct (unique), all the numbers $$$b_j$$$ should be distinct (unique). If there are many optimal answers, print any of them.
1,500
false
true
true
false
false
false
false
false
false
false
5,018
1842E
There are $$$n$$$ pairwise-distinct points and a line $$$x+y=k$$$ on a two-dimensional plane. The $$$i$$$-th point is at $$$(x_i,y_i)$$$. All points have non-negative coordinates and are strictly below the line. Alternatively, $$$0 leq x_i,y_i, x_i+y_i < k$$$. Tenzing wants to erase all the points. He can perform the following two operations: 1. Draw triangle: Tenzing will choose two non-negative integers $$$a$$$, $$$b$$$ that satisfy $$$a+b<k$$$, then all points inside the triangle formed by lines $$$x=a$$$, $$$y=b$$$ and $$$x+y=k$$$ will be erased. It can be shown that this triangle is an isosceles right triangle. Let the side lengths of the triangle be $$$l$$$, $$$l$$$ and $$$sqrt 2 l$$$ respectively. Then, the cost of this operation is $$$l cdot A$$$.The blue area of the following picture describes the triangle with $$$a=1,b=1$$$ with cost $$$=1cdot A$$$. 2. Erase a specific point: Tenzing will choose an integer $$$i$$$ that satisfies $$$1 leq i leq n$$$ and erase the point $$$i$$$. The cost of this operation is $$$c_i$$$. Help Tenzing find the minimum cost to erase all of the points. Input The first line of the input contains three integers $$$n$$$, $$$k$$$ and $$$A$$$ ($$$1leq n,kleq 2cdot 10^5$$$, $$$1leq Aleq 10^4$$$)xa0— the number of points, the coefficient describing the hypotenuse of the triangle and the coefficient describing the cost of drawing a triangle. The following $$$n$$$ lines of the input the $$$i$$$-th line contains three integers $$$x_i,y_i,c_i$$$ ($$$0leq x_i,y_i,x_i+y_i< k$$$, $$$1leq c_ileq 10^4$$$)xa0— the coordinate of the $$$i$$$-th points and the cost of erasing it using the second operation. It is guaranteed that the coordinates are pairwise distinct. Output Output a single integerxa0—the minimum cost needed to erase all of the points. Examples Input 4 6 1 1 2 1 2 1 1 1 1 1 3 2 6 Input 6 7 1 4 2 1 3 3 1 5 1 4 3 2 5 4 1 1 0 6 4 Input 10 4 100 0 0 1 0 1 1 0 2 50 0 3 200 1 0 1 1 1 1 1 2 1 2 0 200 2 1 200 3 0 200 Note The picture of the first example: Tenzing do the following operations: 1. draw a triangle with $$$a=3,b=2$$$, the cost $$$=1cdot A=1$$$. 2. erase the first point, the cost $$$=1$$$. 3. erase the second point, the cost $$$=1$$$. 4. erase the third point, the cost $$$=1$$$. The picture of the second example:
2,300
true
true
false
true
true
false
false
false
false
false
1,235
1833A
Vlad decided to compose a melody on his guitar. Let's represent the melody as a sequence of notes corresponding to the characters 'a', 'b', 'c', 'd', 'e', 'f', and 'g'. However, Vlad is not very experienced in playing the guitar and can only record exactly two notes at a time. Vlad wants to obtain the melody $$$s$$$, and to do this, he can merge the recorded melodies together. In this case, the last sound of the first melody must match the first sound of the second melody. For example, if Vlad recorded the melodies "ab" and "ba", he can merge them together and obtain the melody "aba", and then merge the result with "ab" to get "abab". Help Vlad determine the minimum number of melodies consisting of two notes that he needs to record in order to obtain the melody $$$s$$$. Input The first line of input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Following that are the descriptions of the test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 le n le 50$$$)xa0— the length of the melody $$$s$$$. The second line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of characters 'a', 'b', 'c', 'd', 'e', 'f', 'g'. Output Output $$$t$$$ integers, each representing the answer for the corresponding test case. As the answer output minimum number of melodies consisting of two notes that Vlad needs to record. Example Input 5 4 abab 7 abacaba 6 aaaaaa 7 abcdefg 5 babdd Note In the first sample, you need to record the melodies "ab" and "ba", as described in the problem statement. In the second sample, you need to record the melodies "ab", "ba", "ac", and "ca". In the third sample, the only necessary melody is "aa".
800
false
false
true
false
false
false
false
false
false
false
1,291
140B
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has _n_ friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to _n_ in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the _i_-th friend sent to Alexander a card number _i_. Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules: 1. He will never send to a firend a card that this friend has sent to him. 2. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most. Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times. Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to _n_. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card. Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible. Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules. Input The first line contains an integer _n_ (2u2009≤u2009_n_u2009≤u2009300) — the number of Alexander's friends, equal to the number of cards. Next _n_ lines contain his friends' preference lists. Each list consists of _n_ different integers from 1 to _n_. The last line contains Alexander's preference list in the same format. Output Print _n_ space-separated numbers: the _i_-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the _i_-th friend. If there are several solutions, print any of them. Note In the sample, the algorithm of actions Alexander and his friends perform is as follows: 1. Alexander receives card 1 from the first friend. 2. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3. 3. Alexander receives card 2 from the second friend, now he has two cards — 1 and 2. 4. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend. 5. Alexander receives card 3 from the third friend. 6. Alexander receives card 4 from the fourth friend. 7. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend. Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct).
1,800
false
true
true
false
false
false
true
false
false
false
9,317
1809F
There are $$$n$$$ cities in Berland, arranged in a circle and numbered from $$$1$$$ to $$$n$$$ in clockwise order. You want to travel all over Berland, starting in some city, visiting all the other cities and returning to the starting city. Unfortunately, you can only drive along the Berland Ring Highway, which connects all $$$n$$$ cities. The road was designed by a very titled and respectable minister, so it is one-directionalxa0— it can only be traversed clockwise, only from the city $$$i$$$ to the city $$$(i bmod n) + 1$$$ (i.e. from $$$1$$$ to $$$2$$$, from $$$2$$$ in $$$3$$$, ..., from $$$n$$$ to $$$1$$$). The fuel tank of your car holds up to $$$k$$$ liters of fuel. To drive from the $$$i$$$-th city to the next one, $$$a_i$$$ liters of fuel are needed (and are consumed in the process). Every city has a fuel station; a liter of fuel in the $$$i$$$-th city costs $$$b_i$$$ burles. Refueling between cities is not allowed; if fuel has run out between cities, then your journey is considered incomplete. For each city, calculate the minimum cost of the journey if you start and finish it in that city. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 le n le 2 cdot 10^5$$$; $$$1 le k le 10^9$$$)xa0— the number of cities and the volume of fuel tank, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le k$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le 2$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print $$$n$$$ integers, where the $$$i$$$-th of them is equal to the minimum cost of the journey if you start and finish in the $$$i$$$-th city.
2,500
false
true
true
false
true
false
false
true
false
true
1,415
778B
Bob recently read about bitwise operations used in computers: AND, OR and XOR. He have studied their properties and invented a new game. Initially, Bob chooses integer _m_, bit depth of the game, which means that all numbers in the game will consist of _m_ bits. Then he asks Peter to choose some _m_-bit number. After that, Bob computes the values of _n_ variables. Each variable is assigned either a constant _m_-bit number or result of bitwise operation. Operands of the operation may be either variables defined before, or the number, chosen by Peter. After that, Peter's score equals to the sum of all variable values. Bob wants to know, what number Peter needs to choose to get the minimum possible score, and what number he needs to choose to get the maximum possible score. In both cases, if there are several ways to get the same score, find the minimum number, which he can choose. Input The first line contains two integers _n_ and _m_, the number of variables and bit depth, respectively (1u2009≤u2009_n_u2009≤u20095000; 1u2009≤u2009_m_u2009≤u20091000). The following _n_ lines contain descriptions of the variables. Each line describes exactly one variable. Description has the following format: name of a new variable, space, sign ":=", space, followed by one of: 1. Binary number of exactly _m_ bits. 2. The first operand, space, bitwise operation ("AND", "OR" or "XOR"), space, the second operand. Each operand is either the name of variable defined before or symbol '?', indicating the number chosen by Peter. Variable names are strings consisting of lowercase Latin letters with length at most 10. All variable names are different. Output In the first line output the minimum number that should be chosen by Peter, to make the sum of all variable values minimum possible, in the second line output the minimum number that should be chosen by Peter, to make the sum of all variable values maximum possible. Both numbers should be printed as _m_-bit binary numbers. Examples Input 3 3 a := 101 b := 011 c := ? XOR b Input 5 1 a := 1 bb := 0 cx := ? OR a d := ? XOR ? e := d AND bb Note In the first sample if Peter chooses a number 0112, then _a_u2009=u20091012,u2009_b_u2009=u20090112,u2009_c_u2009=u20090002, the sum of their values is 8. If he chooses the number 1002, then _a_u2009=u20091012,u2009_b_u2009=u20090112,u2009_c_u2009=u20091112, the sum of their values is 15. For the second test, the minimum and maximum sum of variables _a_, _bb_, _cx_, _d_ and _e_ is 2, and this sum doesn't depend on the number chosen by Peter, so the minimum Peter can choose is 0.
1,800
false
false
true
false
false
false
true
false
false
false
6,655
766A
While Mahmoud and Ehab were practicing for IOI, they found a problem which name was Longest common subsequence. They solved it, and then Ehab challenged Mahmoud with another problem. Given two strings _a_ and _b_, find the length of their longest uncommon subsequence, which is the longest string that is a subsequence of one of them and not a subsequence of the other. A subsequence of some string is a sequence of characters that appears in the same order in the string, The appearances don't have to be consecutive, for example, strings "ac", "bc", "abc" and "a" are subsequences of string "abc" while strings "abbc" and "acb" are not. The empty string is a subsequence of any string. Any string is a subsequence of itself. Input The first line contains string _a_, and the second linexa0— string _b_. Both of these strings are non-empty and consist of lowercase letters of English alphabet. The length of each string is not bigger than 105 characters. Output If there's no uncommon subsequence, print "-1". Otherwise print the length of the longest uncommon subsequence of _a_ and _b_. Note In the first example: you can choose "defgh" from string _b_ as it is the longest subsequence of string _b_ that doesn't appear as a subsequence of string _a_.
1,000
false
false
false
false
false
true
false
false
false
false
6,723
1103D
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are $$$n$$$ judges now in the layer and you are trying to pass the entrance exam. You have a number $$$k$$$ — your skill in Cardbluff. Each judge has a number $$$a_i$$$ — an indicator of uncertainty about your entrance to the professional layer and a number $$$e_i$$$ — an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of $$$i$$$-th judge by any natural divisor of $$$a_i$$$ which is at most $$$k$$$. If GCD of all indicators is equal to $$$1$$$, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with $$$x$$$ judges with total experience $$$y$$$ you will spend $$$x cdot y$$$ seconds. Print minimal time to enter to the professional layer or $$$-1$$$ if it's impossible. Input There are two numbers in the first line $$$n$$$ and $$$k$$$ ($$$1 leq n leq 10^6$$$, $$$1 leq k leq 10^{12}$$$) — the number of judges and your skill in Cardbluff. The second line contains $$$n$$$ integers, where $$$i$$$-th number $$$a_i$$$ ($$$1 leq a_i leq 10^{12}$$$) — the uncertainty of $$$i$$$-th judge The third line contains $$$n$$$ integers in the same format ($$$1 leq e_i leq 10^9$$$), $$$e_i$$$ — the experience of $$$i$$$-th judge. Output Print the single integer — minimal number of seconds to pass exam, or $$$-1$$$ if it's impossible Examples Input 3 6 30 30 30 100 4 5
3,100
false
false
false
true
false
false
false
false
false
false
5,184
1789B
Serval has a string $$$s$$$ that only consists of 0 and 1 of length $$$n$$$. The $$$i$$$-th character of $$$s$$$ is denoted as $$$s_i$$$, where $$$1leq ileq n$$$. Serval can perform the following operation called Inversion Magic on the string $$$s$$$: Choose an segment $$$[l, r]$$$ ($$$1leq lleq rleq n$$$). For $$$lleq ileq r$$$, change $$$s_i$$$ into 1 if $$$s_i$$$ is 0, and change $$$s_i$$$ into 0 if $$$s_i$$$ is 1. For example, let $$$s$$$ be 010100 and the segment $$$[2,5]$$$ is chosen. The string $$$s$$$ will be 001010 after performing the Inversion Magic. Serval wants to make $$$s$$$ a palindrome after performing Inversion Magic exactly once. Help him to determine whether it is possible. A string is a palindrome iff it reads the same backwards as forwards. For example, 010010 is a palindrome but 10111 is not. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1leq tleq 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2leq nleq 10^5$$$) — the length of string $$$s$$$. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$. Only characters 0 and 1 can appear in $$$s$$$. It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, print Yes if $$$s$$$ can be a palindrome after performing Inversion Magic exactly once, and print No if not. 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 3 4 1001 5 10010 7 0111011 Note In the first test case, Serval can perform Inversion Magic on the segment $$$[1,4]$$$. The string $$$s$$$ will be 0110 after the magic. In the second test case, Serval can perform Inversion Magic on the segment $$$[1,3]$$$. The string $$$s$$$ will be 01110 after the magic. In the third test case, Serval can't make $$$s$$$ a palindrome by performing Inversion Magic exactly once.
800
false
false
true
false
false
false
true
false
false
false
1,547
958F2
There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of the lightsabers of the Jedi who will go on that mission. Heidi has _n_ Jedi Knights standing in front of her, each one with a lightsaber of one of _m_ possible colors. She knows that for the mission to be the most effective, she needs to select some contiguous interval of knights such that there are exactly _k_1 knights with lightsabers of the first color, _k_2 knights with lightsabers of the second color, ..., _k__m_ knights with lightsabers of the _m_-th color. However, since the last time, she has learned that it is not always possible to select such an interval. Therefore, she decided to ask some Jedi Knights to go on an indefinite unpaid vacation leave near certain pits on Tatooine, if you know what I mean. Help Heidi decide what is the minimum number of Jedi Knights that need to be let go before she is able to select the desired interval from the subsequence of remaining knights. Input The first line of the input contains _n_ (1u2009≤u2009_n_u2009≤u20092·105) and _m_ (1u2009≤u2009_m_u2009≤u2009_n_). The second line contains _n_ integers in the range {1,u20092,u2009...,u2009_m_} representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains _m_ integers _k_1,u2009_k_2,u2009...,u2009_k__m_ (with ) – the desired counts of Jedi Knights with lightsabers of each color from 1 to _m_. Output Output one number: the minimum number of Jedi Knights that need to be removed from the sequence so that, in what remains, there is an interval with the prescribed counts of lightsaber colors. If this is not possible, output u2009-u20091. Example Input 8 3 3 3 1 2 2 1 1 3 3 1 1
1,800
false
false
false
false
false
false
false
true
false
false
5,879
187E
The story was not finished as PMP thought. God offered him one more chance to reincarnate and come back to life. But before he can come back, God told him that PMP should ask _n_ great men including prominent programmers about their life experiences. The men are standing on a straight line. They are numbered 1 through _n_ from left to right. The coordinate of the _i_-th man is _x__i_ (_x__i_u2009<u2009_x__i_u2009+u20091,u2009_i_u2009<u2009_n_). PMP should visit all these people one by one in arbitrary order. Each men should be visited exactly once. At the beginning of his tour, he starts at location of _s_-th man and asks him about his experiences. Each time PMP wants to change his location, he should give a ticket to an angel and the angel carries him to his destination. Angels take PMP from one location, fly to his destination and put him down there. Nobody else is visited in this movement. Moving from _i_-th man to _j_-th man, takes _x__i_u2009-u2009_x__j_ time. PMP can get back to life as soon as he visits all men. There are two types of angels: Some angels are going to the right and they only accept right tickets. Others are going the left and they only accept left tickets. There are an unlimited number of angels of each type. PMP has _l_ left tickets and _n_u2009-u20091u2009-u2009_l_ right tickets. PMP wants to get back to life as soon as possible to be able to compete in this year's final instead of the final he missed last year. He wants to know the quickest way to visit all the men exactly once. He also needs to know the exact sequence moves he should make. Input The first line of input contains three space-separated integers _n_,u2009_l_,u2009_s_ (2u2009≤u2009_n_u2009≤u2009105,u20090u2009≤u2009_l_u2009<u2009_n_,u20091u2009≤u2009_s_u2009≤u2009_n_) — the number of people to visit, the number left tickets PMP got, and initial location of PMP. Next line contains _n_ space-separated integers. The _i_-th integer in this line is _x__i_ (0u2009=u2009_x_1u2009<u2009_x_2u2009<u2009...u2009<u2009_x__n_u2009≤u2009109) — the location of _i_-th man. Output If PMP cannot visit all men with the tickets he got print -1 in the only line of output. Otherwise, in the first line you should print the minimum time PMP can visit all men. In the second line you should print _n_u2009-u20091 integers that are the numbers of the men that PMP should visit in order in one optimal solution. If there are multiple answers, output any of them. 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 7 3 2 0 100 200 201 301 303 305 Note Let us remind here, a great contestant of all times, who left us about a year ago. May Renat Mullakhanov rest in peace.
2,900
false
true
false
false
true
false
false
false
false
false
9,093
527D
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph _G_. It is required to find a subset of vertices _C_ of the maximum size such that any two of them are connected by an edge in graph _G_. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph. Consider _n_ distinct points on a line. Let the _i_-th point have the coordinate _x__i_ and weight _w__i_. Let's form graph _G_, whose vertices are these points and edges connect exactly the pairs of points (_i_,u2009_j_), such that the distance between them is not less than the sum of their weights, or more formally: _x__i_u2009-u2009_x__j_u2009≥u2009_w__i_u2009+u2009_w__j_. Find the size of the maximum clique in such graph. Input The first line contains the integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000) — the number of points. Each of the next _n_ lines contains two numbers _x__i_, _w__i_ (0u2009≤u2009_x__i_u2009≤u2009109,u20091u2009≤u2009_w__i_u2009≤u2009109) — the coordinate and the weight of a point. All _x__i_ are different. Output Print a single number — the number of vertexes in the maximum clique of the given graph. Note If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars! The picture for the sample test.
1,800
false
true
true
true
true
false
false
false
true
false
7,721
1068B
Problem - 1068B - Codeforces =============== xa0 ]( "A") $$$. Output Print one numberxa0— answer for the problem. Examples Input 1 Output 1 Input 2 Output 2 Note In the first example $$$
1,200
true
false
false
false
false
false
false
false
false
false
5,369
371B
Two little greedy bears have found two pieces of cheese in the forest of weight _a_ and _b_ grams, correspondingly. The bears are so greedy that they are ready to fight for the larger piece. That's where the fox comes in and starts the dialog: "Little bears, wait a little, I want to make your pieces equal" "Come off it fox, how are you going to do that?", the curious bears asked. "It's easy", said the fox. "If the mass of a certain piece is divisible by two, then I can eat exactly a half of the piece. If the mass of a certain piece is divisible by three, then I can eat exactly two-thirds, and if the mass is divisible by five, then I can eat four-fifths. I'll eat a little here and there and make the pieces equal". The little bears realize that the fox's proposal contains a catch. But at the same time they realize that they can not make the two pieces equal themselves. So they agreed to her proposal, but on one condition: the fox should make the pieces equal as quickly as possible. Find the minimum number of operations the fox needs to make pieces equal. Input The first line contains two space-separated integers _a_ and _b_ (1u2009≤u2009_a_,u2009_b_u2009≤u2009109). Output If the fox is lying to the little bears and it is impossible to make the pieces equal, print -1. Otherwise, print the required minimum number of operations. If the pieces of the cheese are initially equal, the required number is 0.
1,300
true
false
false
false
false
false
false
false
false
false
8,347
1761C
You are given a binary matrix $$$b$$$ (all elements of the matrix are $$$0$$$ or $$$1$$$) of $$$n$$$ rows and $$$n$$$ columns. You need to construct a $$$n$$$ sets $$$A_1, A_2, ldots, A_n$$$, for which the following conditions are satisfied: Each set is nonempty and consists of distinct integers between $$$1$$$ and $$$n$$$ inclusive. All sets are distinct. For all pairs $$$(i,j)$$$ satisfying $$$1leq i, jleq n$$$, $$$b_{i,j}=1$$$ if and only if $$$A_isubsetneq A_j$$$. In other words, $$$b_{i, j}$$$ is $$$1$$$ if $$$A_i$$$ is a proper subset of $$$A_j$$$ and $$$0$$$ otherwise. Set $$$X$$$ is a proper subset of set $$$Y$$$, if $$$X$$$ is a nonempty subset of $$$Y$$$, and $$$X eq Y$$$. It's guaranteed that for all test cases in this problem, such $$$n$$$ sets exist. Note that it doesn't mean that such $$$n$$$ sets exist for all possible inputs. If there are multiple solutions, you can output any of them. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 1000$$$) — the number of test cases. The description of test cases follows. The first line contains a single integer $$$n$$$ ($$$1le nle 100$$$). The following $$$n$$$ lines contain a binary matrix $$$b$$$, the $$$j$$$-th character of $$$i$$$-th line denotes $$$b_{i,j}$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$. It's guaranteed that for all test cases in this problem, such $$$n$$$ sets exist. Output For each test case, output $$$n$$$ lines. For the $$$i$$$-th line, first output $$$s_i$$$ $$$(1 le s_i le n)$$$ xa0— the size of the set $$$A_i$$$. Then, output $$$s_i$$$ distinct integers from $$$1$$$ to $$$n$$$ xa0— the elements of the set $$$A_i$$$. If there are multiple solutions, you can output any of them. It's guaranteed that for all test cases in this problem, such $$$n$$$ sets exist. Example Input 2 4 0001 1001 0001 0000 3 011 001 000 Output 3 1 2 3 2 1 3 2 2 4 4 1 2 3 4 1 1 2 1 2 3 1 2 3 Note In the first test case, we have $$$A_1 = {1, 2, 3}, A_2 = {1, 3}, A_3 = {2, 4}, A_4 = {1, 2, 3, 4}$$$. Sets $$$A_1, A_2, A_3$$$ are proper subsets of $$$A_4$$$, and also set $$$A_2$$$ is a proper subset of $$$A_1$$$. No other set is a proper subset of any other set. In the second test case, we have $$$A_1 = {1}, A_2 = {1, 2}, A_3 = {1, 2, 3}$$$. $$$A_1$$$ is a proper subset of $$$A_2$$$ and $$$A_3$$$, and $$$A_2$$$ is a proper subset of $$$A_3$$$.
1,400
false
true
false
false
false
true
false
false
false
true
1,746
1671E
You are given a rooted tree of $$$2^n - 1$$$ vertices. Every vertex of this tree has either $$$0$$$ children, or $$$2$$$ children. All leaves of this tree have the same distance from the root, and for every non-leaf vertex, one of its children is the left one, and the other child is the right one. Formally, you are given a perfect binary tree. The vertices of the tree are numbered in the following order: the root has index $$$1$$$; if a vertex has index $$$x$$$, then its left child has index $$$2x$$$, and its right child has index $$$2x+1$$$. Every vertex of the tree has a letter written on it, either A or B. Let's define the character on the vertex $$$x$$$ as $$$s_x$$$. Let the preorder string of some vertex $$$x$$$ be defined in the following way: if the vertex $$$x$$$ is a leaf, then the preorder string of $$$x$$$ be consisting of only one character $$$s_x$$$; otherwise, the preorder string of $$$x$$$ is $$$s_x + f(l_x) + f(r_x)$$$, where $$$+$$$ operator defines concatenation of strings, $$$f(l_x)$$$ is the preorder string of the left child of $$$x$$$, and $$$f(r_x)$$$ is the preorder string of the right child of $$$x$$$. The preorder string of the tree is the preorder string of its root. Now, for the problem itself... You have to calculate the number of different strings that can be obtained as the preorder string of the given tree, if you are allowed to perform the following operation any number of times before constructing the preorder string of the tree: choose any non-leaf vertex $$$x$$$, and swap its children (so, the left child becomes the right one, and vice versa). Input The first line contains one integer $$$n$$$ ($$$2 le n le 18$$$). The second line contains a sequence of $$$2^n-1$$$ characters $$$s_1, s_2, dots, s_{2^n-1}$$$. Each character is either A or B. The characters are not separated by spaces or anything else. Output Print one integer — the number of different strings that can be obtained as the preorder string of the given tree, if you can apply any number of operations described in the statement. Since it can be very large, print it modulo $$$998244353$$$.
2,100
false
false
false
true
false
false
false
false
true
false
2,247
1165F2
The only difference between easy and hard versions is constraints. Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions — and he won't start playing until he gets all of them. Each day (during the morning) Ivan earns exactly one burle. There are $$$n$$$ types of microtransactions in the game. Each microtransaction costs $$$2$$$ burles usually and $$$1$$$ burle if it is on sale. Ivan has to order exactly $$$k_i$$$ microtransactions of the $$$i$$$-th type (he orders microtransactions during the evening). Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for $$$1$$$ burle and otherwise he can buy it for $$$2$$$ burles. There are also $$$m$$$ special offers in the game shop. The $$$j$$$-th offer $$$(d_j, t_j)$$$ means that microtransactions of the $$$t_j$$$-th type are on sale during the $$$d_j$$$-th day. Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2 cdot 10^5$$$) — the number of types of microtransactions and the number of special offers in the game shop. The second line of the input contains $$$n$$$ integers $$$k_1, k_2, dots, k_n$$$ ($$$0 le k_i le 2 cdot 10^5$$$), where $$$k_i$$$ is the number of copies of microtransaction of the $$$i$$$-th type Ivan has to order. It is guaranteed that sum of all $$$k_i$$$ is not less than $$$1$$$ and not greater than $$$2 cdot 10^5$$$. The next $$$m$$$ lines contain special offers. The $$$j$$$-th of these lines contains the $$$j$$$-th special offer. It is given as a pair of integers $$$(d_j, t_j)$$$ ($$$1 le d_j le 2 cdot 10^5, 1 le t_j le n$$$) and means that microtransactions of the $$$t_j$$$-th type are on sale during the $$$d_j$$$-th day. Output Print one integer — the minimum day when Ivan can order all microtransactions he wants and actually start playing. Examples Input 5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3 Input 5 3 4 2 1 3 2 3 5 4 2 2 5
2,000
false
true
true
false
false
false
false
true
false
false
4,890
1183B
There are $$$n$$$ products in the shop. The price of the $$$i$$$-th product is $$$a_i$$$. The owner of the shop wants to equalize the prices of all products. However, he wants to change prices smoothly. In fact, the owner of the shop can change the price of some product $$$i$$$ in such a way that the difference between the old price of this product $$$a_i$$$ and the new price $$$b_i$$$ is at most $$$k$$$. In other words, the condition $$$a_i - b_i le k$$$ should be satisfied ($$$x$$$ is the absolute value of $$$x$$$). He can change the price for each product not more than once. Note that he can leave the old prices for some products. The new price $$$b_i$$$ of each product $$$i$$$ should be positive (i.e. $$$b_i > 0$$$ should be satisfied for all $$$i$$$ from $$$1$$$ to $$$n$$$). Your task is to find out the maximum possible equal price $$$B$$$ of all productts with the restriction that for all products the condiion $$$a_i - B le k$$$ should be satisfied (where $$$a_i$$$ is the old price of the product and $$$B$$$ is the same new price of all products) or report that it is impossible to find such price $$$B$$$. Note that the chosen price $$$B$$$ should be integer. You should answer $$$q$$$ independent queries. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 100$$$) — the number of queries. Each query is presented by two lines. The first line of the query contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 100, 1 le k le 10^8$$$) — the number of products and the value $$$k$$$. The second line of the query contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^8$$$), where $$$a_i$$$ is the price of the $$$i$$$-th product. Output Print $$$q$$$ integers, where the $$$i$$$-th integer is the answer $$$B$$$ on the $$$i$$$-th query. If it is impossible to equalize prices of all given products with restriction that for all products the condition $$$a_i - B le k$$$ should be satisfied (where $$$a_i$$$ is the old price of the product and $$$B$$$ is the new equal price of all products), print -1. Otherwise print the maximum possible equal price of all products. Example Input 4 5 1 1 1 2 3 1 4 2 6 4 8 5 2 2 1 6 3 5 5 2 5 Note In the first example query you can choose the price $$$B=2$$$. It is easy to see that the difference between each old price and each new price $$$B=2$$$ is no more than $$$1$$$. In the second example query you can choose the price $$$B=6$$$ and then all the differences between old and new price $$$B=6$$$ will be no more than $$$2$$$. In the third example query you cannot choose any suitable price $$$B$$$. For any value $$$B$$$ at least one condition out of two will be violated: $$$1-B le 2$$$, $$$6-B le 2$$$. In the fourth example query all values $$$B$$$ between $$$1$$$ and $$$7$$$ are valid. But the maximum is $$$7$$$, so it's the answer.
900
true
false
false
false
false
false
false
false
false
false
4,801
257D
Problem - 257D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy math *1900 No tag edit access → Contest materials ") Разбор Задач") . Vasya wants to add either a "+" or a "-" before each number of array. Thus, Vasya will get an expression consisting of _n_ summands. The value of the resulting expression is the sum of all its elements. The task is to add signs "+" and "-" before each number so that the value of expression _s_ meets the limits 0u2009≤u2009_s_u2009≤u2009_a_1. Print a sequence of signs "+" and "-", satisfying the given limits. It is guaranteed that the solution for the problem exists. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the size of the array. The second line contains space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009109) — the original array. It is guaranteed that the condition _a__i_u2009≤u2009_a__i_u2009+u20091u2009≤u20092·_a__i_ fulfills for any positive integer _i_ (_i_u2009<u2009_n_). Output In a single line print the sequence of _n_ characters "+" and "-", where the _i_-th character is the sign that is placed in front of number _a__i_. The value of the resulting expression _s_ must fit into the limits 0u2009≤u2009_s_u2009≤u2009_a_1. If there are multiple solutions, you are allowed to print any of them. Examples Input 4 1 2 3 5 Output +++- Input 3 3 3 5 Output ++-
1,900
true
true
false
false
false
false
false
false
false
false
8,804
913B
Problem - 913B - Codeforces =============== xa0 . Each of the next _n_u2009-u20091 lines contains one integer _p__i_ (1u2009≤u2009_i_u2009≤u2009_n_u2009-u20091)xa0— the index of the parent of the _i_u2009+u20091-th vertex (1u2009≤u2009_p__i_u2009≤u2009_i_). Vertex 1 is the root. It's guaranteed that the root has at least 2 children. Output Print "Yes" if the tree is a spruce and "No" otherwise. Examples Input 4 1 1 1 Output Yes Input 7 1 1 1 2 2 2 Output No Input 8 1 1 1 1 3 3 3 Output Yes Note The first example: The second example: It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children. The third example:
1,200
false
false
true
false
false
false
false
false
false
false
6,090
1198A
One common way of digitalizing sound is to record sound intensity at particular time moments. For each time moment intensity is recorded as a non-negative integer. Thus we can represent a sound file as an array of $$$n$$$ non-negative integers. If there are exactly $$$K$$$ distinct values in the array, then we need $$$k = lceil log_{2} K ceil$$$ bits to store each value. It then takes $$$nk$$$ bits to store the whole file. To reduce the memory consumption we need to apply some compression. One common way is to reduce the number of possible intensity values. We choose two integers $$$l le r$$$, and after that all intensity values are changed in the following way: if the intensity value is within the range $$$[l;r]$$$, we don't change it. If it is less than $$$l$$$, we change it to $$$l$$$; if it is greater than $$$r$$$, we change it to $$$r$$$. You can see that we lose some low and some high intensities. Your task is to apply this compression in such a way that the file fits onto a disk of size $$$I$$$ bytes, and the number of changed elements in the array is minimal possible. We remind you that $$$1$$$ byte contains $$$8$$$ bits. $$$k = lceil log_{2} K ceil$$$ is the smallest integer such that $$$K le 2^{k}$$$. In particular, if $$$K = 1$$$, then $$$k = 0$$$. Input The first line contains two integers $$$n$$$ and $$$I$$$ ($$$1 le n le 4 cdot 10^{5}$$$, $$$1 le I le 10^{8}$$$)xa0— the length of the array and the size of the disk in bytes, respectively. The next line contains $$$n$$$ integers $$$a_{i}$$$ ($$$0 le a_{i} le 10^{9}$$$)xa0— the array denoting the sound file. Output Print a single integerxa0— the minimal possible number of changed elements. Note In the first example we can choose $$$l=2, r=3$$$. The array becomes 2 2 2 3 3 3, the number of distinct elements is $$$K=2$$$, and the sound file fits onto the disk. Only two values are changed. In the second example the disk is larger, so the initial file fits it and no changes are required. In the third example we have to change both 1s or both 3s.
1,600
false
false
false
false
false
false
false
false
true
false
4,709
1917D
You are given a permutation $$$p_0, p_1, ldots, p_{n-1}$$$ of odd integers from $$$1$$$ to $$$2n-1$$$ and a permutation $$$q_0, q_1, ldots, q_{k-1}$$$ of integers from $$$0$$$ to $$$k-1$$$. An array $$$a_0, a_1, ldots, a_{nk-1}$$$ of length $$$nk$$$ is defined as follows: $$$a_{i cdot k+j}=p_i cdot 2^{q_j}$$$ for all $$$0 le i < n$$$ and all $$$0 le j < k$$$ For example, if $$$p = [3, 5, 1]$$$ and $$$q = [0, 1]$$$, then $$$a = [3, 6, 5, 10, 1, 2]$$$. Note that all arrays in the statement are zero-indexed. Note that each element of the array $$$a$$$ is uniquely determined. Find the number of inversions in the array $$$a$$$. Since this number can be very large, you should find only its remainder modulo $$$998,244,353$$$. An inversion in array $$$a$$$ is a pair $$$(i, j)$$$ ($$$0 le i < j < nk$$$) such that $$$a_i > a_j$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 2 cdot 10^5$$$)xa0— the lengths of arrays $$$p$$$ and $$$q$$$. The second line of each test case contains $$$n$$$ distinct integers $$$p_0, p_1, ldots, p_{n-1}$$$ ($$$1 le p_i le 2n-1$$$, $$$p_i$$$ is odd)xa0— the array $$$p$$$. The third line of each test case contains $$$k$$$ distinct integers $$$q_0, q_1, ldots, q_{k-1}$$$ ($$$0 le q_i < k$$$)xa0— the array $$$q$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$ and the sum of $$$k$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, output one integer: the number of inversions in array $$$a$$$ modulo $$$998,244,353$$$. Example Input 4 3 2 3 5 1 0 1 3 4 1 3 5 3 2 0 1 1 5 1 0 1 2 3 4 8 3 5 1 7 11 15 3 9 13 2 0 1 Note In the first test case, array $$$a$$$ is equal to $$$[3, 6, 5, 10, 1, 2]$$$. There are $$$9$$$ inversions in it: $$$(0, 4)$$$, $$$(0, 5)$$$, $$$(1, 2)$$$, $$$(1, 4)$$$, $$$(1, 5)$$$, $$$(2, 4)$$$, $$$(2, 5)$$$, $$$(3, 4)$$$, $$$(3, 5)$$$. Note that these are pairs $$$(i, j)$$$ such that $$$i < j$$$ and $$$a_i > a_j$$$. In the second test case, array $$$a$$$ is equal to $$$[8, 4, 1, 2, 24, 12, 3, 6, 40, 20, 5, 10]$$$. There are $$$25$$$ inversions in it. In the third test case, array $$$a$$$ is equal to $$$[1, 2, 4, 8, 16]$$$. There are no inversions in it.
2,300
true
false
true
true
true
false
false
false
false
false
791
1202B
Suppose you have a special $$$x$$$-$$$y$$$-counter. This counter can store some value as a decimal number; at first, the counter has value $$$0$$$. The counter performs the following algorithm: it prints its lowest digit and, after that, adds either $$$x$$$ or $$$y$$$ to its value. So all sequences this counter generates are starting from $$$0$$$. For example, a $$$4$$$-$$$2$$$-counter can act as follows: 1. it prints $$$0$$$, and adds $$$4$$$ to its value, so the current value is $$$4$$$, and the output is $$$0$$$; 2. it prints $$$4$$$, and adds $$$4$$$ to its value, so the current value is $$$8$$$, and the output is $$$04$$$; 3. it prints $$$8$$$, and adds $$$4$$$ to its value, so the current value is $$$12$$$, and the output is $$$048$$$; 4. it prints $$$2$$$, and adds $$$2$$$ to its value, so the current value is $$$14$$$, and the output is $$$0482$$$; 5. it prints $$$4$$$, and adds $$$4$$$ to its value, so the current value is $$$18$$$, and the output is $$$04824$$$. This is only one of the possible outputs; for example, the same counter could generate $$$0246802468024$$$ as the output, if we chose to add $$$2$$$ during each step. You wrote down a printed sequence from one of such $$$x$$$-$$$y$$$-counters. But the sequence was corrupted and several elements from the sequence could be erased. Now you'd like to recover data you've lost, but you don't even know the type of the counter you used. You have a decimal string $$$s$$$ — the remaining data of the sequence. For all $$$0 le x, y < 10$$$, calculate the minimum number of digits you have to insert in the string $$$s$$$ to make it a possible output of the $$$x$$$-$$$y$$$-counter. Note that you can't change the order of digits in string $$$s$$$ or erase any of them; only insertions are allowed. Input The first line contains a single string $$$s$$$ ($$$1 le s le 2 cdot 10^6$$$, $$$s_i in { ext{0} - ext{9}}$$$) — the remaining data you have. It's guaranteed that $$$s_1 = 0$$$. Output Print a $$$10 imes 10$$$ matrix, where the $$$j$$$-th integer ($$$0$$$-indexed) on the $$$i$$$-th line ($$$0$$$-indexed too) is equal to the minimum number of digits you have to insert in the string $$$s$$$ to make it a possible output of the $$$i$$$-$$$j$$$-counter, or $$$-1$$$ if there is no way to do so. Example Output -1 17 7 7 7 -1 2 17 2 7 17 17 7 5 5 5 2 7 2 7 7 7 7 4 3 7 1 7 2 5 7 5 4 7 3 3 2 5 2 3 7 5 3 3 7 7 1 7 2 7 -1 5 7 3 7 -1 2 9 2 7 2 2 1 2 1 2 2 2 0 1 17 7 7 5 7 9 2 17 2 3 2 2 2 2 2 2 0 2 2 2 7 7 5 3 7 7 1 3 2 7 Note Let's take, for example, $$$4$$$-$$$3$$$-counter. One of the possible outcomes the counter could print is $$$0(4)8(1)4(7)0$$$ (lost elements are in the brackets). One of the possible outcomes a $$$2$$$-$$$3$$$-counter could print is $$$0(35)8(1)4(7)0$$$. The $$$6$$$-$$$8$$$-counter could print exactly the string $$$0840$$$.
1,700
false
false
false
true
false
false
true
false
false
false
4,688
733A
Problem - 733A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *1000 No tag edit access → Contest materials ") editorial") that is needed to overcome the given string, jumping only on vowels. Examples Input ABABBBACFEYUKOTT Output 4 Input AAA Output 1
1,000
false
false
true
false
false
false
false
false
false
false
6,864
1705F
Mark is administering an online exam consisting of $$$n$$$ true-false questions. However, he has lost all answer keys. He needs a way to retrieve the answers before his client gets infuriated. Fortunately, he has access to the grading system. Thus, for each query, you can input the answers to all $$$n$$$ questions, and the grading system will output how many of them are correct. He doesn't have much time, so he can use the grading system at most $$$675$$$ times. Help Mark determine the answer keys. Note that answer keys are fixed in advance and will not change depending on your queries. Input The first line of the input consists of an integer $$$n$$$ ($$$1leq nleq 1000$$$) — the number of questions. Interaction After reading $$$n$$$, you can start making queries to the grading system. For each query, print a line containing a string $$$s$$$ of length $$$n$$$ consisting of only letters 'T' and 'F'. $$$s_i = $$$'T' means that you answer the $$$i$$$-question true. $$$s_i = $$$'F' means that you answer the $$$i$$$-question false. After a successful query, you should read an integer $$$k$$$ ($$$0leq kleq n$$$) — the number of correct answers. If you read $$$n$$$, then you found the answers, and your program should not make any more queries. If your program reads $$$k = -1$$$ instead of the number of correct answers, it means that you either made an invalid query or exceeded the query limits. Exit immediately after receiving $$$-1$$$, and you will see Wrong answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hacks To hack, use the following format: The first line contains an integer $$$n$$$ ($$$1leq nleq 1000$$$) — the number of questions. The second line contains a string $$$s$$$ of length $$$n$$$ consisting of only 'T' and 'F' — the answer key. Note The empty lines in the example are just for you to better understand the interaction process. You're not required to print them. In the first example, there are $$$3$$$ questions, and the answer to each question is 'true', 'true', and 'false', respectively. The first query, guessing the answers to be 'false', 'true', and 'true', respectively, guesses only one question — the $$$2$$$-nd question — correctly. Then, in the second query, the program correctly guesses the answer key. The interaction ends here. In the second example, there are $$$4$$$ questions, and the answer to each question is 'true', 'false', 'true', and 'true', respectively. The first query guessed none of the questions correctly, resulting in the answer $$$0$$$. The second query guessed the $$$1$$$-st, $$$3$$$-rd, and $$$4$$$-th question correctly, resulting in the answer $$$3$$$. In the third query, the program correctly guesses the answer key. Then, the interaction ends.
2,900
false
false
false
false
false
true
false
false
false
false
2,041
1332E
Alice has got addicted to a game called Sirtet recently. In Sirtet, player is given an $$$n imes m$$$ grid. Initially $$$a_{i,j}$$$ cubes are stacked up in the cell $$$(i,j)$$$. Two cells are called adjacent if they share a side. Player can perform the following operations: stack up one cube in two adjacent cells; stack up two cubes in one cell. Cubes mentioned above are identical in height. Here is an illustration of the game. States on the right are obtained by performing one of the above operations on the state on the left, and grey cubes are added due to the operation. Player's goal is to make the height of all cells the same (i.e. so that each cell has the same number of cubes in it) using above operations. Alice, however, has found out that on some starting grids she may never reach the goal no matter what strategy she uses. Thus, she is wondering the number of initial grids such that $$$L le a_{i,j} le R$$$ for all $$$1 le i le n$$$, $$$1 le j le m$$$; player can reach the goal using above operations. Please help Alice with it. Notice that the answer might be large, please output the desired value modulo $$$998,244,353$$$. Input The only line contains four integers $$$n$$$, $$$m$$$, $$$L$$$ and $$$R$$$ ($$$1le n,m,L,R le 10^9$$$, $$$L le R$$$, $$$n cdot m ge 2$$$). Output Output one integer, representing the desired answer modulo $$$998,244,353$$$. Note In the first sample, the only initial grid that satisfies the requirements is $$$a_{1,1}=a_{2,1}=a_{1,2}=a_{2,2}=1$$$. Thus the answer should be $$$1$$$. In the second sample, initial grids that satisfy the requirements are $$$a_{1,1}=a_{1,2}=1$$$ and $$$a_{1,1}=a_{1,2}=2$$$. Thus the answer should be $$$2$$$.
2,100
true
false
false
false
false
true
false
false
false
false
4,046
1343E
You are given an undirected unweighted graph consisting of $$$n$$$ vertices and $$$m$$$ edges (which represents the map of Bertown) and the array of prices $$$p$$$ of length $$$m$$$. It is guaranteed that there is a path between each pair of vertices (districts). Mike has planned a trip from the vertex (district) $$$a$$$ to the vertex (district) $$$b$$$ and then from the vertex (district) $$$b$$$ to the vertex (district) $$$c$$$. He can visit the same district twice or more. But there is one issue: authorities of the city want to set a price for using the road so if someone goes along the road then he should pay the price corresponding to this road (he pays each time he goes along the road). The list of prices that will be used $$$p$$$ is ready and they just want to distribute it between all roads in the town in such a way that each price from the array corresponds to exactly one road. You are a good friend of Mike (and suddenly a mayor of Bertown) and want to help him to make his trip as cheap as possible. So, your task is to distribute prices between roads in such a way that if Mike chooses the optimal path then the price of the trip is the minimum possible. Note that you cannot rearrange prices after the start of the trip. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains five integers $$$n, m, a, b$$$ and $$$c$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$n-1 le m le min(frac{n(n-1)}{2}, 2 cdot 10^5)$$$, $$$1 le a, b, c le n$$$) — the number of vertices, the number of edges and districts in Mike's trip. The second line of the test case contains $$$m$$$ integers $$$p_1, p_2, dots, p_m$$$ ($$$1 le p_i le 10^9$$$), where $$$p_i$$$ is the $$$i$$$-th price from the array. The following $$$m$$$ lines of the test case denote edges: edge $$$i$$$ is represented by a pair of integers $$$v_i$$$, $$$u_i$$$ ($$$1 le v_i, u_i le n$$$, $$$u_i e v_i$$$), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i.u2009e. for each pair ($$$v_i, u_i$$$) there are no other pairs ($$$v_i, u_i$$$) or ($$$u_i, v_i$$$) in the array of edges, and for each pair $$$(v_i, u_i)$$$ the condition $$$v_i e u_i$$$ is satisfied. It is guaranteed that the given graph is connected. It is guaranteed that the sum of $$$n$$$ (as well as the sum of $$$m$$$) does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$, $$$sum m le 2 cdot 10^5$$$).
2,100
false
true
false
false
false
false
true
false
true
true
3,989
1644F
For an array of integers $$$a$$$, let's define $$$a$$$ as the number of elements in it. Let's denote two functions: $$$F(a, k)$$$ is a function that takes an array of integers $$$a$$$ and a positive integer $$$k$$$. The result of this function is the array containing $$$a$$$ first elements of the array that you get by replacing each element of $$$a$$$ with exactly $$$k$$$ copies of that element.For example, $$$F([2, 2, 1, 3, 5, 6, 8], 2)$$$ is calculated as follows: first, you replace each element of the array with $$$2$$$ copies of it, so you obtain $$$[2, 2, 2, 2, 1, 1, 3, 3, 5, 5, 6, 6, 8, 8]$$$. Then, you take the first $$$7$$$ elements of the array you obtained, so the result of the function is $$$[2, 2, 2, 2, 1, 1, 3]$$$. $$$G(a, x, y)$$$ is a function that takes an array of integers $$$a$$$ and two different integers $$$x$$$ and $$$y$$$. The result of this function is the array $$$a$$$ with every element equal to $$$x$$$ replaced by $$$y$$$, and every element equal to $$$y$$$ replaced by $$$x$$$.For example, $$$G([1, 1, 2, 3, 5], 3, 1) = [3, 3, 2, 1, 5]$$$. An array $$$a$$$ is a parent of the array $$$b$$$ if: either there exists a positive integer $$$k$$$ such that $$$F(a, k) = b$$$; or there exist two different integers $$$x$$$ and $$$y$$$ such that $$$G(a, x, y) = b$$$. An array $$$a$$$ is an ancestor of the array $$$b$$$ if there exists a finite sequence of arrays $$$c_0, c_1, dots, c_m$$$ ($$$m ge 0$$$) such that $$$c_0$$$ is $$$a$$$, $$$c_m$$$ is $$$b$$$, and for every $$$i in [1, m]$$$, $$$c_{i-1}$$$ is a parent of $$$c_i$$$. And now, the problem itself. You are given two integers $$$n$$$ and $$$k$$$. Your goal is to construct a sequence of arrays $$$s_1, s_2, dots, s_m$$$ in such a way that: every array $$$s_i$$$ contains exactly $$$n$$$ elements, and all elements are integers from $$$1$$$ to $$$k$$$; for every array $$$a$$$ consisting of exactly $$$n$$$ integers from $$$1$$$ to $$$k$$$, the sequence contains at least one array $$$s_i$$$ such that $$$s_i$$$ is an ancestor of $$$a$$$. Print the minimum number of arrays in such sequence. Input The only line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 2 cdot 10^5$$$). Output Print one integer — the minimum number of elements in a sequence of arrays meeting the constraints. Since the answer can be large, output it modulo $$$998244353$$$. Note Let's analyze the first example. One of the possible answers for the first example is the sequence $$$[[2, 1, 2], [1, 2, 2]]$$$. Every array of size $$$3$$$ consisting of elements from $$$1$$$ to $$$2$$$ has an ancestor in this sequence: for the array $$$[1, 1, 1]$$$, the ancestor is $$$[1, 2, 2]$$$: $$$F([1, 2, 2], 13) = [1, 1, 1]$$$; for the array $$$[1, 1, 2]$$$, the ancestor is $$$[1, 2, 2]$$$: $$$F([1, 2, 2], 2) = [1, 1, 2]$$$; for the array $$$[1, 2, 1]$$$, the ancestor is $$$[2, 1, 2]$$$: $$$G([2, 1, 2], 1, 2) = [1, 2, 1]$$$; for the array $$$[1, 2, 2]$$$, the ancestor is $$$[1, 2, 2]$$$; for the array $$$[2, 1, 1]$$$, the ancestor is $$$[1, 2, 2]$$$: $$$G([1, 2, 2], 1, 2) = [2, 1, 1]$$$; for the array $$$[2, 1, 2]$$$, the ancestor is $$$[2, 1, 2]$$$; for the array $$$[2, 2, 1]$$$, the ancestor is $$$[2, 1, 2]$$$: $$$F([2, 1, 2], 2) = [2, 2, 1]$$$; for the array $$$[2, 2, 2]$$$, the ancestor is $$$[1, 2, 2]$$$: $$$G(F([1, 2, 2], 4), 1, 2) = G([1, 1, 1], 1, 2) = [2, 2, 2]$$$.
2,900
true
false
false
false
false
false
false
false
false
false
2,396
522C
Polycarp is flying in the airplane. Finally, it is his favorite time — the lunchtime. The BerAvia company stewardess is giving food consecutively to all the passengers from the 1-th one to the last one. Polycarp is sitting on seat _m_, that means, he will be the _m_-th person to get food. The flight menu has _k_ dishes in total and when Polycarp boarded the flight, he had time to count the number of portions of each dish on board. Thus, he knows values _a_1,u2009_a_2,u2009...,u2009_a__k_, where _a__i_ is the number of portions of the _i_-th dish. The stewardess has already given food to _m_u2009-u20091 passengers, gave Polycarp a polite smile and asked him what he would prefer. That's when Polycarp realized that they might have run out of some dishes by that moment. For some of the _m_u2009-u20091 passengers ahead of him, he noticed what dishes they were given. Besides, he's heard some strange mumbling from some of the _m_u2009-u20091 passengers ahead of him, similar to phrase 'I'm disappointed'. That happened when a passenger asked for some dish but the stewardess gave him a polite smile and said that they had run out of that dish. In that case the passenger needed to choose some other dish that was available. If Polycarp heard no more sounds from a passenger, that meant that the passenger chose his dish at the first try. Help Polycarp to find out for each dish: whether they could have run out of the dish by the moment Polyarp was served or that dish was definitely available. Input Each test in this problem consists of one or more input sets. First goes a string that contains a single integer _t_ (1u2009≤u2009_t_u2009≤u2009100u2009000) — the number of input data sets in the test. Then the sets follow, each set is preceded by an empty line. The first line of each set of the input contains integers _m_, _k_ (2u2009≤u2009_m_u2009≤u2009100u2009000, 1u2009≤u2009_k_u2009≤u2009100u2009000) — the number of Polycarp's seat and the number of dishes, respectively. The second line contains a sequence of _k_ integers _a_1,u2009_a_2,u2009...,u2009_a__k_ (1u2009≤u2009_a__i_u2009≤u2009100u2009000), where _a__i_ is the initial number of portions of the _i_-th dish. Then _m_u2009-u20091 lines follow, each line contains the description of Polycarp's observations about giving food to a passenger sitting in front of him: the _j_-th line contains a pair of integers _t__j_,u2009_r__j_ (0u2009≤u2009_t__j_u2009≤u2009_k_,u20090u2009≤u2009_r__j_u2009≤u20091), where _t__j_ is the number of the dish that was given to the _j_-th passenger (or 0, if Polycarp didn't notice what dish was given to the passenger), and _r__j_ — a 1 or a 0, depending on whether the _j_-th passenger was or wasn't disappointed, respectively. We know that sum _a__i_ equals at least _m_, that is,Polycarp will definitely get some dish, even if it is the last thing he wanted. It is guaranteed that the data is consistent. Sum _m_ for all input sets doesn't exceed 100u2009000. Sum _k_ for all input sets doesn't exceed 100u2009000. Output For each input set print the answer as a single line. Print a string of _k_ letters "Y" or "N". Letter "Y" in position _i_ should be printed if they could have run out of the _i_-th dish by the time the stewardess started serving Polycarp. Examples Input 23 4 2 3 2 1 1 0 0 0 5 5 1 2 1 3 1 3 0 0 0 2 1 4 0 Note In the first input set depending on the choice of the second passenger the situation could develop in different ways: If he chose the first dish, then by the moment the stewardess reaches Polycarp, they will have run out of the first dish; If he chose the fourth dish, then by the moment the stewardess reaches Polycarp, they will have run out of the fourth dish; Otherwise, Polycarp will be able to choose from any of the four dishes. Thus, the answer is "YNNY". In the second input set there is, for example, the following possible scenario. First, the first passenger takes the only third dish, then the second passenger takes the second dish. Then, the third passenger asks for the third dish, but it is not available, so he makes disappointed muttering and ends up with the second dish. Then the fourth passenger takes the fourth dish, and Polycarp ends up with the choice between the first, fourth and fifth dish. Likewise, another possible scenario is when by the time the stewardess comes to Polycarp, they will have run out of either the first or the fifth dish (this can happen if one of these dishes is taken by the second passenger). It is easy to see that there is more than enough of the fourth dish, so Polycarp can always count on it. Thus, the answer is "YYYNY".
2,100
false
true
false
false
false
false
false
false
false
false
7,748
1849C
You are given a string $$$s$$$ consisting of $$$n$$$ characters 0 and/or 1. You make $$$m$$$ copies of this string, let the $$$i$$$-th copy be the string $$$t_i$$$. Then you perform exactly one operation on each of the copies: for the $$$i$$$-th copy, you sort its substring $$$[l_i; r_i]$$$ (the substring from the $$$l_i$$$-th character to the $$$r_i$$$-th character, both endpoints inclusive). Note that each operation affects only one copy, and each copy is affected by only one operation. Your task is to calculate the number of different strings among $$$t_1, t_2, ldots, t_m$$$. Note that the initial string $$$s$$$ should be counted only if at least one of the copies stays the same after the operation. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2 cdot 10^5$$$) — the length of $$$s$$$ and the number of copies, respectively. The second line contains $$$n$$$ characters 0 and/or 1 — the string $$$s$$$. Then $$$m$$$ lines follow. The $$$i$$$-th of them contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$) — the description of the operation applied to the $$$i$$$-th copy. The sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. The sum of $$$m$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output Print one integer — the number of different strings among $$$t_1, t_2, ldots, t_m$$$. Example Input 3 6 5 101100 1 2 1 3 2 4 5 5 1 6 6 4 100111 2 2 1 4 1 3 1 2 1 1 0 1 1 Note Consider the first example. Copies below are given in order of the input operations. Underlined substrings are substrings that are sorted: 1. 101100 $$$ ightarrow$$$ 011100; 2. 101100 $$$ ightarrow$$$ 011100; 3. 101100 $$$ ightarrow$$$ 101100; 4. 101100 $$$ ightarrow$$$ 101100; 5. 101100 $$$ ightarrow$$$ 000111. There are three different strings among $$$t_1, t_2, t_3, t_4, t_5$$$: 000111, 011100 and 101100. Consider the second example: 1. 100111 $$$ ightarrow$$$ 100111; 2. 100111 $$$ ightarrow$$$ 001111; 3. 100111 $$$ ightarrow$$$ 001111; 4. 100111 $$$ ightarrow$$$ 010111. There are three different strings among $$$t_1, t_2, t_3, t_4$$$: 001111, 010111 and 100111.
1,600
false
false
false
false
true
false
true
true
false
false
1,186
245E
Problem - 245E - 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 *1400 No tag edit access → Contest materials ")
1,400
false
true
true
false
false
false
false
false
false
false
8,852
935F
Problem - 935F - 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 *2600 No tag edit access → Contest materials ") is defined as . He wants to do _q_ queries of two types: 1u2009_l_u2009_r_u2009_x_ — find the maximum possible value of _f_(_A_), if _x_ is to be added to one element in the range
2,600
false
true
false
false
true
false
false
false
false
false
5,959
1494F
You are given an undirected connected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. Your goal is to destroy all edges of the given graph. You may choose any vertex as the starting one and begin walking from it along the edges. When you walk along an edge, you destroy it. Obviously, you cannot walk along an edge if it is destroyed. You can perform the mode shift operation at most once during your walk, and this operation can only be performed when you are at some vertex (you cannot perform it while traversing an edge). After the mode shift, the edges you go through are deleted in the following way: the first edge after the mode shift is not destroyed, the second one is destroyed, the third one is not destroyed, the fourth one is destroyed, and so on. You cannot switch back to the original mode, and you don't have to perform this operation if you don't want to. Can you destroy all the edges of the given graph? Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 3000$$$; $$$n - 1 le m le min(frac{n(n-1)}{2}, 3000$$$)) — the numbef of vertices and the number of edges in the graph. Then $$$m$$$ lines follow, each containing two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$; $$$x_i e y_i$$$) — the endpoints of the $$$i$$$-th edge. These edges form a connected undirected graph without multiple edges. Output If it's impossible to destroy all of the edges, print 0. Otherwise, print the sequence of your actions as follows. First, print $$$k$$$ — the number of actions ($$$k le 2m + 2$$$). Then, print the sequence itself, consisting of $$$k$$$ integers. The first integer should be the index of the starting vertex. Then, each of the next integers should be either the index of the next vertex in your traversal, or $$$-1$$$ if you use mode shift. You are allowed to use mode shift at most once. If there are multiple answers, print any of them. Examples Input 5 5 1 2 2 3 3 1 2 4 2 5 Output 9 2 3 1 2 -1 4 2 5 2 Input 5 5 1 2 2 3 3 1 2 4 4 5 Input 6 5 1 2 2 3 3 4 4 5 3 6
2,900
false
false
true
false
false
true
true
false
false
true
3,197
1618A
Polycarp had an array $$$a$$$ of $$$3$$$ positive integers. He wrote out the sums of all non-empty subsequences of this array, sorted them in non-decreasing order, and got an array $$$b$$$ of $$$7$$$ integers. For example, if $$$a = {1, 4, 3}$$$, then Polycarp wrote out $$$1$$$, $$$4$$$, $$$3$$$, $$$1 + 4 = 5$$$, $$$1 + 3 = 4$$$, $$$4 + 3 = 7$$$, $$$1 + 4 + 3 = 8$$$. After sorting, he got an array $$$b = {1, 3, 4, 4, 5, 7, 8}.$$$ Unfortunately, Polycarp lost the array $$$a$$$. He only has the array $$$b$$$ left. Help him to restore the array $$$a$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 5000$$$) — the number of test cases. Each test case consists of one line which contains $$$7$$$ integers $$$b_1, b_2, dots, b_7$$$ ($$$1 le b_i le 10^9$$$; $$$b_i le b_{i+1}$$$). Additional constraint on the input: there exists at least one array $$$a$$$ which yields this array $$$b$$$ as described in the statement. Output For each test case, print $$$3$$$ integers — $$$a_1$$$, $$$a_2$$$ and $$$a_3$$$. If there can be several answers, print any of them. Note The subsequence of the array $$$a$$$ is a sequence that can be obtained from $$$a$$$ by removing zero or more of its elements. Two subsequences are considered different if index sets of elements included in them are different. That is, the values of the elements don't matter in the comparison of subsequences. In particular, any array of length $$$3$$$ has exactly $$$7$$$ different non-empty subsequences.
800
true
false
false
false
false
false
false
false
true
false
2,542