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
496A
Mike is trying rock climbing but he is awful at it. There are _n_ holds on the wall, _i_-th hold is at height _a__i_ off the ground. Besides, let the sequence _a__i_ increase, that is, _a__i_u2009<u2009_a__i_u2009+u20091 for all _i_ from 1 to _n_u2009-u20091; we will call such sequence a track. Mike thinks that the track _a_1, ..., _a__n_ has difficulty . In other words, difficulty equals the maximum distance between two holds that are adjacent in height. Today Mike decided to cover the track with holds hanging on heights _a_1, ..., _a__n_. To make the problem harder, Mike decided to remove one hold, that is, remove one element of the sequence (for example, if we take the sequence (1,u20092,u20093,u20094,u20095) and remove the third element from it, we obtain the sequence (1,u20092,u20094,u20095)). However, as Mike is awful at climbing, he wants the final difficulty (i.e. the maximum difference of heights between adjacent holds after removing the hold) to be as small as possible among all possible options of removing a hold. The first and last holds must stay at their positions. Help Mike determine the minimum difficulty of the track after removing one hold. Input The first line contains a single integer _n_ (3u2009≤u2009_n_u2009≤u2009100)xa0— the number of holds. The next line contains _n_ space-separated integers _a__i_ (1u2009≤u2009_a__i_u2009≤u20091000), where _a__i_ is the height where the hold number _i_ hangs. The sequence _a__i_ is increasing (i.e. each element except for the first one is strictly larger than the previous one). Output Print a single number — the minimum difficulty of the track after removing a single hold. Note In the first sample you can remove only the second hold, then the sequence looks like (1,u20096), the maximum difference of the neighboring elements equals 5. In the second test after removing every hold the difficulty equals 2. In the third test you can obtain sequences (1,u20093,u20097,u20098), (1,u20092,u20097,u20098), (1,u20092,u20093,u20098), for which the difficulty is 4, 5 and 5, respectively. Thus, after removing the second element we obtain the optimal answer — 4.
900
true
false
true
false
false
false
true
false
false
false
7,851
1349B
Slime has a sequence of positive integers $$$a_1, a_2, ldots, a_n$$$. In one operation Orac can choose an arbitrary subsegment $$$[l ldots r]$$$ of this sequence and replace all values $$$a_l, a_{l + 1}, ldots, a_r$$$ to the value of median of $$${a_l, a_{l + 1}, ldots, a_r}$$$. In this problem, for the integer multiset $$$s$$$, the median of $$$s$$$ is equal to the $$$lfloor frac{s+1}{2} floor$$$-th smallest number in it. For example, the median of $$${1,4,4,6,5}$$$ is $$$4$$$, and the median of $$${1,7,5,8}$$$ is $$$5$$$. Slime wants Orac to make $$$a_1 = a_2 = ldots = a_n = k$$$ using these operations. Orac thinks that it is impossible, and he does not want to waste his time, so he decided to ask you if it is possible to satisfy the Slime's requirement, he may ask you these questions several times. Input The first line of the input is a single integer $$$t$$$: the number of queries. The first line of each query contains two integers $$$n (1le nle 100,000)$$$ and $$$k (1le kle 10^9)$$$, the second line contains $$$n$$$ positive integers $$$a_1,a_2,dots,a_n (1le a_ile 10^9)$$$ The total sum of $$$n$$$ is at most $$$100,000$$$. Output The output should contain $$$t$$$ lines. The $$$i$$$-th line should be equal to 'yes' if it is possible to make all integers $$$k$$$ in some number of operations or 'no', otherwise. You can print each letter in lowercase or uppercase. Note In the first query, Orac can't turn all elements into $$$3$$$. In the second query, $$$a_1=6$$$ is already satisfied. In the third query, Orac can select the complete array and turn all elements into $$$2$$$. In the fourth query, Orac can't turn all elements into $$$3$$$. In the fifth query, Orac can select $$$[1,6]$$$ at first and then select $$$[2,10]$$$.
2,000
true
true
false
false
false
true
false
false
false
false
3,963
2000D
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 966 (Div. 3) Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy implementation two pointers *1200 No tag edit access → Contest materials Announcement Tutorial #1 Video Tutorial (en) Discussion stream (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST D. Right Left Wrong time limit per test2 seconds memory limit per test256 megabytes Vlad found a strip of $$$n$$$ cells, numbered from left to right from $$$1$$$ to $$$n$$$. In the $$$i$$$-th cell, there is a positive integer $$$a_i$$$ and a letter $$$s_i$$$, where all $$$s_i$$$ are either 'L' or 'R'. Vlad invites you to try to score the maximum possible points by performing any (possibly zero) number of operations. In one operation, you can choose two indices $$$l$$$ and $$$r$$$ ($$$1 le l < r le n$$$) such that $$$s_l$$$ = 'L' and $$$s_r$$$ = 'R' and do the following: add $$$a_l + a_{l + 1} + dots + a_{r - 1} + a_r$$$ points to the current score; replace $$$s_i$$$ with '.' for all $$$l le i le r$$$, meaning you can no longer choose these indices. For example, consider the following strip: $$$3$$$ $$$5$$$ $$$1$$$ $$$4$$$ $$$3$$$ $$$2$$$ L R L L L R You can first choose $$$l = 1$$$, $$$r = 2$$$ and add $$$3 + 5 = 8$$$ to your score. $$$3$$$ $$$5$$$ $$$1$$$ $$$4$$$ $$$3$$$ $$$2$$$ . . L L L R Then choose $$$l = 3$$$, $$$r = 6$$$ and add $$$1 + 4 + 3 + 2 = 10$$$ to your score. $$$3$$$ $$$5$$$ $$$1$$$ $$$4$$$ $$$3$$$ $$$2$$$ . . . . . . As a result, it is impossible to perform another operation, and the final score is $$$18$$$. What is the maximum score that can be achieved? Input The first line contains one 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$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0— the length of the strip. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^5$$$)xa0— the numbers written on the strip. The third line of each test case contains a string $$$s$$$ of $$$n$$$ characters 'L' and 'R'. It is guaranteed that the sum of the values of $$$n$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output one integerxa0— the maximum possible number of points that can be scored. Example input 4 6 3 5 1 4 3 2 LRLLLR 2 2 8 LR 2 3 9 RL 5 1 2 3 4 5 LRLRR output 18 10 0 22 Codeforces (c)
1,200
false
true
true
false
false
false
false
false
false
false
260
1767E
Algebra Flash 2.2 has just been released! Changelog: New gamemode! Thank you for the continued support of the game! Huh, is that it? Slightly disappointed, you boot up the game and click on the new gamemode. It says "Colored platforms". There are $$$n$$$ platforms, numbered from $$$1$$$ to $$$n$$$, placed one after another. There are $$$m$$$ colors available in the game, numbered from $$$1$$$ to $$$m$$$. The $$$i$$$-th platform is colored $$$c_i$$$. You start on the platform $$$1$$$ and want to reach platform $$$n$$$. In one move, you can jump from some platform $$$i$$$ to platforms $$$i + 1$$$ or $$$i + 2$$$. All platforms are initially deactivated (including platforms $$$1$$$ and $$$n$$$). For each color $$$j$$$, you can pay $$$x_j$$$ coins to activate all platforms of that color. You want to activate some platforms so that you could start on an activated platform $$$1$$$, jump through some activated platforms and reach an activated platform $$$n$$$. What's the smallest amount of coins you can spend to achieve that? Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 3 cdot 10^5$$$; $$$1 le m le 40$$$)xa0— the number of platforms and the number of colors, respectively. The second line contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le m$$$)xa0— the colors of the platforms. The third line contains $$$m$$$ integers $$$x_1, x_2, dots, x_m$$$ ($$$1 le x_i le 10^7$$$)xa0— the cost of activating all platforms of each color. Output Print the smallest amount of coins you can spend to activate some platforms so that you could start on an activated platform $$$1$$$, jump through some activated platforms and reach an activated platform $$$n$$$. Examples Input 5 3 1 3 2 3 1 1 10 100 Input 5 3 1 3 2 3 1 1 200 20 Input 10 10 3 8 6 2 10 5 2 3 7 3 9 7 4 2 1 8 2 6 2 2
2,500
true
false
false
true
false
false
true
false
false
true
1,693
62D
Arnie the Worm has finished eating an apple house yet again and decided to move. He made up his mind on the plan, the way the rooms are located and how they are joined by corridors. He numbered all the rooms from 1 to _n_. All the corridors are bidirectional. Arnie wants the new house to look just like the previous one. That is, it should have exactly _n_ rooms and, if a corridor from room _i_ to room _j_ existed in the old house, it should be built in the new one. We know that during the house constructing process Arnie starts to eat an apple starting from some room and only stops when he eats his way through all the corridors and returns to the starting room. It is also known that Arnie eats without stopping. That is, until Arnie finishes constructing the house, he is busy every moment of his time gnawing a new corridor. Arnie doesn't move along the already built corridors. However, gnawing out corridors in one and the same order any time you change a house is a very difficult activity. That's why Arnie, knowing the order in which the corridors were located in the previous house, wants to gnaw corridors in another order. It is represented as a list of rooms in the order in which they should be visited. The new list should be lexicographically smallest, but it also should be strictly lexicographically greater than the previous one. Help the worm. Input The first line contains two integers _n_ and _m_ (3u2009≤u2009_n_u2009≤u2009100,u20093u2009≤u2009_m_u2009≤u20092000). It is the number of rooms and corridors in Arnie's house correspondingly. The next line contains _m_u2009+u20091 positive integers that do not exceed _n_. They are the description of Arnie's old path represented as a list of rooms he visited during the gnawing. It is guaranteed that the last number in the list coincides with the first one. The first room described in the list is the main entrance, that's why Arnie should begin gnawing from it. You may assume that there is no room which is connected to itself and there is at most one corridor between any pair of rooms. However, it is possible to find some isolated rooms which are disconnected from others. Output Print _m_u2009+u20091 positive integers that do not exceed _n_. Those numbers are the description of the new path, according to which Arnie should gnaw out his new house. If it is impossible to find new path you should print out No solution. The first number in your answer should be equal to the last one. Also it should be equal to the main entrance.
2,300
false
false
false
false
false
false
false
false
false
true
9,662
1776D
As soon as SWERC starts, your experienced $$$3$$$-person team immediately realizes that the contest features $$$a$$$ easy problems, $$$b$$$ medium problems, and $$$c$$$ hard problems. Solving a problem will take any of you $$$2$$$, $$$3$$$, or $$$4$$$ time units, depending on whether the problem is easy, medium, or hard. Regardless of the difficulty of the problem, the last time unit spent to solve it has to be spent using your shared computer. You organize your efforts so that each of you starts (and ends) solving problems at integer time units. Any given problem can be solved by only one contestant; it requires a contiguous amount of time (which depends on the difficulty of the problem). None of the $$$3$$$ of you can solve more than one problem at a time, but you can start solving a new problem immediately after finishing one. Similarly, the shared computer cannot be used by more than one of you at a time, but any of you can start using the computer (to complete the problem currently being solved) immediately after someone else stops using it. Given that the contest lasts $$$l$$$ time units, find the maximum number of problems that your team can solve. Additionally, find one way to solve the maximum number of problems. Input The input has a single line. It contains four integers $$$a$$$, $$$b$$$, $$$c$$$, $$$l$$$ ($$$0 leq a, b, c, leq 10^4$$$, $$$0 le l le 10^5$$$) — the number of easy, medium, and hard problems, and the duration of the contest. Output On the first line, print a single integer $$$n$$$ — the maximum number of problems that your team can solve. Then, on the $$$j$$$-th of the following $$$n$$$ lines, print three integers $$$x_j$$$, $$$p_j$$$, $$$q_j$$$ ($$$1 leq x leq 3$$$, $$$0 leq p_j < q_j leq l$$$) — the contestant that solves the $$$j$$$-th problem, and the start and end time for solving the $$$j$$$-th problem (measured as time units elapsed from the beginning of the contest). The difference $$$q_j - p_j$$$ is $$$2$$$, $$$3$$$, or $$$4$$$, depending on the difficulty of the problem. The last $$$n$$$ lines are to be provided in increasing order of end time: $$$q_1 < q_2 < cdots < q_n$$$. If there are multiple ways to solve $$$n$$$ problems, output any of them. Examples Output 4 1 0 2 2 0 3 3 0 4 1 2 5 Note In the first sample, the first contestant solves an easy problem between time $$$0$$$ and time $$$2$$$ while the second contestant solves a medium problem between time $$$0$$$ and time $$$3$$$. In the second sample, the first contestant solves an easy problem between time $$$0$$$ and time $$$2$$$, and then also solves a medium problem between time $$$2$$$ and time $$$5$$$. In the meantime, the second contestant solves another medium problem between time $$$0$$$ and time $$$3$$$, while the third contestant solves a hard problem between time $$$0$$$ and time $$$4$$$. In the third sample, the contest only has medium and hard problems, and there is not enough time to solve any of them.
2,800
true
true
false
false
false
true
false
false
false
false
1,625
624A
Luke Skywalker got locked up in a rubbish shredder between two presses. R2D2 is already working on his rescue, but Luke needs to stay alive as long as possible. For simplicity we will assume that everything happens on a straight line, the presses are initially at coordinates 0 and _L_, and they move towards each other with speed _v_1 and _v_2, respectively. Luke has width _d_ and is able to choose any position between the presses. Luke dies as soon as the distance between the presses is less than his width. Your task is to determine for how long Luke can stay alive. Input The first line of the input contains four integers _d_, _L_, _v_1, _v_2 (1u2009≤u2009_d_,u2009_L_,u2009_v_1,u2009_v_2u2009≤u200910u2009000,u2009_d_u2009<u2009_L_)xa0— Luke's width, the initial position of the second press and the speed of the first and second presses, respectively. Output Print a single real valuexa0— the maximum period of time Luke can stay alive for. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct, if . Examples Output 1.00000000000000000000 Output 2.66666666666666650000 Note In the first sample Luke should stay exactly in the middle of the segment, that is at coordinates [2;4], as the presses move with the same speed. In the second sample he needs to occupy the position . In this case both presses move to his edges at the same time.
800
true
false
false
false
false
false
false
false
false
false
7,335
1882B
You have $$$n$$$ sets of integers $$$S_{1}, S_{2}, ldots, S_{n}$$$. We call a set $$$S$$$ attainable, if it is possible to choose some (possibly, none) of the sets $$$S_{1}, S_{2}, ldots, S_{n}$$$ so that $$$S$$$ is equal to their union$$$^{dagger}$$$. If you choose none of $$$S_{1}, S_{2}, ldots, S_{n}$$$, their union is an empty set. Find the maximum number of elements in an attainable $$$S$$$ such that $$$S eq S_{1} cup S_{2} cup ldots cup S_{n}$$$. $$$^{dagger}$$$ The union of sets $$$A_1, A_2, ldots, A_k$$$ is defined as the set of elements present in at least one of these sets. It is denoted by $$$A_1 cup A_2 cup ldots cup A_k$$$. For example, $$${2, 4, 6} cup {2, 3} cup {3, 6, 7} = {2, 3, 4, 6, 7}$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 50$$$). The following $$$n$$$ lines describe the sets $$$S_1, S_2, ldots, S_n$$$. The $$$i$$$-th of these lines contains an integer $$$k_{i}$$$ ($$$1 le k_{i} le 50$$$)xa0— the number of elements in $$$S_{i}$$$, followed by $$$k_{i}$$$ integers $$$s_{i, 1}, s_{i, 2}, ldots, s_{i, k_{i}}$$$ ($$$1 le s_{i, 1} < s_{i, 2} < ldots < s_{i, k_{i}} le 50$$$)xa0— the elements of $$$S_{i}$$$. Output For each test case, print a single integerxa0— the maximum number of elements in an attainable $$$S$$$ such that $$$S eq S_{1} cup S_{2} cup ldots cup S_{n}$$$. Example Input 4 3 3 1 2 3 2 4 5 2 3 4 4 4 1 2 3 4 3 2 5 6 3 3 5 6 3 4 5 6 5 1 1 3 3 6 10 1 9 2 1 3 3 5 8 9 1 2 4 28 Note In the first test case, $$$S = S_{1} cup S_{3} = {1, 2, 3, 4}$$$ is the largest attainable set not equal to $$$S_1 cup S_2 cup S_3 = {1, 2, 3, 4, 5}$$$. In the second test case, we can pick $$$S = S_{2} cup S_{3} cup S_{4} = {2, 3, 4, 5, 6}$$$. In the third test case, we can pick $$$S = S_{2} cup S_{5} = S_{2} cup S_{3} cup S_{5} = {3, 5, 6, 8, 9, 10}$$$. In the fourth test case, the only attainable set is $$$S = varnothing$$$.
1,300
false
true
false
false
false
true
true
false
false
false
996
771B
In the army, it isn't easy to form a group of soldiers that will be effective on the battlefield. The communication is crucial and thus no two soldiers should share a name (what would happen if they got an order that Bob is a scouter, if there are two Bobs?). A group of soldiers is effective if and only if their names are different. For example, a group (John, Bob, Limak) would be effective, while groups (Gary, Bob, Gary) and (Alice, Alice) wouldn't. You are a spy in the enemy's camp. You noticed _n_ soldiers standing in a row, numbered 1 through _n_. The general wants to choose a group of _k_ consecutive soldiers. For every _k_ consecutive soldiers, the general wrote down whether they would be an effective group or not. You managed to steal the general's notes, with _n_u2009-u2009_k_u2009+u20091 strings _s_1,u2009_s_2,u2009...,u2009_s__n_u2009-u2009_k_u2009+u20091, each either "YES" or "NO". The string _s_1 describes a group of soldiers 1 through _k_ ("YES" if the group is effective, and "NO" otherwise). The string _s_2 describes a group of soldiers 2 through _k_u2009+u20091. And so on, till the string _s__n_u2009-u2009_k_u2009+u20091 that describes a group of soldiers _n_u2009-u2009_k_u2009+u20091 through _n_. Your task is to find possible names of _n_ soldiers. Names should match the stolen notes. Each name should be a string that consists of between 1 and 10 English letters, inclusive. The first letter should be uppercase, and all other letters should be lowercase. Names don't have to be existing namesxa0— it's allowed to print "Xyzzzdj" or "T" for example. Find and print any solution. It can be proved that there always exists at least one solution. Input The first line of the input contains two integers _n_ and _k_ (2u2009≤u2009_k_u2009≤u2009_n_u2009≤u200950)xa0— the number of soldiers and the size of a group respectively. The second line contains _n_u2009-u2009_k_u2009+u20091 strings _s_1,u2009_s_2,u2009...,u2009_s__n_u2009-u2009_k_u2009+u20091. The string _s__i_ is "YES" if the group of soldiers _i_ through _i_u2009+u2009_k_u2009-u20091 is effective, and "NO" otherwise. Output Find any solution satisfying all given conditions. In one line print _n_ space-separated strings, denoting possible names of soldiers in the order. The first letter of each name should be uppercase, while the other letters should be lowercase. Each name should contain English letters only and has length from 1 to 10. If there are multiple valid solutions, print any of them.
1,500
false
true
false
false
false
true
false
false
false
false
6,697
1201A
A class of students wrote a multiple-choice test. There are $$$n$$$ students in the class. The test had $$$m$$$ questions, each of them had $$$5$$$ possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question $$$i$$$ worth $$$a_i$$$ points. Incorrect answers are graded with zero points. The students remember what answers they gave on the exam, but they don't know what are the correct answers. They are very optimistic, so they want to know what is the maximum possible total score of all students in the class. Input The first line contains integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 1000$$$)xa0— the number of students in the class and the number of questions in the test. Each of the next $$$n$$$ lines contains string $$$s_i$$$ ($$$s_i = m$$$), describing an answer of the $$$i$$$-th student. The $$$j$$$-th character represents the student answer (A, B, C, D or E) on the $$$j$$$-th question. The last line contains $$$m$$$ integers $$$a_1, a_2, ldots, a_m$$$ ($$$1 le a_i le 1000$$$)xa0— the number of points for the correct answer for every question. Output Print a single integerxa0— the maximum possible total score of the class. Examples Input 2 4 ABCD ABCE 1 2 3 4 Input 3 3 ABC BCD CDE 5 4 12 Note In the first example, one of the most optimal test answers is "ABCD", this way the total number of points will be $$$16$$$. In the second example, one of the most optimal test answers is "CCC", this way each question will be answered by exactly one student and the total number of points is $$$5 + 4 + 12 = 21$$$.
900
false
false
true
false
false
false
false
false
false
false
4,695
1927B
Polycarp lost the string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters, but he still has its trace. The trace of the string $$$s$$$ is an array $$$a$$$ of $$$n$$$ integers, where $$$a_i$$$ is the number of such indices $$$j$$$ ($$$j < i$$$) that $$$s_i=s_j$$$. For example, the trace of the string abracadabra is the array [$$$0, 0, 0, 1, 0, 2, 0, 3, 1, 1, 4$$$]. Given a trace of a string, find any string $$$s$$$ from which it could have been obtained. The string $$$s$$$ should consist only of lowercase Latin letters a-z. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the length of the lost string. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i < n$$$)xa0— the trace of the string. It is guaranteed that for the given trace, there exists a suitable string $$$s$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a string $$$s$$$ that corresponds to the given trace. If there are multiple such strings $$$s$$$, then output any of them. The string $$$s$$$ should consist of lowercase Latin letters a-z. It is guaranteed that for each test case, a valid answer exists. Example Input 5 11 0 0 0 1 0 2 0 3 1 1 4 10 0 0 0 0 0 1 0 1 1 0 1 0 8 0 1 2 3 4 5 6 7 8 0 0 0 0 0 0 0 0 Output abracadabra codeforces a aaaaaaaa dijkstra
900
false
true
false
false
false
true
false
false
false
false
729
1369B
# AccurateLee Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes Lee was cleaning his house for the party when he found a messy string under the carpets. Now he’d like to make it clean accurately and in a stylish way. . . The string s he found is a binary string of length n (i. e. string consists only of 0-s and 1-s). In one move he can choose two consecutive characters si and si+1 , and if si is 1 and si+1 is 0, he can erase exactly one of them (he can choose which one to erase but he can’t erase both characters simultaneously). The string shrinks after erasing. Lee can make an arbitrary number of moves (possibly zero) and he’d like to make the string s as clean as possible. He thinks for two different strings x and y, the shorter string is cleaner, and if they are the same length, then the lexicographically smaller string is cleaner. Now you should answer t test cases: for the i-th test case, print the cleanest possible string that Lee can get by doing some number of moves. Small reminder: if we have two strings x and y of the same length then x is lexicographically smaller than y if there is a position i such that x1 = y1, x2 = y2,. . . , xi−1 = yi−1 and xi < y i. # Input The first line contains the integer t (1 ≤ t ≤ 10 4) x16 the number of test cases. Next 2t lines contain test cases x16 one per two lines. The first line of each test case contains the integer n (1 ≤ n ≤ 10 5) x16 the length of the string s.The second line contains the binary string s. The string s is a string of length n which consists only of zeroes and ones. It’s guaranteed that sum of n over test cases doesn’t exceed 10 5. # Output Print t answers x16 one per test case. The answer to the i-th test case is the cleanest string Lee can get after doing some number of moves (possibly zero). # Example standard input standard output 510 0001111111 40101 811001101 10 1110000000 110001111111 001 01 01 Page 1 of 2 Note In the first test case, Lee can’t perform any moves. In the second test case, Lee should erase s2.In the third test case, Lee can make moves, for example, in the following order: 11001101 → 1100101 → 110101 → 10101 → 1101 → 101 → 01. Page 2 of 2
1,200
false
true
true
false
false
false
false
false
false
false
3,826
1033E
Bob has a simple undirected connected graph (without self-loops and multiple edges). He wants to learn whether his graph is bipartite (that is, you can paint all vertices of the graph into two colors so that there is no edge connecting two vertices of the same color) or not. As he is not very good at programming, he asked Alice for help. He does not want to disclose his graph to Alice, but he agreed that Alice can ask him some questions about the graph. The only question that Alice can ask is the following: she sends $$$s$$$xa0— a subset of vertices of the original graph. Bob answers with the number of edges that have both endpoints in $$$s$$$. Since he doesn't want Alice to learn too much about the graph, he allows her to ask no more than $$$20000$$$ questions. Furthermore, he suspects that Alice might introduce false messages to their communication channel, so when Alice finally tells him whether the graph is bipartite or not, she also needs to provide a proofxa0— either the partitions themselves or a cycle of odd length. Your task is to help Alice to construct the queries, find whether the graph is bipartite. Interaction First, read an integer $$$n$$$ ($$$1leq nleq 600$$$)xa0— the number of vertices in Bob's graph. To make a query, print two lines. First of which should be in the format "? k" ($$$1 leq k leq n$$$), where $$$k$$$ is the size of the set to be queried. The second line should contain $$$k$$$ space separated distinct integers $$$s_1, s_2, dots, s_k$$$ ($$$1 leq s_i leq n$$$)xa0— the vertices of the queried set. After each query read a single integer $$$m$$$ ($$$0 leq m leq frac{n(n-1)}{2}$$$)xa0— the number of edges between the vertices of the set $$${s_i}$$$. You are not allowed to ask more than $$$20000$$$ queries. If $$$m = -1$$$, it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive Wrong Answer; it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream. After printing a query do not forget to print 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. When you know the answer, you need to print it. The format of the answer depends on whether the graph is bipartite or not. If the graph is bipartite, print two lines. The first should contain the letter "Y" (short for "YES") followed by a space, and then a single integer $$$s$$$xa0($$$0 leq s leq n$$$)xa0— the number of vertices in one of the partitions. Second line should contain $$$s$$$ integers $$$a_1, a_2, dots, a_s$$$xa0— vertices belonging to the first partition. All $$$a_i$$$ must be distinct, and all edges in the main graph must have exactly one endpoint in the set $$${a_i}$$$. If the graph is not bipartite, print two lines. The first should contain the letter "N" (short for "NO") followed by a space, and then a single integer $$$l$$$xa0($$$3 leq l leq n$$$)xa0— the length of one simple cycle of odd length. Second line should contain $$$l$$$ integers $$$c_1, c_2, dots, c_l$$$xa0— the vertices along the cycle. It must hold that for all $$$1 leq i leq l$$$, there is an edge $$${c_i, c_{(i mod l)+1}}$$$ in the main graph, and all $$$c_i$$$ are distinct. If there are multiple possible answers, you may print any of them. Hacks format For hacks, use the following format: The first line contains two integers $$$n$$$ and $$$m~(1 leq n leq 600, 0 leq m leq frac{n(n-1)}{2})$$$xa0— the number of vertices and edges of the graph, respectively. Each of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i~(1 leq u_i, v_i leq n)$$$ mean that there is an edge between $$$u_i$$$ and $$$v_i$$$. There must not be any multiple edges, no loops, and the graph must be connected. For example, you need to use this test to get the first sample: 4 4 4 1 1 3 3 2 2 4 Note In the first case, Alice learns that there are $$$4$$$ edges in the whole graph. Over the course of the next three queries, she learns that vertex $$$1$$$ has two neighbors: $$$3$$$ and $$$4$$$. She then learns that while vertex $$$2$$$ is adjacent to $$$4$$$, the vertex $$$3$$$ isn't adjacent to $$$4$$$. There is only one option for the remaining edge, and that is $$$(2, 3)$$$. This means that the graph is a cycle on four vertices, with $$$(1, 2)$$$ being one partition and $$$(3, 4)$$$ being the second. Here, it would be also valid to output "3 4" on the second line. In the second case, we also have a graph on four vertices and four edges. In the second query, Alice learns that there are three edges among vertices $$$(1, 2, 4)$$$. The only way this could possibly happen is that those form a triangle. As the triangle is not bipartite, Alice can report it as a proof. Notice that she does not learn where the fourth edge is, but she is able to answer Bob correctly anyway.
2,800
false
false
false
false
false
true
false
true
false
true
5,531
1711A
You are given a positive integer $$$n$$$. The weight of a permutation $$$p_1, p_2, ldots, p_n$$$ is the number of indices $$$1le ile n$$$ such that $$$i$$$ divides $$$p_i$$$. Find a permutation $$$p_1,p_2,dots, p_n$$$ with the minimum possible weight (among all permutations of length $$$n$$$). A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 10^4$$$). The description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$) — the length of permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print a line containing $$$n$$$ integers $$$p_1, p_2,dots, p_n$$$ so that the permutation $$$p$$$ has the minimum possible weight. If there are several possible answers, you can print any of them. Note In the first test case, the only valid permutation is $$$p=[1]$$$. Its weight is $$$1$$$. In the second test case, one possible answer is the permutation $$$p=[2,1,4,3]$$$. One can check that $$$1$$$ divides $$$p_1$$$ and $$$i$$$ does not divide $$$p_i$$$ for $$$i=2,3,4$$$, so the weight of this permutation is $$$1$$$. It is impossible to find a permutation of length $$$4$$$ with a strictly smaller weight.
800
false
false
false
false
false
true
false
false
false
false
2,015
570E
Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting of _n_ rows and _m_ columns. We enumerate the rows of the rectangle from top to bottom with numbers from 1 to _n_, and the columns — from left to right with numbers from 1 to _m_. Let's denote the cell at the intersection of the _r_-th row and the _c_-th column as (_r_,u2009_c_). Initially the pig stands in cell (1,u20091), and in the end she wants to be in cell (_n_,u2009_m_). Since the pig is in a hurry to get home, she can go from cell (_r_,u2009_c_), only to either cell (_r_u2009+u20091,u2009_c_) or (_r_,u2009_c_u2009+u20091). She cannot leave the forest. The forest, where the pig is, is very unusual. Some cells of the forest similar to each other, and some look very different. Peppa enjoys taking pictures and at every step she takes a picture of the cell where she is now. The path through the forest is considered to be beautiful if photographs taken on her way, can be viewed in both forward and in reverse order, showing the same sequence of photos. More formally, the line formed by the cells in order of visiting should be a palindrome (you can read a formal definition of a palindrome in the previous problem). Count the number of beautiful paths from cell (1,u20091) to cell (_n_,u2009_m_). Since this number can be very large, determine the remainder after dividing it by 109u2009+u20097. Input The first line contains two integers _n_,u2009_m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009500) — the height and width of the field. Each of the following _n_ lines contains _m_ lowercase English letters identifying the types of cells of the forest. Identical cells are represented by identical letters, different cells are represented by different letters.
2,300
false
false
false
true
false
false
false
false
false
false
7,567
1000A
Codehorses has just hosted the second Codehorses Cup. This year, the same as the previous one, organizers are giving T-shirts for the winners. The valid sizes of T-shirts are either "M" or from $$$0$$$ to $$$3$$$ "X" followed by "S" or "L". For example, sizes "M", "XXS", "L", "XXXL" are valid and "XM", "Z", "XXXXL" are not. There are $$$n$$$ winners to the cup for both the previous year and the current year. Ksenia has a list with the T-shirt sizes printed for the last year cup and is yet to send the new list to the printing office. Organizers want to distribute the prizes as soon as possible, so now Ksenia is required not to write the whole list from the scratch but just make some changes to the list of the previous year. In one second she can choose arbitrary position in any word and replace its character with some uppercase Latin letter. Ksenia can't remove or add letters in any of the words. What is the minimal number of seconds Ksenia is required to spend to change the last year list to the current one? The lists are unordered. That means, two lists are considered equal if and only if the number of occurrences of any string is the same in both lists. Input The first line contains one integer $$$n$$$ ($$$1 le n le 100$$$) — the number of T-shirts. The $$$i$$$-th of the next $$$n$$$ lines contains $$$a_i$$$ — the size of the $$$i$$$-th T-shirt of the list for the previous year. The $$$i$$$-th of the next $$$n$$$ lines contains $$$b_i$$$ — the size of the $$$i$$$-th T-shirt of the list for the current year. It is guaranteed that all the sizes in the input are valid. It is also guaranteed that Ksenia can produce list $$$b$$$ from the list $$$a$$$. Note In the first example Ksenia can replace "M" with "S" and "S" in one of the occurrences of "XS" with "L". In the second example Ksenia should replace "L" in "XXXL" with "S". In the third example lists are equal.
1,200
false
true
true
false
false
false
false
false
false
false
5,705
1290E
Ildar is the algorithm teacher of William and Harris. Today, Ildar is teaching Cartesian Tree. However, Harris is sick, so Ildar is only teaching William. A cartesian tree is a rooted tree, that can be constructed from a sequence of distinct integers. We build the cartesian tree as follows: 1. If the sequence is empty, return an empty tree; 2. Let the position of the maximum element be $$$x$$$; 3. Remove element on the position $$$x$$$ from the sequence and break it into the left part and the right part (which might be empty) (not actually removing it, just taking it away temporarily); 4. Build cartesian tree for each part; 5. Create a new vertex for the element, that was on the position $$$x$$$ which will serve as the root of the new tree. Then, for the root of the left part and right part, if exists, will become the children for this vertex; 6. Return the tree we have gotten. For example, this is the cartesian tree for the sequence $$$4, 2, 7, 3, 5, 6, 1$$$: After teaching what the cartesian tree is, Ildar has assigned homework. He starts with an empty sequence $$$a$$$. In the $$$i$$$-th round, he inserts an element with value $$$i$$$ somewhere in $$$a$$$. Then, he asks a question: what is the sum of the sizes of the subtrees for every node in the cartesian tree for the current sequence $$$a$$$? Node $$$v$$$ is in the node $$$u$$$ subtree if and only if $$$v = u$$$ or $$$v$$$ is in the subtree of one of the vertex $$$u$$$ children. The size of the subtree of node $$$u$$$ is the number of nodes $$$v$$$ such that $$$v$$$ is in the subtree of $$$u$$$. Ildar will do $$$n$$$ rounds in total. The homework is the sequence of answers to the $$$n$$$ questions. The next day, Ildar told Harris that he has to complete the homework as well. Harris obtained the final state of the sequence $$$a$$$ from William. However, he has no idea how to find the answers to the $$$n$$$ questions. Help Harris! Input The first line contains a single integer $$$n$$$ ($$$1 le n le 150000$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$). It is guarenteed that each integer from $$$1$$$ to $$$n$$$ appears in the sequence exactly once. Output Print $$$n$$$ lines, $$$i$$$-th line should contain a single integer xa0— the answer to the $$$i$$$-th question. Note After the first round, the sequence is $$$1$$$. The tree is The answer is $$$1$$$. After the second round, the sequence is $$$2, 1$$$. The tree is The answer is $$$2+1=3$$$. After the third round, the sequence is $$$2, 1, 3$$$. The tree is The answer is $$$2+1+3=6$$$. After the fourth round, the sequence is $$$2, 4, 1, 3$$$. The tree is The answer is $$$1+4+1+2=8$$$. After the fifth round, the sequence is $$$2, 4, 1, 5, 3$$$. The tree is The answer is $$$1+3+1+5+1=11$$$.
3,300
false
false
false
false
true
false
false
false
false
false
4,242
899E
Vasya has an array of integers of length _n_. Vasya performs the following operations on the array: on each step he finds the longest segment of consecutive equal integers (the leftmost, if there are several such segments) and removes it. For example, if Vasya's array is [13,u200913,u20097,u20097,u20097,u20092,u20092,u20092], then after one operation it becomes [13,u200913,u20092,u20092,u20092]. Compute the number of operations Vasya should make until the array becomes empty, i.e. Vasya removes all elements from it. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000) — the length of the array. The second line contains a sequence _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) — Vasya's array. Output Print the number of operations Vasya should make to remove all elements from the array. Examples Input 8 4 4 4 2 2 100 100 100 Note In the first example, at first Vasya removes two fives at the second and third positions. The array becomes [2,u20092]. In the second operation Vasya removes two twos at the first and second positions. After that the array becomes empty. In the second example Vasya has to perform five operations to make the array empty. In each of them he removes the first element from the array. In the third example Vasya needs three operations. In the first operation he removes all integers 4, in the second — all integers 100, in the third — all integers 2. In the fourth example in the first operation Vasya removes the first two integers 10. After that the array becomes [50,u200910,u200950,u200950]. Then in the second operation Vasya removes the two rightmost integers 50, so that the array becomes [50,u200910]. In the third operation he removes the remaining 50, and the array becomes [10] after that. In the last, fourth operation he removes the only remaining 10. The array is empty after that.
2,000
false
false
true
false
true
false
false
false
false
false
6,148
1554E
You are given a tree with $$$n$$$ nodes. As a reminder, a tree is a connected undirected graph without cycles. Let $$$a_1, a_2, ldots, a_n$$$ be a sequence of integers. Perform the following operation exactly $$$n$$$ times: Select an unerased node $$$u$$$. Assign $$$a_u :=$$$ number of unerased nodes adjacent to $$$u$$$. Then, erase the node $$$u$$$ along with all edges that have it as an endpoint. For each integer $$$k$$$ from $$$1$$$ to $$$n$$$, find the number, modulo $$$998,244,353$$$, of different sequences $$$a_1, a_2, ldots, a_n$$$ that satisfy the following conditions: it is possible to obtain $$$a$$$ by performing the aforementioned operations exactly $$$n$$$ times in some order. $$$operatorname{gcd}(a_1, a_2, ldots, a_n) = k$$$. Here, $$$operatorname{gcd}$$$ means the greatest common divisor of the elements in $$$a$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10,000$$$) xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$). Each of the next $$$n - 1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$) indicating there is an edge between vertices $$$u$$$ and $$$v$$$. It is guaranteed that the given edges form a tree. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3 cdot 10^5$$$. Output For each test case, print $$$n$$$ integers in a single line, where for each $$$k$$$ from $$$1$$$ to $$$n$$$, the $$$k$$$-th integer denotes the answer when $$$operatorname{gcd}$$$ equals to $$$k$$$. Note In the first test case, If we delete the nodes in order $$$1 ightarrow 2 ightarrow 3$$$ or $$$1 ightarrow 3 ightarrow 2$$$, then the obtained sequence will be $$$a = [2, 0, 0]$$$ which has $$$operatorname{gcd}$$$ equals to $$$2$$$. If we delete the nodes in order $$$2 ightarrow 1 ightarrow 3$$$, then the obtained sequence will be $$$a = [1, 1, 0]$$$ which has $$$operatorname{gcd}$$$ equals to $$$1$$$. If we delete the nodes in order $$$3 ightarrow 1 ightarrow 2$$$, then the obtained sequence will be $$$a = [1, 0, 1]$$$ which has $$$operatorname{gcd}$$$ equals to $$$1$$$. If we delete the nodes in order $$$2 ightarrow 3 ightarrow 1$$$ or $$$3 ightarrow 2 ightarrow 1$$$, then the obtained sequence will be $$$a = [0, 1, 1]$$$ which has $$$operatorname{gcd}$$$ equals to $$$1$$$. Note that here we are counting the number of different sequences, not the number of different orders of deleting nodes.
2,600
true
false
false
true
false
false
false
false
false
false
2,862
1578B
There are $$$n$$$ villages lying equidistant on a circle in the middle of a thick, impassable forest. From ancient times, it was impossible to move from one village to another, but technical progress has changed a lot. Now, there is a technology to build passable trails in the forest. The building process consists of $$$m$$$ events. Each event is either building a trail or querying if two villages are connected. Trails are built as straight lines connecting two villages. After a trail is built, anybody can walk along the trail from one village to another. Moreover, if two trails cross, anybody can turn at the intersection, and if other trails leave a village you have just reached, they can also be used to walk along. So, for example, if villages are numbered $$$1$$$ to $$$6$$$ in the order around the circle, and there are trails $$$1$$$ to $$$3$$$, $$$2$$$ to $$$4$$$, and $$$4$$$ to $$$6$$$, then all villages, except the $$$5$$$-th, are reachable from the $$$1$$$-st village. Given a list of $$$m$$$ events, for each query, find if two given villages are reachable from each other at that moment. Input The first line contains two integers $$$n$$$ ($$$2 le n le 2cdot 10^5$$$) and $$$m$$$ ($$$1 le m le 3cdot 10^5$$$)xa0— the number of villages and the number of events respectively. Next $$$m$$$ lines contain events. Each event description consists of three integers $$$e$$$ ($$$e$$$ is $$$1$$$ or $$$2$$$), $$$v$$$ ($$$1 le v le n$$$), and $$$u$$$ ($$$1 le u le n$$$, $$$u e v$$$). If $$$e = 1$$$, then the event is building a trail between villages $$$v$$$ and $$$u$$$. If $$$e = 2$$$, then the event is a query if the villages $$$v$$$ and $$$u$$$ are connected. It is guaranteed that each trail is built at most once. Villages are numbered $$$1$$$ to $$$n$$$ in clockwise order around the circle.
2,800
false
false
false
false
true
false
false
false
false
false
2,753
835C
The Cartesian coordinate system is set in the sky. There you can see _n_ stars, the _i_-th has coordinates (_x__i_, _y__i_), a maximum brightness _c_, equal for all stars, and an initial brightness _s__i_ (0u2009≤u2009_s__i_u2009≤u2009_c_). Over time the stars twinkle. At moment 0 the _i_-th star has brightness _s__i_. Let at moment _t_ some star has brightness _x_. Then at moment (_t_u2009+u20091) this star will have brightness _x_u2009+u20091, if _x_u2009+u20091u2009≤u2009_c_, and 0, otherwise. You want to look at the sky _q_ times. In the _i_-th time you will look at the moment _t__i_ and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (_x_1_i_, _y_1_i_) and the upper rightxa0— (_x_2_i_, _y_2_i_). For each view, you want to know the total brightness of the stars lying in the viewed rectangle. A star lies in a rectangle if it lies on its border or lies strictly inside it. Input The first line contains three integers _n_, _q_, _c_ (1u2009≤u2009_n_,u2009_q_u2009≤u2009105, 1u2009≤u2009_c_u2009≤u200910)xa0— the number of the stars, the number of the views and the maximum brightness of the stars. The next _n_ lines contain the stars description. The _i_-th from these lines contains three integers _x__i_, _y__i_, _s__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009100, 0u2009≤u2009_s__i_u2009≤u2009_c_u2009≤u200910)xa0— the coordinates of _i_-th star and its initial brightness. The next _q_ lines contain the views description. The _i_-th from these lines contains five integers _t__i_, _x_1_i_, _y_1_i_, _x_2_i_, _y_2_i_ (0u2009≤u2009_t__i_u2009≤u2009109, 1u2009≤u2009_x_1_i_u2009<u2009_x_2_i_u2009≤u2009100, 1u2009≤u2009_y_1_i_u2009<u2009_y_2_i_u2009≤u2009100)xa0— the moment of the _i_-th view and the coordinates of the viewed rectangle. Output For each view print the total brightness of the viewed stars. Examples Input 2 3 3 1 1 1 3 2 0 2 1 1 2 2 0 2 1 4 5 5 1 1 5 5 Input 3 4 5 1 1 2 2 3 0 3 3 1 0 1 1 100 100 1 2 2 4 4 2 2 1 4 7 1 50 50 51 51 Note Let's consider the first example. At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3. At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0. At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3.
1,600
false
false
true
true
false
false
false
false
false
false
6,425
1734C
You are given a set $$$S$$$, which contains the first $$$n$$$ positive integers: $$$1, 2, ldots, n$$$. You can perform the following operation on $$$S$$$ any number of times (possibly zero): Choose a positive integer $$$k$$$ where $$$1 le k le n$$$, such that there exists a multiple of $$$k$$$ in $$$S$$$. Then, delete the smallest multiple of $$$k$$$ from $$$S$$$. This operation requires a cost of $$$k$$$. You are given a set $$$T$$$, which is a subset of $$$S$$$. Find the minimum possible total cost of operations such that $$$S$$$ would be transformed into $$$T$$$. We can show that such a transformation is always possible. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10,000$$$)xa0— the number of test cases. The description of the test cases follows. The first line contains a single positive integer $$$n$$$ ($$$1 le n le 10^6$$$). The second line of each test case contains a binary string of length $$$n$$$, describing the set $$$T$$$. The $$$i$$$-th character of the string is '1' if and only if $$$i$$$ is an element of $$$T$$$, and '0' otherwise. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Note In the first test case, we shall not perform any operations as $$$S$$$ is already equal to $$$T$$$, which is the set $$${1, 2, 3, 4, 5, 6}$$$. In the second test case, initially, $$$S = {1, 2, 3, 4, 5, 6, 7}$$$, and $$$T = {1, 2, 4, 7}$$$. We shall perform the following operations: 1. Choose $$$k=3$$$, then delete $$$3$$$ from $$$S$$$. 2. Choose $$$k=3$$$, then delete $$$6$$$ from $$$S$$$. 3. Choose $$$k=5$$$, then delete $$$5$$$ from $$$S$$$. The total cost is $$$3+3+5 = 11$$$. It can be shown that this is the smallest cost possible. In the third test case, initially, $$$S = {1, 2, 3, 4}$$$ and $$$T = {}$$$ (empty set). We shall perform $$$4$$$ operations of $$$k=1$$$ to delete $$$1$$$, $$$2$$$, $$$3$$$, and $$$4$$$. In the fourth test case, initially, $$$S = {1, 2, 3, 4}$$$ and $$$T = {3}$$$. We shall perform two operations with $$$k=1$$$ to delete $$$1$$$ and $$$2$$$, then perform one operation with $$$k=2$$$ to delete $$$4$$$.
1,200
true
true
false
false
false
false
false
false
false
false
1,884
1492A
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool. It takes the first swimmer exactly $$$a$$$ minutes to swim across the entire pool and come back, exactly $$$b$$$ minutes for the second swimmer and $$$c$$$ minutes for the third. Hence, the first swimmer will be on the left side of the pool after $$$0$$$, $$$a$$$, $$$2a$$$, $$$3a$$$, ... minutes after the start time, the second one will be at $$$0$$$, $$$b$$$, $$$2b$$$, $$$3b$$$, ... minutes, and the third one will be on the left side of the pool after $$$0$$$, $$$c$$$, $$$2c$$$, $$$3c$$$, ... minutes. You came to the left side of the pool exactly $$$p$$$ minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool. Input The first line of the input contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. Next $$$t$$$ lines contains test case descriptions, one per line. Each line contains four integers $$$p$$$, $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 leq p, a, b, c leq 10^{18}$$$), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back. Note In the first test case, the first swimmer is on the left side in $$$0, 5, 10, 15, ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 4, 8, 12, ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 8, 16, 24, ldots$$$ minutes after the start time. You arrived at the pool in $$$9$$$ minutes after the start time and in a minute you will meet the first swimmer on the left side. In the second test case, the first swimmer is on the left side in $$$0, 6, 12, 18, ldots$$$ minutes after the start time, the second swimmer is on the left side in $$$0, 10, 20, 30, ldots$$$ minutes after the start time, and the third swimmer is on the left side in $$$0, 9, 18, 27, ldots$$$ minutes after the start time. You arrived at the pool $$$2$$$ minutes after the start time and after $$$4$$$ minutes meet the first swimmer on the left side. In the third test case, you came to the pool $$$10$$$ minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck! In the fourth test case, all swimmers are located on the left side in $$$0, 9, 18, 27, ldots$$$ minutes after the start time. You arrived at the pool $$$10$$$ minutes after the start time and after $$$8$$$ minutes meet all three swimmers on the left side.
800
true
false
false
false
false
false
false
false
false
false
3,213
1632D
New Year is just around the corner, which means that in School 179, preparations for the concert are in full swing. There are $$$n$$$ classes in the school, numbered from $$$1$$$ to $$$n$$$, the $$$i$$$-th class has prepared a scene of length $$$a_i$$$ minutes. As the main one responsible for holding the concert, Idnar knows that if a concert has $$$k$$$ scenes of lengths $$$b_1$$$, $$$b_2$$$, $$$ldots$$$, $$$b_k$$$ minutes, then the audience will get bored if there exist two integers $$$l$$$ and $$$r$$$ such that $$$1 le l le r le k$$$ and $$$gcd(b_l, b_{l + 1}, ldots, b_{r - 1}, b_r) = r - l + 1$$$, where $$$gcd(b_l, b_{l + 1}, ldots, b_{r - 1}, b_r)$$$ is equal to the for the $$$t$$$-th class ($$$1 le t le k$$$) to make a new scene $$$d$$$ minutes in length, where $$$d$$$ can be any positive integer. Thus, after this operation, $$$b_t$$$ is equal to $$$d$$$. Note that $$$t$$$ and $$$d$$$ can be different for each operation. For a sequence of scene lengths $$$b_1$$$, $$$b_2$$$, $$$ldots$$$, $$$b_{k}$$$, let $$$f(b)$$$ be the minimum number of classes Idnar has to ask to change their scene if he wants to avoid boring the audience. Idnar hasn't decided which scenes will be allowed for the concert, so he wants to know the value of $$$f$$$ for each non-empty prefix of $$$a$$$. In other words, Idnar wants to know the values of $$$f(a_1)$$$, $$$f(a_1$$$,$$$a_2)$$$, $$$ldots$$$, $$$f(a_1$$$,$$$a_2$$$,$$$ldots$$$,$$$a_n)$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of classes in the school. The second line contains $$$n$$$ positive integers $$$a_1$$$, $$$a_2$$$, $$$ldots$$$, $$$a_n$$$ ($$$1 le a_i le 10^9$$$) — the lengths of the class scenes. Output Print a sequence of $$$n$$$ integers in a single line — $$$f(a_1)$$$, $$$f(a_1$$$,$$$a_2)$$$, $$$ldots$$$, $$$f(a_1$$$,$$$a_2$$$,$$$ldots$$$,$$$a_n)$$$.
2,000
true
true
false
false
true
false
false
true
false
false
2,455
542E
Vova and Marina love offering puzzles to each other. Today Marina offered Vova to cope with the following task. Vova has a non-directed graph consisting of _n_ vertices and _m_ edges without loops and multiple edges. Let's define the operation of contraction two vertices _a_ and _b_ that are not connected by an edge. As a result of this operation vertices _a_ and _b_ are deleted and instead of them a new vertex _x_ is added into the graph, and also edges are drawn from it to all vertices that were connected with _a_ or with _b_ (specifically, if the vertex was connected with both _a_ and _b_, then also exactly one edge is added from _x_ to it). Thus, as a result of contraction again a non-directed graph is formed, it contains no loops nor multiple edges, and it contains (_n_u2009-u20091) vertices. Vova must perform the contraction an arbitrary number of times to transform the given graph into a chain of the maximum length. A chain of length _k_ (_k_u2009≥u20090) is a connected graph whose vertices can be numbered with integers from 1 to _k_u2009+u20091 so that the edges of the graph connect all pairs of vertices (_i_,u2009_i_u2009+u20091) (1u2009≤u2009_i_u2009≤u2009_k_) and only them. Specifically, the graph that consists of one vertex is a chain of length 0. The vertices that are formed as a result of the contraction are allowed to be used in the following operations of contraction. The picture illustrates the contraction of two vertices marked by red. Help Vova cope with his girlfriend's task. Find the maximum length of the chain that can be obtained from the resulting graph or else determine that it is impossible to obtain the chain. Input The first line contains two integers _n_,u2009_m_ (1u2009≤u2009_n_u2009≤u20091000, 0u2009≤u2009_m_u2009≤u2009100u2009000) — the number of vertices and the number of edges in the original graph. Next _m_ lines contain the descriptions of edges in the format _a__i_,u2009_b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_), which means that there is an edge between vertices _a__i_ and _b__i_. It is guaranteed that there is at most one edge between each pair of vertexes. Output If it is impossible to obtain a chain from the given graph, print u2009-u20091. Otherwise, print the maximum possible number of edges in the resulting chain. Examples Input 4 6 1 2 2 3 1 3 3 4 2 4 1 4 Note In the first sample test you can contract vertices 4 and 5 and obtain a chain of length 3. In the second sample test it is initially impossible to contract any pair of vertexes, so it is impossible to achieve the desired result. In the third sample test you can contract vertices 1 and 2 and obtain a chain of length 2.
2,600
false
false
false
false
false
false
false
false
false
true
7,671
1353D
You are given an array $$$a$$$ of length $$$n$$$ consisting of zeros. You perform $$$n$$$ actions with this array: during the $$$i$$$-th action, the following sequence of operations appears: 1. Choose the maximum by length subarray (continuous subsegment) consisting only of zeros, among all such segments choose the leftmost one; 2. Let this segment be $$$[l; r]$$$. If $$$r-l+1$$$ is odd (not divisible by $$$2$$$) then assign (set) $$$a[frac{l+r}{2}] := i$$$ (where $$$i$$$ is the number of the current action), otherwise (if $$$r-l+1$$$ is even) assign (set) $$$a[frac{l+r-1}{2}] := i$$$. Consider the array $$$a$$$ of length $$$5$$$ (initially $$$a=[0, 0, 0, 0, 0]$$$). Then it changes as follows: 1. Firstly, we choose the segment $$$[1; 5]$$$ and assign $$$a[3] := 1$$$, so $$$a$$$ becomes $$$[0, 0, 1, 0, 0]$$$; 2. then we choose the segment $$$[1; 2]$$$ and assign $$$a[1] := 2$$$, so $$$a$$$ becomes $$$[2, 0, 1, 0, 0]$$$; 3. then we choose the segment $$$[4; 5]$$$ and assign $$$a[4] := 3$$$, so $$$a$$$ becomes $$$[2, 0, 1, 3, 0]$$$; 4. then we choose the segment $$$[2; 2]$$$ and assign $$$a[2] := 4$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 0]$$$; 5. and at last we choose the segment $$$[5; 5]$$$ and assign $$$a[5] := 5$$$, so $$$a$$$ becomes $$$[2, 4, 1, 3, 5]$$$. Your task is to find the array $$$a$$$ of length $$$n$$$ after performing all $$$n$$$ actions. Note that the answer exists and unique. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the length of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Output For each test case, print the answer — the array $$$a$$$ of length $$$n$$$ after performing $$$n$$$ actions described in the problem statement. Note that the answer exists and unique. Example Output 1 1 2 2 1 3 3 1 2 4 2 4 1 3 5 3 4 1 5 2 6
1,600
false
false
false
false
true
true
false
false
true
false
3,942
1923E
You are given a tree, consisting of $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$. Every vertex is colored in some color, denoted by an integer from $$$1$$$ to $$$n$$$. A simple path of the tree is called beautiful if: it consists of at least $$$2$$$ vertices; the first and the last vertices of the path have the same color; no other vertex on the path has the same color as the first vertex. Count the number of the beautiful simple paths of the tree. Note that paths are considered undirected (i. e. the path from $$$x$$$ to $$$y$$$ is the same as the path from $$$y$$$ to $$$x$$$). Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0— the number of vertices in the tree. The second line contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le n$$$)xa0— the color of each vertex. The $$$i$$$-th of the next $$$n - 1$$$ lines contains two integers $$$v_i$$$ and $$$u_i$$$ ($$$1 le v_i, u_i le n$$$; $$$v_i eq u_i$$$)xa0— the $$$i$$$-th edge of the tree. The given edges form a valid tree. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print a single integerxa0— the number of the beautiful simple paths of the tree. Example Input 4 3 1 2 1 1 2 2 3 5 2 1 2 1 2 1 2 1 3 3 4 4 5 5 1 2 3 4 5 1 2 1 3 3 4 4 5 4 2 2 2 2 3 1 3 2 3 4
2,000
false
false
false
true
true
false
false
false
false
true
748
1935E
The New Year has arrived in the Master's Assistance Center, which means it's time to introduce a new feature! Now students are given distance learning courses, with a total of $$$n$$$ courses available. For the $$$i$$$-th distance learning course, a student can receive a grade ranging from $$$x_i$$$ to $$$y_i$$$. However, not all courses may be available to each student. Specifically, the $$$j$$$-th student is only given courses with numbers from $$$l_j$$$ to $$$r_j$$$, meaning the distance learning courses with numbers $$$l_j, l_j + 1, ldots, r_j$$$. The creators of the distance learning courses have decided to determine the final grade in a special way. Let the $$$j$$$-th student receive grades $$$c_{l_j}, c_{l_j + 1}, ldots, c_{r_j}$$$ for their distance learning courses. Then their final grade will be equal to $$$c_{l_j}$$$ $$$$$$ $$$c_{l_j + 1}$$$ $$$$$$ $$$ldots$$$ $$$$$$ $$$c_{r_j}$$$, where $$$$$$ denotes the — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of distance learning courses. Each of the following $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 le x_i le y_i < 2^{30}$$$) — the minimum and maximum grade that can be received for the $$$i$$$-th course. The next line contains a single integer $$$q$$$ ($$$1 le q le 2cdot10^5$$$) — the number of students. Each of the following $$$q$$$ lines contains two integers $$$l_j$$$ and $$$r_j$$$ ($$$1 le l_j le r_j le n$$$) — the minimum and maximum course numbers accessible to the $$$j$$$-th student. It is guaranteed that the sum of $$$n$$$ over all test cases and the sum of $$$q$$$ over all test cases do not exceed $$$2cdot10^5$$$. Output For each test case, output $$$q$$$ integers, where the $$$j$$$-th integer is the maximum final grade that the $$$j$$$-th student can achieve.
2,400
true
true
false
false
true
false
true
false
false
false
670
755D
PolandBall has such a convex polygon with _n_ veritces that no three of its diagonals intersect at the same point. PolandBall decided to improve it and draw some red segments. He chose a number _k_ such that _gcd_(_n_,u2009_k_)u2009=u20091. Vertices of the polygon are numbered from 1 to _n_ in a clockwise way. PolandBall repeats the following process _n_ times, starting from the vertex 1: Assume you've ended last operation in vertex _x_ (consider _x_u2009=u20091 if it is the first operation). Draw a new segment from vertex _x_ to _k_-th next vertex in clockwise direction. This is a vertex _x_u2009+u2009_k_ or _x_u2009+u2009_k_u2009-u2009_n_ depending on which of these is a valid index of polygon's vertex. Your task is to calculate number of polygon's sections after each drawing. A section is a clear area inside the polygon bounded with drawn diagonals or the polygon's sides. Input There are only two numbers in the input: _n_ and _k_ (5u2009≤u2009_n_u2009≤u2009106, 2u2009≤u2009_k_u2009≤u2009_n_u2009-u20092, _gcd_(_n_,u2009_k_)u2009=u20091). Output You should print _n_ values separated by spaces. The _i_-th value should represent number of polygon's sections after drawing first _i_ lines. Examples Output 2 3 4 6 9 12 16 21 26 31 Note The greatest common divisor (gcd) of two integers _a_ and _b_ is the largest positive integer that divides both _a_ and _b_ without a remainder. For the first sample testcase, you should output "2 3 5 8 11". Pictures below correspond to situations after drawing lines.
2,000
false
false
false
false
true
false
false
false
false
false
6,774
1454F
You are given an array $$$a$$$ consisting of $$$n$$$ integers. Let $$$min(l, r)$$$ be the minimum value among $$$a_l, a_{l + 1}, ldots, a_r$$$ and $$$max(l, r)$$$ be the maximum value among $$$a_l, a_{l + 1}, ldots, a_r$$$. Your task is to choose three positive (greater than $$$0$$$) integers $$$x$$$, $$$y$$$ and $$$z$$$ such that: $$$x + y + z = n$$$; $$$max(1, x) = min(x + 1, x + y) = max(x + y + 1, n)$$$. In other words, you have to split the array $$$a$$$ into three consecutive non-empty parts that cover the whole array and the maximum in the first part equals the minimum in the second part and equals the maximum in the third part (or determine it is impossible to find such a partition). Among all such triples (partitions), you can choose any. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$3 le n le 2 cdot 10^5$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Output For each test case, print the answer: NO in the only line if there is no such partition of $$$a$$$ that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers $$$x$$$, $$$y$$$ and $$$z$$$ ($$$x + y + z = n$$$) in the second line. If there are several answers, you can print any. Example Input 6 11 1 2 3 3 3 4 4 3 4 2 1 8 2 9 1 7 3 9 4 1 9 2 1 4 2 4 3 3 1 2 7 4 2 1 1 4 1 4 5 1 1 1 1 1 7 4 3 4 3 3 3 4 Output YES 6 1 4 NO YES 2 5 2 YES 4 1 2 YES 1 1 3 YES 2 1 4
2,100
false
true
false
false
true
false
false
true
false
false
3,403
1642B
Sam is a kindergartener, and there are $$$n$$$ children in his group. He decided to create a team with some of his children to play "brawl:go 2". Sam has $$$n$$$ power-ups, the $$$i$$$-th has type $$$a_i$$$. A child's strength is equal to the number of different types among power-ups he has. For a team of size $$$k$$$, Sam will distribute all $$$n$$$ power-ups to $$$k$$$ children in such a way that each of the $$$k$$$ children receives at least one power-up, and each power-up is given to someone. For each integer $$$k$$$ from $$$1$$$ to $$$n$$$, find the minimum sum of strengths of a team of $$$k$$$ children Sam can get. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 3 cdot 10^5$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$) — types of Sam's power-ups. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For every test case print $$$n$$$ integers. The $$$k$$$-th integer should be equal to the minimum sum of strengths of children in the team of size $$$k$$$ that Sam can get. Note One of the ways to give power-ups to minimise the sum of strengths in the first test case: $$$k = 1: {1, 1, 2}$$$ $$$k = 2: {1, 1}, {2}$$$ $$$k = 3: {1}, {1}, {2}$$$ One of the ways to give power-ups to minimise the sum of strengths in the second test case: $$$k = 1: {1, 2, 2, 2, 4, 5}$$$ $$$k = 2: {2, 2, 2, 4, 5}, {1}$$$ $$$k = 3: {2, 2, 2, 5}, {1}, {4}$$$ $$$k = 4: {2, 2, 2}, {1}, {4}, {5}$$$ $$$k = 5: {2, 2}, {1}, {2}, {4}, {5}$$$ $$$k = 6: {1}, {2}, {2}, {2}, {4}, {5}$$$
900
false
true
false
false
false
false
false
false
false
false
2,402
44E
Problem - 44E - 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 *1400 No tag edit access → Contest materials ") : tutorial of A-E, H, J.") : tutorial of F, G and I.") . The second line contains a sequence of lowercase Latin letters — the text typed by Anfisa. It is guaranteed that the given line is not empty and its length does not exceed 200 symbols. Output Print _k_ lines, each of which contains no less than _a_ and no more than _b_ symbols — Anfisa's text divided into lines. It is not allowed to perform any changes in the text, such as: deleting or adding symbols, changing their order, etc. If the solution is not unique, print any of them. If there is no solution, print "No solution" (without quotes). Examples Input 3 2 5 abrakadabra Output ab rakad abra Input 4 1 2 abrakadabra Output No solution
1,400
false
false
false
true
false
false
false
false
false
false
9,765
346A
Problem - 346A - 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 games math number theory *1600 No tag edit access → Contest materials can choose two distinct integers _x_ and _y_ from the set, such that the set doesn't contain their absolute difference _x_u2009-u2009_y_. Then this player adds integer _x_u2009-u2009_y_ to the set (so, the size of the set increases by one). If the current player has no valid move, he (or she) loses the game. The question is who will finally win the game if both players play optimally. Remember that Alice always moves first. Input The first line contains an integer _n_ (2u2009≤u2009_n_u2009≤u2009100) — the initial number of elements in the set. The second line contains _n_ distinct space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) — the elements of the set. Output Print a single line with the winner's name. If Alice wins print "Alice", otherwise print "Bob" (without quotes). Examples Input 2 2 3 Output Alice Input 2 5 3 Output Alice Input 3 5 6 7 Output Bob Note Consider the first test sample. Alice moves first, and the only move she can do is to choose 2 and 3, then to add 1 to the set. Next Bob moves, there is no valid move anymore, so the winner is Alice.
1,600
true
false
false
false
false
false
false
false
false
false
8,449
1713D
This is an interactive problem. There was a tournament consisting of $$$2^n$$$ contestants. The $$$1$$$-st contestant competed with the $$$2$$$-nd, the $$$3$$$-rd competed with the $$$4$$$-th, and so on. After that, the winner of the first match competed with the winner of second match, etc. The tournament ended when there was only one contestant left, who was declared the winner of the tournament. Such a tournament scheme is known as the single-elimination tournament. You don't know the results, but you want to find the winner of the tournament. In one query, you select two integers $$$a$$$ and $$$b$$$, which are the indices of two contestants. The jury will return $$$1$$$ if $$$a$$$ won more matches than $$$b$$$, $$$2$$$ if $$$b$$$ won more matches than $$$a$$$, or $$$0$$$ if their number of wins was equal. Find the winner in no more than $$$left lceil frac{1}{3} cdot 2^{n + 1} ight ceil$$$ queries. Here $$$lceil x ceil$$$ denotes the value of $$$x$$$ rounded up to the nearest integer. Note that the tournament is long over, meaning that the results are fixed and do not depend on your queries. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 2^{14}$$$) — the number of test cases. The only line of input contains a single integer $$$n$$$ ($$$1 leq n leq 17$$$). It is guaranteed that the sum of $$$2^n$$$ over all test cases does not exceed $$$2^{17}$$$. Interaction The interaction for each test case begins by reading the integer $$$n$$$. To make a query, output "? a b" ($$$1 leq a, b leq 2^n$$$) without quotes. Afterwards, you should read one single integer — the answer for your query. You can make at most $$$left lceil frac{1}{3} cdot 2^{n + 1} ight ceil$$$ such queries in each test case. 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 exceed the limit of queries, or has given 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. When you are ready to give the final answer, output "! x" ($$$1 leq x leq 2^n$$$) without quotes — the winner of the tournament. Giving this answer does not count towards the limit of queries. After solving a test case, your program should move to the next one immediately. After solving all test cases, your program should be terminated immediately. After printing a query or the answer do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hacks To hack, use the following format. The first line contains an integer $$$t$$$ ($$$1 leq t leq 2^{14}$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 17$$$). The second line of each test case contains $$$2^n$$$ numbers on a line — the number of wins of each participant. There should be a sequence of matches that is consistent with the number of wins. The sum of $$$2^n$$$ should not exceed $$$2^{17}$$$. Example Output ? 1 4 ? 1 6 ? 5 7 ! 7 Note The tournament in the first test case is shown below. The number of wins is $$$[1,0,0,2,0,1,3,0]$$$. In this example, the winner is the $$$7$$$-th contestant.
1,800
false
true
false
false
false
true
false
false
false
false
2,003
93E
Igor K. very much likes a multiplayer role playing game WineAge II. Who knows, perhaps, that might be the reason for his poor performance at the university. As any person who plays the game, he is interested in equipping his hero with as good weapon and outfit as possible. One day, as he was reading the game's forum yet again, he discovered a very interesting fact. As it turns out, each weapon in the game is characterised with _k_ different numbers: _a_1,u2009...,u2009_a__k_. They are called hit indicators and according to the game developers' plan they are pairwise coprime. The damage that is inflicted during a hit depends not only on the weapon's characteristics, but also on the hero's strength parameter. Thus, if the hero's strength equals _n_, than the inflicted damage will be calculated as the number of numbers on the segment , that aren't divisible by any hit indicator _a__i_. Recently, having fulfilled another quest, Igor K. found a new Lostborn sword. He wants to know how much damage he will inflict upon his enemies if he uses it. Input The first line contains two integers: _n_ and _k_ (1u2009≤u2009_n_u2009≤u20091013, 1u2009≤u2009_k_u2009≤u2009100). They are the indicator of Igor K's hero's strength and the number of hit indicators. The next line contains space-separated _k_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u20091000). They are Lostborn sword's hit indicators. The given _k_ numbers are pairwise coprime. Output Print the single number — the damage that will be inflicted by Igor K.'s hero when he uses his new weapon. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.
2,600
true
false
false
true
false
false
false
false
false
false
9,511
377C
Kostya is a progamer specializing in the discipline of Dota 2. Valve Corporation, the developer of this game, has recently released a new patch which turned the balance of the game upside down. Kostya, as the captain of the team, realizes that the greatest responsibility lies on him, so he wants to resort to the analysis of innovations patch from the mathematical point of view to choose the best heroes for his team in every game. A Dota 2 match involves two teams, each of them must choose some heroes that the players of the team are going to play for, and it is forbidden to choose the same hero several times, even in different teams. In large electronic sports competitions where Kostya's team is going to participate, the matches are held in the Captains Mode. In this mode the captains select the heroes by making one of two possible actions in a certain, predetermined order: pick or ban. To pick a hero for the team. After the captain picks, the picked hero goes to his team (later one of a team members will play it) and can no longer be selected by any of the teams. To ban a hero. After the ban the hero is not sent to any of the teams, but it still can no longer be selected by any of the teams. The team captain may miss a pick or a ban. If he misses a pick, a random hero is added to his team from those that were available at that moment, and if he misses a ban, no hero is banned, as if there was no ban. Kostya has already identified the strength of all the heroes based on the new patch fixes. Of course, Kostya knows the order of picks and bans. The strength of a team is the sum of the strengths of the team's heroes and both teams that participate in the match seek to maximize the difference in strengths in their favor. Help Kostya determine what team, the first one or the second one, has advantage in the match, and how large the advantage is. Input The first line contains a single integer _n_ (2u2009≤u2009_n_u2009≤u2009100)xa0— the number of heroes in Dota 2. The second line contains _n_ integers _s_1, _s_2, ..., _s__n_ (1u2009≤u2009_s__i_u2009≤u2009106)xa0— the strengths of all the heroes. The third line contains a single integer _m_ (2u2009≤u2009_m_u2009≤u2009_min_(_n_,u200920))xa0— the number of actions the captains of the team must perform. Next _m_ lines look like "_action_xa0_team_", where _action_ is the needed action: a pick (represented as a "p") or a ban (represented as a "b"), and _team_ is the number of the team that needs to perform the action (number 1 or 2). It is guaranteed that each team makes at least one pick. Besides, each team has the same number of picks and the same number of bans.
2,200
false
false
false
true
false
false
false
false
false
false
8,322
1998E1
Drink water. — Sun Tzu, The Art of Becoming a Healthy Programmer This is the easy version of the problem. The only difference is that $$$x=n$$$ in this version. You must solve both versions to be able to hack. You are given two integers $$$n$$$ and $$$x$$$ ($$$x=n$$$). There are $$$n$$$ balls lined up in a row, numbered from $$$1$$$ to $$$n$$$ from left to right. Initially, there is a value $$$a_i$$$ written on the $$$i$$$-th ball. For each integer $$$i$$$ from $$$1$$$ to $$$n$$$, we define a function $$$f(i)$$$ as follows: Suppose you have a set $$$S = {1, 2, ldots, i}$$$. In each operation, you have to select an integer $$$l$$$ ($$$1 leq l < i$$$) from $$$S$$$ such that $$$l$$$ is not the largest element of $$$S$$$. Suppose $$$r$$$ is the smallest element in $$$S$$$ which is greater than $$$l$$$. If $$$a_l > a_r$$$, you set $$$a_l = a_l + a_r$$$ and remove $$$r$$$ from $$$S$$$. If $$$a_l < a_r$$$, you set $$$a_r = a_l + a_r$$$ and remove $$$l$$$ from $$$S$$$. If $$$a_l = a_r$$$, you choose either the integer $$$l$$$ or $$$r$$$ to remove from $$$S$$$: If you choose to remove $$$l$$$ from $$$S$$$, you set $$$a_r = a_l + a_r$$$ and remove $$$l$$$ from $$$S$$$. If you choose to remove $$$r$$$ from $$$S$$$, you set $$$a_l = a_l + a_r$$$ and remove $$$r$$$ from $$$S$$$. $$$f(i)$$$ denotes the number of integers $$$j$$$ ($$$1 le j le i$$$) such that it is possible to obtain $$$S = {j}$$$ after performing the above operations exactly $$$i - 1$$$ times. For each integer $$$i$$$ from $$$x$$$ to $$$n$$$, you need to find $$$f(i)$$$. Input The first line contains $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 leq n leq 2 cdot 10^5; x = n$$$)xa0— the number of balls and the smallest index $$$i$$$ for which you need to find $$$f(i)$$$. The second line of each test case contains $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the initial number written on each ball. 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 $$$n-x+1$$$ space separated integers on a new line, where the $$$j$$$-th integer should represent $$$f(x+j-1)$$$. Example Input 3 5 5 1 2 3 2 1 7 7 4 5 1 2 1 4 5 11 11 1 2 3 1 1 9 3 2 4 1 3 Note In the first test case, you are required to calculate $$$f(5)$$$. It can be shown that after $$$4$$$ operations, $$$S$$$ can contain $$$2$$$, $$$3$$$, or $$$4$$$. The following shows the operations required to make $$$S = {4}$$$. Initially, $$$S = {1, 2, 3, 4, 5}$$$ and $$$a = [1, 2, 3, 2, 1]$$$. Choose $$$l = 1$$$. Naturally, $$$r = 2$$$. Since $$$a_1 < a_2$$$, we set $$$a_2 = 1 + 2$$$ and remove $$$1$$$ from $$$S$$$. Now, $$$S = {2, 3, 4, 5}$$$ and $$$a = [1, 3, 3, 2, 1]$$$. Choose $$$l = 4$$$. Naturally, $$$r = 5$$$. Since $$$a_4 > a_5$$$, we set $$$a_4 = 2 + 1$$$ and remove $$$5$$$ from $$$S$$$. Now, $$$S = {2, 3, 4}$$$ and $$$a = [1, 3, 3, 3, 1]$$$. Choose $$$l = 3$$$. Naturally, $$$r = 4$$$. Since $$$a_3 = a_4$$$, we have a choice whether to remove $$$3$$$ or $$$4$$$. Since we want to preserve $$$4$$$, let's remove $$$3$$$. So, set $$$a_4 = 3 + 3$$$ and remove $$$3$$$ from $$$S$$$. Now, $$$S = {2, 4}$$$ and $$$a = [1, 3, 3, 6, 1]$$$. Choose $$$l = 2$$$. Naturally, $$$r = 4$$$. Since $$$a_2 < a_4$$$, we set $$$a_4 = 3 + 6$$$ and remove $$$2$$$ from $$$S$$$. Finally, $$$S = {4}$$$ and $$$a = [1, 3, 3, 9, 1]$$$. In the second test case, you are required to calculate $$$f(7)$$$. It can be shown that after $$$6$$$ operations, $$$S$$$ can contain $$$2$$$, $$$4$$$, $$$6$$$, or $$$7$$$.
2,200
false
true
false
false
true
false
true
true
false
false
273
1511A
You are an upcoming movie director, and you have just released your first movie. You have also launched a simple review site with two buttons to pressxa0— upvote and downvote. However, the site is not so simple on the inside. There are two servers, each with its separate counts for the upvotes and the downvotes. $$$n$$$ reviewers enter the site one by one. Each reviewer is one of the following types: type $$$1$$$: a reviewer has watched the movie, and they like itxa0— they press the upvote button; type $$$2$$$: a reviewer has watched the movie, and they dislike itxa0— they press the downvote button; type $$$3$$$: a reviewer hasn't watched the moviexa0— they look at the current number of upvotes and downvotes of the movie on the server they are in and decide what button to press. If there are more downvotes than upvotes, then a reviewer downvotes the movie. Otherwise, they upvote the movie. Each reviewer votes on the movie exactly once. Since you have two servers, you can actually manipulate the votes so that your movie gets as many upvotes as possible. When a reviewer enters a site, you know their type, and you can send them either to the first server or to the second one. What is the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 le n le 50$$$)xa0— the number of reviewers. The second line of each testcase contains $$$n$$$ integers $$$r_1, r_2, dots, r_n$$$ ($$$1 le r_i le 3$$$)xa0— the types of the reviewers in the same order they enter the site. Output For each testcase print a single integerxa0— the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to. Example Input 4 1 2 3 1 2 3 5 1 1 1 1 1 3 3 3 2 Note In the first testcase of the example you can send the only reviewer to either of the serversxa0— they'll downvote anyway. The movie won't receive any upvotes. In the second testcase of the example you can send all reviewers to the first server: the first reviewer upvotes; the second reviewer downvotes; the last reviewer sees that the number of downvotes is not greater than the number of upvotesxa0— upvote themselves. There are two upvotes in total. Alternatevely, you can send the first and the second reviewers to the first server and the last reviewerxa0— to the second server: the first reviewer upvotes on the first server; the second reviewer downvotes on the first server; the last reviewer sees no upvotes or downvotes on the second serverxa0— upvote themselves.
800
false
true
false
false
false
false
false
false
false
false
3,116
1073D
XXI Berland Annual Fair is coming really soon! Traditionally fair consists of $$$n$$$ booths, arranged in a circle. The booths are numbered $$$1$$$ through $$$n$$$ clockwise with $$$n$$$ being adjacent to $$$1$$$. The $$$i$$$-th booths sells some candies for the price of $$$a_i$$$ burles per item. Each booth has an unlimited supply of candies. Polycarp has decided to spend at most $$$T$$$ burles at the fair. However, he has some plan in mind for his path across the booths: at first, he visits booth number $$$1$$$; if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately; then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not). Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth. Calculate the number of candies Polycarp will buy. Input The first line contains two integers $$$n$$$ and $$$T$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le T le 10^{18}$$$) — the number of booths at the fair and the initial amount of burles Polycarp has. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$) — the price of the single candy at booth number $$$i$$$. Output Print a single integer — the total number of candies Polycarp will buy. Note Let's consider the first example. Here are Polycarp's moves until he runs out of money: 1. Booth $$$1$$$, buys candy for $$$5$$$, $$$T = 33$$$; 2. Booth $$$2$$$, buys candy for $$$2$$$, $$$T = 31$$$; 3. Booth $$$3$$$, buys candy for $$$5$$$, $$$T = 26$$$; 4. Booth $$$1$$$, buys candy for $$$5$$$, $$$T = 21$$$; 5. Booth $$$2$$$, buys candy for $$$2$$$, $$$T = 19$$$; 6. Booth $$$3$$$, buys candy for $$$5$$$, $$$T = 14$$$; 7. Booth $$$1$$$, buys candy for $$$5$$$, $$$T = 9$$$; 8. Booth $$$2$$$, buys candy for $$$2$$$, $$$T = 7$$$; 9. Booth $$$3$$$, buys candy for $$$5$$$, $$$T = 2$$$; 10. Booth $$$1$$$, buys no candy, not enough money; 11. Booth $$$2$$$, buys candy for $$$2$$$, $$$T = 0$$$. No candy can be bought later. The total number of candies bought is $$$10$$$. In the second example he has $$$1$$$ burle left at the end of his path, no candy can be bought with this amount.
1,700
false
true
false
false
true
false
true
true
false
false
5,350
1530C
You and your friend Ilya are participating in an individual programming contest consisting of multiple stages. A contestant can get between $$$0$$$ and $$$100$$$ points, inclusive, for each stage, independently of other contestants. Points received by contestants in different stages are used for forming overall contest results. Suppose that $$$k$$$ stages of the contest are completed. For each contestant, $$$k - lfloor frac{k}{4} floor$$$ stages with the highest scores are selected, and these scores are added up. This sum is the overall result of the contestant. (Here $$$lfloor t floor$$$ denotes rounding $$$t$$$ down.) For example, suppose $$$9$$$ stages are completed, and your scores are $$$50, 30, 50, 50, 100, 10, 30, 100, 50$$$. First, $$$7$$$ stages with the highest scores are chosenxa0— for example, all stages except for the $$$2$$$-nd and the $$$6$$$-th can be chosen. Then your overall result is equal to $$$50 + 50 + 50 + 100 + 30 + 100 + 50 = 430$$$. As of now, $$$n$$$ stages are completed, and you know the points you and Ilya got for these stages. However, it is unknown how many more stages will be held. You wonder what the smallest number of additional stages is, after which your result might become greater than or equal to Ilya's result, at least in theory. Find this number! Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the number of completed stages. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 100$$$)xa0— your points for the completed stages. The third line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$0 le b_i le 100$$$)xa0— Ilya's points for the completed stages. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print a single integerxa0— the smallest number of additional stages required for your result to be able to become greater than or equal to Ilya's result. If your result is already not less than Ilya's result, print $$$0$$$. Example Input 5 1 100 0 1 0 100 4 20 30 40 50 100 100 100 100 4 10 20 30 40 100 100 100 100 7 7 59 62 52 27 31 55 33 35 50 98 83 80 64 Note In the first test case, you have scored $$$100$$$ points for the first stage, while Ilya has scored $$$0$$$. Thus, your overall result ($$$100$$$) is already not less than Ilya's result ($$$0$$$). In the second test case, you have scored $$$0$$$ points for the first stage, while Ilya has scored $$$100$$$. A single stage with an opposite result is enough for both your and Ilya's overall scores to become equal to $$$100$$$. In the third test case, your overall result is $$$30 + 40 + 50 = 120$$$, while Ilya's result is $$$100 + 100 + 100 = 300$$$. After three additional stages your result might become equal to $$$420$$$, while Ilya's result might become equal to $$$400$$$. In the fourth test case, your overall result after four additional stages might become equal to $$$470$$$, while Ilya's result might become equal to $$$400$$$. Three stages are not enough.
1,200
false
true
false
false
false
false
true
true
true
false
3,013
1423K
In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks. More precisely, two different numbers $$$a$$$ and $$$b$$$ are friends if $$$gcd(a,b)$$$, $$$frac{a}{gcd(a,b)}$$$, $$$frac{b}{gcd(a,b)}$$$ can form sides of a triangle. Three numbers $$$a$$$, $$$b$$$ and $$$c$$$ can form sides of a triangle if $$$a + b > c$$$, $$$b + c > a$$$ and $$$c + a > b$$$. In a group of numbers, a number is lonely if it doesn't have any friends in that group. Given a group of numbers containing all numbers from $$$1, 2, 3, ..., n$$$, how many numbers in that group are lonely? Input The first line contains a single integer $$$t$$$ $$$(1 leq t leq 10^6)$$$ - number of test cases. On next line there are $$$t$$$ numbers, $$$n_i$$$ $$$(1 leq n_i leq 10^6)$$$ - meaning that in case $$$i$$$ you should solve for numbers $$$1, 2, 3, ..., n_i$$$. Output For each test case, print the answer on separate lines: number of lonely numbers in group $$$1, 2, 3, ..., n_i$$$. Note For first test case, $$$1$$$ is the only number and therefore lonely. For second test case where $$$n=5$$$, numbers $$$1$$$, $$$3$$$ and $$$5$$$ are lonely. For third test case where $$$n=10$$$, numbers $$$1$$$, $$$5$$$ and $$$7$$$ are lonely.
1,600
true
false
false
false
false
false
false
true
false
false
3,553
1385B
Problem - 1385B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy *800 No tag edit access → Contest materials ") Editorial") — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 50$$$) — the length of permutation. The second line of the test case contains $$$2n$$$ integers $$$a_1, a_2, dots, a_{2n}$$$ ($$$1 le a_i le n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the array $$$a$$$ represents the result of merging of some permutation $$$p$$$ with the same permutation $$$p$$$. Output For each test case, print the answer: $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$), representing the initial permutation. It is guaranteed that the answer exists and is unique. Example Input 5 2 1 1 2 2 4 1 3 1 4 3 4 2 2 5 1 2 1 2 3 4 3 5 4 5 3 1 2 3 1 2 3 4 2 3 2 4 1 3 4 1 Output 1 2 1 3 4 2 1 2 3 4 5 1 2 3 2 3 4 1
800
false
true
false
false
false
false
false
false
false
false
3,746
1157E
You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$. All elements of both arrays are from $$$0$$$ to $$$n-1$$$. You can reorder elements of the array $$$b$$$ (if you want, you may leave the order of elements as it is). After that, let array $$$c$$$ be the array of length $$$n$$$, the $$$i$$$-th element of this array is $$$c_i = (a_i + b_i) % n$$$, where $$$x % y$$$ is $$$x$$$ modulo $$$y$$$. Your task is to reorder elements of the array $$$b$$$ to obtain the lexicographically minimum possible array $$$c$$$. Array $$$x$$$ of length $$$n$$$ is lexicographically less than array $$$y$$$ of length $$$n$$$, if there exists such $$$i$$$ ($$$1 le i le n$$$), that $$$x_i < y_i$$$, and for any $$$j$$$ ($$$1 le j < i$$$) $$$x_j = y_j$$$. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of elements in $$$a$$$, $$$b$$$ and $$$c$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i < n$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$0 le b_i < n$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. Output Print the lexicographically minimum possible array $$$c$$$. Recall that your task is to reorder elements of the array $$$b$$$ and obtain the lexicographically minimum possible array $$$c$$$, where the $$$i$$$-th element of $$$c$$$ is $$$c_i = (a_i + b_i) % n$$$. Examples Input 7 2 5 1 5 3 4 3 2 4 3 5 6 5 1
1,700
false
true
false
false
true
false
false
true
false
false
4,918
982A
Problem - 982A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force constructive algorithms *1200 No tag edit access → Contest materials ") ") . The goal is to determine whether this seating is "maximal". Note that the first and last seats are not adjacent (if $$$n e 2$$$). Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 1000$$$)xa0— the number of chairs. The next line contains a string of $$$n$$$ characters, each of them is either zero or one, describing the seating. Output Output "Yes" (without quotation marks) if the seating is "maximal". Otherwise print "No". You are allowed to print letters in whatever case you'd like (uppercase or lowercase). Examples Input 3 101 Output Yes Input 4 1011 Output No Input 5 10001 Output No Note In sample case one the given seating is maximal. In sample case two the person at chair three has a neighbour to the right. In sample case three it is possible to seat yet another person into chair three.
1,200
false
false
false
false
false
true
true
false
false
false
5,792
1005F
There are $$$n$$$ cities in Berland. Some pairs of cities are connected by roads. All roads are bidirectional. Each road connects two different cities. There is at most one road between a pair of cities. The cities are numbered from $$$1$$$ to $$$n$$$. It is known that, from the capital (the city with the number $$$1$$$), you can reach any other city by moving along the roads. The President of Berland plans to improve the country's road network. The budget is enough to repair exactly $$$n-1$$$ roads. The President plans to choose a set of $$$n-1$$$ roads such that: it is possible to travel from the capital to any other city along the $$$n-1$$$ chosen roads, if $$$d_i$$$ is the number of roads needed to travel from the capital to city $$$i$$$, moving only along the $$$n-1$$$ chosen roads, then $$$d_1 + d_2 + dots + d_n$$$ is minimized (i.e. as minimal as possible). In other words, the set of $$$n-1$$$ roads should preserve the connectivity of the country, and the sum of distances from city $$$1$$$ to all cities should be minimized (where you can only use the $$$n-1$$$ chosen roads). The president instructed the ministry to prepare $$$k$$$ possible options to choose $$$n-1$$$ roads so that both conditions above are met. Write a program that will find $$$k$$$ possible ways to choose roads for repair. If there are fewer than $$$k$$$ ways, then the program should output all possible valid ways to choose roads. Input The first line of the input contains integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 le n le 2cdot10^5, n-1 le m le 2cdot10^5, 1 le k le 2cdot10^5$$$), where $$$n$$$ is the number of cities in the country, $$$m$$$ is the number of roads and $$$k$$$ is the number of options to choose a set of roads for repair. It is guaranteed that $$$m cdot k le 10^6$$$. The following $$$m$$$ lines describe the roads, one road per line. Each line contains two integers $$$a_i$$$, $$$b_i$$$ ($$$1 le a_i, b_i le n$$$, $$$a_i e b_i$$$) — the numbers of the cities that the $$$i$$$-th road connects. There is at most one road between a pair of cities. The given set of roads is such that you can reach any city from the capital. Output Print $$$t$$$ ($$$1 le t le k$$$) — the number of ways to choose a set of roads for repair. Recall that you need to find $$$k$$$ different options; if there are fewer than $$$k$$$ of them, then you need to find all possible different valid options. In the following $$$t$$$ lines, print the options, one per line. Print an option as a string of $$$m$$$ characters where the $$$j$$$-th character is equal to '1' if the $$$j$$$-th road is included in the option, and is equal to '0' if the road is not included. The roads should be numbered according to their order in the input. The options can be printed in any order. All the $$$t$$$ lines should be different. Since it is guaranteed that $$$m cdot k le 10^6$$$, the total length of all the $$$t$$$ lines will not exceed $$$10^6$$$. If there are several answers, output any of them. Examples Input 4 6 3 1 2 2 3 1 4 4 3 2 4 1 3 Input 5 6 2 1 2 1 3 2 4 2 5 3 4 3 5
2,100
false
false
false
false
false
false
true
false
false
true
5,656
1637B
Problem - 1637B - Codeforces =============== xa0 ,$$$$$$ where $$$operatorname{mex}$$$ of a set of numbers $$$S$$$ is the smallest non-negative integer that does not occur in the set $$$S$$$. In other words, the cost of a partition is the number of segments plus the sum of MEX over all segments. Let's define the value of an array $$$b_1, b_2, ldots, b_k$$$ as the maximum possible cost over all partitions of this array. You are given an array $$$a$$$ of size $$$n$$$. Find the sum of values of all its subsegments. An array $$$x$$$ is a subsegment of an array $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The input contains several test cases. The first line contains one integer $$$t$$$ ($$$1 leq t leq 30$$$)xa0— the number of test cases. The first line for each test case contains one integer $$$n$$$ ($$$1 leq n leq 100$$$)xa0— the length of the array. The second line contains a sequence of integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 10^9$$$)xa0— the array elements. It is guaranteed that the sum of the values $$$n$$$ over all test cases does not exceed $$$100$$$. Output For each test case print a single integerxa0— the answer to the problem. Example Input 4 2 1 2 3 2 0 1 4 2 0 5 1 5 0 1 1 0 1 Output 4 14 26 48 Note In the second test case: The best partition for the subsegment $$$
1,100
true
true
false
true
false
false
true
false
false
false
2,433
1140G
You are given a special undirected graph. It consists of $$$2n$$$ vertices numbered from $$$1$$$ to $$$2n$$$. The following properties hold for the graph: there are exactly $$$3n-2$$$ edges in the graph: $$$n$$$ edges connect vertices having odd numbers with vertices having even numbers, $$$n - 1$$$ edges connect vertices having odd numbers with each other, and $$$n - 1$$$ edges connect vertices having even numbers with each other; for each edge $$$(u, v)$$$ between a pair of vertices with odd numbers, there exists an edge $$$(u + 1, v + 1)$$$, and vice versa; for each odd number $$$u in [1, 2n - 1]$$$, there exists an edge $$$(u, u + 1)$$$; the graph is connected; moreover, if we delete all vertices with even numbers from it, and all edges incident to them, the graph will become a tree (the same applies to deleting odd vertices). So, the graph can be represented as two trees having the same structure, and $$$n$$$ edges connecting each vertex of the first tree to the corresponding vertex of the second tree. Edges of the graph are weighted. The length of some simple path in the graph is the sum of weights of traversed edges. You are given $$$q$$$ queries to this graph; in each query, you are asked to compute the length of the shortest path between some pair of vertices in this graph. Can you answer all of the queries? Input The first line of the input contains one integer $$$n$$$ ($$$2 le n le 3 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$w_{1, 2}$$$, $$$w_{3,4}$$$, ..., $$$w_{2n - 1, 2n}$$$ ($$$1 le w_{i, i + 1} le 10^{12}$$$). These integers describe the weights of the edges connecting odd vertices with even ones. Then $$$n-1$$$ lines follow. $$$i$$$-th line contains four integers $$$x_i$$$, $$$y_i$$$, $$$w_{i, 1}$$$ and $$$w_{i, 2}$$$ ($$$1 le x_i, y_i le n$$$, $$$x_i e y_i$$$, $$$1 le w_{i, j} le 10^{12}$$$); it describes two edges: one connecting $$$2x_i - 1$$$ with $$$2y_i - 1$$$ and having weight $$$w_{i, 1}$$$; another connecting $$$2x_i$$$ with $$$2y_i$$$ and having weight $$$w_{i, 2}$$$. The next line contains one integer $$$q$$$ ($$$1 le q le 6 cdot 10^5$$$) — the number of queries. Then $$$q$$$ lines follow, $$$i$$$-th line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le 2n$$$, $$$u_i e v_i$$$), describing a query "compute the length of the shortest path between vertices $$$u_i$$$ and $$$v_i$$$".
2,700
false
false
false
false
true
false
false
false
false
false
5,022
279A
Problem - 279A - 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 geometry implementation *1400 No tag edit access → Contest materials ") ") Разбор Задач") ,u2009(1,u20090)],
1,400
false
false
true
false
false
false
true
false
false
false
8,720
1967D
Little R is a magician who likes non-decreasing arrays. She has an array of length $$$n$$$, initially as $$$a_1, ldots, a_n$$$, in which each element is an integer between $$$[1, m]$$$. She wants it to be non-decreasing, i.e., $$$a_1 leq a_2 leq ldots leq a_n$$$. To do this, she can perform several magic tricks. Little R has a fixed array $$$b_1ldots b_m$$$ of length $$$m$$$. Formally, let's define a trick as a procedure that does the following things in order: Choose a set $$$S subseteq {1, 2, ldots, n}$$$. For each $$$u in S$$$, assign $$$a_u$$$ with $$$b_{a_u}$$$. Little R wonders how many tricks are needed at least to make the initial array non-decreasing. If it is not possible with any amount of tricks, print $$$-1$$$ instead. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1le tle 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1leq n leq 10^6$$$, $$$1 leq m leq 10^6$$$)xa0— the length of the initial array and the range of the elements in the array. The second line of each test case contains $$$n$$$ integers $$$a_1, ldots, a_n$$$ ($$$1 leq a_i leq m$$$)xa0— the initial array. The third line of each test case contains $$$m$$$ integers $$$b_1, ldots, b_m$$$ ($$$1 leq b_i leq m$$$)xa0— the fixed magic array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output a single integer: the minimum number of tricks needed, or $$$-1$$$ if it is impossible to make $$$a_1, ldots, a_n$$$ non-decreasing. Example Input 3 5 8 1 6 3 7 1 2 3 5 8 7 1 5 6 3 3 1 3 2 2 1 3 10 10 2 8 5 4 8 4 1 5 10 10 6 7 2 6 3 4 1 1 3 5 Note In the first case, the initial array $$$a_1, ldots, a_n$$$ is $$$[1, 6, 3, 7, 1]$$$. You can choose $$$S$$$ as follows: first trick: $$$S = [2, 4, 5]$$$, $$$a = [1, 1, 3, 5, 2]$$$; second trick: $$$S = [5]$$$, $$$a = [1, 1, 3, 5, 3]$$$; third trick: $$$S = [5]$$$, $$$a = [1, 1, 3, 5, 5]$$$. So it is possible to make $$$a_1, ldots, a_n$$$ non-decreasing using $$$3$$$ tricks. It can be shown that this is the minimum possible amount of tricks. In the second case, it is impossible to make $$$a_1, ldots, a_n$$$ non-decreasing.
2,800
false
false
true
false
false
false
false
true
false
true
504
1657A
There's a chip in the point $$$(0, 0)$$$ of the coordinate plane. In one operation, you can move the chip from some point $$$(x_1, y_1)$$$ to some point $$$(x_2, y_2)$$$ if the Euclidean distance between these two points is an integer (i.e. $$$sqrt{(x_1-x_2)^2+(y_1-y_2)^2}$$$ is integer). Your task is to determine the minimum number of operations required to move the chip from the point $$$(0, 0)$$$ to the point $$$(x, y)$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 3000$$$)xa0— number of test cases. The single line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$0 le x, y le 50$$$)xa0— the coordinates of the destination point. Output For each test case, print one integerxa0— the minimum number of operations required to move the chip from the point $$$(0, 0)$$$ to the point $$$(x, y)$$$. Note In the first example, one operation $$$(0, 0) ightarrow (8, 6)$$$ is enough. $$$sqrt{(0-8)^2+(0-6)^2}=sqrt{64+36}=sqrt{100}=10$$$ is an integer. In the second example, the chip is already at the destination point. In the third example, the chip can be moved as follows: $$$(0, 0) ightarrow (5, 12) ightarrow (9, 15)$$$. $$$sqrt{(0-5)^2+(0-12)^2}=sqrt{25+144}=sqrt{169}=13$$$ and $$$sqrt{(5-9)^2+(12-15)^2}=sqrt{16+9}=sqrt{25}=5$$$ are integers.
800
true
false
false
false
false
false
true
false
false
false
2,345
958C2
Heidi has now broken the first level of encryption of the Death Star plans, and is staring at the screen presenting her with the description of the next code she has to enter. It looks surprisingly similar to the first one – seems like the Empire engineers were quite lazy... Heidi is once again given a sequence _A_, but now she is also given two integers _k_ and _p_. She needs to find out what the encryption key _S_ is. Let _X_ be a sequence of integers, and _p_ a positive integer. We define the score of _X_ to be the sum of the elements of _X_ modulo _p_. Heidi is given a sequence _A_ that consists of _N_ integers, and also given integers _k_ and _p_. Her goal is to split _A_ into _k_ part such that: Each part contains at least 1 element of _A_, and each part consists of contiguous elements of _A_. No two parts overlap. The total sum _S_ of the scores of those parts is maximized. Output the sum _S_ – the encryption code. Input The first line of the input contains three space-separated integer _N_, _k_ and _p_ (_k_u2009≤u2009_N_u2009≤u200920u2009000, 2u2009≤u2009_k_u2009≤u200950, 2u2009≤u2009_p_u2009≤u2009100) – the number of elements in _A_, the number of parts _A_ should be split into, and the modulo for computing scores, respectively. The second line contains _N_ space-separated integers that are the elements of _A_. Each integer is from the interval [1,u20091u2009000u2009000]. Output Output the number _S_ as described in the problem statement. Examples Input 10 5 12 16 3 24 13 9 8 7 5 12 12 Note In the first example, if the input sequence is split as (3,u20094), (7), (2), the total score would be . It is easy to see that this score is maximum. In the second example, one possible way to obtain score 37 is to make the following split: (16,u20093,u200924), (13,u20099), (8), (7), (5,u200912,u200912).
2,000
false
false
false
true
false
false
false
false
false
false
5,887
707C
Problem - 707C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math number theory *1500 No tag edit access → Contest materials , (5,u200912,u200913) and (6,u20098,u200910) are Pythagorean triples. Here Katya wondered if she can specify the length of some side of right triangle and find any Pythagorean triple corresponding to such length? Note that the side which length is specified can be a cathetus as well as hypotenuse. Katya had no problems with completing this task. Will you do the same? Input The only line of the input contains single integer _n_ (1u2009≤u2009_n_u2009≤u2009109)xa0— the length of some side of a right triangle. Output Print two integers _m_ and _k_ (1u2009≤u2009_m_,u2009_k_u2009≤u20091018), such that _n_, _m_ and _k_ form a Pythagorean triple, in the only line. In case if there is no any Pythagorean triple containing integer _n_, print u2009-u20091 in the only line. If there are many answers, print any of them. Examples Input 3 Output 4 5 Input 6 Output 8 10 Input 1 Output -1 Input 17 Output 144 145 Input 67 Output 2244 2245 Note Illustration for the first sample.
1,500
true
false
false
false
false
false
false
false
false
false
6,992
1583E
She does her utmost to flawlessly carry out a person's last rites and preserve the world's balance of yin and yang. Hu Tao, being the little prankster she is, has tried to scare you with this graph problem! You are given a connected undirected graph of $$$n$$$ nodes with $$$m$$$ edges. You also have $$$q$$$ queries. Each query consists of two nodes $$$a$$$ and $$$b$$$. Initially, all edges in the graph have a weight of $$$0$$$. For each query, you must choose a simple path starting from $$$a$$$ and ending at $$$b$$$. Then you add $$$1$$$ to every edge along this path. Determine if it's possible, after processing all $$$q$$$ queries, for all edges in this graph to have an even weight. If so, output the choice of paths for each query. If it is not possible, determine the smallest number of extra queries you could add to make it possible. It can be shown that this number will not exceed $$$10^{18}$$$ under the given constraints. A simple path is defined as any path that does not visit a node more than once. An edge is said to have an even weight if its value is divisible by $$$2$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 3 cdot 10^5$$$, $$$n-1 leq m leq min{left(frac{n(n-1)}{2}, 3 cdot 10^5 ight)}$$$). Each of the next $$$m$$$ lines contains two integers $$$x$$$ and $$$y$$$ ($$$1 leq x, y leq n$$$, $$$x eq y$$$) indicating an undirected edge between node $$$x$$$ and $$$y$$$. The input will not contain self-loops or duplicate edges, and the provided graph will be connected. The next line contains a single integer $$$q$$$ ($$$1 leq q leq 3 cdot 10^5$$$). Each of the next $$$q$$$ lines contains two integers $$$a$$$ and $$$b$$$ ($$$1 leq a, b leq n, a eq b$$$), the description of each query. It is guaranteed that $$$nq leq 3 cdot 10^5$$$. Output If it is possible to force all edge weights to be even, print "YES" on the first line, followed by $$$2q$$$ lines indicating the choice of path for each query in the same order the queries are given. For each query, the first line should contain a single integer $$$x$$$: the number of nodes in the chosen path. The next line should then contain $$$x$$$ spaced separated integers $$$p_i$$$ indicating the path you take ($$$p_1 = a, p_x = b$$$ and all numbers should fall between $$$1$$$ and $$$n$$$). This path cannot contain duplicate nodes and must be a valid simple path in the graph. If it is impossible to force all edge weights to be even, print "NO" on the first line and the minimum number of added queries on the second line. Examples Input 6 7 2 1 2 3 3 5 1 4 6 1 5 6 4 5 3 1 4 5 1 4 5 Output YES 2 1 4 4 5 3 2 1 5 4 1 2 3 5 Input 5 7 4 3 4 5 2 1 1 4 1 3 3 5 3 2 4 4 2 3 5 5 1 4 5 Note Here is what the queries look like for the first test case (red corresponds to the 1st query, blue 2nd query, and green 3rd query): Notice that every edge in the graph is part of either $$$0$$$ or $$$2$$$ colored query edges. The graph in the second test case looks like this: There does not exist an assignment of paths that will force all edges to have even weights with the given queries. One must add at least $$$2$$$ new queries to obtain a set of queries that can satisfy the condition.
2,200
false
true
false
false
false
true
false
false
false
true
2,713
2041D
Image generated by ChatGPT 4o. You are given a two-dimensional maze with a start and end position. Your task is to find the fastest way to get from the start to the end position. The fastest way is to make the minimum number of steps where one step is going left, right, up, or down. Of course, you cannot walk through walls. There is, however, a catch: If you make more than three steps in the same direction, you lose balance and fall down. Therefore, it is forbidden to make more than three consecutive steps in the same direction. It is okay to walk three times to the right, then one step to the left, and then again three steps to the right. This has the same effect as taking five steps to the right, but is slower. Input The first line contains two numbers $$$n$$$ and $$$m$$$, which are the height and width of the maze. This is followed by an ASCII-representation of the maze where $$$ t{#}$$$ is a wall, $$$ t{.}$$$ is an empty space, and $$$ t S$$$ and $$$ t T$$$ are the start and end positions. $$$12 leq n imes m leq 200000$$$. $$$3leq n,m leq 10000$$$. Characters are only $$$ t{.#ST}$$$ and there is exactly one $$$ t{S}$$$ and one $$$ t{T}$$$. The outer borders are only $$$ t{#}$$$ (walls). Output The minimum number of steps to reach the end position from the start position or -1 if that is impossible. Examples Input 7 12 ############ #S........T# #.########.# #..........# #..........# #..#..#....# ############ Input 5 8 ######## #......# #.####.# #...T#S# ######## Input 5 8 ######## #.#S...# #.####.# #...T#.# ########
1,700
false
false
false
false
false
false
true
false
false
true
10
1360E
Polygon is not only the best platform for developing problems but also a square matrix with side $$$n$$$, initially filled with the character 0. On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly $$$2n$$$ cannons were placed. Initial polygon for $$$n=4$$$. Cannons shoot character 1. At any moment of time, no more than one cannon is shooting. When a 1 flies out of a cannon, it flies forward (in the direction of the shot) until it collides with a polygon border or another 1. After that, it takes the cell in which it was before the collision and remains there. Take a look at the examples for better understanding. More formally: if a cannon stands in the row $$$i$$$, to the left of the first column, and shoots with a 1, then the 1 starts its flight from the cell ($$$i, 1$$$) and ends in some cell ($$$i, j$$$); if a cannon stands in the column $$$j$$$, above the first row, and shoots with a 1, then the 1 starts its flight from the cell ($$$1, j$$$) and ends in some cell ($$$i, j$$$). For example, consider the following sequence of shots: 1. Shoot the cannon in the row $$$2$$$. xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0 2. Shoot the cannon in the row $$$2$$$. xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0xa0 3. Shoot the cannon in column $$$3$$$. You have a report from the military training on your desk. This report is a square matrix with side length $$$n$$$ consisting of 0 and 1. You wonder if the training actually happened. In other words, is there a sequence of shots such that, after the training, you get the given matrix? Each cannon can make an arbitrary number of shots. Before the training, each cell of the polygon contains 0. Input The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 le n le 50$$$)xa0— the size of the polygon. This is followed by $$$n$$$ lines of length $$$n$$$, consisting of 0 and 1xa0— the polygon matrix after the training. The total area of the matrices in all test cases in one test does not exceed $$$10^5$$$. Note The first test case was explained in the statement. The answer to the second test case is NO, since a 1 in a cell ($$$1, 1$$$) flying out of any cannon would continue its flight further.
1,300
false
false
true
true
false
false
false
false
false
true
3,881
186A
Problem - 186A - 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 *1100 No tag edit access → Contest materials Bachelor courses, have been told "no genome, no degree". That means that all dwarves should write a thesis on genome. Dwarven genome is far from simple. It is represented by a string that consists of lowercase Latin letters. Dwarf Misha has already chosen the subject for his thesis: determining by two dwarven genomes, whether they belong to the same race. Two dwarves belong to the same race if we can swap two characters in the first dwarf's genome and get the second dwarf's genome as a result. Help Dwarf Misha and find out whether two gnomes belong to the same race or not. Input The first line contains the first dwarf's genome: a non-empty string, consisting of lowercase Latin letters. The second line contains the second dwarf's genome: a non-empty string, consisting of lowercase Latin letters. The number of letters in each genome doesn't exceed 105. It is guaranteed that the strings that correspond to the genomes are different. The given genomes may have different length. Output Print "YES", if the dwarves belong to the same race. Otherwise, print "NO". Examples Input ab ba Output YES Input aa ab Output NO Note First example: you can simply swap two letters in string "ab". So we get "ba". Second example: we can't change string "aa" into string "ab", because "aa" does not contain letter "b".
1,100
false
false
true
false
false
false
false
false
false
false
9,099
1090C
Santa has prepared boxes with presents for $$$n$$$ kids, one box for each kid. There are $$$m$$$ kinds of presents: balloons, sweets, chocolate bars, toy cars... A child would be disappointed to receive two presents of the same kind, so all kinds of presents in one box are distinct. Having packed all the presents, Santa realized that different boxes can contain different number of presents. It would be unfair to the children, so he decided to move some presents between boxes, and make their sizes similar. After all movements, the difference between the maximal and the minimal number of presents in a box must be as small as possible. All presents in each box should still be distinct. Santa wants to finish the job as fast as possible, so he wants to minimize the number of movements required to complete the task. Given the sets of presents in each box, find the shortest sequence of movements of presents between boxes that minimizes the difference of sizes of the smallest and the largest box, and keeps all presents in each box distinct. Input The first line of input contains two integers $$$n$$$, $$$m$$$ ($$$1 leq n, m leq 100 000$$$), the number of boxes and the number of kinds of the presents. Denote presents with integers from $$$1$$$ to $$$m$$$. Each of the following $$$n$$$ lines contains the description of one box. It begins with an integer $$$s_i$$$ ($$$s_i geq 0$$$), the number of presents in the box, $$$s_i$$$ distinct integers between $$$1$$$ and $$$m$$$ follow, denoting the kinds of presents in that box. The total number of presents in all boxes does not exceed $$$500,000$$$. Output Print one integer $$$k$$$ at the first line of output, the number of movements in the shortest sequence that makes the sizes of the boxes differ by at most one. Then print $$$k$$$ lines that describe movements in the same order in which they should be performed. Each movement is described by three integers $$$from_i$$$, $$$to_i$$$, $$$kind_i$$$. It means that the present of kind $$$kind_i$$$ is moved from the box with number $$$from_i$$$ to the box with number $$$to_i$$$. Boxes are numbered from one in the order they are given in the input. At the moment when the movement is performed the present with kind $$$kind_i$$$ must be present in the box with number $$$from_i$$$. After performing all moves each box must not contain two presents of the same kind. If there are several optimal solutions, output any of them. Example Input 3 5 5 1 2 3 4 5 2 1 2 2 3 4
2,400
false
false
false
false
true
true
false
false
false
false
5,270
1194D
Problem - 1194D - 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 games math *1700 No tag edit access → Contest materials . Players take turns, Alice is first. Each player during his or her turn has to move the chip 1, 2 or _k_ cells to the left (so, if the chip is currently in the cell _i_, the player can move it into cell _i_u2009-u20091, _i_u2009-u20092 or _i_u2009-u2009_k_). The chip should not leave the borders of the paper strip: it is impossible, for example, to move it _k_ cells to the left if the current cell has number _i_u2009<u2009_k_. The player who can't make a move loses the game. Who wins if both participants play optimally? Alice and Bob would like to play several games, so you should determine the winner in each game. Input The first line contains the single integer _T_ (1u2009≤u2009_T_u2009≤u2009100) — the number of games. Next _T_ lines contain one game per line. All games are independent. Each of the next _T_ lines contains two integers _n_ and _k_ (0u2009≤u2009_n_u2009≤u2009109, 3u2009≤u2009_k_u2009≤u2009109) — the length of the strip and the constant denoting the third move, respectively. Output For each game, print Alice if Alice wins this game and Bob otherwise. Example Input 4 0 3 3 3 3 4 4 4 Output Bob Alice Bob Alice
1,700
true
false
false
false
false
false
false
false
false
false
4,733
920A
It is winter now, and Max decided it's about time he watered the garden. The garden can be represented as _n_ consecutive garden beds, numbered from 1 to _n_. _k_ beds contain water taps (_i_-th tap is located in the bed _x__i_), which, if turned on, start delivering water to neighbouring beds. If the tap on the bed _x__i_ is turned on, then after one second has passed, the bed _x__i_ will be watered; after two seconds have passed, the beds from the segment [_x__i_u2009-u20091,u2009_x__i_u2009+u20091] will be watered (if they exist); after _j_ seconds have passed (_j_ is an integer number), the beds from the segment [_x__i_u2009-u2009(_j_u2009-u20091),u2009_x__i_u2009+u2009(_j_u2009-u20091)] will be watered (if they exist). Nothing changes during the seconds, so, for example, we can't say that the segment [_x__i_u2009-u20092.5,u2009_x__i_u2009+u20092.5] will be watered after 2.5 seconds have passed; only the segment [_x__i_u2009-u20092,u2009_x__i_u2009+u20092] will be watered at that moment. The garden from test 1. White colour denotes a garden bed without a tap, red colour — a garden bed with a tap. The garden from test 1 after 2 seconds have passed after turning on the tap. White colour denotes an unwatered garden bed, blue colour — a watered bed. Max wants to turn on all the water taps at the same moment, and now he wonders, what is the minimum number of seconds that have to pass after he turns on some taps until the whole garden is watered. Help him to find the answer! Input The first line contains one integer _t_ — the number of test cases to solve (1u2009≤u2009_t_u2009≤u2009200). Then _t_ test cases follow. The first line of each test case contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009200, 1u2009≤u2009_k_u2009≤u2009_n_) — the number of garden beds and water taps, respectively. Next line contains _k_ integers _x__i_ (1u2009≤u2009_x__i_u2009≤u2009_n_) — the location of _i_-th water tap. It is guaranteed that for each condition _x__i_u2009-u20091u2009<u2009_x__i_ holds. It is guaranteed that the sum of _n_ over all test cases doesn't exceed 200. Note that in hacks you have to set _t_u2009=u20091. Output For each test case print one integer — the minimum number of seconds that have to pass after Max turns on some of the water taps, until the whole garden is watered. Note The first example consists of 3 tests: 1. There are 5 garden beds, and a water tap in the bed 3. If we turn it on, then after 1 second passes, only bed 3 will be watered; after 2 seconds pass, beds [1,u20093] will be watered, and after 3 seconds pass, everything will be watered. 2. There are 3 garden beds, and there is a water tap in each one. If we turn all of them on, then everything will be watered after 1 second passes. 3. There are 4 garden beds, and only one tap in the bed 1. It will take 4 seconds to water, for example, bed 4.
1,000
false
false
true
false
false
false
false
false
false
false
6,050
258E
The Little Elephant loves trees very much, he especially loves root trees. He's got a tree consisting of _n_ nodes (the nodes are numbered from 1 to _n_), with root at node number 1. Each node of the tree contains some list of numbers which initially is empty. The Little Elephant wants to apply _m_ operations. On the _i_-th operation (1u2009≤u2009_i_u2009≤u2009_m_) he first adds number _i_ to lists of all nodes of a subtree with the root in node number _a__i_, and then he adds number _i_ to lists of all nodes of the subtree with root in node _b__i_. After applying all operations the Little Elephant wants to count for each node _i_ number _c__i_ — the number of integers _j_ (1u2009≤u2009_j_u2009≤u2009_n_;xa0_j_u2009≠u2009_i_), such that the lists of the _i_-th and the _j_-th nodes contain at least one common number. Help the Little Elephant, count numbers _c__i_ for him. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105) — the number of the tree nodes and the number of operations. Each of the following _n_u2009-u20091 lines contains two space-separated integers, _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_,u2009_u__i_u2009≠u2009_v__i_), that mean that there is an edge between nodes number _u__i_ and _v__i_. Each of the following _m_ lines contains two space-separated integers, _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_), that stand for the indexes of the nodes in the _i_-th operation. It is guaranteed that the given graph is an undirected tree. Output In a single line print _n_ space-separated integers — _c_1,u2009_c_2,u2009...,u2009_c__n_. Examples Input 11 3 1 2 2 3 2 4 1 5 5 6 5 7 5 8 6 9 8 10 8 11 2 9 3 6 2 8 Output 0 6 7 6 0 2 0 5 4 5 5
2,400
false
false
false
false
true
false
false
false
false
false
8,798
1613B
You are given a sequence $$$a_1, a_2, dots, a_n$$$ consisting of $$$n$$$ pairwise distinct positive integers. Find $$$leftlfloor frac n 2 ight floor$$$ different pairs of integers $$$x$$$ and $$$y$$$ such that: $$$x eq y$$$; $$$x$$$ and $$$y$$$ appear in $$$a$$$; $$$x~mod~y$$$ doesn't appear in $$$a$$$. Note that some $$$x$$$ or $$$y$$$ can belong to multiple pairs. $$$lfloor x floor$$$ denotes the floor functionxa0— the largest integer less than or equal to $$$x$$$. $$$x~mod~y$$$ denotes the remainder from dividing $$$x$$$ by $$$y$$$. If there are multiple solutions, print any of them. It can be shown that at least one solution always exists. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0— the length of the sequence. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^6$$$). All numbers in the sequence are pairwise distinct. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output The answer for each testcase should contain $$$leftlfloor frac n 2 ight floor$$$ different pairs of integers $$$x$$$ and $$$y$$$ such that $$$x eq y$$$, $$$x$$$ and $$$y$$$ appear in $$$a$$$ and $$$x~mod~y$$$ doesn't appear in $$$a$$$. Print the pairs one after another. You can print the pairs in any order. However, the order of numbers in the pair should be exactly such that the first number is $$$x$$$ and the second number is $$$y$$$. All pairs should be pairwise distinct. If there are multiple solutions, print any of them. Example Input 4 2 1 4 4 2 8 3 4 5 3 8 5 9 7 6 2 7 5 3 4 8 Output 4 1 8 2 8 4 9 5 7 5 8 7 4 3 5 2 Note In the first testcase there are only two pairs: $$$(1, 4)$$$ and $$$(4, 1)$$$. $$$leftlfloor frac 2 2 ight floor=1$$$, so we have to find one pair. $$$1~mod~4=1$$$, and $$$1$$$ appears in $$$a$$$, so that pair is invalid. Thus, the only possible answer is a pair $$$(4, 1)$$$. In the second testcase, we chose pairs $$$8~mod~2=0$$$ and $$$8~mod~4=0$$$. $$$0$$$ doesn't appear in $$$a$$$, so that answer is valid. There are multiple possible answers for that testcase. In the third testcase, the chosen pairs are $$$9~mod~5=4$$$ and $$$7~mod~5=2$$$. Neither $$$4$$$, nor $$$2$$$, appears in $$$a$$$, so that answer is valid.
1,000
false
true
true
false
false
false
false
false
true
false
2,575
1729A
Vlad went into his appartment house entrance, now he is on the $$$1$$$-th floor. He was going to call the elevator to go up to his apartment. There are only two elevators in his house. Vlad knows for sure that: the first elevator is currently on the floor $$$a$$$ (it is currently motionless), the second elevator is located on floor $$$b$$$ and goes to floor $$$c$$$ ($$$b e c$$$). Please note, if $$$b=1$$$, then the elevator is already leaving the floor $$$1$$$ and Vlad does not have time to enter it. If you call the first elevator, it will immediately start to go to the floor $$$1$$$. If you call the second one, then first it will reach the floor $$$c$$$ and only then it will go to the floor $$$1$$$. It takes $$$x - y$$$ seconds for each elevator to move from floor $$$x$$$ to floor $$$y$$$. Vlad wants to call an elevator that will come to him faster. Help him choose such an elevator. Input The first line of the input contains the only $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. This is followed by $$$t$$$ lines, three integers each $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 le a, b, c le 10^8$$$, $$$b e c$$$)xa0— floor numbers described in the statement. Output Output $$$t$$$ numbers, each of which is the answer to the corresponding test case. As an answer, output: $$$1$$$, if it is better to call the first elevator; $$$2$$$, if it is better to call the second one; $$$3$$$, if it doesn't matter which elevator to call (both elevators will arrive in the same time). Note In the first test case of the example, the first elevator is already on the floor of $$$1$$$. In the second test case of the example, when called, the elevators would move as follows: At the time of the call, the first elevator is on the floor of $$$3$$$, and the second one is on the floor of $$$1$$$, but is already going to another floor; in $$$1$$$ second after the call, the first elevator would be on the floor $$$2$$$, the second one would also reach the floor $$$2$$$ and now can go to the floor $$$1$$$; in $$$2$$$ seconds, any elevator would reach the floor $$$1$$$. In the third test case of the example, the first elevator will arrive in $$$2$$$ seconds, and the second in $$$1$$$.
800
true
false
false
false
false
false
false
false
false
false
1,918
73C
Vasya plays the LionAge II. He was bored of playing with a stupid computer, so he installed this popular MMORPG, to fight with his friends. Vasya came up with the name of his character — non-empty string _s_, consisting of a lowercase Latin letters. However, in order not to put up a front of friends, Vasya has decided to change no more than _k_ letters of the character name so that the new name sounded as good as possible. Euphony of the line is defined as follows: for each pair of adjacent letters _x_ and _y_ (_x_ immediately precedes _y_) the bonus _c_(_x_,u2009_y_) is added to the result. Your task is to determine what the greatest Euphony can be obtained by changing at most _k_ letters in the name of the Vasya's character. Input The first line contains character's name _s_ and an integer number _k_ (0u2009≤u2009_k_u2009≤u2009100). The length of the nonempty string _s_ does not exceed 100. The second line contains an integer number _n_ (0u2009≤u2009_n_u2009≤u2009676) — amount of pairs of letters, giving bonus to the euphony. The next _n_ lines contain description of these pairs «_x_ _y_ _c_», which means that sequence _xy_ gives bonus _c_ (_x_,u2009_y_ — lowercase Latin letters, u2009-u20091000u2009≤u2009_c_u2009≤u20091000). It is guaranteed that no pair _x_ _y_ mentioned twice in the input data. Output Output the only number — maximum possible euphony оf the new character's name. Examples Input winner 4 4 s e 7 o s 8 l o 13 o o 8 Input abcdef 1 5 a b -10 b c 5 c d 5 d e 5 e f 5 Note In the first example the most euphony name will be _looser_. It is easy to calculate that its euphony is 36.
1,800
false
false
false
true
false
false
false
false
false
false
9,600
1638E
Problem - 1638E - 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 data structures implementation *2400 No tag edit access → Contest materials ") . Add $$$c$$$ $$$x$$$: Add $$$x$$$ to values of all elements $$$a_i$$$ ($$$1 le i le n$$$) of color $$$c$$$ ($$$1 le c le n$$$, $$$-10^9 le x le 10^9$$$). Query $$$i$$$: Print $$$a_i$$$ ($$$1 le i le n$$$). Input The first line of input contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n,q le 10^6$$$)xa0— the length of array $$$a$$$ and the number of queries you have to perform. Each of the next $$$q$$$ lines contains the query given in the form described in the problem statement. Output Print the answers to the queries of the third type on separate lines. Examples Input 5 8 Color 2 4 2 Add 2 2 Query 3 Color 4 5 3 Color 2 2 3 Add 3 3 Query 2 Query 5 Output 2 5 3 Input 2 7 Add 1 7 Query 1 Add 2 4 Query 2 Color 1 1 1 Add 1 1 Query 2 Output 7 7 8 Note The first sample test is explained below. Blue, red and green represent colors $$$1$$$, $$$2$$$ and $$$3$$$ respectively.
2,400
false
false
true
false
true
false
true
false
false
false
2,422
1866C
Pak Chanek has a directed acyclic graph (a directed graph that does not have any cycles) containing $$$N$$$ vertices. Vertex $$$i$$$ has $$$S_i$$$ edges directed away from that vertex. The $$$j$$$-th edge of vertex $$$i$$$ that is directed away from it, is directed towards vertex $$$L_{i,j}$$$ and has an integer $$$W_{i,j}$$$ ($$$0leq W_{i,j}leq1$$$). Another information about the graph is that the graph is shaped in such a way such that each vertex can be reached from vertex $$$1$$$ via zero or more directed edges. Pak Chanek has an array $$$Z$$$ that is initially empty. Pak Chanek defines the function dfs as follows: ``` // dfs from vertex ivoid dfs(int i) { // iterate each edge of vertex i that is directed away from it for(int j = 1; j <= S[i]; j++) { Z.push_back(W[i][j]); // add the integer in the edge to the end of Z dfs(L[i][j]); // recurse to the next vertex }} ``` Note that the function does not keep track of which vertices have been visited, so each vertex can be processed more than once. Let's say Pak Chanek does dfs(1) once. After that, Pak Chanek will get an array $$$Z$$$ containing some elements $$$0$$$ or $$$1$$$. Define an inversion in array $$$Z$$$ as a pair of indices $$$(x, y)$$$ ($$$x < y$$$) such that $$$Z_x > Z_y$$$. How many different inversions in $$$Z$$$ are there if Pak Chanek does dfs(1) once? Since the answer can be very big, output the answer modulo $$$998,244,353$$$. Input The first line contains a single integer $$$N$$$ ($$$2 leq N leq 10^5$$$) — the number of vertices in the graph. The following lines contain the description of each vertex from vertex $$$1$$$ to vertex $$$N$$$. The first line of each vertex $$$i$$$ contains a single integer $$$S_i$$$ ($$$0 leq S_i leq N-1$$$) — the number of edges directed away from vertex $$$i$$$. The $$$j$$$-th of the next $$$S_i$$$ lines of each vertex $$$i$$$ contains two integers $$$L_{i,j}$$$ and $$$W_{i,j}$$$ ($$$1 leq L_{i,j} leq N$$$; $$$0 leq W_{i,j} leq 1$$$) — an edge directed away from vertex $$$i$$$ that is directed towards vertex $$$L_{i,j}$$$ and has an integer $$$W_{i,j}$$$. For each $$$i$$$, the values of $$$L_{i,1}$$$, $$$L_{i,2}$$$, ..., $$$L_{i,S_i}$$$ are pairwise distinct. It is guaranteed that the sum of $$$S_i$$$ over all vertices does not exceed $$$2 cdot 10^5$$$. There are no cycles in the graph. Each vertex can be reached from vertex $$$1$$$ via zero or more directed edges. Output An integer representing the number of inversions in $$$Z$$$ if Pak Chanek does dfs(1) once. Since the answer can be very big, output the answer modulo $$$998,244,353$$$. Example Input 5 2 4 0 3 1 0 1 2 0 2 3 1 5 1 0 Note The following is the dfs(1) process on the graph. In the end, $$$Z=[0,1,0,1,1,0]$$$. All of its inversions are $$$(2,3)$$$, $$$(2,6)$$$, $$$(4,6)$$$, and $$$(5,6)$$$.
1,900
false
false
false
true
false
false
false
false
false
true
1,086
622B
Problem - 622B - Codeforces =============== xa0 ]( "Educational Codeforces Round 7") . The hours and the minutes are given with two digits (the hours or the minutes less than 10 are given with the leading zeroes). The second line contains integer _a_ (0u2009≤u2009_a_u2009≤u2009104) — the number of the minutes passed. Output The only line should contain the time after _a_ minutes in the format described in the input. Note that you should print exactly two digits for the hours and the minutes (add leading zeroes to the numbers if needed). See the examples to check the input/output format. Examples Input 23:59 10 Output 00:09 Input 20:20 121 Output 22:21 Input 10:10 0 Output 10:10
900
false
false
true
false
false
false
false
false
false
false
7,345
1857C
Sasha has an array $$$a$$$ of $$$n$$$ integers. He got bored and for all $$$i$$$, $$$j$$$ ($$$i < j$$$), he wrote down the minimum value of $$$a_i$$$ and $$$a_j$$$. He obtained a new array $$$b$$$ of size $$$frac{ncdot (n-1)}{2}$$$. For example, if $$$a=$$$ [$$$2,3,5,1$$$], he would write [$$$min(2, 3), min(2, 5), min(2, 1), min(3, 5), min(3, 1), min(5, 1)$$$] $$$=$$$ [$$$2, 2, 1, 3, 1, 1$$$]. Then, he randomly shuffled all the elements of the array $$$b$$$. Unfortunately, he forgot the array $$$a$$$, and your task is to restore any possible array $$$a$$$ from which the array $$$b$$$ could have been obtained. The elements of array $$$a$$$ should be in the range $$$[-10^9,10^9]$$$. Input The first line contains a single integer $$$t$$$ ($$$1le tle 200$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2le nle 10^3$$$)xa0— the length of array $$$a$$$. The second line of each test case contains $$$frac{ncdot (n-1)}{2}$$$ integers $$$b_1,b_2,dots,b_{frac{ncdot (n-1)}{2}}$$$ ($$$−10^9le b_ile 10^9$$$)xa0— the elements of array $$$b$$$. It is guaranteed that the sum of $$$n$$$ over all tests does not exceed $$$10^3$$$ and for each array $$$b$$$ in the test, there exists an original array. Output For each test case, output any possible array $$$a$$$ of length $$$n$$$. Example Input 5 3 1 3 1 2 10 4 7 5 3 5 3 3 5 2 2 2 2 2 2 2 2 2 2 5 3 0 0 -2 0 -2 0 0 -2 -2 Output 1 3 3 10 10 7 5 3 12 2 2 2 2 2 0 -2 0 3 5 Note In the first sample, Sasha chose the array $$$[1,3,3]$$$, then the array $$$b$$$ will look like $$$[min(a_1,a_2)=1, min(a_1,a_3)=1, min(a_2,a_3)=3]$$$, after shuffling its elements, the array can look like $$$[1,3,1]$$$. In the second sample, there is only one pair, so the array $$$[10,10]$$$ is suitable. Another suitable array could be $$$[15,10]$$$.
1,200
false
true
false
false
false
false
false
false
true
false
1,142
1697D
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: [ jury has chosen a string $$$s$$$ consisting of $$$n$$$ characters; each character of $$$s$$$ is a lowercase Latin letter. Your task is to guess this string; initially, you know only its length. You may ask queries of two types: $$$1$$$ $$$i$$$ — the query of the first type, where $$$i$$$ is an integer from $$$1$$$ to $$$n$$$. In response to this query, the jury will tell you the character $$$s_i$$$; $$$2$$$ $$$l$$$ $$$r$$$ — the query of the second type, where $$$l$$$ and $$$r$$$ are integers such that $$$1 le l le r le n$$$. In response to this query, the jury will tell you the number of different characters among $$$s_l, s_{l+1}, dots, s_r$$$. You are allowed to ask no more than $$$26$$$ queries of the first type, and no more than $$$6000$$$ queries of the second type. Your task is to restore the string $$$s$$$. For each test in this problem, the string $$$s$$$ is fixed beforehand, and will be the same for every submission. Input Initially, the jury program sends one integer $$$n$$$ on a separate line — the size of $$$s$$$ ($$$1 le n le 1000$$$). Output To give the answer, print one line ! s with a line break in the end, where $$$s$$$ should be the string picked by the jury. After that, your program should flush the output and terminate gracefully. Interaction To ask a query, you should send one line containing the query, in one of the following formats: ? 1 i — for a query of the first type ($$$1 le i le n$$$); ? 2 l r — for a query of the second type ($$$1 le l le r le n$$$). Don't forget to flush the output after sending the query line. The answer to your query will be given on a separate line. For a query of the first type, the answer will be the character $$$s_i$$$. For a query of the second type, the answer will be an integer equal to the number of different characters among $$$s_l, s_{l+1}, dots, s_r$$$. You are allowed to ask no more than $$$26$$$ queries of the first type, and no more than $$$6000$$$ queries of the second type. In case you ask too many queries, or the jury program fails to recognize your query format, the answer to your query will be one integer $$$0$$$. After receiving $$$0$$$ as the 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 ? 2 1 5 ? 1 2 ? 2 1 2 ? 1 1 ? 1 3 ? 1 4 ? 2 4 5 ! guess Note Let's analyze the example of interaction. The string chosen by the jury is guess, so initially the jury sends one integer $$$5$$$. 1. the first query is ? 2 1 5, which means "count the number of different characters among $$$s_1, s_2, dots, s_5$$$". The answer to it is $$$4$$$. 2. the second query is ? 1 2, which means "tell which character is $$$s_2$$$". The answer to it is u. 3. the third query is ? 2 1 2, which means "count the number of different characters among $$$s_1$$$ and $$$s_2$$$". The answer to it is $$$2$$$. 4. the fourth query is ? 1 1, which means "tell which character is $$$s_1$$$". The answer to it is g. 5. the fifth query is ? 1 3, which means "tell which character is $$$s_3$$$". The answer to it is e. 6. the sixth query is ? 1 4, which means "tell which character is $$$s_4$$$". The answer to it is s. 7. the seventh query is ? 2 4 5, which means "count the number of different characters among $$$s_4$$$ and $$$s_5$$$". The answer to it is $$$1$$$, so it's possible to deduce that $$$s_4$$$ is the same as $$$s_5$$$. In the end, the answer is submitted as ! guess, and it is deduced correctly.
1,900
false
false
false
false
false
true
false
true
false
false
2,097
1287A
It's a walking tour day in SIS.Winter, so $$$t$$$ groups of students are visiting Torzhok. Streets of Torzhok are so narrow that students have to go in a row one after another. Initially, some students are angry. Let's describe a group of students by a string of capital letters "A" and "P": "A" corresponds to an angry student "P" corresponds to a patient student Such string describes the row from the last to the first student. Every minute every angry student throws a snowball at the next student. Formally, if an angry student corresponds to the character with index $$$i$$$ in the string describing a group then they will throw a snowball at the student that corresponds to the character with index $$$i+1$$$ (students are given from the last to the first student). If the target student was not angry yet, they become angry. Even if the first (the rightmost in the string) student is angry, they don't throw a snowball since there is no one in front of them. Let's look at the first example test. The row initially looks like this: PPAP. Then, after a minute the only single angry student will throw a snowball at the student in front of them, and they also become angry: PPAA. After that, no more students will become angry. Your task is to help SIS.Winter teachers to determine the last moment a student becomes angry for every group. Input The first line contains a single integer $$$t$$$xa0— the number of groups of students ($$$1 le t le 100$$$). The following $$$2t$$$ lines contain descriptions of groups of students. The description of the group starts with an integer $$$k_i$$$ ($$$1 le k_i le 100$$$)xa0— the number of students in the group, followed by a string $$$s_i$$$, consisting of $$$k_i$$$ letters "A" and "P", which describes the $$$i$$$-th group of students. Output For every group output single integerxa0— the last moment a student becomes angry. Examples Input 3 12 APPAPPPAPPPP 3 AAP 3 PPA Note In the first test, after $$$1$$$ minute the state of students becomes PPAA. After that, no new angry students will appear. In the second tets, state of students in the first group is: after $$$1$$$ minutexa0— AAPAAPPAAPPP after $$$2$$$ minutesxa0— AAAAAAPAAAPP after $$$3$$$ minutesxa0— AAAAAAAAAAAP after $$$4$$$ minutes all $$$12$$$ students are angry In the second group after $$$1$$$ minute, all students are angry.
800
false
true
true
false
false
false
false
false
false
false
4,254
212B
We'll call string _s_[_a_,u2009_b_]u2009=u2009_s__a__s__a_u2009+u20091... _s__b_ (1u2009≤u2009_a_u2009≤u2009_b_u2009≤u2009_s_) a substring of string _s_u2009=u2009_s_1_s_2... _s__s_, where _s_ is the length of string _s_. The trace of a non-empty string _t_ is a set of characters that the string consists of. For example, the trace of string "aab" equals {'a', 'b'}. Let's consider an arbitrary string _s_ and the set of its substrings with trace equal to _C_. We will denote the number of substrings from this set that are maximal by inclusion by _r_(_C_,u2009_s_). Substring _s_[_a_,u2009_b_] of length _n_u2009=u2009_b_u2009-u2009_a_u2009+u20091 belonging to some set is called maximal by inclusion, if there is no substring _s_[_x_,u2009_y_] in this set with length greater than _n_, such that 1u2009≤u2009_x_u2009≤u2009_a_u2009≤u2009_b_u2009≤u2009_y_u2009≤u2009_s_. Two substrings of string _s_ are considered different even if they are equal but they are located at different positions of _s_. Polycarpus got a challenging practical task on a stringology exam. He must do the following: given string _s_ and non-empty sets of characters _C_1, _C_2, ..., _C__m_, find _r_(_C__i_,u2009_s_) for each set _C__i_. Help Polycarpus to solve the problem as he really doesn't want to be expelled from the university and go to the army! Input The first line contains a non-empty string _s_ (1u2009≤u2009_s_u2009≤u2009106). The second line contains a single integer _m_ (1u2009≤u2009_m_u2009≤u2009104). Next _m_ lines contain descriptions of sets _C__i_. The _i_-th line contains string _c__i_ such that its trace equals _C__i_. It is guaranteed that all characters of each string _c__i_ are different. Note that _C__i_ are not necessarily different. All given strings consist of lowercase English letters.
2,300
false
false
true
false
false
false
false
false
false
false
8,992
842A
Problem - 842A - 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 two pointers *1200 No tag edit access → Contest materials ") ") ·(_y_u2009-u2009_x_u2009+u20091) potions). Kirill wants to buy a potion which has efficiency _k_. Will he be able to do this? Input First string contains five integer numbers _l_, _r_, _x_, _y_, _k_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009107, 1u2009≤u2009_x_u2009≤u2009_y_u2009≤u2009107, 1u2009≤u2009_k_u2009≤u2009107). Output Print "YES" without quotes if a potion with efficiency exactly _k_ can be bought in the store and "NO" without quotes otherwise. You can output each of the letters in any register. Examples Input 1 10 1 10 1 Output YES Input 1 5 6 10 1 Output NO
1,200
false
false
false
false
false
false
true
false
false
false
6,396
526F
In this problem you will meet the simplified model of game Pudding Monsters. An important process in developing any game is creating levels. A game field in Pudding Monsters is an _n_u2009×u2009_n_ rectangular grid, _n_ of its cells contain monsters and some other cells contain game objects. The gameplay is about moving the monsters around the field. When two monsters are touching each other, they glue together into a single big one (as they are from pudding, remember?). Statistics showed that the most interesting maps appear if initially each row and each column contains exactly one monster and the rest of map specifics is set up by the correct positioning of the other game objects. A technique that's widely used to make the development process more efficient is reusing the available resources. For example, if there is a large _n_u2009×u2009_n_ map, you can choose in it a smaller _k_u2009×u2009_k_ square part, containing exactly _k_ monsters and suggest it as a simplified version of the original map. You wonder how many ways there are to choose in the initial map a _k_u2009×u2009_k_ (1u2009≤u2009_k_u2009≤u2009_n_) square fragment, containing exactly _k_ pudding monsters. Calculate this number. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20093u2009×u2009105) — the size of the initial field. Next _n_ lines contain the coordinates of the cells initially containing monsters. The _i_-th of the next lines contains two numbers _r__i_,u2009_c__i_ (1u2009≤u2009_r__i_,u2009_c__i_u2009≤u2009_n_) — the row number and the column number of the cell that initially contains the _i_-th monster. It is guaranteed that all _r__i_ are distinct numbers and all _c__i_ are distinct numbers. Output Print the number of distinct square fragments of the original field that can form a new map.
3,000
false
false
false
false
true
false
false
false
false
false
7,726
1165A
You are given a huge decimal number consisting of $$$n$$$ digits. It is guaranteed that this number has no leading zeros. Each digit of this number is either 0 or 1. You may perform several (possibly zero) operations with this number. During each operation you are allowed to change any digit of your number; you may change 0 to 1 or 1 to 0. It is possible that after some operation you can obtain a number with leading zeroes, but it does not matter for this problem. You are also given two integers $$$0 le y < x < n$$$. Your task is to calculate the minimum number of operations you should perform to obtain the number that has remainder $$$10^y$$$ modulo $$$10^x$$$. In other words, the obtained number should have remainder $$$10^y$$$ when divided by $$$10^x$$$. Input The first line of the input contains three integers $$$n, x, y$$$ ($$$0 le y < x < n le 2 cdot 10^5$$$) — the length of the number and the integers $$$x$$$ and $$$y$$$, respectively. The second line of the input contains one decimal number consisting of $$$n$$$ digits, each digit of this number is either 0 or 1. It is guaranteed that the first digit of the number is 1. Output Print one integer — the minimum number of operations you should perform to obtain the number having remainder $$$10^y$$$ modulo $$$10^x$$$. In other words, the obtained number should have remainder $$$10^y$$$ when divided by $$$10^x$$$. Note In the first example the number will be $$$11010100100$$$ after performing one operation. It has remainder $$$100$$$ modulo $$$100000$$$. In the second example the number will be $$$11010100010$$$ after performing three operations. It has remainder $$$10$$$ modulo $$$100000$$$.
1,100
true
false
true
false
false
false
false
false
false
false
4,896
144B
The Super Duper Secret Meeting of the Super Duper Secret Military Squad takes place in a Super Duper Secret Place. The place is an infinite plane with introduced Cartesian coordinate system. The meeting table is represented as a rectangle whose sides are parallel to the coordinate axes and whose vertexes are located at the integer points of the plane. At each integer point which belongs to the table perimeter there is a chair in which a general sits. Some points on the plane contain radiators for the generals not to freeze in winter. Each radiator is characterized by the number _r__i_ — the radius of the area this radiator can heat. That is, if the distance between some general and the given radiator is less than or equal to _r__i_, than the general feels comfortable and warm. Here distance is defined as Euclidean distance, so the distance between points (_x_1,u2009_y_1) and (_x_2,u2009_y_2) is Each general who is located outside the radiators' heating area can get sick. Thus, you should bring him a warm blanket. Your task is to count the number of warm blankets you should bring to the Super Duper Secret Place. The generals who are already comfortable do not need a blanket. Also the generals never overheat, ever if they are located in the heating area of several radiators. The radiators can be located at any integer points on the plane, even inside the rectangle (under the table) or on the perimeter (directly under some general). Even in this case their radius does not change. Input The first input line contains coordinates of two opposite table corners _x__a_, _y__a_, _x__b_, _y__b_ (_x__a_u2009≠u2009_x__b_,u2009_y__a_u2009≠u2009_y__b_). The second line contains integer _n_ — the number of radiators (1u2009≤u2009_n_u2009≤u2009103). Then _n_ lines contain the heaters' coordinates as "_x__i_ _y__i_ _r__i_", the numbers are separated by spaces. All input data numbers are integers. The absolute value of all coordinates does not exceed 1000, 1u2009≤u2009_r__i_u2009≤u20091000. Several radiators can be located at the same point. Note In the first sample the generals are sitting at points: (2,u20092), (2,u20093), (2,u20094), (2,u20095), (3,u20092), (3,u20095), (4,u20092), (4,u20093), (4,u20094), (4,u20095). Among them, 4 generals are located outside the heating range. They are the generals at points: (2,u20095), (3,u20095), (4,u20094), (4,u20095). In the second sample the generals are sitting at points: (5,u20092), (5,u20093), (6,u20092), (6,u20093). All of them are located inside the heating range.
1,300
false
false
true
false
false
false
false
false
false
false
9,299
1313B
Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be $$$n$$$ participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules. Suppose in the first round participant A took $$$x$$$-th place and in the second roundxa0— $$$y$$$-th place. Then the total score of the participant A is sum $$$x + y$$$. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every $$$i$$$ from $$$1$$$ to $$$n$$$ exactly one participant took $$$i$$$-th place in first round and exactly one participant took $$$i$$$-th place in second round. Right after the end of the Olympiad, Nikolay was informed that he got $$$x$$$-th place in first round and $$$y$$$-th place in the second round. Nikolay doesn't know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question. Input The first line contains an integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases to solve. Each of the following $$$t$$$ lines contains integers $$$n$$$, $$$x$$$, $$$y$$$ ($$$1 leq n leq 10^9$$$, $$$1 le x, y le n$$$)xa0— the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round. Output Print two integersxa0— the minimum and maximum possible overall place Nikolay could take. Note Explanation for the first example: Suppose there were 5 participants A-E. Let's denote Nikolay as A. The the most favorable results for Nikolay could look as follows: However, the results of the Olympiad could also look like this: In the first case Nikolay would have taken first place, and in the secondxa0— third place.
1,700
true
true
true
false
false
true
false
false
false
false
4,129
120J
You are given a set of _n_ vectors on a plane. For each vector you are allowed to multiply any of its coordinates by -1. Thus, each vector _v__i_u2009=u2009(_x__i_,u2009_y__i_) can be transformed into one of the following four vectors: _v__i_1u2009=u2009(_x__i_,u2009_y__i_), _v__i_2u2009=u2009(u2009-u2009_x__i_,u2009_y__i_), _v__i_3u2009=u2009(_x__i_,u2009u2009-u2009_y__i_), _v__i_4u2009=u2009(u2009-u2009_x__i_,u2009u2009-u2009_y__i_). You should find two vectors from the set and determine which of their coordinates should be multiplied by -1 so that the absolute value of the sum of the resulting vectors was minimally possible. More formally, you should choose two vectors _v__i_, _v__j_ (1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_,u2009_i_u2009≠u2009_j_) and two numbers _k_1, _k_2 (1u2009≤u2009_k_1,u2009_k_2u2009≤u20094), so that the value of the expression _v__i__k_1u2009+u2009_v__j__k_2 were minimum. Input The first line contains a single integer _n_ (2u2009≤u2009_n_u2009≤u2009105). Then _n_ lines contain vectors as pairs of integers "_x__i_ _y__i_" (u2009-u200910000u2009≤u2009_x__i_,u2009_y__i_u2009≤u200910000), one pair per line. Output Print on the first line four space-separated numbers "_i_ _k_1 _j_ _k_2" — the answer to the problem. If there are several variants the absolute value of whose sums is minimum, you can print any of them. Examples Input 5 -7 -3 9 0 -8 6 7 -8 4 -5 Note A sum of two vectors _v_u2009=u2009(_x__v_,u2009_y__v_) and _u_u2009=u2009(_x__u_,u2009_y__u_) is vector _s_u2009=u2009_v_u2009+u2009_u_u2009=u2009(_x__v_u2009+u2009_x__u_,u2009_y__v_u2009+u2009_y__u_). An absolute value of vector _v_u2009=u2009(_x_,u2009_y_) is number . In the second sample there are several valid answers, such as: (3 1 4 2), (3 1 4 4), (3 4 4 1), (3 4 4 3), (4 1 3 2), (4 1 3 4), (4 2 3 1).
1,900
false
false
false
false
false
false
false
false
true
false
9,397
1486D
Problem - 1486D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search data structures dp *2100 No tag edit access → Contest materials ") Editorial") = 2$$$, $$$median(
2,100
false
false
false
true
true
false
false
true
false
false
3,249
1909I
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Pinely Round 3 (Div. 1 + Div. 2) Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp fft math *1900 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST I. Short Permutation Problem time limit per test7 seconds memory limit per test1024 megabytes Xomu - Last Dance ⠀ You are given an integer $$$n$$$. For each $$$(m, k)$$$ such that $$$3 leq m leq n+1$$$ and $$$0 leq k leq n-1$$$, count the permutations of $$$[1, 2, ..., n]$$$ such that $$$p_i + p_{i+1} geq m$$$ for exactly $$$k$$$ indices $$$i$$$, modulo $$$998,244,353$$$. Input The input consists of a single line, which contains two integers $$$n$$$, $$$x$$$ ($$$2 leq n leq 4000$$$, $$$1 leq x < 1,000,000,007$$$). Output Let $$$a_{m,k}$$$ be the answer for the pair $$$(m, k)$$$, modulo $$$998,244,353$$$. Let $$$$$$large S = sum_{m=3}^{n+1} sum_{k=0}^{n-1} a_{m,k}x^{mn+k}phantom{0}.$$$$$$ Output a single line with an integer: $$$S$$$ modulo $$$1,000,000,007$$$. Note that using two different modulos is intentional. We want you to calculate all the $$$a_{m,k}$$$ modulo $$$998,244,353$$$, then treat them like integers in the range $$$[0, 998,244,352]$$$, and hash them modulo $$$1,000,000,007$$$. Examples input 3 2 output 77824 input 4 1000000000 output 30984329 input 8 327869541 output 85039220 input 4000 1149333 output 584870166 Note In the first test case, the answers for all $$$(m, k)$$$ are shown in the following table: $$$k = 0$$$ $$$k = 1$$$ $$$k = 2$$$ $$$m = 3$$$ $$$0$$$ $$$0$$$ $$$6$$$ $$$m = 4$$$ $$$0$$$ $$$4$$$ $$$2$$$ The answer for $$$(m, k) = (3, 2)$$$ is $$$6$$$, because for every permutation of length $$$3$$$, $$$a_i + a_{i+1} geq 3$$$ exactly $$$2$$$ times. The answer for $$$(m, k) = (4, 2)$$$ is $$$2$$$. In fact, there are $$$2$$$ permutations of length $$$3$$$ such that $$$a_i + a_{i+1} geq 4$$$ exactly $$$2$$$ times: $$$[1, 3, 2]$$$, $$$[2, 3, 1]$$$. Therefore, the value to print is $$$2^9 cdot 0 + 2^{10} cdot 0 + 2^{11} cdot 6 + 2^{12} cdot 0 + 2^{13} cdot 4 + 2^{14} cdot 2 equiv 77,824 phantom{0} ( ext{mod} phantom{0} 1,000,000,007)$$$. In the second test case, the answers for all $$$(m, k)$$$ are shown in the following table: $$$k = 0$$$ $$$k = 1$$$ $$$k = 2$$$ $$$k = 3$$$ $$$m = 3$$$ $$$0$$$ $$$0$$$ $$$0$$$ $$$24$$$ $$$m = 4$$$ $$$0$$$ $$$0$$$ $$$12$$$ $$$12$$$ $$$m = 5$$$ $$$0$$$ $$$4$$$ $$$16$$$ $$$4$$$ The answer for $$$(m, k) = (5, 1)$$$ is $$$4$$$. In fact, there are $$$4$$$ permutations of length $$$4$$$ such that $$$a_i + a_{i+1} geq 5$$$ exactly $$$1$$$ time: $$$[2, 1, 3, 4]$$$, $$$[3, 1, 2, 4]$$$, $$$[4, 2, 1, 3]$$$, $$$[4, 3, 1, 2]$$$. In the third test case, the answers for all $$$(m, k)$$$ are shown in the following table: $$$k = 0$$$ $$$k = 1$$$ $$$k = 2$$$ $$$k = 3$$$ $$$k = 4$$$ $$$k = 5$$$ $$$k = 6$$$ $$$k = 7$$$ $$$m = 3$$$ $$$0$$$ $$$0$$$ $$$0$$$ $$$0$$$ $$$0$$$ $$$0$$$ $$$0$$$ $$$40320$$$ $$$m = 4$$$ $$$0$$$ $$$0$$$ $$$0$$$ $$$0$$$ $$$0$$$ $$$0$$$ $$$10080$$$ $$$30240$$$ $$$m = 5$$$ $$$0$$$ $$$0$$$ $$$0$$$ $$$0$$$ $$$0$$$ $$$1440$$$ $$$17280$$$ $$$21600$$$ $$$m = 6$$$ $$$0$$$ $$$0$$$ $$$0$$$ $$$0$$$ $$$480$$$ $$$8640$$$ $$$21600$$$ $$$9600$$$ $$$m = 7$$$ $$$0$$$ $$$0$$$ $$$0$$$ $$$96$$$ $$$3456$$$ $$$16416$$$ $$$16896$$$ $$$3456$$$ $$$m = 8$$$ $$$0$$$ $$$0$$$ $$$48$$$ $$$2160$$$ $$$12960$$$ $$$18240$$$ $$$6480$$$ $$$432$$$ $$$m = 9$$$ $$$0$$$ $$$16$$$ $$$1152$$$ $$$9648$$$ $$$18688$$$ $$$9648$$$ $$$1152$$$ $$$16$$$ Codeforces (c)
1,900
true
false
false
true
false
false
false
false
false
false
848
1558B
Note that the memory limit in this problem is lower than in others. You have a vertical strip with $$$n$$$ cells, numbered consecutively from $$$1$$$ to $$$n$$$ from top to bottom. You also have a token that is initially placed in cell $$$n$$$. You will move the token up until it arrives at cell $$$1$$$. Let the token be in cell $$$x > 1$$$ at some moment. One shift of the token can have either of the following kinds: Subtraction: you choose an integer $$$y$$$ between $$$1$$$ and $$$x-1$$$, inclusive, and move the token from cell $$$x$$$ to cell $$$x - y$$$. Floored division: you choose an integer $$$z$$$ between $$$2$$$ and $$$x$$$, inclusive, and move the token from cell $$$x$$$ to cell $$$lfloor frac{x}{z} floor$$$ ($$$x$$$ divided by $$$z$$$ rounded down). Find the number of ways to move the token from cell $$$n$$$ to cell $$$1$$$ using one or more shifts, and print it modulo $$$m$$$. Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding). Input The only line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 4 cdot 10^6$$$; $$$10^8 < m < 10^9$$$; $$$m$$$ is a prime number)xa0— the length of the strip and the modulo. Output Print the number of ways to move the token from cell $$$n$$$ to cell $$$1$$$, modulo $$$m$$$. Note In the first test, there are three ways to move the token from cell $$$3$$$ to cell $$$1$$$ in one shift: using subtraction of $$$y = 2$$$, or using division by $$$z = 2$$$ or $$$z = 3$$$. There are also two ways to move the token from cell $$$3$$$ to cell $$$1$$$ via cell $$$2$$$: first subtract $$$y = 1$$$, and then either subtract $$$y = 1$$$ again or divide by $$$z = 2$$$. Therefore, there are five ways in total.
1,900
true
false
false
true
false
false
true
false
false
false
2,841
1739B
Problem - 1739B - 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 greedy math *1100 No tag edit access → Contest materials xa0— the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 100$$$)xa0— the size of the arrays $$$a$$$ and $$$d$$$. The second line contains $$$n$$$ integers $$$d_1, d_2, dots, d_n$$$ ($$$0 le d_i le 100$$$)xa0— the elements of the array $$$d$$$. It can be shown that there always exists at least one suitable array $$$a$$$ under these constraints. Output For each test case, print the elements of the array $$$a$$$, if there is only one possible array $$$a$$$. Otherwise, print $$$-1$$$. Example Input 3 4 1 0 2 5 3 2 6 3 5 0 0 0 0 0 Output 1 1 3 8 -1 0 0 0 0 0 Note In the second example, there are two suitable arrays: $$$
1,100
true
true
false
false
false
true
false
false
false
false
1,852
1537E1
This is the easy version of the problem. The only difference is the constraints on $$$n$$$ and $$$k$$$. You can make hacks only if all versions of the problem are solved. You have a string $$$s$$$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $$$s:=s+s$$$, where $$$+$$$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $$$k$$$ that can be obtained by doing these operations on string $$$s$$$. A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a e b$$$; In the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. Input The first line contains two integers $$$n$$$, $$$k$$$ ($$$1 leq n, k leq 5000$$$) — the length of the original string $$$s$$$ and the length of the desired string. The second line contains the string $$$s$$$, consisting of $$$n$$$ lowercase English letters. Output Print the lexicographically smallest string of length $$$k$$$ that can be obtained by doing the operations on string $$$s$$$. Note In the first test, it is optimal to make one duplication: "dbcadabc" $$$ o$$$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $$$3$$$ characters, then duplicate the string $$$3$$$ times, then delete the last $$$3$$$ characters to make the string have length $$$k$$$. "abcd" $$$ o$$$ "abc" $$$ o$$$ "ab" $$$ o$$$ "a" $$$ o$$$ "aa" $$$ o$$$ "aaaa" $$$ o$$$ "aaaaaaaa" $$$ o$$$ "aaaaaaa" $$$ o$$$ "aaaaaa" $$$ o$$$ "aaaaa".
1,600
false
true
true
true
false
false
true
true
false
false
2,958
864B
Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string _s_ consisting only of lowercase and uppercase Latin letters. Let _A_ be a set of positions in the string. Let's call it pretty if following conditions are met: letters on positions from _A_ in the string are all distinct and lowercase; there are no uppercase letters in the string which are situated between positions from _A_ (i.e. there is no such _j_ that _s_[_j_] is an uppercase letter, and _a_1u2009<u2009_j_u2009<u2009_a_2 for some _a_1 and _a_2 from _A_). Write a program that will determine the maximum number of elements in a pretty set of positions. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200) — length of string _s_. The second line contains a string _s_ consisting of lowercase and uppercase Latin letters. Output Print maximum number of elements in pretty set of positions for string _s_. Note In the first example the desired positions might be 6 and 8 or 7 and 8. Positions 6 and 7 contain letters 'a', position 8 contains letter 'b'. The pair of positions 1 and 8 is not suitable because there is an uppercase letter 'B' between these position. In the second example desired positions can be 7, 8 and 11. There are other ways to choose pretty set consisting of three elements. In the third example the given string _s_ does not contain any lowercase letters, so the answer is 0.
1,000
false
false
true
false
false
false
true
false
false
false
6,286
1065A
There is a special offer in Vasya's favourite supermarket: if the customer buys $$$a$$$ chocolate bars, he or she may take $$$b$$$ additional bars for free. This special offer can be used any number of times. Vasya currently has $$$s$$$ roubles, and he wants to get as many chocolate bars for free. Each chocolate bar costs $$$c$$$ roubles. Help Vasya to calculate the maximum possible number of chocolate bars he can get! Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of testcases. Each of the next $$$t$$$ lines contains four integers $$$s, a, b, c~(1 le s, a, b, c le 10^9)$$$ — the number of roubles Vasya has, the number of chocolate bars you have to buy to use the special offer, the number of bars you get for free, and the cost of one bar, respectively. Output Print $$$t$$$ lines. $$$i$$$-th line should contain the maximum possible number of chocolate bars Vasya can get in $$$i$$$-th test. Example Input 2 10 3 1 1 1000000000 1 1000000000 1 Output 13 1000000001000000000 Note In the first test of the example Vasya can buy $$$9$$$ bars, get $$$3$$$ for free, buy another bar, and so he will get $$$13$$$ bars. In the second test Vasya buys $$$1000000000$$$ bars and gets $$$1000000000000000000$$$ for free. So he has $$$1000000001000000000$$$ bars.
800
true
false
true
false
false
false
false
false
false
false
5,388
765A
There are literally dozens of snooker competitions held each year, and team Jinotega tries to attend them all (for some reason they prefer name "snookah")! When a competition takes place somewhere far from their hometown, Ivan, Artsem and Konstantin take a flight to the contest and back. Jinotega's best friends, team Base have found a list of their itinerary receipts with information about departure and arrival airports. Now they wonder, where is Jinotega now: at home or at some competition far away? They know that: this list contains all Jinotega's flights in this year (in arbitrary order), Jinotega has only flown from his hometown to a snooker contest and back, after each competition Jinotega flies back home (though they may attend a competition in one place several times), and finally, at the beginning of the year Jinotega was at home. Please help them to determine Jinotega's location! Input In the first line of input there is a single integer _n_: the number of Jinotega's flights (1u2009≤u2009_n_u2009≤u2009100). In the second line there is a string of 3 capital Latin letters: the name of Jinotega's home airport. In the next _n_ lines there is flight information, one flight per line, in form "XXX->YYY", where "XXX" is the name of departure airport "YYY" is the name of arrival airport. Exactly one of these airports is Jinotega's home airport. It is guaranteed that flights information is consistent with the knowledge of Jinotega's friends, which is described in the main part of the statement. Output If Jinotega is now at home, print "home" (without quotes), otherwise print "contest". Examples Input 4 SVO SVO->CDG LHR->SVO SVO->LHR CDG->SVO Input 3 SVO SVO->HKT HKT->SVO SVO->RAP Note In the first sample Jinotega might first fly from SVO to CDG and back, and then from SVO to LHR and back, so now they should be at home. In the second sample Jinotega must now be at RAP because a flight from RAP back to SVO is not on the list.
900
true
false
true
false
false
false
false
false
false
false
6,730
1852A
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 887 (Div. 1) Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search math number theory *1800 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST A. Ntarsis' Set time limit per test2 seconds memory limit per test256 megabytes Ntarsis has been given a set $$$S$$$, initially containing integers $$$1, 2, 3, ldots, 10^{1000}$$$ in sorted order. Every day, he will remove the $$$a_1$$$-th, $$$a_2$$$-th, $$$ldots$$$, $$$a_n$$$-th smallest numbers in $$$S$$$ simultaneously. What is the smallest element in $$$S$$$ after $$$k$$$ days? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). The description of the test cases follows. The first line of each test case consists of two integers $$$n$$$ and $$$k$$$ ($$$1 leq n,k leq 2 cdot 10^5$$$)xa0— the length of $$$a$$$ and the number of days. The following line of each test case consists of $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the elements of array $$$a$$$. It is guaranteed that: The sum of $$$n$$$ over all test cases won't exceed $$$2 cdot 10^5$$$; The sum of $$$k$$$ over all test cases won't exceed $$$2 cdot 10^5$$$; $$$a_1 < a_2 < cdots < a_n$$$ for all test cases. Output For each test case, print an integer that is the smallest element in $$$S$$$ after $$$k$$$ days. Example input 7 5 1 1 2 4 5 6 5 3 1 3 5 6 7 4 1000 2 3 4 5 9 1434 1 4 7 9 12 15 17 18 20 10 4 1 3 5 7 9 11 13 15 17 19 10 6 1 4 7 10 13 16 19 22 25 28 10 150000 1 3 4 5 10 11 12 13 14 15 output 3 9 1 12874 16 18 1499986 Note For the first test case, each day the $$$1$$$-st, $$$2$$$-nd, $$$4$$$-th, $$$5$$$-th, and $$$6$$$-th smallest elements need to be removed from $$$S$$$. So after the first day, $$$S$$$ will become $$$ equire{cancel}$$$ $$${cancel 1, cancel 2, 3, cancel 4, cancel 5, cancel 6, 7, 8, 9, ldots} = {3, 7, 8, 9, ldots}$$$. The smallest element is $$$3$$$. For the second case, each day the $$$1$$$-st, $$$3$$$-rd, $$$5$$$-th, $$$6$$$-th and $$$7$$$-th smallest elements need to be removed from $$$S$$$. $$$S$$$ will be changed as follows: Day $$$S$$$ before $$$S$$$ after 1 $$${cancel 1, 2, cancel 3, 4, cancel 5, cancel 6, cancel 7, 8, 9, 10, ldots }$$$ $$$ o$$$ $$${2, 4, 8, 9, 10, ldots}$$$ 2 $$${cancel 2, 4, cancel 8, 9, cancel{10}, cancel{11}, cancel{12}, 13, 14, 15, ldots}$$$ $$$ o$$$ $$${4, 9, 13, 14, 15, ldots}$$$ 3 $$${cancel 4, 9, cancel{13}, 14, cancel{15}, cancel{16}, cancel{17}, 18, 19, 20, ldots}$$$ $$$ o$$$ $$${9, 14, 18, 19, 20, ldots}$$$ The smallest element left after $$$k = 3$$$ days is $$$9$$$. Codeforces (c)
1,800
true
false
false
false
false
false
false
true
false
false
1,167
587C
Recently Duff has been a soldier in the army. Malek is her commander. Their country, Andarz Gu has _n_ cities (numbered from 1 to _n_) and _n_u2009-u20091 bidirectional roads. Each road connects two different cities. There exist a unique path between any two cities. There are also _m_ people living in Andarz Gu (numbered from 1 to _m_). Each person has and ID number. ID number of _i_u2009-u2009_th_ person is _i_ and he/she lives in city number _c__i_. Note that there may be more than one person in a city, also there may be no people living in the city. Malek loves to order. That's why he asks Duff to answer to _q_ queries. In each query, he gives her numbers _v_,u2009_u_ and _a_. To answer a query: Assume there are _x_ people living in the cities lying on the path from city _v_ to city _u_. Assume these people's IDs are _p_1,u2009_p_2,u2009...,u2009_p__x_ in increasing order. If _k_u2009=u2009_min_(_x_,u2009_a_), then Duff should tell Malek numbers _k_,u2009_p_1,u2009_p_2,u2009...,u2009_p__k_ in this order. In the other words, Malek wants to know _a_ minimums on that path (or less, if there are less than _a_ people). Duff is very busy at the moment, so she asked you to help her and answer the queries. Input The first line of input contains three integers, _n_,u2009_m_ and _q_ (1u2009≤u2009_n_,u2009_m_,u2009_q_u2009≤u2009105). The next _n_u2009-u20091 lines contain the roads. Each line contains two integers _v_ and _u_, endpoints of a road (1u2009≤u2009_v_,u2009_u_u2009≤u2009_n_, _v_u2009≠u2009_u_). Next line contains _m_ integers _c_1,u2009_c_2,u2009...,u2009_c__m_ separated by spaces (1u2009≤u2009_c__i_u2009≤u2009_n_ for each 1u2009≤u2009_i_u2009≤u2009_m_). Next _q_ lines contain the queries. Each of them contains three integers, _v_,u2009_u_ and _a_ (1u2009≤u2009_v_,u2009_u_u2009≤u2009_n_ and 1u2009≤u2009_a_u2009≤u200910). Output For each query, print numbers _k_,u2009_p_1,u2009_p_2,u2009...,u2009_p__k_ separated by spaces in one line.
2,200
false
false
false
false
true
false
false
false
false
false
7,495
1146F
You are given a rooted tree with $$$n$$$ nodes, labeled from $$$1$$$ to $$$n$$$. The tree is rooted at node $$$1$$$. The parent of the $$$i$$$-th node is $$$p_i$$$. A leaf is node with no children. For a given set of leaves $$$L$$$, let $$$f(L)$$$ denote the smallest connected subgraph that contains all leaves $$$L$$$. You would like to partition the leaves such that for any two different sets $$$x, y$$$ of the partition, $$$f(x)$$$ and $$$f(y)$$$ are disjoint. Count the number of ways to partition the leaves, modulo $$$998244353$$$. Two ways are different if there are two leaves such that they are in the same set in one way but in different sets in the other. Input The first line contains an integer $$$n$$$ ($$$2 leq n leq 200,000$$$)xa0— the number of nodes in the tree. The next line contains $$$n-1$$$ integers $$$p_2, p_3, ldots, p_n$$$ ($$$1 leq p_i < i$$$). Output Print a single integer, the number of ways to partition the leaves, modulo $$$998244353$$$. Examples Input 10 1 2 3 4 5 6 7 8 9 Note In the first example, the leaf nodes are $$$2,3,4,5$$$. The ways to partition the leaves are in the following image In the second example, the only leaf is node $$$10$$$ so there is only one partition. Note that node $$$1$$$ is not a leaf.
2,500
false
false
false
true
false
false
false
false
false
false
4,986
1628D1
This is the easy version of the problem. The difference is the constraints on $$$n$$$, $$$m$$$ and $$$t$$$. You can make hacks only if all versions of the problem are solved. Alice and Bob are given the numbers $$$n$$$, $$$m$$$ and $$$k$$$, and play a game as follows: The game has a score that Alice tries to maximize, and Bob tries to minimize. The score is initially $$$0$$$. The game consists of $$$n$$$ turns. Each turn, Alice picks a real number from $$$0$$$ to $$$k$$$ (inclusive) which Bob either adds to or subtracts from the score of the game. But throughout the game, Bob has to choose to add at least $$$m$$$ out of the $$$n$$$ turns. Bob gets to know which number Alice picked before deciding whether to add or subtract the number from the score, and Alice gets to know whether Bob added or subtracted the number for the previous turn before picking the number for the current turn (except on the first turn since there was no previous turn). If Alice and Bob play optimally, what will the final score of the game be? Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. The description of test cases follows. Each test case consists of a single line containing the three integers, $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 le m le n le 2000, 0 le k < 10^9 + 7$$$) — the number of turns, how many of those turns Bob has to add, and the biggest number Alice can choose, respectively. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$. Output For each test case output a single integer number — the score of the optimal game modulo $$$10^9 + 7$$$. Formally, let $$$M = 10^9 + 7$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output the integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 le x < M$$$ and $$$x cdot q equiv p pmod{M}$$$. Example Input 7 3 3 2 2 1 10 6 3 10 6 4 10 100 1 1 4 4 0 69 4 20 Output 6 5 375000012 500000026 958557139 0 49735962 Note In the first test case, the entire game has $$$3$$$ turns, and since $$$m = 3$$$, Bob has to add in each of them. Therefore Alice should pick the biggest number she can, which is $$$k = 2$$$, every turn. In the third test case, Alice has a strategy to guarantee a score of $$$frac{75}{8} equiv 375000012 pmod{10^9 + 7}$$$. In the fourth test case, Alice has a strategy to guarantee a score of $$$frac{45}{2} equiv 500000026 pmod{10^9 + 7}$$$.
2,100
true
false
false
true
false
false
false
false
false
false
2,472
248B
Problem - 248B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math number theory *1400 No tag edit access → Contest materials . Help him with that. A number's length is the number of digits in its decimal representation without leading zeros. Input A single input line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009105). Output Print a single integer — the answer to the problem without leading zeroes, or "-1" (without the quotes), if the number that meet the problem condition does not exist. Examples Input 1 Output -1 Input 5 Output 10080
1,400
true
false
false
false
false
false
false
false
false
false
8,842
1044D
There is an array $$$a$$$ of $$$2^{30}$$$ integers, indexed from $$$0$$$ to $$$2^{30}-1$$$. Initially, you know that $$$0 leq a_i < 2^{30}$$$ ($$$0 leq i < 2^{30}$$$), but you do not know any of the values. Your task is to process queries of two types: 1 l r x: You are informed that the bitwise xor of the subarray $$$[l, r]$$$ (ends inclusive) is equal to $$$x$$$. That is, $$$a_l oplus a_{l+1} oplus ldots oplus a_{r-1} oplus a_r = x$$$, where $$$oplus$$$ is the bitwise xor operator. In some cases, the received update contradicts past updates. In this case, you should ignore the contradicting update (the current update). 2 l r: You are asked to output the bitwise xor of the subarray $$$[l, r]$$$ (ends inclusive). If it is still impossible to know this value, considering all past updates, then output $$$-1$$$. Note that the queries are encoded. That is, you need to write an online solution. Input The first line contains a single integer $$$q$$$ ($$$1 leq q leq 2 cdot 10^5$$$)xa0— the number of queries. Each of the next $$$q$$$ lines describes a query. It contains one integer $$$t$$$ ($$$1 leq t leq 2$$$)xa0— the type of query. The given queries will be encoded in the following way: let $$$last$$$ be the answer to the last query of the second type that you have answered (initially, $$$last = 0$$$). If the last answer was $$$-1$$$, set $$$last = 1$$$. If $$$t = 1$$$, three integers follow, $$$l'$$$, $$$r'$$$, and $$$x'$$$ ($$$0 leq l', r', x' < 2^{30}$$$), meaning that you got an update. First, do the following: $$$l = l' oplus last$$$, $$$r = r' oplus last$$$, $$$x = x' oplus last$$$ and, if $$$l > r$$$, swap $$$l$$$ and $$$r$$$. This means you got an update that the bitwise xor of the subarray $$$[l, r]$$$ is equal to $$$x$$$ (notice that you need to ignore updates that contradict previous updates). If $$$t = 2$$$, two integers follow, $$$l'$$$ and $$$r'$$$ ($$$0 leq l', r' < 2^{30}$$$), meaning that you got a query. First, do the following: $$$l = l' oplus last$$$, $$$r = r' oplus last$$$ and, if $$$l > r$$$, swap $$$l$$$ and $$$r$$$. For the given query, you need to print the bitwise xor of the subarray $$$[l, r]$$$. If it is impossible to know, print $$$-1$$$. Don't forget to change the value of $$$last$$$. It is guaranteed there will be at least one query of the second type. Output After every query of the second type, output the bitwise xor of the given subarray or $$$-1$$$ if it is still impossible to know. Examples Input 12 2 1 2 2 1 1073741822 1 0 3 4 2 0 0 2 3 3 2 0 3 1 6 7 3 2 4 4 1 0 2 1 2 0 0 2 4 4 2 0 0 Input 4 1 5 5 9 1 6 6 5 1 6 5 10 2 6 5 Note In the first example, the real queries (without being encoded) are: 12 2 1 2 2 0 1073741823 1 1 2 5 2 1 1 2 2 2 2 1 2 1 2 3 6 2 1 1 1 1 3 0 2 1 1 2 2 2 2 3 3 The answers for the first two queries are $$$-1$$$ because we don't have any such information on the array initially. The first update tells us $$$a_1 oplus a_2 = 5$$$. Note that we still can't be certain about the values $$$a_1$$$ or $$$a_2$$$ independently (for example, it could be that $$$a_1 = 1, a_2 = 4$$$, and also $$$a_1 = 3, a_2 = 6$$$). After we receive all three updates, we have enough information to deduce $$$a_1, a_2, a_3$$$ independently. In the second example, notice that after the first two updates we already know that $$$a_5 oplus a_6 = 12$$$, so the third update is contradicting, and we ignore it.
2,400
false
false
false
false
true
false
false
false
false
false
5,473
1574D
Ivan is playing yet another roguelike computer game. He controls a single hero in the game. The hero has $$$n$$$ equipment slots. There is a list of $$$c_i$$$ items for the $$$i$$$-th slot, the $$$j$$$-th of them increases the hero strength by $$$a_{i,j}$$$. The items for each slot are pairwise distinct and are listed in the increasing order of their strength increase. So, $$$a_{i,1} < a_{i,2} < dots < a_{i,c_i}$$$. For each slot Ivan chooses exactly one item. Let the chosen item for the $$$i$$$-th slot be the $$$b_i$$$-th item in the corresponding list. The sequence of choices $$$[b_1, b_2, dots, b_n]$$$ is called a build. The strength of a build is the sum of the strength increases of the items in it. Some builds are banned from the game. There is a list of $$$m$$$ pairwise distinct banned builds. It's guaranteed that there's at least one build that's not banned. What is the build with the maximum strength that is not banned from the game? If there are multiple builds with maximum strength, print any of them. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10$$$)xa0— the number of equipment slots. The $$$i$$$-th of the next $$$n$$$ lines contains the description of the items for the $$$i$$$-th slot. First, one integer $$$c_i$$$ ($$$1 le c_i le 2 cdot 10^5$$$)xa0— the number of items for the $$$i$$$-th slot. Then $$$c_i$$$ integers $$$a_{i,1}, a_{i,2}, dots, a_{i,c_i}$$$ ($$$1 le a_{i,1} < a_{i,2} < dots < a_{i,c_i} le 10^8$$$). The sum of $$$c_i$$$ doesn't exceed $$$2 cdot 10^5$$$. The next line contains a single integer $$$m$$$ ($$$0 le m le 10^5$$$)xa0— the number of banned builds. Each of the next $$$m$$$ lines contains a description of a banned buildxa0— a sequence of $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le c_i$$$). The builds are pairwise distinct, and there's at least one build that's not banned. Output Print the build with the maximum strength that is not banned from the game. If there are multiple builds with maximum strength, print any of them. Examples Input 3 3 1 2 3 2 1 5 3 2 4 6 2 3 2 3 3 2 2 Input 3 3 1 2 3 2 1 5 3 2 4 6 2 3 2 3 2 2 3 Input 3 3 1 2 3 2 1 5 3 2 4 6 2 3 2 3 2 2 3 Input 4 1 10 1 4 1 7 1 3 0
2,000
false
true
true
false
true
false
true
true
false
true
2,771
228D
The court wizard Zigzag wants to become a famous mathematician. For that, he needs his own theorem, like the Cauchy theorem, or his sum, like the Minkowski sum. But most of all he wants to have his sequence, like the Fibonacci sequence, and his function, like the Euler's totient function. The Zigag's sequence with the zigzag factor z is an infinite sequence _S__i__z_ (_i_u2009≥u20091;xa0_z_u2009≥u20092), that is determined as follows: Operation means taking the remainder from dividing number _x_ by number _y_. For example, the beginning of sequence _S__i_3 (zigzag factor 3) looks as follows: 1, 2, 3, 2, 1, 2, 3, 2, 1. Let's assume that we are given an array _a_, consisting of _n_ integers. Let's define element number _i_ (1u2009≤u2009_i_u2009≤u2009_n_) of the array as _a__i_. The Zigzag function is function , where _l_,u2009_r_,u2009_z_ satisfy the inequalities 1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_, _z_u2009≥u20092. To become better acquainted with the Zigzag sequence and the Zigzag function, the wizard offers you to implement the following operations on the given array _a_. 1. The assignment operation. The operation parameters are (_p_,u2009_v_). The operation denotes assigning value _v_ to the _p_-th array element. After the operation is applied, the value of the array element _a__p_ equals _v_. 2. The Zigzag operation. The operation parameters are (_l_,u2009_r_,u2009_z_). The operation denotes calculating the Zigzag function _Z_(_l_,u2009_r_,u2009_z_). Explore the magical powers of zigzags, implement the described operations. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — The number of elements in array _a_. The second line contains _n_ space-separated integers: _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) — the elements of the array. The third line contains integer _m_ (1u2009≤u2009_m_u2009≤u2009105) — the number of operations. Next _m_ lines contain the operations' descriptions. An operation's description starts with integer _t__i_ (1u2009≤u2009_t__i_u2009≤u20092) — the operation type. If _t__i_u2009=u20091 (assignment operation), then on the line follow two space-separated integers: _p__i_,u2009_v__i_ (1u2009≤u2009_p__i_u2009≤u2009_n_;xa01u2009≤u2009_v__i_u2009≤u2009109) — the parameters of the assigning operation. If _t__i_u2009=u20092 (Zigzag operation), then on the line follow three space-separated integers: _l__i_,u2009_r__i_,u2009_z__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_;xa02u2009≤u2009_z__i_u2009≤u20096) — the parameters of the Zigzag operation. You should execute the operations in the order, in which they are given in the input. Output For each Zigzag operation print the calculated value of the Zigzag function on a single line. Print the values for Zigzag functions in the order, in which they are given in the input. 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.
2,100
false
false
false
false
true
false
false
false
false
false
8,925
1635C
You are given an array $$$a$$$ of $$$n$$$ elements. Your can perform the following operation no more than $$$n$$$ times: Select three indices $$$x,y,z$$$ $$$(1 leq x < y < z leq n)$$$ and replace $$$a_x$$$ with $$$a_y - a_z$$$. After the operation, $$$a_x$$$ need to be less than $$$10^{18}$$$. Your goal is to make the resulting array non-decreasing. If there are multiple solutions, you can output any. If it is impossible to achieve, you should report it as well. Input Each test contains multiple test cases. The first line will contain a single integer $$$t$$$ $$$(1 leq t leq 10000)$$$ — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ $$$(3 leq n leq 2 cdot 10^5)$$$ — the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots ,a_n$$$ $$$(-10^9 leq a_i leq 10^9)$$$, the elements of $$$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 $$$-1$$$ in a single line if there is no solution. Otherwise in the first line you should print a single integer $$$m$$$ $$$(0 leq m leq n)$$$ — number of operations you performed. Then the $$$i$$$-th of the following $$$m$$$ lines should contain three integers $$$x,y,z$$$ $$$(1 leq x < y < z leq n)$$$— description of the $$$i$$$-th operation. If there are multiple solutions, you can output any. Note that you don't have to minimize the number of operations in this task. Example Input 3 5 5 -4 2 -1 2 3 4 3 2 3 -3 -2 -1 Note In the first example, the array becomes $$$[-6,-4,2,-1,2]$$$ after the first operation, $$$[-6,-4,-3,-1,2]$$$ after the second operation. In the second example, it is impossible to make the array sorted after any sequence of operations. In the third example, the array is already sorted, so we don't need to perform any operations.
1,200
false
true
false
false
false
true
false
false
false
false
2,438
1742B
Problem - 1742B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy implementation sortings *800 No tag edit access → Contest materials ") xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 100$$$)xa0— the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the elements of the array. Output For each test case, output "YES" (without quotes) if the array satisfies the condition, and "NO" (without quotes) 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 3 4 1 1 1 1 5 8 7 1 3 4 1 5 Output NO YES YES Note In the first test case any rearrangement will keep the array $$$
800
false
true
true
false
false
false
false
false
true
false
1,830
1936A
This is an interactive problem. There is a secret sequence $$$p_0, p_1, ldots, p_{n-1}$$$, which is a permutation of $$${0,1,ldots,n-1}$$$. You need to find any two indices $$$i$$$ and $$$j$$$ such that $$$p_i oplus p_j$$$ is maximized, where $$$oplus$$$ denotes the . Next, the jury calculates $$$x = (p_a mid p_b)$$$ and $$$y = (p_c mid p_d)$$$, where $$$$$$ denotes the such that $$$p_i oplus p_j$$$ is maximum among all such pairs, using at most $$$3n$$$ queries. If there are multiple pairs of indices satisfying the condition, you may output any one of them. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^3$$$). The description of the test cases follows. Interaction The first line of each test case contains one integer $$$n$$$ ($$$2 le n le 10^4$$$). At this moment, the permutation $$$p_0, p_1, ldots, p_{n-1}$$$ is chosen. The interactor in this task is not adaptive. In other words, the sequence $$$p$$$ is fixed in every test case and does not change during the interaction. To ask a query, you need to pick four indices $$$a$$$, $$$b$$$, $$$c$$$, and $$$d$$$ ($$$0 le a,b,c,d < n$$$) and print the line of the following form: "? a b c d" After that, you receive: "<" if $$$(p_a mid p_b) < (p_c mid p_d)$$$; "=" if $$$(p_a mid p_b) = (p_c mid p_d)$$$; ">" if $$$(p_a mid p_b) > (p_c mid p_d)$$$. You can make at most $$$3n$$$ queries of this form. Next, if your program has found a pair of indices $$$i$$$ and $$$j$$$ ($$$0 le i, j < n$$$) such that $$$p_i oplus p_j$$$ is maximized, print the line of the following form: "! i j" Note that this line is not considered a query and is not taken into account when counting the number of queries asked. After this, proceed to the next test case. If you make more than $$$3n$$$ queries during an interaction, your program must terminate immediately, and you will receive the 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 for a test case, do not forget to output the end of line and flush the output. Otherwise, you will get the verdict Idleness Limit Exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$. Hacks To hack, follow the test format below. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^3$$$). The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 le n le 10^4$$$). The second line of each test case contains $$$n$$$ integers $$$p_0,p_1,ldots,p_{n-1}$$$, which represent a permutation of integers from $$$0$$$ to $$$n - 1$$$. The sum of $$$n$$$ over all test cases should not exceed $$$10^4$$$. Example Output ? 0 2 3 1 ? 1 1 2 3 ? 1 2 0 3 ! 3 2 ! 0 1 Note In the first test case, the hidden permutation is $$$p=[0,3,1,2]$$$. For the query "? 0 2 3 1", the jury return "<" because $$$(p_0 mid p_2) = (0 mid 1) =1 < (p_3 mid p_1) = (2 mid 3) = 3$$$. For the query "? 1 1 2 3", the jury return "=" because $$$(p_1 mid p_1) = (3mid 3)= 3 = (p_2 mid p_3) = (1 mid 2)=3$$$. For the query "? 1 2 0 3", the jury return ">" because $$$(p_1 mid p_2) = (3 mid 1) = 3 > (p_0 mid p_3) = (0mid 2)=2$$$. The answer $$$i = 3$$$ and $$$j = 2$$$ is valid: $$$(p_3 oplus p_2) = (2 oplus 1) = 3$$$ is indeed equal to the maximum possible value of $$$p_i oplus p_j$$$. Another valid answer would be $$$i=0$$$ and $$$j=1$$$. As the number of queries does not exceed $$$3n=12$$$, the answer is considered correct. In the second test case, $$$n = 2$$$, so $$$p$$$ is either $$$[0, 1]$$$ or $$$[1, 0]$$$. In any case, $$$p_0 oplus p_1 = 1$$$ is maximum possible.
1,700
true
true
false
false
false
true
false
false
false
false
668
1139A
You are given a string $$$s=s_1s_2dots s_n$$$ of length $$$n$$$, which only contains digits $$$1$$$, $$$2$$$, ..., $$$9$$$. A substring $$$s[l dots r]$$$ of $$$s$$$ is a string $$$s_l s_{l + 1} s_{l + 2} ldots s_r$$$. A substring $$$s[l dots r]$$$ of $$$s$$$ is called even if the number represented by it is even. Find the number of even substrings of $$$s$$$. Note, that even if some substrings are equal as strings, but have different $$$l$$$ and $$$r$$$, they are counted as different substrings. Input The first line contains an integer $$$n$$$ ($$$1 le n le 65000$$$)xa0— the length of the string $$$s$$$. The second line contains a string $$$s$$$ of length $$$n$$$. The string $$$s$$$ consists only of digits $$$1$$$, $$$2$$$, ..., $$$9$$$. Output Print the number of even substrings of $$$s$$$. Note In the first example, the $$$[l, r]$$$ pairs corresponding to even substrings are: $$$s[1 dots 2]$$$ $$$s[2 dots 2]$$$ $$$s[1 dots 4]$$$ $$$s[2 dots 4]$$$ $$$s[3 dots 4]$$$ $$$s[4 dots 4]$$$ In the second example, all $$$10$$$ substrings of $$$s$$$ are even substrings. Note, that while substrings $$$s[1 dots 1]$$$ and $$$s[2 dots 2]$$$ both define the substring "2", they are still counted as different substrings.
800
false
false
true
false
false
false
false
false
false
false
5,034
552C
Vanya has a scales for weighing loads and weights of masses _w_0,u2009_w_1,u2009_w_2,u2009...,u2009_w_100 grams where _w_ is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass _m_ using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass _m_ and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance. Input The first line contains two integers _w_,u2009_m_ (2u2009≤u2009_w_u2009≤u2009109, 1u2009≤u2009_m_u2009≤u2009109) — the number defining the masses of the weights and the mass of the item. Output Print word 'YES' if the item can be weighted and 'NO' if it cannot. Note Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7u2009+u20093u2009=u20099u2009+u20091. Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100. Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input.
1,900
true
true
false
true
false
false
true
false
false
false
7,625
1986G1
This is a simple version of the problem. The only difference is that in this version $$$n leq 10^5$$$ and the sum of $$$n$$$ for all sets of input data does not exceed $$$10^5$$$. You are given a permutation $$$p$$$ of length $$$n$$$. Calculate the number of index pairs $$$1 leq i < j leq n$$$ such that $$$p_i cdot p_j$$$ is divisible by $$$i cdot j$$$ without remainder. A permutation is a sequence of $$$n$$$ integers, where each integer from $$$1$$$ to $$$n$$$ occurs exactly once. For example, $$$[1]$$$, $$$[3,5,2,1,4]$$$, $$$[1,3,2]$$$ are permutations, while $$$[2,3,2]$$$, $$$[4,3,1]$$$, $$$[0]$$$ are not. Input Each test consists of multiple sets of input data. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of sets of input data. Then follows their description. The first line of each set of input data contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$)xa0— the length of the permutation $$$p$$$. The second line of each set of input data contains $$$n$$$ distinct integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 leq p_i leq n$$$)xa0— the permutation $$$p$$$. It is guaranteed that the sum of $$$n$$$ for all sets of input data does not exceed $$$10^5$$$. Output For each set of input data, output the number of index pairs $$$1 leq i < j leq n$$$ such that $$$p_i cdot p_j$$$ is divisible by $$$i cdot j$$$ without remainder. Example Input 6 1 1 2 1 2 3 2 3 1 5 2 4 1 3 5 12 8 9 7 12 1 10 6 3 2 4 11 5 15 1 2 4 6 8 10 12 14 3 9 15 5 7 11 13 Note In the first set of input data, there are no index pairs, as the size of the permutation is $$$1$$$. In the second set of input data, there is one index pair $$$(1, 2)$$$ and it is valid. In the third set of input data, the index pair $$$(1, 2)$$$ is valid. In the fourth set of input data, the index pairs $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 5)$$$ are valid.
2,200
true
false
false
false
true
false
true
true
false
false
359
74A
Let us remind you part of the rules of Codeforces. The given rules slightly simplified, use the problem statement as a formal document. In the beginning of the round the contestants are divided into rooms. Each room contains exactly _n_ participants. During the contest the participants are suggested to solve five problems, _A_, _B_, _C_, _D_ and _E_. For each of these problem, depending on when the given problem was solved and whether it was solved at all, the participants receive some points. Besides, a contestant can perform hacks on other contestants. For each successful hack a contestant earns 100 points, for each unsuccessful hack a contestant loses 50 points. The number of points for every contestant is represented by the sum of points he has received from all his problems, including hacks. You are suggested to determine the leader for some room; the leader is a participant who has maximum points. Input The first line contains an integer _n_, which is the number of contestants in the room (1u2009≤u2009_n_u2009≤u200950). The next _n_ lines contain the participants of a given room. The _i_-th line has the format of "_handle__i_ _plus__i_ _minus__i_ _a__i_ _b__i_ _c__i_ _d__i_ _e__i_" — it is the handle of a contestant, the number of successful hacks, the number of unsuccessful hacks and the number of points he has received from problems _A_, _B_, _C_, _D_, _E_ correspondingly. The handle of each participant consists of Latin letters, digits and underscores and has the length from 1 to 20 characters. There are the following limitations imposed upon the numbers: 0u2009≤u2009_plus__i_,u2009_minus__i_u2009≤u200950; 150u2009≤u2009_a__i_u2009≤u2009500 or _a__i_u2009=u20090, if problem _A_ is not solved; 300u2009≤u2009_b__i_u2009≤u20091000 or _b__i_u2009=u20090, if problem _B_ is not solved; 450u2009≤u2009_c__i_u2009≤u20091500 or _c__i_u2009=u20090, if problem _C_ is not solved; 600u2009≤u2009_d__i_u2009≤u20092000 or _d__i_u2009=u20090, if problem _D_ is not solved; 750u2009≤u2009_e__i_u2009≤u20092500 or _e__i_u2009=u20090, if problem _E_ is not solved. All the numbers are integer. All the participants have different handles. It is guaranteed that there is exactly one leader in the room (i.e. there are no two participants with the maximal number of points). Output Print on the single line the handle of the room leader. Examples Input 5 Petr 3 1 490 920 1000 1200 0 tourist 2 0 490 950 1100 1400 0 Egor 7 0 480 900 950 0 1000 c00lH4x0R 0 10 150 0 0 0 0 some_participant 2 1 450 720 900 0 0 Note The number of points that each participant from the example earns, are as follows: Petr — 3860 tourist — 4140 Egor — 4030 c00lH4x0R — u2009-u2009350 some_participant — 2220 Thus, the leader of the room is tourist.
1,000
false
false
true
false
false
false
false
false
false
false
9,596
333A
Gerald has been selling state secrets at leisure. All the secrets cost the same: _n_ marks. The state which secrets Gerald is selling, has no paper money, only coins. But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on. There are no coins of other denominations. Of course, Gerald likes it when he gets money without the change. And all buyers respect him and try to give the desired sum without change, if possible. But this does not always happen. One day an unlucky buyer came. He did not have the desired sum without change. Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible. What is the maximum number of coins he could get? The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of _n_ marks without change. For each such combination calculate the minimum number of coins that can bring the buyer at least _n_ marks. Among all combinations choose the maximum of the minimum number of coins. This is the number we want. Input The single line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20091017). Please, do not use the %lld specifier to read or write 64 bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Output In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with. Note In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin. In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change. In the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins. The buyer cannot give three coins as he wants to minimize the number of coins that he gives.
1,600
false
true
false
false
false
false
false
false
false
false
8,505