Dataset Viewer
Auto-converted to Parquet Duplicate
Problem ID
stringlengths
2
6
Problem Description
stringlengths
848
8.26k
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
combinatorics
bool
2 classes
trees
bool
2 classes
strings
bool
2 classes
graphs
bool
2 classes
__index_level_0__
int64
3
9.98k
2007B
After receiving yet another integer array $$$a_1, a_2, ldots, a_n$$$ at her birthday party, Index decides to perform some operations on it. Formally, there are $$$m$$$ operations that she is going to perform in order. Each of them belongs to one of the two types: $$$ exttt{+ l r}$$$. Given two integers $$$l$$$ and $$$r$$$, for all $$$1 leq i leq n$$$ such that $$$l leq a_i leq r$$$, set $$$a_i := a_i + 1$$$. $$$ exttt{- l r}$$$. Given two integers $$$l$$$ and $$$r$$$, for all $$$1 leq i leq n$$$ such that $$$l leq a_i leq r$$$, set $$$a_i := a_i - 1$$$. For example, if the initial array $$$a = [7, 1, 3, 4, 3]$$$, after performing the operation $$$ exttt{+} space 2 space 4$$$, the array $$$a = [7, 1, 4, 5, 4]$$$. Then, after performing the operation $$$ exttt{-} space 1 space 10$$$, the array $$$a = [6, 0, 3, 4, 3]$$$. Index is curious about the maximum value in the array $$$a$$$. Please help her find it after each of the $$$m$$$ operations. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 2 cdot 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n leq 10^5$$$, $$$1 leq m leq 10^5$$$)xa0— the length of the array and the number of operations. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the initial array $$$a$$$. Then $$$m$$$ lines follow, each line corresponds to the operation, in the following format: $$$ exttt{c l r}$$$ ($$$c in { exttt +, exttt -}$$$, $$$l$$$ and $$$r$$$ are integers, $$$1 leq l leq r leq 10^9$$$)xa0— the description of the operation. Note that the elements $$$a_i$$$ may not satisfy $$$1le a_ile 10^9$$$ after some operations, as it is shown in the example. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$m$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output one single line containing $$$m$$$ integers, with the $$$i$$$-th of them describing the maximum value of the array after the $$$i$$$-th operation. Example Input 5 5 5 1 2 3 2 1 + 1 3 - 2 3 + 1 2 + 2 4 - 6 8 5 5 1 3 3 4 5 + 1 4 + 2 3 - 4 5 - 3 3 - 2 6 5 5 1 1 1 1 1 + 2 3 - 4 5 + 1 6 - 2 5 + 1 8 1 1 1 - 1 1 1 1 1000000000 + 1000000000 1000000000 Output 4 4 4 5 5 5 5 4 4 3 1 1 2 1 2 0 1000000001 Note In the first test case, the process of the operations is listed below: After the first operation, the array becomes equal $$$[2,3,4,3,2]$$$. The maximum value is $$$4$$$. After the second operation, the array becomes equal $$$[1,2,4,2,1]$$$. The maximum value is $$$4$$$. After the third operation, the array becomes equal $$$[2,3,4,3,2]$$$. The maximum value is $$$4$$$. After the fourth operation, the array becomes equal $$$[3,4,5,4,3]$$$. The maximum value is $$$5$$$. After the fifth operation, the array becomes equal $$$[3,4,5,4,3]$$$. The maximum value is $$$5$$$. In the second test case, the process of the operations is listed below: After the first operation, the array becomes equal $$$[2,4,4,5,5]$$$. The maximum value is $$$5$$$. After the second operation, the array becomes equal $$$[3,4,4,5,5]$$$. The maximum value is $$$5$$$. After the third operation, the array becomes equal $$$[3,3,3,4,4]$$$. The maximum value is $$$4$$$. After the fourth operation, the array becomes equal $$$[2,2,2,4,4]$$$. The maximum value is $$$4$$$. After the fifth operation, the array becomes equal $$$[1,1,1,3,3]$$$. The maximum value is $$$3$$$.
900
false
true
false
false
true
false
false
false
false
false
210
665E
Problem - 665E - Codeforces =============== xa0 []( []( [Register]( [Home]( [Top]( [Catalog]( [Contests]( [Gym]( [Problemset]( [Groups]( [Rating]( [Edu]( [API]( [Calendar]( [Help]( [Rayan]( [Educational Codeforces Round 12]( --- 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 strings trees *2100 No tag edit access → Contest materials [Announcement (en)]( "Educational Codeforces Round 12") [Editorial (en)]( "Editorial") [Problems]( [Submit]( [Status]( [Standings]( [Custom test]( The problem statement has recently been changed. [View the changes.]( Beautiful Subarrays time limit per test 3 seconds memory limit per test 512 megabytes input standard input output standard output One day, ZS the Coder wrote down an array of integers _a_u2009with elements _a_1,u2009u2009_a_2,u2009u2009...,u2009u2009_a__n_. A subarray of the array _a_ is a sequence _a__l_,u2009u2009_a__l_u2009u2009+u2009u20091,u2009u2009...,u2009u2009_a__r_ for some integers (_l_,u2009u2009_r_) such that 1u2009u2009≤u2009u2009_l_u2009u2009≤u2009u2009_r_u2009u2009≤u2009u2009_n_. ZS the Coder thinks that a subarray of _a_ is beautiful if the bitwise xor of all the elements in the subarray is at least _k_. Help ZS the Coder find the number of beautiful subarrays of _a_! Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009106,u20091u2009≤u2009_k_u2009≤u2009109) — the number of elements in the array _a_ and the value of the parameter _k_. The second line contains _n_ integers _a__i_ (0u2009≤u2009_a__i_u2009≤u2009109) — the elements of the array _a_. Output Print the only integer _c_ — the number of beautiful subarrays of the array _a_. Examples Input 3 1 1 2 3 Output 5 Input 3 2 1 2 3 Output 3 Input 3 3 1 2 3 Output 2 [Codeforces]( (c) Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 10:16:48 (j2). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name ---
2,100
false
false
false
false
true
false
false
true
true
false
7,157
1842G
Yet another random problem. Tenzing has an array $$$a$$$ of length $$$n$$$ and an integer $$$v$$$. Tenzing will perform the following operation $$$m$$$ times: 1. Choose an integer $$$i$$$ such that $$$1 leq i leq n$$$ uniformly at random. 2. For all $$$j$$$ such that $$$i leq j leq n$$$, set $$$a_j := a_j + v$$$. Tenzing wants to know the expected value of $$$prod_{i=1}^n a_i$$$ after performing the $$$m$$$ operations, modulo $$$10^9+7$$$. Formally, let $$$M = 10^9+7$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output the integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output the integer $$$x$$$ that $$$0 le x < M$$$ and $$$x cdot q equiv p pmod{M}$$$. Input The first line of input contains three integers $$$n$$$, $$$m$$$ and $$$v$$$ ($$$1leq nleq 5000$$$, $$$1leq m,vleq 10^9$$$). The second line of input contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1leq a_ileq 10^9$$$). Output Output the expected value of $$$prod_{i=1}^n a_i$$$ modulo $$$10^9+7$$$. Note There are three types of $$$a$$$ after performing all the $$$m$$$ operations : 1. $$$a_1=2,a_2=12$$$ with $$$frac{1}{4}$$$ probability. 2. $$$a_1=a_2=12$$$ with $$$frac{1}{4}$$$ probability. 3. $$$a_1=7,a_2=12$$$ with $$$frac{1}{2}$$$ probability. So the expected value of $$$a_1cdot a_2$$$ is $$$frac{1}{4}cdot (24+144) + frac{1}{2}cdot 84=84$$$.
2,800
true
false
false
true
false
false
true
false
false
false
1,233
472F
Problem - 472F - Codeforces =============== xa0 []( []( [Register]( [Home]( [Top]( [Catalog]( [Contests]( [Gym]( [Problemset]( [Groups]( [Rating]( [Edu]( [API]( [Calendar]( [Help]( [Rayan]( [Codeforces Round 270]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms math matrices *2700 No tag edit access → Contest materials [Codeforces Round #270]( "Codeforces Round #270") [Editorial (en)]( "Editorial") [Problems]( [Submit]( [Status]( [Standings]( [Custom test]( The problem statement has recently been changed. [View the changes.]( Design Tutorial: Change the Goal time limit per test 2 seconds memory limit per test 256 megabytes input stdin output stdout There are some tasks which have the following structure: you are given a model, and you can do some operations, you should use these operations to achive the goal. One way to create a new task is to use the same model and same operations, but change the goal. Let's have a try. I have created the following task for Topcoder SRM 557 Div1-Hard: you are given _n_ integers _x_1,u2009_x_2,u2009...,u2009_x__n_. You are allowed to perform the assignments (as many as you want) of the following form _x__i_ ^= _x__j_ (in the original task _i_ and _j_ must be different, but in this task we allow _i_ to equal _j_). The goal is to maximize the sum of all _x__i_. Now we just change the goal. You are also given _n_ integers _y_1,u2009_y_2,u2009...,u2009_y__n_. You should make _x_1,u2009_x_2,u2009...,u2009_x__n_ exactly equal to _y_1,u2009_y_2,u2009...,u2009_y__n_. In other words, for each _i_ number _x__i_ should be equal to _y__i_. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u200910000). The second line contains _n_ integers: _x_1 to _x__n_ (0u2009≤u2009_x__i_u2009≤u2009109). The third line contains _n_ integers: _y_1 to _y__n_ (0u2009≤u2009_y__i_u2009≤u2009109). Output If there is no solution, output -1. If there is a solution, then in the first line output an integer _m_ (0u2009≤u2009_m_u2009≤u20091000000) – the number of assignments you need to perform. Then print _m_ lines, each line should contain two integers _i_ and _j_ (1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_), which denote assignment _x__i_ ^= _x__j_. If there are multiple solutions you can print any of them. We can prove that under these constraints if there exists a solution then there always exists a solution with no more than 106 operations. Examples Input 2 3 5 6 0 Output 2 1 2 2 2 Input 5 0 0 0 0 0 1 2 3 4 5 Output -1 Input 3 4 5 6 1 2 3 Output 5 3 1 1 2 2 2 2 3 3 1 Input 3 1 2 3 4 5 6 Output -1 Note Assignment _a_ ^= _b_ denotes assignment _a_ = _a_ ^ _b_, where operation "^" is bitwise XOR of two integers. [Codeforces]( (c) Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 10:57:13 (g1). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name ---
2,700
true
false
false
false
false
true
false
false
false
false
7,942
519E
A and B are preparing themselves for programming contests. The University where A and B study is a set of rooms connected by corridors. Overall, the University has _n_ rooms connected by _n_u2009-u20091 corridors so that you can get from any room to any other one by moving along the corridors. The rooms are numbered from 1 to _n_. Every day А and B write contests in some rooms of their university, and after each contest they gather together in the same room and discuss problems. A and B want the distance from the rooms where problems are discussed to the rooms where contests are written to be equal. The distance between two rooms is the number of edges on the shortest path between them. As they write contests in new rooms every day, they asked you to help them find the number of possible rooms to discuss problems for each of the following _m_ days. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of rooms in the University. The next _n_u2009-u20091 lines describe the corridors. The _i_-th of these lines (1u2009≤u2009_i_u2009≤u2009_n_u2009-u20091) contains two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_), showing that the _i_-th corridor connects rooms _a__i_ and _b__i_. The next line contains integer _m_ (1u2009≤u2009_m_u2009≤u2009105) — the number of queries. Next _m_ lines describe the queries. The _j_-th of these lines (1u2009≤u2009_j_u2009≤u2009_m_) contains two integers _x__j_ and _y__j_ (1u2009≤u2009_x__j_,u2009_y__j_u2009≤u2009_n_) that means that on the _j_-th day A will write the contest in the room _x__j_, B will write in the room _y__j_. Output In the _i_-th (1u2009≤u2009_i_u2009≤u2009_m_) line print the number of rooms that are equidistant from the rooms where A and B write contest on the _i_-th day. Note in the first sample there is only one room at the same distance from rooms number 2 and 3 — room number 1.
2,100
false
false
false
true
true
false
false
true
false
false
7,758
1849C
You are given a string $$$s$$$ consisting of $$$n$$$ characters 0 and/or 1. You make $$$m$$$ copies of this string, let the $$$i$$$-th copy be the string $$$t_i$$$. Then you perform exactly one operation on each of the copies: for the $$$i$$$-th copy, you sort its substring $$$[l_i; r_i]$$$ (the substring from the $$$l_i$$$-th character to the $$$r_i$$$-th character, both endpoints inclusive). Note that each operation affects only one copy, and each copy is affected by only one operation. Your task is to calculate the number of different strings among $$$t_1, t_2, ldots, t_m$$$. Note that the initial string $$$s$$$ should be counted only if at least one of the copies stays the same after the operation. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2 cdot 10^5$$$) — the length of $$$s$$$ and the number of copies, respectively. The second line contains $$$n$$$ characters 0 and/or 1 — the string $$$s$$$. Then $$$m$$$ lines follow. The $$$i$$$-th of them contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$) — the description of the operation applied to the $$$i$$$-th copy. The sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. The sum of $$$m$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output Print one integer — the number of different strings among $$$t_1, t_2, ldots, t_m$$$. Example Input 3 6 5 101100 1 2 1 3 2 4 5 5 1 6 6 4 100111 2 2 1 4 1 3 1 2 1 1 0 1 1 Note Consider the first example. Copies below are given in order of the input operations. Underlined substrings are substrings that are sorted: 1. 101100 $$$ ightarrow$$$ 011100; 2. 101100 $$$ ightarrow$$$ 011100; 3. 101100 $$$ ightarrow$$$ 101100; 4. 101100 $$$ ightarrow$$$ 101100; 5. 101100 $$$ ightarrow$$$ 000111. There are three different strings among $$$t_1, t_2, t_3, t_4, t_5$$$: 000111, 011100 and 101100. Consider the second example: 1. 100111 $$$ ightarrow$$$ 100111; 2. 100111 $$$ ightarrow$$$ 001111; 3. 100111 $$$ ightarrow$$$ 001111; 4. 100111 $$$ ightarrow$$$ 010111. There are three different strings among $$$t_1, t_2, t_3, t_4$$$: 001111, 010111 and 100111.
1,600
false
false
false
false
true
false
false
false
true
false
1,186
316G1
Smart Beaver recently got interested in a new word game. The point is as follows: count the number of distinct good substrings of some string _s_. To determine if a string is good or not the game uses rules. Overall there are _n_ rules. Each rule is described by a group of three (_p_,u2009_l_,u2009_r_), where _p_ is a string and _l_ and _r_ (_l_u2009≤u2009_r_) are integers. We’ll say that string _t_ complies with rule (_p_,u2009_l_,u2009_r_), if the number of occurrences of string _t_ in string _p_ lies between _l_ and _r_, inclusive. For example, string "ab", complies with rules ("ab", 1, 2) and ("aab", 0, 1), but does not comply with rules ("cd", 1, 2) and ("abab", 0, 1). A substring _s_[_l_... _r_] (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_s_) of string _s_u2009=u2009_s_1_s_2... _s__s_ (_s_ is a length of _s_) is string _s__l__s__l_u2009+u20091... _s__r_. Consider a number of occurrences of string _t_ in string _p_ as a number of pairs of integers _l_,u2009_r_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_p_) such that _p_[_l_... _r_]u2009=u2009_t_. We’ll say that string _t_ is good if it complies with all _n_ rules. Smart Beaver asks you to help him to write a program that can calculate the number of distinct good substrings of string _s_. Two substrings _s_[_x_... _y_] and _s_[_z_... _w_] are cosidered to be distinct iff _s_[_x_... _y_]u2009≠u2009_s_[_z_... _w_]. Input The first line contains string _s_. The second line contains integer _n_. Next _n_ lines contain the rules, one per line. Each of these lines contains a string and two integers _p__i_,u2009_l__i_,u2009_r__i_, separated by single spaces (0u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_p__i_). It is guaranteed that all the given strings are non-empty and only contain lowercase English letters. The input limits for scoring 30 points are (subproblem G1): 0u2009≤u2009_n_u2009≤u200910. The length of string _s_ and the maximum length of string _p_ is u2009≤u2009200. The input limits for scoring 70 points are (subproblems G1+G2): 0u2009≤u2009_n_u2009≤u200910. The length of string _s_ and the maximum length of string _p_ is u2009≤u20092000. The input limits for scoring 100 points are (subproblems G1+G2+G3): 0u2009≤u2009_n_u2009≤u200910. The length of string _s_ and the maximum length of string _p_ is u2009≤u200950000. Note There are three good substrings in the first sample test: «aab», «ab» and «b». In the second test only substrings «e» and «t» are good.
1,700
false
false
false
false
false
false
false
false
true
false
8,568
171H
Problem - 171H - Codeforces =============== xa0 []( []( [Register]( [Home]( [Top]( [Catalog]( [Contests]( [Gym]( [Problemset]( [Groups]( [Rating]( [Edu]( [API]( [Calendar]( [Help]( [Rayan]( → Attention The package for this problem was not updated by the problem writer or Codeforces administration after we've upgraded the judging servers. To adjust the time limit constraint, a solution execution time will be multiplied by 2. For example, if your solution works for 400 ms on judging servers, then the value 800 ms will be displayed and used to determine the verdict. [April Fools Day Contest]( --- Finished → Languages The following languages are additionally available for the problems from the contest April Fools Day Contest: Secret_171 → 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 *special problem implementation *1700 No tag edit access → Contest materials [April Fools Day Contest]( "April Fools Day Contest") [Tutorial]( "April Fools Day Contest: editorial") [Problems]( [Submit]( [Status]( [Standings]( [Custom test]( The problem statement has recently been changed. [View the changes.]( A polyline time limit per test 2 seconds memory limit per test 256 megabytes input stdin output stdout Input The input contains two integers _a_,u2009_b_ (1u2009≤u2009_a_u2009≤u200910,u20090u2009≤u2009_b_u2009≤u200922·_a_u2009-u20091) separated by a single space. Output Output two integers separated by a single space. Examples Input 1 0 Output 0 0 Input 2 15 Output 3 0 Input 4 160 Output 12 12 [Codeforces]( (c) Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 12:08:33 (g1). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name ---
1,700
false
false
true
false
false
false
false
false
false
false
9,179
1794B
You are given an array of $$$n$$$ positive integers $$$a_1, a_2, ldots, a_n$$$. In one operation, you can choose any number of the array and add $$$1$$$ to it. Make at most $$$2n$$$ operations so that the array satisfies the following property: $$$a_{i+1}$$$ is not divisible by $$$a_i$$$, for each $$$i = 1, 2, ldots, n-1$$$. You do not need to minimize the number of operations. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1le nle 10^4$$$)xa0— the length of the given array. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1le a_ileq 10^9$$$)xa0— the given array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5cdot 10^4$$$. Output For each test case, print the answer on a separate line. In the only line, print $$$n$$$ integersxa0— the resulting array $$$a$$$ after applying at most $$$2n$$$ operations. We can show that an answer always exists under the given constraints. If there are multiple answers, print any of them. Note In the first test case, the array $$$[4, 5, 6, 7]$$$ can be achieved by applying $$$2$$$ operations to the first element, $$$1$$$ operation to the second element, $$$3$$$ operations to the third element, and $$$1$$$ operation to the last element. The total number of operations performed is $$$7$$$, which is less than the allowed $$$8$$$ operations in this case. In the second test case, the array $$$[3, 2, 3]$$$ can be achieved by applying two operations to the first element. Another possible resulting array could be $$$[2, 3, 5]$$$, because the total number of operations does not need to be minimum. In the third test case, not applying any operations results in an array that satisfies the statement's property. Observe that it is not mandatory to make operations.
900
true
true
false
false
false
true
false
false
false
false
1,513
1367D
# Task On The Board Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes Polycarp wrote on the board a string s containing only lowercase Latin letters (‘ a’-’ z’). This string is known for you and given in the input. After that, he erased some letters from the string s, and he rewrote the remaining letters in any order. As a result, he got some new string t. You have to find it with some additional information. Suppose that the string t has length m and the characters are numbered from left to right from 1 to m.You are given a sequence of m integers: b1, b 2, . . . , b m, where bi is the sum of the distances i − j from the index i to all such indices j that tj > t i (consider that ‘ a’<‘ b’<. . . <‘ z’). In other words, to calculate bi, Polycarp finds all such indices j that the index j contains a letter that is later in the alphabet than ti and sums all the values i − j.For example, if t = “ abzb ”, then: • since t1=‘ a’, all other indices contain letters which are later in the alphabet, that is: b1 = 1 − 2 + 1 − 3 + 1 − 4 = 1 + 2 + 3 = 6 ; • since t2=‘ b’, only the index j = 3 contains the letter, which is later in the alphabet, that is: b2 = 2 − 3 = 1 ; • since t3=‘ z’, then there are no indexes j such that tj > t i, thus b3 = 0 ; • since t4=‘ b’, only the index j = 3 contains the letter, which is later in the alphabet, that is: b4 = 4 − 3 = 1 .Thus, if t = “ abzb ”, then b = [6 , 1, 0, 1] .Given the string s and the array b, find any possible string t for which the following two requirements are fulfilled simultaneously: • t is obtained from s by erasing some letters (possibly zero) and then writing the rest in any order; • the array, constructed from the string t according to the rules above, equals to the array b specified in the input data. # Input The first line contains an integer q (1 ≤ q ≤ 100 ) x16 the number of test cases in the test. Then q test cases follow. Each test case consists of three lines: • the first line contains string s, which has a length from 1 to 50 and consists of lowercase English letters; • the second line contains positive integer m (1 ≤ m ≤ s), where s is the length of the string s,and m is the length of the array b; • the third line contains the integers b1, b 2, . . . , b m (0 ≤ bi ≤ 1225 ). It is guaranteed that in each test case an answer exists. Page 1 of 2 Output Output q lines: the k-th of them should contain the answer (string t) to the k-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. # Example standard input standard output 4abac 32 1 0 abc 10abba 31 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 aac baba codeforces # Note In the first test case, such strings t are suitable: “ aac ’, “ aab ”. In the second test case, such trings t are suitable: “ a”, “ b”, “ c”. In the third test case, only the string t equals to “ aba ” is suitable, but the character ‘ b’ can be from the second or third position. Page 2 of 2
1,800
false
true
true
false
false
true
false
false
false
false
3,840
1282C
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $$$n$$$ problems that can be solved in $$$T$$$ minutes. Thus, the exam begins at time $$$0$$$ and ends at time $$$T$$$. Petya can leave the exam at any integer time from $$$0$$$ to $$$T$$$, inclusive. All problems are divided into two types: easy problems — Petya takes exactly $$$a$$$ minutes to solve any easy problem; hard problems — Petya takes exactly $$$b$$$ minutes ($$$b > a$$$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $$$x$$$, then it will be solved at time $$$x+a$$$. Similarly, if at a time $$$x$$$ Petya starts to solve a hard problem, then it will be solved at time $$$x+b$$$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $$$t_i$$$ ($$$0 le t_i le T$$$) at which it will become mandatory (required). If Petya leaves the exam at time $$$s$$$ and there is such a problem $$$i$$$ that $$$t_i le s$$$ and he didn't solve it, then he will receive $$$0$$$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $$$t_i le s$$$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $$$s$$$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $$$n=2$$$, $$$T=5$$$, $$$a=2$$$, $$$b=3$$$, the first problem is hard and $$$t_1=3$$$ and the second problem is easy and $$$t_2=2$$$. Then: if he leaves at time $$$s=0$$$, then he will receive $$$0$$$ points since he will not have time to solve any problems; if he leaves at time $$$s=1$$$, he will receive $$$0$$$ points since he will not have time to solve any problems; if he leaves at time $$$s=2$$$, then he can get a $$$1$$$ point by solving the problem with the number $$$2$$$ (it must be solved in the range from $$$0$$$ to $$$2$$$); if he leaves at time $$$s=3$$$, then he will receive $$$0$$$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $$$s=4$$$, then he will receive $$$0$$$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $$$s=5$$$, then he can get $$$2$$$ points by solving all problems. Thus, the answer to this test is $$$2$$$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. Input The first line contains the integer $$$m$$$ ($$$1 le m le 10^4$$$)xa0— the number of test cases in the test. The next lines contain a description of $$$m$$$ test cases. The first line of each test case contains four integers $$$n, T, a, b$$$ ($$$2 le n le 2cdot10^5$$$, $$$1 le T le 10^9$$$, $$$1 le a < b le 10^9$$$)xa0— the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $$$n$$$ numbers $$$0$$$ or $$$1$$$, separated by single space: the $$$i$$$-th number means the type of the $$$i$$$-th problem. A value of $$$0$$$ means that the problem is easy, and a value of $$$1$$$ that the problem is hard. The third line of each test case contains $$$n$$$ integers $$$t_i$$$ ($$$0 le t_i le T$$$), where the $$$i$$$-th number means the time at which the $$$i$$$-th problem will become mandatory. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2cdot10^5$$$. Output Print the answers to $$$m$$$ test cases. For each set, print a single integerxa0— maximal number of points that he can receive, before leaving the exam.
1,800
false
true
false
false
false
false
false
false
false
false
4,283
1491G
There are $$$n$$$ coins labeled from $$$1$$$ to $$$n$$$. Initially, coin $$$c_i$$$ is on position $$$i$$$ and is facing upwards (($$$c_1, c_2, dots, c_n)$$$ is a permutation of numbers from $$$1$$$ to $$$n$$$). You can do some operations on these coins. In one operation, you can do the following: Choose $$$2$$$ distinct indices $$$i$$$ and $$$j$$$. Then, swap the coins on positions $$$i$$$ and $$$j$$$. Then, flip both coins on positions $$$i$$$ and $$$j$$$. (If they are initially faced up, they will be faced down after the operation and vice versa) Construct a sequence of at most $$$n+1$$$ operations such that after performing all these operations the coin $$$i$$$ will be on position $$$i$$$ at the end, facing up. Note that you do not need to minimize the number of operations. Input The first line contains an integer $$$n$$$ ($$$3 leq n leq 2 cdot 10^5$$$) — the number of coins. The second line contains $$$n$$$ integers $$$c_1,c_2,dots,c_n$$$ ($$$1 le c_i le n$$$, $$$c_i eq c_j$$$ for $$$i eq j$$$). Output In the first line, output an integer $$$q$$$ $$$(0 leq q leq n+1)$$$ — the number of operations you used. In the following $$$q$$$ lines, output two integers $$$i$$$ and $$$j$$$ $$$(1 leq i, j leq n, i e j)$$$ — the positions you chose for the current operation. Note Let coin $$$i$$$ facing upwards be denoted as $$$i$$$ and coin $$$i$$$ facing downwards be denoted as $$$-i$$$. The series of moves performed in the first sample changes the coins as such: $$$[~~~2,~~~1,~~~3]$$$ $$$[-3,~~~1,-2]$$$ $$$[-3,~~~2,-1]$$$ $$$[~~~1,~~~2,~~~3]$$$ In the second sample, the coins are already in their correct positions so there is no need to swap.
2,800
true
false
false
false
false
true
false
false
false
true
3,216
1519A
You have $$$r$$$ red and $$$b$$$ blue beans. You'd like to distribute them among several (maybe, one) packets in such a way that each packet: has at least one red bean (or the number of red beans $$$r_i ge 1$$$); has at least one blue bean (or the number of blue beans $$$b_i ge 1$$$); the number of red and blue beans should differ in no more than $$$d$$$ (or $$$r_i - b_i le d$$$) Can you distribute all beans? Input The first line contains the single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first and only line of each test case contains three integers $$$r$$$, $$$b$$$, and $$$d$$$ ($$$1 le r, b le 10^9$$$; $$$0 le d le 10^9$$$)xa0— the number of red and blue beans and the maximum absolute difference in each packet. Output For each test case, if you can distribute all beans, print YES. Otherwise, print NO. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer). Example Input 4 1 1 0 2 7 3 6 1 4 5 4 0 Note In the first test case, you can form one packet with $$$1$$$ red and $$$1$$$ blue bean. The absolute difference $$$1 - 1 = 0 le d$$$. In the second test case, you can form two packets: $$$1$$$ red and $$$4$$$ blue beans in the first packet and $$$1$$$ red and $$$3$$$ blue beans in the second one. In the third test case, since $$$b = 1$$$, you can form only one packet with $$$6$$$ red and $$$1$$$ blue beans. The absolute difference $$$6 - 1 = 5 > d$$$. In the fourth test case, since $$$d = 0$$$ so each packet should contain the same number of red and blue beans, but $$$r eq b$$$.
800
true
false
false
false
false
false
false
false
false
false
3,069
710C
[Home]( [Top]( [Catalog]( [Contests]( [Gym]( [Problemset]( [Groups]( [Rating]( [Edu]( [API]( [Calendar]( [Help]( [Rayan]( 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. constructive algorithms math *1500 No tag edit access [Problems]( [Submit]( [Status]( [Standings]( [Custom test]( Magic Odd Square time limit per test 1 second memory limit per test 256 megabytes Find an _n_u2009×u2009_n_ matrix with different numbers from 1 to _n_2, so the sum in each row, column and both main diagonals are odd. Input The only line contains odd integer _n_ (1u2009≤u2009_n_u2009≤u200949). Output Print _n_ lines with _n_ integers. All the integers should be different and from 1 to _n_2. The sum in each row, column and both main diagonals should be odd. [Codeforces]( (c) Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 10:08:40 (l1). Supported by [](
1,500
true
false
false
false
false
true
false
false
false
false
6,980
36C
Problem - 36C - Codeforces =============== xa0 []( []( [Register]( [Home]( [Top]( [Catalog]( [Contests]( [Gym]( [Problemset]( [Groups]( [Rating]( [Edu]( [API]( [Calendar]( [Help]( [Rayan]( → Attention The package for this problem was not updated by the problem writer or Codeforces administration after we've upgraded the judging servers. To adjust the time limit constraint, a solution execution time will be multiplied by 2. For example, if your solution works for 400 ms on judging servers, then the value 800 ms will be displayed and used to determine the verdict. [Codeforces Beta Round 36]( --- 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 geometry implementation *2200 No tag edit access → Contest materials [Announcement]( "Codeforces Beta Round #35 (Div. 2) and Codeforces Beta Round #36") [Tutorial #1]( "Codeforces Beta Round #36 (Problem E solution)") [Tutorial #2 (en)]( "Codeforces Beta Round #36 (Problem C Tutorial)") [Tutorial #3]( "Codeforces Beta Round #36 (Problem B solution)") [Tutorial #4]( "Codeforces Beta Round #36 (Problem D solution)") [Tutorial #5]( "Codeforces Beta Round #36 (Problem A Tutorial)") [Tutorial #6 (en)]( "Codeforces Beta Round #36") [Problems]( [Submit]( [Status]( [Standings]( [Custom test]( The problem statement has recently been changed. [View the changes.]( Bowls time limit per test 2 seconds memory limit per test 64 megabytes input input.txt output output.txt Once Petya was in such a good mood that he decided to help his mum with the washing-up. There were _n_ dirty bowls in the sink. From the geometrical point of view each bowl looks like a blunted cone. We can disregard the width of the walls and bottom. Petya puts the clean bowls one on another naturally, i. e. so that their vertical axes coincide (see the picture). You will be given the order in which Petya washes the bowls. Determine the height of the construction, i.e. the distance from the bottom of the lowest bowl to the top of the highest one. Input The first input line contains integer _n_ (1u2009≤u2009_n_u2009≤u20093000). Each of the following _n_ lines contains 3 integers _h_, _r_ and _R_ (1u2009≤u2009_h_u2009≤u200910000,u20091u2009≤u2009_r_u2009<u2009_R_u2009≤u200910000). They are the height of a bowl, the radius of its bottom and the radius of its top. The plates are given in the order Petya puts them on the table. Output Output the height of the plate pile accurate to at least 10u2009-u20096. Examples Input 2 40 10 50 60 20 30 Output 70.00000000 Input 3 50 30 80 35 25 70 40 10 90 Output 55.00000000 [Codeforces]( (c) Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 12:43:20 (h1). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name ---
2,200
false
false
true
false
false
false
false
false
false
false
9,816
1736C2
This is the hard version of this problem. In this version, we have queries. Note that we do not have multiple test cases in this version. You can make hacks only if both versions of the problem are solved. An array $$$b$$$ of length $$$m$$$ is good if for all $$$i$$$ the $$$i$$$-th element is greater than or equal to $$$i$$$. In other words, $$$b$$$ is good if and only if $$$b_i geq i$$$ for all $$$i$$$ ($$$1 leq i leq m$$$). You are given an array $$$a$$$ consisting of $$$n$$$ positive integers, and you are asked $$$q$$$ queries. In each query, you are given two integers $$$p$$$ and $$$x$$$ ($$$1 leq p,x leq n$$$). You have to do $$$a_p := x$$$ (assign $$$x$$$ to $$$a_p$$$). In the updated array, find the number of pairs of indices $$$(l, r)$$$, where $$$1 le l le r le n$$$, such that the array $$$[a_l, a_{l+1}, ldots, a_r]$$$ is good. Note that all queries are independent, which means after each query, the initial array $$$a$$$ is restored. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$). The third line contains an integer $$$q$$$ ($$$1 leq q leq 2 cdot 10^5$$$) — the number of queries. Each of the next $$$q$$$ lines contains two integers $$$p_j$$$ and $$$x_j$$$ ($$$1 leq p_j, x_j leq n$$$) – the description of the $$$j$$$-th query. Output For each query, print the number of suitable pairs of indices after making the change. Examples Input 4 2 4 1 4 3 2 4 3 3 2 1 Input 5 1 1 3 2 1 3 1 3 2 5 4 5 Note Here are notes for first example. In first query, after update $$$a=[2,4,1,4]$$$. Now $$$(1,1)$$$, $$$(2,2)$$$, $$$(3,3)$$$, $$$(4,4)$$$, $$$(1,2)$$$, and $$$(3,4)$$$ are suitable pairs. In second query, after update $$$a=[2,4,3,4]$$$. Now all subarrays of $$$a$$$ are good. In third query, after update $$$a=[2,1,1,4]$$$. Now $$$(1,1)$$$, $$$(2,2)$$$, $$$(3,3)$$$, $$$(4,4)$$$, and $$$(3,4)$$$ are suitable.
2,400
false
false
false
true
true
false
false
false
false
false
1,871
1874E
It is well known that quick sort works by randomly selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. But Jellyfish thinks that choosing a random element is just a waste of time, so she always chooses the first element to be the pivot. The time her code needs to run can be calculated by the following pseudocode: function fun(A) if A.length > 0 let L[1 ... L.length] and R[1 ... R.length] be new arrays L.length = R.length = 0 for i = 2 to A.length if A[i] < A[1] L.length = L.length + 1 L[L.length] = A[i] else R.length = R.length + 1 R[R.length] = A[i] return A.length + fun(L) + fun(R) else return 0 Now you want to show her that her code is slow. When the function $$$mathrm{fun(A)}$$$ is greater than or equal to $$$lim$$$, her code will get $$$ ext{Time Limit Exceeded}$$$. You want to know how many distinct permutations $$$P$$$ of $$$[1, 2, dots, n]$$$ satisfies $$$mathrm{fun(P)} geq lim$$$. Because the answer may be large, you will only need to find the answer modulo $$$10^9+7$$$. Input The only line of the input contains two integers $$$n$$$ and $$$lim$$$ ($$$1 leq n leq 200$$$, $$$1 leq lim leq 10^9$$$). Output Output the number of different permutations that satisfy the condition modulo $$$10^9+7$$$. Note In the first example, $$$P = [1, 4, 2, 3]$$$ satisfies the condition, because: $$$mathrm{fun([1, 4, 2, 3]) = 4 + fun([4, 2, 3]) = 7 + fun([2, 3]) = 9 + fun([3]) = 10}$$$ Do remember to output the answer modulo $$$10^9+7$$$.
3,000
true
false
false
true
false
false
false
false
false
false
1,032
1706D1
This is the easy version of the problem. The only difference between the versions is the constraints on $$$n$$$, $$$k$$$, $$$a_i$$$, and the sum of $$$n$$$ over all test cases. You can make hacks only if both versions of the problem are solved. Note the unusual memory limit. You are given an array of integers $$$a_1, a_2, ldots, a_n$$$ of length $$$n$$$, and an integer $$$k$$$. The cost of an array of integers $$$p_1, p_2, ldots, p_n$$$ of length $$$n$$$ is $$$$$$maxlimits_{1 le i le n}left(left lfloor frac{a_i}{p_i} ight floor ight) - minlimits_{1 le i le n}left(left lfloor frac{a_i}{p_i} ight floor ight).$$$$$$ Here, $$$lfloor frac{x}{y} floor$$$ denotes the integer part of the division of $$$x$$$ by $$$y$$$. Find the minimum cost of an array $$$p$$$ such that $$$1 le p_i le k$$$ for all $$$1 le i le n$$$. Input The first line contains a 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$$$ and $$$k$$$ ($$$1 le n, k le 3000$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_1 le a_2 le ldots le a_n le 3000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. Output For each test case, print a single integerxa0— the minimum possible cost of an array $$$p$$$ satisfying the condition above. Example Input 7 5 2 4 5 6 8 11 5 12 4 5 6 8 11 3 1 2 9 15 7 3 2 3 5 5 6 9 10 6 56 54 286 527 1436 2450 2681 3 95 16 340 2241 2 2 1 3 Note In the first test case, the optimal array is $$$p = [1, 1, 1, 2, 2]$$$. The resulting array of values of $$$lfloor frac{a_i}{p_i} floor$$$ is $$$[4, 5, 6, 4, 5]$$$. The cost of $$$p$$$ is $$$maxlimits_{1 le i le n}(lfloor frac{a_i}{p_i} floor) - minlimits_{1 le i le n}(lfloor frac{a_i}{p_i} floor) = 6 - 4 = 2$$$. We can show that there is no array (satisfying the condition from the statement) with a smaller cost. In the second test case, one of the optimal arrays is $$$p = [12, 12, 12, 12, 12]$$$, which results in all $$$lfloor frac{a_i}{p_i} floor$$$ being $$$0$$$. In the third test case, the only possible array is $$$p = [1, 1, 1]$$$.
1,700
false
true
false
false
false
true
false
false
false
false
2,037
1748F
You have an array $$$a_0, a_1, ldots, a_{n-1}$$$ of length $$$n$$$. Initially, $$$a_i = 2^i$$$ for all $$$0 le i lt n$$$. Note that array $$$a$$$ is zero-indexed. You want to reverse this array (that is, make $$$a_i$$$ equal to $$$2^{n-1-i}$$$ for all $$$0 le i lt n$$$). To do this, you can perform the following operation no more than $$$250,000$$$ times: Select an integer $$$i$$$ ($$$0 le i lt n$$$) and replace $$$a_i$$$ by $$$a_i oplus a_{(i+1)bmod n}$$$. Here, $$$oplus$$$ denotes the [bitwise XOR operation]( task is to find any sequence of operations that will result in the array $$$a$$$ being reversed. It can be shown that under the given constraints, a solution always exists. Output On the first line print one integer $$$k$$$ ($$$0 le k le 250,000$$$)xa0— the number of operations performed. On the second line print $$$k$$$ integers $$$i_1,i_2,ldots,i_k$$$ ($$$0 le i_j lt n$$$). Here, $$$i_j$$$ should be the integer selected on the $$$j$$$-th operation. Note that you don't need to minimize the number of operations. Note In the notes, the elements on which the operations are performed are colored red. In the first test case, array $$$a$$$ will change in the following way: $$$[1,color{red}{2}] ightarrow [color{red}{1},3] ightarrow [2,color{red}{3}] ightarrow [2,1]$$$. In the second test case, array $$$a$$$ will change in the following way: $$$[1,color{red}{2},4] ightarrow [color{red}{1},6,4] ightarrow [7,color{red}{6},4] ightarrow [color{red}{7},2,4] ightarrow [5,2,color{red}{4}] ightarrow [5,color{red}{2},1] ightarrow [color{red}{5},3,1] ightarrow [6,color{red}{3},1] ightarrow [color{red}{6},2,1] ightarrow [4,2,1]$$$.
3,000
false
false
false
false
false
true
false
false
false
false
1,792
1931G
You have a one-dimensional puzzle, all the elements of which need to be put in one row, connecting with each other. All the puzzle elements are completely white and distinguishable from each other only if they have different shapes. Each element has straight borders at the top and bottom, and on the left and right it has connections, each of which can be a protrusion or a recess. You cannot rotate the elements. You can see that there are exactly $$$4$$$ types of elements. Two elements can be connected if the right connection of the left element is opposite to the left connection of the right element. All possible types of elements. The puzzle contains $$$c_1, c_2, c_3, c_4$$$ elements of each type. The puzzle is considered complete if you have managed to combine all elements into one long chain. You want to know how many ways this can be done. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 2 cdot 10^5$$$) — the number of input test cases. The descriptions of the test cases follow. The description of each test case contains $$$4$$$ integers $$$c_i$$$ ($$$0 le c_i le 10^6$$$) — the number of elements of each type, respectively. It is guaranteed that the sum of $$$c_i$$$ for all test cases does not exceed $$$4 cdot 10^6$$$. Output For each test case, print one integer — the number of possible ways to solve the puzzle. Two methods are considered different if there is $$$i$$$, such that the types of elements at the $$$i$$$ position in these methods differ. Since the answer can be very large, output it modulo $$$998244353$$$. If it is impossible to solve the puzzle, print $$$0$$$. Example Input 11 1 1 1 1 1 2 5 10 4 6 100 200 900000 900000 900000 900000 0 0 0 0 0 0 566 239 1 0 0 0 100 0 100 0 0 0 0 4 5 5 0 2 5 4 0 5 Output 4 66 0 794100779 1 0 1 0 1 36 126
2,000
true
false
false
false
false
false
true
false
false
false
695
1488D
Polycarp has decided to do a problemsolving marathon. He wants to solve $$$s$$$ problems in $$$n$$$ days. Let $$$a_i$$$ be the number of problems he solves during the $$$i$$$-th day. He wants to find a distribution of problems into days such that: $$$a_i$$$ is an integer value for all $$$i$$$ from $$$1$$$ to $$$n$$$; $$$a_i ge 1$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$; $$$a_{i + 1} ge a_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$; $$$a_{i + 1} le 2 cdot a_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$; $$$a_n$$$ is maximized. Note that $$$a_1$$$ can be arbitrarily large. What is the largest value of $$$a_n$$$ Polycarp can obtain? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The only line of each testcase contains two integers $$$n$$$ and $$$s$$$ ($$$1 le n le s le 10^{18}$$$)xa0— the number of days and the number of problems Polycarp wants to solve. It's guaranteed that the distribution always exists within the given constraints. Output For each testcase print a single integerxa0— the maximum value of $$$a_n$$$. Note In the first testcase there is only one distribution: $$$[15]$$$. In the second testcase the distribution that maximizes $$$a_n$$$ is: $$$[2, 3, 4]$$$. In the third testcase the distribution that maximizes $$$a_n$$$ is: $$$[2, 4]$$$. $$$[3, 3]$$$ is a valid distribution but $$$a_n=3$$$ which is smaller than $$$4$$$. $$$[1, 5]$$$ is not a valid distribution because $$$5 > 2 cdot 1$$$.
1,900
false
true
false
false
false
false
false
false
false
false
3,236
549B
The Looksery company, consisting of _n_ staff members, is planning another big party. Every employee has his phone number and the phone numbers of his friends in the phone book. Everyone who comes to the party, sends messages to his contacts about how cool it is. At the same time everyone is trying to spend as much time on the fun as possible, so they send messages to everyone without special thinking, moreover, each person even sends a message to himself or herself. Igor and Max, Looksery developers, started a dispute on how many messages each person gets. Igor indicates _n_ numbers, the _i_-th of which indicates how many messages, in his view, the _i_-th employee is going to take. If Igor guesses correctly at least one of these numbers, he wins, otherwise Max wins. You support Max in this debate, so you need, given the contact lists of the employees, to determine whether there is a situation where Igor loses. Specifically, you need to determine which employees should come to the party, and which should not, so after all the visitors send messages to their contacts, each employee received a number of messages that is different from what Igor stated. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of employees of company Looksery. Next _n_ lines contain the description of the contact lists of the employees. The _i_-th of these lines contains a string of length _n_, consisting of digits zero and one, specifying the contact list of the _i_-th employee. If the _j_-th character of the _i_-th string equals 1, then the _j_-th employee is in the _i_-th employee's contact list, otherwise he isn't. It is guaranteed that the _i_-th character of the _i_-th line is always equal to 1. The last line contains _n_ space-separated integers: _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009_n_), where _a__i_ represents the number of messages that the _i_-th employee should get according to Igor. Output In the first line print a single integer _m_ — the number of employees who should come to the party so that Igor loses the dispute. In the second line print _m_ space-separated integers — the numbers of these employees in an arbitrary order. If Igor wins the dispute in any case, print -1. If there are multiple possible solutions, print any of them. Examples Input 4 1111 0101 1110 0001 1 0 1 0 Note In the first sample Igor supposes that the first employee will receive 0 messages. Since he isn't contained in any other contact list he must come to the party in order to receive one message from himself. If he is the only who come to the party then he will receive 1 message, the second employee will receive 0 messages and the third will also receive 1 message. Thereby Igor won't guess any number. In the second sample if the single employee comes to the party he receives 1 message and Igor wins, so he shouldn't do it. In the third sample the first employee will receive 2 messages, the second — 3, the third — 2, the fourth — 3.
2,300
false
true
false
false
false
true
false
false
false
true
7,644
1677B
Tokitsukaze is arranging a meeting. There are $$$n$$$ rows and $$$m$$$ columns of seats in the meeting hall. There are exactly $$$n cdot m$$$ students attending the meeting, including several naughty students and several serious students. The students are numerated from $$$1$$$ to $$$ncdot m$$$. The students will enter the meeting hall in order. When the $$$i$$$-th student enters the meeting hall, he will sit in the $$$1$$$-st column of the $$$1$$$-st row, and the students who are already seated will move back one seat. Specifically, the student sitting in the $$$j$$$-th ($$$1leq j leq m-1$$$) column of the $$$i$$$-th row will move to the $$$(j+1)$$$-th column of the $$$i$$$-th row, and the student sitting in $$$m$$$-th column of the $$$i$$$-th row will move to the $$$1$$$-st column of the $$$(i+1)$$$-th row. For example, there is a meeting hall with $$$2$$$ rows and $$$2$$$ columns of seats shown as below: There will be $$$4$$$ students entering the meeting hall in order, represented as a binary string "1100", of which '0' represents naughty students and '1' represents serious students. The changes of seats in the meeting hall are as follows: Denote a row or a column good if and only if there is at least one serious student in this row or column. Please predict the number of good rows and columns just after the $$$i$$$-th student enters the meeting hall, for all $$$i$$$. Input The first contains a single positive integer $$$t$$$ ($$$1 leq t leq 10,000$$$)xa0— the number of test cases. For each test case, the first line contains two integers $$$n$$$, $$$m$$$ ($$$1 leq n,m leq 10^6$$$; $$$1 leq n cdot m leq 10^6$$$), denoting there are $$$n$$$ rows and $$$m$$$ columns of seats in the meeting hall. The second line contains a binary string $$$s$$$ of length $$$n cdot m$$$, consisting only of zeros and ones. If $$$s_i$$$ equal to '0' represents the $$$i$$$-th student is a naughty student, and $$$s_i$$$ equal to '1' represents the $$$i$$$-th student is a serious student. It is guaranteed that the sum of $$$n cdot m$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, print a single line with $$$n cdot m$$$ integersxa0— the number of good rows and columns just after the $$$i$$$-th student enters the meeting hall. Example Input 3 2 2 1100 4 2 11001101 2 4 11001101 Output 2 3 4 3 2 3 4 3 5 4 6 5 2 3 3 3 4 4 4 5 Note The first test case is shown in the statement. After the $$$1$$$-st student enters the meeting hall, there are $$$2$$$ good rows and columns: the $$$1$$$-st row and the $$$1$$$-st column. After the $$$2$$$-nd student enters the meeting hall, there are $$$3$$$ good rows and columns: the $$$1$$$-st row, the $$$1$$$-st column and the $$$2$$$-nd column. After the $$$3$$$-rd student enters the meeting hall, the $$$4$$$ rows and columns are all good. After the $$$4$$$-th student enters the meeting hall, there are $$$3$$$ good rows and columns: the $$$2$$$-nd row, the $$$1$$$-st column and the $$$2$$$-nd column.
1,700
true
false
true
false
true
false
false
false
false
false
2,205
1823B
Let's define a permutation of length $$$n$$$ as an array $$$p$$$ of length $$$n$$$, which contains every number from $$$1$$$ to $$$n$$$ exactly once. You are given a permutation $$$p_1, p_2, dots, p_n$$$ and a number $$$k$$$. You need to sort this permutation in the ascending order. In order to do it, you can repeat the following operation any number of times (possibly, zero): pick two elements of the permutation $$$p_i$$$ and $$$p_j$$$ such that $$$i - j = k$$$, and swap them. Unfortunately, some permutations can't be sorted with some fixed numbers $$$k$$$. For example, it's impossible to sort $$$[2, 4, 3, 1]$$$ with $$$k = 2$$$. That's why, before starting the sorting, you can make at most one preliminary exchange: choose any pair $$$p_i$$$ and $$$p_j$$$ and swap them. Your task is to: 1. check whether is it possible to sort the permutation without any preliminary exchanges, 2. if it's not, check, whether is it possible to sort the permutation using exactly one preliminary exchange. For example, if $$$k = 2$$$ and permutation is $$$[2, 4, 3, 1]$$$, then you can make a preliminary exchange of $$$p_1$$$ and $$$p_4$$$, which will produce permutation $$$[1, 4, 3, 2]$$$, which is possible to sort with given $$$k$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 2 cdot 10^5$$$; $$$1 le k le n - 1$$$)xa0— length of the permutation, and a distance between elements that can be swapped. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$)xa0— elements of the permutation $$$p$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10 ^ 5$$$. Note In the first test case, there is no need in preliminary exchange, as it is possible to swap $$$(p_1, p_2)$$$ and then $$$(p_2, p_3)$$$. In the second test case, there is no need in preliminary exchange, as it is possible to swap $$$(p_1, p_3)$$$ and then $$$(p_2, p_4)$$$. In the third test case, you need to apply preliminary exchange to $$$(p_2, p_3)$$$. After that the permutation becomes $$$[3, 4, 1, 2]$$$ and can be sorted with $$$k = 2$$$.
900
true
false
false
false
false
false
false
false
false
false
1,342
2025A
There are two screens which can display sequences of uppercase Latin letters. Initially, both screens display nothing. In one second, you can do one of the following two actions: choose a screen and an uppercase Latin letter, and append that letter to the end of the sequence displayed on that screen; choose a screen and copy the sequence from it to the other screen, overwriting the sequence that was displayed on the other screen. You have to calculate the minimum number of seconds you have to spend so that the first screen displays the sequence $$$s$$$, and the second screen displays the sequence $$$t$$$. Input The first line contains one integer $$$q$$$ ($$$1 le q le 500$$$) — the number of test cases. Each test case consists of two lines. The first line contains the string $$$s$$$, and the second line contains the string $$$t$$$ ($$$1 le s, t le 100$$$). Both strings consist of uppercase Latin letters. Output For each test case, print one integer — the minimum possible number of seconds you have to spend so that the first screen displays the sequence $$$s$$$, and the second screen displays the sequence $$$t$$$. Example Input 3 GARAGE GARAGEFORSALE ABCDE AABCD TRAINING DRAINING Note In the first test case, the following sequence of actions is possible: spend $$$6$$$ seconds to write the sequence GARAGE on the first screen; copy the sequence from the first screen to the second screen; spend $$$7$$$ seconds to complete the sequence on the second screen by writing FORSALE. In the second test case, the following sequence of actions is possible: spend $$$1$$$ second to write the sequence A on the second screen; copy the sequence from the second screen to the first screen; spend $$$4$$$ seconds to complete the sequence on the first screen by writing BCDE; spend $$$4$$$ seconds to complete the sequence on the second screen by writing ABCD. In the third test case, the fastest way to display the sequences is to type both of them character by character without copying, and this requires $$$16$$$ seconds.
800
false
true
false
false
false
false
false
false
true
false
123
492E
Vanya decided to walk in the field of size _n_u2009×u2009_n_ cells. The field contains _m_ apple trees, the _i_-th apple tree is at the cell with coordinates (_x__i_,u2009_y__i_). Vanya moves towards vector (_dx_,u2009_dy_). That means that if Vanya is now at the cell (_x_,u2009_y_), then in a second he will be at cell . The following condition is satisfied for the vector: , where is the largest integer that divides both _a_ and _b_. Vanya ends his path when he reaches the square he has already visited. Vanya wonders, from what square of the field he should start his path to see as many apple trees as possible. Input The first line contains integers _n_,u2009_m_,u2009_dx_,u2009_dy_(1u2009≤u2009_n_u2009≤u2009106, 1u2009≤u2009_m_u2009≤u2009105, 1u2009≤u2009_dx_,u2009_dy_u2009≤u2009_n_) — the size of the field, the number of apple trees and the vector of Vanya's movement. Next _m_ lines contain integers _x__i_,u2009_y__i_ (0u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_u2009-u20091) — the coordinates of apples. One cell may contain multiple apple trees. Output Print two space-separated numbers — the coordinates of the cell from which you should start your path. If there are several answers you are allowed to print any of them. Examples Input 5 5 2 3 0 0 1 2 1 3 2 4 3 1 Note In the first sample Vanya's path will look like: (1,u20093)u2009-u2009(3,u20091)u2009-u2009(0,u20094)u2009-u2009(2,u20092)u2009-u2009(4,u20090)u2009-u2009(1,u20093) In the second sample: (0,u20090)u2009-u2009(1,u20091)u2009-u2009(0,u20090)
2,000
true
false
false
false
false
false
false
false
false
false
7,864
1234C
You are given a system of pipes. It consists of two rows, each row consists of $$$n$$$ pipes. The top left pipe has the coordinates $$$(1, 1)$$$ and the bottom right — $$$(2, n)$$$. There are six types of pipes: two types of straight pipes and four types of curved pipes. Here are the examples of all six types: Types of pipes You can turn each of the given pipes $$$90$$$ degrees clockwise or counterclockwise arbitrary (possibly, zero) number of times (so the types $$$1$$$ and $$$2$$$ can become each other and types $$$3, 4, 5, 6$$$ can become each other). You want to turn some pipes in a way that the water flow can start at $$$(1, 0)$$$ (to the left of the top left pipe), move to the pipe at $$$(1, 1)$$$, flow somehow by connected pipes to the pipe at $$$(2, n)$$$ and flow right to $$$(2, n + 1)$$$. Pipes are connected if they are adjacent in the system and their ends are connected. Here are examples of connected pipes: Examples of connected pipes Let's describe the problem using some example: The first example input And its solution is below: The first example answer As you can see, the water flow is the poorly drawn blue line. To obtain the answer, we need to turn the pipe at $$$(1, 2)$$$ $$$90$$$ degrees clockwise, the pipe at $$$(2, 3)$$$ $$$90$$$ degrees, the pipe at $$$(1, 6)$$$ $$$90$$$ degrees, the pipe at $$$(1, 7)$$$ $$$180$$$ degrees and the pipe at $$$(2, 7)$$$ $$$180$$$ degrees. Then the flow of water can reach $$$(2, n + 1)$$$ from $$$(1, 0)$$$. You have to answer $$$q$$$ independent queries. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 10^4$$$) — the number of queries. Then $$$q$$$ queries follow. Each query consists of exactly three lines. The first line of the query contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of pipes in each row. The next two lines contain a description of the first and the second rows correspondingly. Each row description consists of $$$n$$$ digits from $$$1$$$ to $$$6$$$ without any whitespaces between them, each digit corresponds to the type of pipe in the corresponding cell. See the problem statement to understand which digits correspond to which types of pipes. It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 cdot 10^5$$$. Output For the $$$i$$$-th query print the answer for it — "YES" (without quotes) if it is possible to turn some pipes in a way that the water flow can reach $$$(2, n + 1)$$$ from $$$(1, 0)$$$, and "NO" otherwise.
1,500
false
false
true
true
false
false
false
false
false
false
4,522
653D
Niwel is a little golden bear. As everyone knows, bears live in forests, but Niwel got tired of seeing all the trees so he decided to move to the city. In the city, Niwel took on a job managing bears to deliver goods. The city that he lives in can be represented as a directed graph with _n_ nodes and _m_ edges. Each edge has a weight capacity. A delivery consists of a bear carrying weights with their bear hands on a simple path from node 1 to node _n_. The total weight that travels across a particular edge must not exceed the weight capacity of that edge. Niwel has exactly _x_ bears. In the interest of fairness, no bear can rest, and the weight that each bear carries must be exactly the same. However, each bear may take different paths if they like. Niwel would like to determine, what is the maximum amount of weight he can deliver (it's the sum of weights carried by bears). Find the maximum weight. Input The first line contains three integers _n_, _m_ and _x_ (2u2009≤u2009_n_u2009≤u200950, 1u2009≤u2009_m_u2009≤u2009500, 1u2009≤u2009_x_u2009≤u2009100u2009000)xa0— the number of nodes, the number of directed edges and the number of bears, respectively. Each of the following _m_ lines contains three integers _a__i_, _b__i_ and _c__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_, 1u2009≤u2009_c__i_u2009≤u20091u2009000u2009000). This represents a directed edge from node _a__i_ to _b__i_ with weight capacity _c__i_. There are no self loops and no multiple edges from one city to the other city. More formally, for each _i_ and _j_ that _i_u2009≠u2009_j_ it's guaranteed that _a__i_u2009≠u2009_a__j_ or _b__i_u2009≠u2009_b__j_. It is also guaranteed that there is at least one path from node 1 to node _n_. Output Print one real value on a single linexa0— the maximum amount of weight Niwel can deliver if he uses exactly _x_ bears. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct if . Examples Input 4 4 3 1 2 2 2 4 1 1 3 1 3 4 2 Input 5 11 23 1 2 3 2 3 4 3 4 5 4 5 6 1 3 4 2 4 5 3 5 6 1 4 2 2 5 3 1 5 2 3 2 30 Note In the first sample, Niwel has three bears. Two bears can choose the path , while one bear can choose the path . Even though the bear that goes on the path can carry one unit of weight, in the interest of fairness, he is restricted to carry 0.5 units of weight. Thus, the total weight is 1.5 units overall. Note that even though Niwel can deliver more weight with just 2 bears, he must use exactly 3 bears on this day.
2,200
false
false
false
false
false
false
false
false
false
true
7,201
1038F
You are given a binary string $$$s$$$. Find the number of distinct cyclical binary strings of length $$$n$$$ which contain $$$s$$$ as a substring. The cyclical string $$$t$$$ contains $$$s$$$ as a substring if there is some cyclical shift of string $$$t$$$, such that $$$s$$$ is a substring of this cyclical shift of $$$t$$$. For example, the cyclical string "000111" contains substrings "001", "01110" and "10", but doesn't contain "0110" and "10110". Two cyclical strings are called different if they differ from each other as strings. For example, two different strings, which differ from each other by a cyclical shift, are still considered different cyclical strings. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 40$$$)xa0— the length of the target string $$$t$$$. The next line contains the string $$$s$$$ ($$$1 le s le n$$$)xa0— the string which must be a substring of cyclical string $$$t$$$. String $$$s$$$ contains only characters '0' and '1'. Output Print the only integerxa0— the number of distinct cyclical binary strings $$$t$$$, which contain $$$s$$$ as a substring. Note In the first example, there are three cyclical strings, which contain "0"xa0— "00", "01" and "10". In the second example, there are only two such stringsxa0— "1010", "0101".
2,900
false
false
false
true
false
false
false
false
true
false
5,503
837F
Problem - 837F - Codeforces =============== xa0 []( []( [Register]( [Home]( [Top]( [Catalog]( [Contests]( [Gym]( [Problemset]( [Groups]( [Rating]( [Edu]( [API]( [Calendar]( [Help]( [Rayan]( [Educational Codeforces Round 26]( --- 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 brute force combinatorics math matrices *2400 No tag edit access → Contest materials [Announcement]( "Educational Codeforces Round 26") [Tutorial]( "53662") [Problems]( [Submit]( [Status]( [Standings]( [Custom test]( The problem statement has recently been changed. [View the changes.]( Prefix Sums time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Consider the function _p_(_x_), where _x_ is an array of _m_ integers, which returns an array _y_ consisting of _m_u2009+u20091 integers such that _y__i_ is equal to the sum of first _i_ elements of array _x_ (0u2009≤u2009_i_u2009≤u2009_m_). You have an infinite sequence of arrays _A_0,u2009_A_1,u2009_A_2..., where _A_0 is given in the input, and for each _i_u2009≥u20091 _A__i_u2009=u2009_p_(_A__i_u2009-u20091). Also you have a positive integer _k_. You have to find minimum possible _i_ such that _A__i_ contains a number which is larger or equal than _k_. Input The first line contains two integers _n_ and _k_ (2u2009≤u2009_n_u2009≤u2009200000, 1u2009≤u2009_k_u2009≤u20091018). _n_ is the size of array _A_0. The second line contains _n_ integers _A_00,u2009_A_01... _A_0_n_u2009-u20091 — the elements of _A_0 (0u2009≤u2009_A_0_i_u2009≤u2009109). At least two elements of _A_0 are positive. Output Print the minimum _i_ such that _A__i_ contains a number which is larger or equal than _k_. Examples Input 2 2 1 1 Output 1 Input 3 6 1 1 1 Output 2 Input 3 1 1 0 1 Output 0 [Codeforces]( (c) Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 09:38:54 (f1). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name ---
2,400
true
false
false
false
false
false
true
false
false
false
6,416
873C
Ivan is playing a strange game. He has a matrix _a_ with _n_ rows and _m_ columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows: 1. Initially Ivan's score is 0; 2. In each column, Ivan will find the topmost 1 (that is, if the current column is _j_, then he will find minimum _i_ such that _a__i_,u2009_j_u2009=u20091). If there are no 1's in the column, this column is skipped; 3. Ivan will look at the next _min_(_k_,u2009_n_u2009-u2009_i_u2009+u20091) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score. Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score. Input The first line contains three integer numbers _n_, _m_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_m_u2009≤u2009100). Then _n_ lines follow, _i_-th of them contains _m_ integer numbers — the elements of _i_-th row of matrix _a_. Each number is either 0 or 1. Output Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score. Examples Input 4 3 2 0 1 0 1 0 1 0 1 0 1 1 1 Note In the first example Ivan will replace the element _a_1,u20092.
1,600
false
true
false
false
false
false
false
false
false
false
6,252
1063B
You are playing some computer game. One of its levels puts you in a maze consisting of _n_ lines, each of which contains _m_ cells. Each cell either is free or is occupied by an obstacle. The starting cell is in the row _r_ and column _c_. In one step you can move one square up, left, down or right, if the target cell is not occupied by an obstacle. You can't move beyond the boundaries of the labyrinth. Unfortunately, your keyboard is about to break, so you can move left no more than _x_ times and move right no more than _y_ times. There are no restrictions on the number of moves up and down since the keys used to move up and down are in perfect condition. Now you would like to determine for each cell whether there exists a sequence of moves that will put you from the starting cell to this particular one. How many cells of the board have this property? Input The first line contains two integers _n_, _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20092000)xa0— the number of rows and the number columns in the labyrinth respectively. The second line contains two integers _r_, _c_ (1u2009≤u2009_r_u2009≤u2009_n_, 1u2009≤u2009_c_u2009≤u2009_m_)xa0— index of the row and index of the column that define the starting cell. The third line contains two integers _x_, _y_ (0u2009≤u2009_x_,u2009_y_u2009≤u2009109)xa0— the maximum allowed number of movements to the left and to the right respectively. The next _n_ lines describe the labyrinth. Each of them has length of _m_ and consists only of symbols '.' and '*'. The _j_-th character of the _i_-th line corresponds to the cell of labyrinth at row _i_ and column _j_. Symbol '.' denotes the free cell, while symbol '*' denotes the cell with an obstacle. It is guaranteed, that the starting cell contains no obstacles. Output Print exactly one integerxa0— the number of cells in the labyrinth, which are reachable from starting cell, including the starting cell itself. Examples Input 4 5 3 2 1 2 ..... .***. ...* *.... Input 4 4 2 2 0 1 .... ..*. .... .... Note Cells, reachable in the corresponding example, are marked with '+'. First example: +++.. +***. +++* *+++. Second example: .++. .+*. .++. .++.
1,800
false
false
false
false
false
false
false
false
false
true
5,395
808G
Berland has a long and glorious history. To increase awareness about it among younger citizens, King of Berland decided to compose an anthem. Though there are lots and lots of victories in history of Berland, there is the one that stand out the most. King wants to mention it in the anthem as many times as possible. He has already composed major part of the anthem and now just needs to fill in some letters. King asked you to help him with this work. The anthem is the string _s_ of no more than 105 small Latin letters and question marks. The most glorious victory is the string _t_ of no more than 105 small Latin letters. You should replace all the question marks with small Latin letters in such a way that the number of occurrences of string _t_ in string _s_ is maximal. Note that the occurrences of string _t_ in _s_ can overlap. Check the third example for clarification. Input The first line contains string of small Latin letters and question marks _s_ (1u2009≤u2009_s_u2009≤u2009105). The second line contains string of small Latin letters _t_ (1u2009≤u2009_t_u2009≤u2009105). Product of lengths of strings _s_·_t_ won't exceed 107. Output Output the maximum number of occurrences of string _t_ you can achieve by replacing all the question marks in string _s_ with small Latin letters. Examples Input winlose???winl???w?? win Note In the first example the resulting string _s_ is "winlosewinwinlwinwin" In the second example the resulting string _s_ is "glorytoreorand". The last letter of the string can be arbitrary. In the third example occurrences of string _t_ are overlapping. String _s_ with maximal number of occurrences of _t_ is "abcabcab".
2,300
false
false
false
true
false
false
false
false
true
false
6,529
232C
John Doe decided that some mathematical object must be named after him. So he invented the Doe graphs. The Doe graphs are a family of undirected graphs, each of them is characterized by a single non-negative number — its order. We'll denote a graph of order _k_ as _D_(_k_), and we'll denote the number of vertices in the graph _D_(_k_) as _D_(_k_). Then let's define the Doe graphs as follows: _D_(0) consists of a single vertex, that has number 1. _D_(1) consists of two vertices with numbers 1 and 2, connected by an edge. _D_(_n_) for _n_u2009≥u20092 is obtained from graphs _D_(_n_u2009-u20091) and _D_(_n_u2009-u20092). _D_(_n_u2009-u20091) and _D_(_n_u2009-u20092) are joined in one graph, at that numbers of all vertices of graph _D_(_n_u2009-u20092) increase by _D_(_n_u2009-u20091) (for example, vertex number 1 of graph _D_(_n_u2009-u20092) becomes vertex number 1u2009+u2009_D_(_n_u2009-u20091)). After that two edges are added to the graph: the first one goes between vertices with numbers _D_(_n_u2009-u20091) and _D_(_n_u2009-u20091)u2009+u20091, the second one goes between vertices with numbers _D_(_n_u2009-u20091)u2009+u20091 and 1. Note that the definition of graph _D_(_n_) implies, that _D_(_n_) is a connected graph, its vertices are numbered from 1 to _D_(_n_). The picture shows the Doe graphs of order 1, 2, 3 and 4, from left to right. John thinks that Doe graphs are that great because for them exists a polynomial algorithm for the search of Hamiltonian path. However, your task is to answer queries of finding the shortest-length path between the vertices _a__i_ and _b__i_ in the graph _D_(_n_). A path between a pair of vertices _u_ and _v_ in the graph is a sequence of vertices _x_1, _x_2, ..., _x__k_ (_k_u2009>u20091) such, that _x_1u2009=u2009_u_, _x__k_u2009=u2009_v_, and for any _i_ (_i_u2009<u2009_k_) vertices _x__i_ and _x__i_u2009+u20091 are connected by a graph edge. The length of path _x_1, _x_2, ..., _x__k_ is number (_k_u2009-u20091). Input The first line contains two integers _t_ and _n_ (1u2009≤u2009_t_u2009≤u2009105;xa01u2009≤u2009_n_u2009≤u2009103) — the number of queries and the order of the given graph. The _i_-th of the next _t_ lines contains two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u20091016, _a__i_u2009≠u2009_b__i_) — numbers of two vertices in the _i_-th query. It is guaranteed that _a__i_,u2009_b__i_u2009≤u2009_D_(_n_). Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier. Output For each query print a single integer on a single line — the length of the shortest path between vertices _a__i_ and _b__i_. Print the answers to the queries in the order, in which the queries are given in the input. Examples Input 10 5 1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 4 3 5 4 5
2,600
false
false
false
true
false
true
false
false
false
true
8,909
625B
A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue their fierce competition. Crucial moment is just around the corner: Gogol is ready to release it's new tablet Lastus 3000. This new device is equipped with specially designed artificial intelligence (AI). Employees of Pineapple did their best to postpone the release of Lastus 3000 as long as possible. Finally, they found out, that the name of the new artificial intelligence is similar to the name of the phone, that Pineapple released 200 years ago. As all rights on its name belong to Pineapple, they stand on changing the name of Gogol's artificial intelligence. Pineapple insists, that the name of their phone occurs in the name of AI as a substring. Because the name of technology was already printed on all devices, the Gogol's director decided to replace some characters in AI name with "#". As this operation is pretty expensive, you should find the minimum number of characters to replace with "#", such that the name of AI doesn't contain the name of the phone as a substring. Substring is a continuous subsequence of a string. Input The first line of the input contains the name of AI designed by Gogol, its length doesn't exceed 100u2009000 characters. Second line contains the name of the phone released by Pineapple 200 years ago, its length doesn't exceed 30. Both string are non-empty and consist of only small English letters. Output Print the minimum number of characters that must be replaced with "#" in order to obtain that the name of the phone doesn't occur in the name of AI as a substring. Note In the first sample AI's name may be replaced with "int#llect". In the second sample Gogol can just keep things as they are. In the third sample one of the new possible names of AI may be "s#ris#ri".
1,200
false
true
false
false
false
true
false
false
true
false
7,332
863C
Ilya is working for the company that constructs robots. Ilya writes programs for entertainment robots, and his current project is "Bob", a new-generation game robot. Ilya's boss wants to know his progress so far. Especially he is interested if Bob is better at playing different games than the previous model, "Alice". So now Ilya wants to compare his robots' performance in a simple game called "1-2-3". This game is similar to the "Rock-Paper-Scissors" game: both robots secretly choose a number from the set {1,u20092,u20093} and say it at the same moment. If both robots choose the same number, then it's a draw and noone gets any points. But if chosen numbers are different, then one of the robots gets a point: 3 beats 2, 2 beats 1 and 1 beats 3. Both robots' programs make them choose their numbers in such a way that their choice in (_i_u2009+u20091)-th game depends only on the numbers chosen by them in _i_-th game. Ilya knows that the robots will play _k_ games, Alice will choose number _a_ in the first game, and Bob will choose _b_ in the first game. He also knows both robots' programs and can tell what each robot will choose depending on their choices in previous game. Ilya doesn't want to wait until robots play all _k_ games, so he asks you to predict the number of points they will have after the final game. Input The first line contains three numbers _k_, _a_, _b_ (1u2009≤u2009_k_u2009≤u20091018, 1u2009≤u2009_a_,u2009_b_u2009≤u20093). Then 3 lines follow, _i_-th of them containing 3 numbers _A__i_,u20091, _A__i_,u20092, _A__i_,u20093, where _A__i_,u2009_j_ represents Alice's choice in the game if Alice chose _i_ in previous game and Bob chose _j_ (1u2009≤u2009_A__i_,u2009_j_u2009≤u20093). Then 3 lines follow, _i_-th of them containing 3 numbers _B__i_,u20091, _B__i_,u20092, _B__i_,u20093, where _B__i_,u2009_j_ represents Bob's choice in the game if Alice chose _i_ in previous game and Bob chose _j_ (1u2009≤u2009_B__i_,u2009_j_u2009≤u20093). Output Print two numbers. First of them has to be equal to the number of points Alice will have, and second of them must be Bob's score after _k_ games. Examples Input 10 2 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 Input 8 1 1 2 2 1 3 3 1 3 1 3 1 1 1 2 1 1 1 2 3 Input 5 1 1 1 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 Note In the second example game goes like this: The fourth and the seventh game are won by Bob, the first game is draw and the rest are won by Alice.
1,800
false
false
true
false
false
false
false
false
false
true
6,292
1555C
Alice and Bob are playing a game on a matrix, consisting of $$$2$$$ rows and $$$m$$$ columns. The cell in the $$$i$$$-th row in the $$$j$$$-th column contains $$$a_{i, j}$$$ coins in it. Initially, both Alice and Bob are standing in a cell $$$(1, 1)$$$. They are going to perform a sequence of moves to reach a cell $$$(2, m)$$$. The possible moves are: Move rightxa0— from some cell $$$(x, y)$$$ to $$$(x, y + 1)$$$; Move downxa0— from some cell $$$(x, y)$$$ to $$$(x + 1, y)$$$. First, Alice makes all her moves until she reaches $$$(2, m)$$$. She collects the coins in all cells she visit (including the starting cell). When Alice finishes, Bob starts his journey. He also performs the moves to reach $$$(2, m)$$$ and collects the coins in all cells that he visited, but Alice didn't. The score of the game is the total number of coins Bob collects. Alice wants to minimize the score. Bob wants to maximize the score. What will the score of the game be if both players play optimally? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The first line of the testcase contains a single integer $$$m$$$ ($$$1 le m le 10^5$$$)xa0— the number of columns of the matrix. The $$$i$$$-th of the next $$$2$$$ lines contain $$$m$$$ integers $$$a_{i,1}, a_{i,2}, dots, a_{i,m}$$$ ($$$1 le a_{i,j} le 10^4$$$)xa0— the number of coins in the cell in the $$$i$$$-th row in the $$$j$$$-th column of the matrix. The sum of $$$m$$$ over all testcases doesn't exceed $$$10^5$$$. Output For each testcase print a single integerxa0— the score of the game if both players play optimally. Example Input 3 3 1 3 7 3 5 1 3 1 3 9 3 5 1 1 4 7 Note The paths for the testcases are shown on the following pictures. Alice's path is depicted in red and Bob's path is depicted in blue.
1,300
false
false
true
true
false
true
false
false
false
false
2,859
439C
Devu being a small kid, likes to play a lot, but he only likes to play with arrays. While playing he came up with an interesting question which he could not solve, can you please solve it for him? Given an array consisting of distinct integers. Is it possible to partition the whole array into _k_ disjoint non-empty parts such that _p_ of the parts have even sum (each of them must have even sum) and remaining _k_ - _p_ have odd sum? (note that parts need not to be continuous). If it is possible to partition the array, also give any possible way of valid partitioning. Input The first line will contain three space separated integers _n_, _k_, _p_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009105;xa00u2009≤u2009_p_u2009≤u2009_k_). The next line will contain _n_ space-separated distinct integers representing the content of array _a_: _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). Output In the first line print "YES" (without the quotes) if it is possible to partition the array in the required way. Otherwise print "NO" (without the quotes). If the required partition exists, print _k_ lines after the first line. The _i__th_ of them should contain the content of the _i__th_ part. Print the content of the part in the line in the following way: firstly print the number of elements of the part, then print all the elements of the part in arbitrary order. There must be exactly _p_ parts with even sum, each of the remaining _k_ - _p_ parts must have odd sum. As there can be multiple partitions, you are allowed to print any valid partition. Examples Output YES 1 9 1 5 1 10 1 6 1 2
1,700
false
false
true
false
false
true
false
false
false
false
8,082
46F
Today you are to solve the problem even the famous Hercule Poirot can't cope with! That's why this crime has not yet been solved and this story was never included in Agatha Christie's detective story books. You are not informed on what crime was committed, when and where the corpse was found and other details. We only know that the crime was committed in a house that has _n_ rooms and _m_ doors between the pairs of rooms. The house residents are very suspicious, that's why all the doors can be locked with keys and all the keys are different. According to the provided evidence on Thursday night all the doors in the house were locked, and it is known in what rooms were the residents, and what kind of keys had any one of them. The same is known for the Friday night, when all the doors were also locked. On Friday it was raining heavily, that's why nobody left the house and nobody entered it. During the day the house residents could open and close doors to the neighboring rooms using the keys at their disposal (every door can be opened and closed from each side); move freely from a room to a room if a corresponding door is open; give keys to one another, being in one room. "Little grey matter" of Hercule Poirot are not capable of coping with such amount of information. Find out if the positions of people and keys on the Thursday night could result in the positions on Friday night, otherwise somebody among the witnesses is surely lying. Input The first line contains three preset integers _n_, _m_ и _k_ (1u2009≤u2009_n_,u2009_m_,u2009_k_u2009≤u20091000) — the number of rooms, the number of doors and the number of house residents respectively. The next _m_ lines contain pairs of room numbers which join the doors. The rooms are numbered with integers from 1 to _n_. There cannot be more that one door between the pair of rooms. No door connects a room with itself. The next _k_ lines describe the residents' position on the first night. Every line contains a resident's name (a non-empty line consisting of no more than 10 Latin letters), then after a space follows the room number, then, after a space — the number of keys the resident has. Then follow written space-separated numbers of the doors that can be unlocked by these keys. The doors are numbered with integers from 1 to _m_ in the order in which they are described in the input data. All the residents have different names, uppercase and lowercase letters considered to be different. Every _m_ keys occurs exactly once in the description. Multiple people may be present in one room, some rooms may be empty. The next _k_ lines describe the position of the residents on the second night in the very same format. It is guaranteed that in the second night's description the residents' names remain the same and every _m_ keys occurs exactly once. Output Print "YES" (without quotes) if the second arrangement can result from the first one, otherwise, print "NO". Examples Input 2 1 2 1 2 Dmitry 1 1 1 Natalia 2 0 Natalia 1 1 1 Dmitry 2 0 Input 4 4 3 1 3 1 2 2 3 3 4 Artem 1 1 4 Dmitry 1 1 2 Edvard 4 2 1 3 Artem 2 0 Dmitry 1 0 Edvard 4 4 1 2 3 4
2,300
false
false
false
false
false
false
false
false
false
true
9,744
1840B
Once upon a time, Toma found himself in a binary cafe. It is a very popular and unusual place. The cafe offers visitors $$$k$$$ different delicious desserts. The desserts are numbered from $$$0$$$ to $$$k-1$$$. The cost of the $$$i$$$-th dessert is $$$2^i$$$ coins, because it is a binary cafe! Toma is willing to spend no more than $$$n$$$ coins on tasting desserts. At the same time, he is not interested in buying any dessert more than once, because one is enough to evaluate the taste. In how many different ways can he buy several desserts (possibly zero) for tasting? Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. Then follows $$$t$$$ lines, each of which describes one test case. Each test case is given on a single line and consists of two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 10^9$$$)xa0— the number of coins Toma is willing to spend and the number of desserts in the binary cafe. Output Output $$$t$$$ integers, the $$$i$$$-th of which should be equal to the answer for the $$$i$$$-th test casexa0— the number of ways to buy desserts for tasting. Example Input 5 1 2 2 1 2 2 10 2 179 100 Note Variants for 1st sample: {}, {1} Variants for 2nd sample: {}, {1} Variants for 3rd sample: {}, {1}, {2} Variants for 4th sample: {}, {1}, {2}, {1, 2}
1,100
true
false
false
false
false
false
true
false
false
false
1,252
1095C
Problem - 1095C - Codeforces =============== xa0 []( []( [Register]( [Home]( [Top]( [Catalog]( [Contests]( [Gym]( [Problemset]( [Groups]( [Rating]( [Edu]( [API]( [Calendar]( [Help]( [Rayan]( [Codeforces Round 529 (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 bitmasks greedy *1400 No tag edit access → Contest materials [Announcement]( "Codeforces Round #529 (Div. 3)") [Tutorial]( "Codeforces Round #529 (Div. 3) Editorial") [Problems]( [Submit]( [Status]( [Standings]( [Custom test]( The problem statement has recently been changed. [View the changes.]( Powers Of Two time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output A positive integer $$$x$$$ is called a power of two if it can be represented as $$$x = 2^y$$$, where $$$y$$$ is a non-negative integer. So, the powers of two are $$$1, 2, 4, 8, 16, dots$$$. You are given two positive integers $$$n$$$ and $$$k$$$. Your task is to represent $$$n$$$ as the sum of exactly $$$k$$$ powers of two. Input The only line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 10^9$$$, $$$1 le k le 2 cdot 10^5$$$). Output If it is impossible to represent $$$n$$$ as the sum of $$$k$$$ powers of two, print NO. Otherwise, print YES, and then print $$$k$$$ positive integers $$$b_1, b_2, dots, b_k$$$ such that each of $$$b_i$$$ is a power of two, and $$$sum limits_{i = 1}^{k} b_i = n$$$. If there are multiple answers, you may print any of them. Examples Input 9 4 Output YES 1 2 2 4 Input 8 1 Output YES 8 Input 5 1 Output NO Input 3 7 Output NO [Codeforces]( (c) Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 07:58:07 (h1). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name ---
1,400
false
true
false
false
false
false
false
false
false
false
5,235
1552F
An ant moves on the real line with constant speed of $$$1$$$ unit per second. It starts at $$$0$$$ and always moves to the right (so its position increases by $$$1$$$ each second). There are $$$n$$$ portals, the $$$i$$$-th of which is located at position $$$x_i$$$ and teleports to position $$$y_i < x_i$$$. Each portal can be either active or inactive. The initial state of the $$$i$$$-th portal is determined by $$$s_i$$$: if $$$s_i=0$$$ then the $$$i$$$-th portal is initially inactive, if $$$s_i=1$$$ then the $$$i$$$-th portal is initially active. When the ant travels through a portal (i.e., when its position coincides with the position of a portal): if the portal is inactive, it becomes active (in this case the path of the ant is not affected); if the portal is active, it becomes inactive and the ant is instantly teleported to the position $$$y_i$$$, where it keeps on moving as normal. How long (from the instant it starts moving) does it take for the ant to reach the position $$$x_n + 1$$$? It can be shown that this happens in a finite amount of time. Since the answer may be very large, compute it modulo $$$998,244,353$$$. Input The first line contains the integer $$$n$$$ ($$$1le nle 2cdot 10^5$$$) — the number of portals. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$ and $$$s_i$$$ ($$$1le y_i < x_ile 10^9$$$, $$$s_iin{0,1}$$$) — the position of the $$$i$$$-th portal, the position where the ant is teleported when it travels through the $$$i$$$-th portal (if it is active), and the initial state of the $$$i$$$-th portal. The positions of the portals are strictly increasing, that is $$$x_1<x_2<cdots<x_n$$$. It is guaranteed that the $$$2n$$$ integers $$$x_1, , x_2, , dots, , x_n, , y_1, , y_2, , dots, , y_n$$$ are all distinct. Output Output the amount of time elapsed, in seconds, from the instant the ant starts moving to the instant it reaches the position $$$x_n+1$$$. Since the answer may be very large, output it modulo $$$998,244,353$$$. Examples Input 4 3 2 0 6 5 1 7 4 0 8 1 1 Input 1 454971987 406874902 1 Input 5 243385510 42245605 0 644426565 574769163 0 708622105 208990040 0 786625660 616437691 0 899754846 382774619 0 Input 5 200000000 100000000 1 600000000 400000000 0 800000000 300000000 0 900000000 700000000 1 1000000000 500000000 0 Note Explanation of the first sample: The ant moves as follows (a curvy arrow denotes a teleporting, a straight arrow denotes normal movement with speed $$$1$$$ and the time spent during the movement is written above the arrow). $$$$$$ 0 stackrel{6}{longrightarrow} 6 leadsto 5 stackrel{3}{longrightarrow} 8 leadsto 1 stackrel{2}{longrightarrow} 3 leadsto 2 stackrel{4}{longrightarrow} 6 leadsto 5 stackrel{2}{longrightarrow} 7 leadsto 4 stackrel{2}{longrightarrow} 6 leadsto 5 stackrel{4}{longrightarrow} 9 $$$$$$ Notice that the total time is $$$6+3+2+4+2+2+4=23$$$. Explanation of the second sample: The ant moves as follows (a curvy arrow denotes a teleporting, a straight arrow denotes normal movement with speed $$$1$$$ and the time spent during the movement is written above the arrow). $$$$$$ 0 stackrel{454971987}{longrightarrow} 454971987 leadsto 406874902 stackrel{48097086}{longrightarrow} 454971988 $$$$$$ Notice that the total time is $$$454971987+48097086=503069073$$$. Explanation of the third sample: Since all portals are initially off, the ant will not be teleported and will go straight from $$$0$$$ to $$$x_n+1=899754846+1=899754847$$$.
2,200
false
false
false
true
true
false
false
false
false
false
2,879
1353E
You are given a garland consisting of $$$n$$$ lamps. States of the lamps are represented by the string $$$s$$$ of length $$$n$$$. The $$$i$$$-th character of the string $$$s_i$$$ equals '0' if the $$$i$$$-th lamp is turned off or '1' if the $$$i$$$-th lamp is turned on. You are also given a positive integer $$$k$$$. In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa). The garland is called $$$k$$$-periodic if the distance between each pair of adjacent turned on lamps is exactly $$$k$$$. Consider the case $$$k=3$$$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa. Your task is to find the minimum number of moves you need to make to obtain $$$k$$$-periodic garland from the given one. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 25~ 000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 10^6; 1 le k le n$$$) — the length of $$$s$$$ and the required period. The second line of the test case contains the string $$$s$$$ consisting of $$$n$$$ characters '0' and '1'. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$ ($$$sum n le 10^6$$$). Output For each test case, print the answer — the minimum number of moves you need to make to obtain $$$k$$$-periodic garland from the given one. Example Input 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0
1,900
false
true
false
true
false
false
false
false
false
false
3,941
1393C
Pinkie Pie has bought a bag of patty-cakes with different fillings! But it appeared that not all patty-cakes differ from one another with filling. In other words, the bag contains some patty-cakes with the same filling. Pinkie Pie eats the patty-cakes one-by-one. She likes having fun so she decided not to simply eat the patty-cakes but to try not to eat the patty-cakes with the same filling way too often. To achieve this she wants the minimum distance between the eaten with the same filling to be the largest possible. Herein Pinkie Pie called the distance between two patty-cakes the number of eaten patty-cakes strictly between them. Pinkie Pie can eat the patty-cakes in any order. She is impatient about eating all the patty-cakes up so she asks you to help her to count the greatest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating! Pinkie Pie is going to buy more bags of patty-cakes so she asks you to solve this problem for several bags! Input The first line contains a single integer $$$T$$$ ($$$1 le T le 100$$$): the number of bags for which you need to solve the problem. The first line of each bag description contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$): the number of patty-cakes in it. The second line of the bag description contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$): the information of patty-cakes' fillings: same fillings are defined as same integers, different fillings are defined as different integers. It is guaranteed that each bag contains at least two patty-cakes with the same filling. It is guaranteed that the sum of $$$n$$$ over all bags does not exceed $$$10^5$$$. Output For each bag print in separate line one single integer: the largest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating for that bag. Example Input 4 7 1 7 1 6 4 4 6 8 1 1 4 6 4 6 4 7 3 3 3 3 6 2 5 2 3 1 4 Note For the first bag Pinkie Pie can eat the patty-cakes in the following order (by fillings): $$$1$$$, $$$6$$$, $$$4$$$, $$$7$$$, $$$1$$$, $$$6$$$, $$$4$$$ (in this way, the minimum distance is equal to $$$3$$$). For the second bag Pinkie Pie can eat the patty-cakes in the following order (by fillings): $$$1$$$, $$$4$$$, $$$6$$$, $$$7$$$, $$$4$$$, $$$1$$$, $$$6$$$, $$$4$$$ (in this way, the minimum distance is equal to $$$2$$$).
1,700
true
true
false
false
false
true
false
false
false
false
3,705
2029A
You are given a positive integer $$$k$$$ and a set $$$S$$$ of all integers from $$$l$$$ to $$$r$$$ (inclusive). You can perform the following two-step operation any number of times (possibly zero): 1. First, choose a number $$$x$$$ from the set $$$S$$$, such that there are at least $$$k$$$ multiples of $$$x$$$ in $$$S$$$ (including $$$x$$$ itself); 2. Then, remove $$$x$$$ from $$$S$$$ (note that nothing else is removed). Find the maximum possible number of operations that can be performed. Input Each test contains multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0— the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$l$$$, $$$r$$$, and $$$k$$$ ($$$1le lle rleq 10^9$$$, $$$1leq kle r-l+1$$$)xa0— the minimum integer in $$$S$$$, the maximum integer in $$$S$$$, and the parameter $$$k$$$. Note In the first test case, initially, $$$S = {3,4,5,6,7,8,9}$$$. One possible optimal sequence of operations is: 1. Choose $$$x = 4$$$ for the first operation, since there are two multiples of $$$4$$$ in $$$S$$$: $$$4$$$ and $$$8$$$. $$$S$$$ becomes equal to $$${3,5,6,7,8,9}$$$; 2. Choose $$$x = 3$$$ for the second operation, since there are three multiples of $$$3$$$ in $$$S$$$: $$$3$$$, $$$6$$$, and $$$9$$$. $$$S$$$ becomes equal to $$${5,6,7,8,9}$$$. In the second test case, initially, $$$S={4,5,6,7,8,9}$$$. One possible optimal sequence of operations is: 1. Choose $$$x = 5$$$, $$$S$$$ becomes equal to $$${4,6,7,8,9}$$$; 2. Choose $$$x = 6$$$, $$$S$$$ becomes equal to $$${4,7,8,9}$$$; 3. Choose $$$x = 4$$$, $$$S$$$ becomes equal to $$${7,8,9}$$$; 4. Choose $$$x = 8$$$, $$$S$$$ becomes equal to $$${7,9}$$$; 5. Choose $$$x = 7$$$, $$$S$$$ becomes equal to $$${9}$$$; 6. Choose $$$x = 9$$$, $$$S$$$ becomes equal to $$${}$$$. In the third test case, initially, $$$S={7,8,9}$$$. For each $$$x$$$ in $$$S$$$, no multiple of $$$x$$$ other than $$$x$$$ itself can be found in $$$S$$$. Since $$$k = 2$$$, you can perform no operations. In the fourth test case, initially, $$$S={2,3,4,5,6,7,8,9,10}$$$. One possible optimal sequence of operations is: 1. Choose $$$x = 2$$$, $$$S$$$ becomes equal to $$${3,4,5,6,7,8,9,10}$$$; 2. Choose $$$x = 4$$$, $$$S$$$ becomes equal to $$${3,5,6,7,8,9,10}$$$; 3. Choose $$$x = 3$$$, $$$S$$$ becomes equal to $$${5,6,7,8,9,10}$$$; 4. Choose $$$x = 5$$$, $$$S$$$ becomes equal to $$${6,7,8,9,10}$$$.
800
true
true
false
false
false
false
false
false
false
false
97
21A
Jabber ID on the national Berland service «Babber» has a form <username>@<hostname>[/resource], where <username> — is a sequence of Latin letters (lowercase or uppercase), digits or underscores characters «_», the length of <username> is between 1 and 16, inclusive. <hostname> — is a sequence of word separated by periods (characters «.»), where each word should contain only characters allowed for <username>, the length of each word is between 1 and 16, inclusive. The length of <hostname> is between 1 and 32, inclusive. <resource> — is a sequence of Latin letters (lowercase or uppercase), digits or underscores characters «_», the length of <resource> is between 1 and 16, inclusive. The content of square brackets is optional — it can be present or can be absent. There are the samples of correct Jabber IDs: mike@codeforces.com, 007@en.codeforces.com/contest. Your task is to write program which checks if given string is a correct Jabber ID. Input The input contains of a single line. The line has the length between 1 and 100 characters, inclusive. Each characters has ASCII-code between 33 and 127, inclusive. Examples Input john.smith@codeforces.ru/contest.icpc/12
1,900
false
false
true
false
false
false
false
false
true
false
9,892
1138A
Arkady invited Anna for a dinner to a sushi restaurant. The restaurant is a bit unusual: it offers $$$n$$$ pieces of sushi aligned in a row, and a customer has to choose a continuous subsegment of these sushi to buy. The pieces of sushi are of two types: either with tuna or with eel. Let's denote the type of the $$$i$$$-th from the left sushi as $$$t_i$$$, where $$$t_i = 1$$$ means it is with tuna, and $$$t_i = 2$$$ means it is with eel. Arkady does not like tuna, Anna does not like eel. Arkady wants to choose such a continuous subsegment of sushi that it has equal number of sushi of each type and each half of the subsegment has only sushi of one type. For example, subsegment $$$[2, 2, 2, 1, 1, 1]$$$ is valid, but subsegment $$$[1, 2, 1, 2, 1, 2]$$$ is not, because both halves contain both types of sushi. Find the length of the longest continuous subsegment of sushi Arkady can buy. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 100,000$$$)xa0— the number of pieces of sushi. The second line contains $$$n$$$ integers $$$t_1$$$, $$$t_2$$$, ..., $$$t_n$$$ ($$$t_i = 1$$$, denoting a sushi with tuna or $$$t_i = 2$$$, denoting a sushi with eel), representing the types of sushi from left to right. It is guaranteed that there is at least one piece of sushi of each type. Note that it means that there is at least one valid continuous segment. Output Print a single integerxa0— the maximum length of a valid continuous segment. Note In the first example Arkady can choose the subsegment $$$[2, 2, 1, 1]$$$ or the subsegment $$$[1, 1, 2, 2]$$$ with length $$$4$$$. In the second example there is no way but to choose one of the subsegments $$$[2, 1]$$$ or $$$[1, 2]$$$ with length $$$2$$$. In the third example Arkady's best choice is the subsegment $$$[1, 1, 1, 2, 2, 2]$$$.
900
false
true
true
false
false
false
false
false
false
false
5,036
1467A
There are $$$n$$$ digital panels placed in a straight line. Each panel can show any digit from $$$0$$$ to $$$9$$$. Initially, all panels show $$$0$$$. Every second, the digit shown by each panel increases by $$$1$$$. In other words, at the end of every second, a panel that showed $$$9$$$ would now show $$$0$$$, a panel that showed $$$0$$$ would now show $$$1$$$, a panel that showed $$$1$$$ would now show $$$2$$$, and so on. When a panel is paused, the digit displayed on the panel does not change in the subsequent seconds. You must pause exactly one of these panels, at any second you wish. Then, the panels adjacent to it get paused one second later, the panels adjacent to those get paused $$$2$$$ seconds later, and so on. In other words, if you pause panel $$$x$$$, panel $$$y$$$ (for all valid $$$y$$$) would be paused exactly $$$x−y$$$ seconds later. For example, suppose there are $$$4$$$ panels, and the $$$3$$$-rd panel is paused when the digit $$$9$$$ is on it. The panel $$$1$$$ pauses $$$2$$$ seconds later, so it has the digit $$$1$$$; the panel $$$2$$$ pauses $$$1$$$ second later, so it has the digit $$$0$$$; the panel $$$4$$$ pauses $$$1$$$ second later, so it has the digit $$$0$$$. The resulting $$$4$$$-digit number is $$$1090$$$. Note that this example is not optimal for $$$n = 4$$$. Once all panels have been paused, you write the digits displayed on them from left to right, to form an $$$n$$$ digit number (it can consist of leading zeros). What is the largest possible number you can get? Initially, all panels show $$$0$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. Each test case consists of a single line containing a single integer $$$n$$$ ($$$1 le n le 2cdot10^5$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Note In the first test case, it is optimal to pause the first panel when the number $$$9$$$ is displayed on it. In the second test case, it is optimal to pause the second panel when the number $$$8$$$ is displayed on it.
900
true
true
false
false
false
true
false
false
false
false
3,357
1033F
Alice has a computer that operates on $$$w$$$-bit integers. The computer has $$$n$$$ registers for values. The current content of the registers is given as an array $$$a_1, a_2, ldots, a_n$$$. The computer uses so-called "number gates" to manipulate this data. Each "number gate" takes two registers as inputs and calculates a function of the two values stored in those registers. Note that you can use the same register as both inputs. Each "number gate" is assembled from bit gates. There are six types of bit gates: AND, OR, XOR, NOTxa0AND, NOTxa0OR, and NOTxa0XOR, denoted "A", "O", "X", "a", "o", "x", respectively. Each bit gate takes two bits as input. Its output given the input bits $$$b_1$$$, $$$b_2$$$ is given below: $$$begin{matrix} b_1 & b_2 & A & O & X & a & o & x 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 0 & 1 & 0 & 1 & 1 & 1 & 0 & 0 1 & 0 & 0 & 1 & 1 & 1 & 0 & 0 1 & 1 & 1 & 1 & 0 & 0 & 0 & 1 end{matrix}$$$ To build a "number gate", one takes $$$w$$$ bit gates and assembles them into an array. A "number gate" takes two $$$w$$$-bit integers $$$x_1$$$ and $$$x_2$$$ as input. The "number gate" splits the integers into $$$w$$$ bits and feeds the $$$i$$$-th bit of each input to the $$$i$$$-th bit gate. After that, it assembles the resulting bits again to form an output word. For instance, for $$$4$$$-bit computer we might have a "number gate" "AXoA" (AND, XOR, NOT OR, AND). For two inputs, $$$13 = 1101_2$$$ and $$$10 = 1010_2$$$, this returns $$$12 = 1100_2$$$, as $$$1$$$ and $$$1$$$ is $$$1$$$, $$$1$$$ xor $$$0$$$ is $$$1$$$, not ($$$0$$$ or $$$1$$$) is $$$0$$$, and finally $$$1$$$ and $$$0$$$ is $$$0$$$. You are given a description of $$$m$$$ "number gates". For each gate, your goal is to report the number of register pairs for which the "number gate" outputs the number $$$0$$$. In other words, find the number of ordered pairs $$$(i,j)$$$ where $$$1 leq i,j leq n$$$, such that $$$w_k(a_i, a_j) = 0$$$, where $$$w_k$$$ is the function computed by the $$$k$$$-th "number gate". Input The first line contains three integers: $$$w$$$, $$$n$$$, and $$$m~(1 leq w leq 12, 1 leq n leq 3cdot 10^4, 1 leq m leq 5cdot 10^4)$$$xa0— the word size, the number of variables, and the number of gates. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ $$$(0 leq a_i < 2^w)$$$xa0— the value of variables stored in the registers. Each of the next $$$m$$$ lines contains a string $$$g_j~(g_j = w)$$$ with a description of a single gate. Each character of $$$g_j$$$ is one of "A", "O", "X", "a", "o", "x". Note In the first test case, the inputs in binary are $$$1101$$$, $$$1010$$$, $$$0110$$$. The pairs that return $$$0$$$ are $$$(13, 6)$$$, $$$(6, 13)$$$, and $$$(6, 6)$$$. As it was already mentioned in the problem statement, $$$13 oplus 10 = 10 oplus 13 = 12$$$. The other pairs are $$$13 oplus 13 = 11$$$, $$$10 oplus 10 = 8$$$ and $$$10 oplus 6 = 6 oplus 10 = 4$$$.
2,800
true
false
false
false
false
false
false
false
false
false
5,530
645B
It is a balmy spring afternoon, and Farmer John's _n_ cows are ruminating about link-cut cacti in their stalls. The cows, labeled 1 through _n_, are arranged so that the _i_-th cow occupies the _i_-th stall from the left. However, Elsie, after realizing that she will forever live in the shadows beyond Bessie's limelight, has formed the Mischievous Mess Makers and is plotting to disrupt this beautiful pastoral rhythm. While Farmer John takes his _k_ minute long nap, Elsie and the Mess Makers plan to repeatedly choose two distinct stalls and swap the cows occupying those stalls, making no more than one swap each minute. Being the meticulous pranksters that they are, the Mischievous Mess Makers would like to know the maximum messiness attainable in the _k_ minutes that they have. We denote as _p__i_ the label of the cow in the _i_-th stall. The messiness of an arrangement of cows is defined as the number of pairs (_i_,u2009_j_) such that _i_u2009<u2009_j_ and _p__i_u2009>u2009_p__j_. Input The first line of the input contains two integers _n_ and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u2009100u2009000)xa0— the number of cows and the length of Farmer John's nap, respectively. Output Output a single integer, the maximum messiness that the Mischievous Mess Makers can achieve by performing no more than _k_ swaps. Note In the first sample, the Mischievous Mess Makers can swap the cows in the stalls 1 and 5 during the first minute, then the cows in stalls 2 and 4 during the second minute. This reverses the arrangement of cows, giving us a total messiness of 10. In the second sample, there is only one cow, so the maximum possible messiness is 0.
1,200
true
true
false
false
false
false
false
false
false
false
7,233
1420E
A lighthouse keeper Peter commands an army of $$$n$$$ battle lemmings. He ordered his army to stand in a line and numbered the lemmings from $$$1$$$ to $$$n$$$ from left to right. Some of the lemmings hold shields. Each lemming cannot hold more than one shield. The more protected Peter's army is, the better. To calculate the protection of the army, he finds the number of protected pairs of lemmings, that is such pairs that both lemmings in the pair don't hold a shield, but there is a lemming with a shield between them. Now it's time to prepare for defence and increase the protection of the army. To do this, Peter can give orders. He chooses a lemming with a shield and gives him one of the two orders: give the shield to the left neighbor if it exists and doesn't have a shield; give the shield to the right neighbor if it exists and doesn't have a shield. In one second Peter can give exactly one order. It's not clear how much time Peter has before the defence. So he decided to determine the maximal value of army protection for each $$$k$$$ from $$$0$$$ to $$$frac{n(n-1)}2$$$, if he gives no more that $$$k$$$ orders. Help Peter to calculate it! Input First line contains a single integer $$$n$$$ ($$$1 le n le 80$$$), the number of lemmings in Peter's army. Second line contains $$$n$$$ integers $$$a_i$$$ ($$$0 le a_i le 1$$$). If $$$a_i = 1$$$, then the $$$i$$$-th lemming has a shield, otherwise $$$a_i = 0$$$. Output Print $$$frac{n(n-1)}2 + 1$$$ numbers, the greatest possible protection after no more than $$$0, 1, dots, frac{n(n-1)}2$$$ orders. Examples Output 0 2 3 3 3 3 3 3 3 3 3 Input 12 0 0 0 0 1 1 1 1 0 1 1 0 Output 9 12 13 14 14 14 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 Note Consider the first example. The protection is initially equal to zero, because for each pair of lemmings without shields there is no lemmings with shield. In one second Peter can order the first lemming give his shield to the right neighbor. In this case, the protection is two, as there are two protected pairs of lemmings, $$$(1, 3)$$$ and $$$(1, 4)$$$. In two seconds Peter can act in the following way. First, he orders the fifth lemming to give a shield to the left neighbor. Then, he orders the first lemming to give a shield to the right neighbor. In this case Peter has three protected pairs of lemmingsxa0— $$$(1, 3)$$$, $$$(1, 5)$$$ and $$$(3, 5)$$$. You can make sure that it's impossible to give orders in such a way that the protection becomes greater than three.
2,500
false
true
false
true
false
false
false
false
false
false
3,575
1038A
You are given a string $$$s$$$ of length $$$n$$$, which consists only of the first $$$k$$$ letters of the Latin alphabet. All letters in string $$$s$$$ are uppercase. A subsequence of string $$$s$$$ is a string that can be derived from $$$s$$$ by deleting some of its symbols without changing the order of the remaining symbols. For example, "ADE" and "BD" are subsequences of "ABCDE", but "DEA" is not. A subsequence of $$$s$$$ called good if the number of occurences of each of the first $$$k$$$ letters of the alphabet is the same. Find the length of the longest good subsequence of $$$s$$$. Input The first line of the input contains integers $$$n$$$ ($$$1le n le 10^5$$$) and $$$k$$$ ($$$1 le k le 26$$$). The second line of the input contains the string $$$s$$$ of length $$$n$$$. String $$$s$$$ only contains uppercase letters from 'A' to the $$$k$$$-th letter of Latin alphabet. Output Print the only integerxa0— the length of the longest good subsequence of string $$$s$$$. Note In the first example, "ACBCAB" ("ACAABCCAB") is one of the subsequences that has the same frequency of 'A', 'B' and 'C'. Subsequence "CAB" also has the same frequency of these letters, but doesn't have the maximum possible length. In the second example, none of the subsequences can have 'D', hence the answer is $$$0$$$.
800
false
false
true
false
false
false
false
false
true
false
5,508
156C
Sherlock Holmes found a mysterious correspondence of two VIPs and made up his mind to read it. But there is a problem! The correspondence turned out to be encrypted. The detective tried really hard to decipher the correspondence, but he couldn't understand anything. At last, after some thought, he thought of something. Let's say there is a word _s_, consisting of _s_ lowercase Latin letters. Then for one operation you can choose a certain position _p_ (1u2009≤u2009_p_u2009<u2009_s_) and perform one of the following actions: either replace letter _s__p_ with the one that alphabetically follows it and replace letter _s__p_u2009+u20091 with the one that alphabetically precedes it; or replace letter _s__p_ with the one that alphabetically precedes it and replace letter _s__p_u2009+u20091 with the one that alphabetically follows it. Let us note that letter "z" doesn't have a defined following letter and letter "a" doesn't have a defined preceding letter. That's why the corresponding changes are not acceptable. If the operation requires performing at least one unacceptable change, then such operation cannot be performed. Two words coincide in their meaning iff one of them can be transformed into the other one as a result of zero or more operations. Sherlock Holmes needs to learn to quickly determine the following for each word: how many words can exist that coincide in their meaning with the given word, but differs from the given word in at least one character? Count this number for him modulo 1000000007 (109u2009+u20097). Input The input data contains several tests. The first line contains the only integer _t_ (1u2009≤u2009_t_u2009≤u2009104) — the number of tests. Next _t_ lines contain the words, one per line. Each word consists of lowercase Latin letters and has length from 1 to 100, inclusive. Lengths of words can differ. Output For each word you should print the number of different other words that coincide with it in their meaning — not from the words listed in the input data, but from all possible words. As the sought number can be very large, print its value modulo 1000000007 (109u2009+u20097). Note Some explanations about the operation: Note that for each letter, we can clearly define the letter that follows it. Letter "b" alphabetically follows letter "a", letter "c" follows letter "b", ..., "z" follows letter "y". Preceding letters are defined in the similar manner: letter "y" precedes letter "z", ..., "a" precedes letter "b". Note that the operation never changes a word's length. In the first sample you can obtain the only other word "ba". In the second sample you cannot obtain any other word, so the correct answer is 0. Consider the third sample. One operation can transform word "klmbfxzb" into word "klmcexzb": we should choose _p_u2009=u20094, and replace the fourth letter with the following one ("b" u2009→u2009 "c"), and the fifth one — with the preceding one ("f" u2009→u2009 "e"). Also, we can obtain many other words from this one. An operation can transform word "ya" only into one other word "xb". Word "ya" coincides in its meaning with words "xb", "wc", "vd", ..., "ay" (overall there are 24 other words). The word "klmbfxzb has many more variants — there are 3320092814 other words that coincide with in the meaning. So the answer for the first word equals 24 and for the second one equals 320092793 — the number 3320092814 modulo 109u2009+u20097
2,000
false
false
false
true
false
false
true
false
false
false
9,250
578A
Problem - 578A - Codeforces =============== xa0 []( []( [Register]( [Home]( [Top]( [Catalog]( [Contests]( [Gym]( [Problemset]( [Groups]( [Rating]( [Edu]( [API]( [Calendar]( [Help]( [Rayan]( [Codeforces Round 320 (Div. 1) [Bayan Thanks-Round]]( --- 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 geometry math *1700 No tag edit access → Contest materials [Announcement (en)]( "Codeforces Round #320 [Bayan Thanks-Round]") [Tutorial (en)]( "Codeforces Round #320 [Bayan Thanks-Round] Editorial") [Problems]( [Submit]( [Status]( [Standings]( [Custom test]( The problem statement has recently been changed. [View the changes.]( A Problem about Polyline time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output There is a polyline going through points (0,u20090)u2009–u2009(_x_,u2009_x_)u2009–u2009(2_x_,u20090)u2009–u2009(3_x_,u2009_x_)u2009–u2009(4_x_,u20090)u2009–u2009...u2009-u2009(2_kx_,u20090)u2009–u2009(2_kx_u2009+u2009_x_,u2009_x_)u2009–u2009.... We know that the polyline passes through the point (_a_,u2009_b_). Find minimum positive value _x_ such that it is true or determine that there is no such _x_. Input Only one line containing two positive integers _a_ and _b_ (1u2009≤u2009_a_,u2009_b_u2009≤u2009109). Output Output the only line containing the answer. Your answer will be considered correct if its relative or absolute error doesn't exceed 10u2009-u20099. If there is no such _x_ then output u2009-u20091 as the answer. Examples Input 3 1 Output 1.000000000000 Input 1 3 Output -1 Input 4 1 Output 1.250000000000 Note You can see following graphs for sample 1 and sample 3. [Codeforces]( (c) Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 10:34:54 (f1). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name ---
1,700
true
false
false
false
false
false
false
false
false
false
7,536
1379E
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from $$$1$$$ to $$$n$$$, and $$$s_i$$$ denotes the child of the person $$$i$$$ (and $$$s_i = 0$$$ for exactly one person who does not have any children). We say that $$$a$$$ is an ancestor of $$$b$$$ if either $$$a = b$$$, or $$$a$$$ has a child, who is an ancestor of $$$b$$$. That is $$$a$$$ is an ancestor for $$$a$$$, $$$s_a$$$, $$$s_{s_a}$$$, etc. We say that person $$$i$$$ is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got $$$k$$$ people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with $$$n$$$ people that have $$$k$$$ imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 100,000$$$, $$$0 leq k leq n$$$), the total number of people and the number of imbalanced people. Output If there are no constructions with $$$n$$$ people and $$$k$$$ imbalanced people, output NO. Otherwise output YES on the first line, and then $$$n$$$ integers $$$s_1, s_2, ldots, s_n$$$ ($$$0 leq s_i leq n$$$), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2,800
true
false
false
true
false
true
false
true
false
false
3,774
1523H
William really wants to get a pet. Since his childhood he dreamt about getting a pet grasshopper. William is being very responsible about choosing his pet, so he wants to set up a trial for the grasshopper! The trial takes place on an array $$$a$$$ of length $$$n$$$, which defines lengths of hops for each of $$$n$$$ cells. A grasshopper can hop around the sells according to the following rule: from a cell with index $$$i$$$ it can jump to any cell with indices from $$$i$$$ to $$$i+a_i$$$ inclusive. Let's call the $$$k$$$-grasshopper value of some array the smallest number of hops it would take a grasshopper to hop from the first cell to the last, but before starting you can select no more than $$$k$$$ cells and remove them from the array. When a cell is removed all other cells are renumbered but the values of $$$a_i$$$ for each cell remains the same. During this the first and the last cells may not be removed. It is required to process $$$q$$$ queries of the following format: you are given three numbers $$$l$$$, $$$r$$$, $$$k$$$. You are required to find the $$$k$$$-grasshopper value for an array, which is a subarray of the array $$$a$$$ with elements from $$$l$$$ to $$$r$$$ inclusive. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 20000$$$), the length of the array and the number of queries respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$) xa0– the elements of the array. The following $$$q$$$ lines contain queries: each line contains three integers $$$l$$$, $$$r$$$ and $$$k$$$ ($$$1 le l le r le n$$$, $$$0 le k le min(30, r-l)$$$), which are the edges of the subarray and the number of the grasshopper value respectively. Output For each query print a single number in a new line xa0— the response to a query. Example Input 9 5 1 1 2 1 3 1 2 1 1 1 1 0 2 5 1 5 9 1 2 8 2 1 9 4 Note For the second query the process occurs like this: For the third query the process occurs like this:
3,500
false
false
false
true
true
false
false
false
false
false
3,043
100G
The famous singer, Aryo, is going to publish a new album of his great work! Unfortunately these days, there are many albums, Aryo wants to choose a new name for his album, a name that has not been used or at least has not been used recently. He has a list of all used album names together with the year the albums were published. He also has a list of suitable names for his album. If he finds a suitable name which has not been used before, he'll use it. Otherwise he will use the name which was used as long ago as possible. If two such names are found (that haven't been used or were used at the same year), he uses the name that is alphabetically latest. Help him name his album. Input The first line contains a single integer _n_ (0u2009≤u2009_n_u2009≤u2009105), the number of used names. The following _n_ lines each contain a string (the album name) and an integer (the year album was published). Album names are made of lowercase Latin letters and contain at most 14 letters. The year is in range [1900,u20092011]. The following line contains a single integer _m_ (1u2009≤u2009_m_u2009≤u2009104), the number of suitable album names. The following _m_ lines each contain a string — a suitable name. It contains at most 14 lowercase Latin letters. All album names and suitable names are non-empty. Output Write a single string. The name of the new album. Examples Input 3 eyesonme 2008 anewdayhascome 2002 oneheart 2003 2 oneheart bienbien Input 2 nasimevasl 2003 basetareha 2006 2 nasimevasl basetareha
1,800
false
false
true
false
true
false
false
false
false
false
9,483
1614E
Divan's new cottage is finally complete! However, after a thorough inspection, it turned out that the workers had installed the insulation incorrectly, and now the temperature in the house directly depends on the temperature outside. More precisely, if the temperature in the house is $$$P$$$ in the morning, and the street temperature is $$$T$$$, then by the next morning the temperature in the house changes according to the following rule: $$$P_{new} = P + 1$$$, if $$$P < T$$$, $$$P_{new} = P - 1$$$, if $$$P > T$$$, $$$P_{new} = P$$$, if $$$P = T$$$. Here $$$P_{new}$$$ is the temperature in the house next morning. Divan is a very busy businessman, so sometimes he is not at home for long periods and does not know what the temperature is there now, so he hired you to find it. You will work for $$$n$$$ days. In the beginning of the $$$i$$$-th day, the temperature outside $$$T_i$$$ is first given to you. After that, on the $$$i$$$-th day, you will receive $$$k_i$$$ queries. Each query asks the following: "if the temperature in the house was $$$x_i$$$ at the morning of the first day, what would be the temperature in the house next morning (after day $$$i$$$)?" Please answer all the businessman's queries. Input The first line of the input contains the number $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$) — the number of days. The following is a description of $$$n$$$ days in the following format. The first line of the description contains an integer $$$T_i$$$ ($$$0 leq T_i leq 10^9$$$) — the temperature on that day. The second line contains a non-negative integer $$$k_i$$$ ($$$0 le k_i le 2 cdot 10^5$$$) — the number of queries that day. The third line contains $$$k$$$ integers $$$x'_i$$$ ($$$0 leq x'_{i} leq 10^9$$$) — the encrypted version of Divan's queries. Let $$$lastans = 0$$$ initially. Divan's actual queries are given by $$$x_i = (x'_i + lastans) bmod (10^9 + 1)$$$, where $$$a bmod b$$$ is the reminder when $$$a$$$ is divided by $$$b$$$. After answering the query, set $$$lastans$$$ to the answer. It is guaranteed that the total number of queries (the sum of all $$$k_i$$$) does not exceed $$$2 cdot 10^5$$$. Output For each query, output a single integer — the temperature in the house after day $$$i$$$. Examples Input 3 50 3 1 2 3 50 3 4 5 6 0 3 7 8 9 Output 2 5 9 15 22 30 38 47 53 Input 4 728 3 859 1045 182 104 1 689 346 6 634 356 912 214 1 1 755 3 241 765 473 Output 858 1902 2083 2770 3401 3754 4663 4874 4872 4870 5107 5868 6337 Input 2 500000000 3 1000000000 999999999 888888888 250000000 5 777777777 666666666 555555555 444444444 333333333 Output 999999999 999999996 888888882 666666656 333333321 888888874 333333317 666666648 Note Let's look at the first four queries from the example input. The temperature is $$$50$$$ on the first day, $$$50$$$ on the second day, and $$$0$$$ on the third day. Note that $$$lastans = 0$$$ initially. The initial temperature of the first query of the first day is $$$(1 , + , lastans) bmod (10^9 + 1) = 1$$$. After the first day, the temperature rises by $$$1$$$, because $$$1 < 50$$$. So the answer to the query is $$$2$$$. Then, we set $$$lastans = 2$$$. The initial temperature of the second query of the first day is $$$(2 , + , lastans) bmod (10^9 + 1) = 4$$$. After the first day, the temperature rises by $$$1$$$, because $$$4 < 50$$$. So the answer to the query is $$$5$$$. Then, we set $$$lastans = 5$$$. The initial temperature of the third query of the first day is $$$(3 , + , lastans) bmod (10^9 + 1) = 8$$$. After the first day, the temperature rises by $$$1$$$. So the answer to the query is $$$9$$$. Then, we set $$$lastans = 9$$$. The initial temperature of the first query of the second day is $$$(4 , + , lastans) bmod (10^9 + 1) = 13$$$. After the first day, the temperature rises by $$$1$$$. After the second day, the temperature rises by $$$1$$$. So the answer to the query is $$$15$$$. Then, we set $$$lastans = 15$$$.
2,600
false
false
false
false
true
false
false
false
false
false
2,565
1380F
Let $$$a$$$ and $$$b$$$ be some non-negative integers. Let's define strange addition of $$$a$$$ and $$$b$$$ as following: 1. write down the numbers one under another and align them by their least significant digit; 2. add them up digit by digit and concatenate the respective sums together. Assume that both numbers have an infinite number of leading zeros. For example, let's take a look at a strange addition of numbers $$$3248$$$ and $$$908$$$: You are given a string $$$c$$$, consisting of $$$n$$$ digits from $$$0$$$ to $$$9$$$. You are also given $$$m$$$ updates of form: $$$x~d$$$xa0— replace the digit at the $$$x$$$-th position of $$$c$$$ with a digit $$$d$$$. Note that string $$$c$$$ might have leading zeros at any point of time. After each update print the number of pairs $$$(a, b)$$$ such that both $$$a$$$ and $$$b$$$ are non-negative integers and the result of a strange addition of $$$a$$$ and $$$b$$$ is equal to $$$c$$$. Note that the numbers of pairs can be quite large, so print them modulo $$$998244353$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 5 cdot 10^5$$$)xa0— the length of the number $$$c$$$ and the number of updates. The second line contains a string $$$c$$$, consisting of exactly $$$n$$$ digits from $$$0$$$ to $$$9$$$. Each of the next $$$m$$$ lines contains two integers $$$x$$$ and $$$d$$$ ($$$1 le x le n$$$, $$$0 le d le 9$$$)xa0— the descriptions of updates. Output Print $$$m$$$ integersxa0— the $$$i$$$-th value should be equal to the number of pairs $$$(a, b)$$$ such that both $$$a$$$ and $$$b$$$ are non-negative integers and the result of a strange addition of $$$a$$$ and $$$b$$$ is equal to $$$c$$$ after $$$i$$$ updates are applied. Note that the numbers of pairs can be quite large, so print them modulo $$$998244353$$$. Note After the first update $$$c$$$ is equal to $$$14$$$. The pairs that sum up to $$$14$$$ are: $$$(0, 14)$$$, $$$(1, 13)$$$, $$$(2, 12)$$$, $$$(3, 11)$$$, $$$(4, 10)$$$, $$$(5, 9)$$$, $$$(6, 8)$$$, $$$(7, 7)$$$, $$$(8, 6)$$$, $$$(9, 5)$$$, $$$(10, 4)$$$, $$$(11, 3)$$$, $$$(12, 2)$$$, $$$(13, 1)$$$, $$$(14, 0)$$$. After the second update $$$c$$$ is equal to $$$11$$$. After the third update $$$c$$$ is equal to $$$01$$$.
2,600
false
false
false
true
true
false
false
false
false
false
3,766
765F
Artsem is on vacation and wants to buy souvenirs for his two teammates. There are _n_ souvenir shops along the street. In _i_-th shop Artsem can buy one souvenir for _a__i_ dollars, and he cannot buy more than one souvenir in one shop. He doesn't want to introduce envy in his team, so he wants to buy two souvenirs with least possible difference in price. Artsem has visited the shopping street _m_ times. For some strange reason on the _i_-th day only shops with numbers from _l__i_ to _r__i_ were operating (weird? yes it is, but have you ever tried to come up with a reasonable legend for a range query problem?). For each visit, Artsem wants to know the minimum possible difference in prices of two different souvenirs he can buy in the opened shops. In other words, for each Artsem's visit you should find the minimum possible value of _a__s_u2009-u2009_a__t_ where _l__i_u2009≤u2009_s_,u2009_t_u2009≤u2009_r__i_, _s_u2009≠u2009_t_. Input The first line contains an integer _n_ (2u2009≤u2009_n_u2009≤u2009105). The second line contains _n_ space-separated integers _a_1, ..., _a__n_ (0u2009≤u2009_a__i_u2009≤u2009109). The third line contains the number of queries _m_ (1u2009≤u2009_m_u2009≤u20093·105). Next _m_ lines describe the queries. _i_-th of these lines contains two space-separated integers _l__i_ and _r__i_ denoting the range of shops working on _i_-th day (1u2009≤u2009_l__i_u2009<u2009_r__i_u2009≤u2009_n_). Output Print the answer to each query in a separate line. Example Input 8 3 1 4 1 5 9 2 6 4 1 8 1 3 4 8 5 7
3,100
false
false
false
false
true
false
false
false
false
false
6,725
1904D2
This is the hard version of the problem. The only differences between the two versions of this problem are the constraints on $$$n$$$ and the time limit. You can make hacks only if all versions of the problem are solved. You are given two arrays $$$a$$$ and $$$b$$$ of length $$$n$$$. You can perform the following operation some (possibly zero) times: 1. choose $$$l$$$ and $$$r$$$ such that $$$1 leq l leq r leq n$$$. 2. let $$$x=max(a_l,a_{l+1},ldots,a_r)$$$. 3. for all $$$l leq i leq r$$$, set $$$a_i := x$$$. Determine if you can make array $$$a$$$ equal to array $$$b$$$. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the length of the arrays. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the elements of array $$$a$$$. The third line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le n$$$)xa0— the elements of array $$$b$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output "YES" (without quotes) if you can make $$$a$$$ into $$$b$$$ using any number of operations, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response). Example Input 5 5 1 2 3 2 4 1 3 3 2 4 5 3 4 2 2 4 3 4 3 4 4 5 3 2 1 1 1 3 3 3 2 2 2 1 1 1 2 3 1 1 2 2 1 2 Note In the first test case, we can achieve array $$$b$$$ by applying a single operation: $$$(l,r)=(2,3)$$$. In the second test case, it can be shown we cannot achieve array $$$b$$$ in any amount of operations. In the third test case, we can achieve array $$$b$$$ by applying two operations: $$$(l,r)=(2,5)$$$. followed by $$$(l,r)=(1,3)$$$. In the fourth and fifth test cases, it can be shown we cannot achieve array $$$b$$$ in any amount of operations.
1,800
false
true
true
false
true
true
false
false
false
false
886
1080F
It is a very important day for Katya. She has a test in a programming class. As always, she was given an interesting problem that she solved very fast. Can you solve that problem? You are given $$$n$$$ ordered segments sets. Each segment can be represented as a pair of two integers $$$[l, r]$$$ where $$$lleq r$$$. Each set can contain an arbitrary number of segments (even $$$0$$$). It is possible that some segments are equal. You are also given $$$m$$$ queries, each of them can be represented as four numbers: $$$a, b, x, y$$$. For each segment, find out whether it is true that each set $$$p$$$ ($$$aleq pleq b$$$) contains at least one segment $$$[l, r]$$$ that lies entirely on the segment $$$[x, y]$$$, that is $$$xleq lleq rleq y$$$. Find out the answer to each query. Note that you need to solve this problem online. That is, you will get a new query only after you print the answer for the previous query. Input The first line contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ $$$(1leq n,mleq 10^5, 1leq kleq 3cdot10^5)$$$xa0— the number of sets, queries, and segments respectively. Each of the next $$$k$$$ lines contains three integers $$$l$$$, $$$r$$$, and $$$p$$$ $$$(1leq lleq rleq 10^9, 1leq pleq n)$$$xa0— the limits of the segment and the index of a set, to which this segment belongs. Each of the next $$$m$$$ lines contains four integers $$$a, b, x, y$$$ $$$(1leq aleq bleq n, 1leq xleq yleq 10^9)$$$xa0— the description of the query. Output For each query, print "yes" or "no" in a new line. Interaction After printing a query, do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Example Input 5 5 9 3 6 3 1 3 1 2 4 2 1 2 3 4 6 5 2 5 3 7 9 4 2 3 1 4 10 4 1 2 2 3 1 2 2 4 1 3 1 5 2 3 3 6 2 4 2 9 Note For the first query, the answer is negative since the second set does not contain a segment that lies on the segment $$$[2, 3]$$$. In the second query, the first set contains $$$[2, 3]$$$, and the second set contains $$$[2, 4]$$$. In the third query, the first set contains $$$[2, 3]$$$, the second set contains $$$[2, 4]$$$, and the third set contains $$$[2, 5]$$$. In the fourth query, the second set does not contain a segment that lies on the segment $$$[3, 6]$$$. In the fifth query, the second set contains $$$[2, 4]$$$, the third set contains $$$[2, 5]$$$, and the fourth contains $$$[7, 9]$$$.
2,400
false
false
false
false
true
false
false
false
false
false
5,324
1934B
You have $$$5$$$ different types of coins, each with a value equal to one of the first $$$5$$$ triangular numbers: $$$1$$$, $$$3$$$, $$$6$$$, $$$10$$$, and $$$15$$$. These coin types are available in abundance. Your goal is to find the minimum number of these coins required such that their total value sums up to exactly $$$n$$$. We can show that the answer always exists. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 10^9$$$)xa0— the target value. Output For each test case, output a single numberxa0— the minimum number of coins required. Example Input 14 1 2 3 5 7 11 12 14 16 17 18 20 98 402931328 Output 1 2 1 3 2 2 2 3 2 3 2 2 8 26862090 Note In the first test case, for $$$n = 1$$$, the answer is $$$1$$$ since only one $$$1$$$ value coin is sufficient. $$$1 = 1 cdot 1$$$. In the fourth test case, for $$$n = 5$$$, the answer is $$$3$$$, which can be achieved using two $$$1$$$ value coins and one $$$3$$$ value coin. $$$5 = 2 cdot 1 + 1 cdot 3$$$. In the seventh test case, for $$$n = 12$$$, the answer is $$$2$$$, which can be achieved using two $$$6$$$ value coins. In the ninth test case, for $$$n = 16$$$, the answer is $$$2$$$, which can be achieved using one $$$1$$$ value coin and one $$$15$$$ value coin or using one $$$10$$$ value coin and one $$$6$$$ value coin. $$$16 = 1 cdot 1 + 1 cdot 15 = 1 cdot 6 + 1 cdot 10$$$.
1,200
true
true
false
true
false
false
false
false
false
false
679
1335E1
The only difference between easy and hard versions is constraints. You are given a sequence $$$a$$$ consisting of $$$n$$$ positive integers. Let's define a three blocks palindrome as the sequence, consisting of at most two distinct elements (let these elements are $$$a$$$ and $$$b$$$, $$$a$$$ can be equal $$$b$$$) and is as follows: $$$[underbrace{a, a, dots, a}_{x}, underbrace{b, b, dots, b}_{y}, underbrace{a, a, dots, a}_{x}]$$$. There $$$x, y$$$ are integers greater than or equal to $$$0$$$. For example, sequences $$$[]$$$, $$$[2]$$$, $$$[1, 1]$$$, $$$[1, 2, 1]$$$, $$$[1, 2, 2, 1]$$$ and $$$[1, 1, 2, 1, 1]$$$ are three block palindromes but $$$[1, 2, 3, 2, 1]$$$, $$$[1, 2, 1, 2, 1]$$$ and $$$[1, 2]$$$ are not. Your task is to choose the maximum by length subsequence of $$$a$$$ that is a three blocks palindrome. You have to answer $$$t$$$ independent test cases. Recall that the sequence $$$t$$$ is a a subsequence of the sequence $$$s$$$ if $$$t$$$ can be derived from $$$s$$$ by removing zero or more elements without changing the order of the remaining elements. For example, if $$$s=[1, 2, 1, 3, 1, 2, 1]$$$, then possible subsequences are: $$$[1, 1, 1, 1]$$$, $$$[3]$$$ and $$$[1, 2, 1, 3, 1, 2, 1]$$$, but not $$$[3, 2, 3]$$$ and $$$[1, 1, 1, 1, 2]$$$. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 2000$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 26$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. Note that the maximum value of $$$a_i$$$ can be up to $$$26$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$ ($$$sum n le 2000$$$).
1,700
false
false
false
true
true
false
false
false
false
false
4,026
1575L
Problem - 1575L - Codeforces =============== xa0 []( []( [Register]( [Home]( [Top]( [Catalog]( [Contests]( [Gym]( [Problemset]( [Groups]( [Rating]( [Edu]( [API]( [Calendar]( [Help]( [Rayan]( [COMPFEST 13 - Finals Online Mirror (Unrated, ICPC Rules, Teams Preferred)]( --- 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 dp sortings *2100 No tag edit access → Contest materials [Announcement (en)]( "COMPFEST 13 Finals Mirror (Unrated, ICPC Rules, Teams Preferred)") [COMPFEST 13 — Editorial (en)]( "COMPFEST 13 — Editorial") [Problems]( [Submit]( [Status]( [Standings]( [Custom test]( The problem statement has recently been changed. [View the changes.]( Longest Array Deconstruction time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output Mr. Chanek gives you a sequence $$$a$$$ indexed from $$$1$$$ to $$$n$$$. Define $$$f(a)$$$ as the number of indices where $$$a_i = i$$$. You can pick an element from the current sequence and remove it, then concatenate the remaining elements together. For example, if you remove the $$$3$$$-rd element from the sequence $$$[4, 2, 3, 1]$$$, the resulting sequence will be $$$[4, 2, 1]$$$. You want to remove some elements from $$$a$$$ in order to maximize $$$f(a)$$$, using zero or more operations. Find the largest possible $$$f(a)$$$. Input The first line contains one integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$) — the initial length of the sequence. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 2 cdot 10^5$$$) — the initial sequence $$$a$$$. Output Output an integer denoting the largest $$$f(a)$$$ that can be obtained by doing zero or more operations. Examples Input 7 2 1 4 2 5 3 7 Output 3 Input 4 4 2 3 1 Output 2 Note In the first example, $$$f(A) = 3$$$ by doing the following operations. $$$[2,1, extbf{4},2,5,3,7] ightarrow [ extbf{2},1,2,5,3,7] ightarrow [1,2,5,3, extbf{7}] ightarrow [1,2, extbf{5},3] ightarrow [1,2,3]$$$ In the second example, $$$f(A) = 2$$$ and no additional operation is needed. [Codeforces]( (c) Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 03:13:57 (f1). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name ---
2,100
false
false
false
true
true
false
false
false
false
false
2,757
954H
Problem - 954H - Codeforces =============== xa0 []( []( [Register]( [Home]( [Top]( [Catalog]( [Contests]( [Gym]( [Problemset]( [Groups]( [Rating]( [Edu]( [API]( [Calendar]( [Help]( [Rayan]( [Educational Codeforces Round 40 (Rated for Div. 2)]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp *2500 No tag edit access → Contest materials [Announcement]( "Educational Codeforces Round 40 [Rated for Div. 2]") [Tutorial]( "58519") [Problems]( [Submit]( [Status]( [Standings]( [Custom test]( The problem statement has recently been changed. [View the changes.]( Path Counting time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output You are given a rooted tree. Let's denote _d_(_x_) as depth of node _x_: depth of the root is 1, depth of any other node _x_ is _d_(_y_)u2009+u20091, where _y_ is a parent of _x_. The tree has the following property: every node _x_ with _d_(_x_)u2009=u2009_i_ has exactly _a__i_ children. Maximum possible depth of a node is _n_, and _a__n_u2009=u20090. We define _f__k_ as the number of unordered pairs of vertices in the tree such that the number of edges on the simple path between them is equal to _k_. Calculate _f__k_ modulo 109u2009+u20097 for every 1u2009≤u2009_k_u2009≤u20092_n_u2009-u20092. Input The first line of input contains an integer _n_ (2u2009u2009≤u2009u2009_n_u2009u2009≤u2009u20095u2009000) — the maximum depth of a node. The second line of input contains _n_u2009-u20091 integers _a_1,u2009u2009_a_2,u2009u2009...,u2009u2009_a__n_u2009-u20091 (2u2009≤u2009u2009_a__i_u2009u2009≤u2009109), where _a__i_ is the number of children of every node _x_ such that _d_(_x_)u2009=u2009_i_. Since _a__n_u2009=u20090, it is not given in the input. Output Print 2_n_u2009-u20092 numbers. The _k_-th of these numbers must be equal to _f__k_ modulo 109u2009+u20097. Examples Input 4 2 2 2 Output 14 19 20 20 16 16 Input 3 2 3 Output 8 13 6 9 Note This the tree from the first sample: [Codeforces]( (c) Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 08:59:17 (l2). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name ---
2,500
false
false
false
true
false
false
true
false
false
false
5,902
557D
After Vitaly was expelled from the university, he became interested in the graph theory. Vitaly especially liked the cycles of an odd length in which each vertex occurs at most once. Vitaly was wondering how to solve the following problem. You are given an undirected graph consisting of _n_ vertices and _m_ edges, not necessarily connected, without parallel edges and loops. You need to find _t_ — the minimum number of edges that must be added to the given graph in order to form a simple cycle of an odd length, consisting of more than one vertex. Moreover, he must find _w_ — the number of ways to add _t_ edges in order to form a cycle of an odd length (consisting of more than one vertex). It is prohibited to add loops or parallel edges. Two ways to add edges to the graph are considered equal if they have the same sets of added edges. Since Vitaly does not study at the university, he asked you to help him with this task. Input The first line of the input contains two integers _n_ and _m_ (xa0—xa0the number of vertices in the graph and the number of edges in the graph. Next _m_ lines contain the descriptions of the edges of the graph, one edge per line. Each edge is given by a pair of integers _a__i_, _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_)xa0—xa0the vertices that are connected by the _i_-th edge. All numbers in the lines are separated by a single space. It is guaranteed that the given graph doesn't contain any loops and parallel edges. The graph isn't necessarily connected. Output Print in the first line of the output two space-separated integers _t_ and _w_xa0—xa0the minimum number of edges that should be added to the graph to form a simple cycle of an odd length consisting of more than one vertex where each vertex occurs at most once, and the number of ways to do this. Note The simple cycle is a cycle that doesn't contain any vertex twice.
2,000
true
false
false
false
false
false
true
false
false
true
7,605
1861A
A prime number is a positive integer that has exactly two different positive divisors: $$$1$$$ and the integer itself. For example, $$$2$$$, $$$3$$$, $$$13$$$ and $$$101$$$ are prime numbers; $$$1$$$, $$$4$$$, $$$6$$$ and $$$42$$$ are not. You are given a sequence of digits from $$$1$$$ to $$$9$$$, in which every digit from $$$1$$$ to $$$9$$$ appears exactly once. You are allowed to do the following operation several (maybe zero) times: choose any digit from the sequence and delete it. However, you cannot perform this operation if the sequence consists of only two digits. Your goal is to obtain a sequence which represents a prime number. Note that you cannot reorder the digits in the sequence. Print the resulting sequence, or report that it is impossible to perform the operations so that the resulting sequence is a prime number. Input The first line contains one integer $$$t$$$ ($$$1 le t le 5000$$$) — the number of test cases. Each test case consists of one line containing a string of $$$9$$$ digits (without any characters between them). Each digit from $$$1$$$ to $$$9$$$ appears in this string exactly once. Output For each test case, print the answer on a separate line as follows: if it is impossible to perform the described operations, print $$$-1$$$; otherwise, print any sequence that represents a prime number, which can be obtained from the given string performing several (maybe zero) operations described in the statement. If there are multiple such sequences, you can print any one of them. Example Input 4 123456789 987654321 243567918 576318429 Output 167 53 3571 57638429
800
true
false
false
false
false
true
false
false
false
false
1,119
1477D
Nezzar designs a brand new game "Hidden Permutations" and shares it with his best friend, Nanako. At the beginning of the game, Nanako and Nezzar both know integers $$$n$$$ and $$$m$$$. The game goes in the following way: Firstly, Nezzar hides two permutations $$$p_1,p_2,ldots,p_n$$$ and $$$q_1,q_2,ldots,q_n$$$ of integers from $$$1$$$ to $$$n$$$, and Nanako secretly selects $$$m$$$ unordered pairs $$$(l_1,r_1),(l_2,r_2),ldots,(l_m,r_m)$$$; After that, Nanako sends his chosen pairs to Nezzar; On receiving those $$$m$$$ unordered pairs, Nezzar checks if there exists $$$1 le i le m$$$, such that $$$(p_{l_i}-p_{r_i})$$$ and $$$(q_{l_i}-q_{r_i})$$$ have different signs. If so, Nezzar instantly loses the game and gets a score of $$$-1$$$. Otherwise, the score Nezzar gets is equal to the number of indices $$$1 le i le n$$$ such that $$$p_i eq q_i$$$. However, Nezzar accidentally knows Nanako's unordered pairs and decides to take advantage of them. Please help Nezzar find out two permutations $$$p$$$ and $$$q$$$ such that the score is maximized. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 5 cdot 10^5$$$) — the number of test cases. The first line of each test case contains two integers $$$n,m$$$ ($$$1 le n le 5 cdot 10^5, 0 le m le min(frac{n(n-1)}{2},5 cdot 10^5)$$$). Then $$$m$$$ lines follow, $$$i$$$-th of them contains two integers $$$l_i,r_i$$$ ($$$1 le l_i,r_i le n$$$, $$$l_i eq r_i$$$), describing the $$$i$$$-th unordered pair Nanako chooses. It is guaranteed that all $$$m$$$ unordered pairs are distinct. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$5 cdot 10^5$$$, and the sum of $$$m$$$ for all test cases does not exceed $$$5cdot 10^5$$$. Output For each test case, print two permutations $$$p_1,p_2,ldots,p_n$$$ and $$$q_1,q_2,ldots,q_n$$$ such that the score Nezzar gets is maximized. Example Input 3 4 2 1 2 3 4 6 4 1 2 1 3 3 5 3 6 2 1 1 2 Output 1 2 3 4 3 4 1 2 2 3 4 1 6 5 1 4 3 2 5 6 1 2 1 2 Note For first test case, for each pair given by Nanako: for the first pair $$$(1,2)$$$: $$$p_1 - p_2 = 1 - 2 = -1$$$, $$$q_1 - q_2 = 3 - 4 = -1$$$, they have the same sign; for the second pair $$$(3,4)$$$: $$$p_3 - p_4 = 3 - 4 = -1$$$, $$$q_3 - q_4 = 1 - 2 = -1$$$, they have the same sign. As Nezzar does not lose instantly, Nezzar gains the score of $$$4$$$ as $$$p_i eq q_i$$$ for all $$$1 leq i leq 4$$$. Obviously, it is the maximum possible score Nezzar can get.
2,800
false
false
false
false
false
true
false
false
false
true
3,287
621C
There are _n_ sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharks _i_ and _i_u2009+u20091 are neighbours for all _i_ from 1 to _n_u2009-u20091. Sharks _n_ and 1 are neighbours too. Each shark will grow some number of flowers _s__i_. For _i_-th shark value _s__i_ is random integer equiprobably chosen in range from _l__i_ to _r__i_. Wet Shark has it's favourite prime number _p_, and he really likes it! If for any pair of neighbouring sharks _i_ and _j_ the product _s__i_·_s__j_ is divisible by _p_, then Wet Shark becomes happy and gives 1000 dollars to each of these sharks. At the end of the day sharks sum all the money Wet Shark granted to them. Find the expectation of this value. Input The first line of the input contains two space-separated integers _n_ and _p_ (3u2009≤u2009_n_u2009≤u2009100u2009000,u20092u2009≤u2009_p_u2009≤u2009109)xa0— the number of sharks and Wet Shark's favourite prime number. It is guaranteed that _p_ is prime. The _i_-th of the following _n_ lines contains information about _i_-th sharkxa0— two space-separated integers _l__i_ and _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009109), the range of flowers shark _i_ can produce. Remember that _s__i_ is chosen equiprobably among all integers from _l__i_ to _r__i_, inclusive. Output Print a single real number — the expected number of dollars that the sharks receive in total. You answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct, if . Examples Input 3 2 1 2 420 421 420420 420421 Note A prime number is a positive integer number that is divisible only by 1 and itself. 1 is not considered to be prime. Consider the first sample. First shark grows some number of flowers from 1 to 2, second sharks grows from 420 to 421 flowers and third from 420420 to 420421. There are eight cases for the quantities of flowers (_s_0,u2009_s_1,u2009_s_2) each shark grows: 1. (1,u2009420,u2009420420): note that _s_0·_s_1u2009=u2009420, _s_1·_s_2u2009=u2009176576400, and _s_2·_s_0u2009=u2009420420. For each pair, 1000 dollars will be awarded to each shark. Therefore, each shark will be awarded 2000 dollars, for a total of 6000 dollars. 2. (1,u2009420,u2009420421): now, the product _s_2·_s_0 is not divisible by 2. Therefore, sharks _s_0 and _s_2 will receive 1000 dollars, while shark _s_1 will receive 2000. The total is 4000. 3. (1,u2009421,u2009420420): total is 4000 4. (1,u2009421,u2009420421): total is 0. 5. (2,u2009420,u2009420420): total is 6000. 6. (2,u2009420,u2009420421): total is 6000. 7. (2,u2009421,u2009420420): total is 6000. 8. (2,u2009421,u2009420421): total is 4000. The expected value is . In the second sample, no combination of quantities will garner the sharks any money.
1,700
true
false
false
false
false
false
true
false
false
false
7,349
895C
Problem - 895C - Codeforces =============== xa0 []( []( [Register]( [Home]( [Top]( [Catalog]( [Contests]( [Gym]( [Problemset]( [Groups]( [Rating]( [Edu]( [API]( [Calendar]( [Help]( [Rayan]( [Codeforces Round 448 (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 combinatorics dp math *2000 No tag edit access → Contest materials [Announcement]( "Codeforces Round #448(Div.2)") [Tutorial]( "Codeforces Round #448(Div.2) Editorial") [Problems]( [Submit]( [Status]( [Standings]( [Custom test]( The problem statement has recently been changed. [View the changes.]( Square Subsets time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Petya was late for the lesson too. The teacher gave him an additional task. For some array _a_ Petya should find the number of different ways to select non-empty subset of elements from it in such a way that their product is equal to a square of some integer. Two ways are considered different if sets of indexes of elements chosen by these ways are different. Since the answer can be very large, you should find the answer modulo 109u2009+u20097. Input First line contains one integer _n_ (1u2009≤u2009_n_u2009≤u2009105)xa0— the number of elements in the array. Second line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u200970)xa0— the elements of the array. Output Print one integerxa0— the number of different ways to choose some elements so that their product is a square of a certain integer modulo 109u2009+u20097. Examples Input 4 1 1 1 1 Output 15 Input 4 2 2 2 2 Output 7 Input 5 1 2 4 5 8 Output 7 Note In first sample product of elements chosen by any way is 1 and 1u2009=u200912. So the answer is 24u2009-u20091u2009=u200915. In second sample there are six different ways to choose elements so that their product is 4, and only one way so that their product is 16. So the answer is 6u2009+u20091u2009=u20097. [Codeforces]( (c) Copyright 2010-2024 Mike Mirzayanov The only programming contests Web 2.0 platform Server time: Nov/30/2024 09:27:30 (g1). Desktop version, switch to [mobile version]( Policy]( by []( User lists Name ---
2,000
true
false
false
true
false
false
true
false
false
false
6,168
1855A
Dalton is the teacher of a class with $$$n$$$ students, numbered from $$$1$$$ to $$$n$$$. The classroom contains $$$n$$$ chairs, also numbered from $$$1$$$ to $$$n$$$. Initially student $$$i$$$ is seated on chair $$$p_i$$$. It is guaranteed that $$$p_1,p_2,dots, p_n$$$ is a permutation of length $$$n$$$. A student is happy if his/her number is different from the number of his/her chair. In order to make all of his students happy, Dalton can repeatedly perform the following operation: choose two distinct students and swap their chairs. What is the minimum number of moves required to make all the students happy? One can show that, under the constraints of this problem, it is possible to make all the students happy with a finite number of moves. A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). The description of the test cases follows. The first line contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$) — the number of students. The second line contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$) — $$$p_i$$$ denotes the initial chair of student $$$i$$$. It is guaranteed that $$$p$$$ is a permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output the minimum number of moves required. Example Input 5 2 2 1 3 1 2 3 5 1 2 5 4 3 4 1 2 4 3 10 10 2 1 3 6 5 4 7 9 8 Note In the first test case, both students are already happy, so Dalton can perform $$$0$$$ moves. In the second test case, Dalton can swap the chairs of students $$$1$$$ and $$$2$$$ to get the array $$$[2, 1, 3]$$$. Then he can swap chairs of students $$$2$$$ and $$$3$$$ to get the array $$$[2, 3, 1]$$$. At this point all the students are happy, and he performed $$$2$$$ moves. It is impossible to perform the task with fewer moves. In the third test case, by swapping the chairs of students $$$1$$$ and $$$2$$$ and then swapping the chairs of students $$$4$$$ and $$$5$$$, Dalton gets the array $$$[2, 1, 5, 3, 4]$$$ in $$$2$$$ moves.
800
true
true
false
false
false
false
false
false
false
false
1,152
1798B
Lottery "Three Sevens" was held for $$$m$$$ days. On day $$$i$$$, $$$n_i$$$ people with the numbers $$$a_{i, 1}, ldots, a_{i, n_i}$$$ participated in the lottery. It is known that in each of the $$$m$$$ days, only one winner was selected from the lottery participants. The lottery winner on day $$$i$$$ was not allowed to participate in the lottery in the days from $$$i+1$$$ to $$$m$$$. Unfortunately, the information about the lottery winners has been lost. You need to find any possible list of lottery winners on days from $$$1$$$ to $$$m$$$ or determine that no solution exists. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 50,000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$m$$$ ($$$1 le m le 50,000$$$)xa0— the number of days in which the lottery was held. Next, for each $$$i$$$ from $$$1$$$ to $$$m$$$, follows a two-line block of data. The first line of each block contains a single integer $$$n_i$$$ ($$$1 le n_i le 50,000$$$)xa0— the number of lottery participants on day $$$i$$$. The second line of the block contains integers $$$a_{i, 1}, ldots, a_{i, n_i}$$$ ($$$1 le a_{i, j} le 50,000$$$)xa0— lottery participants on day $$$i$$$. It is guaranteed that all the numbers $$$a_{i, 1}, ldots, a_{i, n_i}$$$ are pairwise distinct. It is guaranteed that the sum of $$$n_i$$$ over all blocks of all test cases does not exceed $$$50,000$$$. Output For each test case, if there is no solution, print a single integer $$$-1$$$. Otherwise, print $$$m$$$ integers $$$p_1, p_2, ldots, p_m$$$ ($$$1 le p_i le 50,000$$$)xa0— lottery winners on days from $$$1$$$ to $$$m$$$. If there are multiple solutions, print any of them. Example Input 3 3 4 1 2 4 8 3 2 9 1 2 1 4 2 2 1 2 2 2 1 4 4 1 2 3 4 1 1 1 4 1 3 Note In the first test case, one of the answers is $$$[8, 2, 1]$$$ since the participant with the number $$$8$$$ participated on day $$$1$$$, but did not participate on days $$$2$$$ and $$$3$$$; the participant with the number $$$2$$$ participated on day $$$2$$$, but did not participate on day $$$3$$$; and the participant with the number $$$1$$$ participated on day $$$3$$$. Note that this is not the only possible answer, for example, $$$[8, 9, 4]$$$ is also a correct answer. In the second test case, both lottery participants participated on both days, so any possible lottery winner on the day $$$1$$$ must have participated on the day $$$2$$$, which is not allowed. Thus, there is no correct answer. In the third test case, only one participant participated on days $$$2$$$, $$$3$$$, $$$4$$$, and on day $$$1$$$ there is only one participant who did not participate in the lottery on days $$$2, 3, 4$$$xa0— participant $$$2$$$, which means $$$[2, 1, 4, 3]$$$ is the only correct answer to this test case.
1,000
false
true
true
false
true
false
false
false
false
false
1,489
1307F
Bessie is planning a vacation! In Cow-lifornia, there are $$$n$$$ cities, with $$$n-1$$$ bidirectional roads connecting them. It is guaranteed that one can reach any city from any other city. Bessie is considering $$$v$$$ possible vacation plans, with the $$$i$$$-th one consisting of a start city $$$a_i$$$ and destination city $$$b_i$$$. It is known that only $$$r$$$ of the cities have rest stops. Bessie gets tired easily, and cannot travel across more than $$$k$$$ consecutive roads without resting. In fact, she is so desperate to rest that she may travel through the same city multiple times in order to do so. For each of the vacation plans, does there exist a way for Bessie to travel from the starting city to the destination city? Input The first line contains three integers $$$n$$$, $$$k$$$, and $$$r$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$1 le k,r le n$$$) xa0— the number of cities, the maximum number of roads Bessie is willing to travel through in a row without resting, and the number of rest stops. Each of the following $$$n-1$$$ lines contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$, $$$x_i eq y_i$$$), meaning city $$$x_i$$$ and city $$$y_i$$$ are connected by a road. The next line contains $$$r$$$ integers separated by spaces xa0— the cities with rest stops. Each city will appear at most once. The next line contains $$$v$$$ ($$$1 le v le 2 cdot 10^5$$$) xa0— the number of vacation plans. Each of the following $$$v$$$ lines contain two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le n$$$, $$$a_i e b_i$$$) xa0— the start and end city of the vacation plan. Output If Bessie can reach her destination without traveling across more than $$$k$$$ roads without resting for the $$$i$$$-th vacation plan, print YES. Otherwise, print NO. Examples Input 6 2 1 1 2 2 3 2 4 4 5 5 6 2 3 1 3 3 5 3 6 Input 8 3 3 1 2 2 3 3 4 4 5 4 6 6 7 7 8 2 5 8 2 7 1 8 1 Note The graph for the first example is shown below. The rest stop is denoted by red. For the first query, Bessie can visit these cities in order: $$$1, 2, 3$$$. For the second query, Bessie can visit these cities in order: $$$3, 2, 4, 5$$$. For the third query, Bessie cannot travel to her destination. For example, if she attempts to travel this way: $$$3, 2, 4, 5, 6$$$, she travels on more than $$$2$$$ roads without resting. The graph for the second example is shown below.
3,300
false
false
false
false
false
false
false
true
false
false
4,151
1552E
The numbers $$$1, , 2, , dots, , n cdot k$$$ are colored with $$$n$$$ colors. These colors are indexed by $$$1, , 2, , dots, , n$$$. For each $$$1 le i le n$$$, there are exactly $$$k$$$ numbers colored with color $$$i$$$. Let $$$[a, , b]$$$ denote the interval of integers between $$$a$$$ and $$$b$$$ inclusive, that is, the set $$${a, , a + 1, , dots, , b}$$$. You must choose $$$n$$$ intervals $$$[a_1, , b_1], , [a_2, , b_2], , dots, [a_n, , b_n]$$$ such that: for each $$$1 le i le n$$$, it holds $$$1 le a_i < b_i le n cdot k$$$; for each $$$1 le i le n$$$, the numbers $$$a_i$$$ and $$$b_i$$$ are colored with color $$$i$$$; each number $$$1 le x le n cdot k$$$ belongs to at most $$$leftlceil frac{n}{k - 1} ight ceil$$$ intervals. One can show that such a family of intervals always exists under the given constraints. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 100$$$, $$$2 le k le 100$$$) — the number of colors and the number of occurrences of each color. The second line contains $$$n cdot k$$$ integers $$$c_1, , c_2, , dots, , c_{nk}$$$ ($$$1 le c_j le n$$$), where $$$c_j$$$ is the color of number $$$j$$$. It is guaranteed that, for each $$$1 le i le n$$$, it holds $$$c_j = i$$$ for exactly $$$k$$$ distinct indices $$$j$$$. Output Output $$$n$$$ lines. The $$$i$$$-th line should contain the two integers $$$a_i$$$ and $$$b_i$$$. If there are multiple valid choices of the intervals, output any. Note In the first sample, each number can be contained in at most $$$leftlceil frac{4}{3 - 1} ight ceil = 2$$$ intervals. The output is described by the following picture: In the second sample, the only interval to be chosen is forced to be $$$[1, , 2]$$$, and each number is indeed contained in at most $$$leftlceil frac{1}{2 - 1} ight ceil = 1$$$ interval. In the third sample, each number can be contained in at most $$$leftlceil frac{3}{3 - 1} ight ceil = 2$$$ intervals. The output is described by the following picture:
2,300
false
true
false
false
true
true
false
false
false
false
2,880
348B
You are given a rooted tree with _n_ vertices. In each leaf vertex there's a single integer — the number of apples in this vertex. The weight of a subtree is the sum of all numbers in this subtree leaves. For instance, the weight of a subtree that corresponds to some leaf is the number written in the leaf. A tree is balanced if for every vertex _v_ of the tree all its subtrees, corresponding to the children of vertex _v_, are of equal weight. Count the minimum number of apples that you need to remove from the tree (specifically, from some of its leaves) in order to make the tree balanced. Notice that you can always achieve the goal by just removing all apples. Input The first line contains integer _n_ (2u2009≤u2009_n_u2009≤u2009105), showing the number of vertices in the tree. The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009108), _a__i_ is the number of apples in the vertex number _i_. The number of apples in non-leaf vertices is guaranteed to be zero. Then follow _n_u2009-u20091 lines, describing the tree edges. Each line contains a pair of integers _x__i_,u2009_y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_,u2009_x__i_u2009≠u2009_y__i_) — the vertices connected by an edge. The vertices are indexed from 1 to _n_. Vertex 1 is the root. Output Print a single integer — the minimum number of apples to remove in order to make the tree balanced. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the sin, cout streams cin, cout or the %I64d specifier. Examples Input 6 0 0 12 13 5 6 1 2 1 3 1 4 2 5 2 6
2,100
false
false
false
false
false
false
false
true
false
false
8,441
2009G2
This is the hard version of the problem. In this version, it is guaranteed that $$$r geq l+k-1$$$ for all queries. For an arbitrary array $$$b$$$, Yunli can perform the following operation any number of times: Select an index $$$i$$$. Set $$$b_i = x$$$ where $$$x$$$ is any integer she desires ($$$x$$$ is not limited to the interval $$$[1,n]$$$). Denote $$$f(b)$$$ as the minimum number of operations she needs to perform until there exists a consecutive subarray$$$^{ ext{∗}}$$$ of length at least $$$k$$$ in $$$b$$$. Yunli is given an array $$$a$$$ of size $$$n$$$ and asks you $$$q$$$ queries. In each query, you must output $$$sum_{j=l+k-1}^{r} f([a_l, a_{l+1}, ldots, a_j])$$$. $$$^{ ext{∗}}$$$If there exists a consecutive subarray of length $$$k$$$ that starts at index $$$i$$$ ($$$1 leq i leq b-k+1$$$), then $$$b_j = b_{j-1} + 1$$$ for all $$$i < j leq i+k-1$$$. Input The first line contains $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$k$$$, and $$$q$$$ ($$$1 leq k leq n leq 2 cdot 10^5$$$, $$$1 leq q leq 2 cdot 10^5$$$) — the length of the array, the length of the consecutive subarray, and the number of queries. The following line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ ($$$1 leq a_i leq n$$$). The following $$$q$$$ lines contain two integers $$$l$$$ and $$$r$$$ ($$$1 leq l leq r leq n$$$, $$$r geq l+k-1$$$) — the bounds of the query. It is guaranteed the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$ and the sum of $$$q$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output Output $$$sum_{j=l+k-1}^{r} f([a_l, a_{l+1}, ldots, a_j])$$$ for each query on a new line. Example Input 3 7 5 3 1 2 3 2 1 2 3 1 7 2 7 3 7 8 4 2 4 3 1 1 2 4 3 2 3 6 1 5 5 4 2 4 5 1 2 3 1 4 1 5 Note In the second query of the first testcase, we calculate the following function values: $$$f([2,3,2,1,2])=3$$$ because Yunli can set $$$b_3=4$$$, $$$b_4=5$$$, and $$$b_5=6$$$, making a consecutive subarray of size $$$5$$$ in $$$3$$$ moves. $$$f([2,3,2,1,2,3])=2$$$ because we can set $$$b_3=0$$$ and $$$b_2=-1$$$, making a consecutive subarray of size $$$5$$$ in $$$2$$$ moves (starting at position $$$2$$$) The answer to this query is $$$3+2=5$$$.
2,200
false
false
false
true
true
false
false
false
false
false
193
End of preview. Expand in Data Studio
README.md exists but content is empty.
Downloads last month
8