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
1421C
Ringo found a string $$$s$$$ of length $$$n$$$ in his and to append the substring $$$s_2s_3 ldots s_i$$$ ($$$i - 1$$$ characters) reversed to the front of $$$s$$$. The second operation allows him to choose $$$i$$$ ($$$2 le i le n-1$$$) and to append the substring $$$s_i s_{i + 1}ldots s_{n - 1}$$$ ($$$n - i$$$ characters) reversed to the end of $$$s$$$. Note that characters in the string in this problem are indexed from $$$1$$$. For example suppose $$$s=$$$abcdef. If he performs the first operation with $$$i=3$$$ then he appends cb to the front of $$$s$$$ and the result will be cbabcdef. Performing the second operation on the resulted string with $$$i=5$$$ will yield cbabcdefedc. Your task is to help Ringo make the entire string a palindrome by applying any of the two operations (in total) at most $$$30$$$ times. The length of the resulting palindrome must not exceed $$$10^6$$$ It is guaranteed that under these constraints there always is a solution. Also note you do not have to minimize neither the number of operations applied, nor the length of the resulting string, but they have to fit into the constraints. Input The only line contains the string $$$S$$$ ($$$3 le s le 10^5$$$) of lowercase letters from the English alphabet. Output The first line should contain $$$k$$$ ($$$0le k le 30$$$) xa0— the number of operations performed. Each of the following $$$k$$$ lines should describe an operation in form L i or R i. $$$L$$$ represents the first operation, $$$R$$$ represents the second operation, $$$i$$$ represents the index chosen. The length of the resulting palindrome must not exceed $$$10^6$$$. Note For the first example the following operations are performed: abac $$$ o$$$ abacab $$$ o$$$ abacaba The second sample performs the following operations: acccc $$$ o$$$ cccacccc $$$ o$$$ ccccacccc The third example is already a palindrome so no operations are required.
1,400
false
false
false
false
false
true
false
false
false
false
3,572
838A
You are given an image, that can be represented with a 2-d _n_ by _m_ grid of pixels. Each pixel of the image is either on or off, denoted by the characters "0" or "1", respectively. You would like to compress this image. You want to choose an integer _k_u2009>u20091 and split the image into _k_ by _k_ blocks. If _n_ and _m_ are not divisible by _k_, the image is padded with only zeros on the right and bottom so that they are divisible by _k_. Each pixel in each individual block must have the same value. The given image may not be compressible in its current state. Find the minimum number of pixels you need to toggle (after padding) in order for the image to be compressible for some _k_. More specifically, the steps are to first choose _k_, then the image is padded with zeros, then, we can toggle the pixels so it is compressible for this _k_. The image must be compressible in that state. Input The first line of input will contain two integers _n_,u2009_m_ (2u2009≤u2009_n_,u2009_m_u2009≤u20092u2009500), the dimensions of the image. The next _n_ lines of input will contain a binary string with exactly _m_ characters, representing the image. Output Print a single integer, the minimum number of pixels needed to toggle to make the image compressible. Note We first choose _k_u2009=u20092. The image is padded as follows: 001000 101100 110010 000000 We can toggle the image to look as follows: 001100 001100 000000 000000 We can see that this image is compressible for _k_u2009=u20092.
1,400
false
false
false
false
false
false
true
false
false
false
6,414
69A
A guy named Vasya attends the final grade of a high school. One day Vasya decided to watch a match of his favorite hockey team. And, as the boy loves hockey very much, even more than physics, he forgot to do the homework. Specifically, he forgot to complete his physics tasks. Next day the teacher got very angry at Vasya and decided to teach him a lesson. He gave the lazy student a seemingly easy task: You are given an idle body in space and the forces that affect it. The body can be considered as a material point with coordinates (0; 0; 0). Vasya had only to answer whether it is in equilibrium. "Piece of cake" — thought Vasya, we need only to check if the sum of all vectors is equal to 0. So, Vasya began to solve the problem. But later it turned out that there can be lots and lots of these forces, and Vasya can not cope without your help. Help him. Write a program that determines whether a body is idle or is moving by the given vectors of forces. Input The first line contains a positive integer _n_ (1u2009≤u2009_n_u2009≤u2009100), then follow _n_ lines containing three integers each: the _x__i_ coordinate, the _y__i_ coordinate and the _z__i_ coordinate of the force vector, applied to the body (u2009-u2009100u2009≤u2009_x__i_,u2009_y__i_,u2009_z__i_u2009≤u2009100). Output Print the word "YES" if the body is in equilibrium, or the word "NO" if it is not. Examples Input 3 4 1 7 -2 4 -1 1 -5 -3 Input 3 3 -1 7 -5 2 -4 2 -1 -3
1,000
true
false
true
false
false
false
false
false
false
false
9,626
1403B
Spring cleanings are probably the most boring parts of our lives, except this year, when Flóra and her mother found a dusty old tree graph under the carpet. This tree has $$$N$$$ nodes (numbered from $$$1$$$ to $$$N$$$), connected by $$$N-1$$$ edges. The edges gathered too much dust, so Flóra's mom decided to clean them. Cleaning the edges of an arbitrary tree is done by repeating the following process: She chooses 2 different leaves (a node is a leaf if it is connected to exactly one other node by an edge), and cleans every edge lying on the shortest path between them. If this path has $$$d$$$ edges, then the cost of cleaning this path is $$$d$$$. She doesn't want to harm the leaves of the tree, so she chooses every one of them at most once. A tree is cleaned when all of its edges are cleaned. The cost of this is the sum of costs for all cleaned paths. Flóra thinks the tree they found is too small and simple, so she imagines $$$Q$$$ variations of it. In the $$$i$$$-th variation, she adds a total of $$$D_i$$$ extra leaves to the original tree: for each new leaf, she chooses a node from the original tree, and connects that node with the new leaf by an edge. Note that some nodes may stop being leaves during this step. For all these $$$Q$$$ variations, we are interested in the minimum cost that is required to clean the tree. Input The first line contains two space-separated integer, $$$N$$$ and $$$Q$$$ ($$$3 leq N leq 10^{5}$$$, $$$1 leq Q leq 10^{5}$$$) – the number of nodes the tree has and the number of variations. Each of the next $$$N-1$$$ lines contains two space-separated integers $$$u$$$ and $$$v$$$ denoting that nodes $$$u$$$ and $$$v$$$ are connected by an edge ($$$1 leq u, v leq N$$$). The next $$$Q$$$ lines describe the variations. The first integer in the $$$i$$$th line is $$$D_i$$$ ($$$1 leq D_i leq 10^{5}$$$). Then $$$D_i$$$ space-separated integers follow: if the $$$j$$$th number is $$$a_j$$$, it means that Flóra adds a new leaf to node $$$a_j$$$ ($$$1 leq a_j leq N$$$). We may add more than one leaf to the same node. $$$sum_{1}^{Q} D_i leq 10^{5}$$$ i.e. the sum of $$$D_i$$$ in all varations is at most $$$10^5$$$. After each variation, Flóra restarts and adds extra leaves to the original tree. Output You should print $$$Q$$$ lines. In the $$$i$$$-th line, print a single integer: the minimum cost required to clean the $$$i$$$-th variation of the tree. If the tree cannot be cleaned, print $$$-1$$$. Note The following picture shows the second variation. A possible solution is to clean the path between leaves $$$1 - 6$$$, $$$A - 7$$$ and $$$ B - 3$$$. You can download the above example and an additional (bigger) sample input here: [
2,300
false
false
false
false
true
false
false
false
false
true
3,655
1748C
The score of an array $$$v_1,v_2,ldots,v_n$$$ is defined as the number of indices $$$i$$$ ($$$1 le i le n$$$) such that $$$v_1+v_2+ldots+v_i = 0$$$. You are given an array $$$a_1,a_2,ldots,a_n$$$ of length $$$n$$$. You can perform the following operation multiple times: select an index $$$i$$$ ($$$1 le i le n$$$) such that $$$a_i=0$$$; then replace $$$a_i$$$ by an arbitrary integer. What is the maximum possible score of $$$a$$$ that can be obtained by performing a sequence of such operations? Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$-10^9 le a_i le 10^9$$$)xa0— array $$$a$$$. 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 maximum possible score of the array $$$a$$$ after performing a sequence of operations. Example Input 5 5 2 0 1 -1 0 3 1000000000 1000000000 0 4 0 0 0 0 8 3 0 2 -10 10 -30 30 0 9 1 0 0 1 -1 0 1 0 -1 Note In the first test case, it is optimal to change the value of $$$a_2$$$ to $$$-2$$$ in one operation. The resulting array $$$a$$$ will be $$$[2,-2,1,-1,0]$$$, with a score of $$$3$$$: $$$a_1+a_2=2-2=0$$$; $$$a_1+a_2+a_3+a_4=2-2+1-1=0$$$; $$$a_1+a_2+a_3+a_4+a_5=2-2+1-1+0=0$$$. In the second test case, it is optimal to change the value of $$$a_3$$$ to $$$-2,000,000,000$$$, giving us an array with a score of $$$1$$$. In the third test case, it is not necessary to perform any operations.
1,600
false
true
true
true
true
false
true
false
false
false
1,795
584D
Problem - 584D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force math number theory *1800 No tag edit access → Contest materials , such that 1. 1u2009≤u2009_k_u2009≤u20093 2. _p__i_ is a prime 3. The numbers _p__i_ do not necessarily have to be distinct. It is guaranteed that at least one possible solution exists. Input The single line contains an odd number _n_ (3u2009≤u2009_n_u2009<u2009109). Output In the first line print _k_ (1u2009≤u2009_k_u2009≤u20093), showing how many numbers are in the representation you found. In the second line print numbers _p__i_ in any order. If there are multiple possible solutions, you can print any of them. Examples Input 27 Output 3 5 11 11 Note A prime is an integer strictly larger than one that is divisible only by one and by itself.
1,800
true
false
false
false
false
false
true
false
false
false
7,507
1860F
You are given $$$2n$$$ tuples of values $$$(a, b, c)$$$, where $$$a$$$ and $$$b$$$ are positive integers and $$$c$$$ is a bracket '(' or ')'. Exactly $$$n$$$ tuples have $$$c = $$$'(' and the other $$$n$$$ tuples have $$$c =$$$ ')'. You are asked to choose two positive values $$$x$$$ and $$$y$$$ ($$$x > 0$$$ and $$$y > 0$$$; not necessarily integers) and sort the tuples in the increasing value of $$$a cdot x + b cdot y$$$. If several tuples have the same value, you can place them in any order among themselves. Is it possible to choose such $$$x$$$ and $$$y$$$ that taking brackets $$$c$$$ from the tuples in the resulting order produces a regular bracket sequence? A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1500$$$)xa0— the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$1 le n le 1500$$$). The $$$i$$$-th of the next $$$2n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le 10^6$$$) and a character $$$c_i$$$ ($$$c_i = $$$'(' or $$$c_i = $$$')'). Exactly $$$n$$$ tuples have $$$c_i = $$$'(' and the other $$$n$$$ tuples have $$$c_i =$$$ ')'. The sum of $$$n$$$ over all testcases doesn't exceed $$$1500$$$. Output For each testcase, print "YES" if it's possible to choose such $$$x$$$ and $$$y$$$ that taking brackets $$$c$$$ from the tuples in the resulting order produces a regular bracket sequence. Otherwise, print "NO". Example Input 4 1 1 4 ( 2 3 ) 1 1 2 ) 3 4 ( 4 16 5 ( 12 3 ( 19 6 ) 4 10 ) 3 10 ) 19 11 ( 19 7 ) 7 14 ( 4 16 8 ( 11 9 ) 20 10 ) 20 19 ) 2 13 ( 18 7 ( 15 19 ) 5 6 ( Note In the first testcase, you can choose $$$x = 10, y = 0.1$$$. The values for tuples will be $$$10 cdot 1 + 0.1 cdot 4 = 10.4$$$ and $$$10 cdot 2 + 0.1 cdot 3 = 20.3$$$. Thus, the first tuple will go first, then the second one, making the bracket sequence "()", which is regular. In the second testcase, you can't choose positive $$$x$$$ and $$$y$$$ such that the opening brackets gets assigned a value less or equal to the value of the closing bracket. In the fourth testcase, you can choose $$$x = 0.6, y = 0.55$$$. The bracket sequence is "(()(()))".
2,900
true
false
true
false
true
false
false
false
true
false
1,120
1401A
We have a point $$$A$$$ with coordinate $$$x = n$$$ on $$$OX$$$-axis. We'd like to find an integer point $$$B$$$ (also on $$$OX$$$-axis), such that the absolute difference between the distance from $$$O$$$ to $$$B$$$ and the distance from $$$A$$$ to $$$B$$$ is equal to $$$k$$$. The description of the first test case. Since sometimes it's impossible to find such point $$$B$$$, we can, in one step, increase or decrease the coordinate of $$$A$$$ by $$$1$$$. What is the minimum number of steps we should do to make such point $$$B$$$ exist? Input The first line contains one integer $$$t$$$ ($$$1 le t le 6000$$$)xa0— the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$0 le n, k le 10^6$$$)xa0— the initial position of point $$$A$$$ and desirable absolute difference. Output For each test case, print the minimum number of steps to make point $$$B$$$ exist. Example Input 6 4 0 5 8 0 1000000 0 0 1 0 1000000 1000000 Note In the first test case (picture above), if we set the coordinate of $$$B$$$ as $$$2$$$ then the absolute difference will be equal to $$$(2 - 0) - (4 - 2) = 0$$$ and we don't have to move $$$A$$$. So the answer is $$$0$$$. In the second test case, we can increase the coordinate of $$$A$$$ by $$$3$$$ and set the coordinate of $$$B$$$ as $$$0$$$ or $$$8$$$. The absolute difference will be equal to $$$8 - 0 = 8$$$, so the answer is $$$3$$$.
900
true
false
false
false
false
true
false
false
false
false
3,665
255A
Greg is a beginner bodybuilder. Today the gym coach gave him the training plan. All it had was _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_. These numbers mean that Greg needs to do exactly _n_ exercises today. Besides, Greg should repeat the _i_-th in order exercise _a__i_ times. Greg now only does three types of exercises: "chest" exercises, "biceps" exercises and "back" exercises. Besides, his training is cyclic, that is, the first exercise he does is a "chest" one, the second one is "biceps", the third one is "back", the fourth one is "chest", the fifth one is "biceps", and so on to the _n_-th exercise. Now Greg wonders, which muscle will get the most exercise during his training. We know that the exercise Greg repeats the maximum number of times, trains the corresponding muscle the most. Help Greg, determine which muscle will get the most training. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u200920). The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u200925) — the number of times Greg repeats the exercises. Output Print word "chest" (without the quotes), if the chest gets the most exercise, "biceps" (without the quotes), if the biceps gets the most exercise and print "back" (without the quotes) if the back gets the most exercise. It is guaranteed that the input is such that the answer to the problem is unambiguous. Note In the first sample Greg does 2 chest, 8 biceps and zero back exercises, so the biceps gets the most exercises. In the second sample Greg does 5 chest, 1 biceps and 10 back exercises, so the back gets the most exercises. In the third sample Greg does 18 chest, 12 biceps and 8 back exercises, so the chest gets the most exercise.
800
false
false
true
false
false
false
false
false
false
false
8,814
6B
President of Berland has a very vast office-room, where, apart from him, work his subordinates. Each subordinate, as well as President himself, has his own desk of a unique colour. Each desk is rectangular, and its sides are parallel to the office walls. One day President decided to establish an assembly, of which all his deputies will be members. Unfortunately, he does not remember the exact amount of his deputies, but he remembers that the desk of each his deputy is adjacent to his own desk, that is to say, the two desks (President's and each deputy's) have a common side of a positive length. The office-room plan can be viewed as a matrix with _n_ rows and _m_ columns. Each cell of this matrix is either empty, or contains a part of a desk. An uppercase Latin letter stands for each desk colour. The «period» character («.») stands for an empty cell. Input The first line contains two separated by a space integer numbers _n_, _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100) — the length and the width of the office-room, and _c_ character — the President's desk colour. The following _n_ lines contain _m_ characters each — the office-room description. It is guaranteed that the colour of each desk is unique, and each desk represents a continuous subrectangle of the given matrix. All colours are marked by uppercase Latin letters. Output Print the only number — the amount of President's deputies.
1,100
false
false
true
false
false
false
false
false
false
false
9,964
1117F
You are given a string of length $$$n$$$. Each character is one of the first $$$p$$$ lowercase Latin letters. You are also given a matrix $$$A$$$ with binary values of size $$$p imes p$$$. This matrix is symmetric ($$$A_{ij} = A_{ji}$$$). $$$A_{ij} = 1$$$ means that the string can have the $$$i$$$-th and $$$j$$$-th letters of Latin alphabet adjacent. Let's call the string crisp if all of the adjacent characters in it can be adjacent (have 1 in the corresponding cell of matrix $$$A$$$). You are allowed to do the following move. Choose any letter, remove all its occurrences and join the remaining parts of the string without changing their order. For example, removing letter 'a' from "abacaba" will yield "bcb". The string you are given is crisp. The string should remain crisp after every move you make. You are allowed to do arbitrary number of moves (possible zero). What is the shortest resulting string you can obtain? Input The first line contains two integers $$$n$$$ and $$$p$$$ ($$$1 le n le 10^5$$$, $$$1 le p le 17$$$) — the length of the initial string and the length of the allowed prefix of Latin alphabet. The second line contains the initial string. It is guaranteed that it contains only first $$$p$$$ lowercase Latin letters and that is it crisp. Some of these $$$p$$$ first Latin letters might not be present in the string. Each of the next $$$p$$$ lines contains $$$p$$$ integer numbers — the matrix $$$A$$$ ($$$0 le A_{ij} le 1$$$, $$$A_{ij} = A_{ji}$$$). $$$A_{ij} = 1$$$ means that the string can have the $$$i$$$-th and $$$j$$$-th letters of Latin alphabet adjacent. Output Print a single integer — the length of the shortest string after you make arbitrary number of moves (possible zero). Examples Input 7 3 abacaba 0 1 1 1 0 0 1 0 0 Input 7 3 abacaba 1 1 1 1 0 0 1 0 0 Input 7 4 bacadab 0 1 1 1 1 0 0 0 1 0 0 0 1 0 0 0 Input 3 3 cbc 0 0 0 0 0 1 0 1 0 Note In the first example no letter can be removed from the initial string. In the second example you can remove letters in order: 'b', 'c', 'a'. The strings on the intermediate steps will be: "abacaba" $$$ ightarrow$$$ "aacaa" $$$ ightarrow$$$ "aaaa" $$$ ightarrow$$$ "". In the third example you can remove letter 'b' and that's it. In the fourth example you can remove letters in order 'c', 'b', but not in the order 'b', 'c' because two letters 'c' can't be adjacent.
2,500
false
false
false
true
false
false
false
false
false
false
5,104
1175E
You are given $$$n$$$ intervals in form $$$[l; r]$$$ on a number line. You are also given $$$m$$$ queries in form $$$[x; y]$$$. What is the minimal number of intervals you have to take so that every point (not necessarily integer) from $$$x$$$ to $$$y$$$ is covered by at least one of them? If you can't choose intervals so that every point from $$$x$$$ to $$$y$$$ is covered, then print -1 for that query. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2 cdot 10^5$$$) — the number of intervals and the number of queries, respectively. Each of the next $$$n$$$ lines contains two integer numbers $$$l_i$$$ and $$$r_i$$$ ($$$0 le l_i < r_i le 5 cdot 10^5$$$) — the given intervals. Each of the next $$$m$$$ lines contains two integer numbers $$$x_i$$$ and $$$y_i$$$ ($$$0 le x_i < y_i le 5 cdot 10^5$$$) — the queries. Output Print $$$m$$$ integer numbers. The $$$i$$$-th number should be the answer to the $$$i$$$-th query: either the minimal number of intervals you have to take so that every point (not necessarily integer) from $$$x_i$$$ to $$$y_i$$$ is covered by at least one of them or -1 if you can't choose intervals so that every point from $$$x_i$$$ to $$$y_i$$$ is covered. Examples Input 2 3 1 3 2 4 1 3 1 4 3 4 Input 3 4 1 3 1 3 4 5 1 2 1 3 1 4 1 5 Note In the first example there are three queries: 1. query $$$[1; 3]$$$ can be covered by interval $$$[1; 3]$$$; 2. query $$$[1; 4]$$$ can be covered by intervals $$$[1; 3]$$$ and $$$[2; 4]$$$. There is no way to cover $$$[1; 4]$$$ by a single interval; 3. query $$$[3; 4]$$$ can be covered by interval $$$[2; 4]$$$. It doesn't matter that the other points are covered besides the given query. In the second example there are four queries: 1. query $$$[1; 2]$$$ can be covered by interval $$$[1; 3]$$$. Note that you can choose any of the two given intervals $$$[1; 3]$$$; 2. query $$$[1; 3]$$$ can be covered by interval $$$[1; 3]$$$; 3. query $$$[1; 4]$$$ can't be covered by any set of intervals; 4. query $$$[1; 5]$$$ can't be covered by any set of intervals. Note that intervals $$$[1; 3]$$$ and $$$[4; 5]$$$ together don't cover $$$[1; 5]$$$ because even non-integer points should be covered. Here $$$3.5$$$, for example, isn't covered.
2,200
false
true
true
true
true
false
false
false
false
false
4,841
1832C
For an array of integers $$$[a_1, a_2, dots, a_n]$$$, let's call the value $$$a_1-a_2+a_2-a_3+cdots+a_{n-1}-a_n$$$ the contrast of the array. Note that the contrast of an array of size $$$1$$$ is equal to $$$0$$$. You are given an array of integers $$$a$$$. Your task is to build an array of $$$b$$$ in such a way that all the following conditions are met: $$$b$$$ is not empty, i.e there is at least one element; $$$b$$$ is a subsequence of $$$a$$$, i.e $$$b$$$ can be produced by deleting some elements from $$$a$$$ (maybe zero); the contrast of $$$b$$$ is equal to the contrast of $$$a$$$. What is the minimum possible size of the array $$$b$$$? 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 a single integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$)xa0— the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, cdot, a_n$$$ ($$$0 le a_i le 10^9$$$)xa0— elements of the array itself. The sum of $$$n$$$ over all test cases doesn't exceed $$$3 cdot 10^5$$$. Output For each test case, print a single integerxa0— the minimum possible size of the array $$$b$$$. Example Input 4 5 1 3 3 3 7 2 4 2 4 1 1 1 1 7 5 4 2 1 0 0 4
1,200
false
true
true
false
false
false
false
false
false
false
1,296
805B
Problem - 805B - 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 *1000 No tag edit access → Contest materials xa0— the length of the string. Output Print the string that satisfies all the constraints. If there are multiple answers, print any of them. Examples Input 2 Output aa Input 3 Output bba Note A palindrome is a sequence of characters which reads the same backward and forward.
1,000
false
false
false
false
false
true
false
false
false
false
6,538
1284D
Filled with optimism, Hyunuk will host a conference about how great this new year will be! The conference will have $$$n$$$ lectures. Hyunuk has two candidate venues $$$a$$$ and $$$b$$$. For each of the $$$n$$$ lectures, the speaker specified two time intervals $$$[sa_i, ea_i]$$$ ($$$sa_i le ea_i$$$) and $$$[sb_i, eb_i]$$$ ($$$sb_i le eb_i$$$). If the conference is situated in venue $$$a$$$, the lecture will be held from $$$sa_i$$$ to $$$ea_i$$$, and if the conference is situated in venue $$$b$$$, the lecture will be held from $$$sb_i$$$ to $$$eb_i$$$. Hyunuk will choose one of these venues and all lectures will be held at that venue. Two lectures are said to overlap if they share any point in time in common. Formally, a lecture held in interval $$$[x, y]$$$ overlaps with a lecture held in interval $$$[u, v]$$$ if and only if $$$max(x, u) le min(y, v)$$$. We say that a participant can attend a subset $$$s$$$ of the lectures if the lectures in $$$s$$$ do not pairwise overlap (i.e. no two lectures overlap). Note that the possibility of attending may depend on whether Hyunuk selected venue $$$a$$$ or venue $$$b$$$ to hold the conference. A subset of lectures $$$s$$$ is said to be venue-sensitive if, for one of the venues, the participant can attend $$$s$$$, but for the other venue, the participant cannot attend $$$s$$$. A venue-sensitive set is problematic for a participant who is interested in attending the lectures in $$$s$$$ because the participant cannot be sure whether the lecture times will overlap. Hyunuk will be happy if and only if there are no venue-sensitive sets. Determine whether Hyunuk will be happy. Input The first line contains an integer $$$n$$$ ($$$1 le n le 100,000$$$), the number of lectures held in the conference. Each of the next $$$n$$$ lines contains four integers $$$sa_i$$$, $$$ea_i$$$, $$$sb_i$$$, $$$eb_i$$$ ($$$1 le sa_i, ea_i, sb_i, eb_i le 10^9$$$, $$$sa_i le ea_i, sb_i le eb_i$$$). Note In second example, lecture set $$${1, 3}$$$ is venue-sensitive. Because participant can't attend this lectures in venue $$$a$$$, but can attend in venue $$$b$$$. In first and third example, venue-sensitive set does not exist.
2,100
false
false
false
false
true
false
false
true
true
false
4,271
1081H
After learning some fancy algorithms about palindromes, Chouti found palindromes very interesting, so he wants to challenge you with this problem. Chouti has got two strings $$$A$$$ and $$$B$$$. Since he likes . The second line contains a single string $$$B$$$ ($$$1 le B le 2 cdot 10^5$$$). Strings $$$A$$$ and $$$B$$$ contain only lowercase English letters. Output The first and only line should contain a single integerxa0— the number of possible strings. Note In the first example, attainable strings are "a" + "a" = "aa", "aa" + "a" = "aaa", "aa" + "aba" = "aaaba", "aa" + "b" = "aab", "a" + "aba" = "aaba", "a" + "b" = "ab". In the second example, attainable strings are "aa", "aaa", "aaaa", "aaaba", "aab", "aaba", "ab", "abaa", "abaaa", "abaaba", "abab", "ba", "baa", "baba", "bb". Notice that though "a"+"aa"="aa"+"a"="aaa", "aaa" will only be counted once.
3,500
false
false
false
false
true
false
false
false
false
false
5,316
1176A
You are given an integer $$$n$$$. You can perform any of the following operations with this number an arbitrary (possibly, zero) number of times: 1. Replace $$$n$$$ with $$$frac{n}{2}$$$ if $$$n$$$ is divisible by $$$2$$$; 2. Replace $$$n$$$ with $$$frac{2n}{3}$$$ if $$$n$$$ is divisible by $$$3$$$; 3. Replace $$$n$$$ with $$$frac{4n}{5}$$$ if $$$n$$$ is divisible by $$$5$$$. For example, you can replace $$$30$$$ with $$$15$$$ using the first operation, with $$$20$$$ using the second operation or with $$$24$$$ using the third operation. Your task is to find the minimum number of moves required to obtain $$$1$$$ from $$$n$$$ or say that it is impossible to do it. You have to answer $$$q$$$ independent queries. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 1000$$$) — the number of queries. The next $$$q$$$ lines contain the queries. For each query you are given the integer number $$$n$$$ ($$$1 le n le 10^{18}$$$). Output Print the answer for each query on a new line. If it is impossible to obtain $$$1$$$ from $$$n$$$, print -1. Otherwise, print the minimum number of moves required to do it. Example Input 7 1 10 25 30 14 27 1000000000000000000
800
false
true
true
false
false
false
true
false
false
false
4,838
571B
Problem - 571B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp greedy sortings *2000 No tag edit access → Contest materials ). The second line contains _n_ integers _A_
2,000
false
true
false
true
false
false
false
false
true
false
7,565
886A
Problem - 886A - Codeforces =============== xa0 ") ") — scores of the participants Output Print "YES" (quotes for clarity), if it is possible to build teams with equal score, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Examples Input 1 3 2 1 2 1 Output YES Input 1 1 1 1 1 99 Output NO Note In the first sample, first team can be composed of 1st, 2nd and 6th participant, second — of 3rd, 4th and 5th: team scores are 1u2009+u20093u2009+u20091u2009=u20092u2009+u20091u2009+u20092u2009=u20095. In the second sample, score of participant number 6 is too high: his team score will be definitely greater.
1,000
false
false
false
false
false
false
true
false
false
false
6,208
174C
Polycarpus is an amateur programmer. Now he is analyzing a friend's program. He has already found there the function rangeIncrement(l, r), that adds 1 to each element of some array _a_ for all indexes in the segment [_l_,u2009_r_]. In other words, this function does the following: function rangeIncrement(l, r) for i := l .. r do a[i] = a[i] + 1 Polycarpus knows the state of the array _a_ after a series of function calls. He wants to determine the minimum number of function calls that lead to such state. In addition, he wants to find what function calls are needed in this case. It is guaranteed that the required number of calls does not exceed 105. Before calls of function rangeIncrement(l, r) all array elements equal zero. Input The first input line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the length of the array _a_[1... _n_]. The second line contains its integer space-separated elements, _a_[1],u2009_a_[2],u2009...,u2009_a_[_n_] (0u2009≤u2009_a_[_i_]u2009≤u2009105) after some series of function calls rangeIncrement(l, r). It is guaranteed that at least one element of the array is positive. It is guaranteed that the answer contains no more than 105 calls of function rangeIncrement(l, r). Output Print on the first line _t_ — the minimum number of calls of function rangeIncrement(l, r), that lead to the array from the input data. It is guaranteed that this number will turn out not more than 105. Then print _t_ lines — the descriptions of function calls, one per line. Each line should contain two integers _l__i_,u2009_r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_) — the arguments of the _i_-th call rangeIncrement(l, r). Calls can be applied in any order. If there are multiple solutions, you are allowed to print any of them. Note The first sample requires a call for the entire array, and four additional calls: one for the segment [2,2] (i.e. the second element of the array), three for the segment [5,5] (i.e. the fifth element of the array).
1,800
false
true
false
false
true
false
false
false
false
false
9,166
1875A
Flowey has planted a bomb in Snowdin! The bomb has a timer that is initially set to $$$b$$$. Every second, the timer will decrease by $$$1$$$. When the timer reaches $$$0$$$, the bomb will explode! To give the residents of Snowdin enough time to evacuate, you will need to delay the bomb from exploding for as long as possible. You have $$$n$$$ tools. Each tool can only be used at most once. If you use the $$$i$$$-th tool, the timer will increase by $$$x_i$$$. However, if the timer is changed to an integer larger than $$$a$$$, the timer will be set to $$$a$$$ due to a bug. More specifically, the following events will happen every second in the following order: 1. You will choose some (possibly none) of your tools that have not been used before. If you choose the $$$i$$$-th tool, and the bomb's timer is currently set to $$$c$$$, the timer will be changed to $$$min(c + x_i, a)$$$. 2. The timer decreases by $$$1$$$. 3. If the timer reaches $$$0$$$, the bomb explodes. Jellyfish now wants to know the maximum time in seconds until the bomb explodes if the tools are used optimally. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 2000$$$). The description of the test cases follows. The first line of each test case contains three integers $$$a$$$, $$$b$$$ and $$$n$$$ ($$$1 leq b leq a leq 10^9$$$, $$$1 leq n leq 100$$$)xa0— the maximum value of the bomb's timer, the initial value of the timer of the bomb and the number of tools. The second line of each test contains $$$n$$$ integers $$$x_1, x_2, dots, x_n$$$ ($$$1 leq x_i leq 10^9$$$)xa0— the number the timer can increase by using the $$$i$$$-th tool. Note that the sum of $$$n$$$ over all test cases is not bounded. Output For each test case, output a single integerxa0— the maximum time in seconds until the bomb explodes. Example Input 2 5 3 3 1 1 7 7 1 5 1 2 5 6 8 Note Let $$$c$$$ denote the value of the bomb's timer. In the first test case: Second $$$1$$$: choose tool $$$1$$$ and $$$2$$$ at this second, then $$$c=5$$$; the timer decreases by $$$1$$$, then $$$c=4$$$. Second $$$2$$$: the timer decreases by $$$1$$$, then $$$c=3$$$. Second $$$3$$$: the timer decreases by $$$1$$$, then $$$c=2$$$. Second $$$4$$$: the timer decreases by $$$1$$$, then $$$c=1$$$. Second $$$5$$$: choose tool $$$3$$$, then $$$c=5$$$; the timer decreases by $$$1$$$, then $$$c=4$$$. Second $$$6$$$: the timer decreases by $$$1$$$, then $$$c=3$$$. Second $$$7$$$: the timer decreases by $$$1$$$, then $$$c=2$$$. Second $$$8$$$: the timer decreases by $$$1$$$, then $$$c=1$$$. Second $$$9$$$: the timer decreases by $$$1$$$, then $$$c=0$$$. The bomb explodes. It can be proved that there is no way to use the tools such that the bomb explodes after more than $$$9$$$ seconds.
900
false
true
false
false
false
false
true
false
false
false
1,029
1520C
Problem - 1520C - 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 *1000 No tag edit access → Contest materials $$$ cells $$$(r, c-1)$$$, $$$(r, c+1)$$$, $$$(r-1, c)$$$ and $$$(r+1, c)$$$ are adjacent to it. For a given number $$$n$$$, construct a square matrix $$$n imes n$$$ such that: Each integer from $$$1$$$ to $$$n^2$$$ occurs in this matrix exactly once; If $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ are adjacent cells, then the numbers written in them must not be adjacent. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$). Then $$$t$$$ test cases follow. Each test case is characterized by one integer $$$n$$$ ($$$1 le n le 100$$$). Output For each test case, output: -1, if the required matrix does not exist; the required matrix, otherwise (any such matrix if many of them exist). The matrix should be outputted as $$$n$$$ lines, where each line contains $$$n$$$ integers. Example Input 3 1 2 3 Output 1 -1 2 9 7 4 6 3 1 8 5
1,000
false
false
false
false
false
true
false
false
false
false
3,061
1185E
After a hard-working week Polycarp prefers to have fun. Polycarp's favorite entertainment is drawing snakes. He takes a rectangular checkered sheet of paper of size $$$n imes m$$$ (where $$$n$$$ is the number of rows, $$$m$$$ is the number of columns) and starts to draw snakes in cells. Polycarp draws snakes with lowercase Latin letters. He always draws the first snake with the symbol 'a', the second snake with the symbol 'b', the third snake with the symbol 'c' and so on. All snakes have their own unique symbol. There are only $$$26$$$ letters in the Latin alphabet, Polycarp is very tired and he doesn't want to invent new symbols, so the total number of drawn snakes doesn't exceed $$$26$$$. Since by the end of the week Polycarp is very tired, he draws snakes as straight lines without bends. So each snake is positioned either vertically or horizontally. Width of any snake equals $$$1$$$, i.e. each snake has size either $$$1 imes l$$$ or $$$l imes 1$$$, where $$$l$$$ is snake's length. Note that snakes can't bend. When Polycarp draws a new snake, he can use already occupied cells for drawing the snake. In this situation, he draws the snake "over the top" and overwrites the previous value in the cell. Recently when Polycarp was at work he found a checkered sheet of paper with Latin letters. He wants to know if it is possible to get this sheet of paper from an empty sheet by drawing some snakes according to the rules described above. If it is possible, he is interested in a way to draw snakes. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^5$$$) — the number of test cases to solve. Then $$$t$$$ test cases follow. The first line of the test case description contains two integers $$$n$$$, $$$m$$$ ($$$1 le n,m le 2000$$$)xa0— length and width of the checkered sheet of paper respectively. Next $$$n$$$ lines of test case description contain $$$m$$$ symbols, which are responsible for the content of the corresponding cell on the sheet. It can be either lowercase Latin letter or symbol dot ('.'), which stands for an empty cell. It is guaranteed that the total area of all sheets in one test doesn't exceed $$$4cdot10^6$$$. Output Print the answer for each test case in the input. In the first line of the output for a test case print YES if it is possible to draw snakes, so that you can get a sheet of paper from the input. If it is impossible, print NO. If the answer to this question is positive, then print the way to draw snakes in the following format. In the next line print one integer $$$k$$$ ($$$0 le k le 26$$$)xa0— number of snakes. Then print $$$k$$$ lines, in each line print four integers $$$r_{1,i}$$$, $$$c_{1,i}$$$, $$$r_{2,i}$$$ and $$$c_{2,i}$$$xa0— coordinates of extreme cells for the $$$i$$$-th snake ($$$1 le r_{1,i}, r_{2,i} le n$$$, $$$1 le c_{1,i}, c_{2,i} le m$$$). Snakes should be printed in order of their drawing. If there are multiple solutions, you are allowed to print any of them. Note that Polycarp starts drawing of snakes with an empty sheet of paper. Examples Input 1 5 6 ...a.. ..bbb. ...a.. .cccc. ...a.. Output YES 3 1 4 5 4 2 3 2 5 4 2 4 5 Input 3 3 3 ... ... ... 4 4 ..c. adda bbcb .... 3 5 ..b.. aaaaa ..b.. Output YES 0 YES 4 2 1 2 4 3 1 3 4 1 3 3 3 2 2 2 3 NO Input 2 3 3 ... .a. ... 2 2 bb cc Output YES 1 2 2 2 2 YES 3 1 1 1 2 1 1 1 2 2 1 2 2
2,000
false
false
true
false
false
false
true
false
false
false
4,775
1066B
Vova's house is an array consisting of $$$n$$$ elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The $$$i$$$-th element of the array is $$$1$$$ if there is a heater in the position $$$i$$$, otherwise the $$$i$$$-th element of the array is $$$0$$$. Each heater has a value $$$r$$$ ($$$r$$$ is the same for all heaters). This value means that the heater at the position $$$pos$$$ can warm up all the elements in range $$$[pos - r + 1; pos + r - 1]$$$. Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater. Vova's target is to warm up the whole house (all the elements of the array), i.e. if $$$n = 6$$$, $$$r = 2$$$ and heaters are at positions $$$2$$$ and $$$5$$$, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first $$$3$$$ elements will be warmed up by the first heater and the last $$$3$$$ elements will be warmed up by the second heater). Initially, all the heaters are off. But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater. Your task is to find this number of heaters or say that it is impossible to warm up the whole house. Input The first line of the input contains two integers $$$n$$$ and $$$r$$$ ($$$1 le n, r le 1000$$$) — the number of elements in the array and the value of heaters. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 1$$$) — the Vova's house description. Note In the first example the heater at the position $$$2$$$ warms up elements $$$[1; 3]$$$, the heater at the position $$$3$$$ warms up elements $$$[2, 4]$$$ and the heater at the position $$$6$$$ warms up elements $$$[5; 6]$$$ so the answer is $$$3$$$. In the second example the heater at the position $$$1$$$ warms up elements $$$[1; 3]$$$ and the heater at the position $$$5$$$ warms up elements $$$[3; 5]$$$ so the answer is $$$2$$$. In the third example there are no heaters so the answer is -1. In the fourth example the heater at the position $$$3$$$ warms up elements $$$[1; 5]$$$, the heater at the position $$$6$$$ warms up elements $$$[4; 8]$$$ and the heater at the position $$$10$$$ warms up elements $$$[8; 10]$$$ so the answer is $$$3$$$.
1,500
false
true
false
false
false
false
false
false
false
false
5,380
1503A
A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not. You are given a binary string $$$s$$$ of length $$$n$$$. Construct two balanced bracket sequences $$$a$$$ and $$$b$$$ of length $$$n$$$ such that for all $$$1le ile n$$$: if $$$s_i=1$$$, then $$$a_i=b_i$$$ if $$$s_i=0$$$, then $$$a_i e b_i$$$ If it is impossible, you should report about it. Input The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2le nle 2cdot 10^5$$$, $$$n$$$ is even). The next line contains a string $$$s$$$ of length $$$n$$$, consisting of characters 0 and 1. The sum of $$$n$$$ across all test cases does not exceed $$$2cdot 10^5$$$. Output If such two balanced bracked sequences exist, output "YES" on the first line, otherwise output "NO". You can print each letter in any case (upper or lower). If the answer is "YES", output the balanced bracket sequences $$$a$$$ and $$$b$$$ satisfying the conditions on the next two lines. If there are multiple solutions, you may print any. Example Input 3 6 101101 10 1001101101 4 1100 Output YES ()()() ((())) YES ()()((())) (())()()() NO Note In the first test case, $$$a=$$$"()()()" and $$$b=$$$"((()))". The characters are equal in positions $$$1$$$, $$$3$$$, $$$4$$$, and $$$6$$$, which are the exact same positions where $$$s_i=1$$$. In the second test case, $$$a=$$$"()()((()))" and $$$b=$$$"(())()()()". The characters are equal in positions $$$1$$$, $$$4$$$, $$$5$$$, $$$7$$$, $$$8$$$, $$$10$$$, which are the exact same positions where $$$s_i=1$$$. In the third test case, there is no solution.
1,600
false
true
false
false
false
true
false
false
false
false
3,160
1658E
Marin feels exhausted after a long day of cosplay, so Gojou invites her to play a game! Marin and Gojou take turns to place one of their tokens on an $$$n imes n$$$ grid with Marin starting first. There are some restrictions and allowances on where to place tokens: Apart from the first move, the token placed by a player must be more than Manhattan distance $$$k$$$ away from the previous token placed on the matrix. In other words, if a player places a token at $$$(x_1, y_1)$$$, then the token placed by the other player in the next move must be in a cell $$$(x_2, y_2)$$$ satisfying $$$x_2 - x_1 + y_2 - y_1 > k$$$. Apart from the previous restriction, a token can be placed anywhere on the matrix, including cells where tokens were previously placed by any player. Whenever a player places a token on cell $$$(x, y)$$$, that player gets $$$v_{x, y}$$$ points. All values of $$$v$$$ on the grid are distinct. You still get points from a cell even if tokens were already placed onto the cell. The game finishes when each player makes $$$10^{100}$$$ moves. Marin and Gojou will play $$$n^2$$$ games. For each cell of the grid, there will be exactly one game where Marin places a token on that cell on her first move. Please answer for each game, if Marin and Gojou play optimally (after Marin's first move), who will have more points at the end? Or will the game end in a draw (both players have the same points at the end)? Input The first line contains two integers $$$n$$$, $$$k$$$ ($$$3 le n le 2000$$$, $$$1 leq k leq n - 2$$$). Note that under these constraints it is always possible to make a move. The following $$$n$$$ lines contains $$$n$$$ integers each. The $$$j$$$-th integer in the $$$i$$$-th line is $$$v_{i,j}$$$ ($$$1 le v_{i,j} le n^2$$$). All elements in $$$v$$$ are distinct. Output You should print $$$n$$$ lines. In the $$$i$$$-th line, print $$$n$$$ characters, where the $$$j$$$-th character is the result of the game in which Marin places her first token in the cell $$$(i, j)$$$. Print 'M' if Marin wins, 'G' if Gojou wins, and 'D' if the game ends in a draw. Do not print spaces between the characters in one line. Example Input 3 1 1 2 4 6 8 3 9 5 7
2,500
true
false
true
true
true
false
false
false
true
false
2,334
2028B
Alice mixed up the words transmutation and permutation! She has an array $$$a$$$ specified via three integers $$$n$$$, $$$b$$$, $$$c$$$: the array $$$a$$$ has length $$$n$$$ and is given via $$$a_i = bcdot (i - 1) + c$$$ for $$$1le ile n$$$. For example, if $$$n=3$$$, $$$b=2$$$, and $$$c=1$$$, then $$$a=[2 cdot 0 + 1, 2 cdot 1 + 1, 2 cdot 2 + 1] = [1, 3, 5]$$$. Now, Alice really enjoys permutations of $$$[0, ldots, n-1]$$$$$$^{ ext{∗}}$$$ and would like to transform $$$a$$$ into a permutation. In one operation, Alice replaces the maximum element of $$$a$$$ with the $$$operatorname{MEX}$$$$$$^{ ext{†}}$$$ of $$$a$$$. If there are multiple maximum elements in $$$a$$$, Alice chooses the leftmost one to replace. Can you help Alice figure out how many operations she has to do for $$$a$$$ to become a permutation for the first time? If it is impossible, you should report it. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). The description of the test cases follows. The only line of each test case contains three integers $$$n$$$, $$$b$$$, $$$c$$$ ($$$1le nle 10^{18}$$$; $$$0le b$$$, $$$cle 10^{18}$$$) — the parameters of the array. Output For each test case, if the array can never become a permutation, output $$$-1$$$. Otherwise, output the minimum number of operations for the array to become a permutation. Example Input 7 10 1 0 1 2 3 100 2 1 3 0 1 3 0 0 1000000000000000000 0 0 1000000000000000000 1000000000000000000 1000000000000000000 Output 0 1 50 2 -1 -1 1000000000000000000 Note In the first test case, the array is already $$$[0, 1, ldots, 9]$$$, so no operations are required. In the third test case, the starting array is $$$[1, 3, 5, ldots, 199]$$$. After the first operation, the $$$199$$$ gets transformed into a $$$0$$$. In the second operation, the $$$197$$$ gets transformed into a $$$2$$$. If we continue this, it will take exactly $$$50$$$ operations to get the array $$$[0, 1, 2, 3, ldots, 99]$$$. In the fourth test case, two operations are needed: $$$[1,1,1] o [0,1,1] o [0,2,1]$$$. In the fifth test case, the process is $$$[0,0,0] o [1,0,0] o [2,0,0] o [1,0,0] o [2,0,0]$$$. This process repeats forever, so the array is never a permutation and the answer is $$$-1$$$.
1,400
true
false
true
false
false
false
false
true
false
false
102
32B
Problem - 32B - 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 expression parsing implementation *800 No tag edit access → Contest materials ") . Output Output the decoded ternary number. It can have leading zeroes. Examples Input .-.-- Output 012 Input --. Output 20 Input -..-.-- Output 1012
800
false
false
true
false
false
false
false
false
false
false
9,837
1583C
The problem statement looms below, filling you with determination. Consider a grid in which some cells are empty and some cells are filled. Call a cell in this grid exitable if, starting at that cell, you can exit the grid by moving up and left through only empty cells. This includes the cell itself, so all filled in cells are not exitable. Note that you can exit the grid from any leftmost empty cell (cell in the first column) by going left, and from any topmost empty cell (cell in the first row) by going up. Let's call a grid determinable if, given only which cells are exitable, we can exactly determine which cells are filled in and which aren't. You are given a grid $$$a$$$ of dimensions $$$n imes m$$$ , i. e. a grid with $$$n$$$ rows and $$$m$$$ columns. You need to answer $$$q$$$ queries ($$$1 leq q leq 2 cdot 10^5$$$). Each query gives two integers $$$x_1, x_2$$$ ($$$1 leq x_1 leq x_2 leq m$$$) and asks whether the subgrid of $$$a$$$ consisting of the columns $$$x_1, x_1 + 1, ldots, x_2 - 1, x_2$$$ is determinable. Input The first line contains two integers $$$n, m$$$ ($$$1 leq n, m leq 10^6$$$, $$$nm leq 10^6$$$) xa0— the dimensions of the grid $$$a$$$. $$$n$$$ lines follow. The $$$y$$$-th line contains $$$m$$$ characters, the $$$x$$$-th of which is 'X' if the cell on the intersection of the the $$$y$$$-th row and $$$x$$$-th column is filled and "." if it is empty. The next line contains a single integer $$$q$$$ ($$$1 leq q leq 2 cdot 10^5$$$) xa0— the number of queries. $$$q$$$ lines follow. Each line contains two integers $$$x_1$$$ and $$$x_2$$$ ($$$1 leq x_1 leq x_2 leq m$$$), representing a query asking whether the subgrid of $$$a$$$ containing the columns $$$x_1, x_1 + 1, ldots, x_2 - 1, x_2$$$ is determinable. Output For each query, output one line containing "YES" if the subgrid specified by the query is determinable and "NO" otherwise. The output is case insensitive (so "yEs" and "No" will also be accepted). Example Input 4 5 ..XXX ...X. ...X. ...X. 5 1 3 3 3 4 5 5 5 1 5 Note For each query of the example, the corresponding subgrid is displayed twice below: first in its input format, then with each cell marked as "E" if it is exitable and "N" otherwise. For the first query: ..X EEN ... EEE ... EEE ... EEE For the second query: X N . E . E . E Note that you can exit the grid by going left from any leftmost cell (or up from any topmost cell); you do not need to reach the top left corner cell to exit the grid. For the third query: XX NN X. NN X. NN X. NN This subgrid cannot be determined only from whether each cell is exitable, because the below grid produces the above "exitability grid" as well: XX XX XX XX For the fourth query: X N . E . E . E For the fifth query: ..XXX EENNN ...X. EEENN ...X. EEENN ...X. EEENN This query is simply the entire grid. It cannot be determined only from whether each cell is exitable because the below grid produces the above "exitability grid" as well: ..XXX ...XX ...XX ...XX
1,700
false
false
false
true
true
false
false
false
false
false
2,715
148B
The princess is going to escape the dragon's cave, and she needs to plan it carefully. The princess runs at _v__p_ miles per hour, and the dragon flies at _v__d_ miles per hour. The dragon will discover the escape after _t_ hours and will chase the princess immediately. Looks like there's no chance to success, but the princess noticed that the dragon is very greedy and not too smart. To delay him, the princess decides to borrow a couple of bijous from his treasury. Once the dragon overtakes the princess, she will drop one bijou to distract him. In this case he will stop, pick up the item, return to the cave and spend _f_ hours to straighten the things out in the treasury. Only after this will he resume the chase again from the very beginning. The princess is going to run on the straight. The distance between the cave and the king's castle she's aiming for is _c_ miles. How many bijous will she need to take from the treasury to be able to reach the castle? If the dragon overtakes the princess at exactly the same moment she has reached the castle, we assume that she reached the castle before the dragon reached her, and doesn't need an extra bijou to hold him off. Input The input data contains integers _v__p_,u2009_v__d_,u2009_t_,u2009_f_ and _c_, one per line (1u2009≤u2009_v__p_,u2009_v__d_u2009≤u2009100, 1u2009≤u2009_t_,u2009_f_u2009≤u200910, 1u2009≤u2009_c_u2009≤u20091000). Output Output the minimal number of bijous required for the escape to succeed. Note In the first case one hour after the escape the dragon will discover it, and the princess will be 1 mile away from the cave. In two hours the dragon will overtake the princess 2 miles away from the cave, and she will need to drop the first bijou. Return to the cave and fixing the treasury will take the dragon two more hours; meanwhile the princess will be 4 miles away from the cave. Next time the dragon will overtake the princess 8 miles away from the cave, and she will need the second bijou, but after this she will reach the castle without any further trouble. The second case is similar to the first one, but the second time the dragon overtakes the princess when she has reached the castle, and she won't need the second bijou.
1,500
true
false
true
false
false
false
false
false
false
false
9,285
1234E
Let's define $$$p_i(n)$$$ as the following permutation: $$$[i, 1, 2, dots, i - 1, i + 1, dots, n]$$$. This means that the $$$i$$$-th permutation is almost identity (i.e. which maps every element to itself) permutation but the element $$$i$$$ is on the first position. Examples: $$$p_1(4) = [1, 2, 3, 4]$$$; $$$p_2(4) = [2, 1, 3, 4]$$$; $$$p_3(4) = [3, 1, 2, 4]$$$; $$$p_4(4) = [4, 1, 2, 3]$$$. You are given an array $$$x_1, x_2, dots, x_m$$$ ($$$1 le x_i le n$$$). Let $$$pos(p, val)$$$ be the position of the element $$$val$$$ in $$$p$$$. So, $$$pos(p_1(4), 3) = 3, pos(p_2(4), 2) = 1, pos(p_4(4), 4) = 1$$$. Let's define a function $$$f(p) = sumlimits_{i=1}^{m - 1} pos(p, x_i) - pos(p, x_{i + 1})$$$, where $$$val$$$ is the absolute value of $$$val$$$. This function means the sum of distances between adjacent elements of $$$x$$$ in $$$p$$$. Your task is to calculate $$$f(p_1(n)), f(p_2(n)), dots, f(p_n(n))$$$. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n, m le 2 cdot 10^5$$$) — the number of elements in each permutation and the number of elements in $$$x$$$. The second line of the input contains $$$m$$$ integers ($$$m$$$, not $$$n$$$) $$$x_1, x_2, dots, x_m$$$ ($$$1 le x_i le n$$$), where $$$x_i$$$ is the $$$i$$$-th element of $$$x$$$. Elements of $$$x$$$ can repeat and appear in arbitrary order. Output Print $$$n$$$ integers: $$$f(p_1(n)), f(p_2(n)), dots, f(p_n(n))$$$. Note Consider the first example: $$$x = [1, 2, 3, 4]$$$, so for the permutation $$$p_1(4) = [1, 2, 3, 4]$$$ the answer is $$$1 - 2 + 2 - 3 + 3 - 4 = 3$$$; for the permutation $$$p_2(4) = [2, 1, 3, 4]$$$ the answer is $$$2 - 1 + 1 - 3 + 3 - 4 = 4$$$; for the permutation $$$p_3(4) = [3, 1, 2, 4]$$$ the answer is $$$2 - 3 + 3 - 1 + 1 - 4 = 6$$$; for the permutation $$$p_4(4) = [4, 1, 2, 3]$$$ the answer is $$$2 - 3 + 3 - 4 + 4 - 1 = 5$$$. Consider the second example: $$$x = [2, 1, 5, 3, 5]$$$, so for the permutation $$$p_1(5) = [1, 2, 3, 4, 5]$$$ the answer is $$$2 - 1 + 1 - 5 + 5 - 3 + 3 - 5 = 9$$$; for the permutation $$$p_2(5) = [2, 1, 3, 4, 5]$$$ the answer is $$$1 - 2 + 2 - 5 + 5 - 3 + 3 - 5 = 8$$$; for the permutation $$$p_3(5) = [3, 1, 2, 4, 5]$$$ the answer is $$$3 - 2 + 2 - 5 + 5 - 1 + 1 - 5 = 12$$$; for the permutation $$$p_4(5) = [4, 1, 2, 3, 5]$$$ the answer is $$$3 - 2 + 2 - 5 + 5 - 4 + 4 - 5 = 6$$$; for the permutation $$$p_5(5) = [5, 1, 2, 3, 4]$$$ the answer is $$$3 - 2 + 2 - 1 + 1 - 4 + 4 - 1 = 8$$$.
2,000
true
false
false
false
false
false
false
false
false
false
4,520
922A
Problem - 922A - 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 *1300 No tag edit access → Contest materials ") xa0— the number of copies and the number of original toys Imp wants to get (including the initial one). Output Print "Yes", if the desired configuration is possible, and "No" otherwise. You can print each letter in arbitrary case (upper or lower). Examples Input 6 3 Output Yes Input 4 2 Output No Input 1000 1001 Output Yes Note In the first example, Imp has to apply the machine twice to original toys and then twice to copies.
1,300
false
false
true
false
false
false
false
false
false
false
6,029
1760F
There are $$$n$$$ quests. If you complete the $$$i$$$-th quest, you will gain $$$a_i$$$ coins. You can only complete at most one quest per day. However, once you complete a quest, you cannot do the same quest again for $$$k$$$ days. (For example, if $$$k=2$$$ and you do quest $$$1$$$ on day $$$1$$$, then you cannot do it on day $$$2$$$ or $$$3$$$, but you can do it again on day $$$4$$$.) You are given two integers $$$c$$$ and $$$d$$$. Find the maximum value of $$$k$$$ such that you can gain at least $$$c$$$ coins over $$$d$$$ days. If no such $$$k$$$ exists, output Impossible. If $$$k$$$ can be arbitrarily large, output Infinity. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains three integers $$$n,c,d$$$ ($$$2 leq n leq 2cdot10^5$$$; $$$1 leq c leq 10^{16}$$$; $$$1 leq d leq 2cdot10^5$$$)xa0— the number of quests, the number of coins you need, and the number of days. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the rewards for the quests. The sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$, and the sum of $$$d$$$ over all test cases does not exceed $$$2cdot10^5$$$. Output For each test case, output one of the following. If no such $$$k$$$ exists, output Impossible. If $$$k$$$ can be arbitrarily large, output Infinity. Otherwise, output a single integerxa0— the maximum value of $$$k$$$ such that you can gain at least $$$c$$$ coins over $$$d$$$ days. Please note, the checker is case-sensitive, and you should output strings exactly as they are given. Example Input 6 2 5 4 1 2 2 20 10 100 10 3 100 3 7 2 6 4 20 3 4 5 6 7 4 100000000000 2022 8217734 927368 26389746 627896974 2 20 4 5 1 Output 2 Infinity Impossible 1 12 0 Note In the first test case, one way to earn $$$5$$$ coins over $$$4$$$ days with $$$k=2$$$ is as follows: Day 1: do quest 2, and earn $$$2$$$ coins. Day 2: do quest 1, and earn $$$1$$$ coin. Day 3: do nothing. Day 4: do quest 2, and earn $$$2$$$ coins. In total, we earned $$$2+1+2=5$$$ coins. In the second test case, we can make over $$$20$$$ coins on the first day itself by doing the first quest to earn $$$100$$$ coins, so the value of $$$k$$$ can be arbitrarily large, since we never need to do another quest. In the third test case, no matter what we do, we can't earn $$$100$$$ coins over $$$3$$$ days.
1,500
false
true
false
false
false
false
false
true
true
false
1,750
764A
Problem - 764A - 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 implementation math *800 No tag edit access → Contest materials ") . Output Print single integerxa0— the minimum number of artists that should be killed so that there are no artists in the room when Ilia calls. Examples Input 1 1 10 Output 10 Input 1 2 5 Output 2 Input 2 3 9 Output 1 Note Taymyr is a place in the north of Russia. In the first test the artists come each minute, as well as the calls, so we need to kill all of them. In the second test we need to kill artists which come on the second and the fourth minutes. In the third testxa0— only the artist which comes on the sixth minute.
800
true
false
true
false
false
false
true
false
false
false
6,732
741D
Problem - 741D - 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 dfs and similar trees *2900 No tag edit access → Contest materials consisting of _n_ vertices. The vertices are numbered 1 through _n_, the vertex 1 is the root. There is a letter written on each edge of this tree. Mehrdad is a fan of Dokhtar-kosh things. He call a string Dokhtar-kosh, if we can shuffle the characters in string such that it becomes palindrome. He asks Arpa, for each vertex _v_, what is the length of the longest simple path in subtree of _v_ that form a Dokhtar-kosh string. Input The first line contains integer _n_ (1u2009u2009≤u2009u2009_n_u2009u2009≤u2009u20095·105)xa0— the number of vertices in the tree. (_n_u2009u2009-u2009u20091) lines follow, the _i_-th of them contain an integer _p__i_u2009+u20091 and a letter _c__i_u2009+u20091 (1u2009u2009≤u2009u2009_p__i_u2009+u20091u2009u2009≤u2009u2009_i_, _c__i_u2009+u20091 is lowercase English letter, between a and v, inclusively), that mean that there is an edge between nodes _p__i_u2009+u20091 and _i_u2009+u20091 and there is a letter _c__i_u2009+u20091 written on this edge. Output Print _n_ integers. The _i_-th of them should be the length of the longest simple path in subtree of the _i_-th vertex that form a Dokhtar-kosh string. Examples Input 4 1 s 2 a 3 s Output 3 1 1 0 Input 5 1 a 2 h 1 a 4 h Output 4 1 0 1 0
2,900
false
false
false
false
true
false
false
false
false
false
6,833
1175B
You are given a function $$$f$$$ written in some basic language. The function accepts an integer value, which is immediately written into some variable $$$x$$$. $$$x$$$ is an integer variable and can be assigned values from $$$0$$$ to $$$2^{32}-1$$$. The function contains three types of commands: for $$$n$$$ — for loop; end — every command between "for $$$n$$$" and corresponding "end" is executed $$$n$$$ times; add — adds 1 to $$$x$$$. After the execution of these commands, value of $$$x$$$ is returned. Every "for $$$n$$$" is matched with "end", thus the function is guaranteed to be valid. "for $$$n$$$" can be immediately followed by "end"."add" command can be outside of any for loops. Notice that "add" commands might overflow the value of $$$x$$$! It means that the value of $$$x$$$ becomes greater than $$$2^{32}-1$$$ after some "add" command. Now you run $$$f(0)$$$ and wonder if the resulting value of $$$x$$$ is correct or some overflow made it incorrect. If overflow happened then output "OVERFLOW!!!", otherwise print the resulting value of $$$x$$$. Input The first line contains a single integer $$$l$$$ ($$$1 le l le 10^5$$$) — the number of lines in the function. Each of the next $$$l$$$ lines contains a single command of one of three types: for $$$n$$$ ($$$1 le n le 100$$$) — for loop; end — every command between "for $$$n$$$" and corresponding "end" is executed $$$n$$$ times; add — adds 1 to $$$x$$$. Output If overflow happened during execution of $$$f(0)$$$, then output "OVERFLOW!!!", otherwise print the resulting value of $$$x$$$. Examples Input 9 add for 43 end for 10 for 15 add end add end Input 11 for 100 for 100 for 100 for 100 for 100 add end end end end end Note In the first example the first "add" is executed 1 time, the second "add" is executed 150 times and the last "add" is executed 10 times. Note that "for $$$n$$$" can be immediately followed by "end" and that "add" can be outside of any for loops. In the second example there are no commands "add", thus the returning value is 0. In the third example "add" command is executed too many times, which causes $$$x$$$ to go over $$$2^{32}-1$$$.
1,600
false
false
true
false
true
false
false
false
false
false
4,844
1843A
Sasha found an array $$$a$$$ consisting of $$$n$$$ integers and asked you to paint elements. You have to paint each element of the array. You can use as many colors as you want, but each element should be painted into exactly one color, and for each color, there should be at least one element of that color. The cost of one color is the value of $$$max(S) - min(S)$$$, where $$$S$$$ is the sequence of elements of that color. The cost of the whole coloring is the sum of costs over all colors. For example, suppose you have an array $$$a = [color{red}{1}, color{red}{5}, color{blue}{6}, color{blue}{3}, color{red}{4}]$$$, and you painted its elements into two colors as follows: elements on positions $$$1$$$, $$$2$$$ and $$$5$$$ have color $$$1$$$; elements on positions $$$3$$$ and $$$4$$$ have color $$$2$$$. Then: the cost of the color $$$1$$$ is $$$max([1, 5, 4]) - min([1, 5, 4]) = 5 - 1 = 4$$$; the cost of the color $$$2$$$ is $$$max([6, 3]) - min([6, 3]) = 6 - 3 = 3$$$; the total cost of the coloring is $$$7$$$. For the given array $$$a$$$, you have to calculate the maximum possible cost of the coloring. Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 50$$$)xa0— length of $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq 50$$$)xa0— array $$$a$$$. Output For each test case output the maximum possible cost of the coloring. Example Input 6 5 1 5 6 3 4 1 5 4 1 6 3 9 6 1 13 9 3 7 2 4 2 2 2 2 5 4 5 2 2 3 Note In the first example one of the optimal coloring is $$$[color{red}{1}, color{red}{5}, color{blue}{6}, color{blue}{3}, color{red}{4}]$$$. The answer is $$$(5 - 1) + (6 - 3) = 7$$$. In the second example, the only possible coloring is $$$[color{blue}{5}]$$$, for which the answer is $$$5 - 5 = 0$$$. In the third example, the optimal coloring is $$$[color{blue}{1}, color{red}{6}, color{red}{3}, color{blue}{9}]$$$, the answer is $$$(9 - 1) + (6 - 3) = 11$$$.
800
false
true
false
false
false
false
false
false
true
false
1,230
1646F
There are $$$n$$$ players, numbered from $$$1$$$ to $$$n$$$ sitting around a round table. The $$$(i+1)$$$-th player sits to the right of the $$$i$$$-th player for $$$1 le i < n$$$, and the $$$1$$$-st player sits to the right of the $$$n$$$-th player. There are $$$n^2$$$ cards, each of which has an integer between $$$1$$$ and $$$n$$$ written on it. For each integer from $$$1$$$ to $$$n$$$, there are exactly $$$n$$$ cards having this number. Initially, all these cards are distributed among all the players, in such a way that each of them has exactly $$$n$$$ cards. In one operation, each player chooses one of his cards and passes it to the player to his right. All these actions are performed simultaneously. Player $$$i$$$ is called solid if all his cards have the integer $$$i$$$ written on them. Their objective is to reach a configuration in which everyone is solid. Find a way to do it using at most $$$(n^2-n)$$$ operations. You do not need to minimize the number of operations. Input The first line contains a single integer $$$n$$$ ($$$2le nle 100$$$). Then $$$n$$$ lines follow. The $$$i$$$-th of them contains $$$n$$$ integers $$$c_1, c_2, ldots, c_n$$$ ($$$1le c_jle n$$$)xa0— the initial cards of the $$$i$$$-th player. It is guaranteed that for each integer $$$i$$$ from $$$1$$$ to $$$n$$$, there are exactly $$$n$$$ cards having the number $$$i$$$. Output In the first line print an integer $$$k$$$ ($$$0le kle (n^2-n)$$$)xa0— the numbers of operations you want to make. Then $$$k$$$ lines should follow. In the $$$i$$$-th of them print $$$n$$$ integers $$$d_1, d_2, ldots, d_n$$$ ($$$1le d_jle n$$$) where $$$d_j$$$ is the number written on the card which $$$j$$$-th player passes to the player to his right in the $$$i$$$-th operation. We can show that an answer always exists under the given constraints. If there are multiple answers, print any. Examples Output 6 1 2 3 3 1 2 2 3 1 1 2 3 3 1 2 2 3 1 Note In the first test case, if the first player passes a card with number $$$2$$$ and the second player passes a card with number $$$1$$$, then the first player has two cards with number $$$1$$$ and the second player has two cards with number $$$2$$$. Then, after making this operation, both players are solid. In the second test case, $$$0$$$ operations would be enough too. Note that you do not need to minimize the number of operations.
2,900
false
true
true
false
false
true
false
false
false
false
2,390
1463A
You are playing a new computer game in which you have to fight monsters. In a dungeon you are trying to clear, you met three monsters; the first of them has $$$a$$$ health points, the second has $$$b$$$ health points, and the third has $$$c$$$. To kill the monsters, you can use a cannon that, when fired, deals $$$1$$$ damage to the selected monster. Every $$$7$$$-th (i.xa0e. shots with numbers $$$7$$$, $$$14$$$, $$$21$$$ etc.) cannon shot is enhanced and deals $$$1$$$ damage to all monsters, not just one of them. If some monster's current amount of health points is $$$0$$$, it can't be targeted by a regular shot and does not receive damage from an enhanced shot. You want to pass the dungeon beautifully, i.xa0e., kill all the monsters with the same enhanced shot (i.xa0e. after some enhanced shot, the health points of each of the monsters should become equal to $$$0$$$ for the first time). Each shot must hit a monster, i.xa0e. each shot deals damage to at least one monster. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Each test case consists of a single line that contains three integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 le a, b, c le 10^8$$$) — the number of health points each monster has. Output For each test case, print YES if you can kill all the monsters with the same enhanced shot. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). Example Input 3 3 2 4 1 1 1 10 1 7 Note In the first test case, you can do as follows: $$$1$$$-th shot to the first monster, $$$2$$$-th shot to the second monster, $$$3$$$-th shot to the third monster, $$$4$$$-th shot to the first monster, $$$5$$$-th shot to the third monster, $$$6$$$-th shot to the third monster, and $$$7$$$-th enhanced shot will kill all the monsters. In the second test case, you can't kill monsters with the same enhanced shot, because the total number of health points of monsters is $$$3$$$, and you will kill them in the first 3 shots.
1,100
true
false
false
false
false
false
false
true
false
false
3,373
442B
Andrey needs one more problem to conduct a programming contest. He has _n_ friends who are always willing to help. He can ask some of them to come up with a contest problem. Andrey knows one value for each of his fiends — the probability that this friend will come up with a problem if Andrey asks him. Help Andrey choose people to ask. As he needs only one problem, Andrey is going to be really upset if no one comes up with a problem or if he gets more than one problem from his friends. You need to choose such a set of people that maximizes the chances of Andrey not getting upset. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of Andrey's friends. The second line contains _n_ real numbers _p__i_ (0.0u2009≤u2009_p__i_u2009≤u20091.0) — the probability that the _i_-th friend can come up with a problem. The probabilities are given with at most 6 digits after decimal point. Output Print a single real number — the probability that Andrey won't get upset at the optimal choice of friends. The answer will be considered valid if it differs from the correct one by at most 10u2009-u20099. Note In the first sample the best strategy for Andrey is to ask only one of his friends, the most reliable one. In the second sample the best strategy for Andrey is to ask all of his friends to come up with a problem. Then the probability that he will get exactly one problem is 0.1·0.8u2009+u20090.9·0.2u2009=u20090.26.
1,800
true
true
false
false
false
false
false
false
false
false
8,069
509D
Vasya had two arrays consisting of non-negative integers: _a_ of size _n_ and _b_ of size _m_. Vasya chose a positive integer _k_ and created an _n_u2009×u2009_m_ matrix _v_ using the following formula: Vasya wrote down matrix _v_ on a piece of paper and put it in the table. A year later Vasya was cleaning his table when he found a piece of paper containing an _n_u2009×u2009_m_ matrix _w_. He remembered making a matrix one day by the rules given above but he was not sure if he had found the paper with the matrix _v_ from those days. Your task is to find out if the matrix _w_ that you've found could have been obtained by following these rules and if it could, then for what numbers _k_,u2009_a_1,u2009_a_2,u2009...,u2009_a__n_,u2009_b_1,u2009_b_2,u2009...,u2009_b__m_ it is possible. Input The first line contains integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100), separated by a space — the number of rows and columns in the found matrix, respectively. The _i_-th of the following lines contains numbers _w__i_,u20091,u2009_w__i_,u20092,u2009...,u2009_w__i_,u2009_m_ (0u2009≤u2009_w__i_,u2009_j_u2009≤u2009109), separated by spaces — the elements of the _i_-th row of matrix _w_. Output If the matrix _w_ could not have been obtained in the manner described above, print "NO" (without quotes) in the single line of output. Otherwise, print four lines. In the first line print "YES" (without quotes). In the second line print an integer _k_ (1u2009≤u2009_k_u2009≤u20091018). Note that each element of table _w_ should be in range between 0 and _k_u2009-u20091 inclusively. In the third line print _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u20091018), separated by spaces. In the fourth line print _m_ integers _b_1,u2009_b_2,u2009...,u2009_b__m_ (0u2009≤u2009_b__i_u2009≤u20091018), separated by spaces. Examples Output YES 1000000007 0 1 1 2 3 Note By we denote the remainder of integer division of _b_ by _c_. It is guaranteed that if there exists some set of numbers _k_,u2009_a_1,u2009...,u2009_a__n_,u2009_b_1,u2009...,u2009_b__m_, that you could use to make matrix _w_, then there also exists a set of numbers that meets the limits 1u2009≤u2009_k_u2009≤u20091018, 1u2009≤u2009_a__i_u2009≤u20091018, 1u2009≤u2009_b__i_u2009≤u20091018 in the output format. In other words, these upper bounds are introduced only for checking convenience purposes.
2,200
true
false
false
false
false
true
false
false
false
false
7,803
1474B
Positive integer $$$x$$$ is called divisor of positive integer $$$y$$$, if $$$y$$$ is divisible by $$$x$$$ without remainder. For example, $$$1$$$ is a divisor of $$$7$$$ and $$$3$$$ is not divisor of $$$8$$$. We gave you an integer $$$d$$$ and asked you to find the smallest positive integer $$$a$$$, such that $$$a$$$ has at least $$$4$$$ divisors; difference between any two divisors of $$$a$$$ is at least $$$d$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 3000$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$d$$$ ($$$1 leq d leq 10000$$$). Output For each test case print one integer $$$a$$$xa0— the answer for this test case. Note In the first test case, integer $$$6$$$ have following divisors: $$$[1, 2, 3, 6]$$$. There are $$$4$$$ of them and the difference between any two of them is at least $$$1$$$. There is no smaller integer with at least $$$4$$$ divisors. In the second test case, integer $$$15$$$ have following divisors: $$$[1, 3, 5, 15]$$$. There are $$$4$$$ of them and the difference between any two of them is at least $$$2$$$. The answer $$$12$$$ is INVALID because divisors are $$$[1, 2, 3, 4, 6, 12]$$$. And the difference between, for example, divisors $$$2$$$ and $$$3$$$ is less than $$$d=2$$$.
1,000
true
true
false
false
false
true
false
true
false
false
3,309
1163B2
This problem is same as the previous one, but has larger constraints. Shiro's just moved to the new house. She wants to invite all friends of her to the house so they can play monopoly. However, her house is too small, so she can only invite one friend at a time. For each of the $$$n$$$ days since the day Shiro moved to the new house, there will be exactly one cat coming to the Shiro's house. The cat coming in the $$$i$$$-th day has a ribbon with color $$$u_i$$$. Shiro wants to know the largest number $$$x$$$, such that if we consider the streak of the first $$$x$$$ days, it is possible to remove exactly one day from this streak so that every ribbon color that has appeared among the remaining $$$x - 1$$$ will have the same number of occurrences. For example, consider the following sequence of $$$u_i$$$: $$$[2, 2, 1, 1, 5, 4, 4, 5]$$$. Then $$$x = 7$$$ makes a streak, since if we remove the leftmost $$$u_i = 5$$$, each ribbon color will appear exactly twice in the prefix of $$$x - 1$$$ days. Note that $$$x = 8$$$ doesn't form a streak, since you must remove exactly one day. Since Shiro is just a cat, she is not very good at counting and needs your help finding the longest streak. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$)xa0— the total number of days. The second line contains $$$n$$$ integers $$$u_1, u_2, ldots, u_n$$$ ($$$1 leq u_i leq 10^5$$$)xa0— the colors of the ribbons the cats wear. Note In the first example, we can choose the longest streak of $$$13$$$ days, since upon removing the last day out of the streak, all of the remaining colors $$$1$$$, $$$2$$$, $$$3$$$, and $$$4$$$ will have the same number of occurrences of $$$3$$$. Note that the streak can also be $$$10$$$ days (by removing the $$$10$$$-th day from this streak) but we are interested in the longest streak. In the fourth example, if we take the streak of the first $$$6$$$ days, we can remove the third day from this streak then all of the remaining colors $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$ and $$$5$$$ will occur exactly once.
1,600
false
false
true
false
true
false
false
false
false
false
4,902
955D
Jenya has recently acquired quite a useful tool — _k_-scissors for cutting strings. They are generally used for cutting out two non-intersecting substrings of length _k_ from an arbitrary string _s_ (its length should be at least 2·_k_ in order to perform this operation) and concatenating them afterwards (preserving the initial order). For example, with the help of 2-scissors you can cut _ab_ and _de_ out of _abcde_ and concatenate them into _abde_, but not _ab_ and _bc_ since they're intersecting. It's a nice idea to test this tool before using it in practice. After looking through the papers, Jenya came up with two strings _s_ and _t_. His question is whether it is possible to apply his scissors to string _s_ such that the resulting concatenation contains _t_ as a substring? Input The first line contains three integers _n_, _m_, _k_ (2u2009≤u2009_m_u2009≤u20092·_k_u2009≤u2009_n_u2009≤u20095·105)xa0— length of _s_, length of _t_ and the aforementioned scissors' parameter correspondingly. The next two lines feature _s_ and _t_ consisting of lowercase latin letters. Output If there is no answer, print «No». Otherwise print «Yes» and two integers _L_ and _R_ denoting the indexes where cutted substrings start (1-indexed). If there are several possible answers, output any. Note In the first sample case you can cut out two substrings starting at 1 and 5. The resulting string baaaab contains aaaa as a substring. In the second sample case the resulting string is bccb.
2,600
false
false
false
false
false
false
true
false
false
false
5,897
1687B
As for the technology in the outside world, it is really too advanced for Gensokyo to even look up to. —Yasaka Kanako, Symposium of Post-mysticism This is an interactive problem. Under the direct supervision of Kanako and the Moriya Shrine, the railway system of Gensokyo is finally finished. GSKR (Gensokyo Railways) consists of $$$n$$$ stations with $$$m$$$ bidirectional tracks connecting them. The $$$i$$$-th track has length $$$l_i$$$ ($$$1le l_ile 10^6$$$). Due to budget limits, the railway system may not be connected, though there may be more than one track between two stations. The value of a railway system is defined as the total length of its all tracks. The maximum (or minimum) capacity of a railway system is defined as the maximum (or minimum) value among all of the currently functional system's — the number of stations and tracks. Interaction Begin the interaction by reading $$$n,m$$$. To make a query, print "? $$$s$$$" (without quotes, $$$s$$$ is a string of length $$$m$$$, consisting of characters 0 and/or 1). Then you should read our response from standard input — the maximum capacity of the system in the simulated state. If your program has made an invalid query or has run out of tries, the interactor will terminate immediately and your program will get a verdict Wrong answer. To give the final answer, print "! $$$L$$$" (without the quotes, $$$L$$$ is the minimum capacity of the system with all tracks functional). Note that giving this answer is not counted towards the limit of $$$2m$$$ queries. 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 The first line of input must contain two integers $$$n,m$$$ ($$$2 leq n leq 200$$$, $$$1le m le 500$$$) — the number of stations and tracks. The next $$$m$$$ lines of input must contain exactly $$$3$$$ space-separated integers $$$u_i$$$, $$$v_i$$$, $$$l_i$$$ ($$$1le u_i,v_i le n$$$, $$$u_i e v_i$$$, $$$1 leq l_i leq 10^6$$$) — the endpoints and the length of the $$$i$$$-th track. Example Output ? 0000 ? 1110 ? 1111 ? 1101 ! 7 Note Here is the graph of the example, satisfying $$$l_i=i$$$.
1,700
false
true
false
false
false
true
false
false
true
true
2,156
56A
Problem - 56A - 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 ") which is the number of the bar's clients. Then follow _n_ lines, each describing one visitor. A line either contains his age (an integer from 0 to 1000) or his drink (a string of capital Latin letters from 1 to 100 in length). It is guaranteed that the input data does not contain spaces and other unnecessary separators. Only the drinks from the list given above should be considered alcohol. Output Print a single number which is the number of people Vasya should check to guarantee the law enforcement. Examples Input 5 18 VODKA COKE 19 17 Output 2 Note In the sample test the second and fifth clients should be checked.
1,000
false
false
true
false
false
false
false
false
false
false
9,695
41E
Problem - 41E - 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 greedy *1900 No tag edit access → Contest materials ") — the number of cities in Berland. Output On the first line must be printed number _m_ — the maximal number of roads in Berland. Then print _m_ lines containing two numbers each — the numbers of cities that the given road joins. The cities are numbered with integers from 1 to _n_. If there are several variants of solving the problem, print any of them. Examples Input 3 Output 2 1 2 2 3 Input 4 Output 4 1 2 2 3 3 4 4 1
1,900
false
true
false
false
false
true
false
false
false
true
9,780
1998A
I couldn't think of a good title for this problem, so I decided to learn from LeetCode. — Sun Tzu, The Art of War You are given three integers $$$x_c$$$, $$$y_c$$$, and $$$k$$$ ($$$-100 leq x_c, y_c leq 100$$$, $$$1 leq k leq 1000$$$). You need to find $$$k$$$ distinct points ($$$x_1, y_1$$$), ($$$x_2, y_2$$$), $$$ldots$$$, ($$$x_k, y_k$$$), having integer coordinates, on the 2D coordinate plane such that: their center$$$^{ ext{∗}}$$$ is ($$$x_c, y_c$$$) $$$-10^9 leq x_i, y_i leq 10^9$$$ for all $$$i$$$ from $$$1$$$ to $$$k$$$ It can be proven that at least one set of $$$k$$$ distinct points always exists that satisfies these conditions. $$$^{ ext{∗}}$$$The center of $$$k$$$ points ($$$x_1, y_1$$$), ($$$x_2, y_2$$$), $$$ldots$$$, ($$$x_k, y_k$$$) is $$$left( frac{x_1 + x_2 + ldots + x_k}{k}, frac{y_1 + y_2 + ldots + y_k}{k} ight)$$$. Input The first line contains $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. Each test case contains three integers $$$x_c$$$, $$$y_c$$$, and $$$k$$$ ($$$-100 leq x_c, y_c leq 100$$$, $$$1 leq k leq 1000$$$)xa0— the coordinates of the center and the number of distinct points you must output. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$1000$$$. Output For each test case, output $$$k$$$ lines, the $$$i$$$-th line containing two space separated integers, $$$x_i$$$ and $$$y_i$$$, ($$$-10^9 leq x_i, y_i leq 10^9$$$)xa0— denoting the position of the $$$i$$$-th point. If there are multiple answers, print any of them. It can be shown that a solution always exists under the given constraints. Note For the first test case, $$$left( frac{10}{1}, frac{10}{1} ight) = (10, 10)$$$. For the second test case, $$$left( frac{-1 + 5 - 4}{3}, frac{-1 -1 + 2}{3} ight) = (0, 0)$$$.
800
true
false
true
false
false
true
false
false
false
false
277
1267J
BerPhone X is almost ready for release with $$$n$$$ applications being preinstalled on the phone. A category of an application characterizes a genre or a theme of this application (like "game", "business", or "education"). The categories are given as integers between $$$1$$$ and $$$n$$$, inclusive; the $$$i$$$-th application has category $$$c_i$$$. You can choose $$$m$$$xa0— the number of screens and $$$s$$$xa0— the size of each screen. You need to fit all $$$n$$$ icons of the applications (one icon representing one application) meeting the following requirements: On each screen, all the icons must belong to applications of the same category (but different screens can contain icons of applications of the same category); Each screen must be either completely filled with icons (the number of icons on the screen is equal toxa0$$$s$$$) or almost filled with icons (the number of icons is equal to $$$s-1$$$). Your task is to find the minimal possible number of screens $$$m$$$. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10,000$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 2cdot10^6$$$)xa0— the number of the icons. The second line contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le n$$$), where $$$c_i$$$ is the category of the $$$i$$$-th application. It is guaranteed that the sum of the values of $$$n$$$ for all test cases in the input does not exceed $$$2cdot10^6$$$. Output Print $$$t$$$ integers — the answers to the given test cases in the order they follow in the input. The answer to a test case is an integer $$$m$$$xa0— the minimum number of screens on which all $$$n$$$ icons can be placed satisfying the given requirements. Example Input 3 11 1 5 1 5 1 5 1 1 1 1 5 6 1 2 2 2 2 1 5 4 3 3 1 2 Note In the first test case of the example, all the icons can be placed on three screens of size $$$4$$$: a screen with $$$4$$$ icons of the category $$$1$$$, a screen with $$$3$$$ icons of the category $$$1$$$, and a screen with $$$4$$$ icons of the categoryxa0$$$5$$$.
1,800
true
true
true
false
false
false
false
false
false
false
4,354
922F
Problem - 922F - 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 dp greedy number theory *2400 No tag edit access → Contest materials ") , that . Input The only line contains two integers _n_ and _k_ . Output If there is no answer, print "No". Otherwise, in the first line print "Yes", in the secondxa0— an integer _m_ that denotes the size of the set you have found, in the second line print _m_ integersxa0— the elements of the set , in any order. If there are multiple answers, print any of them. Examples Input 3 3 Output No Input 6 6 Output Yes 5 1 2 4 5 6 Input 8 3 Output Yes 4 2 4 5 8 Note In the second sample, the valid pairs in the output set are (1,u20092), (1,u20094), (1,u20095), (1,u20096), (2,u20094), (2,u20096). Thus, . In the third example, the valid pairs in the output set are (2,u20094), (4,u20098), (2,u20098). Thus, .
2,400
false
true
false
true
false
true
false
false
false
false
6,024
652D
Problem - 652D - Codeforces =============== xa0 ]( "Announcement") — the number of segments on a line. Each of the next _n_ lines contains two integers _l__i_ and _r__i_ (u2009-u2009109u2009≤u2009_l__i_u2009<u2009_r__i_u2009≤u2009109) — the coordinates of the left and the right ends of the _i_-th segment. It is guaranteed that there are no ends of some segments that coincide. Output Print _n_ lines. The _j_-th of them should contain the only integer _a__j_ — the number of segments contained in the _j_-th segment. Examples Input 4 1 8 2 3 4 7 5 6 Output 3 0 1 0 Input 3 3 4 1 5 2 6 Output 0 1 1
1,800
false
false
false
false
true
false
false
false
true
false
7,207
237C
Problem - 237C - 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 number theory two pointers *1600 No tag edit access → Contest materials ") Разбор Задач") . You want to find the minimum integer _l_ (1u2009≤u2009_l_u2009≤u2009_b_u2009-u2009_a_u2009+u20091) such that for any integer _x_ (_a_u2009≤u2009_x_u2009≤u2009_b_u2009-u2009_l_u2009+u20091) among _l_ integers _x_, _x_u2009+u20091, ..., _x_u2009+u2009_l_u2009-u20091 there are at least _k_ prime numbers. Find and print the required minimum _l_. If no value _l_ meets the described limitations, print -1. Input A single line contains three space-separated integers _a_,u2009_b_,u2009_k_ (1u2009≤u2009_a_,u2009_b_,u2009_k_u2009≤u2009106;xa0_a_u2009≤u2009_b_). Output In a single line print a single integer — the required minimum _l_. If there's no solution, print -1. Examples Input 2 4 2 Output 3 Input 6 13 1 Output 4 Input 1 4 3 Output -1
1,600
false
false
false
false
false
false
false
true
false
false
8,887
229A
You are given a table consisting of _n_ rows and _m_ columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right. To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "00110" one cell to the left, we get a row "01100". Determine the minimum number of moves needed to make some table column consist only of numbers 1. Input The first line contains two space-separated integers: _n_ (1u2009≤u2009_n_u2009≤u2009100)xa0— the number of rows in the table and _m_ (1u2009≤u2009_m_u2009≤u2009104)xa0— the number of columns in the table. Then _n_ lines follow, each of them contains _m_ characters "0" or "1": the _j_-th character of the _i_-th line describes the contents of the cell in the _i_-th row and in the _j_-th column of the table. It is guaranteed that the description of the table contains no other characters besides "0" and "1". Output Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1. Examples Input 3 6 101010 000100 100000 Note In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s. In the second sample one can't shift the rows to get a column containing only 1s.
1,500
false
false
false
false
false
false
true
false
false
false
8,923
1731A
Joey is low on money. His friend Chandler wants to lend Joey some money, but can't give him directly, as Joey is too proud of himself to accept it. So, in order to trick him, Chandler asks Joey to play a game. In this game, Chandler gives Joey an array $$$a_1, a_2, dots, a_n$$$ ($$$n geq 2$$$) of positive integers ($$$a_i ge 1$$$). Joey can perform the following operation on the array any number of times: 1. Take two indices $$$i$$$ and $$$j$$$ ($$$1 le i < j le n)$$$. 2. Choose two integers $$$x$$$ and $$$y$$$ ($$$x, y ge 1$$$) such that $$$x cdot y = a_i cdot a_j$$$. 3. Replace $$$a_i$$$ by $$$x$$$ and $$$a_j$$$ by $$$y$$$. In the end, Joey will get the money equal to the sum of elements of the final array. Find the maximum amount of money $$$mathrm{ans}$$$ Joey can get but print $$$2022 cdot mathrm{ans}$$$. Why multiplied by $$$2022$$$? Because we are never gonna see it again! It is guaranteed that the product of all the elements of the array $$$a$$$ doesn't exceed $$$10^{12}$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 4000$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 leq n leq 50$$$)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 10^6$$$)xa0— the array itself. It's guaranteed that the product of all $$$a_i$$$ doesn't exceed $$$10^{12}$$$ (i.xa0e. $$$a_1 cdot a_2 cdot ldots cdot a_n le 10^{12}$$$). Output For each test case, print the maximum money Joey can get multiplied by $$$2022$$$. Example Input 3 3 2 3 2 2 1 3 3 1000000 1000000 1 Output 28308 8088 2022000000004044 Note In the first test case, Joey can do the following: 1. He chooses $$$i = 1$$$ and $$$j = 2$$$ (so he has $$$a[i] cdot a[j] = 6$$$), chooses $$$x = 6$$$ and $$$y = 1$$$ and makes $$$a[i] = 6$$$ and $$$a[j] = 1$$$. $$$$$$[2, 3, 2] xrightarrow[x = 6,; y = 1]{i = 1,; j = 2} [6, 1, 2]$$$$$$ 2. He chooses $$$i = 1$$$ and $$$j = 3$$$ (so he has $$$a[i] cdot a[j] = 12$$$), chooses $$$x = 12$$$ and $$$y = 1$$$ and makes $$$a[i] = 12$$$ and $$$a[j] = 1$$$. $$$$$$[6, 1, 2] xrightarrow[x = 12,; y = 1]{i = 1,; j = 3} [12, 1, 1]$$$$$$ The sum is $$$14$$$ which is the maximum of all possible sums. The answer is $$$2022 cdot 14 = 28308$$$.
800
true
true
false
false
false
false
false
false
false
false
1,905
1117E
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: [ are given a string $$$t$$$ consisting of $$$n$$$ lowercase Latin letters. This string was cyphered as follows: initially, the jury had a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. Then they applied a sequence of no more than $$$n$$$ (possibly zero) operations. $$$i$$$-th operation is denoted by two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le n$$$), and means swapping two elements of the string with indices $$$a_i$$$ and $$$b_i$$$. All operations were done in the order they were placed in the sequence. For example, if $$$s$$$ is xyz and $$$2$$$ following operations are performed: $$$a_1 = 1, b_1 = 2$$$; $$$a_2 = 2, b_2 = 3$$$, then after the first operation the current string is yxz, and after the second operation the current string is yzx, so $$$t$$$ is yzx. You are asked to restore the original string $$$s$$$. Unfortunately, you have no information about the operations used in the algorithm (you don't even know if there were any operations in the sequence). But you may run the same sequence of operations on any string you want, provided that it contains only lowercase Latin letters and its length is $$$n$$$, and get the resulting string after those operations. Can you guess the original string $$$s$$$ asking the testing system to run the sequence of swaps no more than $$$3$$$ times? The string $$$s$$$ and the sequence of swaps are fixed in each test; the interactor doesn't try to adapt the test to your solution. Input Initially the testing system sends one string $$$t$$$, consisting of lowercase Latin letters ($$$1 le t = n le 10^4$$$). Output To give the answer, your program should print one line $$$!$$$ $$$s$$$ with a line break in the end. After that, it should flush the output and terminate gracefully. Interaction Before giving the answer, you may submit no more than $$$3$$$ queries. To ask a query, print one line in the following format: $$$?$$$ $$$s'$$$, where $$$s'$$$ should be a string consisting of exaclty $$$n$$$ lowercase Latin letters. The line should be ended with a line break character. After submitting a query, flush the output and read the answer to your query — a string $$$t'$$$ consisting of $$$n$$$ lowercase Latin letters, which is the result of applying the sequence of swaps to string $$$s'$$$. This string will be given on a separate line ended by a line break character. If you submit an incorrect query (or ask more than $$$3$$$ queries), the answer to it will be one string 0. After receiving such an answer, your program should terminate immediately — otherwise you may receive verdict "Runtime error", "Time limit exceeded" or some other verdict instead of "Wrong answer". Example Output ? baa ? aba ? aab ! xyz Note In the sample, the testcase described in the statement is used. The participant asks the first query with string baa, which is transformed to aab. The second query contains string aba, which is transformed to baa. The third query contains string aab, which is transformed to aba. The participant can deduce that the initial string $$$s$$$ was xyz. Note for hacking phase: To submit a test in hacking phase, you should provide it in the following format: The first line should contain the string $$$s$$$ you guess, consisting of $$$n in [1, 10000]$$$ lowercase Latin letters. The second line should contain $$$k$$$ ($$$0 le k le n$$$) — the number of swap operations in the sequence. Then $$$k$$$ lines should follow, $$$i$$$-th of them should denote $$$i$$$-th operation with two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le n$$$). For example, the sample test would look like that: xyz 2 1 2 2 3
2,200
true
false
false
false
false
true
false
false
false
false
5,105
1619C
Tanya is learning how to add numbers, but so far she is not doing it correctly. She is adding two numbers $$$a$$$ and $$$b$$$ using the following algorithm: 1. If one of the numbers is shorter than the other, Tanya adds leading zeros so that the numbers are the same length. 2. The numbers are processed from right to left (that is, from the least significant digits to the most significant). 3. In the first step, she adds the last digit of $$$a$$$ to the last digit of $$$b$$$ and writes their sum in the answer. 4. At each next step, she performs the same operation on each pair of digits in the same place and writes the result to the left side of the answer. For example, the numbers $$$a = 17236$$$ and $$$b = 3465$$$ Tanya adds up as follows: $$$$$$ large{ begin{array}{r} + begin{array}{r} 17236 03465 end{array} hline begin{array}{r} 1106911 end{array} end{array}} $$$$$$ calculates the sum of $$$6 + 5 = 11$$$ and writes $$$11$$$ in the answer. calculates the sum of $$$3 + 6 = 9$$$ and writes the result to the left side of the answer to get $$$911$$$. calculates the sum of $$$2 + 4 = 6$$$ and writes the result to the left side of the answer to get $$$6911$$$. calculates the sum of $$$7 + 3 = 10$$$, and writes the result to the left side of the answer to get $$$106911$$$. calculates the sum of $$$1 + 0 = 1$$$ and writes the result to the left side of the answer and get $$$1106911$$$. As a result, she gets $$$1106911$$$. You are given two positive integers $$$a$$$ and $$$s$$$. Find the number $$$b$$$ such that by adding $$$a$$$ and $$$b$$$ as described above, Tanya will get $$$s$$$. Or determine that no suitable $$$b$$$ exists. Input The first line of input data contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Each test case consists of a single line containing two positive integers $$$a$$$ and $$$s$$$ ($$$1 le a lt s le 10^{18}$$$) separated by a space. Output For each test case print the answer on a separate line. If the solution exists, print a single positive integer $$$b$$$. The answer must be written without leading zeros. If multiple answers exist, print any of them. If no suitable number $$$b$$$ exists, output -1. Example Input 6 17236 1106911 1 5 108 112 12345 1023412 1 11 1 20 Output 3465 4 -1 90007 10 -1 Note The first test case is explained in the main part of the statement. In the third test case, we cannot choose $$$b$$$ that satisfies the problem statement.
1,200
false
false
true
false
false
false
false
false
false
false
2,533
630M
Vasya started working in a machine vision company of IT City. Vasya's team creates software and hardware for identification of people by their face. One of the project's know-how is a camera rotating around its optical axis on shooting. People see an eye-catching gadget — a rotating camera — come up to it to see it better, look into it. And the camera takes their photo at that time. What could be better for high quality identification? But not everything is so simple. The pictures from camera appear rotated too (on clockwise camera rotation frame the content becomes rotated counter-clockwise). But the identification algorithm can work only with faces that are just slightly deviated from vertical. Vasya was entrusted to correct the situation — to rotate a captured image so that image would be minimally deviated from vertical. Requirements were severe. Firstly, the picture should be rotated only on angle divisible by 90 degrees to not lose a bit of information about the image. Secondly, the frames from the camera are so huge and FPS is so big that adequate rotation speed is provided by hardware FPGA solution only. And this solution can rotate only by 90 degrees clockwise. Of course, one can apply 90 degrees turn several times but for the sake of performance the number of turns should be minimized. Help Vasya implement the program that by the given rotation angle of the camera can determine the minimum number of 90 degrees clockwise turns necessary to get a picture in which up direction deviation from vertical is minimum. The next figure contains frames taken from an unrotated camera, then from rotated 90 degrees clockwise, then from rotated 90 degrees counter-clockwise. Arrows show direction to "true up". The next figure shows 90 degrees clockwise turn by FPGA hardware. Note When the camera is rotated 60 degrees counter-clockwise (the second example), an image from it is rotated 60 degrees clockwise. One 90 degrees clockwise turn of the image result in 150 degrees clockwise total rotation and deviation from "true up" for one turn is 150 degrees. Two 90 degrees clockwise turns of the image result in 240 degrees clockwise total rotation and deviation from "true up" for two turns is 120 degrees because 240 degrees clockwise equal to 120 degrees counter-clockwise. Three 90 degrees clockwise turns of the image result in 330 degrees clockwise total rotation and deviation from "true up" for three turns is 30 degrees because 330 degrees clockwise equal to 30 degrees counter-clockwise. From 60, 150, 120 and 30 degrees deviations the smallest is 30, and it it achieved in three 90 degrees clockwise turns.
1,800
true
false
false
false
false
false
false
false
false
false
7,292
868C
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of _n_ problems, and they want to select any non-empty subset of it as a problemset. _k_ experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems. Determine if Snark and Philip can make an interesting problemset! Input The first line contains two integers _n_, _k_ (1u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_k_u2009≤u20094)xa0— the number of problems and the number of experienced teams. Each of the next _n_ lines contains _k_ integers, each equal to 0 or 1. The _j_-th number in the _i_-th line is 1 if _j_-th team knows _i_-th problem and 0 otherwise. Output Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise. You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES"). Examples Input 5 3 1 0 1 1 1 0 1 0 0 1 0 0 1 0 0 Note In the first example you can't make any interesting problemset, because the first team knows all problems. In the second example you can choose the first and the third problems.
1,500
false
false
false
true
false
true
true
false
false
false
6,271
1702F
Multisetxa0—is a set of numbers in which there can be equal elements, and the order of the numbers does not matter. Two multisets are equal when each value occurs the same number of times. For example, the multisets $$${2,2,4}$$$ and $$${2,4,2}$$$ are equal, but the multisets $$${1,2,2}$$$ and $$${1,1,2}$$$xa0— are not. You are given two multisets $$$a$$$ and $$$b$$$, each consisting of $$$n$$$ integers. In a single operation, any element of the $$$b$$$ multiset can be doubled or halved (rounded down). In other words, you have one of the following operations available for an element $$$x$$$ of the $$$b$$$ multiset: replace $$$x$$$ with $$$x cdot 2$$$, or replace $$$x$$$ with $$$lfloor frac{x}{2} floor$$$ (round down). Note that you cannot change the elements of the $$$a$$$ multiset. See if you can make the multiset $$$b$$$ become equal to the multiset $$$a$$$ in an arbitrary number of operations (maybe $$$0$$$). For example, if $$$n = 4$$$, $$$a = {4, 24, 5, 2}$$$, $$$b = {4, 1, 6, 11}$$$, then the answer is yes. We can proceed as follows: Replace $$$1$$$ with $$$1 cdot 2 = 2$$$. We get $$$b = {4, 2, 6, 11}$$$. Replace $$$11$$$ with $$$lfloor frac{11}{2} floor = 5$$$. We get $$$b = {4, 2, 6, 5}$$$. Replace $$$6$$$ with $$$6 cdot 2 = 12$$$. We get $$$b = {4, 2, 12, 5}$$$. Replace $$$12$$$ with $$$12 cdot 2 = 24$$$. We get $$$b = {4, 2, 24, 5}$$$. Got equal multisets $$$a = {4, 24, 5, 2}$$$ and $$$b = {4, 2, 24, 5}$$$. Input The first line of input data contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0—the number of test cases. Each test case consists of three lines. The first line of the test case contains an integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0—the number of elements in the multisets $$$a$$$ and $$$b$$$. The second line gives $$$n$$$ integers: $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_1 le a_2 le dots le a_n le 10^9$$$)xa0—the elements of the multiset $$$a$$$. Note that the elements may be equal. The third line contains $$$n$$$ integers: $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_1 le b_2 le dots le b_n le 10^9$$$)xa0— elements of the multiset $$$b$$$. Note that the elements may be equal. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print on a separate line: YES if you can make the multiset $$$b$$$ become equal to $$$a$$$, NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive answer). Note The first example is explained in the statement. In the second example, it is impossible to get the value $$$31$$$ from the numbers of the multiset $$$b$$$ by available operations. In the third example, we can proceed as follows: Replace $$$2$$$ with $$$2 cdot 2 = 4$$$. We get $$$b = {4, 14, 14, 26, 42}$$$. Replace $$$14$$$ with $$$lfloor frac{14}{2} floor = 7$$$. We get $$$b = {4, 7, 14, 26, 42}$$$. Replace $$$26$$$ with $$$lfloor frac{26}{2} floor = 13$$$. We get $$$b = {4, 7, 14, 13, 42}$$$. Replace $$$42$$$ with $$$lfloor frac{42}{2} floor = 21$$$. We get $$$b = {4, 7, 14, 13, 21}$$$. Replace $$$21$$$ with $$$lfloor frac{21}{2} floor = 10$$$. We get $$$b = {4, 7, 14, 13, 10}$$$. Got equal multisets $$$a = {4, 7, 10, 13, 14}$$$ and $$$b = {4, 7, 14, 13, 10}$$$.
1,700
true
true
false
false
true
true
false
false
false
false
2,065
1499A
You have a board represented as a grid with $$$2 imes n$$$ cells. The first $$$k_1$$$ cells on the first row and first $$$k_2$$$ cells on the second row are colored in white. All other cells are colored in black. You have $$$w$$$ white dominoes ($$$2 imes 1$$$ tiles, both cells are colored in white) and $$$b$$$ black dominoes ($$$2 imes 1$$$ tiles, both cells are colored in black). You can place a white domino on the board if both board's cells are white and not occupied by any other domino. In the same way, you can place a black domino if both cells are black and not occupied by any other domino. Can you place all $$$w + b$$$ dominoes on the board if you can place dominoes both horizontally and vertically? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 3000$$$)xa0— the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$k_1$$$ and $$$k_2$$$ ($$$1 le n le 1000$$$; $$$0 le k_1, k_2 le n$$$). The second line of each test case contains two integers $$$w$$$ and $$$b$$$ ($$$0 le w, b le n$$$). Output For each test case, print YES if it's possible to place all $$$w + b$$$ dominoes on the board and NO, otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer). Note In the first test case, $$$n = 1$$$, $$$k_1 = 0$$$ and $$$k_2 = 1$$$. It means that $$$2 imes 1$$$ board has black cell $$$(1, 1)$$$ and white cell $$$(2, 1)$$$. So, you can't place any white domino, since there is only one white cell. In the second test case, the board of the same size $$$2 imes 1$$$, but both cell are white. Since $$$w = 0$$$ and $$$b = 0$$$, so you can place $$$0 + 0 = 0$$$ dominoes on the board. In the third test case, board $$$2 imes 3$$$, but fully colored in black (since $$$k_1 = k_2 = 0$$$), so you can't place any white domino. In the fourth test case, cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, and $$$(2, 1)$$$ are white and other cells are black. You can place $$$2$$$ white dominoes at positions $$$((1, 1), (2, 1))$$$ and $$$((1, 2), (1, 3))$$$ and $$$2$$$ black dominoes at positions $$$((1, 4), (2, 4))$$$ $$$((2, 2), (2, 3))$$$.
800
true
false
false
false
false
true
false
false
false
false
3,175
84C
Perhaps many have heard that the World Biathlon Championship has finished. Although our hero Valera was not present at this spectacular event himself and only watched it on TV, it excited him so much that he decided to enroll in a biathlon section. Of course, biathlon as any sport, proved very difficult in practice. It takes much time and effort. Workouts, workouts, and workouts, — that's what awaited Valera on his way to great achievements in biathlon. As for the workouts, you all probably know that every professional biathlete should ski fast and shoot precisely at the shooting range. Only in this case you can hope to be successful, because running and shooting are the two main components of biathlon. Valera has been diligent in his ski trainings, which is why he runs really fast, however, his shooting accuracy is nothing to write home about. On a biathlon base where Valera is preparing for the competition, there is a huge rifle range with _n_ targets. Each target have shape of a circle, and the center of each circle is located on the _Ox_ axis. At the last training session Valera made the total of _m_ shots. To make monitoring of his own results easier for him, one rather well-known programmer (of course it is you) was commissioned to write a program that would reveal how many and which targets Valera hit. More specifically, for each target the program must print the number of the first successful shot (in the target), or "-1" if this was not hit. The target is considered hit if the shot is inside the circle or on its boundary. Valera is counting on you and perhaps, thanks to you he will one day win international competitions. Input The first line of the input file contains the integer _n_ (1u2009≤u2009_n_u2009≤u2009104), which is the number of targets. The next _n_ lines contain descriptions of the targets. Each target is a circle whose center is located on the _Ox_ axis. Each circle is given by its coordinate of the center _x_ (u2009-u20092·104u2009≤u2009_x_u2009≤u20092·104) and its radius _r_ (1u2009≤u2009_r_u2009≤u20091000). It is guaranteed that no two targets coincide, intersect or are nested into each other, but they can touch each other. The next line contains integer _m_ (1u2009≤u2009_m_u2009≤u20092·105), which is the number of shots. Next _m_ lines contain descriptions of the shots, which are points on the plane, given by their coordinates _x_ and _y_ (u2009-u20092·104u2009≤u2009_x_,u2009_y_u2009≤u20092·104). All the numbers in the input are integers. Targets and shots are numbered starting from one in the order of the input. Output Print on the first line a single number, the number of targets hit by Valera. Print on the second line for each of the targets the number of its first hit or "-1" (without quotes) if this number does not exist. Separate numbers with spaces.
1,700
false
false
true
false
false
false
false
true
false
false
9,547
1722F
An L-shape is a figure on gridded paper that looks like the first four pictures below. An L-shape contains exactly three shaded cells (denoted by *), which can be rotated in any way. You are given a rectangular grid. Determine if it contains L-shapes only, where L-shapes can't touch an edge or corner. More formally: Each shaded cell in the grid is part of exactly one L-shape, and no two L-shapes are adjacent by edge or corner. For example, the last two grids in the picture above do not satisfy the condition because the two L-shapes touch by corner and edge, respectively. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 50$$$)xa0— the number of rows and columns in the grid, respectively. Then $$$n$$$ lines follow, each containing $$$m$$$ characters. Each of these characters is either '.' or '*'xa0— an empty cell or a shaded cell, respectively. Output For each test case, output "YES" if the grid is made up of L-shape that don't share edges or corners, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Example Input 10 6 10 ........** .**......* ..*..*.... .....**... ...*.....* ..**....** 6 10 ....*...** .**......* ..*..*.... .....**... ...*.....* ..**....** 3 3 ... *** ... 4 4 .*.. **.. ..** ..*. 5 4 .*.. **.. .... ..** ..*. 3 2 .* ** *. 2 3 *.. .** 3 2 .. ** *. 3 3 .** *.* **. 3 3 ..* .** ..* Output YES NO NO NO YES NO NO YES NO NO
1,700
false
false
true
false
false
false
false
false
false
false
1,948
1741B
A sequence of $$$n$$$ numbers is called permutation if it contains all numbers from $$$1$$$ to $$$n$$$ exactly once. For example, the sequences $$$[3, 1, 4, 2]$$$, [$$$1$$$] and $$$[2,1]$$$ are permutations, but $$$[1,2,1]$$$, $$$[0,1]$$$ and $$$[1,3,4]$$$ are not. For a given number $$$n$$$ you need to make a permutation $$$p$$$ such that two requirements are satisfied at the same time: For each element $$$p_i$$$, at least one of its neighbors has a value that differs from the value of $$$p_i$$$ by one. That is, for each element $$$p_i$$$ ($$$1 le i le n$$$), at least one of its neighboring elements (standing to the left or right of $$$p_i$$$) must be $$$p_i + 1$$$, or $$$p_i - 1$$$. the permutation must have no fixed points. That is, for every $$$i$$$ ($$$1 le i le n$$$), $$$p_i eq i$$$ must be satisfied. Let's call the permutation that satisfies these requirements funny. For example, let $$$n = 4$$$. Then [$$$4, 3, 1, 2$$$] is a funny permutation, since: to the right of $$$p_1=4$$$ is $$$p_2=p_1-1=4-1=3$$$; to the left of $$$p_2=3$$$ is $$$p_1=p_2+1=3+1=4$$$; to the right of $$$p_3=1$$$ is $$$p_4=p_3+1=1+1=2$$$; to the left of $$$p_4=2$$$ is $$$p_3=p_4-1=2-1=1$$$. for all $$$i$$$ is $$$p_i e i$$$. For a given positive integer $$$n$$$, output any funny permutation of length $$$n$$$, or output -1 if funny permutation of length $$$n$$$ does not exist. Input The first line of input data contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. Each test case consists of f single line containing one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$). 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 on a separate line: any funny permutation $$$p$$$ of length $$$n$$$; or the number -1 if the permutation you are looking for does not exist. Example Output 3 4 2 1 -1 6 7 4 5 3 2 1 5 4 1 2 3 2 1 Note The first test case is explained in the problem statement. In the second test case, it is not possible to make the required permutation: permutations $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[3, 2, 1]$$$ have fixed points, and in $$$[2, 3, 1]$$$ and $$$[3, 1, 2]$$$ the first condition is met not for all positions.
800
true
false
false
false
false
true
false
false
false
false
1,837
1512G
Problem - 1512G - 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 dp math number theory *1700 No tag edit access → Contest materials ") Editorial") $$$ the sum of all divisors of the number $$$n$$$, i.e. $$$d(n) = sumlimits_{k n} k$$$. For example, $$$d(1) = 1$$$, $$$d(4) = 1+2+4=7$$$, $$$d(6) = 1+2+3+6=12$$$. For a given number $$$c$$$, find the minimum $$$n$$$ such that $$$d(n) = c$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$). Then $$$t$$$ test cases follow. Each test case is characterized by one integer $$$c$$$ ($$$1 le c le 10^7$$$). Output For each test case, output: "-1" if there is no such $$$n$$$ that $$$d(n) = c$$$; $$$n$$$, otherwise. Example Input 12 1 2 3 4 5 6 7 8 9 10 39 691 Output 1 -1 2 3 -1 5 4 7 -1 -1 18 -1
1,700
true
false
false
true
false
false
true
false
false
false
3,103
177C1
To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings. More formally, for each invited person the following conditions should be fulfilled: all his friends should also be invited to the party; the party shouldn't have any people he dislikes; all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people _a_1 and _a__p_ are connected through a chain of common friends if there exists a sequence of people _a_2,u2009_a_3,u2009...,u2009_a__p_u2009-u20091 such that all pairs of people _a__i_ and _a__i_u2009+u20091 (1u2009≤u2009_i_u2009<u2009_p_) are friends. Help the Beaver find the maximum number of acquaintances he can invite. Input The first line of input contains an integer _n_ — the number of the Beaver's acquaintances. The second line contains an integer _k_ — the number of pairs of friends. Next _k_ lines contain space-separated pairs of integers _u__i_,u2009_v__i_ — indices of people who form the _i_-th pair of friends. The next line contains an integer _m_ — the number of pairs of people who dislike each other. Next _m_ lines describe pairs of people who dislike each other in the same format as the pairs of friends were described. Each pair of people is mentioned in the input at most once . In particular, two persons cannot be friends and dislike each other at the same time. The input limitations for getting 30 points are: 2u2009≤u2009_n_u2009≤u200914 The input limitations for getting 100 points are: 2u2009≤u2009_n_u2009≤u20092000 Output Output a single number — the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0. Examples Input 9 8 1 2 1 3 2 3 4 5 6 7 7 8 8 9 9 6 2 1 6 7 9 Note Let's have a look at the example. Two groups of people can be invited: {1,u20092,u20093} and {4,u20095}, thus the answer will be the size of the largest of these groups. Group {6,u20097,u20098,u20099} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1,u20092,u20093,u20094,u20095} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected).
1,500
false
false
false
false
false
false
false
false
false
true
9,150
1698C
You are given an array $$$a$$$ of length $$$n$$$. The array is called 3SUM-closed if for all distinct indices $$$i$$$, $$$j$$$, $$$k$$$, the sum $$$a_i + a_j + a_k$$$ is an element of the array. More formally, $$$a$$$ is 3SUM-closed if for all integers $$$1 leq i < j < k leq n$$$, there exists some integer $$$1 leq l leq n$$$ such that $$$a_i + a_j + a_k = a_l$$$. Determine if $$$a$$$ is 3SUM-closed. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$3 leq n leq 2 cdot 10^5$$$)xa0— the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-10^9 leq a_i leq 10^9$$$)xa0— the elements of the array. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output "YES" (without quotes) if $$$a$$$ is 3SUM-closed and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). Example Input 4 3 -1 0 1 5 1 -2 -2 1 -3 6 0 0 0 0 0 0 4 -1 2 -3 4 Note In the first test case, there is only one triple where $$$i=1$$$, $$$j=2$$$, $$$k=3$$$. In this case, $$$a_1 + a_2 + a_3 = 0$$$, which is an element of the array ($$$a_2 = 0$$$), so the array is 3SUM-closed. In the second test case, $$$a_1 + a_4 + a_5 = -1$$$, which is not an element of the array. Therefore, the array is not 3SUM-closed. In the third test case, $$$a_i + a_j + a_k = 0$$$ for all distinct $$$i$$$, $$$j$$$, $$$k$$$, and $$$0$$$ is an element of the array, so the array is 3SUM-closed.
1,300
false
false
false
false
true
false
true
false
false
false
2,092
677B
Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed _h_ and the processor smashes _k_ centimeters of potato each second. If there are less than _k_ centimeters remaining, than during this second processor smashes all the remaining potato. Vanya has _n_ pieces of potato, the height of the _i_-th piece is equal to _a__i_. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number _n_. Formally, each second the following happens: 1. If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece. 2. Processor smashes _k_ centimeters of potato (or just everything that is inside). Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed. Input The first line of the input contains integers _n_, _h_ and _k_ (1u2009≤u2009_n_u2009≤u2009100u2009000,u20091u2009≤u2009_k_u2009≤u2009_h_u2009≤u2009109)xa0— the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively. The second line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009_h_)xa0— the heights of the pieces. Output Print a single integerxa0— the number of seconds required to smash all the potatoes following the process described in the problem statement. Note Consider the first sample. 1. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. 2. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. 3. Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second. 4. Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3. 5. During this second processor finally smashes all the remaining potato and the process finishes. In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2·5u2009=u200910 seconds. In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds.
1,400
true
false
true
false
false
false
false
false
false
false
7,120
525A
After a hard day Vitaly got very hungry and he wants to eat his favorite potato pie. But it's not that simple. Vitaly is in the first room of the house with _n_ room located in a line and numbered starting from one from left to right. You can go from the first room to the second room, from the second room to the third room and so on — you can go from the (_n_u2009-u20091)-th room to the _n_-th room. Thus, you can go to room _x_ only from room _x_u2009-u20091. The potato pie is located in the _n_-th room and Vitaly needs to go there. Each pair of consecutive rooms has a door between them. In order to go to room _x_ from room _x_u2009-u20091, you need to open the door between the rooms with the corresponding key. In total the house has several types of doors (represented by uppercase Latin letters) and several types of keys (represented by lowercase Latin letters). The key of type _t_ can open the door of type _T_ if and only if _t_ and _T_ are the same letter, written in different cases. For example, key f can open door F. Each of the first _n_u2009-u20091 rooms contains exactly one key of some type that Vitaly can use to get to next rooms. Once the door is open with some key, Vitaly won't get the key from the keyhole but he will immediately run into the next room. In other words, each key can open no more than one door. Vitaly realizes that he may end up in some room without the key that opens the door to the next room. Before the start his run for the potato pie Vitaly can buy any number of keys of any type that is guaranteed to get to room _n_. Given the plan of the house, Vitaly wants to know what is the minimum number of keys he needs to buy to surely get to the room _n_, which has a delicious potato pie. Write a program that will help Vitaly find out this number. Input The first line of the input contains a positive integer _n_ (2u2009≤u2009_n_u2009≤u2009105)xa0—xa0the number of rooms in the house. The second line of the input contains string _s_ of length 2·_n_u2009-u20092. Let's number the elements of the string from left to right, starting from one. The odd positions in the given string _s_ contain lowercase Latin lettersxa0—xa0the types of the keys that lie in the corresponding rooms. Thus, each odd position _i_ of the given string _s_ contains a lowercase Latin letter — the type of the key that lies in room number (_i_u2009+u20091)u2009/u20092. The even positions in the given string contain uppercase Latin letters — the types of doors between the rooms. Thus, each even position _i_ of the given string _s_ contains an uppercase letter — the type of the door that leads from room _i_u2009/u20092 to room _i_u2009/u20092u2009+u20091.
1,100
false
true
false
false
false
false
false
false
false
false
7,736
1534F1
This is the easy version of the problem. The difference between the versions is the constraints on $$$a_i$$$. You can make hacks only if all versions of the problem are solved. Little Dormi has recently received a puzzle from his friend and needs your help to solve it. The puzzle consists of an upright board with $$$n$$$ rows and $$$m$$$ columns of cells, some empty and some filled with blocks of sand, and $$$m$$$ non-negative integers $$$a_1,a_2,ldots,a_m$$$ ($$$0 leq a_i leq n$$$). In this version of the problem, $$$a_i$$$ will be equal to the number of blocks of sand in column $$$i$$$. When a cell filled with a block of sand is disturbed, the block of sand will fall from its cell to the sand counter at the bottom of the column (each column has a sand counter). While a block of sand is falling, other blocks of sand that are adjacent at any point to the falling block of sand will also be disturbed and start to fall. Specifically, a block of sand disturbed at a cell $$$(i,j)$$$ will pass through all cells below and including the cell $$$(i,j)$$$ within the column, disturbing all adjacent cells along the way. Here, the cells adjacent to a cell $$$(i,j)$$$ are defined as $$$(i-1,j)$$$, $$$(i,j-1)$$$, $$$(i+1,j)$$$, and $$$(i,j+1)$$$ (if they are within the grid). Note that the newly falling blocks can disturb other blocks. In one operation you are able to disturb any piece of sand. The puzzle is solved when there are at least $$$a_i$$$ blocks of sand counted in the $$$i$$$-th sand counter for each column from $$$1$$$ to $$$m$$$. You are now tasked with finding the minimum amount of operations in order to solve the puzzle. Note that Little Dormi will never give you a puzzle that is impossible to solve. Input The first line consists of two space-separated positive integers $$$n$$$ and $$$m$$$ ($$$1 leq n cdot m leq 400,000$$$). Each of the next $$$n$$$ lines contains $$$m$$$ characters, describing each row of the board. If a character on a line is '.', the corresponding cell is empty. If it is '#', the cell contains a block of sand. The final line contains $$$m$$$ non-negative integers $$$a_1,a_2,ldots,a_m$$$ ($$$0 leq a_i leq n$$$) — the minimum amount of blocks of sand that needs to fall below the board in each column. In this version of the problem, $$$a_i$$$ will be equal to the number of blocks of sand in column $$$i$$$. Note For example $$$1$$$, by disturbing both blocks of sand on the first row from the top at the first and sixth columns from the left, and the block of sand on the second row from the top and the fourth column from the left, it is possible to have all the required amounts of sand fall in each column. It can be proved that this is not possible with fewer than $$$3$$$ operations, and as such the answer is $$$3$$$. Here is the puzzle from the first example. For example $$$2$$$, by disturbing the cell on the top row and rightmost column, one can cause all of the blocks of sand in the board to fall into the counters at the bottom. Thus, the answer is $$$1$$$. Here is the puzzle from the second example.
2,500
false
true
false
false
false
false
false
false
false
true
2,978
1928E
You are given two integers $$$x$$$ and $$$y$$$. A sequence $$$a$$$ of length $$$n$$$ is called modular if $$$a_1=x$$$, and for all $$$1 < i le n$$$ the value of $$$a_{i}$$$ is either $$$a_{i-1} + y$$$ or $$$a_{i-1} bmod y$$$. Here $$$x bmod y$$$ denotes the remainder from dividing $$$x$$$ by $$$y$$$. Determine if there exists a modular sequence of length $$$n$$$ with the sum of its elements equal to $$$S$$$, and if it exists, find any such sequence. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$). The description of the test cases follows. The first and only line of each test case contains four integers $$$n$$$, $$$x$$$, $$$y$$$, and $$$s$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$0 le x le 2 cdot 10^5$$$, $$$1 le y le 2 cdot 10^5$$$, $$$0 le s le 2 cdot 10^5$$$)xa0— the length of the sequence, the parameters $$$x$$$ and $$$y$$$, and the required sum of the sequence elements. The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$, and also the sum of $$$s$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, if the desired sequence exists, output "Yes" on the first line (without quotes). Then, on the second line, output $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ separated by a spacexa0— the elements of the sequence $$$a$$$. If there are multiple suitable sequences, output any of them. If the sequence does not exist, output "No" on a single line. You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer. Example Input 3 5 8 3 28 3 5 3 6 9 1 5 79 Output YES 8 11 2 2 5 NO NO Note In the first example, the sequence $$$[8, 11, 2, 5, 2]$$$ satisfies the conditions. Thus, $$$a_1 = 8 = x$$$, $$$a_2 = 11 = a_1 + 3$$$, $$$a_3 = 2 = a_2 bmod 3$$$, $$$a_4 = 5 = a_3 + 3$$$, $$$a_5 = 2 = a_4 bmod 3$$$. In the second example, the first element of the sequence should be equal to $$$5$$$, so the sequence $$$[2, 2, 2]$$$ is not suitable.
2,300
true
true
false
true
false
true
true
false
false
true
719
1598D
Monocarp is the coach of the Berland State University programming teams. He decided to compose a problemset for a training session for his teams. Monocarp has $$$n$$$ problems that none of his students have seen yet. The $$$i$$$-th problem has a topic $$$a_i$$$ (an integer from $$$1$$$ to $$$n$$$) and a difficulty $$$b_i$$$ (an integer from $$$1$$$ to $$$n$$$). All problems are different, that is, there are no two tasks that have the same topic and difficulty at the same time. Monocarp decided to select exactly $$$3$$$ problems from $$$n$$$ problems for the problemset. The problems should satisfy at least one of two conditions (possibly, both): the topics of all three selected problems are different; the difficulties of all three selected problems are different. Your task is to determine the number of ways to select three problems for the problemset. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 50000$$$)xa0— the number of testcases. The first line of each testcase contains an integer $$$n$$$ ($$$3 le n le 2 cdot 10^5$$$) — the number of problems that Monocarp have. In the $$$i$$$-th of the following $$$n$$$ lines, there are two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le n$$$)xa0— the topic and the difficulty of the $$$i$$$-th problem. It is guaranteed that there are no two problems that have the same topic and difficulty at the same time. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output Print the number of ways to select three training problems that meet either of the requirements described in the statement. Example Input 2 4 2 4 3 4 2 1 1 3 5 1 5 2 4 3 3 4 2 5 1 Note In the first example, you can take the following sets of three problems: problems $$$1$$$, $$$2$$$, $$$4$$$; problems $$$1$$$, $$$3$$$, $$$4$$$; problems $$$2$$$, $$$3$$$, $$$4$$$. Thus, the number of ways is equal to three.
1,700
true
false
true
false
true
false
false
false
false
false
2,668
1214A
Andrew was very excited to participate in Olympiad of Metropolises. Days flew by quickly, and Andrew is already at the airport, ready to go home. He has $$$n$$$ rubles left, and would like to exchange them to euro and dollar bills. Andrew can mix dollar bills and euro bills in whatever way he wants. The price of one dollar is $$$d$$$ rubles, and one euro costs $$$e$$$ rubles. Recall that there exist the following dollar bills: $$$1$$$, $$$2$$$, $$$5$$$, $$$10$$$, $$$20$$$, $$$50$$$, $$$100$$$, and the following euro billsxa0— $$$5$$$, $$$10$$$, $$$20$$$, $$$50$$$, $$$100$$$, $$$200$$$ (note that, in this problem we do not consider the $$$500$$$ euro bill, it is hard to find such bills in the currency exchange points). Andrew can buy any combination of bills, and his goal is to minimize the total number of rubles he will have after the exchange. Help himxa0— write a program that given integers $$$n$$$, $$$e$$$ and $$$d$$$, finds the minimum number of rubles Andrew can get after buying dollar and euro bills. Input The first line of the input contains one integer $$$n$$$ ($$$1 leq n leq 10^8$$$)xa0— the initial sum in rubles Andrew has. The second line of the input contains one integer $$$d$$$ ($$$30 leq d leq 100$$$)xa0— the price of one dollar in rubles. The third line of the input contains integer $$$e$$$ ($$$30 leq e leq 100$$$)xa0— the price of one euro in rubles. Note In the first example, we can buy just $$$1$$$ dollar because there is no $$$1$$$ euro bill. In the second example, optimal exchange is to buy $$$5$$$ euro and $$$1$$$ dollar. In the third example, optimal exchange is to buy $$$10$$$ dollars in one bill.
1,400
true
false
false
false
false
false
true
false
false
false
4,611
59A
Problem - 59A - 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 strings *800 No tag edit access → Contest materials ") ")
800
false
false
true
false
false
false
false
false
false
false
9,680
743E
Vladik was bored on his way home and decided to play the following game. He took _n_ cards and put them in a row in front of himself. Every card has a positive integer number not exceeding 8 written on it. He decided to find the longest subsequence of cards which satisfies the following conditions: the number of occurrences of each number from 1 to 8 in the subsequence doesn't differ by more then 1 from the number of occurrences of any other number. Formally, if there are _c__k_ cards with number _k_ on them in the subsequence, than for all pairs of integers the condition _c__i_u2009-u2009_c__j_u2009≤u20091 must hold. if there is at least one card with number _x_ on it in the subsequence, then all cards with number _x_ in this subsequence must form a continuous segment in it (but not necessarily a continuous segment in the original sequence). For example, the subsequence [1,u20091,u20092,u20092] satisfies this condition while the subsequence [1,u20092,u20092,u20091] doesn't. Note that [1,u20091,u20092,u20092] doesn't satisfy the first condition. Please help Vladik to find the length of the longest subsequence that satisfies both conditions. Input The first line contains single integer _n_ (1u2009≤u2009_n_u2009≤u20091000)xa0— the number of cards in Vladik's sequence. The second line contains the sequence of _n_ positive integers not exceeding 8xa0— the description of Vladik's sequence. Output Print single integerxa0— the length of the longest subsequence of Vladik's sequence that satisfies both conditions. Examples Input 24 1 8 1 2 8 2 3 8 3 4 8 4 5 8 5 6 8 6 7 8 7 8 8 8 Note In the first sample all the numbers written on the cards are equal, so you can't take more than one card, otherwise you'll violate the first condition.
2,200
false
false
false
true
false
false
true
true
false
false
6,825
1706E
You are given a connected undirected graph with $$$n$$$ vertices and $$$m$$$ edges. Vertices of the graph are numbered by integers from $$$1$$$ to $$$n$$$ and edges of the graph are numbered by integers from $$$1$$$ to $$$m$$$. Your task is to answer $$$q$$$ queries, each consisting of two integers $$$l$$$ and $$$r$$$. The answer to each query is the smallest non-negative integer $$$k$$$ such that the following condition holds: For all pairs of integers $$$(a, b)$$$ such that $$$lle ale ble r$$$, vertices $$$a$$$ and $$$b$$$ are reachable from one another using only the first $$$k$$$ edges (that is, edges $$$1, 2, ldots, k$$$). Input The first line contains a single integer $$$t$$$ ($$$1le tle 1000$$$)xa0— the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$q$$$ ($$$2le nle 10^5$$$, $$$1le m, qle 2cdot 10^5$$$)xa0— the number of vertices, edges, and queries respectively. Each of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1le u_i, v_ile n$$$)xa0— ends of the $$$i$$$-th edge. It is guaranteed that the graph is connected and there are no multiple edges or self-loops. Each of the next $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1le lle rle n$$$)xa0— descriptions of the queries. It is guaranteed that that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$, the sum of $$$m$$$ over all test cases does not exceed $$$2cdot 10^5$$$, and the sum of $$$q$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Note Graph from the first test case. The integer near the edge is its number. In the first test case, the graph contains $$$2$$$ vertices and a single edge connecting vertices $$$1$$$ and $$$2$$$. In the first query, $$$l=1$$$ and $$$r=1$$$. It is possible to reach any vertex from itself, so the answer to this query is $$$0$$$. In the second query, $$$l=1$$$ and $$$r=2$$$. Vertices $$$1$$$ and $$$2$$$ are reachable from one another using only the first edge, through the path $$$1 longleftrightarrow 2$$$. It is impossible to reach vertex $$$2$$$ from vertex $$$1$$$ using only the first $$$0$$$ edges. So, the answer to this query is $$$1$$$. Graph from the second test case. The integer near the edge is its number. In the second test case, the graph contains $$$5$$$ vertices and $$$5$$$ edges. In the first query, $$$l=1$$$ and $$$r=4$$$. It is enough to use the first $$$3$$$ edges to satisfy the condition from the statement: Vertices $$$1$$$ and $$$2$$$ are reachable from one another through the path $$$1 longleftrightarrow 2$$$ (edge $$$1$$$). Vertices $$$1$$$ and $$$3$$$ are reachable from one another through the path $$$1 longleftrightarrow 3$$$ (edge $$$2$$$). Vertices $$$1$$$ and $$$4$$$ are reachable from one another through the path $$$1 longleftrightarrow 2 longleftrightarrow 4$$$ (edges $$$1$$$ and $$$3$$$). Vertices $$$2$$$ and $$$3$$$ are reachable from one another through the path $$$2 longleftrightarrow 1 longleftrightarrow 3$$$ (edges $$$1$$$ and $$$2$$$). Vertices $$$2$$$ and $$$4$$$ are reachable from one another through the path $$$2 longleftrightarrow 4$$$ (edge $$$3$$$). Vertices $$$3$$$ and $$$4$$$ are reachable from one another through the path $$$3 longleftrightarrow 1 longleftrightarrow 2 longleftrightarrow 4$$$ (edges $$$2$$$, $$$1$$$, and $$$3$$$). If we use less than $$$3$$$ of the first edges, then the condition won't be satisfied. For example, it is impossible to reach vertex $$$4$$$ from vertex $$$1$$$ using only the first $$$2$$$ edges. So, the answer to this query is $$$3$$$. In the second query, $$$l=3$$$ and $$$r=4$$$. Vertices $$$3$$$ and $$$4$$$ are reachable from one another through the path $$$3 longleftrightarrow 1 longleftrightarrow 2 longleftrightarrow 4$$$ (edges $$$2$$$, $$$1$$$, and $$$3$$$). If we use any fewer of the first edges, nodes $$$3$$$ and $$$4$$$ will not be reachable from one another.
2,300
false
true
false
false
true
false
false
true
false
false
2,035
518F
On a certain meeting of a ruling party "A" minister Pavel suggested to improve the sewer system and to create a new pipe in the city. The city is an _n_u2009×u2009_m_ rectangular squared field. Each square of the field is either empty (then the pipe can go in it), or occupied (the pipe cannot go in such square). Empty squares are denoted by character '.', occupied squares are denoted by character '#'. The pipe must meet the following criteria: the pipe is a polyline of width 1, the pipe goes in empty squares, the pipe starts from the edge of the field, but not from a corner square, the pipe ends at the edge of the field but not in a corner square, the pipe has at most 2 turns (90 degrees), the border squares of the field must share exactly two squares with the pipe, if the pipe looks like a single segment, then the end points of the pipe must lie on distinct edges of the field, for each non-border square of the pipe there are exacly two side-adjacent squares that also belong to the pipe, for each border square of the pipe there is exactly one side-adjacent cell that also belongs to the pipe. Here are some samples of allowed piping routes: ....# ....# .*..# **** ****. .***. ..#.. ..#*. ..#*. #...# #..*# #..*# ..... ...*. ...*. Here are some samples of forbidden piping routes: .**.# *...# .*.*# ..... ****. .*.*. ..#.. ..#*. .*#*. #...# #..*# #*.*# ..... ...*. .***. In these samples the pipes are represented by characters 'u2009*u2009'. You were asked to write a program that calculates the number of distinct ways to make exactly one pipe in the city. The two ways to make a pipe are considered distinct if they are distinct in at least one square. Input The first line of the input contains two integers _n_,u2009_m_ (2u2009≤u2009_n_,u2009_m_u2009≤u20092000)xa0—xa0the height and width of Berland map. Each of the next _n_ lines contains _m_ characters — the map of the city. If the square of the map is marked by character '.', then the square is empty and the pipe can through it. If the square of the map is marked by character '#', then the square is full and the pipe can't through it. Output In the first line of the output print a single integer — the number of distinct ways to create a pipe. Examples Input 4 5 #...# #...# ###.# ###.# Note In the first sample there are 3 ways to make a pipe (the squares of the pipe are marked by characters 'u2009*u2009'): .*. .*. ... .*# **# **# .*. ... .*.
2,300
false
false
true
true
false
false
true
true
false
false
7,763
2007A
Dora has a set $$$s$$$ containing integers. In the beginning, she will put all integers in $$$xa0— the number of test cases. The description of the test cases follows. The only line of each test case contains two integers $$$l$$$ and $$$r$$$ ($$$1 leq l leq r leq 1000$$$)xa0— the range of integers in the initial set. Note In the first test case, you can choose $$$a = 1$$$, $$$b = 2$$$, $$$c = 3$$$ in the only operation, since $$$gcd(1, 2) = gcd(2, 3) = gcd(1, 3) = 1$$$, and then there are no more integers in the set, so no more operations can be performed. In the second test case, you can choose $$$a = 3$$$, $$$b = 5$$$, $$$c = 7$$$ in the only operation. In the third test case, you can choose $$$a = 11$$$, $$$b = 19$$$, $$$c = 20$$$ in the first operation, $$$a = 13$$$, $$$b = 14$$$, $$$c = 15$$$ in the second operation, and $$$a = 10$$$, $$$b = 17$$$, $$$c = 21$$$ in the third operation. After the three operations, the set $$$s$$$ contains the following integers: $$$12$$$, $$$16$$$, $$$18$$$. It can be proven that it's impossible to perform more than $$$3$$$ operations.
800
true
true
false
false
false
false
false
false
false
false
211
330B
A country has _n_ cities. Initially, there is no road in the country. One day, the king decides to construct some roads connecting pairs of cities. Roads can be traversed either way. He wants those roads to be constructed in such a way that it is possible to go from each city to any other city by traversing at most two roads. You are also given _m_ pairs of cities — roads cannot be constructed between these pairs of cities. Your task is to construct the minimum number of roads that still satisfy the above conditions. The constraints will guarantee that this is always possible. Input The first line consists of two integers _n_ and _m_ . Then _m_ lines follow, each consisting of two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_), which means that it is not possible to construct a road connecting cities _a__i_ and _b__i_. Consider the cities are numbered from 1 to _n_. It is guaranteed that every pair of cities will appear at most once in the input. Output You should print an integer _s_: the minimum number of roads that should be constructed, in the first line. Then _s_ lines should follow, each consisting of two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_), which means that a road should be constructed between cities _a__i_ and _b__i_. If there are several solutions, you may print any of them. Note This is one possible solution of the example: These are examples of wrong solutions: The above solution is wrong because it doesn't use the minimum number of edges (4 vs 3). In addition, it also tries to construct a road between cities 1 and 3, while the input specifies that it is not allowed to construct a road between the pair. The above solution is wrong because you need to traverse at least 3 roads to go from city 1 to city 3, whereas in your country it must be possible to go from any city to another by traversing at most 2 roads. Finally, the above solution is wrong because it must be possible to go from any city to another, whereas it is not possible in this country to go from city 1 to 3, 2 to 3, and 4 to 3.
1,300
false
false
false
false
false
true
false
false
false
true
8,523
1510B
You are standing in front of the room with great treasures. The only thing stopping you is the door with a push-button combination lock. This lock has $$$d$$$ buttons with digits from $$$0$$$ to $$$d - 1$$$. Whenever you press a button, it stays pushed down. You can not pop back up just one button, but there is a "RESET" buttonxa0— pressing it pops up all other buttons. Initially, no buttons are pushed down. The door instantly opens when some specific set of digits is pushed down. Sadly, you don't know the password for it. Having read the documentation for this specific lock, you found out that there are $$$n$$$ possible passwords for this particular lock. Find the shortest sequence of button presses, such that all possible passwords appear at least once during its execution. Any shortest correct sequence of button presses will be accepted. Input The first line contains two integers $$$d$$$ and $$$n$$$ ($$$1 le d le 10$$$; $$$1 le n le 2^d - 1$$$). Next $$$n$$$ lines describe possible passwords. Each line contains a string $$$s_i$$$ of $$$d$$$ zeros and ones: for all $$$j$$$ from $$$1$$$ to $$$d$$$ the $$$j$$$-th character is 1 iff the button with the digit $$$j - 1$$$ must be pushed down. All strings $$$s_i$$$ are different, and each string contains at least one 1. Output On the first line, print the number $$$k$$$xa0— the minimum number of button presses. On the second line, print $$$k$$$ tokens, describing the sequence. Whenever you press a button with a digit, print that digit. Whenever you press "RESET", print "R". Note In the second example, the sequence 1 2 R 2 0 1 is also possible.
2,600
false
false
false
false
false
false
false
false
false
true
3,126
266D
BerDonalds, a well-known fast food restaurant, is going to open a cafe in Bertown. The important thing is to choose the new restaurant's location so that it would be easy to get there. The Bertown road system is represented by _n_ junctions, connected by _m_ bidirectional roads. For each road we know its length. We also know that we can get from any junction to any other one, moving along the roads. Your task is to find such location of the restaurant, that the shortest distance along the roads from the cafe to the farthest junction would be minimum. Note that the restaurant can be located not only on the junction, but at any point of any road. Input The first line contains two integers _n_ and _m_ () — the number of junctions and the number of roads, correspondingly. Then _m_ lines follow, describing all Bertown roads. Each road is described by three integers _a__i_,u2009_b__i_,u2009_w__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_;xa01u2009≤u2009_w__i_u2009≤u2009105), where _a__i_ and _b__i_ are the numbers of the junctions, connected by the _i_-th road, and _w__i_ is the length of the _i_-th road. It is guaranteed that each road connects two distinct junctions, there is at most one road between any two junctions, and you can get from any junction to any other one. Output Print a single real number — the shortest distance from the optimal restaurant location to the farthest junction. The answer will be considered correct, if its absolute or relative error doesn't exceed 10u2009-u20099.
2,400
true
false
false
false
false
false
false
false
false
true
8,768
1628A
Mihai has just learned about the . Append the MEX of the first $$$k$$$ numbers of the array $$$a$$$ to the end of array $$$b$$$ and erase them from the array $$$a$$$, shifting the positions of the remaining numbers in $$$a$$$. But, since Mihai loves big arrays as much as the MEX concept, he wants the new array $$$b$$$ to be the lexicographically maximum. So, Mihai asks you to tell him what the maximum array $$$b$$$ that can be created by constructing the array optimally is. An array $$$x$$$ is lexicographically greater than an array $$$y$$$ if in the first position where $$$x$$$ and $$$y$$$ differ $$$x_i > y_i$$$ or if $$$x > y$$$ and $$$y$$$ is a prefix of $$$x$$$ (where $$$x$$$ denotes the size of the array $$$x$$$). The MEX of a set of non-negative integers is the minimal non-negative integer such that it is not in the set. For example, MEX({$$${1, 2, 3}$$$}) $$$= 0$$$ and MEX({$$${0, 1, 2, 4, 5}$$$}) $$$= 3$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$) — the number of elements in the array $$$a$$$. The second line of each test case contains $$$n$$$ non-negative integers $$$a_1, ldots, a_n$$$ ($$$0 leq a_i leq n$$$), where $$$a_i$$$ is the $$$i$$$-th integer from the array $$$a$$$. 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 $$$m$$$ — the length of the maximum array $$$b$$$ Mihai can create, followed by $$$m$$$ integers denoting the elements of the array $$$b$$$. Example Input 6 5 1 0 2 0 3 8 2 2 3 4 0 1 2 0 1 1 5 0 1 2 3 4 4 0 1 1 0 10 0 0 2 1 1 1 0 0 1 1 Output 1 4 2 5 1 1 0 1 5 2 2 2 4 3 2 2 0 Note In the first test case, the lexicographically maximum array $$$b$$$ is obtained by selecting $$$k=5$$$, resulting in the $$$MEX$$$ of the whole array $$$a$$$. It is lexicographically maximum because an array starting with a smaller number than $$$4$$$ is lexicographically smaller, and choosing a $$$k<5$$$ would result in an array starting with a number smaller than $$$4$$$. In the second test case, there are two ways to obtain the maximum array: first selecting $$$k=6$$$, then $$$k=2$$$, or first selecting $$$k=7$$$ and then $$$k=1$$$.
1,400
true
true
true
false
false
true
false
true
false
false
2,475
1196C
$$$n$$$ robots have escaped from your laboratory! You have to find them as soon as possible, because these robots are experimental, and their behavior is not tested yet, so they may be really dangerous! Fortunately, even though your robots have escaped, you still have some control over them. First of all, you know the location of each robot: the world you live in can be modeled as an infinite coordinate plane, and the $$$i$$$-th robot is currently located at the point having coordinates ($$$x_i$$$, $$$y_i$$$). Furthermore, you may send exactly one command to all of the robots. The command should contain two integer numbers $$$X$$$ and $$$Y$$$, and when each robot receives this command, it starts moving towards the point having coordinates ($$$X$$$, $$$Y$$$). The robot stops its movement in two cases: either it reaches ($$$X$$$, $$$Y$$$); or it cannot get any closer to ($$$X$$$, $$$Y$$$). Normally, all robots should be able to get from any point of the coordinate plane to any other point. Each robot usually can perform four actions to move. Let's denote the current coordinates of the robot as ($$$x_c$$$, $$$y_c$$$). Then the movement system allows it to move to any of the four adjacent points: 1. the first action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c - 1$$$, $$$y_c$$$); 2. the second action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c$$$, $$$y_c + 1$$$); 3. the third action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c + 1$$$, $$$y_c$$$); 4. the fourth action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c$$$, $$$y_c - 1$$$). Unfortunately, it seems that some movement systems of some robots are malfunctioning. For each robot you know which actions it can perform, and which it cannot perform. You want to send a command so all robots gather at the same point. To do so, you have to choose a pair of integer numbers $$$X$$$ and $$$Y$$$ so that each robot can reach the point ($$$X$$$, $$$Y$$$). Is it possible to find such a point? Input The first line contains one integer $$$q$$$ ($$$1 le q le 10^5$$$)xa0— the number of queries. Then $$$q$$$ queries follow. Each query begins with one line containing one integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the number of robots in the query. Then $$$n$$$ lines follow, the $$$i$$$-th of these lines describes the $$$i$$$-th robot in the current query: it contains six integer numbers $$$x_i$$$, $$$y_i$$$, $$$f_{i, 1}$$$, $$$f_{i, 2}$$$, $$$f_{i, 3}$$$ and $$$f_{i, 4}$$$ ($$$-10^5 le x_i, y_i le 10^5$$$, $$$0 le f_{i, j} le 1$$$). The first two numbers describe the initial location of the $$$i$$$-th robot, and the following four numbers describe which actions the $$$i$$$-th robot can use to move ($$$f_{i, j} = 1$$$ if the $$$i$$$-th robot can use the $$$j$$$-th action, and $$$f_{i, j} = 0$$$ if it cannot use the $$$j$$$-th action). It is guaranteed that the total number of robots over all queries does not exceed $$$10^5$$$. Output You should answer each query independently, in the order these queries appear in the input. To answer a query, you should do one of the following: if it is impossible to find a point that is reachable by all $$$n$$$ robots, print one number $$$0$$$ on a separate line; if it is possible to find a point that is reachable by all $$$n$$$ robots, print three space-separated integers on the same line: $$$1$$$ $$$X$$$ $$$Y$$$, where $$$X$$$ and $$$Y$$$ are the coordinates of the point reachable by all $$$n$$$ robots. Both $$$X$$$ and $$$Y$$$ should not exceed $$$10^5$$$ by absolute value; it is guaranteed that if there exists at least one point reachable by all robots, then at least one of such points has both coordinates not exceeding $$$10^5$$$ by absolute value.
1,500
false
false
true
false
false
false
false
false
false
false
4,720
1227D2
This is the harder version of the problem. In this version, $$$1 le n, m le 2cdot10^5$$$. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems. You are given a sequence of integers $$$a=[a_1,a_2,dots,a_n]$$$ of length $$$n$$$. Its subsequence is obtained by removing zero or more elements from the sequence $$$a$$$ (they do not necessarily go consecutively). For example, for the sequence $$$a=[11,20,11,33,11,20,11]$$$: $$$[11,20,11,33,11,20,11]$$$, $$$[11,20,11,33,11,20]$$$, $$$[11,11,11,11]$$$, $$$[20]$$$, $$$[33,20]$$$ are subsequences (these are just some of the long list); $$$[40]$$$, $$$[33,33]$$$, $$$[33,20,20]$$$, $$$[20,20,11,11]$$$ are not subsequences. Suppose that an additional non-negative integer $$$k$$$ ($$$1 le k le n$$$) is given, then the subsequence is called optimal if: it has a length of $$$k$$$ and the sum of its elements is the maximum possible among all subsequences of length $$$k$$$; and among all subsequences of length $$$k$$$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $$$b=[b_1, b_2, dots, b_k]$$$ is lexicographically smaller than the sequence $$$c=[c_1, c_2, dots, c_k]$$$ if the first element (from the left) in which they differ less in the sequence $$$b$$$ than in $$$c$$$. Formally: there exists $$$t$$$ ($$$1 le t le k$$$) such that $$$b_1=c_1$$$, $$$b_2=c_2$$$, ..., $$$b_{t-1}=c_{t-1}$$$ and at the same time $$$b_t<c_t$$$. For example: $$$[10, 20, 20]$$$ lexicographically less than $$$[10, 21, 1]$$$, $$$[7, 99, 99]$$$ is lexicographically less than $$$[10, 21, 1]$$$, $$$[10, 21, 0]$$$ is lexicographically less than $$$[10, 21, 1]$$$. You are given a sequence of $$$a=[a_1,a_2,dots,a_n]$$$ and $$$m$$$ requests, each consisting of two numbers $$$k_j$$$ and $$$pos_j$$$ ($$$1 le k le n$$$, $$$1 le pos_j le k_j$$$). For each query, print the value that is in the index $$$pos_j$$$ of the optimal subsequence of the given sequence $$$a$$$ for $$$k=k_j$$$. For example, if $$$n=4$$$, $$$a=[10,20,30,20]$$$, $$$k_j=2$$$, then the optimal subsequence is $$$[20,30]$$$ — it is the minimum lexicographically among all subsequences of length $$$2$$$ with the maximum total sum of items. Thus, the answer to the request $$$k_j=2$$$, $$$pos_j=1$$$ is the number $$$20$$$, and the answer to the request $$$k_j=2$$$, $$$pos_j=2$$$ is the number $$$30$$$. Input The first line contains an integer $$$n$$$ ($$$1 le n le 2cdot10^5$$$) — the length of the sequence $$$a$$$. The second line contains elements of the sequence $$$a$$$: integer numbers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$). The third line contains an integer $$$m$$$ ($$$1 le m le 2cdot10^5$$$) — the number of requests. The following $$$m$$$ lines contain pairs of integers $$$k_j$$$ and $$$pos_j$$$ ($$$1 le k le n$$$, $$$1 le pos_j le k_j$$$) — the requests.
1,800
false
true
false
false
true
false
false
false
false
false
4,540
2006B
Given a rooted tree with the root at vertex $$$1$$$. For any vertex $$$i$$$ ($$$1 < i leq n$$$) in the tree, there is an edge connecting vertices $$$i$$$ and $$$p_i$$$ ($$$1 leq p_i < i$$$), with a weight equal to $$$t_i$$$. Iris does not know the values of $$$t_i$$$, but she knows that $$$displaystylesum_{i=2}^n t_i = w$$$ and each of the $$$t_i$$$ is a non-negative integer. The vertices of the tree are numbered in a special way: the numbers of the vertices in each subtree are consecutive integers. In other words, the vertices of the tree are numbered in the order of a depth-first search. The tree in this picture satisfies the condition. For example, in the subtree of vertex $$$2$$$, the vertex numbers are $$$2, 3, 4, 5$$$, which are consecutive integers. The tree in this picture does not satisfy the condition, as in the subtree of vertex $$$2$$$, the vertex numbers $$$2$$$ and $$$4$$$ are not consecutive integers. We define $$$operatorname{dist}(u, v)$$$ as the length of the simple path between vertices $$$u$$$ and $$$v$$$ in the tree. Next, there will be $$$n - 1$$$ events: Iris is given integers $$$x$$$ and $$$y$$$, indicating that $$$t_x = y$$$. After each event, Iris wants to know the maximum possible value of $$$operatorname{dist}(i, i bmod n + 1)$$$ independently for each $$$i$$$ ($$$1le ile n$$$). She only needs to know the sum of these $$$n$$$ values. Please help Iris quickly get the answers. Note that when calculating the maximum possible values of $$$operatorname{dist}(i, i bmod n + 1)$$$ and $$$operatorname{dist}(j, j bmod n + 1)$$$ for $$$i e j$$$, the unknown edge weights may be different. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$w$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$0 leq w leq 10^{12}$$$)xa0— the number of vertices in the tree and the sum of the edge weights. The second line of each test case contains $$$n - 1$$$ integers $$$p_2, p_3, ldots, p_n$$$ ($$$1 leq p_i < i$$$)xa0— the description of the edges of the tree. Then follow $$$n-1$$$ lines indicating the events. Each line contains two integers $$$x$$$ and $$$y$$$ ($$$2 leq x leq n$$$, $$$0 leq y leq w$$$), indicating that $$$t_x = y$$$. It is guaranteed that all $$$x$$$ in the events are distinct. It is also guaranteed that the sum of all $$$y$$$ equals $$$w$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output one line containing $$$n-1$$$ integers, each representing the answer after each event. Example Input 4 2 1000000000000 1 2 1000000000000 4 9 1 1 1 2 2 4 4 3 3 6 100 1 2 3 2 1 6 17 3 32 2 4 4 26 5 21 10 511 1 2 2 4 2 1 1 8 8 3 2 6 16 10 256 9 128 2 1 5 8 8 64 4 4 7 32 Output 2000000000000 25 18 18 449 302 247 200 200 4585 4473 2681 1567 1454 1322 1094 1022 1022 Note In the first test case, $$$operatorname{dist}(1, 2) = operatorname{dist}(2, 1) = t_2 = w = 10^{12}$$$, so $$$operatorname{dist}(1, 2) + operatorname{dist}(2, 1) = 2 cdot 10^{12}$$$. In the second test case, the tree after Iris found out all $$$t_x$$$ is shown below: $$$operatorname{dist}(1, 2) = t_2$$$, $$$operatorname{dist}(2, 3) = t_2 + t_3$$$, $$$operatorname{dist}(3, 4) = t_3 + t_4$$$, $$$operatorname{dist}(4, 1) = t_4$$$. After the first event, she found out that $$$t_2 = 2$$$, so $$$operatorname{dist}(1, 2) = 2$$$. At the same time: $$$operatorname{dist}(2, 3)$$$ is maximized if $$$t_3 = 7$$$, $$$t_4 = 0$$$. Then $$$operatorname{dist}(2, 3) = 9$$$. $$$operatorname{dist}(3, 4)$$$ and $$$operatorname{dist}(4, 1)$$$ are maximized if $$$t_3 = 0$$$, $$$t_4 = 7$$$. Then $$$operatorname{dist}(3, 4) = operatorname{dist}(4, 1) = 7$$$. Thus, the answer is $$$2 + 9 + 7 + 7 = 25$$$. After the second event, she found out that $$$t_4 = 4$$$, then $$$t_3 = w - t_2 - t_4 = 4$$$. $$$operatorname{dist}(1, 2) = 2$$$, $$$operatorname{dist}(2, 3) = 2 + 3 = 5$$$, $$$operatorname{dist}(3, 4) = 3 + 4 = 7$$$, $$$operatorname{dist}(4, 1) = 4$$$. Thus, the answer is $$$2 + 5 + 7 + 4 = 18$$$.
1,800
true
false
false
false
true
false
true
false
false
false
216
1388D
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this... There are two arrays $$$a$$$ and $$$b$$$ of length $$$n$$$. Initially, an $$$ans$$$ is equal to $$$0$$$ and the following operation is defined: 1. Choose position $$$i$$$ ($$$1 le i le n$$$); 2. Add $$$a_i$$$ to $$$ans$$$; 3. If $$$b_i eq -1$$$ then add $$$a_i$$$ to $$$a_{b_i}$$$. What is the maximum $$$ans$$$ you can get by performing the operation on each $$$i$$$ ($$$1 le i le n$$$) exactly once? Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them. Input The first line contains the integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the length of arrays $$$a$$$ and $$$b$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$−10^6 le a_i le 10^6$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le n$$$ or $$$b_i = -1$$$). Additional constraint: it's guaranteed that for any $$$i$$$ ($$$1 le i le n$$$) the sequence $$$b_i, b_{b_i}, b_{b_{b_i}}, ldots$$$ is not cyclic, in other words it will always end with $$$-1$$$. Output In the first line, print the maximum $$$ans$$$ you can get. In the second line, print the order of operations: $$$n$$$ different integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$). The $$$p_i$$$ is the position which should be chosen at the $$$i$$$-th step. If there are multiple orders, print any of them. Examples Input 10 -10 -1 2 2 5 -2 -3 -4 2 -6 -1 -1 2 2 -1 5 5 7 7 9 Output -9 3 5 6 1 9 4 10 7 8 2
2,000
false
true
true
false
true
false
false
false
false
true
3,730
1076G
Consider a following game between two players: There is an array $$$b_1$$$, $$$b_2$$$, ..., $$$b_k$$$, consisting of positive integers. Initially a chip is placed into the first cell of the array, and $$$b_1$$$ is decreased by $$$1$$$. Players move in turns. Each turn the current player has to do the following: if the index of the cell where the chip is currently placed is $$$x$$$, then he or she has to choose an index $$$y in [x, min(k, x + m)]$$$ such that $$$b_y > 0$$$, move the chip to the cell $$$y$$$ and decrease $$$b_y$$$ by $$$1$$$. If it's impossible to make a valid move, the current player loses the game. Your task is the following: you are given an array $$$a$$$ consisting of $$$n$$$ positive integers, and $$$q$$$ queries to it. There are two types of queries: $$$1$$$ $$$l$$$ $$$r$$$ $$$d$$$ — for every $$$i in [l, r]$$$ increase $$$a_i$$$ by $$$d$$$; $$$2$$$ $$$l$$$ $$$r$$$ — tell who is the winner of the game that is played on the subarray of $$$a$$$ from index $$$l$$$ to index $$$r$$$ inclusive. Assume both players choose an optimal strategy. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 le n, q le 2 cdot 10^5$$$, $$$1 le m le 5$$$) — the number of elements in $$$a$$$, the parameter described in the game and the number of queries, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 le a_i le 10^{12}$$$) — the elements of array $$$a$$$. Then $$$q$$$ lines follow, each containing a query. There are two types of queries. The query of the first type is denoted by a line $$$1$$$ $$$l$$$ $$$r$$$ $$$d$$$ ($$$1 le l le r le n$$$, $$$1 le d le 10^{12}$$$) and means that for every $$$i in [l, r]$$$ you should increase $$$a_i$$$ by $$$d$$$. The query of the second type is denoted by a line $$$2$$$ $$$l$$$ $$$r$$$ ($$$1 le l le r le n$$$) and means that you have to determine who will win the game if it is played on the subarray of $$$a$$$ from index $$$l$$$ to index $$$r$$$ (inclusive). There is at least one query of type $$$2$$$. Output For each query of type $$$2$$$ print $$$1$$$ if the first player wins in the corresponding game, or $$$2$$$ if the second player wins. Examples Input 5 2 4 1 2 3 4 5 1 3 5 6 2 2 5 1 1 2 3 2 1 5 Input 5 1 3 1 1 3 3 4 2 1 5 2 2 5 2 3 5
3,000
false
false
false
false
true
false
false
false
false
false
5,338
922E
Apart from plush toys, Imp is a huge fan of little yellow birds! To summon birds, Imp needs strong magic. There are _n_ trees in a row on an alley in a park, there is a nest on each of the trees. In the _i_-th nest there are _c__i_ birds; to summon one bird from this nest Imp needs to stay under this tree and it costs him _cost__i_ points of mana. However, for each bird summoned, Imp increases his mana capacity by _B_ points. Imp summons birds one by one, he can summon any number from 0 to _c__i_ birds from the _i_-th nest. Initially Imp stands under the first tree and has _W_ points of mana, and his mana capacity equals _W_ as well. He can only go forward, and each time he moves from a tree to the next one, he restores _X_ points of mana (but it can't exceed his current mana capacity). Moving only forward, what is the maximum number of birds Imp can summon? Input The first line contains four integers _n_, _W_, _B_, _X_ (1u2009≤u2009_n_u2009≤u2009103,u20090u2009≤u2009_W_,u2009_B_,u2009_X_u2009≤u2009109)xa0— the number of trees, the initial points of mana, the number of points the mana capacity increases after a bird is summoned, and the number of points restored when Imp moves from a tree to the next one. The second line contains _n_ integers _c_1,u2009_c_2,u2009...,u2009_c__n_ (0u2009≤u2009_c__i_u2009≤u2009104)xa0— where _c__i_ is the number of birds living in the _i_-th nest. It is guaranteed that . The third line contains _n_ integers _cost_1,u2009_cost_2,u2009...,u2009_cost__n_ (0u2009≤u2009_cost__i_u2009≤u2009109), where _cost__i_ is the mana cost to summon a bird from the _i_-th nest. Output Print a single integerxa0— the maximum number of birds Imp can summon. Examples Input 4 1000 10 35 1 2 4 5 1000 500 250 200 Note In the first sample base amount of Imp's mana is equal to 12 (with maximum capacity also equal to 12). After he summons two birds from the first nest, he loses 8 mana points, although his maximum capacity will not increase (since _B_u2009=u20090). After this step his mana will be 4 of 12; during the move you will replenish 4 mana points, and hence own 8 mana out of 12 possible. Now it's optimal to take 4 birds from the second nest and spend 8 mana. The final answer will be — 6. In the second sample the base amount of mana is equal to 1000. The right choice will be to simply pick all birds from the last nest. Note that Imp's mana doesn't restore while moving because it's initially full.
2,200
false
false
false
true
false
false
false
false
false
false
6,025
859D
The annual college sports-ball tournament is approaching, which for trademark reasons we'll refer to as Third Month Insanity. There are a total of 2_N_ teams participating in the tournament, numbered from 1 to 2_N_. The tournament lasts _N_ rounds, with each round eliminating half the teams. The first round consists of 2_N_u2009-u20091 games, numbered starting from 1. In game _i_, team 2·_i_u2009-u20091 will play against team 2·_i_. The loser is eliminated and the winner advances to the next round (there are no ties). Each subsequent round has half as many games as the previous round, and in game _i_ the winner of the previous round's game 2·_i_u2009-u20091 will play against the winner of the previous round's game 2·_i_. Every year the office has a pool to see who can create the best bracket. A bracket is a set of winner predictions for every game. For games in the first round you may predict either team to win, but for games in later rounds the winner you predict must also be predicted as a winner in the previous round. Note that the bracket is fully constructed before any games are actually played. Correct predictions in the first round are worth 1 point, and correct predictions in each subsequent round are worth twice as many points as the previous, so correct predictions in the final game are worth 2_N_u2009-u20091 points. For every pair of teams in the league, you have estimated the probability of each team winning if they play against each other. Now you want to construct a bracket with the maximum possible expected score. Input Input will begin with a line containing _N_ (2u2009≤u2009_N_u2009≤u20096). 2_N_ lines follow, each with 2_N_ integers. The _j_-th column of the _i_-th row indicates the percentage chance that team _i_ will defeat team _j_, unless _i_u2009=u2009_j_, in which case the value will be 0. It is guaranteed that the _i_-th column of the _j_-th row plus the _j_-th column of the _i_-th row will add to exactly 100. Output Print the maximum possible expected score over all possible brackets. Your answer must be correct to within an absolute or relative error of 10u2009-u20099. Formally, let your answer be _a_, and the jury's answer be _b_. Your answer will be considered correct, if . Examples Input 2 0 40 100 100 60 0 40 40 0 60 0 45 0 60 55 0 Input 3 0 0 100 0 100 0 0 0 100 0 100 0 0 0 100 100 0 0 0 100 100 0 0 0 100 100 0 0 0 0 100 100 0 100 0 100 0 0 100 0 100 100 100 100 100 0 0 0 100 0 100 0 0 100 0 0 100 0 100 0 100 100 100 0 Input 2 0 21 41 26 79 0 97 33 59 3 0 91 74 67 9 0 Note In the first example, you should predict teams 1 and 4 to win in round 1, and team 1 to win in round 2. Recall that the winner you predict in round 2 must also be predicted as a winner in round 1.
2,100
false
false
false
true
false
false
false
false
false
false
6,305
1746A
You have an array $$$a$$$ of size $$$n$$$ consisting only of zeroes and ones and an integer $$$k$$$. In one operation you can do one of the following: Select $$$2$$$ consecutive elements of $$$a$$$ and replace them with their minimum (that is, let $$$a := [a_{1}, a_{2}, ldots, a_{i-1}, min(a_{i}, a_{i+1}), a_{i+2}, ldots, a_{n}]$$$ for some $$$1 le i le n-1$$$). This operation decreases the size of $$$a$$$ by $$$1$$$. Select $$$k$$$ consecutive elements of $$$a$$$ and replace them with their maximum (that is, let $$$a := [a_{1}, a_{2}, ldots, a_{i-1}, max(a_{i}, a_{i+1}, ldots, a_{i+k-1}), a_{i+k}, ldots, a_{n}]$$$ for some $$$1 le i le n-k+1$$$). This operation decreases the size of $$$a$$$ by $$$k-1$$$. Determine if it's possible to turn $$$a$$$ into $$$[1]$$$ after several (possibly zero) operations. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 le k le n le 50$$$), the size of array $$$a$$$ and the length of segments that you can perform second type operation on. The second line contains $$$n$$$ integers $$$a_{1}, a_{2}, ldots, a_{n}$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$), elements of array $$$a$$$. Output For each test case, if it is possible to turn $$$a$$$ into $$$[1]$$$, print "YES", otherwise print "NO". Example Input 7 3 2 0 1 0 5 3 1 0 1 1 0 2 2 1 1 4 4 0 0 0 0 6 3 0 0 1 0 0 1 7 5 1 1 1 1 1 1 1 5 3 0 0 1 0 0 Output YES YES YES NO YES YES YES Note In the first test case, you can perform the second type operation on second and third elements so $$$a$$$ becomes $$$[0, 1]$$$, then you can perform the second type operation on first and second elements, so $$$a$$$ turns to $$$[1]$$$. In the fourth test case, it's obvious to see that you can't make any $$$1$$$, no matter what you do. In the fifth test case, you can first perform a type 2 operation on the first three elements so that $$$a$$$ becomes $$$[1, 0, 0, 1]$$$, then perform a type 2 operation on the elements in positions two through four, so that $$$a$$$ becomes $$$[1, 1]$$$, and finally perform the first type operation on the remaining elements, so that $$$a$$$ becomes $$$[1]$$$.
800
false
true
false
false
false
true
false
false
false
false
1,810
1017B
Rudolf is on his way to the castle. Before getting into the castle, the security staff asked him a question: Given two binary numbers $$$a$$$ and $$$b$$$ of length $$$n$$$. How many different ways of swapping two digits in $$$a$$$ (only in $$$a$$$, not $$$b$$$) so that bitwise OR of these two numbers will be changed? In other words, let $$$c$$$ be the bitwise OR of $$$a$$$ and $$$b$$$, you need to find the number of ways of swapping two bits in $$$a$$$ so that bitwise OR will not be equal to $$$c$$$. Note that binary numbers can contain leading zeros so that length of each number is exactly $$$n$$$. xa0— the number of bits in each number. The second line contains a binary number $$$a$$$ of length $$$n$$$. The third line contains a binary number $$$b$$$ of length $$$n$$$. Output Print the number of ways to swap two bits in $$$a$$$ so that bitwise OR will be changed. Note In the first sample, you can swap bits that have indexes $$$(1, 4)$$$, $$$(2, 3)$$$, $$$(3, 4)$$$, and $$$(3, 5)$$$. In the second example, you can swap bits that have indexes $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$, $$$(3, 5)$$$, and $$$(3, 6)$$$.
1,200
true
false
true
false
false
false
false
false
false
false
5,604
999F
There are $$$n$$$ players sitting at the card table. Each player has a favorite number. The favorite number of the $$$j$$$-th player is $$$f_j$$$. There are $$$k cdot n$$$ cards on the table. Each card contains a single integer: the $$$i$$$-th card contains number $$$c_i$$$. Also, you are given a sequence $$$h_1, h_2, dots, h_k$$$. Its meaning will be explained below. The players have to distribute all the cards in such a way that each of them will hold exactly $$$k$$$ cards. After all the cards are distributed, each player counts the number of cards he has that contains his favorite number. The joy level of a player equals $$$h_t$$$ if the player holds $$$t$$$ cards containing his favorite number. If a player gets no cards with his favorite number (i.e., $$$t=0$$$), his joy level is $$$0$$$. Print the maximum possible total joy levels of the players after the cards are distributed. Note that the sequence $$$h_1, dots, h_k$$$ is the same for all the players. Input The first line of input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 500, 1 le k le 10$$$) — the number of players and the number of cards each player will get. The second line contains $$$k cdot n$$$ integers $$$c_1, c_2, dots, c_{k cdot n}$$$ ($$$1 le c_i le 10^5$$$) — the numbers written on the cards. The third line contains $$$n$$$ integers $$$f_1, f_2, dots, f_n$$$ ($$$1 le f_j le 10^5$$$) — the favorite numbers of the players. The fourth line contains $$$k$$$ integers $$$h_1, h_2, dots, h_k$$$ ($$$1 le h_t le 10^5$$$), where $$$h_t$$$ is the joy level of a player if he gets exactly $$$t$$$ cards with his favorite number written on them. It is guaranteed that the condition $$$h_{t - 1} < h_t$$$ holds for each $$$t in [2..k]$$$. Output Print one integer — the maximum possible total joy levels of the players among all possible card distributions. Examples Input 4 3 1 3 2 8 5 5 8 2 2 8 5 2 1 2 2 5 2 6 7 Input 3 3 9 9 9 9 9 9 9 9 9 1 2 3 1 2 3 Note In the first example, one possible optimal card distribution is the following: Player $$$1$$$ gets cards with numbers $$$[1, 3, 8]$$$; Player $$$2$$$ gets cards with numbers $$$[2, 2, 8]$$$; Player $$$3$$$ gets cards with numbers $$$[2, 2, 8]$$$; Player $$$4$$$ gets cards with numbers $$$[5, 5, 5]$$$. Thus, the answer is $$$2 + 6 + 6 + 7 = 21$$$. In the second example, no player can get a card with his favorite number. Thus, the answer is $$$0$$$.
2,000
false
false
false
true
false
false
false
false
false
false
5,706
681B
Kolya is developing an economy simulator game. His most favourite part of the development process is in-game testing. Once he was entertained by the testing so much, that he found out his game-coin score become equal to 0. Kolya remembers that at the beginning of the game his game-coin score was equal to _n_ and that he have bought only some houses (for 1u2009234u2009567 game-coins each), cars (for 123u2009456 game-coins each) and computers (for 1u2009234 game-coins each). Kolya is now interested, whether he could have spent all of his initial _n_ game-coins buying only houses, cars and computers or there is a bug in the game. Formally, is there a triple of non-negative integers _a_, _b_ and _c_ such that _a_u2009×u20091u2009234u2009567u2009+u2009_b_u2009×u2009123u2009456u2009+u2009_c_u2009×u20091u2009234u2009=u2009_n_? Please help Kolya answer this question. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009109)xa0— Kolya's initial game-coin score. Output Print "YES" (without quotes) if it's possible that Kolya spent all of his initial _n_ coins buying only houses, cars and computers. Otherwise print "NO" (without quotes). Note In the first sample, one of the possible solutions is to buy one house, one car and one computer, spending 1u2009234u2009567u2009+u2009123u2009456u2009+u20091234u2009=u20091u2009359u2009257 game-coins in total.
1,300
false
false
false
false
false
false
true
false
false
false
7,102
32C
Problem - 32C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math *1700 No tag edit access → Contest materials ") . She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board. The flea can count the amount of cells that she can reach from the starting position (_x_,u2009_y_). Let's denote this amount by _d__x_,u2009_y_. Your task is to find the number of such starting positions (_x_,u2009_y_), which have the maximum possible value of _d__x_,u2009_y_. Input The first line contains three integers _n_, _m_, _s_ (1u2009≤u2009_n_,u2009_m_,u2009_s_u2009≤u2009106) — length of the board, width of the board and length of the flea's jump. Output Output the only integer — the number of the required starting positions of the flea. Examples Input 2 3 1000000 Output 6 Input 3 3 2 Output 4
1,700
true
false
false
false
false
false
false
false
false
false
9,836
847A
Doubly linked list is one of the fundamental data structures. A doubly linked list is a sequence of elements, each containing information about the previous and the next elements of the list. In this problem all lists have linear structure. I.e. each element except the first has exactly one previous element, each element except the last has exactly one next element. The list is not closed in a cycle. In this problem you are given _n_ memory cells forming one or more doubly linked lists. Each cell contains information about element from some list. Memory cells are numbered from 1 to _n_. For each cell _i_ you are given two values: _l__i_ — cell containing previous element for the element in the cell _i_; _r__i_ — cell containing next element for the element in the cell _i_. If cell _i_ contains information about the element which has no previous element then _l__i_u2009=u20090. Similarly, if cell _i_ contains information about the element which has no next element then _r__i_u2009=u20090. Three lists are shown on the picture. For example, for the picture above the values of _l_ and _r_ are the following: _l_1u2009=u20094, _r_1u2009=u20097; _l_2u2009=u20095, _r_2u2009=u20090; _l_3u2009=u20090, _r_3u2009=u20090; _l_4u2009=u20096, _r_4u2009=u20091; _l_5u2009=u20090, _r_5u2009=u20092; _l_6u2009=u20090, _r_6u2009=u20094; _l_7u2009=u20091, _r_7u2009=u20090. Your task is to unite all given lists in a single list, joining them to each other in any order. In particular, if the input data already contains a single list, then there is no need to perform any actions. Print the resulting list in the form of values _l__i_, _r__i_. Any other action, other than joining the beginning of one list to the end of another, can not be performed. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of memory cells where the doubly linked lists are located. Each of the following _n_ lines contains two integers _l__i_, _r__i_ (0u2009≤u2009_l__i_,u2009_r__i_u2009≤u2009_n_) — the cells of the previous and the next element of list for cell _i_. Value _l__i_u2009=u20090 if element in cell _i_ has no previous element in its list. Value _r__i_u2009=u20090 if element in cell _i_ has no next element in its list. It is guaranteed that the input contains the correct description of a single or more doubly linked lists. All lists have linear structure: each element of list except the first has exactly one previous element; each element of list except the last has exactly one next element. Each memory cell contains information about one element from some list, each element of each list written in one of _n_ given cells. Output Print _n_ lines, the _i_-th line must contain two integers _l__i_ and _r__i_ — the cells of the previous and the next element of list for cell _i_ after all lists from the input are united in a single list. If there are many solutions print any of them.
1,500
false
false
true
false
false
false
false
false
false
false
6,371
226C
Problem - 226C - 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 implementation math matrices number theory *2400 No tag edit access → Contest materials . 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. Output Print a single integer — the residue from dividing the sought greatest common divisor by _m_. Examples Input 10 1 8 2 Output 3 Input 10 1 8 3 Output 1
2,400
true
false
true
false
true
false
false
false
false
false
8,933
1898D
Kirill has two integer arrays $$$a_1,a_2,ldots,a_n$$$ and $$$b_1,b_2,ldots,b_n$$$ of length $$$n$$$. He defines the absolute beauty of the array $$$b$$$ as $$$$$$sum_{i=1}^{n} a_i - b_i.$$$$$$ Here, $$$x$$$ denotes the absolute value of $$$x$$$. Kirill can perform the following operation at most once: select two indices $$$i$$$ and $$$j$$$ ($$$1 leq i < j leq n$$$) and swap the values of $$$b_i$$$ and $$$b_j$$$. Help him find the maximum possible absolute beauty of the array $$$b$$$ after performing at most one swap. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 10,000$$$). The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2leq nleq 2cdot 10^5$$$)xa0— the length of the arrays $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1leq a_ileq 10^9$$$)xa0— the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1leq b_ileq 10^9$$$)xa0— the array $$$b$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, output one integerxa0— the maximum possible absolute beauty of the array $$$b$$$ after no more than one swap. Example Input 6 3 1 3 5 3 3 3 2 1 2 1 2 2 1 2 2 1 4 1 2 3 4 5 6 7 8 10 1 8 2 5 3 5 3 1 1 3 2 9 2 4 8 2 3 5 3 1 3 47326 6958 358653 3587 35863 59474 Note In the first test case, each of the possible swaps does not change the array $$$b$$$. In the second test case, the absolute beauty of the array $$$b$$$ without performing the swap is $$$1-1 + 2-2 = 0$$$. After swapping the first and the second element in the array $$$b$$$, the absolute beauty becomes $$$1-2 + 2-1 = 2$$$. These are all the possible outcomes, hence the answer is $$$2$$$. In the third test case, it is optimal for Kirill to not perform the swap. Similarly to the previous test case, the answer is $$$2$$$. In the fourth test case, no matter what Kirill does, the absolute beauty of $$$b$$$ remains equal to $$$16$$$.
1,900
true
true
false
false
false
false
false
false
false
false
925
1117D
Reziba has many magic gems. Each magic gem can be split into $$$M$$$ normal gems. The amount of space each magic (and normal) gem takes is $$$1$$$ unit. A normal gem cannot be split. Reziba wants to choose a set of magic gems and split some of them, so the total space occupied by the resulting set of gems is $$$N$$$ units. If a magic gem is chosen and split, it takes $$$M$$$ units of space (since it is split into $$$M$$$ gems); if a magic gem is not split, it takes $$$1$$$ unit. How many different configurations of the resulting set of gems can Reziba have, such that the total amount of space taken is $$$N$$$ units? Print the answer modulo $$$1000000007$$$ ($$$10^9+7$$$). Two configurations are considered different if the number of magic gems Reziba takes to form them differs, or the indices of gems Reziba has to split differ. Input The input contains a single line consisting of $$$2$$$ integers $$$N$$$ and $$$M$$$ ($$$1 le N le 10^{18}$$$, $$$2 le M le 100$$$). Output Print one integer, the total number of configurations of the resulting set of gems, given that the total amount of space taken is $$$N$$$ units. Print the answer modulo $$$1000000007$$$ ($$$10^9+7$$$). Note In the first example each magic gem can split into $$$2$$$ normal gems, and we know that the total amount of gems are $$$4$$$. Let $$$1$$$ denote a magic gem, and $$$0$$$ denote a normal gem. The total configurations you can have is: $$$1 1 1 1$$$ (None of the gems split); $$$0 0 1 1$$$ (First magic gem splits into $$$2$$$ normal gems); $$$1 0 0 1$$$ (Second magic gem splits into $$$2$$$ normal gems); $$$1 1 0 0$$$ (Third magic gem splits into $$$2$$$ normal gems); $$$0 0 0 0$$$ (First and second magic gems split into total $$$4$$$ normal gems). Hence, answer is $$$5$$$.
2,100
true
false
false
true
false
false
false
false
false
false
5,106
1921G
Once the mischievous and wayward shooter named Shel found himself on a rectangular field of size $$$n imes m$$$, divided into unit squares. Each cell either contains a target or not. Shel only had a lucky shotgun with him, with which he can shoot in one of the four directions: right-down, left-down, left-up, or right-up. When fired, the shotgun hits all targets in the chosen direction, the Manhattan distance to which does not exceed a fixed constant $$$k$$$. The Manhattan distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is equal to $$$x_1 - x_2 + y_1 - y_2$$$. Possible hit areas for $$$k = 3$$$. Shel's goal is to hit as many targets as possible. Please help him find this value. Input Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. Then follows the description of the test cases. The first line of each test case contains field dimensions $$$n$$$, $$$m$$$, and the constant for the shotgun's power $$$k$$$ ($$$1 le n, m, k le 10^5, 1 le n cdot m le 10^5$$$). Each of the next $$$n$$$ lines contains $$$m$$$ characters — the description of the next field row, where the character '.' means the cell is empty, and the character '#' indicates the presence of a target. It is guaranteed that the sum of $$$n cdot m$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output a single integer on a separate line, which is equal to the maximum possible number of hit targets with one shot. Example Input 4 3 3 1 .#. ### .#. 2 5 3 ###.. ...## 4 4 2 ..## ###. #..# #### 2 1 3 # # Note Possible optimal shots for the examples in the statement:
2,200
false
false
true
true
true
false
true
false
false
false
759
1352E
Pay attention to the non-standard memory limit in this problem. In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution. The array $$$a=[a_1, a_2, ldots, a_n]$$$ ($$$1 le a_i le n$$$) is given. Its element $$$a_i$$$ is called special if there exists a pair of indices $$$l$$$ and $$$r$$$ ($$$1 le l < r le n$$$) such that $$$a_i = a_l + a_{l+1} + ldots + a_r$$$. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not). Print the number of special elements of the given array $$$a$$$. For example, if $$$n=9$$$ and $$$a=[3,1,4,1,5,9,2,6,5]$$$, then the answer is $$$5$$$: $$$a_3=4$$$ is a special element, since $$$a_3=4=a_1+a_2=3+1$$$; $$$a_5=5$$$ is a special element, since $$$a_5=5=a_2+a_3=1+4$$$; $$$a_6=9$$$ is a special element, since $$$a_6=9=a_1+a_2+a_3+a_4=3+1+4+1$$$; $$$a_8=6$$$ is a special element, since $$$a_8=6=a_2+a_3+a_4=1+4+1$$$; $$$a_9=5$$$ is a special element, since $$$a_9=5=a_2+a_3=1+4$$$. Please note that some of the elements of the array $$$a$$$ may be equal — if several elements are equal and special, then all of them should be counted in the answer. Input The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given in two lines. The first line contains an integer $$$n$$$ ($$$1 le n le 8000$$$) — the length of the array $$$a$$$. The second line contains integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$). It is guaranteed that the sum of the values of $$$n$$$ for all test cases in the input does not exceed $$$8000$$$.
1,500
false
false
true
false
false
false
true
false
false
false
3,948
1762D
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 838 (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 constructive algorithms interactive number theory *2100 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST D. GCD Queries time limit per test3 seconds memory limit per test256 megabytes This is an interactive problem. There is a secret permutation $$$p$$$ of $$$[0,1,2,ldots,n-1]$$$. Your task is to find $$$2$$$ indices $$$x$$$ and $$$y$$$ ($$$1 leq x, y leq n$$$, possibly $$$x=y$$$) such that $$$p_x=0$$$ or $$$p_y=0$$$. In order to find it, you are allowed to ask at most $$$2n$$$ queries. In one query, you give two integers $$$i$$$ and $$$j$$$ ($$$1 leq i, j leq n$$$, $$$i eq j$$$) and receive the value of $$$gcd(p_i,p_j)^dagger$$$. Note that the permutation $$$p$$$ is fixed before any queries are made and does not depend on the queries. $$$^dagger$$$ $$$gcd(x, y)$$$ denotes the greatestxa0common divisor (GCD) of integers $$$x$$$ and $$$y$$$. Note that $$$gcd(x,0)=gcd(0,x)=x$$$ for all positive integers $$$x$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 10^4$$$). 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^4$$$). After reading the integer $$$n$$$ for each test case, you should begin the interaction. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^4$$$. Interaction The interaction for each test case begins by reading the integer $$$n$$$. To make a query, output "? $$$i$$$ $$$j$$$" ($$$1 leq i, j leq n$$$, $$$i eq j$$$) without quotes. Afterwards, you should read a single integer — the answer to your query $$$gcd(p_i,p_j)$$$. You can make at most $$$2n$$$ such queries in each test case. If you want to print the answer, output "! $$$x$$$ $$$y$$$" ($$$1 leq x, y leq n$$$) without quotes. After doing that, read $$$1$$$ or $$$-1$$$. If $$$p_x=0$$$ or $$$p_y=0$$$, you'll receive $$$1$$$, otherwise you'll receive $$$-1$$$. If you receive $$$-1$$$, your program must terminate immediately to receive a Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. If you receive the integer $$$-1$$$ instead of an answer or a valid value of $$$n$$$, it means your program has made an invalid query, has exceeded the limit of queries, or has given an incorrect answer on the previous test case. Your program must terminate immediately to receive a 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 or the answer, do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hacks To hack, use the following format. The first line should contain a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$). The first line of each test case should contain a single integer $$$n$$$ ($$$2 leq n leq 2 cdot 10^4$$$). The second line of each test case should contain $$$n$$$ space separated integers $$$p_1,p_2,ldots,p_n$$$. $$$p$$$ should be a permutation of $$$[0,1,2,ldots,n-1]$$$. The sum of $$$n$$$ should not exceed $$$2 cdot 10^4$$$. Example input 2 2 1 1 5 2 4 1 output ? 1 2 ! 1 2 ? 1 2 ? 2 3 ! 3 3 Note In the first test, the interaction proceeds as follows. Solution Jury Explanation $$$ exttt{2}$$$ There are 2 test cases. $$$ exttt{2}$$$ In the first test case, the hidden permutation is $$$[1,0]$$$, with length $$$2$$$. $$$ exttt{? 1 2}$$$ $$$ exttt{1}$$$ The solution requests $$$gcd(p_1,p_2)$$$, and the jury responds with $$$1$$$. $$$ exttt{! 1 2}$$$ $$$ exttt{1}$$$ The solution knows that either $$$p_1=0$$$ or $$$p_2=0$$$, and prints the answer. Since the output is correct, the jury responds with $$$1$$$ and continues to the next test case. $$$ exttt{5}$$$ In the second test case, the hidden permutation is $$$[2,4,0,1,3]$$$, with length $$$5$$$. $$$ exttt{? 1 2}$$$ $$$ exttt{2}$$$ The solution requests $$$gcd(p_1,p_2)$$$, and the jury responds with $$$2$$$. $$$ exttt{? 2 3}$$$ $$$ exttt{4}$$$ The solution requests $$$gcd(p_2,p_3)$$$, and the jury responds with $$$4$$$. $$$ exttt{! 3 3}$$$ $$$ exttt{1}$$$ The solution has somehow determined that $$$p_3=0$$$, and prints the answer. Since the output is correct, the jury responds with $$$1$$$. Note that the empty lines in the example input and output are for the sake of clarity, and do not occur in the real interaction. After each test case, make sure to read $$$1$$$ or $$$-1$$$. Codeforces (c)
2,100
false
false
false
false
false
true
false
false
false
false
1,737