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
729D
Galya is playing one-dimensional Sea Battle on a 1u2009×u2009_n_ grid. In this game _a_ ships are placed on the grid. Each of the ships consists of _b_ consecutive cells. No cell can be part of two ships, however, the ships can touch each other. Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss"). Galya has already made _k_ shots, all of them were misses. Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. It is guaranteed that there is at least one valid ships placement. Input The first line contains four positive integers _n_, _a_, _b_, _k_ (1u2009≤u2009_n_u2009≤u20092·105, 1u2009≤u2009_a_,u2009_b_u2009≤u2009_n_, 0u2009≤u2009_k_u2009≤u2009_n_u2009-u20091)xa0— the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made. The second line contains a string of length _n_, consisting of zeros and ones. If the _i_-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly _k_ ones in this string. Output In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship. In the second line print the cells Galya should shoot at. Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to _n_, starting from the left. If there are multiple answers, you can print any of them. Examples Input 13 3 2 3 1000000010001 Note There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part.
1,700
true
true
false
false
false
true
false
false
false
false
6,891
1051E
Vasya owns three big integers — $$$a, l, r$$$. Let's define a partition of $$$x$$$ such a sequence of strings $$$s_1, s_2, dots, s_k$$$ that $$$s_1 + s_2 + dots + s_k = x$$$, where $$$+$$$ is a concatanation of strings. $$$s_i$$$ is the $$$i$$$-th element of the partition. For example, number $$$12345$$$ has the following partitions: ["1", "2", "3", "4", "5"], ["123", "4", "5"], ["1", "2345"], ["12345"] and lots of others. Let's call some partition of $$$a$$$ beautiful if each of its elements contains no leading zeros. Vasya want to know the number of beautiful partitions of number $$$a$$$, which has each of $$$s_i$$$ satisfy the condition $$$l le s_i le r$$$. Note that the comparison is the integer comparison, not the string one. Help Vasya to count the amount of partitions of number $$$a$$$ such that they match all the given requirements. The result can be rather big, so print it modulo $$$998244353$$$. Input The first line contains a single integer $$$a~(1 le a le 10^{1000000})$$$. The second line contains a single integer $$$l~(0 le l le 10^{1000000})$$$. The third line contains a single integer $$$r~(0 le r le 10^{1000000})$$$. It is guaranteed that $$$l le r$$$. It is also guaranteed that numbers $$$a, l, r$$$ contain no leading zeros. Note In the first test case, there are two good partitions $$$13+5$$$ and $$$1+3+5$$$. In the second test case, there is one good partition $$$1+0+0+0+0$$$.
2,600
false
false
false
true
true
false
false
true
false
false
5,451
1854C
You have a set $$$S$$$ of $$$n$$$ distinct integers between $$$1$$$ and $$$m$$$. Each second you do the following steps: 1. Pick an element $$$x$$$ in $$$S$$$ uniformly at random. 2. Remove $$$x$$$ from $$$S$$$. 3. If $$$x+1 leq m$$$ and $$$x+1$$$ is not in $$$S$$$, add $$$x+1$$$ to $$$S$$$. What is the expected number of seconds until $$$S$$$ is empty? Output the answer modulo $$$1,000,000,007$$$. Formally, let $$$P = 1,000,000,007$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{a}{b}$$$, where $$$a$$$ and $$$b$$$ are integers and $$$b ot equiv 0 pmod{P}$$$. Output the integer equal to $$$a cdot b^{-1} bmod P$$$. In other words, output an integer $$$z$$$ such that $$$0 le z < P$$$ and $$$z cdot b equiv a pmod{P}$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n leq m leq 500$$$)xa0— the number of elements in the set $$$S$$$ and the upper bound on the value of the elements in $$$S$$$. The second line contains $$$n$$$ integers $$$S_1,,S_2,,dots,,S_n$$$ ($$$1 leq S_1 < S_2 < ldots < S_n leq m$$$)xa0— the elements of the set $$$S$$$. Output Output a single integerxa0— the expected number of seconds until $$$S$$$ is empty, modulo $$$1,000,000,007$$$. Note For test 1, here is a list of all the possible scenarios and their probabilities: 1. $$$[1, 3]$$$ (50% chance) $$$ o$$$ $$$[1]$$$ $$$ o$$$ $$$[2]$$$ $$$ o$$$ $$$[3]$$$ $$$ o$$$ $$$[]$$$ 2. $$$[1, 3]$$$ (50% chance) $$$ o$$$ $$$[2, 3]$$$ (50% chance) $$$ o$$$ $$$[2]$$$ $$$ o$$$ $$$[3]$$$ $$$ o$$$ $$$[]$$$ 3. $$$[1, 3]$$$ (50% chance) $$$ o$$$ $$$[2, 3]$$$ (50% chance) $$$ o$$$ $$$[3]$$$ $$$ o$$$ $$$[]$$$ Adding them up, we get $$$frac{1}{2}cdot 4 + frac{1}{4} cdot 4 + frac{1}{4} cdot 3 = frac{15}{4}$$$. We see that $$$750000009 cdot 4 equiv 15 pmod{1,000,000,007}$$$.
2,500
true
false
false
true
false
false
false
false
false
false
1,156
1213G
You are given a weighted tree consisting of $$$n$$$ vertices. Recall that a tree is a connected graph without cycles. Vertices $$$u_i$$$ and $$$v_i$$$ are connected by an edge with weight $$$w_i$$$. You are given $$$m$$$ queries. The $$$i$$$-th query is given as an integer $$$q_i$$$. In this query you need to calculate the number of pairs of vertices $$$(u, v)$$$ ($$$u < v$$$) such that the maximum weight of an edge on a simple path between $$$u$$$ and $$$v$$$ doesn't exceed $$$q_i$$$. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2 cdot 10^5$$$) — the number of vertices in the tree and the number of queries. Each of the next $$$n - 1$$$ lines describes an edge of the tree. Edge $$$i$$$ is denoted by three integers $$$u_i$$$, $$$v_i$$$ and $$$w_i$$$ — the labels of vertices it connects ($$$1 le u_i, v_i le n$$$, $$$u_i e v_i$$$) and the weight of the edge ($$$1 le w_i le 2 cdot 10^5$$$). It is guaranteed that the given edges form a tree. The last line of the input contains $$$m$$$ integers $$$q_1, q_2, dots, q_m$$$ ($$$1 le q_i le 2 cdot 10^5$$$), where $$$q_i$$$ is the maximum weight of an edge in the $$$i$$$-th query. Output Print $$$m$$$ integers — the answers to the queries. The $$$i$$$-th value should be equal to the number of pairs of vertices $$$(u, v)$$$ ($$$u < v$$$) such that the maximum weight of an edge on a simple path between $$$u$$$ and $$$v$$$ doesn't exceed $$$q_i$$$. Queries are numbered from $$$1$$$ to $$$m$$$ in the order of the input. Examples Input 7 5 1 2 1 3 2 3 2 4 1 4 5 2 5 7 4 3 6 2 5 2 3 4 1 Input 3 3 1 2 1 2 3 2 1 3 2 Note The picture shows the tree from the first example:
1,800
false
false
false
false
false
false
false
false
true
true
4,612
681D
Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are _n_ men in Sasha's family, so let's number them with integers from 1 to _n_. Each man has at most one father but may have arbitrary number of sons. Man number _A_ is considered to be the ancestor of the man number _B_ if at least one of the following conditions is satisfied: _A_u2009=u2009_B_; the man number _A_ is the father of the man number _B_; there is a man number _C_, such that the man number _A_ is his ancestor and the man number _C_ is the father of the man number _B_. Of course, if the man number _A_ is an ancestor of the man number _B_ and _A_u2009≠u2009_B_, then the man number _B_ is not an ancestor of the man number _A_. The tradition of the Sasha's family is to give gifts at the Man's Day. Because giving gifts in a normal way is boring, each year the following happens. 1. A list of candidates is prepared, containing some (possibly all) of the _n_ men in some order. 2. Each of the _n_ men decides to give a gift. 3. In order to choose a person to give a gift to, man _A_ looks through the list and picks the first man _B_ in the list, such that _B_ is an ancestor of _A_ and gives him a gift. Note that according to definition it may happen that a person gives a gift to himself. 4. If there is no ancestor of a person in the list, he becomes sad and leaves the celebration without giving a gift to anyone. This year you have decided to help in organizing celebration and asked each of the _n_ men, who do they want to give presents to (this person is chosen only among ancestors). Are you able to make a list of candidates, such that all the wishes will be satisfied if they give gifts according to the process described above? Input In the first line of the input two integers _n_ and _m_ (0u2009≤u2009_m_u2009<u2009_n_u2009≤u2009100u2009000) are givenxa0— the number of the men in the Sasha's family and the number of family relations in it respectively. The next _m_ lines describe family relations: the (_i_u2009+u20091)_th_ line consists of pair of integers _p__i_ and _q__i_ (1u2009≤u2009_p__i_,u2009_q__i_u2009≤u2009_n_, _p__i_u2009≠u2009_q__i_) meaning that the man numbered _p__i_ is the father of the man numbered _q__i_. It is guaranteed that every pair of numbers appears at most once, that among every pair of two different men at least one of them is not an ancestor of another and that every man has at most one father. The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009_n_), _i__th_ of which means that the man numbered _i_ wants to give a gift to the man numbered _a__i_. It is guaranteed that for every 1u2009≤u2009_i_u2009≤u2009_n_ the man numbered _a__i_ is an ancestor of the man numbered _i_. Output Print an integer _k_ (1u2009≤u2009_k_u2009≤u2009_n_)xa0— the number of the men in the list of candidates, in the first line. Print then _k_ pairwise different positive integers not exceeding _n_ — the numbers of the men in the list in an order satisfying every of the men's wishes, one per line. If there are more than one appropriate lists, print any of them. If there is no appropriate list print u2009-u20091 in the only line. Note The first sample explanation: if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (_a_1u2009=u2009_a_3u2009=u20091); if there would be no 2 in the list then the second man wish would not be satisfied (_a_2u2009=u20092); if 1 would stay before 2 in the answer then the second man would have to give his gift to the first man, but he wants to give it to himself (_a_2u2009=u20092). if, at the other hand, the man numbered 2 would stay before the man numbered 1, then the third man would have to give his gift to the second man, but not to the first (_a_3u2009=u20091).
2,000
false
false
false
false
false
true
false
false
false
true
7,100
1679F
Yura is a mathematician, and his cognition of the world is so absolute as if he have been solving formal problems a hundred of trillions of billions of years. This problem is just that! Consider all non-negative integers from the interval $$$[0, 10^{n})$$$. For convenience we complement all numbers with leading zeros in such way that each number from the given interval consists of exactly $$$n$$$ decimal digits. You are given a set of pairs $$$(u_i, v_i)$$$, where $$$u_i$$$ and $$$v_i$$$ are distinct decimal digits from $$$0$$$ to $$$9$$$. Consider a number $$$x$$$ consisting of $$$n$$$ digits. We will enumerate all digits from left to right and denote them as $$$d_1, d_2, ldots, d_n$$$. In one operation you can swap digits $$$d_i$$$ and $$$d_{i + 1}$$$ if and only if there is a pair $$$(u_j, v_j)$$$ in the set such that at least one of the following conditions is satisfied: 1. $$$d_i = u_j$$$ and $$$d_{i + 1} = v_j$$$, 2. $$$d_i = v_j$$$ and $$$d_{i + 1} = u_j$$$. We will call the numbers $$$x$$$ and $$$y$$$, consisting of $$$n$$$ digits, equivalent if the number $$$x$$$ can be transformed into the number $$$y$$$ using some number of operations described above. In particular, every number is considered equivalent to itself. You are given an integer $$$n$$$ and a set of $$$m$$$ pairs of digits $$$(u_i, v_i)$$$. You have to find the maximum integer $$$k$$$ such that there exists a set of integers $$$x_1, x_2, ldots, x_k$$$ ($$$0 le x_i < 10^{n}$$$) such that for each $$$1 le i < j le k$$$ the number $$$x_i$$$ is not equivalent to the number $$$x_j$$$. Input The first line contains an integer $$$n$$$ ($$$1 le n le 50,000$$$)xa0— the number of digits in considered numbers. The second line contains an integer $$$m$$$ ($$$0 le m le 45$$$)xa0— the number of pairs of digits in the set. Each of the following $$$m$$$ lines contains two digits $$$u_i$$$ and $$$v_i$$$, separated with a space ($$$0 le u_i < v_i le 9$$$). It's guaranteed that all described pairs are pairwise distinct. Output Print one integerxa0— the maximum value $$$k$$$ such that there exists a set of integers $$$x_1, x_2, ldots, x_k$$$ ($$$0 le x_i < 10^{n}$$$) such that for each $$$1 le i < j le k$$$ the number $$$x_i$$$ is not equivalent to the number $$$x_j$$$. As the answer can be big enough, print the number $$$k$$$ modulo $$$998,244,353$$$. Note In the first example we can construct a set that contains all integers from $$$0$$$ to $$$9$$$. It's easy to see that there are no two equivalent numbers in the set. In the second example there exists a unique pair of equivalent numbers: $$$01$$$ and $$$10$$$. We can construct a set that contains all integers from $$$0$$$ to $$$99$$$ despite number $$$1$$$.
2,600
true
false
false
true
false
false
false
false
false
false
2,192
1609F
Problem - 1609F - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures divide and conquer meet-in-the-middle two pointers *2800 No tag edit access → Contest materials , the size of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 10^{18}$$$), the contents of array $$$a$$$. Output Output a single number xa0— the total number of segments that passed the check. Examples Input 5 1 2 3 4 5 Output 9 Input 10 0 5 7 3 9 10 1 6 13 7 Output 18
2,800
false
false
false
false
true
false
false
false
false
false
2,603
1067C
Ivan places knights on infinite chessboard. Initially there are $$$n$$$ knights. If there is free cell which is under attack of at least $$$4$$$ knights then he places new knight in this cell. Ivan repeats this until there are no such free cells. One can prove that this process is finite. One can also prove that position in the end does not depend on the order in which new knights are placed. Ivan asked you to find initial placement of exactly $$$n$$$ knights such that in the end there will be at least $$$lfloor frac{n^{2}}{10} floor$$$ knights. Output Print $$$n$$$ lines. Each line should contain $$$2$$$ numbers $$$x_{i}$$$ and $$$y_{i}$$$ ($$$-10^{9} le x_{i}, ,, y_{i} le 10^{9}$$$)xa0— coordinates of $$$i$$$-th knight. For all $$$i e j$$$, $$$(x_{i}, ,, y_{i}) e (x_{j}, ,, y_{j})$$$ should hold. In other words, all knights should be in different cells. It is guaranteed that the solution exists. Note Let's look at second example: Green zeroes are initial knights. Cell $$$(3, ,, 3)$$$ is under attack of $$$4$$$ knights in cells $$$(1, ,, 2)$$$, $$$(2, ,, 1)$$$, $$$(4, ,, 1)$$$ and $$$(5, ,, 2)$$$, therefore Ivan will place a knight in this cell. Cell $$$(4, ,, 5)$$$ is initially attacked by only $$$3$$$ knights in cells $$$(2, ,, 6)$$$, $$$(5, ,, 7)$$$ and $$$(6, ,, 6)$$$. But new knight in cell $$$(3, ,, 3)$$$ also attacks cell $$$(4, ,, 5)$$$, now it is attacked by $$$4$$$ knights and Ivan will place another knight in this cell. There are no more free cells which are attacked by $$$4$$$ or more knights, so the process stops. There are $$$9$$$ knights in the end, which is not less than $$$lfloor frac{7^{2}}{10} floor = 4$$$.
2,600
false
false
false
false
false
true
false
false
false
false
5,373
1692A
You are given four distinct integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$. Timur and three other people are running a marathon. The value $$$a$$$ is the distance that Timur has run and $$$b$$$, $$$c$$$, $$$d$$$ correspond to the distances the other three participants ran. Output the number of participants in front of Timur. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The description of each test case consists of four distinct integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ ($$$0 leq a, b, c, d leq 10^4$$$). Output For each test case, output a single integerxa0— the number of participants in front of Timur. Example Input 4 2 3 4 1 10000 0 1 2 500 600 400 300 0 9999 10000 9998 Note For the first test case, there are $$$2$$$ people in front of Timur, specifically the participants who ran distances of $$$3$$$ and $$$4$$$. The other participant is not in front of Timur because he ran a shorter distance than Timur. For the second test case, no one is in front of Timur, since he ran a distance of $$$10000$$$ while all others ran a distance of $$$0$$$, $$$1$$$, and $$$2$$$ respectively. For the third test case, only the second person is in front of Timur, who ran a total distance of $$$600$$$ while Timur ran a distance of $$$500$$$.
800
false
false
true
false
false
false
false
false
false
false
2,130
36E
Once archaeologists found _m_ mysterious papers, each of which had a pair of integers written on them. Ancient people were known to like writing down the indexes of the roads they walked along, as «_a_ _b_» or «_b_ _a_», where _a_,u2009_b_ are the indexes of two different cities joint by the road . It is also known that the mysterious papers are pages of two travel journals (those days a new journal was written for every new journey). During one journey the traveler could walk along one and the same road several times in one or several directions but in that case he wrote a new entry for each time in his journal. Besides, the archaeologists think that the direction the traveler took on a road had no effect upon the entry: the entry that looks like «_a_ _b_» could refer to the road from _a_ to _b_ as well as to the road from _b_ to _a_. The archaeologists want to put the pages in the right order and reconstruct the two travel paths but unfortunately, they are bad at programming. That’s where you come in. Go help them! Input The first input line contains integer _m_ (1u2009≤u2009_m_u2009≤u200910000). Each of the following _m_ lines describes one paper. Each description consists of two integers _a_,u2009_b_ (1u2009≤u2009_a_,u2009_b_u2009≤u200910000, _a_u2009≠u2009_b_). Output In the first line output the number _L_1. That is the length of the first path, i.e. the amount of papers in its description. In the following line output _L_1 space-separated numbers — the indexes of the papers that describe the first path. In the third and fourth lines output similarly the length of the second path _L_2 and the path itself. Both paths must contain at least one road, i.e. condition _L_1u2009>u20090 and _L_2u2009>u20090 must be met. The papers are numbered from 1 to _m_ according to the order of their appearance in the input file. The numbers should be output in the order in which the traveler passed the corresponding roads. If the answer is not unique, output any. If it’s impossible to find such two paths, output «-1». Don’t forget that each paper should be used exactly once, i.e _L_1u2009+u2009_L_2u2009=u2009_m_.
2,600
false
false
true
false
false
true
false
false
false
true
9,814
241G
Martha — as a professional problemsetter — proposed a problem for a world-class contest. This is the problem statement: Tomorrow is Nadia's birthday, and Bardia (her brother) is assigned to make the balloons ready! There are _n_ balloons (initially empty) that are tied to a straight line on certain positions _x_1,u2009_x_2,u2009...,u2009_x__n_. Bardia inflates the balloons from left to right. As a result, _i_-th balloon gets bigger and bigger until its radius reaches the pressure endurance _p__i_ or it touches another previously-inflated balloon. While Bardia was busy with the balloons, he wondered "What will be the sum of radius of balloons after all of the balloons are inflated?". Being a nerdy type of guy, he is now thinking about the problem instead of preparing his sister's birthday. Calculate the answer to Bardia's problem so that Nadia's birthday won't be balloon-less. Artha — Martha's student — claimed his solution got accepted. Martha (being his teacher for a long time!) knew he couldn't have solved the problem for real and thus thinks there is something wrong with the testcases. Artha isn't anyhow logical, which means there is no way for Martha to explain the wrong point in his algorithm. So, the only way is to find a testcase to prove him wrong! Artha's pseudo-code is shown below: You should output a small testcase for the problem such that Artha's algorithm is incorrect. The algorithm's output is considered correct if it differs from the correct value by no more than 1.
1,900
false
false
false
false
false
true
false
false
false
false
8,869
363C
Problem - 363C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy implementation *1400 No tag edit access → Contest materials . Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos). Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. Input The single line of the input contains word _s_, its length is from 1 to 200000 characters. The given word _s_ consists of lowercase English letters. Output Print such word _t_ that it doesn't contain any typos described in the problem statement and is obtained from _s_ by deleting the least number of letters. If there are multiple solutions, print any of them. Examples Input helloo Output hello Input woooooow Output woow Note The second valid answer to the test from the statement is "heloo".
1,400
false
true
true
false
false
false
false
false
false
false
8,380
2002H
It's been a long summer's day, with the constant chirping of cicadas and the heat which never seemed to end. Finally, it has drawn to a close. The showdown has passed, the gates are open, and only a gentle breeze is left behind. Your predecessors had taken their final bow; it's your turn to take the stage. Sorting through some notes that were left behind, you found a curious statement named Problem 101: Given a positive integer sequence $$$a_1,a_2,ldots,a_n$$$, you can operate on it any number of times. In an operation, you choose three consecutive elements $$$a_i,a_{i+1},a_{i+2}$$$, and merge them into one element $$$max(a_i+1,a_{i+1},a_{i+2}+1)$$$. Please calculate the maximum number of operations you can do without creating an element greater than $$$m$$$. After some thought, you decided to propose the following problem, named Counting 101: Given $$$n$$$ and $$$m$$$. For each $$$k=0,1,ldots,leftlfloorfrac{n-1}{2} ight floor$$$, please find the number of integer sequences $$$a_1,a_2,ldots,a_n$$$ with elements in $$$[1, m]$$$, such that when used as input for Problem 101, the answer is $$$k$$$. As the answer can be very large, please print it modulo $$$10^9+7$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1le tle10^3$$$). The description of the test cases follows. The only line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1le nle 130$$$, $$$1le mle 30$$$). Output For each test case, output $$$leftlfloorfrac{n+1}{2} ight floor$$$ numbers. The $$$i$$$-th number is the number of valid sequences such that when used as input for Problem 101, the answer is $$$i-1$$$, modulo $$$10^9+7$$$. Example Output 6 2 1590121 23399118 382293180 213020758 379696760 Note In the first test case, there are $$$2^3=8$$$ candidate sequences. Among them, you can operate on $$$[1,2,1]$$$ and $$$[1,1,1]$$$ once; you cannot operate on the other $$$6$$$ sequences.
3,500
false
true
false
false
false
false
false
false
false
false
240
1866A
Chaneka, Pak Chanek's child, is an ambitious kid, so Pak Chanek gives her the following problem to test her ambition. Given an array of integers $$$[A_1, A_2, A_3, ldots, A_N]$$$. In one operation, Chaneka can choose one element, then increase or decrease the element's value by $$$1$$$. Chaneka can do that operation multiple times, even for different elements. What is the minimum number of operations that must be done to make it such that $$$A_1 imes A_2 imes A_3 imes ldots imes A_N = 0$$$? Input The first line contains a single integer $$$N$$$ ($$$1 leq N leq 10^5$$$). The second line contains $$$N$$$ integers $$$A_1, A_2, A_3, ldots, A_N$$$ ($$$-10^5 leq A_i leq 10^5$$$). Output An integer representing the minimum number of operations that must be done to make it such that $$$A_1 imes A_2 imes A_3 imes ldots imes A_N = 0$$$. Note In the first example, initially, $$$A_1 imes A_2 imes A_3=2 imes(-6) imes5=-60$$$. Chaneka can do the following sequence of operations: 1. Decrease the value of $$$A_1$$$ by $$$1$$$. Then, $$$A_1 imes A_2 imes A_3=1 imes(-6) imes5=-30$$$ 2. Decrease the value of $$$A_1$$$ by $$$1$$$. Then, $$$A_1 imes A_2 imes A_3=0 imes(-6) imes5=0$$$ In the third example, Chaneka does not have to do any operations, because from the start, it already holds that $$$A_1 imes A_2 imes A_3 imes A_4 imes A_5=0 imes(-1) imes0 imes1 imes0=0$$$
800
true
false
false
false
false
false
false
false
false
false
1,088
1707A
Doremy is asked to test $$$n$$$ contests. Contest $$$i$$$ can only be tested on day $$$i$$$. The difficulty of contest $$$i$$$ is $$$a_i$$$. Initially, Doremy's IQ is $$$q$$$. On day $$$i$$$ Doremy will choose whether to test contest $$$i$$$ or not. She can only test a contest if her current IQ is strictly greater than $$$0$$$. If Doremy chooses to test contest $$$i$$$ on day $$$i$$$, the following happens: if $$$a_i>q$$$, Doremy will feel she is not wise enough, so $$$q$$$ decreases by $$$1$$$; otherwise, nothing changes. If she chooses not to test a contest, nothing changes. Doremy wants to test as many contests as possible. Please give Doremy a solution. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n le 10^5$$$, $$$1 le q le 10^9$$$)xa0— the number of contests and Doremy's IQ in the beginning. The second line contains $$$n$$$ integers $$$a_1,a_2,cdots,a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the difficulty of each contest. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, you need to output a binary string $$$s$$$, where $$$s_i=1$$$ if Doremy should choose to test contest $$$i$$$, and $$$s_i=0$$$ otherwise. The number of ones in the string should be maximum possible, and she should never test a contest when her IQ is zero or less. If there are multiple solutions, you may output any. Example Input 5 1 1 1 2 1 1 2 3 1 1 2 1 4 2 1 4 3 1 5 2 5 1 2 4 3 Output 1 11 110 1110 01111 Note In the first test case, Doremy tests the only contest. Her IQ doesn't decrease. In the second test case, Doremy tests both contests. Her IQ decreases by $$$1$$$ after testing contest $$$2$$$. In the third test case, Doremy tests contest $$$1$$$ and $$$2$$$. Her IQ decreases to $$$0$$$ after testing contest $$$2$$$, so she can't test contest $$$3$$$.
1,600
false
true
true
false
false
true
false
true
false
false
2,034
546A
Problem - 546A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force implementation math *800 No tag edit access → Contest materials ") editorial") . He has _n_ dollars. How many dollars does he have to borrow from his friend soldier to buy _w_ bananas? Input The first line contains three positive integers _k_,u2009_n_,u2009_w_ (1u2009u2009≤u2009u2009_k_,u2009_w_u2009u2009≤u2009u20091000, 0u2009≤u2009_n_u2009≤u2009109), the cost of the first banana, initial number of dollars the soldier has and number of bananas he wants. Output Output one integer — the amount of dollars that the soldier must borrow from his friend. If he doesn't have to borrow money, output 0. Examples Input 3 17 4 Output 13
800
true
false
true
false
false
false
true
false
false
false
7,657
999D
You are given an array consisting of $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$, and a positive integer $$$m$$$. It is guaranteed that $$$m$$$ is a divisor of $$$n$$$. In a single move, you can choose any position $$$i$$$ between $$$1$$$ and $$$n$$$ and increase $$$a_i$$$ by $$$1$$$. Let's calculate $$$c_r$$$ ($$$0 le r le m-1)$$$ — the number of elements having remainder $$$r$$$ when divided by $$$m$$$. In other words, for each remainder, let's find the number of corresponding elements in $$$a$$$ with that remainder. Your task is to change the array in such a way that $$$c_0 = c_1 = dots = c_{m-1} = frac{n}{m}$$$. Find the minimum number of moves to satisfy the above requirement. Input The first line of input contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2 cdot 10^5, 1 le m le n$$$). It is guaranteed that $$$m$$$ is a divisor of $$$n$$$. The second line of input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 10^9$$$), the elements of the array. Output In the first line, print a single integer — the minimum number of moves required to satisfy the following condition: for each remainder from $$$0$$$ to $$$m - 1$$$, the number of elements of the array having this remainder equals $$$frac{n}{m}$$$. In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed $$$10^{18}$$$.
1,900
false
true
true
false
true
false
false
false
false
false
5,708
1797B
Li Hua has a pattern of size $$$n imes n$$$, each cell is either blue or red. He can perform exactly $$$k$$$ operations. In each operation, he chooses a cell and changes its color from red to blue or from blue to red. Each cell can be chosen as many times as he wants. Is it possible to make the pattern, that matches its rotation by $$$180^{circ}$$$? Suppose you were Li Hua, please solve this problem. Input The first line contains the single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n,k$$$ ($$$1le nle 10^3,0le k le 10^9$$$)xa0— the size of the pattern and the number of operations. Each of next $$$n$$$ lines contains $$$n$$$ integers $$$a_{i,j}$$$ ($$$a_{i,j}in{0,1}$$$)xa0— the initial color of the cell, $$$0$$$ for blue and $$$1$$$ for red. It's guaranteed that sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$. Output For each set of input, print "YES" if it's possible to make the pattern, that matches its rotation by $$$180^{circ}$$$ after applying exactly $$$k$$$ of operations, and "NO" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 3 4 0 1 1 1 1 0 0 0 1 1 0 1 0 1 1 1 1 4 3 1 0 1 1 1 0 0 0 0 1 0 1 1 1 0 1 5 4 0 0 0 0 0 0 1 1 1 1 0 1 0 0 0 1 1 1 1 1 0 0 0 0 0 Note In test case 1, you can't perform any operation. The pattern after rotation is on the right. In test case 2, you can perform operations on $$$(2,1),(3,2),(3,4)$$$. The pattern after operations is in the middle and the pattern after rotation is on the right.
1,100
false
true
false
false
false
true
false
false
false
false
1,495
1665A
You are given a positive integer $$$n$$$. You have to find $$$4$$$ positive integers $$$a, b, c, d$$$ such that $$$a + b + c + d = n$$$, and $$$gcd(a, b) = operatorname{lcm}(c, d)$$$. If there are several possible answers you can output any of them. It is possible to show that the answer always exists. In this problem $$$gcd(a, b)$$$ denotes the $$$ denotes the xa0— the number of test cases. Description of the test cases follows. Each test case contains a single line with integer $$$n$$$ ($$$4 le n le 10^9$$$)xa0— the sum of $$$a$$$, $$$b$$$, $$$c$$$, and $$$d$$$. Output For each test case output $$$4$$$ positive integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ such that $$$a + b + c + d = n$$$ and $$$gcd(a, b) = operatorname{lcm}(c, d)$$$. Example Output 1 1 1 1 2 2 2 1 2 2 2 2 2 4 2 1 3 5 1 1 Note In the first test case $$$gcd(1, 1) = operatorname{lcm}(1, 1) = 1$$$, $$$1 + 1 + 1 + 1 = 4$$$. In the second test case $$$gcd(2, 2) = operatorname{lcm}(2, 1) = 2$$$, $$$2 + 2 + 2 + 1 = 7$$$. In the third test case $$$gcd(2, 2) = operatorname{lcm}(2, 2) = 2$$$, $$$2 + 2 + 2 + 2 = 8$$$. In the fourth test case $$$gcd(2, 4) = operatorname{lcm}(2, 1) = 2$$$, $$$2 + 4 + 2 + 1 = 9$$$. In the fifth test case $$$gcd(3, 5) = operatorname{lcm}(1, 1) = 1$$$, $$$3 + 5 + 1 + 1 = 10$$$.
800
true
false
false
false
false
true
false
false
false
false
2,290
1922A
You are given an integer $$$n$$$ and three strings $$$a, b, c$$$, each consisting of $$$n$$$ lowercase Latin letters. Let a template be a string $$$t$$$ consisting of $$$n$$$ lowercase and/or uppercase Latin letters. The string $$$s$$$ matches the template $$$t$$$ if the following conditions hold for all $$$i$$$ from $$$1$$$ to $$$n$$$: if the $$$i$$$-th letter of the template is lowercase, then $$$s_i$$$ must be the same as $$$t_i$$$; if the $$$i$$$-th letter of the template is uppercase, then $$$s_i$$$ must be different from the lowercase version of $$$t_i$$$. For example, if there is a letter 'A' in the template, you cannot use the letter 'a' in the corresponding position of the string. Accordingly, the string doesn't match the template if the condition doesn't hold for at least one $$$i$$$. Determine whether there exists a template $$$t$$$ such that the strings $$$a$$$ and $$$b$$$ match it, while the string $$$c$$$ does not. Input The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 20$$$)xa0— the length of the given strings. The next three lines contain the strings $$$a, b$$$ and $$$c$$$. Each string consists of exactly $$$n$$$ lowercase Latin letters. Output For each testcase, print "YES" if there exists a template $$$t$$$ such that the strings $$$a$$$ and $$$b$$$ match it, while the string $$$c$$$ does not. Otherwise, print "NO". Example Input 4 1 a b c 2 aa bb aa 10 mathforces luckforces adhoccoder 3 acc abd abc Note In the first test case, you can use the template "C". The first letter of strings $$$a$$$ and $$$b$$$ differ from 'c', so they match the template. The first letter of string $$$c$$$ equals 'c', so it doesn't match. In the third test case, you can use the template "CODEforces".
800
false
false
true
false
false
true
false
false
false
false
758
1847A
Kars is tired and resentful of the narrow mindset of his village since they are content with staying where they are and are not trying to become the perfect life form. Being a top-notch inventor, Kars wishes to enhance his body and become the perfect life form. Unfortunately, $$$n$$$ of the villagers have become suspicious of his ideas. The $$$i$$$-th villager has a suspicion of $$$a_i$$$ on him. Individually each villager is scared of Kars, so they form into groups to be more powerful. The power of the group of villagers from $$$l$$$ to $$$r$$$ be defined as $$$f(l,r)$$$ where $$$$$$f(l,r) = a_l - a_{l+1} + a_{l + 1} - a_{l + 2} + ldots + a_{r-1} - a_r.$$$$$$ Here $$$x-y$$$ is the absolute value of $$$x-y$$$. A group with only one villager has a power of $$$0$$$. Kars wants to break the villagers into exactly $$$k$$$ contiguous subgroups so that the sum of their power is minimized. Formally, he must find $$$k - 1$$$ positive integers $$$1 le r_1 < r_2 < ldots < r_{k - 1} < n$$$ such that $$$f(1, r_1) + f(r_1 + 1, r_2) + ldots + f(r_{k-1} + 1, n)$$$ is minimised. Help Kars in finding the minimum value of $$$f(1, r_1) + f(r_1 + 1, r_2) + ldots + f(r_{k-1} + 1, n)$$$. Input The first line contains a single integer $$$t$$$ $$$(1 leq t leq 100)$$$xa0— the number of test cases. The description of test cases follows. The first line of each test case contains two integers $$$n,k$$$ $$$(1 leq k leq n leq 100)$$$xa0— the number of villagers and the number of groups they must be split into. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2, ldots, a_n$$$ $$$(1 leq a_i leq 500)$$$xa0— the suspicion of each of the villagers. Output For each test case, output a single integerxa0— the minimum possible value of sum of power of all the groups i.xa0e. the minimum possible value of $$$f(1,r_1) + f(r_1 + 1, r_2) + ldots + f(r_{k-1} + 1, n)$$$. Example Input 3 4 2 1 3 5 2 6 3 1 9 12 4 7 2 12 8 1 9 8 2 3 3 1 8 7 7 9 2 Note In the first test case, we will group the villagers with suspicion $$$(1,3,5,2)$$$ into $$$(1,3,5)$$$ and $$$(2)$$$. So, $$$f(1,3) + f(4,4) = (1 - 3 + 3 - 5) + 0 = 4 + 0 = 4$$$. In the second test case, we will group the villagers with suspicion $$$(1,9,12,4,7,2)$$$ into $$$(1),(9,12),(4,7,2)$$$. So, $$$f(1,1) + f(2,3) + f(4,6) = 0 + 3 + 8 = 11$$$.
800
false
true
false
false
false
false
false
false
true
false
1,200
1305F
Kuroni is very angry at the other setters for using him as a theme! As a punishment, he forced them to solve the following problem: You have an array $$$a$$$ consisting of $$$n$$$ positive integers. An operation consists of choosing an element and either adding $$$1$$$ to it or subtracting $$$1$$$ from it, such that the element remains positive. We say the array is good if the greatest common divisor of all its elements is not $$$1$$$. Find the minimum number of operations needed to make the array good. Unable to match Kuroni's intellect, the setters failed to solve the problem. Help them escape from Kuroni's punishment! Input The first line contains an integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) xa0— the number of elements in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$. ($$$1 le a_i le 10^{12}$$$) xa0— the elements of the array. Output Print a single integer xa0— the minimum number of operations required to make the array good. Note In the first example, the first array is already good, since the greatest common divisor of all the elements is $$$2$$$. In the second example, we may apply the following operations: 1. Add $$$1$$$ to the second element, making it equal to $$$9$$$. 2. Subtract $$$1$$$ from the third element, making it equal to $$$6$$$. 3. Add $$$1$$$ to the fifth element, making it equal to $$$2$$$. 4. Add $$$1$$$ to the fifth element again, making it equal to $$$3$$$. The greatest common divisor of all elements will then be equal to $$$3$$$, so the array will be good. It can be shown that no sequence of three or less operations can make the array good.
2,500
true
false
false
false
false
false
false
false
false
false
4,159
149D
Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a solution. Today you will face it. You are given string _s_. It represents a correct bracket sequence. A correct bracket sequence is the sequence of opening ("(") and closing (")") brackets, such that it is possible to obtain a correct mathematical expression from it, inserting numbers and operators between the brackets. For example, such sequences as "(())()" and "()" are correct bracket sequences and such sequences as ")()" and "(()" are not. In a correct bracket sequence each bracket corresponds to the matching bracket (an opening bracket corresponds to the matching closing bracket and vice versa). For example, in a bracket sequence shown of the figure below, the third bracket corresponds to the matching sixth one and the fifth bracket corresponds to the fourth one. You are allowed to color some brackets in the bracket sequence so as all three conditions are fulfilled: Each bracket is either not colored any color, or is colored red, or is colored blue. For any pair of matching brackets exactly one of them is colored. In other words, for any bracket the following is true: either it or the matching bracket that corresponds to it is colored. No two neighboring colored brackets have the same color. Find the number of different ways to color the bracket sequence. The ways should meet the above-given conditions. Two ways of coloring are considered different if they differ in the color of at least one bracket. As the result can be quite large, print it modulo 1000000007 (109u2009+u20097). Input The first line contains the single string _s_ (2u2009≤u2009_s_u2009≤u2009700) which represents a correct bracket sequence. Output Print the only number — the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007 (109u2009+u20097). Note Let's consider the first sample test. The bracket sequence from the sample can be colored, for example, as is shown on two figures below. The two ways of coloring shown below are incorrect.
1,900
false
false
false
true
false
false
false
false
false
false
9,278
1033C
After a long day, Alice and Bob decided to play a little game. The game board consists of $$$n$$$ cells in a straight line, numbered from $$$1$$$ to $$$n$$$, where each cell contains a number $$$a_i$$$ between $$$1$$$ and $$$n$$$. Furthermore, no two cells contain the same number. A token is placed in one of the cells. They take alternating turns of moving the token around the board, with Alice moving first. The current player can move from cell $$$i$$$ to cell $$$j$$$ only if the following two conditions are satisfied: the number in the new cell $$$j$$$ must be strictly larger than the number in the old cell $$$i$$$ (i.e. $$$a_j > a_i$$$), and the distance that the token travels during this turn must be a multiple of the number in the old cell (i.e. $$$i-jbmod a_i = 0$$$). Whoever is unable to make a move, loses. For each possible starting position, determine who wins if they both play optimally. It can be shown that the game is always finite, i.e. there always is a winning strategy for one of the players. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the number of numbers. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$). Furthermore, there are no pair of indices $$$i eq j$$$ such that $$$a_i = a_j$$$. Output Print $$$s$$$xa0— a string of $$$n$$$ characters, where the $$$i$$$-th character represents the outcome of the game if the token is initially placed in the cell $$$i$$$. If Alice wins, then $$$s_i$$$ has to be equal to "A"; otherwise, $$$s_i$$$ has to be equal to "B". Examples Input 15 3 11 2 5 10 9 7 13 15 8 4 12 6 1 14 Note In the first sample, if Bob puts the token on the number (not position): $$$1$$$: Alice can move to any number. She can win by picking $$$7$$$, from which Bob has no move. $$$2$$$: Alice can move to $$$3$$$ and $$$5$$$. Upon moving to $$$5$$$, Bob can win by moving to $$$8$$$. If she chooses $$$3$$$ instead, she wins, as Bob has only a move to $$$4$$$, from which Alice can move to $$$8$$$. $$$3$$$: Alice can only move to $$$4$$$, after which Bob wins by moving to $$$8$$$. $$$4$$$, $$$5$$$, or $$$6$$$: Alice wins by moving to $$$8$$$. $$$7$$$, $$$8$$$: Alice has no move, and hence she loses immediately.
1,600
false
false
false
true
false
false
true
false
false
false
5,533
1734F
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 822 (Div. 2) Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags bitmasks divide and conquer dp math *2500 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST F. Zeros and Ones time limit per test3 seconds memory limit per test256 megabytes Let $$$S$$$ be the Thue-Morse sequence. In other words, $$$S$$$ is the $$$0$$$-indexed binary string with infinite length that can be constructed as follows: Initially, let $$$S$$$ be "0". Then, we perform the following operation infinitely many times: concatenate $$$S$$$ with a copy of itself with flipped bits. For example, here are the first four iterations: Iteration $$$S$$$ before iteration $$$S$$$ before iteration with flipped bits Concatenated $$$S$$$ 1 0 1 01 2 01 10 0110 3 0110 1001 01101001 4 01101001 10010110 0110100110010110 $$$ldots$$$ $$$ldots$$$ $$$ldots$$$ $$$ldots$$$ You are given two positive integers $$$n$$$ and $$$m$$$. Find the number of positions where the strings $$$S_0 S_1 ldots S_{m-1}$$$ and $$$S_n S_{n + 1} ldots S_{n + m - 1}$$$ are different. Input Each test contains multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. The description of the test cases follows. The first and only line of each test case contains two positive integers, $$$n$$$ and $$$m$$$ respectively ($$$1 leq n,m leq 10^{18}$$$). Output For each testcase, output a non-negative integerxa0— the Hamming distance between the two required strings. Example input 6 1 1 5 10 34 211 73 34 19124639 56348772 12073412269 96221437021 output 1 6 95 20 28208137 48102976088 Note The string $$$S$$$ is equal to 0110100110010110.... In the first test case, $$$S_0$$$ is "0", and $$$S_1$$$ is "1". The Hamming distance between the two strings is $$$1$$$. In the second test case, $$$S_0 S_1 ldots S_9$$$ is "0110100110", and $$$S_5 S_6 ldots S_{14}$$$ is "0011001011". The Hamming distance between the two strings is $$$6$$$. Codeforces (c)
2,500
true
false
false
true
false
false
false
false
false
false
1,881
727C
This is an interactive problem. You should use flush operation after each printed line. For example, in C++ you should use fflush(stdout), in Java you should use System.out.flush(), and in Pascalxa0— flush(output). In this problem you should guess an array _a_ which is unknown for you. The only information you have initially is the length _n_ of the array _a_. The only allowed action is to ask the sum of two elements by their indices. Formally, you can print two indices _i_ and _j_ (the indices should be distinct). Then your program should read the response: the single integer equals to _a__i_u2009+u2009_a__j_. It is easy to prove that it is always possible to guess the array using at most _n_ requests. Write a program that will guess the array _a_ by making at most _n_ requests. Interaction In each test your program should guess a single array. The input starts with a line containing integer _n_ (3u2009≤u2009_n_u2009≤u20095000)xa0— the length of the array. Your program should read it at first. After that your program should print to the standard output the requests about the sum of two elements or inform that the array is guessed. In case your program is making a request to ask the sum of two elements, it should print line in the format "? i j" (_i_ and _j_ are distinct integers between 1 and _n_), where _i_ and _j_ are indices in the array _a_. In case your program informs that the array is guessed, it should print line in the format "!xa0_a_1xa0_a_2xa0...xa0_a__n_" (it is guaranteed that all _a__i_ are positive integers not exceeding 105), where _a__i_ is the _i_-th element of the array _a_. The response on a request is a single integer equal to _a__i_u2009+u2009_a__j_, printed on a separate line. Your program can do at most _n_ requests. Note that the final line «!xa0_a_1xa0_a_2xa0...xa0_a__n_» is not counted as a request. Do not forget about flush operation after each printed line. After you program prints the guessed array, it should terminate normally. Example Output xa0 ? 1 5? 2 3 ? 4 1 ? 5 2 ? 3 4 ! 4 6 1 5 5 Note The format of a test to make a hack is: The first line contains an integer number _n_ (3u2009≤u2009_n_u2009≤u20095000)xa0— the length of the array. The second line contains _n_ numbers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009105)xa0— the elements of the array to guess.
1,400
true
false
false
false
false
true
false
false
false
false
6,898
1567E
Alice has recently received an array $$$a_1, a_2, dots, a_n$$$ for her birthday! She is very proud of her array, and when she showed her friend Bob the array, he was very happy with her present too! However, soon Bob became curious, and as any sane friend would do, asked Alice to perform $$$q$$$ operations of two types on her array: $$$1$$$ $$$x$$$ $$$y$$$: update the element $$$a_x$$$ to $$$y$$$ (set $$$a_x = y$$$). $$$2$$$ $$$l$$$ $$$r$$$: calculate how many non-decreasing subarrays exist within the subarray $$$[a_l, a_{l+1}, dots, a_r]$$$. More formally, count the number of pairs of integers $$$(p,q)$$$ such that $$$l le p le q le r$$$ and $$$a_p le a_{p+1} le dots le a_{q-1} le a_q$$$. Help Alice answer Bob's queries! Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 2 cdot 10^5$$$)xa0— the size of the array, and the number of queries, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the elements of Alice's array. The next $$$q$$$ lines consist of three integers each. The first integer of the $$$i$$$-th line is $$$t_i$$$, the operation being performed on the $$$i$$$-th step ($$$t_i = 1$$$ or $$$t_i = 2$$$). If $$$t_i = 1$$$, the next two integers are $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i le n$$$; $$$1 le y_i le 10^9$$$), updating the element at position $$$x_i$$$ to $$$y_i$$$ (setting $$$a_{x_i} = y_i$$$). If $$$t_i = 2$$$, the next two integers are $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$), the two indices Bob asks Alice about for the $$$i$$$-th query. It's guaranteed that there is at least one operation of the second type. Output For each query of type $$$2$$$, print a single integer, the answer to the query. Example Input 5 6 3 1 4 1 5 2 2 5 2 1 3 1 4 4 2 2 5 1 2 6 2 2 5 Note For the first query, $$$l = 2$$$ and $$$r = 5$$$, and the non-decreasing subarrays $$$[p,q]$$$ are $$$[2,2]$$$, $$$[3,3]$$$, $$$[4,4]$$$, $$$[5,5]$$$, $$$[2,3]$$$ and $$$[4,5]$$$.
2,200
true
false
false
false
true
false
false
false
false
false
2,800
1392H
zscoder has a deck of $$$n+m$$$ custom-made cards, which consists of $$$n$$$ cards labelled from $$$1$$$ to $$$n$$$ and $$$m$$$ jokers. Since zscoder is lonely, he wants to play a game with himself using those cards. Initially, the deck is shuffled uniformly randomly and placed on the table. zscoder has a set $$$S$$$ which is initially empty. Every second, zscoder draws the top card from the deck. If the card has a number $$$x$$$ written on it, zscoder removes the card and adds $$$x$$$ to the set $$$S$$$. If the card drawn is a joker, zscoder places all the cards back into the deck and reshuffles (uniformly randomly) the $$$n+m$$$ cards to form a new deck (hence the new deck now contains all cards from $$$1$$$ to $$$n$$$ and the $$$m$$$ jokers). Then, if $$$S$$$ currently contains all the elements from $$$1$$$ to $$$n$$$, the game ends. Shuffling the deck doesn't take time at all. What is the expected number of seconds before the game ends? We can show that the answer can be written in the form $$$frac{P}{Q}$$$ where $$$P, Q$$$ are relatively prime integers and $$$Q eq 0 bmod 998244353$$$. Output the value of $$$(P cdot Q^{-1})$$$ modulo $$$998244353$$$. Input The only line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2 cdot 10^{6}$$$). Output Output a single integer, the value of $$$(P cdot Q^{-1})$$$ modulo $$$998244353$$$. Note For the first sample, it can be proven that the expected time before the game ends is $$$5$$$ seconds. For the second sample, it can be proven that the expected time before the game ends is $$$frac{28}{3}$$$ seconds.
3,000
true
false
false
true
false
false
false
false
false
false
3,709
612B
HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order. One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file. Find the time need to read file split to _n_ fragments. The _i_-th sector contains the _f__i_-th fragment of the file (1u2009≤u2009_f__i_u2009≤u2009_n_). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the _n_-th fragment is read. The fragments are read in the order from the first to the _n_-th. It takes _a_u2009-u2009_b_ time units to move the magnetic head from the sector _a_ to the sector _b_. Reading a fragment takes no time. Input The first line contains a positive integer _n_ (1u2009≤u2009_n_u2009≤u20092·105) — the number of fragments. The second line contains _n_ different integers _f__i_ (1u2009≤u2009_f__i_u2009≤u2009_n_) — the number of the fragment written in the _i_-th sector. Output Print the only integer — the number of time units needed to read the file. Note In the second example the head moves in the following way: 1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units 2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units 3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units 4->5 means movement from the sector 4 to the sector 3, i.e. it takes 1 time units So the answer to the second example is 4u2009+u20093u2009+u20092u2009+u20091u2009=u200910.
1,200
true
false
true
false
false
false
false
false
false
false
7,392
1862B
Tema and Vika are playing the following game. First, Vika comes up with a sequence of positive integers $$$a$$$ of length $$$m$$$ and writes it down on a piece of paper. Then she takes a new piece of paper and writes down the sequence $$$b$$$ according to the following rule: First, she writes down $$$a_1$$$. Then, she writes down only those $$$a_i$$$ ($$$2 le i le m$$$) such that $$$a_{i - 1} le a_i$$$. Let the length of this sequence be denoted as $$$n$$$. For example, from the sequence $$$a=[4, 3, 2, 6, 3, 3]$$$, Vika will obtain the sequence $$$b=[4, 6, 3]$$$. She then gives the piece of paper with the sequence $$$b$$$ to Tema. He, in turn, tries to guess the sequence $$$a$$$. Tema considers winning in such a game highly unlikely, but still wants to find at least one sequence $$$a$$$ that could have been originally chosen by Vika. Help him and output any such sequence. Note that the length of the sequence you output should not exceed the input sequence length by more than two times. Input Each test consists of multiple test cases. The first line of input data contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. This is followed by a description of the test cases. 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 sequence $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$b_1, b_2, b_3, dots, b_n$$$ ($$$1 le b_i le 10^9$$$)xa0— the elements of the sequence. The sum of the values of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output two lines. In the first line, output a single integer $$$m$$$xa0— the length of the sequence ($$$n le m le 2 cdot n$$$). In the second line, output $$$m$$$ integers $$$a_1, a_2, a_3, dots, a_m$$$ ($$$1 le a_i le 10^9$$$)xa0— the assumed sequence that Vika could have written on the first piece of paper. If there are multiple suitable sequences, you can output any of them. Example Input 6 3 4 6 3 3 1 2 3 5 1 7 9 5 7 1 144 2 1 1 5 1 2 2 1 1 Output 6 4 3 2 6 3 3 3 1 2 3 6 1 7 9 3 5 7 1 144 2 1 1 6 1 2 2 1 1 1 Note The first sample is explained in the problem statement. In the second sample, Vika could have chosen the original sequence.
800
false
false
false
false
false
true
false
false
false
false
1,112
788B
Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his motherlandxa0— Uzhlyandia. It is widely known that Uzhlyandia has _n_ cities connected with _m_ bidirectional roads. Also, there are no two roads in the country that connect the same pair of cities, but roads starting and ending in the same city can exist. Igor wants to plan his journey beforehand. Boy thinks a path is good if the path goes over _m_u2009-u20092 roads twice, and over the other 2 exactly once. The good path can start and finish in any city of Uzhlyandia. Now he wants to know how many different good paths are in Uzhlyandia. Two paths are considered different if the sets of roads the paths goes over exactly once differ. Help Igorxa0— calculate the number of good paths. Input The first line contains two integers _n_, _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009106)xa0— the number of cities and roads in Uzhlyandia, respectively. Each of the next _m_ lines contains two integers _u_ and _v_ (1u2009≤u2009_u_,u2009_v_u2009≤u2009_n_) that mean that there is road between cities _u_ and _v_. It is guaranteed that no road will be given in the input twice. That also means that for every city there is no more than one road that connects the city to itself. Output Print out the only integerxa0— the number of good paths in Uzhlyandia. Note In first sample test case the good paths are: 2u2009→u20091u2009→u20093u2009→u20091u2009→u20094u2009→u20091u2009→u20095, 2u2009→u20091u2009→u20093u2009→u20091u2009→u20095u2009→u20091u2009→u20094, 2u2009→u20091u2009→u20094u2009→u20091u2009→u20095u2009→u20091u2009→u20093, 3u2009→u20091u2009→u20092u2009→u20091u2009→u20094u2009→u20091u2009→u20095, 3u2009→u20091u2009→u20092u2009→u20091u2009→u20095u2009→u20091u2009→u20094, 4u2009→u20091u2009→u20092u2009→u20091u2009→u20093u2009→u20091u2009→u20095. There are good paths that are same with displayed above, because the sets of roads they pass over once are same: 2u2009→u20091u2009→u20094u2009→u20091u2009→u20093u2009→u20091u2009→u20095, 2u2009→u20091u2009→u20095u2009→u20091u2009→u20093u2009→u20091u2009→u20094, 2u2009→u20091u2009→u20095u2009→u20091u2009→u20094u2009→u20091u2009→u20093, 3u2009→u20091u2009→u20094u2009→u20091u2009→u20092u2009→u20091u2009→u20095, 3u2009→u20091u2009→u20095u2009→u20091u2009→u20092u2009→u20091u2009→u20094, 4u2009→u20091u2009→u20093u2009→u20091u2009→u20092u2009→u20091u2009→u20095, and all the paths in the other direction. Thus, the answer is 6. In the second test case, Igor simply can not walk by all the roads. In the third case, Igor walks once over every road.
2,100
false
false
false
false
false
true
false
false
false
true
6,620
1646E
Problem - 1646E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force dp math number theory *2200 No tag edit access → Contest materials ") Editorial") . The $$$n$$$ rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the $$$m$$$ columns are numbered from $$$1$$$ to $$$m$$$ from left to right. The cell at the intersection of row $$$i$$$ and column $$$j$$$ contains the number $$$i^j$$$ ($$$i$$$ raised to the power of $$$j$$$). For example, if $$$n=3$$$ and $$$m=3$$$ the board is as follows: Find the number of distinct integers written on the board. Input The only line contains two integers $$$n$$$ and $$$m$$$ ($$$1le n,mle 10^6$$$)xa0— the number of rows and columns of the board. Output Print one integer, the number of distinct integers on the board. Examples Input 3 3 Output 7 Input 2 4 Output 5 Input 4 2 Output 6 Note The statement shows the board for the first test case. In this case there are $$$7$$$ distinct integers: $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$8$$$, $$$9$$$, and $$$27$$$. In the second test case, the board is as follows: There are $$$5$$$ distinct numbers: $$$1$$$, $$$2$$$, $$$4$$$, $$$8$$$ and $$$16$$$. In the third test case, the board is as follows: There are $$$6$$$ distinct numbers: $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$9$$$ and $$$16$$$.
2,200
true
false
false
true
false
false
true
false
false
false
2,391
614B
It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, find their product. If it is turns to be too large, then the servers might have not enough time to assign tanks into teams and the whole game will collapse! There are exactly _n_ distinct countries in the world and the _i_-th country added _a__i_ tanks to the game. As the developers of the game are perfectionists, the number of tanks from each country is beautiful. A beautiful number, according to the developers, is such number that its decimal representation consists only of digits '1' and '0', moreover it contains at most one digit '1'. However, due to complaints from players, some number of tanks of one country was removed from the game, hence the number of tanks of this country may not remain beautiful. Your task is to write the program that solves exactly the same problem in order to verify Gena's code correctness. Just in case. Input The first line of the input contains the number of countries _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000). The second line contains _n_ non-negative integers _a__i_ without leading zeroesxa0— the number of tanks of the _i_-th country. It is guaranteed that the second line contains at least _n_u2009-u20091 beautiful numbers and the total length of all these number's representations doesn't exceed 100u2009000. Output Print a single number without leading zeroesxa0— the product of the number of tanks presented by each country. Note In sample 1 numbers 10 and 1 are beautiful, number 5 is not not. In sample 2 number 11 is not beautiful (contains two '1's), all others are beautiful. In sample 3 number 3 is not beautiful, all others are beautiful.
1,400
true
false
true
false
false
false
false
false
false
false
7,381
1573A
You are given a digital clock with $$$n$$$ digits. Each digit shows an integer from $$$0$$$ to $$$9$$$, so the whole clock shows an integer from $$$0$$$ to $$$10^n-1$$$. The clock will show leading zeroes if the number is smaller than $$$10^{n-1}$$$. You want the clock to show $$$0$$$ with as few operations as possible. In an operation, you can do one of the following: decrease the number on the clock by $$$1$$$, or swap two digits (you can choose which digits to swap, and they don't have to be adjacent). Your task is to determine the minimum number of operations needed to make the clock show $$$0$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^3$$$). The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$) — number of digits on the clock. The second line of each test case contains a string of $$$n$$$ digits $$$s_1, s_2, ldots, s_n$$$ ($$$0 le s_1, s_2, ldots, s_n le 9$$$) — the number on the clock. Note: If the number is smaller than $$$10^{n-1}$$$ the clock will show leading zeroes. Output For each test case, print one integer: the minimum number of operations needed to make the clock show $$$0$$$. Example Input 7 3 007 4 1000 5 00000 3 103 4 2020 9 123456789 30 001678294039710047203946100020 Note In the first example, it's optimal to just decrease the number $$$7$$$ times. In the second example, we can first swap the first and last position and then decrease the number by $$$1$$$. In the third example, the clock already shows $$$0$$$, so we don't have to perform any operations.
800
false
true
false
false
false
false
false
false
false
false
2,776
1991A
You are given an array $$$a$$$ of $$$n$$$ integers, where $$$n$$$ is odd. In one operation, you will remove two adjacent elements from the array $$$a$$$, and then concatenate the remaining parts of the array. For example, given the array $$$[4,7,4,2,9]$$$, we can obtain the arrays $$$[4,2,9]$$$ and $$$[4,7,9]$$$ by the operations $$$[underline{4,7}, 4,2,9] o [4,2,9]$$$ and $$$[4,7,underline{4,2},9] o [4,7,9]$$$ respectively. However, we cannot obtain the array $$$[7,2,9]$$$ as it requires deleting non-adjacent elements $$$[underline{4},7,underline{4},2,9]$$$. You will repeatedly perform this operation until exactly one element remains in $$$a$$$. Find the maximum possible value of the remaining element in $$$a$$$. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 99$$$; $$$n$$$ is odd)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 100$$$)xa0— the elements of the array $$$a$$$. Note that there is no bound on the sum of $$$n$$$ over all test cases. Output For each test case, output a single integerxa0— the maximum possible value of the remaining element in $$$a$$$. Example Input 4 1 6 3 1 3 2 5 4 7 4 2 9 7 3 1 4 1 5 9 2 Note In the first test case, the array $$$a$$$ is $$$[6]$$$. Since there is only one element, no operations are needed. The maximum possible value of the remaining element is $$$6$$$. In the second test case, the array $$$a$$$ is $$$[1, 3, 2]$$$. We can remove the first two elements $$$[underline{1, 3}, 2] o [2]$$$, or remove the last two elements $$$[1, underline{3, 2}] o [1]$$$. Therefore, the maximum possible value of the remaining element is $$$2$$$. In the third test case, the array $$$a$$$ is $$$[4, 7, 4, 2, 9]$$$. One way to maximize the remaining element is $$$[4, underline{7, 4}, 2, 9] o [underline{4, 2}, 9] o [9]$$$. Therefore, the maximum possible value of the remaining element is $$$9$$$. In the fourth test case, the array $$$a$$$ is $$$[3, 1, 4, 1, 5, 9, 2]$$$. It can be shown that the maximum possible value of the remaining element is $$$5$$$.
800
false
true
true
false
false
false
false
false
false
false
328
1562E
Morning desert sun horizonRise above the sands of time... Fates Warning, "Exodus" After crossing the Windswept Wastes, Ori has finally reached the Windtorn Ruins to find the Heart of the Forest! However, the ancient repository containing this priceless Willow light did not want to open! Ori was taken aback, but the Voice of the Forest explained to him that the cunning Gorleks had decided to add protection to the repository. The Gorleks were very fond of the "string expansion" operation. They were also very fond of increasing subsequences. Suppose a string $$$s_1s_2s_3 ldots s_n$$$ is given. Then its "expansion" is defined as the sequence of strings $$$s_1$$$, $$$s_1 s_2$$$, ..., $$$s_1 s_2 ldots s_n$$$, $$$s_2$$$, $$$s_2 s_3$$$, ..., $$$s_2 s_3 ldots s_n$$$, $$$s_3$$$, $$$s_3 s_4$$$, ..., $$$s_{n-1} s_n$$$, $$$s_n$$$. For example, the "expansion" the string 'abcd' will be the following sequence of strings: 'a', 'ab', 'abc', 'abcd', 'b', 'bc', 'bcd', 'c', 'cd', 'd'. To open the ancient repository, Ori must find the size of the largest increasing subsequence of the "expansion" of the string $$$s$$$. Here, strings are compared lexicographically. Help Ori with this task! 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 Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ ($$$1 le t le 10^3$$$), denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer $$$n$$$ ($$$1 le n le 5000$$$)xa0— length of the string. The second line of each test case contains a non-empty string of length $$$n$$$, which consists of lowercase latin letters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$. Note In first test case the "expansion" of the string is: 'a', 'ac', 'acb', 'acba', 'acbac', 'c', 'cb', 'cba', 'cbac', 'b', 'ba', 'bac', 'a', 'ac', 'c'. The answer can be, for example, 'a', 'ac', 'acb', 'acba', 'acbac', 'b', 'ba', 'bac', 'c'.
2,500
false
true
false
true
false
false
false
false
false
false
2,815
1637F
You are given a tree with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. The height of the $$$i$$$-th vertex is $$$h_i$$$. You can place any number of towers into vertices, for each tower you can choose which vertex to put it in, as well as choose its efficiency. Setting up a tower with efficiency $$$e$$$ costs $$$e$$$ coins, where $$$e > 0$$$. It is considered that a vertex $$$x$$$ gets a signal if for some pair of towers at the vertices $$$u$$$ and $$$v$$$ ($$$u eq v$$$, but it is allowed that $$$x = u$$$ or $$$x = v$$$) with efficiencies $$$e_u$$$ and $$$e_v$$$, respectively, it is satisfied that $$$min(e_u, e_v) geq h_x$$$ and $$$x$$$ lies on the path between $$$u$$$ and $$$v$$$. Find the minimum number of coins required to set up towers so that you can get a signal at all vertices. Input The first line contains an integer $$$n$$$ ($$$2 le n le 200,000$$$)xa0— the number of vertices in the tree. The second line contains $$$n$$$ integers $$$h_i$$$ ($$$1 le h_i le 10^9$$$)xa0— the heights of the vertices. Each of the next $$$n - 1$$$ lines contain a pair of numbers $$$v_i, u_i$$$ ($$$1 le v_i, u_i le n$$$)xa0— an edge of the tree. It is guaranteed that the given edges form a tree. Output Print one integerxa0— the minimum required number of coins. Examples Input 5 1 3 3 1 3 1 3 5 4 4 3 2 3 Note In the first test case it's optimal to install two towers with efficiencies $$$2$$$ at vertices $$$1$$$ and $$$3$$$. In the second test case it's optimal to install a tower with efficiency $$$1$$$ at vertex $$$1$$$ and two towers with efficiencies $$$3$$$ at vertices $$$2$$$ and $$$5$$$. In the third test case it's optimal to install two towers with efficiencies $$$6$$$ at vertices $$$1$$$ and $$$2$$$.
2,500
false
true
false
true
false
true
false
false
false
false
2,429
140E
As Gerald, Alexander, Sergey and Gennady are already busy with the usual New Year chores, Edward hastily decorates the New Year Tree. And any decent New Year Tree must be decorated with a good garland. Edward has lamps of _m_ colors and he wants to make a garland from them. That garland should represent a sequence whose length equals _L_. Edward's tree is _n_ layers high and Edward plans to hang the garland so as to decorate the first layer with the first _l_1 lamps, the second layer — with the next _l_2 lamps and so on. The last _n_-th layer should be decorated with the last _l__n_ lamps, Edward adores all sorts of math puzzles, so he suddenly wondered: how many different ways to assemble the garland are there given that the both following two conditions are met: 1. Any two lamps that follow consecutively in the same layer should have different colors. 2. The sets of used colors in every two neighbouring layers must be different. We consider unordered sets (not multisets), where every color occurs no more than once. So the number of lamps of particular color does not matter. Help Edward find the answer to this nagging problem or else he won't manage to decorate the Tree by New Year. You may consider that Edward has an unlimited number of lamps of each of _m_ colors and it is not obligatory to use all _m_ colors. The garlands are considered different if they differ in at least one position when represented as sequences. Calculate the answer modulo _p_. Input The first line contains three integers _n_, _m_ and _p_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009106, 2u2009≤u2009_p_u2009≤u2009109) which are the number of the tree's layers, the number of the lamps' colors and module correspondingly. The next line contains _n_ integers _l__i_ (1u2009≤u2009_l__i_u2009≤u20095000, ). Output Print the only integer — the number of garlands modulo _p_. Note In the first sample the following variants are possible: 121112, 121121, 121212, 121221, 212112, 212121, 212212, 212221. In the second sample the following variants are possible: 1213, 1223, 1231, 1232 and so on. Figure for the first sample:
2,600
false
false
false
true
false
false
false
false
false
false
9,314
1584E
Bob decided to take a break from calculus homework and designed a game for himself. The game is played on a sequence of piles of stones, which can be described with a sequence of integers $$$s_1, ldots, s_k$$$, where $$$s_i$$$ is the number of stones in the $$$i$$$-th pile. On each turn, Bob picks a pair of non-empty adjacent piles $$$i$$$ and $$$i+1$$$ and takes one stone from each. If a pile becomes empty, its adjacent piles do not become adjacent. The game ends when Bob can't make turns anymore. Bob considers himself a winner if at the end all piles are empty. We consider a sequence of piles winning if Bob can start with it and win with some sequence of moves. You are given a sequence $$$a_1, ldots, a_n$$$, count the number of subsegments of $$$a$$$ that describe a winning sequence of piles. In other words find the number of segments $$$[l, r]$$$ ($$$1 leq l leq r leq n$$$), such that the sequence $$$a_l, a_{l+1}, ldots, a_r$$$ is winning. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 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 leq n leq 3 cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output Print a single integer for each test case — the answer to the problem. Example Input 6 2 2 2 3 1 2 3 4 1 1 1 1 4 1 2 2 1 4 1 2 1 2 8 1 2 1 2 1 2 1 2 Note In the first test case, Bob can't win on subsegments of length $$$1$$$, as there is no pair of adjacent piles in an array of length $$$1$$$. In the second test case, every subsegment is not winning. In the fourth test case, the subsegment $$$[1, 4]$$$ is winning, because Bob can make moves with pairs of adjacent piles: $$$(2, 3)$$$, $$$(1, 2)$$$, $$$(3, 4)$$$. Another winning subsegment is $$$[2, 3]$$$.
2,300
false
true
false
false
true
false
false
true
false
false
2,705
926J
Problem - 926J - Codeforces =============== xa0 . → Problem tags data structures *2100 No tag edit access → Contest materials . In particular, if one segment ends in a point _x_, and another segment starts in the point _x_, these two segments belong to the same connected component. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000) — the number of segments. The _i_-th of the next _n_ lines contains two integers _l__i_ and _r__i_ (1u2009≤u2009_l__i_u2009<u2009_r__i_u2009≤u2009109) — the coordinates of the left and the right ends of the _i_-th segment. The segments are listed in the order they are added on the white line. Output Print _n_ integers — the number of connected components of black segments after each segment is added. Examples Input 3 1 3 4 5 2 4 Output 1 2 1 Input 9 10 20 50 60 30 40 70 80 90 100 60 70 10 40 40 50 80 90 Output 1 2 3 4 5 4 3 2 1 Note In the first example there are two components after the addition of the first two segments, because these segments do not intersect. The third added segment intersects the left segment and touches the right segment at the point 4 (these segments belong to the same component, according to the statements). Thus the number of connected components of black segments is equal to 1 after that.
2,100
false
false
false
false
true
false
false
false
false
false
5,996
1028H
We call an array $$$b_1, b_2, ldots, b_m$$$ good, if there exist two indices $$$i < j$$$ such that $$$b_i cdot b_j$$$ is a $$$ be the minimum number of actions needed to make the array $$$b$$$ good. You are given an array of $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ and $$$q$$$ queries of the form $$$l_i, r_i$$$. For each query output $$$f(a_{l_i}, a_{l_i + 1}, ldots, a_{r_i})$$$. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 le n le 194,598$$$, $$$1 le q le 1,049,658$$$) — the length of the array and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 5,032,107$$$)xa0— the elements of the array. Each of the next $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i < r_i le n$$$) — the parameters of a query. Output Output $$$q$$$ lines — the answers for each query in the order they are given in the input. Example Input 10 10 34 37 28 16 44 36 43 50 22 13 1 3 4 8 6 10 9 10 3 10 8 9 5 6 1 4 1 7 2 6 Note In the first query of the first sample you can multiply second number by 7 to get 259 and multiply the third one by 37 to get 1036. Then $$$a_2 cdot a_3 = 268,324 = 518^2$$$. In the second query subarray is already good because $$$a_4 cdot a_6 = 24^2$$$. In the third query you can divide 50 by 2 to get 25. Then $$$a_6 cdot a_8 = 30^2$$$.
2,900
true
false
false
false
false
false
false
false
false
false
5,562
1668B
$$$m$$$ chairs are arranged in a circle sequentially. The chairs are numbered from $$$0$$$ to $$$m-1$$$. $$$n$$$ people want to sit in these chairs. The $$$i$$$-th of them wants at least $$$a[i]$$$ empty chairs both on his right and left side. More formally, if the $$$i$$$-th person sits in the $$$j$$$-th chair, then no one else should sit in the following chairs: $$$(j-a[i]) bmod m$$$, $$$(j-a[i]+1) bmod m$$$, ... $$$(j+a[i]-1) bmod m$$$, $$$(j+a[i]) bmod m$$$. Decide if it is possible to sit down for all of them, under the given limitations. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 5 cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 10^5$$$, $$$1 leq m leq 10^9$$$) — the number of people and the number of chairs. The next line contains $$$n$$$ integers, $$$a_1$$$, $$$a_2$$$, ... $$$a_n$$$ ($$$1 leq a_i leq 10^9$$$) — the minimum number of empty chairs, on both sides of the $$$i$$$-th person. It is guaranteed that the sum of $$$n$$$ over all test cases will not exceed $$$10^5$$$. Output For each test case print "YES" (without quotes) if it is possible for everyone to sit down and fulfil the restrictions, and "NO" (without quotes) otherwise. You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" will all be recognized as positive answers). Example Input 6 3 2 1 1 1 2 4 1 1 2 5 2 1 3 8 1 2 1 4 12 1 2 1 3 4 19 1 2 1 3 Output NO YES NO YES NO YES Note Test case $$$1$$$: $$$n>m$$$, so they can not sit down. Test case $$$2$$$: the first person can sit $$$2$$$-nd and the second person can sit in the $$$0$$$-th chair. Both of them want at least $$$1$$$ empty chair on both sides, chairs $$$1$$$ and $$$3$$$ are free, so this is a good solution. Test case $$$3$$$: if the second person sits down somewhere, he needs $$$2$$$ empty chairs, both on his right and on his left side, so it is impossible to find a place for the first person, because there are only $$$5$$$ chairs. Test case $$$4$$$: they can sit in the $$$1$$$-st, $$$4$$$-th, $$$7$$$-th chairs respectively.
900
true
true
false
false
false
false
false
false
true
false
2,266
1468N
The progress is not standing still in Berland. Recently all garbage containers in Bertown, the capital of Berland, were replaced by differentiated recycling bins, each accepting some category of waste. While this will definitely improve the ecological situation, for some citizens it's difficult to get used to the habit of sorting waste. Monocarp is one of those citizens who tries to get used to waste sorting. Today he has to take out the trash from his house. There are three containers near the Monocarp's house, the first one accepts paper waste, the second one accepts plastic waste, and the third one — all other types of waste. It is possible to fit $$$c_1$$$ items into the first container, $$$c_2$$$ items into the second container and $$$c_3$$$ items into the third container. Monocarp has a lot of items to throw into containers. Some are made of paper, so Monocarp has to put them into the first container (he has $$$a_1$$$ such items), some are made of plastic, so he has to put them into the second container (he has $$$a_2$$$ such items), and some are neither paper nor plastic — so Monocarp has to put them into the third container (he has $$$a_3$$$ such items). Unfortunately, there are also two categories of items that Monocarp is unsure of: he has $$$a_4$$$ items which are partially made of paper, so he will put each of these items either into the first container or into the third container. Similarly, he has $$$a_5$$$ items partially made of plastic, so he has to put each of them either into the second container or into the third container. Obviously, this choice is made separately for each item — for example, Monocarp can throw several partially-plastic items into the second container, and all other partially-plastic items — into the third one. Now Monocarp wonders: is it possible to put each item into some container so that the first container will hold no more than $$$c_1$$$ items, the second one — no more than $$$c_2$$$ items, and the third one — no more than $$$c_3$$$ items? Input The first line contains one integer $$$t$$$ ($$$1 le t le 3 cdot 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains three integers $$$c_1$$$, $$$c_2$$$, $$$c_3$$$ ($$$0 le c_1, c_2, c_3 le 10^8$$$) — the capacities of the containers. The second line of each test case contains five integers $$$a_1$$$, $$$a_2$$$, $$$a_3$$$, $$$a_4$$$, $$$a_5$$$ ($$$0 le a_i le 10^8$$$), where $$$a_i$$$ is the number of items of the $$$i$$$-th category Monocarp has to throw out ($$$i = 1$$$ is paper waste, $$$i = 2$$$ is plastic waste, $$$i = 3$$$ is general waste, $$$i = 4$$$ is partially-paper waste, $$$i = 5$$$ is partially-plastic waste). Output For each test case, print either YES if it is possible to fit all items into containers, or NO otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
900
false
true
true
false
false
false
false
false
false
false
3,339
208D
Vasya, like many others, likes to participate in a variety of sweepstakes and lotteries. Now he collects wrappings from a famous chocolate bar "Jupiter". According to the sweepstake rules, each wrapping has an integer written on it — the number of points that the participant adds to his score as he buys the bar. After a participant earns a certain number of points, he can come to the prize distribution center and exchange the points for prizes. When somebody takes a prize, the prize's cost is simply subtracted from the number of his points. Vasya didn't only bought the bars, he also kept a record of how many points each wrapping cost. Also, he remembers that he always stucks to the greedy strategy — as soon as he could take at least one prize, he went to the prize distribution centre and exchanged the points for prizes. Moreover, if he could choose between multiple prizes, he chose the most expensive one. If after an exchange Vasya had enough points left to get at least one more prize, then he continued to exchange points. The sweepstake has the following prizes (the prizes are sorted by increasing of their cost): a mug (costs _a_ points), a towel (costs _b_ points), a bag (costs _c_ points), a bicycle (costs _d_ points), a car (costs _e_ points). Now Vasya wants to recollect what prizes he has received. You know sequence _p_1,u2009_p_2,u2009...,u2009_p__n_, where _p__i_ is the number of points Vasya got for the _i_-th bar. The sequence of points is given in the chronological order. You also know numbers _a_, _b_, _c_, _d_, _e_. Your task is to find, how many prizes Vasya received, what prizes they are and how many points he's got left after all operations are completed. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u200950) — the number of chocolate bar wrappings that brought points to Vasya. The second line contains space-separated integers _p_1,u2009_p_2,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009≤u2009109). The third line contains 5 integers _a_, _b_, _c_, _d_, _e_ (1u2009≤u2009_a_u2009<u2009_b_u2009<u2009_c_u2009<u2009_d_u2009<u2009_e_u2009≤u2009109) — the prizes' costs. Output Print on the first line 5 integers, separated by a space — the number of mugs, towels, bags, bicycles and cars that Vasya has got, respectively. On the second line print a single integer — the number of points Vasya will have left after all operations of exchange are completed. 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. Note In the first sample Vasya gets 3 points after eating the first chocolate bar. Then he exchanges 2 points and gets a mug. Vasya wins a bag after eating the second chocolate bar. Then he wins a towel after eating the third chocolate bar. After all chocolate bars 3u2009-u20092u2009+u200910u2009-u200910u2009+u20094u2009-u20094u2009=u20091 points remains.
1,200
false
false
true
false
false
false
false
false
false
false
8,998
847F
The elections to Berland parliament are happening today. Voting is in full swing! Totally there are _n_ candidates, they are numbered from 1 to _n_. Based on election results _k_ (1u2009≤u2009_k_u2009≤u2009_n_) top candidates will take seats in the parliament. After the end of the voting the number of votes for each candidate is calculated. In the resulting table the candidates are ordered by the number of votes. In case of tie (equal number of votes) they are ordered by the time of the last vote given. The candidate with ealier last vote stands higher in the resulting table. So in the resulting table candidates are sorted by the number of votes (more votes stand for the higher place) and if two candidates have equal number of votes they are sorted by the time of last vote (earlier last vote stands for the higher place). There is no way for a candidate with zero votes to take a seat in the parliament. So it is possible that less than _k_ candidates will take a seat in the parliament. In Berland there are _m_ citizens who can vote. Each of them will vote for some candidate. Each citizen will give a vote to exactly one of _n_ candidates. There is no option "against everyone" on the elections. It is not accepted to spoil bulletins or not to go to elections. So each of _m_ citizens will vote for exactly one of _n_ candidates. At the moment _a_ citizens have voted already (1u2009≤u2009_a_u2009≤u2009_m_). This is an open election, so for each citizen it is known the candidate for which the citizen has voted. Formally, the _j_-th citizen voted for the candidate _g__j_. The citizens who already voted are numbered in chronological order; i.e. the (_j_u2009+u20091)-th citizen voted after the _j_-th. The remaining _m_u2009-u2009_a_ citizens will vote before the end of elections, each of them will vote for one of _n_ candidates. Your task is to determine for each of _n_ candidates one of the three possible outcomes: a candidate will be elected to the parliament regardless of votes of the remaining _m_u2009-u2009_a_ citizens; a candidate has chance to be elected to the parliament after all _n_ citizens have voted; a candidate has no chances to be elected to the parliament regardless of votes of the remaining _m_u2009-u2009_a_ citizens. Input The first line contains four integers _n_, _k_, _m_ and _a_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_m_u2009≤u2009100, 1u2009≤u2009_a_u2009≤u2009_m_) — the number of candidates, the number of seats in the parliament, the number of Berland citizens and the number of citizens who already have voted. The second line contains a sequence of _a_ integers _g_1,u2009_g_2,u2009...,u2009_g__a_ (1u2009≤u2009_g__j_u2009≤u2009_n_), where _g__j_ is the candidate for which the _j_-th citizen has voted. Citizens who already voted are numbered in increasing order of voting times. Output Print the sequence consisting of _n_ integers _r_1,u2009_r_2,u2009...,u2009_r__n_ where: _r__i_u2009=u20091 means that the _i_-th candidate is guaranteed to take seat in the parliament regardless of votes of the remaining _m_u2009-u2009_a_ citizens; _r__i_u2009=u20092 means that the _i_-th candidate has a chance to take a seat in the parliament, i.e. the remaining _m_u2009-u2009_a_ citizens can vote in such a way that the candidate will take a seat in the parliament; _r__i_u2009=u20093 means that the _i_-th candidate will not take a seat in the parliament regardless of votes of the remaining _m_u2009-u2009_a_ citizens.
2,100
false
true
false
false
false
false
false
false
true
false
6,366
1288B
Problem - 1288B - 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 *1100 No tag edit access → Contest materials $$$ such that $$$1 le a le A$$$, $$$1 le b le B$$$, and the equation $$$a cdot b + a + b = conc(a, b)$$$ is true; $$$conc(a, b)$$$ is the concatenation of $$$a$$$ and $$$b$$$ (for example, $$$conc(12, 23) = 1223$$$, $$$conc(100, 11) = 10011$$$). $$$a$$$ and $$$b$$$ should not contain leading zeroes. Input The first line contains $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. Each test case contains two integers $$$A$$$ and $$$B$$$ $$$(1 le A, B le 10^9)$$$. Output Print one integer — the number of pairs $$$(a, b)$$$ such that $$$1 le a le A$$$, $$$1 le b le B$$$, and the equation $$$a cdot b + a + b = conc(a, b)$$$ is true. Example Input 3 1 11 4 2 191 31415926 Output 1 0 1337 Note There is only one suitable pair in the first test case: $$$a = 1$$$, $$$b = 9$$$ ($$$1 + 9 + 1 cdot 9 = 19$$$).
1,100
true
false
false
false
false
false
false
false
false
false
4,251
1793A
The famous store "Second Food" sells groceries only two days a month. And the prices in each of days differ. You wanted to buy $$$n$$$ kilos of potatoes for a month. You know that on the first day of the month $$$1$$$ kilo of potatoes costs $$$a$$$ coins, and on the second day $$$b$$$ coins. In "Second Food" you can buy any integer kilograms of potatoes. Fortunately, "Second Food" has announced a promotion for potatoes, which is valid only on the first day of the month — for each $$$m$$$ kilos of potatoes you buy, you get $$$1$$$ kilo as a gift! In other words, you can get $$$m + 1$$$ kilograms by paying for $$$m$$$ kilograms. Find the minimum number of coins that you have to spend to buy at least $$$n$$$ kilos of potatoes. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10,000$$$). Description of the test cases follows. The first line of each test case contains two integers $$$a$$$ and $$$b$$$ $$$(1 leq a, b leq 10^9)$$$ — the prices of $$$1$$$ kilo of potatoes on the first and second days, respectively. The second line contains two integers $$$n$$$ and $$$m$$$ $$$(1 leq n, m leq 10^9)$$$ — the required amount of potatoes to buy and the amount of potatoes to use the promotion. Output For each test case print one integer — the minimum number of coins that you have to pay to buy at least $$$n$$$ kilos of potatoes. Example Input 5 5 4 3 1 5 4 3 2 3 4 3 5 20 15 10 2 1000000000 900000000 1000000000 8 Output 9 10 9 135 888888888900000000 Note In the first test case, on the first day you buy $$$1$$$ kilo and get $$$1$$$ more for a promotion. On the second day, you can buy $$$1$$$ kilo of potatoes. Thus, you will spend $$$5+4=9$$$ coins in total. In the second test case, on the first day you buy $$$2$$$ kilo and get another $$$1$$$ more for a promotion. This way you will spend $$$2 cdot 5 = 10$$$ coins.
800
true
true
false
false
false
false
false
false
false
false
1,520
533D
We have an old building with _n_u2009+u20092 columns in a row. These columns support the ceiling. These columns are located in points with coordinates 0u2009=u2009_x_0u2009<u2009_x_1u2009<u2009...u2009<u2009_x__n_u2009<u2009_x__n_u2009+u20091. The leftmost and the rightmost columns are special, we will call them bearing, the other columns are ordinary. For each column we know its durability _d__i_. Let's consider an ordinary column with coordinate _x_. Let's assume that the coordinate of the closest to it column to the left (bearing or ordinary) is _a_ and the coordinate of the closest to it column to the right (also, bearing or ordinary) is _b_. In this task let's assume that this column supports the segment of the ceiling from point to point (here both fractions are considered as real division). If the length of the segment of the ceiling supported by the column exceeds _d__i_, then the column cannot support it and it crashes after a while, and after that the load is being redistributeed between the neighbouring columns according to the same principle. Thus, ordinary columns will be crashing for some time until the process stops at some state. One can prove that the set of the remaining columns doesn't depend on the order in which columns crash. If there are only two bearing columns left in the end, then we assume that the whole construction crashes under the weight of the roof. But if at least one ordinary column stays in addition to the bearing ones, then the building doesn't crash. To make the building stronger, we can add one extra ordinary column of arbitrary durability _d_' at any (not necessarily integer) point 0u2009<u2009_x_'u2009<u2009_x__n_u2009+u20091. If point _x_' is already occupied by an ordinary column, it is replaced by a new one. Your task is to find out: what minimal durability can the added column have so that the building doesn't crash? Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of ordinary columns. The second line contains _n_u2009+u20092 integers _x_0,u2009_x_1,u2009...,u2009_x__n_,u2009_x__n_u2009+u20091 (_x_0u2009=u20090, _x__i_u2009<u2009_x__i_u2009+u20091 for 0u2009≤u2009_i_u2009≤u2009_n_, _x__n_u2009+u20091u2009≤u2009109) — the coordinates of the columns. The third line contains _n_ integers _d_1,u2009_d_2,u2009...,u2009_d__n_ (1u2009≤u2009_d__i_u2009≤u2009109). Output Print a single number — the minimum possible durability of the column that you need to add in order to make the building stay. If you do not have to add the column, please print 0. Your answer will be checked with the relative or absolute error 10u2009-u20094.
3,000
false
false
false
true
true
false
false
false
false
false
7,704
1594B
Problem - 1594B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags bitmasks math *1100 No tag edit access → Contest materials ") gave him a problem about a sequence that consists of only special numbers. Let's call a positive number special if it can be written as a sum of different non-negative powers of $$$n$$$. For example, for $$$n = 4$$$ number $$$17$$$ is special, because it can be written as $$$4^0 + 4^2 = 1 + 16 = 17$$$, but $$$9$$$ is not. Theofanis asks you to help him find the $$$k$$$-th special number if they are sorted in increasing order. Since this number may be too large, output it modulo $$$10^9+7$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 10^9$$$; $$$1 le k le 10^9$$$). Output For each test case, print one integerxa0— the $$$k$$$-th special number in increasing order modulo $$$10^9+7$$$. Example Input 3 3 4 2 12 105 564 Output 9 12 3595374 Note For $$$n = 3$$$ the sequence is $$$
1,100
true
false
false
false
false
false
false
false
false
false
2,677
718A
Efim just received his grade for the last test. He studies in a special school and his grade can be equal to any positive decimal fraction. First he got disappointed, as he expected a way more pleasant result. Then, he developed a tricky plan. Each second, he can ask his teacher to round the grade at any place after the decimal point (also, he can ask to round to the nearest integer). There are _t_ seconds left till the end of the break, so Efim has to act fast. Help him find what is the maximum grade he can get in no more than _t_ seconds. Note, that he can choose to not use all _t_ seconds. Moreover, he can even choose to not round the grade at all. In this problem, classic rounding rules are used: while rounding number to the _n_-th digit one has to take a look at the digit _n_u2009+u20091. If it is less than 5 than the _n_-th digit remain unchanged while all subsequent digits are replaced with 0. Otherwise, if the _n_u2009+u20091 digit is greater or equal to 5, the digit at the position _n_ is increased by 1 (this might also change some other digits, if this one was equal to 9) and all subsequent digits are replaced with 0. At the end, all trailing zeroes are thrown away. For example, if the number 1.14 is rounded to the first decimal place, the result is 1.1, while if we round 1.5 to the nearest integer, the result is 2. Rounding number 1.299996121 in the fifth decimal place will result in number 1.3. Input The first line of the input contains two integers _n_ and _t_ (1u2009≤u2009_n_u2009≤u2009200u2009000, 1u2009≤u2009_t_u2009≤u2009109)xa0— the length of Efim's grade and the number of seconds till the end of the break respectively. The second line contains the grade itself. It's guaranteed that the grade is a positive number, containing at least one digit after the decimal points, and it's representation doesn't finish with 0. Note In the first two samples Efim initially has grade 10.245. During the first second Efim can obtain grade 10.25, and then 10.3 during the next second. Note, that the answer 10.30 will be considered incorrect. In the third sample the optimal strategy is to not perform any rounding at all.
1,700
true
false
true
true
false
false
false
false
false
false
6,943
493D
Vasya decided to learn to play chess. Classic chess doesn't seem interesting to him, so he plays his own sort of chess. The queen is the piece that captures all squares on its vertical, horizontal and diagonal lines. If the cell is located on the same vertical, horizontal or diagonal line with queen, and the cell contains a piece of the enemy color, the queen is able to move to this square. After that the enemy's piece is removed from the board. The queen cannot move to a cell containing an enemy piece if there is some other piece between it and the queen. There is an _n_u2009×u2009_n_ chessboard. We'll denote a cell on the intersection of the _r_-th row and _c_-th column as (_r_,u2009_c_). The square (1,u20091) contains the white queen and the square (1,u2009_n_) contains the black queen. All other squares contain green pawns that don't belong to anyone. The players move in turns. The player that moves first plays for the white queen, his opponent plays for the black queen. On each move the player has to capture some piece with his queen (that is, move to a square that contains either a green pawn or the enemy queen). The player loses if either he cannot capture any piece during his move or the opponent took his queen during the previous move. Help Vasya determine who wins if both players play with an optimal strategy on the board _n_u2009×u2009_n_. Input The input contains a single number _n_ (2u2009≤u2009_n_u2009≤u2009109) — the size of the board. Output On the first line print the answer to problem — string "white" or string "black", depending on who wins if the both players play optimally. If the answer is "white", then you should also print two integers _r_ and _c_ representing the cell (_r_,u2009_c_), where the first player should make his first move to win. If there are multiple such cells, print the one with the minimum _r_. If there are still multiple squares, print the one with the minimum _c_. Note In the first sample test the white queen can capture the black queen at the first move, so the white player wins. In the second test from the statement if the white queen captures the green pawn located on the central vertical line, then it will be captured by the black queen during the next move. So the only move for the white player is to capture the green pawn located at (2,u20091). Similarly, the black queen doesn't have any other options but to capture the green pawn located at (2,u20093), otherwise if it goes to the middle vertical line, it will be captured by the white queen. During the next move the same thing happens — neither the white, nor the black queen has other options rather than to capture green pawns situated above them. Thus, the white queen ends up on square (3,u20091), and the black queen ends up on square (3,u20093). In this situation the white queen has to capture any of the green pawns located on the middle vertical line, after that it will be captured by the black queen. Thus, the player who plays for the black queen wins.
1,700
true
false
false
false
false
true
false
false
false
false
7,860
452E
Problem - 452E - Codeforces =============== xa0 ]( "Announcement of Round 1") . For each integer _l_ (1u2009≤u2009_l_u2009≤u2009_min_(_s_1,u2009_s_2,u2009_s_3) you need to find how many triples (_i_1,u2009_i_2,u2009_i_3) exist such that three strings _s__k_
2,400
false
false
false
false
true
false
false
false
false
false
8,028
417D
A boy named Gena really wants to get to the "Russian Code Cup" finals, or at least get a t-shirt. But the offered problems are too complex, so he made an arrangement with his _n_ friends that they will solve the problems for him. The participants are offered _m_ problems on the contest. For each friend, Gena knows what problems he can solve. But Gena's friends won't agree to help Gena for nothing: the _i_-th friend asks Gena _x__i_ rubles for his help in solving all the problems he can. Also, the friend agreed to write a code for Gena only if Gena's computer is connected to at least _k__i_ monitors, each monitor costs _b_ rubles. Gena is careful with money, so he wants to spend as little money as possible to solve all the problems. Help Gena, tell him how to spend the smallest possible amount of money. Initially, there's no monitors connected to Gena's computer. Input The first line contains three integers _n_, _m_ and _b_ (1u2009≤u2009_n_u2009≤u2009100; 1u2009≤u2009_m_u2009≤u200920; 1u2009≤u2009_b_u2009≤u2009109)xa0— the number of Gena's friends, the number of problems and the cost of a single monitor. The following 2_n_ lines describe the friends. Lines number 2_i_ and (2_i_u2009+u20091) contain the information about the _i_-th friend. The 2_i_-th line contains three integers _x__i_, _k__i_ and _m__i_ (1u2009≤u2009_x__i_u2009≤u2009109; 1u2009≤u2009_k__i_u2009≤u2009109; 1u2009≤u2009_m__i_u2009≤u2009_m_)xa0— the desired amount of money, monitors and the number of problems the friend can solve. The (2_i_u2009+u20091)-th line contains _m__i_ distinct positive integersxa0— the numbers of problems that the _i_-th friend can solve. The problems are numbered from 1 to _m_. Output Print the minimum amount of money Gena needs to spend to solve all the problems. Or print -1, if this cannot be achieved. Examples Input 2 2 1 100 1 1 2 100 2 1 1 Input 3 2 5 100 1 1 1 100 1 1 2 200 1 2 1 2
1,900
false
true
false
true
false
false
false
false
true
false
8,154
910A
A frog lives on the axis _Ox_ and needs to reach home which is in the point _n_. She starts from the point 1. The frog can jump to the right at a distance not more than _d_. So, after she jumped from the point _x_ she can reach the point _x_u2009+u2009_a_, where _a_ is an integer from 1 to _d_. For each point from 1 to _n_ is known if there is a lily flower in it. The frog can jump only in points with a lilies. Guaranteed that there are lilies in the points 1 and _n_. Determine the minimal number of jumps that the frog needs to reach home which is in the point _n_ from the point 1. Consider that initially the frog is in the point 1. If the frog can not reach home, print -1. Input The first line contains two integers _n_ and _d_ (2u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_d_u2009≤u2009_n_u2009-u20091) — the point, which the frog wants to reach, and the maximal length of the frog jump. The second line contains a string _s_ of length _n_, consisting of zeros and ones. If a character of the string _s_ equals to zero, then in the corresponding point there is no lily flower. In the other case, in the corresponding point there is a lily flower. Guaranteed that the first and the last characters of the string _s_ equal to one. Output If the frog can not reach the home, print -1. In the other case, print the minimal number of jumps that the frog needs to reach the home which is in the point _n_ from the point 1. Note In the first example the from can reach home in two jumps: the first jump from the point 1 to the point 4 (the length of the jump is three), and the second jump from the point 4 to the point 8 (the length of the jump is four). In the second example the frog can not reach home, because to make it she need to jump on a distance three, but the maximum length of her jump equals to two.
800
false
true
true
true
false
false
false
false
false
false
6,106
29A
Problem - 29A - 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 *1000 No tag edit access → Contest materials ") — the amount of camels in the zoo. Each of the following _n_ lines contains two integers _x__i_ and _d__i_ (u2009-u2009104u2009≤u2009_x__i_u2009≤u2009104,u20091u2009≤u2009_d__i_u2009≤u20092·104) — records in Bob's notepad. _x__i_ is a position of the _i_-th camel, and _d__i_ is a distance at which the _i_-th camel spitted. Positive values of _d__i_ correspond to the spits right, negative values correspond to the spits left. No two camels may stand in the same position. Output If there are two camels, which spitted at each other, output YES. Otherwise, output NO. Examples Input 2 0 1 1 -1 Output YES Input 3 0 1 1 1 2 -2 Output NO Input 5 2 -10 3 10 0 5 5 -5 10 1 Output YES
1,000
false
false
false
false
false
false
true
false
false
false
9,853
1971A
Problem - 1971A - 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 sortings *800 No tag edit access → Contest materials ") Editorial") xa0— the number of test cases. The only line of each test case contains two space-separated integers $$$x$$$ and $$$y$$$ ($$$0 leq x, y leq 9$$$). Output For each test case, output two integers: the minimum of $$$x$$$ and $$$y$$$, followed by the maximum of $$$x$$$ and $$$y$$$. Example Input 10 1 9 8 4 1 4 3 4 2 0 2 4 6 9 3 3 0 0 9 9 Output 1 9 4 8 1 4 3 4 0 2 2 4 6 9 3 3 0 0 9 9
800
false
false
true
false
false
false
false
false
true
false
465
743C
Problem - 743C - 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 math number theory *1500 No tag edit access → Contest materials ") . Output If the answer exists, print 3 distinct numbers _x_, _y_ and _z_ (1u2009≤u2009_x_,u2009_y_,u2009_z_u2009≤u2009109, _x_u2009≠u2009_y_, _x_u2009≠u2009_z_, _y_u2009≠u2009_z_). Otherwise print -1. If there are multiple answers, print any of them. Examples Input 3 Output 2 7 42 Input 7 Output 7 8 56
1,500
true
false
false
false
false
true
true
false
false
false
6,827
1819C
The fox Yae climbed the tree of the Sacred Sakura. A tree is a connected undirected graph that does not contain cycles. The fox uses her magical powers to move around the tree. Yae can jump from vertex $$$v$$$ to another vertex $$$u$$$ if and only if the distance between these vertices does not exceed $$$2$$$. In other words, in one jump Yae can jump from vertex $$$v$$$ to vertex $$$u$$$ if vertices $$$v$$$ and $$$u$$$ are connected by an edge, or if there exists such vertex $$$w$$$ that vertices $$$v$$$ and $$$w$$$ are connected by an edge, and also vertices $$$u$$$ and $$$w$$$ are connected by an edge. After Yae was able to get the sakura petal, she wondered if there was a cyclic route in the tree $$$v_1, v_2, ldots, v_n$$$ such that: the fox can jump from vertex $$$v_i$$$ to vertex $$$v_{i + 1}$$$, the fox can jump from vertex $$$v_n$$$ to vertex $$$v_1$$$, all $$$v_i$$$ are pairwise distinct. Help the fox determine if the required traversal exists. Input The first line contains one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0—the number of vertices of the tree. Each of the following $$$n - 1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u e v$$$)xa0— vertices connected by an edge. It is guaranteed that these edges form a tree. Output On the first line, print "Yes" (without quotes) if the required route of the tree exists, or "No" (without quotes) otherwise. If the required tree traversal exists, on the second line print $$$n$$$ integers of different integers $$$v_1, v_2, ldots, v_n$$$ ($$$1 le v_i le n$$$)xa0— the vertices of the tree in traversal order. If there are several correct traversals, output any of them. Examples Input 15 1 2 1 3 2 4 2 5 3 6 3 7 4 8 4 9 5 10 5 11 6 12 6 13 7 14 7 15 Note The tree from the first example is shown below. The bold arrows indicate the fox's route. In the second example, any sequence of three different vertices is a correct route, because the fox can jump from any vertex to any vertex. The tree from the third example is shown below. It can be shown that there is no required route for it.
2,400
true
false
true
true
false
true
false
false
false
false
1,363
811D
This is an interactive problem. Vladik has favorite game, in which he plays all his free time. Game field could be represented as _n_u2009×u2009_m_ matrix which consists of cells of three types: «.» — normal cell, player can visit it. «F» — finish cell, player has to finish his way there to win. There is exactly one cell of this type. «*» — dangerous cell, if player comes to this cell, he loses. Initially player is located in the left top cell with coordinates (1,u20091). Player has access to 4 buttons "U", "D", "L", "R", each of them move player up, down, left and right directions respectively. But it’s not that easy! Sometimes friends play game and change functions of buttons. Function of buttons "L" and "R" could have been swapped, also functions of buttons "U" and "D" could have been swapped. Note that functions of buttons can be changed only at the beginning of the game. Help Vladik win the game! Input First line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100) — number of rows and columns respectively. Each of next _n_ lines contains _m_ characters describing corresponding row of field. Set of characters in field is described above. Guaranteed that cell with coordinates (1,u20091) is normal and there is at least one way from initial cell to finish cell without dangerous cells. Interaction You can press buttons no more than 2·_n_·_m_ times. To press a button you should print "U", "D", "L", "R" in new line. It’s necessary to print newline character and flush output. After flushing buffer you should read answer from input data. Answer is the pair of space-separated integers _x_, _y_ — new position of player. In case, if there is no cell in direction of moving, position will not change. If after any move player lost, in other words player move to dangerous cell, then _x_ and _y_ will be equal to u2009-u20091. If after any move player is in finish or dangerous cell, then you should terminate your program. To finish output buffer (i. e. for operation flush) right after printing direction and newline you should do next: fflush(stdout) in C++ System.out.flush() in Java stdout.flush() in Python flush(output) in Pascal read documentation for other languages. Hacks To perform a hack you should use this format: n m swapLR swapUD a_1 a_2 ... a_n Where _n_, _m_xa0— number of rows and columns in game field. _swapLR_ is equal to 1 in case, when directions "L’’ and "R’’ is swapped, and equal to 0 otherwise. _swapUD_ is equal to 1, when directions "U’’ and "D’’ is swapped, and equal to 0 otherwise. _a_1,u2009_a_2,u2009...,u2009_a__n_ — description of corresponding rows of game field. Example Input 4 3 ... **. F*. ... 1 1 1 2 1 3 1 3 2 3 3 3 4 3 4 2 4 1 3 1 Note In first test case all four directions swapped with their opposite directions. Protocol of interaction In more convenient form: This test could be presenter for hack in following way: 4 3 1 1 ... **. F*. ...
2,100
false
false
false
false
false
true
false
false
false
true
6,518
706E
Vasiliy finally got to work, where there is a huge amount of tasks waiting for him. Vasiliy is given a matrix consisting of _n_ rows and _m_ columns and _q_ tasks. Each task is to swap two submatrices of the given matrix. For each task Vasiliy knows six integers _a__i_, _b__i_, _c__i_, _d__i_, _h__i_, _w__i_, where _a__i_ is the index of the row where the top-left corner of the first rectangle is located, _b__i_ is the index of its column, _c__i_ is the index of the row of the top-left corner of the second rectangle, _d__i_ is the index of its column, _h__i_ is the height of the rectangle and _w__i_ is its width. It's guaranteed that two rectangles in one query do not overlap and do not touch, that is, no cell belongs to both rectangles, and no two cells belonging to different rectangles share a side. However, rectangles are allowed to share an angle. Vasiliy wants to know how the matrix will look like after all tasks are performed. Input The first line of the input contains three integers _n_, _m_ and _q_ (2u2009≤u2009_n_,u2009_m_u2009≤u20091000, 1u2009≤u2009_q_u2009≤u200910u2009000)xa0— the number of rows and columns in matrix, and the number of tasks Vasiliy has to perform. Then follow _n_ lines containing _m_ integers _v__i_,u2009_j_ (1u2009≤u2009_v__i_,u2009_j_u2009≤u2009109) eachxa0— initial values of the cells of the matrix. Each of the following _q_ lines contains six integers _a__i_, _b__i_, _c__i_, _d__i_, _h__i_, _w__i_ (1u2009≤u2009_a__i_,u2009_c__i_,u2009_h__i_u2009≤u2009_n_, 1u2009≤u2009_b__i_,u2009_d__i_,u2009_w__i_u2009≤u2009_m_). Output Print _n_ lines containing _m_ integers eachxa0— the resulting matrix. Examples Input 4 4 2 1 1 2 2 1 1 2 2 3 3 4 4 3 3 4 4 1 1 3 3 2 2 3 1 1 3 2 2 Output 4 4 3 3 4 4 3 3 2 2 1 1 2 2 1 1 Input 4 2 1 1 1 1 1 2 2 2 2 1 1 4 1 1 2
2,500
false
false
true
false
true
false
false
false
false
false
6,995
1346A
Hmm, how long has it been since the last color revolution? 5 years?! It's totally the time to make a new one! So the general idea is the following. Division $$$1$$$ should have $$$n_1$$$ participants. Division $$$2$$$ should have $$$n_2$$$ and be exactly $$$k$$$ times bigger than division $$$1$$$ ($$$n_2 = k cdot n_1$$$). Division $$$3$$$ should have $$$n_3 = k cdot n_2$$$ participants. Finally, division $$$4$$$ should have $$$n_4 = k cdot n_3$$$ participants. There are $$$n$$$ participants on Codeforces in total. So $$$n_1 + n_2 + n_3 + n_4$$$ should be exactly equal to $$$n$$$. You know the values of $$$n$$$ and $$$k$$$. You also know that $$$n$$$ and $$$k$$$ are chosen in such a way that there exist values $$$n_1, n_2, n_3$$$ and $$$n_4$$$ such that all the conditions are satisfied. What will be the number of participants in each division ($$$n_1, n_2, n_3$$$ and $$$n_4$$$) after the revolution? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of testcases. Each of the next $$$t$$$ lines contains two integers $$$n$$$ and $$$k$$$ ($$$4 le n le 10^9$$$; $$$1 le k le 500$$$) — the total number of participants on Codeforces and the size multiplier for the corresponding testcase. In each testcase, $$$n$$$ and $$$k$$$ are chosen in such a way that the answer exists. Output For each testcase print four integers $$$n_1, n_2, n_3$$$ and $$$n_4$$$ such that $$$n_2 = k cdot n_1$$$, $$$n_3 = k cdot n_2$$$, $$$n_4 = k cdot n_3$$$ and $$$n_1 + n_2 + n_3 + n_4 = n$$$. Example Input 4 40 3 1200 7 320802005 400 4 1 Output 1 3 9 27 3 21 147 1029 5 2000 800000 320000000 1 1 1 1
1,000
true
false
false
false
false
false
false
false
false
false
3,979
1984F
There is a hidden array $$$a_1, a_2, ldots, a_n$$$ of length $$$n$$$ whose elements are integers between $$$-m$$$ and $$$m$$$, inclusive. You are given an array $$$b_1, b_2, ldots, b_n$$$ of length $$$n$$$ and a string $$$s$$$ of length $$$n$$$ consisting of the characters $$$ exttt{P}$$$, $$$ exttt{S}$$$, and $$$ exttt{?}$$$. For each $$$i$$$ from $$$1$$$ to $$$n$$$ inclusive, we must have: If $$$s_i = exttt{P}$$$, $$$b_i$$$ is the sum of $$$a_1$$$ through $$$a_i$$$. If $$$s_i = exttt{S}$$$, $$$b_i$$$ is the sum of $$$a_i$$$ through $$$a_n$$$. Output the number of ways to replace all $$$ exttt{?}$$$ in $$$s$$$ with either $$$ exttt{P}$$$ or $$$ exttt{S}$$$ such that there exists an array $$$a_1, a_2, ldots, a_n$$$ with elements not exceeding $$$m$$$ by absolute value satisfying the constraints given by the array $$$b_1, b_2, ldots, b_n$$$ and the string $$$s$$$. Since the answer may be large, output it modulo $$$998,244,353$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^3$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 2 cdot 10^3$$$, $$$2 leq m leq 10^{9}$$$)xa0— the length of the hidden array $$$a_1, a_2, ldots, a_n$$$ and the maximum absolute value of an element $$$a_i$$$. The second line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of characters $$$ exttt{P}$$$, $$$ exttt{S}$$$, and $$$ exttt{?}$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$b_i leq m cdot n$$$). The sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^3$$$. Output For each test case, output a single integerxa0— the number of ways to replace all $$$ exttt{?}$$$ in $$$s$$$ with either $$$ exttt{P}$$$ or $$$ exttt{S}$$$ that result in the existence of a valid array $$$a_1, a_2, ldots, a_n$$$, modulo $$$998,244,353$$$. Example Input 6 4 10 PSPP 1 9 8 10 4 1000000000 ???? 1 1 1 4000000000 8 1000000000 ?P?SSP?P -857095623 -1424391899 -851974476 673437144 471253851 -543483033 364945701 -178537332 4 7 PPSS 4 2 1 3 9 20 ????????? 1 2 3 4 5 6 7 8 9 3 1000000000 P?? -145463248 -974068460 -1287458396 Note In the first test case, we can see that the following array satisfies all constraints, thus the answer is $$$1$$$: 1. $$$ exttt{P}$$$xa0— $$${[color{red}{ extbf{1}},3,4,2]}$$$: sum of $$$1$$$. 2. $$$ exttt{S}$$$xa0— $$${[1,color{red}{ extbf{3},4,2}]}$$$: sum of $$$9$$$. 3. $$$ exttt{P}$$$xa0— $$${[color{red}{1,3, extbf{4}},2]}$$$: sum of $$$8$$$. 4. $$$ exttt{P}$$$xa0— $$${[color{red}{1,3,4, extbf{2}}]}$$$: sum of $$$10$$$. In the second test case, it can be shown that no array $$$a$$$ with all $$$a_i leq m = 10^9$$$ satisfies all constraints.
2,500
true
false
false
true
false
false
true
false
false
false
377
1004F
Sonya has an array $$$a_1, a_2, ldots, a_n$$$ consisting of $$$n$$$ integers and also one non-negative integer $$$x$$$. She has to perform $$$m$$$ queries of two types: $$$1$$$ $$$i$$$ $$$y$$$: replace $$$i$$$-th element by value $$$y$$$, i.e. to perform an operation $$$a_{i}$$$ := $$$y$$$; $$$2$$$ $$$l$$$ $$$r$$$: find the number of pairs ($$$L$$$, $$$R$$$) that $$$lleq Lleq Rleq r$$$ and bitwise OR of all integers in the range $$$[L, R]$$$ is at least $$$x$$$ (note that $$$x$$$ is a constant for all queries). Can you help Sonya perform all her queries? Bitwise OR is a binary operation on a pair of non-negative integers. To calculate the bitwise OR of two numbers, you need to write both numbers in binary notation. The result is a number, in binary, which contains a one in each digit if there is a one in the binary notation of at least one of the two numbers. For example, $$$10$$$ OR $$$19$$$ = $$$1010_2$$$ OR $$$10011_2$$$ = $$$11011_2$$$ = $$$27$$$. Input The first line contains three integers $$$n$$$, $$$m$$$, and $$$x$$$ ($$$1leq n, mleq 10^5$$$, $$$0leq x<2^{20}$$$)xa0— the number of numbers, the number of queries, and the constant for all queries. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0leq a_i<2^{20}$$$)xa0— numbers of the array. The following $$$m$$$ lines each describe an query. A line has one of the following formats: $$$1$$$ $$$i$$$ $$$y$$$ ($$$1leq ileq n$$$, $$$0leq y<2^{20}$$$), meaning that you have to replace $$$a_{i}$$$ by $$$y$$$; $$$2$$$ $$$l$$$ $$$r$$$ ($$$1leq lleq rleq n$$$), meaning that you have to find the number of subarrays on the segment from $$$l$$$ to $$$r$$$ that the bitwise OR of all numbers there is at least $$$x$$$. Output For each query of type 2, print the number of subarrays such that the bitwise OR of all the numbers in the range is at least $$$x$$$. Examples Input 4 8 7 0 3 6 1 2 1 4 2 3 4 1 1 7 2 1 4 2 1 3 2 1 1 1 3 0 2 1 4 Input 5 5 7 6 0 3 15 2 2 1 5 1 4 4 2 1 5 2 3 5 2 1 4 Note In the first example, there are an array [$$$0$$$, $$$3$$$, $$$6$$$, $$$1$$$] and queries: 1. on the segment [$$$1ldots4$$$], you can choose pairs ($$$1$$$, $$$3$$$), ($$$1$$$, $$$4$$$), ($$$2$$$, $$$3$$$), ($$$2$$$, $$$4$$$), and ($$$3$$$, $$$4$$$); 2. on the segment [$$$3ldots4$$$], you can choose pair ($$$3$$$, $$$4$$$); 3. the first number is being replacing by $$$7$$$, after this operation, the array will consist of [$$$7$$$, $$$3$$$, $$$6$$$, $$$1$$$]; 4. on the segment [$$$1ldots4$$$], you can choose pairs ($$$1$$$, $$$1$$$), ($$$1$$$, $$$2$$$), ($$$1$$$, $$$3$$$), ($$$1$$$, $$$4$$$), ($$$2$$$, $$$3$$$), ($$$2$$$, $$$4$$$), and ($$$3$$$, $$$4$$$); 5. on the segment [$$$1ldots3$$$], you can choose pairs ($$$1$$$, $$$1$$$), ($$$1$$$, $$$2$$$), ($$$1$$$, $$$3$$$), and ($$$2$$$, $$$3$$$); 6. on the segment [$$$1ldots1$$$], you can choose pair ($$$1$$$, $$$1$$$); 7. the third number is being replacing by $$$0$$$, after this operation, the array will consist of [$$$7$$$, $$$3$$$, $$$0$$$, $$$1$$$]; 8. on the segment [$$$1ldots4$$$], you can choose pairs ($$$1$$$, $$$1$$$), ($$$1$$$, $$$2$$$), ($$$1$$$, $$$3$$$), and ($$$1$$$, $$$4$$$). In the second example, there are an array [$$$6$$$, $$$0$$$, $$$3$$$, $$$15$$$, $$$2$$$] are queries: 1. on the segment [$$$1ldots5$$$], you can choose pairs ($$$1$$$, $$$3$$$), ($$$1$$$, $$$4$$$), ($$$1$$$, $$$5$$$), ($$$2$$$, $$$4$$$), ($$$2$$$, $$$5$$$), ($$$3$$$, $$$4$$$), ($$$3$$$, $$$5$$$), ($$$4$$$, $$$4$$$), and ($$$4$$$, $$$5$$$); 2. the fourth number is being replacing by $$$4$$$, after this operation, the array will consist of [$$$6$$$, $$$0$$$, $$$3$$$, $$$4$$$, $$$2$$$]; 3. on the segment [$$$1ldots5$$$], you can choose pairs ($$$1$$$, $$$3$$$), ($$$1$$$, $$$4$$$), ($$$1$$$, $$$5$$$), ($$$2$$$, $$$4$$$), ($$$2$$$, $$$5$$$), ($$$3$$$, $$$4$$$), and ($$$3$$$, $$$5$$$); 4. on the segment [$$$3ldots5$$$], you can choose pairs ($$$3$$$, $$$4$$$) and ($$$3$$$, $$$5$$$); 5. on the segment [$$$1ldots4$$$], you can choose pairs ($$$1$$$, $$$3$$$), ($$$1$$$, $$$4$$$), ($$$2$$$, $$$4$$$), and ($$$3$$$, $$$4$$$).
2,600
false
false
false
false
true
false
false
false
false
false
5,663
263B
Problem - 263B - 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 *900 No tag edit access → Contest materials ") and (_a__i_,u2009_a__i_) are the opposite corners of the _i_-th square. Vasya wants to find such integer point (with integer coordinates) of the plane, that belongs to exactly _k_ drawn squares. We'll say that a point belongs to a square, if the point is located either inside the square, or on its boundary. Help Vasya find a point that would meet the described limits. Input The first line contains two space-separated integers _n_, _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u200950). The second line contains space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). It is guaranteed that all given squares are distinct. Output In a single line print two space-separated integers _x_ and _y_ (0u2009≤u2009_x_,u2009_y_u2009≤u2009109) — the coordinates of the point that belongs to exactly _k_ squares. If there are multiple answers, you are allowed to print any of them. If there is no answer, print "-1" (without the quotes). Examples Input 4 3 5 1 3 4 Output 2 1 Input 3 1 2 4 1 Output 4 0 Input 4 50 5 1 10 2 Output -1
900
false
true
true
false
false
false
false
false
true
false
8,782
285E
Problem - 285E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp math *2600 No tag edit access → Contest materials ") Tutorial") in permutation _p_1,u2009_p_2,u2009...,u2009_p__n_ good, if _p_
2,600
true
false
false
true
false
false
false
false
false
false
8,692
750H
Pay attention to the output section below, where you will see the information about flushing the output. Bearland is a grid with _h_ rows and _w_ columns. Rows are numbered 1 through _h_ from top to bottom. Columns are numbered 1 through _w_ from left to right. Every cell is either allowed (denoted by '.' in the input) or permanently blocked (denoted by '#'). Bearland is a cold land, where heavy snow often makes travelling harder. Every day a few allowed cells are temporarily blocked by snow. Note, that this block works only on this particular day and next day any of these cells might be allowed again (unless there is another temporarily block). It's possible to move directly between two cells only if they share a side and none of them is permanently or temporarily blocked. Limak is a little polar bear who lives in Bearland. His house is at the top left cell, while his school is at the bottom right cell. Every day Limak should first go from his house to the school and then return back to his house. Since he gets bored easily, he doesn't want to visit the same cell twice on one day, except for the cell with his house, where he starts and ends. If Limak can reach a school and return home avoiding revisiting cells, he calls a day interesting. There are _q_ days you must process, one after another. For each of these days you should check if it's interesting and print "YES" or "NO" on a separate line. In order to be able to read the description of the next day you should print the answer for the previous one and flush the output. It's guaranteed that a day with no cells temporarily blocked by snow would be interesting. It's also guaranteed that cells with Limak's house and school are never blocked (neither permanently or temporarily). Input The first line of the input contains three integers _h_, _w_ and _q_ (2u2009≤u2009_h_,u2009_w_u2009≤u20091000, 1u2009≤u2009_q_u2009≤u200910u2009000)xa0— the height and the width of the grid, and the number of days, respectively. Next _h_ lines describe which cells are allowed and which permanently blocked. The _i_-th line contains a string of length _w_, describing the _i_-th row. Every character is either '.' (denoting an allowed cell) or '#' (denoting a permanently blocked cell). It's guaranteed that a day with no cells temporarily blocked by snow would be interesting. Then, the description of _q_ days is given. The description of the _i_-th day starts with a line containing a single integer _k__i_ (1u2009≤u2009_k__i_u2009≤u200910)xa0— the number of cells that are temporarily blocked by snow on that day. Each of next _k__i_ lines contains two integers _r__i_,u2009_j_ and _c__i_,u2009_j_ (1u2009≤u2009_r__i_,u2009_j_u2009≤u2009_h_, 1u2009≤u2009_c__i_,u2009_j_u2009≤u2009_w_), representing a cell at the intersection of the row _r__i_,u2009_j_ and the column _c__i_,u2009_j_. The given _k__i_ cells are distinct and none of them is permanently blocked. Also, none of them contains Limak's house or school. Output For each of _q_ days print "YES" if that day is interesting, and otherwise print "NO", both without the quotes. After printing an answer, you have to both print the end-of-line character and flush the output. Then you can proceed to the next day. You can get Idleness Limit Exceeded if you don't print anything or if you forget to flush the output. To flush you can use (just after printing a YES/NO and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Examples Input 3 5 4 ..... ..... .#... 1 1 4 1 1 5 2 2 4 3 1 2 1 5 3 3 Input 9 31 5 ............................... ............................... .###.###.#.###...###.###.#.###. ...#.#.#.#.#.......#.#.#.#...#. .###.#.#.#.###...###.#.#.#...#. .#...#.#.#.#.#...#...#.#.#...#. .###.###.#.###...###.###.#...#. ............................... ............................... 5 6 5 2 11 1 14 8 15 2 14 5 2 14 1 14 8 16 6 5 2 11 3 2 2 1 4 8 30 10 3 1 3 11 5 16 7 21 4 16 3 5 7 31 3 9 7 25 3 27 10 3 1 3 9 7 25 3 27 7 21 4 17 3 5 7 31 4 16 3 11 Note In the first sample, there are 4 days. Drawings below show how Limak could go to school and return to his home in the second and the third day (on the left and on the right respectively). A permanently blocked cell is painted red, while cells temporarily blocked by snow are painted orange. Black and green arrows should Limak's way to the school and back to the house respectively. For the second sample, below you can see how the grid looks like on each day, where '#' denotes a cell that is blocked, either temporarily or permanently.
3,500
false
false
false
false
false
false
false
false
false
true
6,786
830B
Vasily has a deck of cards consisting of _n_ cards. There is an integer on each of the cards, this integer is between 1 and 100u2009000, inclusive. It is possible that some cards have the same integers on them. Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on the cards in the deck, then he places the card away. Otherwise, he puts it under the deck and takes the next card from the top, and so on. The process ends as soon as there are no cards in the deck. You can assume that Vasily always knows the minimum number written on some card in the remaining deck, but doesn't know where this card (or these cards) is. You are to determine the total number of times Vasily takes the top card from the deck. Input The first line contains single integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000) — the number of cards in the deck. The second line contains a sequence of _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009100u2009000), where _a__i_ is the number written on the _i_-th from top card in the deck. Output Print the total number of times Vasily takes the top card from the deck. Note In the first example Vasily at first looks at the card with number 6 on it, puts it under the deck, then on the card with number 3, puts it under the deck, and then on the card with number 1. He places away the card with 1, because the number written on it is the minimum among the remaining cards. After that the cards from top to bottom are [2,u20096,u20093]. Then Vasily looks at the top card with number 2 and puts it away. After that the cards from top to bottom are [6,u20093]. Then Vasily looks at card 6, puts it under the deck, then at card 3 and puts it away. Then there is only one card with number 6 on it, and Vasily looks at it and puts it away. Thus, in total Vasily looks at 7 cards.
1,600
false
false
true
false
true
false
false
false
true
false
6,446
1025D
Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees! Recently he found a binary search tree and instinctively nibbled all of its edges, hence messing up the vertices. Dima knows that if Andrew, who has been thoroughly assembling the tree for a long time, comes home and sees his creation demolished, he'll get extremely upset. To not let that happen, Dima has to recover the binary search tree. Luckily, he noticed that any two vertices connected by a direct edge had their greatest common divisor value exceed $$$1$$$. Help Dima construct such a binary search tree or determine that it's impossible. The definition and properties of a binary search tree can be found . The second line features $$$n$$$ distinct integers $$$a_i$$$ ($$$2 le a_i le 10^9$$$)xa0— the values of vertices in ascending order. Output If it is possible to reassemble the binary search tree, such that the greatest common divisor of any two vertices connected by the edge is greater than $$$1$$$, print "Yes" (quotes for clarity). Otherwise, print "No" (quotes for clarity). Examples Input 9 4 8 10 12 15 18 33 44 81 Note The picture below illustrates one of the possible trees for the first example. The picture below illustrates one of the possible trees for the third example.
2,100
true
false
false
true
false
false
true
false
false
false
5,580
1608E
On an endless checkered sheet of paper, $$$n$$$ cells are chosen and colored in three colors, where $$$n$$$ is divisible by $$$3$$$. It turns out that there are exactly $$$frac{n}{3}$$$ marked cells of each of three colors! Find the largest such $$$k$$$ that it's possible to choose $$$frac{k}{3}$$$ cells of each color, remove all other marked cells, and then select three rectangles with sides parallel to the grid lines so that the following conditions hold: No two rectangles can intersect (but they can share a part of the boundary). In other words, the area of intersection of any two of these rectangles must be $$$0$$$. The $$$i$$$-th rectangle contains all the chosen cells of the $$$i$$$-th color and no chosen cells of other colors, for $$$i = 1, 2, 3$$$. Input The first line of the input contains a single integer $$$n$$$ — the number of the marked cells ($$$3 leq n le 10^5$$$, $$$n$$$ is divisible by 3). The $$$i$$$-th of the following $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$c_i$$$ ($$$x_i,y_i leq 10^9$$$; $$$1 leq c_i leq 3$$$), where $$$(x_i, y_i)$$$ are the coordinates of the $$$i$$$-th marked cell and $$$c_i$$$ is its color. It's guaranteed that all cells $$$(x_i, y_i)$$$ in the input are distinct, and that there are exactly $$$frac{n}{3}$$$ cells of each color. Output Output a single integer $$$k$$$ — the largest number of cells you can leave. Examples Input 9 2 3 1 4 1 2 2 1 3 3 4 1 5 3 2 4 4 3 2 4 1 5 2 2 3 5 3 Note In the first sample, it's possible to leave $$$6$$$ cells with indexes $$$1, 5, 6, 7, 8, 9$$$. In the second sample, it's possible to leave $$$3$$$ cells with indexes $$$1, 2, 3$$$.
2,800
false
false
true
false
false
false
false
true
true
false
2,611
2038N
Problem - 2038N - 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 *800 No tag edit access → Contest materials , and the middle character is a comparison symbol (<, = or >). An expression is true if the comparison symbol matches the digits (for example, if the first digit is strictly less than the last digit, the comparison symbol should be <). For example, the expressions 1<3, 4>2, 0=0 are true, while 5>5, 7<3 are not. You are given a string $$$s$$$, which is an expression. Change as few characters as possible so that $$$s$$$ becomes a true expression. Note that if $$$s$$$ is already true, you should leave it as it is. Input The first line contains one integer $$$t$$$ ($$$1 le t le 300$$$) — the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$s = 3$$$, the first and the last characters of $$$s$$$ are digits, the second character is a comparison symbol). Output For each test case, print a string consisting of $$$3$$$ characters — a true expression which can be obtained by changing as few characters as possible in $$$s$$$. If there are multiple answers, print any of them. Example Input 5 3<7 3>7 8=9 0=0 5<3 Output 3<7 8>7 8<9 0=0 0<3
800
false
false
true
false
false
false
false
false
false
false
25
1684A
There is an integer $$$n$$$ without zeros in its decimal representation. Alice and Bob are playing a game with this integer. Alice starts first. They play the game in turns. On her turn, Alice must swap any two digits of the integer that are on different positions. Bob on his turn always removes the last digit of the integer. The game ends when there is only one digit left. You have to find the smallest integer Alice can get in the end, if she plays optimally. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Description of the test cases follows. The first and the only line of each test case contains the integer $$$n$$$ ($$$10 le n le 10^9$$$) — the integer for the game. $$$n$$$ does not have zeros in its decimal representation. Output For each test case output a single integer — the smallest integer Alice can get in the end of the game. Note In the first test case Alice has to swap $$$1$$$ and $$$2$$$. After that Bob removes the last digit, $$$1$$$, so the answer is $$$2$$$. In the second test case Alice can swap $$$3$$$ and $$$1$$$: $$$312$$$. After that Bob deletes the last digit: $$$31$$$. Then Alice swaps $$$3$$$ and $$$1$$$: $$$13$$$ and Bob deletes $$$3$$$, so the answer is $$$1$$$.
800
true
false
false
false
false
true
false
false
false
false
2,173
1760A
Problem - 1760A - 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 sortings *800 No tag edit access → Contest materials xa0— the number of test cases. The description of each test case consists of three distinct integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 leq a, b, c leq 20$$$). Output For each test case, output a single integerxa0— the medium number of the three numbers. Example Input 9 5 2 6 14 3 4 20 2 1 1 2 3 11 19 12 10 8 20 6 20 3 4 1 3 19 8 4 Output 5 4 2 2 12 10 6 3 8
800
false
false
true
false
false
false
false
false
true
false
1,755
1921C
Stepan is a very busy person. Today he needs to send $$$n$$$ messages at moments $$$m_1, m_2, dots m_n$$$ ($$$m_i < m_{i + 1}$$$). Unfortunately, by the moment $$$0$$$, his phone only has $$$f$$$ units of charge left. At the moment $$$0$$$, the phone is turned on. The phone loses $$$a$$$ units of charge for each unit of time it is on. Also, at any moment, Stepan can turn off the phone and turn it on later. This action consumes $$$b$$$ units of energy each time. Consider turning on and off to be instantaneous, so you can turn it on at moment $$$x$$$ and send a message at the same moment, and vice versa, send a message at moment $$$x$$$ and turn off the phone at the same moment. If at any point the charge level drops to $$$0$$$ (becomes $$$le 0$$$), it is impossible to send a message at that moment. Since all messages are very important to Stepan, he wants to know if he can send all the messages without the possibility of charging the phone. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. This is followed by the descriptions of the test cases. The first line of each test case contains four integers $$$n$$$, $$$f$$$, $$$a$$$, and $$$b$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le f, a, b le 10^9$$$)xa0— the number of messages, the initial phone's charge, the charge consumption per unit of time, and the consumption when turned off and on sequentially. The second line of each test case contains $$$n$$$ integers $$$m_1, m_2, dots, m_n$$$ ($$$1 le m_i le 10^9$$$, $$$m_i < m_{i + 1}$$$)xa0— the moments at which messages need to be sent. It is guaranteed that in a test the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output "YES" if Stepan can send all the messages, and "NO" otherwise. You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer. Example Input 6 1 3 1 5 3 7 21 1 3 4 6 10 13 17 20 26 5 10 1 2 1 2 3 4 5 1 1000000000 1000000000 1000000000 1000000000 3 11 9 6 6 8 10 12 621526648 2585904 3566299 51789 61859 71998 73401 247675 298086 606959 663464 735972 806043 806459 919683 Output NO YES YES NO NO YES Note In the first test case of the example, at moment $$$0$$$, the phone's charge is $$$3$$$. When sending a message at moment $$$3$$$ without turning it off, $$$(3 - 0) cdot 1 = 3$$$ units of charge will be spent. In this case, the charge will drop to $$$0$$$ and Stepan will not be able to send the message. When turning off and on, the phone's charge will decrease by $$$5$$$, so it will not be possible to send the message in this way. In the third test case of the example, at moment $$$0$$$, the phone's charge is $$$10$$$. The phone loses $$$1$$$ unit of charge per unit of time, and when turned off and on, it loses $$$2$$$ units of charge. To send all messages, the following actions can be taken: Turn off the phone at moment $$$0$$$ and turn it on at moment $$$1$$$, after which $$$10 - 2 = 8$$$ units of charge will remain; send a message at moment $$$1$$$; send a message at moment $$$2$$$, after which $$$8 - (2 - 1) cdot 1 = 7$$$ units of charge will remain; Turn off the phone at moment $$$2$$$ and turn it on at moment $$$3$$$, after which $$$7 - 2 = 5$$$ units of charge will remain; send a message at moment $$$3$$$; Turn off the phone at moment $$$3$$$ and turn it on at moment $$$4$$$, after which $$$5 - 2 = 3$$$ units of charge will remain; send a message at moment $$$4$$$; Turn off the phone at moment $$$4$$$ and turn it on at moment $$$5$$$, after which $$$3 - 2 = 1$$$ unit of charge will remain; send a message at moment $$$5$$$. The last (sixth) test set of the example may fail if there is an integer overflow in your solution.
900
true
true
false
false
false
false
false
false
false
false
763
493B
Vasya has become interested in wrestling. In wrestling wrestlers use techniques for which they are awarded points by judges. The wrestler who gets the most points wins. When the numbers of points of both wrestlers are equal, the wrestler whose sequence of points is lexicographically greater, wins. If the sequences of the awarded points coincide, the wrestler who performed the last technique wins. Your task is to determine which wrestler won. Input The first line contains number _n_ — the number of techniques that the wrestlers have used (1u2009≤u2009_n_u2009≤u20092·105). The following _n_ lines contain integer numbers _a__i_ (_a__i_u2009≤u2009109, _a__i_u2009≠u20090). If _a__i_ is positive, that means that the first wrestler performed the technique that was awarded with _a__i_ points. And if _a__i_ is negative, that means that the second wrestler performed the technique that was awarded with (u2009-u2009_a__i_) points. The techniques are given in chronological order. Output If the first wrestler wins, print string "first", otherwise print "second" Note Sequence _x_u2009u2009=u2009u2009_x_1_x_2... _x__x_ is lexicographically larger than sequence _y_u2009u2009=u2009u2009_y_1_y_2... _y__y_, if either _x_u2009u2009>u2009u2009_y_ and _x_1u2009u2009=u2009u2009_y_1,u2009u2009_x_2u2009u2009=u2009u2009_y_2,u2009... ,u2009u2009_x__y_u2009u2009=u2009u2009_y__y_, or there is such number _r_ (_r_u2009u2009<u2009u2009_x_,u2009_r_u2009u2009<u2009u2009_y_), that _x_1u2009u2009=u2009u2009_y_1,u2009u2009_x_2u2009u2009=u2009u2009_y_2,u2009u2009... ,u2009u2009_x__r_u2009u2009=u2009u2009_y__r_ and _x__r_u2009u2009+u2009u20091u2009u2009>u2009u2009_y__r_u2009u2009+u2009u20091. We use notation _a_ to denote length of sequence _a_.
1,400
false
false
true
false
false
false
false
false
false
false
7,862
1547D
A sequence of non-negative integers $$$a_1, a_2, dots, a_n$$$ is called growing if for all $$$i$$$ from $$$1$$$ to $$$n - 1$$$ all ones (of binary representation) in $$$a_i$$$ are in the places of ones (of binary representation) in $$$a_{i + 1}$$$ (in other words, $$$a_i :&: a_{i + 1} = a_i$$$, where $$$&$$$ denotes . Then $$$t$$$ test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — length of the sequence $$$x_i$$$. The second line contains $$$n$$$ integers $$$x_1, x_2, dots, x_n$$$ ($$$0 le x_i < 2^{30}$$$) — elements of the sequence $$$x_i$$$. It is guaranteed that the sum of $$$n$$$ overall all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print $$$n$$$ integers $$$y_1, y_2, dots, y_n$$$ ($$$0 le y_i < 2^{30}$$$) — lexicographically minimal sequence such that such that it's co-growing with given sequence $$$x_i$$$.
1,300
false
true
false
false
false
true
false
false
false
false
2,910
1801A
Kirill wants to weave the very beautiful blanket consisting of $$$n imes m$$$ of the same size square patches of some colors. He matched some non-negative integer to each color. Thus, in our problem, the blanket can be considered a $$$B$$$ matrix of size $$$n imes m$$$ consisting of non-negative integers. Kirill considers that the blanket is very beautiful, if for each submatrix $$$A$$$ of size $$$4 imes 4$$$ of the matrix $$$B$$$ is true: $$$A_{11} oplus A_{12} oplus A_{21} oplus A_{22} = A_{33} oplus A_{34} oplus A_{43} oplus A_{44},$$$ $$$A_{13} oplus A_{14} oplus A_{23} oplus A_{24} = A_{31} oplus A_{32} oplus A_{41} oplus A_{42},$$$ where $$$oplus$$$ means — the number of test cases. The single line of each test case contains two integers $$$n$$$ and $$$m$$$ $$$(4 le n, , m le 200)$$$ — the size of matrix $$$B$$$. It is guaranteed that the sum of $$$n cdot m$$$ does not exceed $$$2 cdot 10^5$$$. Output For each test case, in first line output one integer $$$cnt$$$ $$$(1 le cnt le n cdot m)$$$ — the maximum number of different numbers in the matrix. Then output the matrix $$$B$$$ $$$(0 le B_{ij} < 2^{63})$$$ of size $$$n imes m$$$. If there are several correct matrices, it is allowed to output any one. It can be shown that if there exists a matrix with an optimal number of distinct numbers, then there exists among suitable matrices such a $$$B$$$ that $$$(0 le B_{ij} < 2^{63})$$$. Example Output 25 9740 1549 9744 1553 9748 1550 1551 1554 1555 1558 10252 2061 10256 2065 10260 2062 2063 2066 2067 2070 10764 2573 10768 2577 10772 16 3108 3109 3112 3113 3110 3111 3114 3115 3620 3621 3624 3625 3622 3623 3626 3627 24 548 549 552 553 556 557 550 551 554 555 558 559 1060 1061 1064 1065 1068 1069 1062 1063 1066 1067 1070 1071 36 25800 25801 25804 25805 25808 25809 25802 4294993099 25806 4294993103 25810 4294993107 26312 26313 26316 26317 26320 26321 26314 4294993611 26318 4294993615 26322 4294993619 26824 26825 26828 26829 26832 26833 26826 4294994123 26830 4294994127 26834 4294994131 Note In the first test case, there is only 4 submatrix of size $$$4 imes 4$$$. Consider a submatrix whose upper-left corner coincides with the upper-left corner of the matrix $$$B$$$: $$$ left[ {begin{array}{cccc} 9740 & 1549 & 9744 & 1553 1550 & 1551 & 1554 & 1555 10252 & 2061 & 10256 & 2065 2062 & 2063 & 2066 & 2067 end{array} } ight] $$$ $$$9740 oplus 1549 oplus 1550 oplus 1551$$$ $$$=$$$ $$$10256 oplus 2065 oplus 2066 oplus 2067$$$ $$$=$$$ $$$8192$$$; $$$10252 oplus 2061 oplus 2062 oplus 2063$$$ $$$=$$$ $$$9744 oplus 1553 oplus 1554 oplus 1555$$$ $$$=$$$ $$$8192$$$. So, chosen submatrix fits the condition. Similarly, you can make sure that the other three submatrices also fit the condition.
1,600
false
false
false
false
false
true
false
false
false
false
1,466
245A
Polycarpus is a system administrator. There are two servers under his strict guidance — _a_ and _b_. To stay informed about the servers' performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers _x_ and _y_ (_x_u2009+u2009_y_u2009=u200910;xa0_x_,u2009_y_u2009≥u20090). These numbers mean that _x_ packets successfully reached the corresponding server through the network and _y_ packets were lost. Today Polycarpus has performed overall _n_ ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network. Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results. Input The first line contains a single integer _n_ (2u2009≤u2009_n_u2009≤u20091000) — the number of commands Polycarpus has fulfilled. Each of the following _n_ lines contains three integers — the description of the commands. The _i_-th of these lines contains three space-separated integers _t__i_, _x__i_, _y__i_ (1u2009≤u2009_t__i_u2009≤u20092;xa0_x__i_,u2009_y__i_u2009≥u20090;xa0_x__i_u2009+u2009_y__i_u2009=u200910). If _t__i_u2009=u20091, then the _i_-th command is "ping a", otherwise the _i_-th command is "ping b". Numbers _x__i_, _y__i_ represent the result of executing this command, that is, _x__i_ packets reached the corresponding server successfully and _y__i_ packets were lost. It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command. Output In the first line print string "LIVE" (without the quotes) if server _a_ is "alive", otherwise print "DEAD" (without the quotes). In the second line print the state of server _b_ in the similar format. Note Consider the first test case. There 10 packets were sent to server _a_, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server _b_, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Consider the second test case. There were overall 20 packages sent to server _a_, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server _b_, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
800
false
false
true
false
false
false
false
false
false
false
8,856
1607E
The robot is located on a checkered rectangular board of size $$$n imes m$$$ ($$$n$$$ rows, $$$m$$$ columns). The rows in the board are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columnsxa0— from $$$1$$$ to $$$m$$$ from left to right. The robot is able to move from the current cell to one of the four cells adjacent by side. The sequence of commands $$$s$$$ executed by the robot is given. Each command is denoted by one of the symbols 'L', 'R', 'D' or 'U', and triggers the movement to left, right, down or up, respectively. The robot can start its movement in any cell. The robot executes the commands starting from the first one, strictly in the order in which they are listed in $$$s$$$. If the robot moves beyond the edge of the board, it falls and breaks. A command that causes the robot to break is not considered successfully executed. The robot's task is to execute as many commands as possible without falling off the board. For example, on board $$$3 imes 3$$$, if the robot starts a sequence of actions $$$s=$$$"RRDLUU" ("right", "right", "down", "left", "up", "up") from the central cell, the robot will perform one command, then the next command will force him to cross the edge. If the robot starts moving from the cell $$$(2, 1)$$$ (second row, first column) then all commands will be executed successfully and the robot will stop at the cell $$$(1, 2)$$$ (first row, second column). The robot starts from cell $$$(2, 1)$$$ (second row, first column). It moves right, right, down, left, up, and up. In this case it ends in the cell $$$(1, 2)$$$ (first row, second column). Determine the cell from which the robot should start its movement in order to execute as many commands as possible. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. In the description of each test case, the first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 10^6$$$)xa0— the height and width of the field that the robot is located on. The second line of the description is a string $$$s$$$ consisting solely of characters 'L', 'R', 'D' and 'U'xa0— the sequence of commands the robot executes. The string has a length from $$$1$$$ to $$$10^6$$$ commands. It is guaranteed that the total length of $$$s$$$ over all test cases does not exceed $$$10^6$$$. Output Print $$$t$$$ lines, each of which contains the answer to the corresponding test case. The answer to the test case are two integers $$$r$$$ ($$$1 leq r leq n$$$) and $$$c$$$ ($$$1 leq c leq m$$$), separated by a spacexa0— the coordinates of the cell (row number and column number) from which the robot should start moving to perform as many commands as possible. If there are several such cells, you may output any of them.
1,600
false
false
true
false
false
false
false
false
false
false
2,619
1340A
Denis was very sad after Nastya rejected him. So he decided to walk through the gateways to have some fun. And luck smiled at him! When he entered the first courtyard, he met a strange man who was selling something. Denis bought a mysterious item and it was... Random permutation generator! Denis could not believed his luck. When he arrived home, he began to study how his generator works and learned the algorithm. The process of generating a permutation consists of $$$n$$$ steps. At the $$$i$$$-th step, a place is chosen for the number $$$i$$$ $$$(1 leq i leq n)$$$. The position for the number $$$i$$$ is defined as follows: For all $$$j$$$ from $$$1$$$ to $$$n$$$, we calculate $$$r_j$$$ xa0— the minimum index such that $$$j leq r_j leq n$$$, and the position $$$r_j$$$ is not yet occupied in the permutation. If there are no such positions, then we assume that the value of $$$r_j$$$ is not defined. For all $$$t$$$ from $$$1$$$ to $$$n$$$, we calculate $$$count_t$$$ xa0— the number of positions $$$1 leq j leq n$$$ such that $$$r_j$$$ is defined and $$$r_j = t$$$. Consider the positions that are still not occupied by permutation and among those we consider the positions for which the value in the $$$count$$$ array is maximum. The generator selects one of these positions for the number $$$i$$$. The generator can choose any position. Let's have a look at the operation of the algorithm in the following example: Let $$$n = 5$$$ and the algorithm has already arranged the numbers $$$1, 2, 3$$$ in the permutation. Consider how the generator will choose a position for the number $$$4$$$: The values of $$$r$$$ will be $$$r = [3, 3, 3, 4, imes]$$$, where $$$ imes$$$ means an indefinite value. Then the $$$count$$$ values will be $$$count = [0, 0, 3, 1, 0]$$$. There are only two unoccupied positions in the permutation: $$$3$$$ and $$$4$$$. The value in the $$$count$$$ array for position $$$3$$$ is $$$3$$$, for position $$$4$$$ it is $$$1$$$. The maximum value is reached only for position $$$3$$$, so the algorithm will uniquely select this position for number $$$4$$$. Satisfied with his purchase, Denis went home. For several days without a break, he generated permutations. He believes that he can come up with random permutations no worse than a generator. After that, he wrote out the first permutation that came to mind $$$p_1, p_2, ldots, p_n$$$ and decided to find out if it could be obtained as a result of the generator. Unfortunately, this task was too difficult for him, and he asked you for help. It is necessary to define whether the written permutation could be obtained using the described algorithm if the generator always selects the position Denis needs. Input The first line contains a single integer $$$t$$$ $$$(1 leq t leq 10^5)$$$ xa0— the number of test cases. Then the descriptions of the test cases follow. The first line of the test case contains a single integer $$$n$$$ $$$(1 leq n leq 10^5)$$$ xa0— the size of the permutation. The second line of the test case contains $$$n$$$ different integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 leq p_i leq n$$$) xa0— the permutation written by Denis. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$. Note Let's simulate the operation of the generator in the first test. At the $$$1$$$ step, $$$r = [1, 2, 3, 4, 5], count = [1, 1, 1, 1, 1]$$$. The maximum value is reached in any free position, so the generator can choose a random position from $$$1$$$ to $$$5$$$. In our example, it chose $$$5$$$. At the $$$2$$$ step, $$$r = [1, 2, 3, 4, imes], count = [1, 1, 1, 1, 0]$$$. The maximum value is reached in positions from $$$1$$$ to $$$4$$$, so the generator can choose a random position among them. In our example, it chose $$$1$$$. At the $$$3$$$ step, $$$r = [2, 2, 3, 4, imes], count = [0, 2, 1, 1, 0]$$$. The maximum value is $$$2$$$ and is reached only at the $$$2$$$ position, so the generator will choose this position. At the $$$4$$$ step, $$$r = [3, 3, 3, 4, imes], count = [0, 0, 3, 1, 0]$$$. The maximum value is $$$3$$$ and is reached only at the $$$3$$$ position, so the generator will choose this position. At the $$$5$$$ step, $$$r = [4, 4, 4, 4, imes], count = [0, 0, 0, 4, 0]$$$. The maximum value is $$$4$$$ and is reached only at the $$$4$$$ position, so the generator will choose this position. In total, we got a permutation of $$$2, 3, 4, 5, 1$$$, that is, a generator could generate it.
1,500
false
true
true
false
true
false
true
false
false
false
4,007
1628C
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 767 (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 constructive algorithms greedy implementation interactive math *2300 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST C. Grid Xor time limit per test2 seconds memory limit per test256 megabytes Note: The XOR-sum of set $$${s_1,s_2,ldots,s_m}$$$ is defined as $$$s_1 oplus s_2 oplus ldots oplus s_m$$$, where $$$oplus$$$ denotes the bitwise XOR operation. After almost winning IOI, Victor bought himself an $$$n imes n$$$ grid containing integers in each cell. $$$n$$$ is an even integer. The integer in the cell in the $$$i$$$-th row and $$$j$$$-th column is $$$a_{i,j}$$$. Sadly, Mihai stole the grid from Victor and told him he would return it with only one condition: Victor has to tell Mihai the XOR-sum of all the integers in the whole grid. Victor doesn't remember all the elements of the grid, but he remembers some information about it: For each cell, Victor remembers the XOR-sum of all its neighboring cells. Two cells are considered neighbors if they share an edge — in other words, for some integers $$$1 le i, j, k, l le n$$$, the cell in the $$$i$$$-th row and $$$j$$$-th column is a neighbor of the cell in the $$$k$$$-th row and $$$l$$$-th column if $$$i - k = 1$$$ and $$$j = l$$$, or if $$$i = k$$$ and $$$j - l = 1$$$. To get his grid back, Victor is asking you for your help. Can you use the information Victor remembers to find the XOR-sum of the whole grid? It can be proven that the answer is unique. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single even integer $$$n$$$ ($$$2 leq n leq 1000$$$) — the size of the grid. Then follows $$$n$$$ lines, each containing $$$n$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th of these lines represents the XOR-sum of the integers in all the neighbors of the cell in the $$$i$$$-th row and $$$j$$$-th column. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$1000$$$ and in the original grid $$$0 leq a_{i, j} leq 2^{30} - 1$$$. Hack Format To hack a solution, use the following format: The first line should contain a single integer t ($$$1 le t le 100$$$) — the number of test cases. The first line of each test case should contain a single even integer $$$n$$$ ($$$2 leq n leq 1000$$$) — the size of the grid. Then $$$n$$$ lines should follow, each containing $$$n$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th of these lines is $$$a_{i,j}$$$ in Victor's original grid. The values in the grid should be integers in the range $$$[0, 2^{30}-1]$$$ The sum of $$$n$$$ over all test cases must not exceed $$$1000$$$. Output For each test case, output a single integer — the XOR-sum of the whole grid. Example input 3 2 1 5 5 1 4 1 14 8 9 3 1 5 9 4 13 11 1 1 15 4 11 4 2 4 1 6 3 7 3 10 15 9 4 2 12 7 15 1 output 4 9 5 Note For the first test case, one possibility for Victor's original grid is: $$$1$$$ $$$3$$$ $$$2$$$ $$$4$$$ For the second test case, one possibility for Victor's original grid is: $$$3$$$ $$$8$$$ $$$8$$$ $$$5$$$ $$$9$$$ $$$5$$$ $$$5$$$ $$$1$$$ $$$5$$$ $$$5$$$ $$$9$$$ $$$9$$$ $$$8$$$ $$$4$$$ $$$2$$$ $$$9$$$ For the third test case, one possibility for Victor's original grid is: $$$4$$$ $$$3$$$ $$$2$$$ $$$1$$$ $$$1$$$ $$$2$$$ $$$3$$$ $$$4$$$ $$$5$$$ $$$6$$$ $$$7$$$ $$$8$$$ $$$8$$$ $$$9$$$ $$$9$$$ $$$1$$$ Codeforces (c)
2,300
true
true
true
false
false
true
false
false
false
false
2,473
1970C2
This is the medium version of the problem. The difference in this version is that $$$t=1$$$ and we work on trees. Ron and Hermione are playing a game on a tree of $$$n$$$ nodes that are initially inactive. The game consists of $$$t$$$ rounds, each of which starts with a stone on exactly one node, which is considered as activated. A move consists of picking an inactive neighbor of the node with a stone on it and moving the stone there (thus activating this neighbor). Ron makes the first move, after which he alternates with Hermione until no valid move is available. The player that cannot make a move loses the round. If both players play optimally, who wins each round of this game? Note that all the rounds are played with the same tree; only the starting node changes. Moreover, after each round, all active nodes are considered inactive again. Input The first line contains integers $$$n$$$ ($$$2 leq n leq 2 imes 10^5$$$), $$$t$$$ ($$$t=1$$$), the number of nodes in the tree and the number of rounds, respectively. The next $$$n-1$$$ lines contain two integers $$$1 leq u, v leq n$$$ each, corresponding to an edge of the tree. The next line contains $$$t$$$ integers $$$1 leq u_1 , dots, u_t leq n$$$, corresponding to the node where the stone is initially put. Output The output consists of $$$t=1$$$ line which is either "Ron" or "Hermione".
1,700
false
false
false
true
false
false
false
false
false
false
479
1019D
Problem - 1019D - 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 geometry sortings *2700 No tag edit access → Contest materials ") xa0— the number of cities on the map and the area of the triangle to be found. The next $$$n$$$ lines contain descriptions of the cities, one per line. Each city is described by its integer coordinates $$$x_i$$$, $$$y_i$$$ ($$$-10^9 le x_i, y_i le 10^9$$$). It is guaranteed that all cities are located at distinct points. It is also guaranteed that no three cities lie on the same line. Output If the solution doesn't existxa0— print «No». Otherwise, print «Yes», followed by three pairs of coordinates $$$(x, y)$$$xa0— the locations of the three cities, which form the triangle of area $$$S$$$. Examples Input 3 7 0 0 3 0 0 4 Output No Input 4 3 0 0 2 0 1 2 1 3 Output Yes 0 0 1 3 2 0
2,700
false
false
false
false
false
false
false
true
true
false
5,594
1237H
You have two strings $$$a$$$ and $$$b$$$ of equal even length $$$n$$$ consisting of characters 0 and 1. We're in the endgame now. To finally make the universe perfectly balanced, you need to make strings $$$a$$$ and $$$b$$$ equal. In one step, you can choose any prefix of $$$a$$$ of even length and reverse it. Formally, if $$$a = a_1 a_2 ldots a_n$$$, you can choose a positive even integer $$$p le n$$$ and set $$$a$$$ to $$$a_p a_{p-1} ldots a_1 a_{p+1} a_{p+2} ldots a_n$$$. Find a way to make $$$a$$$ equal to $$$b$$$ using at most $$$n + 1$$$ reversals of the above kind, or determine that such a way doesn't exist. The number of reversals doesn't have to be minimized. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 2000$$$), denoting the number of test cases. Each test case consists of two lines. The first line contains a string $$$a$$$ of length $$$n$$$, and the second line contains a string $$$b$$$ of the same length ($$$2 le n le 4000$$$; $$$n bmod 2 = 0$$$). Both strings consist of characters 0 and 1. The sum of $$$n$$$ over all $$$t$$$ test cases doesn't exceed $$$4000$$$. Output For each test case, if it's impossible to make $$$a$$$ equal to $$$b$$$ in at most $$$n + 1$$$ reversals, output a single integer $$$-1$$$. Otherwise, output an integer $$$k$$$ ($$$0 le k le n + 1$$$), denoting the number of reversals in your sequence of steps, followed by $$$k$$$ even integers $$$p_1, p_2, ldots, p_k$$$ ($$$2 le p_i le n$$$; $$$p_i bmod 2 = 0$$$), denoting the lengths of prefixes of $$$a$$$ to be reversed, in chronological order. Note that $$$k$$$ doesn't have to be minimized. If there are many solutions, output any of them. Example Input 4 0100011011 1101011000 10101010 10101010 0011 1001 100011 110010 Output 3 6 4 10 0 -1 7 2 6 2 6 2 2 6 Note In the first test case, string $$$a$$$ changes as follows: after the first reversal: 1000101011; after the second reversal: 0001101011; after the third reversal: 1101011000.
3,300
false
false
false
false
false
true
false
false
false
false
4,504
225C
You've got an _n_u2009×u2009_m_ pixel picture. Each pixel can be white or black. Your task is to change the colors of as few pixels as possible to obtain a barcode picture. A picture is a barcode if the following conditions are fulfilled: All pixels in each column are of the same color. The width of each monochrome vertical line is at least _x_ and at most _y_ pixels. In other words, if we group all neighbouring columns of the pixels with equal color, the size of each group can not be less than _x_ or greater than _y_. Input The first line contains four space-separated integers _n_, _m_, _x_ and _y_ (1u2009≤u2009_n_,u2009_m_,u2009_x_,u2009_y_u2009≤u20091000;xa0_x_u2009≤u2009_y_). Then follow _n_ lines, describing the original image. Each of these lines contains exactly _m_ characters. Character "." represents a white pixel and "#" represents a black pixel. The picture description doesn't have any other characters besides "." and "#". Output In the first line print the minimum number of pixels to repaint. It is guaranteed that the answer exists. Examples Input 6 5 1 2 ##.#. .###. ###.. #...# .##.# ###.. Note In the first test sample the picture after changing some colors can looks as follows: .##.. .##.. .##.. .##.. .##.. .##.. In the second test sample the picture after changing some colors can looks as follows: .#.#. .#.#.
1,700
false
false
false
true
false
false
false
false
false
false
8,938
1264C
Creatnx has $$$n$$$ mirrors, numbered from $$$1$$$ to $$$n$$$. Every day, Creatnx asks exactly one mirror "Am I beautiful?". The $$$i$$$-th mirror will tell Creatnx that he is beautiful with probability $$$frac{p_i}{100}$$$ for all $$$1 le i le n$$$. Some mirrors are called checkpoints. Initially, only the $$$1$$$st mirror is a checkpoint. It remains a checkpoint all the time. Creatnx asks the mirrors one by one, starting from the $$$1$$$-st mirror. Every day, if he asks $$$i$$$-th mirror, there are two possibilities: The $$$i$$$-th mirror tells Creatnx that he is beautiful. In this case, if $$$i = n$$$ Creatnx will stop and become happy, otherwise he will continue asking the $$$i+1$$$-th mirror next day; In the other case, Creatnx will feel upset. The next day, Creatnx will start asking from the checkpoint with a maximal number that is less or equal to $$$i$$$. There are some changes occur over time: some mirrors become new checkpoints and some mirrors are no longer checkpoints. You are given $$$q$$$ queries, each query is represented by an integer $$$u$$$: If the $$$u$$$-th mirror isn't a checkpoint then we set it as a checkpoint. Otherwise, the $$$u$$$-th mirror is no longer a checkpoint. After each query, you need to calculate xa0— the number of mirrors and queries. The second line contains $$$n$$$ integers: $$$p_1, p_2, ldots, p_n$$$ ($$$1 leq p_i leq 100$$$). Each of $$$q$$$ following lines contains a single integer $$$u$$$ ($$$2 leq u leq n$$$)xa0— next query. Note In the first test after the first query, the first and the second mirrors are checkpoints. Creatnx will ask the first mirror until it will say that he is beautiful, after that he will ask the second mirror until it will say that he is beautiful because the second mirror is a checkpoint. After that, he will become happy. Probabilities that the mirrors will say, that he is beautiful are equal to $$$frac{1}{2}$$$. So, the expected number of days, until one mirror will say, that he is beautiful is equal to $$$2$$$ and the answer will be equal to $$$4 = 2 + 2$$$.
2,400
false
false
false
false
true
false
false
false
false
false
4,379
1783A
An array $$$a$$$ is called ugly if it contains at least one element which is equal to the sum of all elements before it. If the array is not ugly, it is beautiful. For example: the array $$$[6, 3, 9, 6]$$$ is ugly: the element $$$9$$$ is equal to $$$6 + 3$$$; the array $$$[5, 5, 7]$$$ is ugly: the element $$$5$$$ (the second one) is equal to $$$5$$$; the array $$$[8, 4, 10, 14]$$$ is beautiful: $$$8 e 0$$$, $$$4 e 8$$$, $$$10 e 8 + 4$$$, $$$14 e 8 + 4 + 10$$$, so there is no element which is equal to the sum of all elements before it. You are given an array $$$a$$$ such that $$$1 le a_1 le a_2 le dots le a_n le 100$$$. You have to reorder the elements of $$$a$$$ in such a way that the resulting array is beautiful. Note that you are not allowed to insert new elements or erase existing ones, you can only change the order of elements of $$$a$$$. You are allowed to keep the array $$$a$$$ unchanged, if it is beautiful. Input The first line contains one integer $$$t$$$ ($$$1 le t le 2000$$$) — the number of test cases. Each test case consists of two lines. The first line contains one integer $$$n$$$ ($$$2 le n le 50$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_1 le a_2 le dots le a_n le 100$$$). Output For each test case, print the answer as follows: if it is impossible to reorder the elements of $$$a$$$ in such a way that it becomes beautiful, print NO; otherwise, in the first line, print YES. In the second line, print $$$n$$$ integers — any beautiful array which can be obtained from $$$a$$$ by reordering its elements. If there are multiple such arrays, print any of them. Example Input 4 4 3 3 6 6 2 10 10 5 1 2 3 4 5 3 1 4 4 Output YES 3 6 3 6 NO YES 2 4 1 5 3 YES 1 4 4
800
true
false
false
false
false
true
false
false
true
false
1,579
1355B
Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties... Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter $$$e_i$$$xa0— his inexperience. Russell decided that an explorer with inexperience $$$e$$$ can only join the group of $$$e$$$ or more people. Now Russell needs to figure out how many groups he can organize. It's not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him. Input The first line contains the number of independent test cases $$$T$$$($$$1 leq T leq 2 cdot 10^5$$$). Next $$$2T$$$ lines contain description of test cases. The first line of description of each test case contains the number of young explorers $$$N$$$ ($$$1 leq N leq 2 cdot 10^5$$$). The second line contains $$$N$$$ integers $$$e_1, e_2, ldots, e_N$$$ ($$$1 leq e_i leq N$$$), where $$$e_i$$$ is the inexperience of the $$$i$$$-th explorer. It's guaranteed that sum of all $$$N$$$ doesn't exceed $$$3 cdot 10^5$$$. Output Print $$$T$$$ numbers, each number on a separate line. In $$$i$$$-th line print the maximum number of groups Russell can form in $$$i$$$-th test case. Example Input 2 3 1 1 1 5 2 3 1 2 2 Note In the first example we can organize three groups. There will be only one explorer in each group. It's correct because inexperience of each explorer equals to $$$1$$$, so it's not less than the size of his group. In the second example we can organize two groups. Explorers with inexperience $$$1$$$, $$$2$$$ and $$$3$$$ will form the first group, and the other two explorers with inexperience equal to $$$2$$$ will form the second group. This solution is not unique. For example, we can form the first group using the three explorers with inexperience equal to $$$2$$$, and the second group using only one explorer with inexperience equal to $$$1$$$. In this case the young explorer with inexperience equal to $$$3$$$ will not be included in any group.
1,200
false
true
false
true
false
false
false
false
true
false
3,930
1995A
Vitaly503 is given a checkered board with a side of $$$n$$$ and $$$k$$$ chips. He realized that all these $$$k$$$ chips need to be placed on the cells of the board (no more than one chip can be placed on a single cell). Let's denote the cell in the $$$i$$$-th row and $$$j$$$-th column as $$$(i ,j)$$$. A diagonal is the set of cells for which the value $$$i + j$$$ is the same. For example, cells $$$(3, 1)$$$, $$$(2, 2)$$$, and $$$(1, 3)$$$ lie on the same diagonal, but $$$(1, 2)$$$ and $$$(2, 3)$$$ do not. A diagonal is called occupied if it contains at least one chip. Determine what is the minimum possible number of occupied diagonals among all placements of $$$k$$$ chips. Input Each test consists of several sets of input data. The first line contains a single integer $$$t$$$ ($$$1 le t le 500$$$)xa0— the number of sets of input data. Then follow the descriptions of the sets of input data. The only line of each set of input data contains two integers $$$n$$$, $$$k$$$ ($$$1 le n le 100, 0 le k le n^2$$$)xa0— the side of the checkered board and the number of available chips, respectively. Output For each set of input data, output a single integerxa0— the minimum number of occupied diagonals with at least one chip that he can get after placing all $$$k$$$ chips. Example Input 7 1 0 2 2 2 3 2 4 10 50 100 239 3 9 Note In the first test case, there are no chips, so 0 diagonals will be occupied. In the second test case, both chips can be placed on diagonal $$$(2, 1), (1, 2)$$$, so the answer is 1. In the third test case, 3 chips can't be placed on one diagonal, but placing them on $$$(1, 2), (2, 1), (1, 1)$$$ makes 2 diagonals occupied. In the 7th test case, chips will occupy all 5 diagonals in any valid placing.
800
true
true
true
false
false
false
true
false
false
false
297
1158C
Vasya has written some permutation $$$p_1, p_2, ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, so for all $$$1 leq i leq n$$$ it is true that $$$1 leq p_i leq n$$$ and all $$$p_1, p_2, ldots, p_n$$$ are different. After that he wrote $$$n$$$ numbers $$$next_1, next_2, ldots, next_n$$$. The number $$$next_i$$$ is equal to the minimal index $$$i < j leq n$$$, such that $$$p_j > p_i$$$. If there is no such $$$j$$$ let's let's define as $$$next_i = n + 1$$$. In the evening Vasya went home from school and due to rain, his notebook got wet. Now it is impossible to read some written numbers. Permutation and some values $$$next_i$$$ are completely lost! If for some $$$i$$$ the value $$$next_i$$$ is lost, let's say that $$$next_i = -1$$$. You are given numbers $$$next_1, next_2, ldots, next_n$$$ (maybe some of them are equal to $$$-1$$$). Help Vasya to find such permutation $$$p_1, p_2, ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, that he can write it to the notebook and all numbers $$$next_i$$$, which are not equal to $$$-1$$$, will be correct. Input The first line contains one integer $$$t$$$xa0— the number of test cases ($$$1 leq t leq 100,000$$$). Next $$$2 cdot t$$$ lines contains the description of test cases,two lines for each. The first line contains one integer $$$n$$$xa0— the length of the permutation, written by Vasya ($$$1 leq n leq 500,000$$$). The second line contains $$$n$$$ integers $$$next_1, next_2, ldots, next_n$$$, separated by spaces ($$$next_i = -1$$$ or $$$i < next_i leq n + 1$$$). It is guaranteed, that the sum of $$$n$$$ in all test cases doesn't exceed $$$500,000$$$. In hacks you can only use one test case, so $$$T = 1$$$. Output Print $$$T$$$ lines, in $$$i$$$-th of them answer to the $$$i$$$-th test case. If there is no such permutations $$$p_1, p_2, ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, that Vasya could write, print the only number $$$-1$$$. In the other case print $$$n$$$ different integers $$$p_1, p_2, ldots, p_n$$$, separated by spaces ($$$1 leq p_i leq n$$$). All defined values of $$$next_i$$$ which are not equal to $$$-1$$$ should be computed correctly $$$p_1, p_2, ldots, p_n$$$ using defenition given in the statement of the problem. If there exists more than one solution you can find any of them. Example Input 6 3 2 3 4 2 3 3 3 -1 -1 -1 3 3 4 -1 1 2 4 4 -1 4 5 Output 1 2 3 2 1 2 1 3 -1 1 3 2 1 4 Note In the first test case for permutation $$$p = [1, 2, 3]$$$ Vasya should write $$$next = [2, 3, 4]$$$, because each number in permutation is less than next. It's easy to see, that it is the only satisfying permutation. In the third test case, any permutation can be the answer because all numbers $$$next_i$$$ are lost. In the fourth test case, there is no satisfying permutation, so the answer is $$$-1$$$.
2,100
true
true
false
false
true
true
false
false
true
true
4,913
1209H
Airports often use moving walkways to help you walking big distances faster. Each such walkway has some speed that effectively increases your speed. You can stand on such a walkway and let it move you, or you could also walk and then your effective speed is your walking speed plus walkway's speed. Limak wants to get from point $$$0$$$ to point $$$L$$$ on a straight line. There are $$$n$$$ disjoint walkways in between. The $$$i$$$-th walkway is described by two integers $$$x_i$$$ and $$$y_i$$$ and a real value $$$s_i$$$. The $$$i$$$-th walkway starts at $$$x_i$$$, ends at $$$y_i$$$ and has speed $$$s_i$$$. Every walkway is located inside the segment $$$[0, L]$$$ and no two walkways have positive intersection. However, they can touch by endpoints. Limak needs to decide how to distribute his energy. For example, it might make more sense to stand somewhere (or to walk slowly) to then have a lot of energy to walk faster. Limak's initial energy is $$$0$$$ and it must never drop below that value. At any moment, he can walk with any speed $$$v$$$ in the interval $$$[0, 2]$$$ and it will cost him $$$v$$$ energy per second, but he continuously recovers energy with speed of $$$1$$$ energy per second. So, when he walks with speed $$$v$$$, his energy increases by $$$(1-v)$$$. Note that negative value would mean losing energy. In particular, he can walk with speed $$$1$$$ and this won't change his energy at all, while walking with speed $$$0.77$$$ effectively gives him $$$0.23$$$ energy per second. Limak can choose his speed arbitrarily (any real value in interval $$$[0, 2]$$$) at every moment of time (including the moments when he is located on non-integer positions). Everything is continuous (non-discrete). What is the fastest time Limak can get from $$$0$$$ to $$$L$$$? Input The first line contains integers $$$n$$$ and $$$L$$$ ($$$1 le n le 200,000$$$, $$$1 le L le 10^9$$$), the number of walkways and the distance to walk. Each of the next $$$n$$$ lines contains integers $$$x_i$$$, $$$y_i$$$ and real value $$$s_i$$$ ($$$0 le x_i < y_i le L$$$, $$$0.1 le s_i le 10.0$$$). The value $$$s_i$$$ is given with at most $$$9$$$ digits after decimal point. It's guaranteed, that no two walkways have a positive intersection. The walkways are listed from left to right. That is, $$$y_i le x_{i + 1}$$$ for $$$1 le i le n - 1$$$. Note The drawings show the first two examples. In the first one, there is a walkway from $$$0$$$ to $$$2$$$ with speed $$$2.0$$$ and Limak wants to get to point $$$5$$$. The second example has a walkway from $$$2$$$ to $$$4$$$ with speed $$$0.91$$$. In the first example, one of optimal strategies is as follows. Get from $$$0$$$ to $$$2$$$ by standing still on the walkway. It moves you with speed $$$2$$$ so it takes $$$1$$$ second and you save up $$$1$$$ energy. Get from $$$2$$$ to $$$4$$$ by walking with max speed $$$2$$$ for next $$$1$$$ second. It takes $$$1$$$ second again and the energy drops to $$$0$$$. Get from $$$4$$$ to $$$5$$$ by walking with speed $$$1$$$. It takes $$$1$$$ second and the energy stays constant at the value $$$0$$$. The total time is $$$1 + 1 + 1 = 3$$$.
3,300
true
true
false
false
true
false
false
false
false
false
4,637
1091A
Alice and Bob are decorating a Christmas Tree. Alice wants only $$$3$$$ types of ornaments to be used on the Christmas Tree: yellow, blue and red. They have $$$y$$$ yellow ornaments, $$$b$$$ blue ornaments and $$$r$$$ red ornaments. In Bob's opinion, a Christmas Tree will be beautiful if: the number of blue ornaments used is greater by exactly $$$1$$$ than the number of yellow ornaments, and the number of red ornaments used is greater by exactly $$$1$$$ than the number of blue ornaments. That is, if they have $$$8$$$ yellow ornaments, $$$13$$$ blue ornaments and $$$9$$$ red ornaments, we can choose $$$4$$$ yellow, $$$5$$$ blue and $$$6$$$ red ornaments ($$$5=4+1$$$ and $$$6=5+1$$$). Alice wants to choose as many ornaments as possible, but she also wants the Christmas Tree to be beautiful according to Bob's opinion. In the example two paragraphs above, we would choose $$$7$$$ yellow, $$$8$$$ blue and $$$9$$$ red ornaments. If we do it, we will use $$$7+8+9=24$$$ ornaments. That is the maximum number. Since Alice and Bob are busy with preparing food to the New Year's Eve, they are asking you to find out the maximum number of ornaments that can be used in their beautiful Christmas Tree! It is guaranteed that it is possible to choose at least $$$6$$$ ($$$1+2+3=6$$$) ornaments. Input The only line contains three integers $$$y$$$, $$$b$$$, $$$r$$$ ($$$1 leq y leq 100$$$, $$$2 leq b leq 100$$$, $$$3 leq r leq 100$$$)xa0— the number of yellow, blue and red ornaments. It is guaranteed that it is possible to choose at least $$$6$$$ ($$$1+2+3=6$$$) ornaments. Note In the first example, the answer is $$$7+8+9=24$$$. In the second example, the answer is $$$2+3+4=9$$$.
800
true
false
true
false
false
false
true
false
false
false
5,259
1382A
You are given two arrays of integers $$$a_1,ldots,a_n$$$ and $$$b_1,ldots,b_m$$$. Your task is to find a non-empty array $$$c_1,ldots,c_k$$$ that is a subsequence of $$$a_1,ldots,a_n$$$, and also a subsequence of $$$b_1,ldots,b_m$$$. If there are multiple answers, find one of the smallest possible length. If there are still multiple of the smallest possible length, find any. If there are no such arrays, you should report about it. A sequence $$$a$$$ is a subsequence of a sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero) elements. For example, $$$[3,1]$$$ is a subsequence of $$$[3,2,1]$$$ and $$$[4,3,1]$$$, but not a subsequence of $$$[1,3,3,7]$$$ and $$$[3,10,4]$$$. Input The first line contains a single integer $$$t$$$ ($$$1le tle 1000$$$) xa0— the number of test cases. Next $$$3t$$$ lines contain descriptions of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1le n,mle 1000$$$) xa0— the lengths of the two arrays. The second line of each test case contains $$$n$$$ integers $$$a_1,ldots,a_n$$$ ($$$1le a_ile 1000$$$) xa0— the elements of the first array. The third line of each test case contains $$$m$$$ integers $$$b_1,ldots,b_m$$$ ($$$1le b_ile 1000$$$) xa0— the elements of the second array. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ across all test cases does not exceed $$$1000$$$ ($$$sumlimits_{i=1}^t n_i, sumlimits_{i=1}^t m_ile 1000$$$). Output For each test case, output "YES" if a solution exists, or "NO" otherwise. If the answer is "YES", on the next line output an integer $$$k$$$ ($$$1le kle 1000$$$) xa0— the length of the array, followed by $$$k$$$ integers $$$c_1,ldots,c_k$$$ ($$$1le c_ile 1000$$$) xa0— the elements of the array. If there are multiple solutions with the smallest possible $$$k$$$, output any. Example Input 5 4 5 10 8 6 4 1 2 3 4 5 1 1 3 3 1 1 3 2 5 3 1000 2 2 2 3 3 1 5 5 5 1 2 3 4 5 1 2 3 4 5 Output YES 1 4 YES 1 3 NO YES 1 3 YES 1 2 Note In the first test case, $$$[4]$$$ is a subsequence of $$$[10, 8, 6, 4]$$$ and $$$[1, 2, 3, 4, 5]$$$. This array has length $$$1$$$, it is the smallest possible length of a subsequence of both $$$a$$$ and $$$b$$$. In the third test case, no non-empty subsequences of both $$$[3]$$$ and $$$[2]$$$ exist, so the answer is "NO".
800
false
false
false
false
false
false
true
false
false
false
3,758
551A
Professor GukiZ likes programming contests. He especially likes to rate his students on the contests he prepares. Now, he has decided to prepare a new contest. In total, _n_ students will attend, and before the start, every one of them has some positive integer rating. Students are indexed from 1 to _n_. Let's denote the rating of _i_-th student as _a__i_. After the contest ends, every student will end up with some positive integer position. GukiZ expects that his students will take places according to their ratings. He thinks that each student will take place equal to . In particular, if student _A_ has rating strictly lower then student _B_, _A_ will get the strictly better position than _B_, and if two students have equal ratings, they will share the same position. GukiZ would like you to reconstruct the results by following his expectations. Help him and determine the position after the end of the contest for each of his students if everything goes as expected. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20092000), number of GukiZ's students. The second line contains _n_ numbers _a_1,u2009_a_2,u2009... _a__n_ (1u2009≤u2009_a__i_u2009≤u20092000) where _a__i_ is the rating of _i_-th student (1u2009≤u2009_i_u2009≤u2009_n_). Output In a single line, print the position after the end of the contest for each of _n_ students in the same order as they appear in the input. Note In the first sample, students 2 and 3 are positioned first (there is no other student with higher rating), and student 1 is positioned third since there are two students with higher rating. In the second sample, first student is the only one on the contest. In the third sample, students 2 and 5 share the first position with highest rating, student 4 is next with third position, and students 1 and 3 are the last sharing fourth position.
800
false
false
true
false
false
false
true
false
true
false
7,632
1579C
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 744 (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 *1500 No tag edit access → Contest materials Announcement (ru) Tutorial PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST C. Ticks time limit per test2 seconds memory limit per test256 megabytes Casimir has a rectangular piece of paper with a checkered field of size $$$n imes m$$$. Initially, all cells of the field are white. Let us denote the cell with coordinates $$$i$$$ vertically and $$$j$$$ horizontally by $$$(i, j)$$$. The upper left cell will be referred to as $$$(1, 1)$$$ and the lower right cell as $$$(n, m)$$$. Casimir draws ticks of different sizes on the field. A tick of size $$$d$$$ ($$$d > 0$$$) with its center in cell $$$(i, j)$$$ is drawn as follows: First, the center cell $$$(i, j)$$$ is painted black. Then exactly $$$d$$$ cells on the top-left diagonally to the center and exactly $$$d$$$ cells on the top-right diagonally to the center are also painted black. That is all the cells with coordinates $$$(i - h, j pm h)$$$ for all $$$h$$$ between $$$0$$$ and $$$d$$$ are painted. In particular, a tick consists of $$$2d + 1$$$ black cells. An already painted cell will remain black if painted again. Below you can find an example of the $$$4 imes 9$$$ box, with two ticks of sizes $$$2$$$ and $$$3$$$. You are given a description of a checkered field of size $$$n imes m$$$. Casimir claims that this field came about after he drew some (possibly $$$0$$$) ticks on it. The ticks could be of different sizes, but the size of each tick is at least $$$k$$$ (that is, $$$d ge k$$$ for all the ticks). Determine whether this field can indeed be obtained by drawing some (possibly none) ticks of sizes $$$d ge k$$$ or not. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number test cases. The following lines contain the descriptions of the test cases. The first line of the test case description contains the integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 le k le n le 10$$$; $$$1 le m le 19$$$)xa0— the field size and the minimum size of the ticks that Casimir drew. The following $$$n$$$ lines describe the field: each line consists of $$$m$$$ characters either being '.' if the corresponding cell is not yet painted or '*' otherwise. Output Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be YES if the given field can be obtained by drawing ticks of at least the given size and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answers). Example input 8 2 3 1 *.* ... 4 9 2 *.*.*...* .*.*...*. ..*.*.*.. .....*... 4 4 1 *.*. **** .**. .... 5 5 1 ..... *...* .*.*. ..*.* ...*. 5 5 2 ..... *...* .*.*. ..*.* ...*. 4 7 1 *.....* .....*. ..*.*.. ...*... 3 3 1 *** *** *** 3 5 1 *...* .***. .**.. output NO YES YES YES NO NO NO NO Note The first sample test case consists of two asterisks neither of which can be independent ticks since ticks of size $$$0$$$ don't exist. The second sample test case is already described in the statement (check the picture in the statement). This field can be obtained by drawing ticks of sizes $$$2$$$ and $$$3$$$, as shown in the figure. The field in the third sample test case corresponds to three ticks of size $$$1$$$. Their center cells are marked with $$$color{blue}{ ext{blue}}$$$, $$$color{red}{ ext{red}}$$$ and $$$color{green}{ ext{green}}$$$ colors: *.*. *$$$color{blue}{ extbf{*}}$$$** .$$$color{green}{ extbf{*}}color{red}{ extbf{*}}$$$. .... The field in the fourth sample test case could have been obtained by drawing two ticks of sizes $$$1$$$ and $$$2$$$. Their vertices are marked below with $$$color{blue}{ ext{blue}}$$$ and $$$color{red}{ ext{red}}$$$ colors respectively: ..... *...* .*.*. ..$$$color{red}{ extbf{*}}$$$.* ...$$$color{blue}{ extbf{*}}$$$. The field in the fifth sample test case can not be obtained because $$$k = 2$$$, and the last asterisk in the fourth row from the top with coordinates $$$(4, 5)$$$ can only be a part of a tick of size $$$1$$$. The field in the sixth sample test case can not be obtained because the top left asterisk $$$(1, 1)$$$ can't be an independent tick, since the sizes of the ticks must be positive, and cannot be part of a tick with the center cell in the last row, since it is separated from it by a gap (a point, '.') in $$$(2, 2)$$$. In the seventh sample test case, similarly, the field can not be obtained by the described process because the asterisks with coordinates $$$(1, 2)$$$ (second cell in the first row), $$$(3, 1)$$$ and $$$(3, 3)$$$ (leftmost and rightmost cells in the bottom) can not be parts of any ticks. Codeforces (c)
1,500
false
true
true
false
false
false
false
false
false
false
2,739
1802B
Dasha loves guinea pigs very much. In this regard, she decided to settle as many guinea pigs at home as possible and developed a plan for the next $$$n$$$ days. Every day, she will either buy a new guinea pig or call a doctor to examine all her pets. Unfortunately, the store where she was going to buy guinea pigs does not understand them. Therefore, it cannot determine their gender. Dasha can't do it either. The only one who can help is a doctor. To keep guinea pigs, aviaries are needed. Dasha plans to buy them in the same store. Unfortunately, only one species is sold there — a double aviary. No more than two guinea pigs can live in it. Since Dasha does not want to cause moral injury to her pets — she will not settle two guinea pigs of different genders in one aviary. Help Dasha calculate how many aviaries in the worst case you need to buy so that you can be sure that at no moment of time do two guinea pigs of different genders live in the same aviary. As part of this task, we believe that guinea pigs have only two genders — male and female. Input The first line of input data contains one number $$$t$$$ ($$$1 leqslant t leqslant 10^5$$$) — the number of input data sets. The first line of each input data set contains one number $$$n$$$ ($$$1 leqslant n leqslant 10^5$$$) — the number of days Dasha has a plan for. The next line contains $$$n$$$ numbers $$$b_1, b_2, b_3, ldots, b_n$$$ ($$$1 leqslant b_i leqslant 2$$$) — Dasha's plan. If $$$b_i = 1$$$, then on the $$$i$$$th day, Dasha will buy a new guinea pig. If $$$b_i = 2$$$, then on the $$$i$$$th day, a doctor will come to Dasha and help determine the sex of all guinea pigs that Dasha already has. It is guaranteed that the sum of $$$n$$$ for all input data sets does not exceed $$$10^5$$$. Output For each set of input data, output one number — the minimum number of aviaries Dasha needs to buy so that no matter what the genders of the pigs turn out to be, we can settle them so that at no point in time do two guinea pigs of different genders live together. Example Input 6 3 1 1 1 3 2 2 2 5 1 1 1 2 1 10 1 2 1 2 1 2 1 2 1 2 20 1 2 1 1 1 1 1 2 1 2 1 2 2 1 1 1 1 1 1 1 20 2 1 1 2 1 1 2 1 2 2 1 1 1 2 2 1 1 1 1 2 Note In the first set of input data, Dasha needs to put each guinea pig in a separate enclosure, since she does not know their gender. In the second set of input data, Dasha will buy $$$0$$$ guinea pigs, which means she will need $$$0$$$ aviaries. In the third set of input data, you even need $$$3$$$ aviaries to put each guinea pig in a separate aviary before the doctor arrives at the $$$4$$$th day. When she finds out their gender, at least two guinea pigs will be of the same gender and they can be placed in one aviary, and the third in another aviary. Thus, she will have one free aviary in which she can settle a new guinea pig. So answer is $$$3$$$. In the fourth set of input data, we show that $$$4$$$ is the optimal answer. To begin with, we note that the first four guinea pigs can be placed one at a time in an aviary. Then a doctor will come and determine their gender. Among these four guinea pigs there will be at least one pair of the same gender, because: either male guinea pigs are at least $$$2$$$, or they are not more than $$$1$$$, which means that the female is at least $$$3$$$. Now we can put this couple in one aviary, and the other two in separate ones. We will have one more empty aviary where we can put a new pig. Now let's show that the answer is at least $$$4$$$. Let's say that among the first $$$4$$$ guinea pigs, $$$3$$$ are female and $$$1$$$ is male. We need at least $$$3$$$ aviaries to settle them. Then, when we buy a new guinea pig, we will need another aviary in which we will put it, since we do not know its gender.
1,000
true
true
true
false
false
false
false
false
false
false
1,458
109C
Problem - 109C - 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 dsu trees *1900 No tag edit access → Contest materials . An edge is lucky if its weight is a lucky number. Note that a tree with _n_ vertexes is an undirected connected graph that has exactly _n_u2009-u20091 edges. Petya wondered how many vertex triples (_i_,u2009_j_,u2009_k_) exists that on the way from _i_ to _j_, as well as on the way from _i_ to _k_ there must be at least one lucky edge (all three vertexes are pairwise distinct). The order of numbers in the triple matters, that is, the triple (1,u20092,u20093) is not equal to the triple (2,u20091,u20093) and is not equal to the triple (1,u20093,u20092). Find how many such triples of vertexes exist. Input The first line contains the single integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of tree vertexes. Next _n_u2009-u20091 lines contain three integers each: _u__i_ _v__i_ _w__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_,u20091u2009≤u2009_w__i_u2009≤u2009109) — the pair of vertexes connected by the edge and the edge's weight. Output On the single line print the single number — the answer. Please do not use the %lld specificator to read or write 64-bit numbers in С++. It is recommended to use the cin, cout streams or the %I64d specificator. Examples Input 4 1 2 4 3 1 2 1 4 7 Output 16 Input 4 1 2 4 1 3 47 1 4 7447 Output 24 Note The 16 triples of vertexes from the first sample are: (1,u20092,u20094),u2009(1,u20094,u20092),u2009(2,u20091,u20093),u2009(2,u20091,u20094),u2009(2,u20093,u20091),u2009(2,u20093,u20094),u2009(2,u20094,u20091),u2009(2,u20094,u20093),u2009(3,u20092,u20094),u2009(3,u20094,u20092),u2009(4,u20091,u20092),u2009(4,u20091,u20093),u2009(4,u20092,u20091),u2009(4,u20092,u20093),u2009(4,u20093,u20091),u2009(4,u20093,u20092). In the second sample all the triples should be counted: 4·3·2u2009=u200924.
1,900
false
false
false
true
false
false
false
false
false
false
9,447
631A
Blake is a CEO of a large company called "Blake Technologies". He loves his company very much and he thinks that his company should be the best. That is why every candidate needs to pass through the interview that consists of the following problem. We define function _f_(_x_,u2009_l_,u2009_r_) as a bitwise OR of integers _x__l_,u2009_x__l_u2009+u20091,u2009...,u2009_x__r_, where _x__i_ is the _i_-th element of the array _x_. You are given two arrays _a_ and _b_ of length _n_. You need to determine the maximum value of sum _f_(_a_,u2009_l_,u2009_r_)u2009+u2009_f_(_b_,u2009_l_,u2009_r_) among all possible 1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20091000)xa0— the length of the arrays. The second line contains _n_ integers _a__i_ (0u2009≤u2009_a__i_u2009≤u2009109). The third line contains _n_ integers _b__i_ (0u2009≤u2009_b__i_u2009≤u2009109). Output Print a single integerxa0— the maximum value of sum _f_(_a_,u2009_l_,u2009_r_)u2009+u2009_f_(_b_,u2009_l_,u2009_r_) among all possible 1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_. Examples Input 5 1 2 4 3 2 2 3 3 12 1 Input 10 13 2 7 11 8 4 9 8 5 1 5 7 18 9 2 3 0 11 8 6 Note Bitwise OR of two non-negative integers _a_ and _b_ is the number _c_u2009=u2009_a_ _OR_ _b_, such that each of its digits in binary notation is 1 if and only if at least one of _a_ or _b_ have 1 in the corresponding position in binary notation. In the first sample, one of the optimal answers is _l_u2009=u20092 and _r_u2009=u20094, because _f_(_a_,u20092,u20094)u2009+u2009_f_(_b_,u20092,u20094)u2009=u2009(2 _OR_ 4 _OR_ 3)u2009+u2009(3 _OR_ 3 _OR_ 12)u2009=u20097u2009+u200915u2009=u200922. Other ways to get maximum value is to choose _l_u2009=u20091 and _r_u2009=u20094, _l_u2009=u20091 and _r_u2009=u20095, _l_u2009=u20092 and _r_u2009=u20094, _l_u2009=u20092 and _r_u2009=u20095, _l_u2009=u20093 and _r_u2009=u20094, or _l_u2009=u20093 and _r_u2009=u20095. In the second sample, the maximum value is obtained for _l_u2009=u20091 and _r_u2009=u20099.
900
false
false
true
false
false
false
true
false
false
false
7,286
1941F
Rudolf has prepared a set of $$$n$$$ problems with complexities $$$a_1 < a_2 < a_3 < dots < a_n$$$. He is not entirely satisfied with the balance, so he wants to add at most one problem to fix it. For this, Rudolf came up with $$$m$$$ models of problems and $$$k$$$ functions. The complexity of the $$$i$$$-th model is $$$d_i$$$, and the complexity of the $$$j$$$-th function is $$$f_j$$$. To create a problem, he selects values $$$i$$$ and $$$j$$$ ($$$1 le i le m$$$, $$$1 le j le k$$$) and by combining the $$$i$$$-th model with the $$$j$$$-th function, he obtains a new problem with complexity $$$d_i + f_j$$$ (a new element is inserted into the array $$$a$$$). To determine the imbalance of the set, Rudolf sorts the complexities of the problems in ascending order and finds the largest value of $$$a_i - a_{i - 1}$$$ ($$$i > 1$$$). What is the minimum value of imbalance that Rudolf can achieve by adding at most one problem, created according to the described rules? Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$2 le n le 10^5$$$, $$$1 le m, k le 2 cdot 10^5$$$)xa0— the number of prepared problems, the number of models, and the number of functions, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, a_3, dots a_n$$$ ($$$1 le a_i le 2 cdot 10^9$$$, $$$a_i < a_{i+1}$$$)xa0— the complexities of the prepared problems. The third line of each test case contains $$$m$$$ integers $$$d_1, d_2, d_3, dots d_m$$$ ($$$1 le d_i le 10^9$$$)xa0— the complexities of the models. The fourth line of each test case contains $$$k$$$ integers $$$f_1, f_2, f_3, dots f_k$$$ ($$$1 le f_i le 10^9$$$)xa0— the complexities of the functions. It is guaranteed that the sum of $$$n$$$ over all testcases does not exceed $$$10^5$$$. It is guaranteed that the sum of $$$m$$$ over all testcases does not exceed $$$2 cdot 10^5$$$. It is guaranteed that the sum of $$$k$$$ over all testcases does not exceed $$$2 cdot 10^5$$$.
1,800
false
true
false
false
false
false
false
true
true
false
634
1203B
You are given $$$4n$$$ sticks, the length of the $$$i$$$-th stick is $$$a_i$$$. You have to create $$$n$$$ rectangles, each rectangle will consist of exactly $$$4$$$ sticks from the given set. The rectangle consists of four sides, opposite sides should have equal length and all angles in it should be right. Note that each stick can be used in only one rectangle. Each stick should be used as a side, you cannot break the stick or use it not to the full length. You want to all rectangles to have equal area. The area of the rectangle with sides $$$a$$$ and $$$b$$$ is $$$a cdot b$$$. Your task is to say if it is possible to create exactly $$$n$$$ rectangles of equal area or not. You have to answer $$$q$$$ independent queries. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 500$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 le n le 100$$$) — the number of rectangles. The second line of the query contains $$$4n$$$ integers $$$a_1, a_2, dots, a_{4n}$$$ ($$$1 le a_i le 10^4$$$), where $$$a_i$$$ is the length of the $$$i$$$-th stick. Output For each query print the answer to it. If it is impossible to create exactly $$$n$$$ rectangles of equal area using given sticks, print "NO". Otherwise print "YES". Example Input 5 1 1 1 10 10 2 10 5 2 10 1 1 2 5 2 10 5 1 10 5 1 1 1 2 1 1 1 1 1 1 1 1 1 10000 10000 10000 10000
1,200
true
true
false
false
false
false
false
false
false
false
4,682
245F
You've got a list of program warning logs. Each record of a log stream is a string in this format: "2012-MM-DD HH:MM:SS:MESSAGE" (without the quotes). String "MESSAGE" consists of spaces, uppercase and lowercase English letters and characters "!", ".", ",", "?". String "2012-MM-DD" determines a correct date in the year of 2012. String "HH:MM:SS" determines a correct time in the 24 hour format. The described record of a log stream means that at a certain time the record has got some program warning (string "MESSAGE" contains the warning's description). Your task is to print the first moment of time, when the number of warnings for the last _n_ seconds was not less than _m_. Input The first line of the input contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u200910000). The second and the remaining lines of the input represent the log stream. The second line of the input contains the first record of the log stream, the third line contains the second record and so on. Each record of the log stream has the above described format. All records are given in the chronological order, that is, the warning records are given in the order, in which the warnings appeared in the program. It is guaranteed that the log has at least one record. It is guaranteed that the total length of all lines of the log stream doesn't exceed 5·106 (in particular, this means that the length of some line does not exceed 5·106 characters). It is guaranteed that all given dates and times are correct, and the string 'MESSAGE" in all records is non-empty. Output If there is no sought moment of time, print -1. Otherwise print a string in the format "2012-MM-DD HH:MM:SS" (without the quotes) — the first moment of time when the number of warnings for the last _n_ seconds got no less than _m_. Examples Input 60 3 2012-03-16 16:15:25: Disk size is 2012-03-16 16:15:25: Network failute 2012-03-16 16:16:29: Cant write varlog 2012-03-16 16:16:42: Unable to start process 2012-03-16 16:16:43: Disk size is too small 2012-03-16 16:16:53: Timeout detected Output 2012-03-16 16:16:43 Input 1 2 2012-03-16 23:59:59:Disk size 2012-03-17 00:00:00: Network 2012-03-17 00:00:01:Cant write varlog Input 2 2 2012-03-16 23:59:59:Disk size is too sm 2012-03-17 00:00:00:Network failute dete 2012-03-17 00:00:01:Cant write varlogmysq Output 2012-03-17 00:00:00
2,000
false
false
true
false
false
false
true
true
false
false
8,851