contestId int64 0 1.01k | name stringlengths 2 58 | tags listlengths 0 11 | title stringclasses 523
values | time-limit stringclasses 8
values | memory-limit stringclasses 8
values | problem-description stringlengths 0 7.15k | input-specification stringlengths 0 2.05k | output-specification stringlengths 0 1.5k | demo-input listlengths 0 7 | demo-output listlengths 0 7 | note stringlengths 0 5.24k | test_cases listlengths 0 402 | timeConsumedMillis int64 0 8k | memoryConsumedBytes int64 0 537M | score float64 -1 3.99 | __index_level_0__ int64 0 621k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | none | [
"none"
] | null | null | 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*,<=*j*) such that *i*<=<<=*j* and *p**i*<=><=*p**j*. | The first line of the input contains two integers *n* and *k* (1<=≤<=*n*,<=*k*<=≤<=100<=000) — the number of cows and the length of Farmer John's nap, respectively. | Output a single integer, the maximum messiness that the Mischievous Mess Makers can achieve by performing no more than *k* swaps. | [
"5 2\n",
"1 10\n"
] | [
"10\n",
"0\n"
] | 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. | [
{
"input": "5 2",
"output": "10"
},
{
"input": "1 10",
"output": "0"
},
{
"input": "100000 2",
"output": "399990"
},
{
"input": "1 1",
"output": "0"
},
{
"input": "8 3",
"output": "27"
},
{
"input": "7 1",
"output": "11"
},
{
"input": "1000... | 62 | 0 | 0 | 10,958 | |
888 | Maximum Subsequence | [
"bitmasks",
"divide and conquer",
"meet-in-the-middle"
] | null | null | You are given an array *a* consisting of *n* integers, and additionally an integer *m*. You have to choose some sequence of indices *b*1,<=*b*2,<=...,<=*b**k* (1<=≤<=*b*1<=<<=*b*2<=<<=...<=<<=*b**k*<=≤<=*n*) in such a way that the value of is maximized. Chosen sequence can be empty.
Print the maximum possible value of . | The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=35, 1<=≤<=*m*<=≤<=109).
The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≤<=*a**i*<=≤<=109). | Print the maximum possible value of . | [
"4 4\n5 2 4 1\n",
"3 20\n199 41 299\n"
] | [
"3\n",
"19\n"
] | In the first example you can choose a sequence *b* = {1, 2}, so the sum <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c856546022c2feee13d02a4ec9cd1d361ab3a756.png" style="max-width: 100.0%;max-height: 100.0%;"/> is equal to 7 (and that's 3 after taking it modulo 4).
In the second example you can choose a sequence *b* = {3}. | [
{
"input": "4 4\n5 2 4 1",
"output": "3"
},
{
"input": "3 20\n199 41 299",
"output": "19"
},
{
"input": "5 10\n47 100 49 2 56",
"output": "9"
},
{
"input": "5 1000\n38361 75847 14913 11499 8297",
"output": "917"
},
{
"input": "10 10\n48 33 96 77 67 59 35 15 14 86"... | 46 | 5,529,600 | 0 | 10,959 | |
626 | Block Towers | [
"brute force",
"greedy",
"math",
"number theory"
] | null | null | Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. *n* of the students use pieces made of two blocks and *m* of the students use pieces made of three blocks.
The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students' towers. | The first line of the input contains two space-separated integers *n* and *m* (0<=≤<=*n*,<=*m*<=≤<=1<=000<=000, *n*<=+<=*m*<=><=0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively. | Print a single integer, denoting the minimum possible height of the tallest tower. | [
"1 3\n",
"3 2\n",
"5 0\n"
] | [
"9\n",
"8\n",
"10\n"
] | In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks.
In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks. | [
{
"input": "1 3",
"output": "9"
},
{
"input": "3 2",
"output": "8"
},
{
"input": "5 0",
"output": "10"
},
{
"input": "4 2",
"output": "9"
},
{
"input": "0 1000000",
"output": "3000000"
},
{
"input": "1000000 1",
"output": "2000000"
},
{
"in... | 46 | 0 | 0 | 10,966 | |
687 | TOF | [
"dfs and similar",
"graphs"
] | null | null | Today Pari gave Arya a cool graph problem. Arya wrote a non-optimal solution for it, because he believes in his ability to optimize non-optimal solutions. In addition to being non-optimal, his code was buggy and he tried a lot to optimize it, so the code also became dirty! He keeps getting Time Limit Exceeds and he is disappointed. Suddenly a bright idea came to his mind!
Here is how his dirty code looks like:
He asks you to write the TOF function in order to optimize the running time of the code with minimizing the number of calls of the dfs function. The input is a directed graph and in the TOF function you have to rearrange the edges of the graph in the list neighbors for each vertex. The number of calls of dfs function depends on the arrangement of neighbors of each vertex. | The first line of the input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=5000) — the number of vertices and then number of directed edges in the input graph.
Each of the next *m* lines contains a pair of integers *u**i* and *v**i* (1<=<=≤<=<=*u**i*,<=<=*v**i*<=<=≤<=<=*n*), meaning there is a directed edge in the input graph.
You may assume that the graph won't contain any self-loops and there is at most one edge between any unordered pair of vertices. | Print a single integer — the minimum possible number of dfs calls that can be achieved with permuting the edges. | [
"3 3\n1 2\n2 3\n3 1\n",
"6 7\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n6 4\n"
] | [
"2998\n",
"3001\n"
] | none | [] | 30 | 0 | 0 | 11,010 | |
401 | Roman and Numbers | [
"bitmasks",
"brute force",
"combinatorics",
"dp",
"number theory"
] | null | null | Roman is a young mathematician, very famous in Uzhland. Unfortunately, Sereja doesn't think so. To make Sereja change his mind, Roman is ready to solve any mathematical problem. After some thought, Sereja asked Roma to find, how many numbers are close to number *n*, modulo *m*.
Number *x* is considered close to number *n* modulo *m*, if:
- it can be obtained by rearranging the digits of number *n*, - it doesn't have any leading zeroes, - the remainder after dividing number *x* by *m* equals 0.
Roman is a good mathematician, but the number of such numbers is too huge for him. So he asks you to help him. | The first line contains two integers: *n* (1<=≤<=*n*<=<<=1018) and *m* (1<=≤<=*m*<=≤<=100). | In a single line print a single integer — the number of numbers close to number *n* modulo *m*. | [
"104 2\n",
"223 4\n",
"7067678 8\n"
] | [
"3\n",
"1\n",
"47\n"
] | In the first sample the required numbers are: 104, 140, 410.
In the second sample the required number is 232. | [
{
"input": "104 2",
"output": "3"
},
{
"input": "223 4",
"output": "1"
},
{
"input": "7067678 8",
"output": "47"
},
{
"input": "202 10",
"output": "1"
},
{
"input": "1306432 9",
"output": "0"
},
{
"input": "9653092 9",
"output": "0"
},
{
"i... | 4,000 | 243,916,800 | 0 | 11,048 | |
883 | Palindromic Cut | [
"brute force",
"implementation",
"strings"
] | null | null | Kolya has a string *s* of length *n* consisting of lowercase and uppercase Latin letters and digits.
He wants to rearrange the symbols in *s* and cut it into the minimum number of parts so that each part is a palindrome and all parts have the same lengths. A palindrome is a string which reads the same backward as forward, such as madam or racecar.
Your task is to help Kolya and determine the minimum number of palindromes of equal lengths to cut *s* into, if it is allowed to rearrange letters in *s* before cuttings. | The first line contains an integer *n* (1<=≤<=*n*<=≤<=4·105) — the length of string *s*.
The second line contains a string *s* of length *n* consisting of lowercase and uppercase Latin letters and digits. | Print to the first line an integer *k* — minimum number of palindromes into which you can cut a given string.
Print to the second line *k* strings — the palindromes themselves. Separate them by a space. You are allowed to print palindromes in arbitrary order. All of them should have the same length. | [
"6\naabaac\n",
"8\n0rTrT022\n",
"2\naA\n"
] | [
"2\naba aca ",
"1\n02TrrT20 ",
"2\na A \n"
] | none | [
{
"input": "6\naabaac",
"output": "2\naba aca "
},
{
"input": "8\n0rTrT022",
"output": "1\n02TrrT20 "
},
{
"input": "2\naA",
"output": "2\na A "
},
{
"input": "1\ns",
"output": "1\ns "
},
{
"input": "10\n6IIC6CCIIC",
"output": "1\n6CCIIIICC6 "
},
{
"in... | 61 | 5,529,600 | 0 | 11,075 | |
412 | E-mail Addresses | [
"implementation"
] | null | null | One of the most important products of the R1 company is a popular @r1.com mail service. The R1 mailboxes receive and send millions of emails every day.
Today, the online news thundered with terrible information. The R1 database crashed and almost no data could be saved except for one big string. The developers assume that the string contains the letters of some users of the R1 mail. Recovering letters is a tedious mostly manual work. So before you start this process, it was decided to estimate the difficulty of recovering. Namely, we need to calculate the number of different substrings of the saved string that form correct e-mail addresses.
We assume that valid addresses are only the e-mail addresses which meet the following criteria:
- the address should begin with a non-empty sequence of letters, numbers, characters '_', starting with a letter; - then must go character '@'; - then must go a non-empty sequence of letters or numbers; - then must go character '.'; - the address must end with a non-empty sequence of letters.
You got lucky again and the job was entrusted to you! Please note that the substring is several consecutive characters in a string. Two substrings, one consisting of the characters of the string with numbers *l*1,<=*l*1<=+<=1,<=*l*1<=+<=2,<=...,<=*r*1 and the other one consisting of the characters of the string with numbers *l*2,<=*l*2<=+<=1,<=*l*2<=+<=2,<=...,<=*r*2, are considered distinct if *l*1<=≠<=*l*2 or *r*1<=≠<=*r*2. | The first and the only line contains the sequence of characters *s*1*s*2... *s**n* (1<=≤<=*n*<=≤<=106) — the saved string. It is guaranteed that the given string contains only small English letters, digits and characters '.', '_', '@'. | Print in a single line the number of substrings that are valid e-mail addresses. | [
"[email protected]\n",
"[email protected]@[email protected]\n",
"[email protected]\n",
".asd123__..@\n"
] | [
"18\n",
"8\n",
"1\n",
"0\n"
] | In the first test case all the substrings that are correct e-mail addresses begin from one of the letters of the word agapov and end in one of the letters of the word com.
In the second test case note that the e-mail [[email protected]](/cdn-cgi/l/email-protection) is considered twice in the answer. Note that in this example the e-mail entries overlap inside the string. | [
{
"input": "gerald.agapov1991@gmail.com",
"output": "18"
},
{
"input": "x@x.x@x.x_e_@r1.com",
"output": "8"
},
{
"input": "a___@1.r",
"output": "1"
},
{
"input": ".asd123__..@",
"output": "0"
},
{
"input": "@",
"output": "0"
},
{
"input": ".",
"out... | 1,000 | 18,022,400 | 0 | 11,099 | |
474 | Ant colony | [
"data structures",
"math",
"number theory"
] | null | null | Mole is hungry again. He found one ant colony, consisting of *n* ants, ordered in a row. Each ant *i* (1<=≤<=*i*<=≤<=*n*) has a strength *s**i*.
In order to make his dinner more interesting, Mole organizes a version of «Hunger Games» for the ants. He chooses two numbers *l* and *r* (1<=≤<=*l*<=≤<=*r*<=≤<=*n*) and each pair of ants with indices between *l* and *r* (inclusively) will fight. When two ants *i* and *j* fight, ant *i* gets one battle point only if *s**i* divides *s**j* (also, ant *j* gets one battle point only if *s**j* divides *s**i*).
After all fights have been finished, Mole makes the ranking. An ant *i*, with *v**i* battle points obtained, is going to be freed only if *v**i*<==<=*r*<=-<=*l*, or in other words only if it took a point in every fight it participated. After that, Mole eats the rest of the ants. Note that there can be many ants freed or even none.
In order to choose the best sequence, Mole gives you *t* segments [*l**i*,<=*r**i*] and asks for each of them how many ants is he going to eat if those ants fight. | The first line contains one integer *n* (1<=≤<=*n*<=≤<=105), the size of the ant colony.
The second line contains *n* integers *s*1,<=*s*2,<=...,<=*s**n* (1<=≤<=*s**i*<=≤<=109), the strengths of the ants.
The third line contains one integer *t* (1<=≤<=*t*<=≤<=105), the number of test cases.
Each of the next *t* lines contains two integers *l**i* and *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*), describing one query. | Print to the standard output *t* lines. The *i*-th line contains number of ants that Mole eats from the segment [*l**i*,<=*r**i*]. | [
"5\n1 3 2 4 2\n4\n1 5\n2 5\n3 5\n4 5\n"
] | [
"4\n4\n1\n1\n"
] | In the first test battle points for each ant are *v* = [4, 0, 2, 0, 2], so ant number 1 is freed. Mole eats the ants 2, 3, 4, 5.
In the second test case battle points are *v* = [0, 2, 0, 2], so no ant is freed and all of them are eaten by Mole.
In the third test case battle points are *v* = [2, 0, 2], so ants number 3 and 5 are freed. Mole eats only the ant 4.
In the fourth test case battle points are *v* = [0, 1], so ant number 5 is freed. Mole eats the ant 4. | [
{
"input": "5\n1 3 2 4 2\n4\n1 5\n2 5\n3 5\n4 5",
"output": "4\n4\n1\n1"
}
] | 514 | 11,264,000 | 0 | 11,101 | |
109 | Lucky Sorting | [
"constructive algorithms",
"sortings"
] | D. Lucky Sorting | 3 | 256 | Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya got an array consisting of *n* numbers, it is the gift for his birthday. Now he wants to sort it in the non-decreasing order. However, a usual sorting is boring to perform, that's why Petya invented the following limitation: one can swap any two numbers but only if at least one of them is lucky. Your task is to sort the array according to the specified limitation. Find any possible sequence of the swaps (the number of operations in the sequence should not exceed 2*n*). | The first line contains an integer *n* (1<=≤<=*n*<=≤<=105) — the number of elements in the array. The second line contains *n* positive integers, not exceeding 109 — the array that needs to be sorted in the non-decreasing order. | On the first line print number *k* (0<=≤<=*k*<=≤<=2*n*) — the number of the swaps in the sorting. On the following *k* lines print one pair of distinct numbers (a pair per line) — the indexes of elements to swap. The numbers in the array are numbered starting from 1. If it is impossible to sort the given sequence, print the single number -1.
If there are several solutions, output any. Note that you don't have to minimize *k*. Any sorting with no more than 2*n* swaps is accepted. | [
"2\n4 7\n",
"3\n4 2 1\n",
"7\n77 66 55 44 33 22 11\n"
] | [
"0\n",
"1\n1 3\n",
"7\n1 7\n7 2\n2 6\n6 7\n3 4\n5 3\n4 5\n"
] | none | [
{
"input": "2\n4 7",
"output": "0"
},
{
"input": "3\n4 2 1",
"output": "1\n1 3"
},
{
"input": "7\n77 66 55 44 33 22 11",
"output": "9\n4 7\n1 7\n1 6\n2 6\n2 5\n3 5\n2 3\n1 2\n1 4"
},
{
"input": "7\n1 2 3 4 5 6 7",
"output": "0"
},
{
"input": "4\n47 1 7 2",
"ou... | 280 | 0 | 0 | 11,126 |
261 | Maxim and Discounts | [
"greedy",
"sortings"
] | null | null | Maxim always goes to the supermarket on Sundays. Today the supermarket has a special offer of discount systems.
There are *m* types of discounts. We assume that the discounts are indexed from 1 to *m*. To use the discount number *i*, the customer takes a special basket, where he puts exactly *q**i* items he buys. Under the terms of the discount system, in addition to the items in the cart the customer can receive at most two items from the supermarket for free. The number of the "free items" (0, 1 or 2) to give is selected by the customer. The only condition imposed on the selected "free items" is as follows: each of them mustn't be more expensive than the cheapest item out of the *q**i* items in the cart.
Maxim now needs to buy *n* items in the shop. Count the minimum sum of money that Maxim needs to buy them, if he use the discount system optimally well.
Please assume that the supermarket has enough carts for any actions. Maxim can use the same discount multiple times. Of course, Maxim can buy items without any discounts. | The first line contains integer *m* (1<=≤<=*m*<=≤<=105) — the number of discount types. The second line contains *m* integers: *q*1,<=*q*2,<=...,<=*q**m* (1<=≤<=*q**i*<=≤<=105).
The third line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of items Maxim needs. The fourth line contains *n* integers: *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=104) — the items' prices.
The numbers in the lines are separated by single spaces. | In a single line print a single integer — the answer to the problem. | [
"1\n2\n4\n50 50 100 100\n",
"2\n2 3\n5\n50 50 50 50 50\n",
"1\n1\n7\n1 1 1 1 1 1 1\n"
] | [
"200\n",
"150\n",
"3\n"
] | In the first sample Maxim needs to buy two items that cost 100 and get a discount for two free items that cost 50. In that case, Maxim is going to pay 200.
In the second sample the best strategy for Maxim is to buy 3 items and get 2 items for free using the discount. In that case, Maxim is going to pay 150. | [
{
"input": "1\n2\n4\n50 50 100 100",
"output": "200"
},
{
"input": "2\n2 3\n5\n50 50 50 50 50",
"output": "150"
},
{
"input": "1\n1\n7\n1 1 1 1 1 1 1",
"output": "3"
},
{
"input": "60\n7 4 20 15 17 6 2 2 3 18 13 14 16 11 13 12 6 10 14 1 16 6 4 9 10 8 10 15 16 13 13 9 16 11 5 ... | 404 | 9,420,800 | 3 | 11,136 | |
40 | Repaintings | [
"math"
] | B. Repaintings | 2 | 256 | A chessboard *n*<=×<=*m* in size is given. During the zero minute we repaint all the black squares to the 0 color. During the *i*-th minute we repaint to the *i* color the initially black squares that have exactly four corner-adjacent squares painted *i*<=-<=1 (all such squares are repainted simultaneously). This process continues ad infinitum. You have to figure out how many squares we repainted exactly *x* times.
The upper left square of the board has to be assumed to be always black. Two squares are called corner-adjacent, if they have exactly one common point. | The first line contains integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=5000). The second line contains integer *x* (1<=≤<=*x*<=≤<=109). | Print how many squares will be painted exactly *x* times. | [
"3 3\n1\n",
"3 3\n2\n",
"1 1\n1\n"
] | [
"4\n",
"1\n",
"1\n"
] | none | [
{
"input": "3 3\n1",
"output": "4"
},
{
"input": "3 3\n2",
"output": "1"
},
{
"input": "1 1\n1",
"output": "1"
},
{
"input": "8 8\n8",
"output": "0"
},
{
"input": "9 10\n1",
"output": "17"
},
{
"input": "9 9\n3",
"output": "8"
},
{
"input":... | 0 | 0 | -1 | 11,165 |
0 | none | [
"none"
] | null | null | You are given a sequence *a*1,<=*a*2,<=...,<=*a**n* consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.
Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.
Every element of the sequence must appear in exactly one subsequence. | The first line of input data contains integer *n* (1<=≤<=*n*<=≤<=105) — the length of the sequence.
The second line of input data contains *n* different integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=≤<=*a**i*<=≤<=109) — the elements of the sequence. It is guaranteed that all elements of the sequence are distinct. | In the first line print the maximum number of subsequences *k*, which the original sequence can be split into while fulfilling the requirements.
In the next *k* lines print the description of subsequences in the following format: the number of elements in subsequence *c**i* (0<=<<=*c**i*<=≤<=*n*), then *c**i* integers *l*1,<=*l*2,<=...,<=*l**c**i* (1<=≤<=*l**j*<=≤<=*n*) — indices of these elements in the original sequence.
Indices could be printed in any order. Every index from 1 to *n* must appear in output exactly once.
If there are several possible answers, print any of them. | [
"6\n3 2 1 6 5 4\n",
"6\n83 -75 -49 11 37 62\n"
] | [
"4\n2 1 3\n1 2\n2 4 6\n1 5\n",
"1\n6 1 2 3 4 5 6\n"
] | In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing. | [
{
"input": "6\n3 2 1 6 5 4",
"output": "4\n2 1 3\n1 2\n2 4 6\n1 5"
},
{
"input": "6\n83 -75 -49 11 37 62",
"output": "1\n6 1 2 3 4 5 6"
},
{
"input": "1\n1",
"output": "1\n1 1"
},
{
"input": "2\n1 2",
"output": "2\n1 1\n1 2"
},
{
"input": "2\n2 1",
"output": "... | 436 | 21,606,400 | 3 | 11,177 | |
456 | Fedya and Maths | [
"math",
"number theory"
] | null | null | Fedya studies in a gymnasium. Fedya's maths hometask is to calculate the following expression:
for given value of *n*. Fedya managed to complete the task. Can you? Note that given number *n* can be extremely large (e.g. it can exceed any integer type of your programming language). | The single line contains a single integer *n* (0<=≤<=*n*<=≤<=10105). The number doesn't contain any leading zeroes. | Print the value of the expression without leading zeros. | [
"4\n",
"124356983594583453458888889\n"
] | [
"4\n",
"0\n"
] | Operation *x* *mod* *y* means taking remainder after division *x* by *y*.
Note to the first sample:
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/825f244180bb10323db01645118c3cfdb312fa89.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "4",
"output": "4"
},
{
"input": "124356983594583453458888889",
"output": "0"
},
{
"input": "2",
"output": "0"
},
{
"input": "7854",
"output": "0"
},
{
"input": "584660",
"output": "4"
},
{
"input": "464",
"output": "4"
},
{
"inp... | 30 | 0 | -1 | 11,179 | |
808 | Anthem of Berland | [
"dp",
"strings"
] | null | null | 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. | The first line contains string of small Latin letters and question marks *s* (1<=≤<=|*s*|<=≤<=105).
The second line contains string of small Latin letters *t* (1<=≤<=|*t*|<=≤<=105).
Product of lengths of strings |*s*|·|*t*| won't exceed 107. | 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. | [
"winlose???winl???w??\nwin\n",
"glo?yto?e??an?\nor\n",
"??c?????\nabcab\n"
] | [
"5\n",
"3\n",
"2\n"
] | 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". | [
{
"input": "winlose???winl???w??\nwin",
"output": "5"
},
{
"input": "glo?yto?e??an?\nor",
"output": "3"
},
{
"input": "??c?????\nabcab",
"output": "2"
},
{
"input": "ddddd\nd",
"output": "5"
},
{
"input": "ww?ww\nw",
"output": "5"
},
{
"input": "?????\... | 296 | 7,782,400 | 0 | 11,210 | |
635 | Orchestra | [
"brute force",
"implementation"
] | null | null | Paul is at the orchestra. The string section is arranged in an *r*<=×<=*c* rectangular grid and is filled with violinists with the exception of *n* violists. Paul really likes violas, so he would like to take a picture including at least *k* of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.
Two pictures are considered to be different if the coordinates of corresponding rectangles are different. | The first line of input contains four space-separated integers *r*, *c*, *n*, *k* (1<=≤<=*r*,<=*c*,<=*n*<=≤<=10, 1<=≤<=*k*<=≤<=*n*) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.
The next *n* lines each contain two integers *x**i* and *y**i* (1<=≤<=*x**i*<=≤<=*r*, 1<=≤<=*y**i*<=≤<=*c*): the position of the *i*-th viola. It is guaranteed that no location appears more than once in the input. | Print a single integer — the number of photographs Paul can take which include at least *k* violas. | [
"2 2 1 1\n1 2\n",
"3 2 3 3\n1 1\n3 1\n2 2\n",
"3 2 3 2\n1 1\n3 1\n2 2\n"
] | [
"4\n",
"1\n",
"4\n"
] | We will use '*' to denote violinists and '#' to denote violists.
In the first sample, the orchestra looks as follows
In the second sample, the orchestra looks as follows
In the third sample, the orchestra looks the same as in the second sample. | [
{
"input": "2 2 1 1\n1 2",
"output": "4"
},
{
"input": "3 2 3 3\n1 1\n3 1\n2 2",
"output": "1"
},
{
"input": "3 2 3 2\n1 1\n3 1\n2 2",
"output": "4"
},
{
"input": "1 1 1 1\n1 1",
"output": "1"
},
{
"input": "10 10 10 10\n6 1\n3 8\n10 6\n10 3\n10 4\n8 9\n2 3\n5 7\n... | 62 | 0 | 3 | 11,219 | |
0 | none | [
"none"
] | null | null | Two participants are each given a pair of distinct numbers from 1 to 9 such that there's exactly one number that is present in both pairs. They want to figure out the number that matches by using a communication channel you have access to without revealing it to you.
Both participants communicated to each other a set of pairs of numbers, that includes the pair given to them. Each pair in the communicated sets comprises two different numbers.
Determine if you can with certainty deduce the common number, or if you can determine with certainty that both participants know the number but you do not. | The first line contains two integers $n$ and $m$ ($1 \le n, m \le 12$) — the number of pairs the first participant communicated to the second and vice versa.
The second line contains $n$ pairs of integers, each between $1$ and $9$, — pairs of numbers communicated from first participant to the second.
The third line contains $m$ pairs of integers, each between $1$ and $9$, — pairs of numbers communicated from the second participant to the first.
All pairs within each set are distinct (in particular, if there is a pair $(1,2)$, there will be no pair $(2,1)$ within the same set), and no pair contains the same number twice.
It is guaranteed that the two sets do not contradict the statements, in other words, there is pair from the first set and a pair from the second set that share exactly one number. | If you can deduce the shared number with certainty, print that number.
If you can with certainty deduce that both participants know the shared number, but you do not know it, print $0$.
Otherwise print $-1$. | [
"2 2\n1 2 3 4\n1 5 3 4\n",
"2 2\n1 2 3 4\n1 5 6 4\n",
"2 3\n1 2 4 5\n1 2 1 3 2 3\n"
] | [
"1\n",
"0\n",
"-1\n"
] | In the first example the first participant communicated pairs $(1,2)$ and $(3,4)$, and the second communicated $(1,5)$, $(3,4)$. Since we know that the actual pairs they received share exactly one number, it can't be that they both have $(3,4)$. Thus, the first participant has $(1,2)$ and the second has $(1,5)$, and at this point you already know the shared number is $1$.
In the second example either the first participant has $(1,2)$ and the second has $(1,5)$, or the first has $(3,4)$ and the second has $(6,4)$. In the first case both of them know the shared number is $1$, in the second case both of them know the shared number is $4$. You don't have enough information to tell $1$ and $4$ apart.
In the third case if the first participant was given $(1,2)$, they don't know what the shared number is, since from their perspective the second participant might have been given either $(1,3)$, in which case the shared number is $1$, or $(2,3)$, in which case the shared number is $2$. While the second participant does know the number with certainty, neither you nor the first participant do, so the output is $-1$. | [
{
"input": "2 2\n1 2 3 4\n1 5 3 4",
"output": "1"
},
{
"input": "2 2\n1 2 3 4\n1 5 6 4",
"output": "0"
},
{
"input": "2 3\n1 2 4 5\n1 2 1 3 2 3",
"output": "-1"
},
{
"input": "2 1\n1 2 1 3\n1 2",
"output": "1"
},
{
"input": "4 4\n1 2 3 4 5 6 7 8\n2 3 4 5 6 7 8 1",... | 93 | 102,400 | 3 | 11,274 | |
239 | Easy Tape Programming | [
"brute force",
"implementation"
] | null | null | There is a programming language in which every program is a non-empty sequence of "<" and ">" signs and digits. Let's explain how the interpreter of this programming language works. A program is interpreted using movement of instruction pointer (IP) which consists of two parts.
- Current character pointer (CP); - Direction pointer (DP) which can point left or right;
Initially CP points to the leftmost character of the sequence and DP points to the right.
We repeat the following steps until the first moment that CP points to somewhere outside the sequence.
- If CP is pointing to a digit the interpreter prints that digit then CP moves one step according to the direction of DP. After that the value of the printed digit in the sequence decreases by one. If the printed digit was 0 then it cannot be decreased therefore it's erased from the sequence and the length of the sequence decreases by one. - If CP is pointing to "<" or ">" then the direction of DP changes to "left" or "right" correspondingly. Then CP moves one step according to DP. If the new character that CP is pointing to is "<" or ">" then the previous character will be erased from the sequence.
If at any moment the CP goes outside of the sequence the execution is terminated.
It's obvious the every program in this language terminates after some steps.
We have a sequence *s*1,<=*s*2,<=...,<=*s**n* of "<", ">" and digits. You should answer *q* queries. Each query gives you *l* and *r* and asks how many of each digit will be printed if we run the sequence *s**l*,<=*s**l*<=+<=1,<=...,<=*s**r* as an independent program in this language. | The first line of input contains two integers *n* and *q* (1<=≤<=*n*,<=*q*<=≤<=100) — represents the length of the sequence *s* and the number of queries.
The second line contains *s*, a sequence of "<", ">" and digits (0..9) written from left to right. Note, that the characters of *s* are not separated with spaces.
The next *q* lines each contains two integers *l**i* and *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*) — the *i*-th query. | For each query print 10 space separated integers: *x*0,<=*x*1,<=...,<=*x*9 where *x**i* equals the number of times the interpreter prints *i* while running the corresponding program. Print answers to the queries in the order they are given in input. | [
"7 4\n1>3>22<\n1 3\n4 7\n7 7\n1 7\n"
] | [
"0 1 0 1 0 0 0 0 0 0 \n2 2 2 0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n2 3 2 1 0 0 0 0 0 0 \n"
] | none | [
{
"input": "7 4\n1>3>22<\n1 3\n4 7\n7 7\n1 7",
"output": "0 1 0 1 0 0 0 0 0 0 \n2 2 2 0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 0 0 0 \n2 3 2 1 0 0 0 0 0 0 "
},
{
"input": "5 2\n>>>>>\n1 5\n1 2",
"output": "0 0 0 0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 0 0 0 "
},
{
"input": "1 3\n9\n1 1\n1 1\n1 1",
"outpu... | 186 | 307,200 | 0 | 11,285 | |
769 | k-Interesting Pairs Of Integers | [
"*special",
"bitmasks",
"brute force",
"meet-in-the-middle"
] | null | null | Vasya has the sequence consisting of *n* integers. Vasya consider the pair of integers *x* and *y* k-interesting, if their binary representation differs from each other exactly in *k* bits. For example, if *k*<==<=2, the pair of integers *x*<==<=5 and *y*<==<=3 is k-interesting, because their binary representation *x*=101 and *y*=011 differs exactly in two bits.
Vasya wants to know how many pairs of indexes (*i*, *j*) are in his sequence so that *i*<=<<=*j* and the pair of integers *a**i* and *a**j* is k-interesting. Your task is to help Vasya and determine this number. | The first line contains two integers *n* and *k* (2<=≤<=*n*<=≤<=105, 0<=≤<=*k*<=≤<=14) — the number of integers in Vasya's sequence and the number of bits in which integers in k-interesting pair should differ.
The second line contains the sequence *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=104), which Vasya has. | Print the number of pairs (*i*, *j*) so that *i*<=<<=*j* and the pair of integers *a**i* and *a**j* is k-interesting. | [
"4 1\n0 3 2 1\n",
"6 0\n200 100 100 100 200 200\n"
] | [
"4\n",
"6\n"
] | In the first test there are 4 k-interesting pairs:
- (1, 3), - (1, 4), - (2, 3), - (2, 4).
In the second test *k* = 0. Consequently, integers in any k-interesting pair should be equal to themselves. Thus, for the second test there are 6 k-interesting pairs:
- (1, 5), - (1, 6), - (2, 3), - (2, 4), - (3, 4), - (5, 6). | [
{
"input": "4 1\n0 3 2 1",
"output": "4"
},
{
"input": "6 0\n200 100 100 100 200 200",
"output": "6"
},
{
"input": "2 0\n1 1",
"output": "1"
},
{
"input": "2 0\n0 0",
"output": "1"
},
{
"input": "2 0\n10000 10000",
"output": "1"
},
{
"input": "2 0\n0 1... | 2,000 | 9,216,000 | 0 | 11,306 | |
196 | Infinite Maze | [
"dfs and similar",
"graphs"
] | null | null | We've got a rectangular *n*<=×<=*m*-cell maze. Each cell is either passable, or is a wall (impassable). A little boy found the maze and cyclically tiled a plane with it so that the plane became an infinite maze. Now on this plane cell (*x*,<=*y*) is a wall if and only if cell is a wall.
In this problem is a remainder of dividing number *a* by number *b*.
The little boy stood at some cell on the plane and he wondered whether he can walk infinitely far away from his starting position. From cell (*x*,<=*y*) he can go to one of the following cells: (*x*,<=*y*<=-<=1), (*x*,<=*y*<=+<=1), (*x*<=-<=1,<=*y*) and (*x*<=+<=1,<=*y*), provided that the cell he goes to is not a wall. | The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=1500) — the height and the width of the maze that the boy used to cyclically tile the plane.
Each of the next *n* lines contains *m* characters — the description of the labyrinth. Each character is either a "#", that marks a wall, a ".", that marks a passable cell, or an "S", that marks the little boy's starting point.
The starting point is a passable cell. It is guaranteed that character "S" occurs exactly once in the input. | Print "Yes" (without the quotes), if the little boy can walk infinitely far from the starting point. Otherwise, print "No" (without the quotes). | [
"5 4\n##.#\n##S#\n#..#\n#.##\n#..#\n",
"5 4\n##.#\n##S#\n#..#\n..#.\n#.##\n"
] | [
"Yes\n",
"No\n"
] | In the first sample the little boy can go up for infinitely long as there is a "clear path" that goes vertically. He just needs to repeat the following steps infinitely: up, up, left, up, up, right, up.
In the second sample the vertical path is blocked. The path to the left doesn't work, too — the next "copy" of the maze traps the boy. | [] | 2,000 | 123,392,000 | 0 | 11,311 | |
883 | Lost in Transliteration | [
"implementation"
] | null | null | There are some ambiguities when one writes Berland names with the letters of the Latin alphabet.
For example, the Berland sound u can be written in the Latin alphabet as "u", and can be written as "oo". For this reason, two words "ulyana" and "oolyana" denote the same name.
The second ambiguity is about the Berland sound h: one can use both "h" and "kh" to write it. For example, the words "mihail" and "mikhail" denote the same name.
There are *n* users registered on the Polycarp's website. Each of them indicated a name represented by the Latin letters. How many distinct names are there among them, if two ambiguities described above are taken into account?
Formally, we assume that two words denote the same name, if using the replacements "u" "oo" and "h" "kh", you can make the words equal. One can make replacements in both directions, in any of the two words an arbitrary number of times. A letter that resulted from the previous replacement can participate in the next replacements.
For example, the following pairs of words denote the same name:
- "koouper" and "kuooper". Making the replacements described above, you can make both words to be equal: "koouper" "kuuper" and "kuooper" "kuuper". - "khun" and "kkkhoon". With the replacements described above you can make both words to be equal: "khun" "khoon" and "kkkhoon" "kkhoon" "khoon".
For a given list of words, find the minimal number of groups where the words in each group denote the same name. | The first line contains integer number *n* (2<=≤<=*n*<=≤<=400) — number of the words in the list.
The following *n* lines contain words, one word per line. Each word consists of only lowercase Latin letters. The length of each word is between 1 and 20 letters inclusive. | Print the minimal number of groups where the words in each group denote the same name. | [
"10\nmihail\noolyana\nkooooper\nhoon\nulyana\nkoouper\nmikhail\nkhun\nkuooper\nkkkhoon\n",
"9\nhariton\nhkariton\nbuoi\nkkkhariton\nboooi\nbui\nkhariton\nboui\nboi\n",
"2\nalex\nalex\n"
] | [
"4\n",
"5\n",
"1\n"
] | There are four groups of words in the first example. Words in each group denote same name:
1. "mihail", "mikhail" 1. "oolyana", "ulyana" 1. "kooooper", "koouper" 1. "hoon", "khun", "kkkhoon"
There are five groups of words in the second example. Words in each group denote same name:
1. "hariton", "kkkhariton", "khariton" 1. "hkariton" 1. "buoi", "boooi", "boui" 1. "bui" 1. "boi"
In the third example the words are equal, so they denote the same name. | [
{
"input": "10\nmihail\noolyana\nkooooper\nhoon\nulyana\nkoouper\nmikhail\nkhun\nkuooper\nkkkhoon",
"output": "4"
},
{
"input": "9\nhariton\nhkariton\nbuoi\nkkkhariton\nboooi\nbui\nkhariton\nboui\nboi",
"output": "5"
},
{
"input": "2\nalex\nalex",
"output": "1"
},
{
"input": ... | 62 | 5,529,600 | 3 | 11,325 | |
35 | Parade | [
"data structures",
"sortings"
] | E. Parade | 2 | 64 | No Great Victory anniversary in Berland has ever passed without the war parade. This year is not an exception. That’s why the preparations are on in full strength. Tanks are building a line, artillery mounts are ready to fire, soldiers are marching on the main square... And the air forces general Mr. Generalov is in trouble again. This year a lot of sky-scrapers have been built which makes it difficult for the airplanes to fly above the city. It was decided that the planes should fly strictly from south to north. Moreover, there must be no sky scraper on a plane’s route, otherwise the anniversary will become a tragedy. The Ministry of Building gave the data on *n* sky scrapers (the rest of the buildings are rather small and will not be a problem to the planes). When looking at the city from south to north as a geometrical plane, the *i*-th building is a rectangle of height *h**i*. Its westernmost point has the x-coordinate of *l**i* and the easternmost — of *r**i*. The terrain of the area is plain so that all the buildings stand on one level. Your task as the Ministry of Defence’s head programmer is to find an enveloping polyline using the data on the sky-scrapers. The polyline’s properties are as follows:
- If you look at the city from south to north as a plane, then any part of any building will be inside or on the boarder of the area that the polyline encloses together with the land surface. - The polyline starts and ends on the land level, i.e. at the height equal to 0. - The segments of the polyline are parallel to the coordinate axes, i.e. they can only be vertical or horizontal. - The polyline’s vertices should have integer coordinates. - If you look at the city from south to north the polyline (together with the land surface) must enclose the minimum possible area. - The polyline must have the smallest length among all the polylines, enclosing the minimum possible area with the land. - The consecutive segments of the polyline must be perpendicular. | The first input line contains integer *n* (1<=≤<=*n*<=≤<=100000). Then follow *n* lines, each containing three integers *h**i*, *l**i*, *r**i* (1<=≤<=*h**i*<=≤<=109,<=<=-<=109<=≤<=*l**i*<=<<=*r**i*<=≤<=109). | In the first line output integer *m* — amount of vertices of the enveloping polyline. The next *m* lines should contain 2 integers each — the position and the height of the polyline’s vertex. Output the coordinates of each vertex in the order of traversing the polyline from west to east. Remember that the first and the last vertices of the polyline should have the height of 0. | [
"2\n3 0 2\n4 1 3\n",
"5\n3 -3 0\n2 -1 1\n4 2 4\n2 3 7\n3 6 8\n"
] | [
"6\n0 0\n0 3\n1 3\n1 4\n3 4\n3 0\n",
"14\n-3 0\n-3 3\n0 3\n0 2\n1 2\n1 0\n2 0\n2 4\n4 4\n4 2\n6 2\n6 3\n8 3\n8 0\n"
] | none | [
{
"input": "2\n3 0 2\n4 1 3",
"output": "6\n0 0\n0 3\n1 3\n1 4\n3 4\n3 0"
},
{
"input": "5\n3 -3 0\n2 -1 1\n4 2 4\n2 3 7\n3 6 8",
"output": "14\n-3 0\n-3 3\n0 3\n0 2\n1 2\n1 0\n2 0\n2 4\n4 4\n4 2\n6 2\n6 3\n8 3\n8 0"
},
{
"input": "7\n5 -5 -4\n3 -3 0\n2 -1 1\n1 0 1\n4 2 4\n2 3 7\n3 6 8",... | 46 | 0 | 0 | 11,330 |
166 | Median | [
"greedy",
"math",
"sortings"
] | null | null | A median in an array with the length of *n* is an element which occupies position number after we sort the elements in the non-decreasing order (the array elements are numbered starting with 1). A median of an array (2,<=6,<=1,<=2,<=3) is the number 2, and a median of array (0,<=96,<=17,<=23) — the number 17.
We define an expression as the integer part of dividing number *a* by number *b*.
One day Vasya showed Petya an array consisting of *n* integers and suggested finding the array's median. Petya didn't even look at the array and said that it equals *x*. Petya is a very honest boy, so he decided to add several numbers to the given array so that the median of the resulting array would be equal to *x*.
Petya can add any integers from 1 to 105 to the array, including the same numbers. Of course, he can add nothing to the array. If a number is added multiple times, then we should consider it the number of times it occurs. It is not allowed to delete of change initial numbers of the array.
While Petya is busy distracting Vasya, your task is to find the minimum number of elements he will need. | The first input line contains two space-separated integers *n* and *x* (1<=≤<=*n*<=≤<=500, 1<=≤<=*x*<=≤<=105) — the initial array's length and the required median's value. The second line contains *n* space-separated numbers — the initial array. The elements of the array are integers from 1 to 105. The array elements are not necessarily different. | Print the only integer — the minimum number of elements Petya needs to add to the array so that its median equals *x*. | [
"3 10\n10 20 30\n",
"3 4\n1 2 3\n"
] | [
"1\n",
"4\n"
] | In the first sample we can add number 9 to array (10, 20, 30). The resulting array (9, 10, 20, 30) will have a median in position <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7dd92241318a531b780c7783dfa446a3e413115e.png" style="max-width: 100.0%;max-height: 100.0%;"/>, that is, 10.
In the second sample you should add numbers 4, 5, 5, 5. The resulting array has median equal to 4. | [
{
"input": "3 10\n10 20 30",
"output": "1"
},
{
"input": "3 4\n1 2 3",
"output": "4"
},
{
"input": "2 2\n3 2",
"output": "0"
},
{
"input": "5 1\n1 1 2 1 2",
"output": "0"
},
{
"input": "5 4\n5 5 4 3 5",
"output": "1"
},
{
"input": "10 2\n2 2 1 3 2 1 2 ... | 156 | 6,758,400 | 3 | 11,333 | |
519 | A and B and Interesting Substrings | [
"data structures",
"dp",
"two pointers"
] | null | null | A and B are preparing themselves for programming contests.
After several years of doing sports programming and solving many problems that require calculating all sorts of abstract objects, A and B also developed rather peculiar tastes.
A likes lowercase letters of the Latin alphabet. He has assigned to each letter a number that shows how much he likes that letter (he has assigned negative numbers to the letters he dislikes).
B likes substrings. He especially likes the ones that start and end with the same letter (their length must exceed one).
Also, A and B have a string *s*. Now they are trying to find out how many substrings *t* of a string *s* are interesting to B (that is, *t* starts and ends with the same letter and its length is larger than one), and also the sum of values of all letters (assigned by A), except for the first and the last one is equal to zero.
Naturally, A and B have quickly found the number of substrings *t* that are interesting to them. Can you do it? | The first line contains 26 integers *x**a*,<=*x**b*,<=...,<=*x**z* (<=-<=105<=≤<=*x**i*<=≤<=105) — the value assigned to letters *a*,<=*b*,<=*c*,<=...,<=*z* respectively.
The second line contains string *s* of length between 1 and 105 characters, consisting of Lating lowercase letters— the string for which you need to calculate the answer. | Print the answer to the problem. | [
"1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\nxabcab\n",
"1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\naaa\n"
] | [
"2\n",
"2\n"
] | In the first sample test strings satisfying the condition above are *abca* and *bcab*.
In the second sample test strings satisfying the condition above are two occurences of *aa*. | [
{
"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\nxabcab",
"output": "2"
},
{
"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\naaa",
"output": "2"
},
{
"input": "1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1\naabbccdd",
"output": "4"
},
{
... | 77 | 0 | 0 | 11,353 | |
889 | Mod Mod Mod | [
"binary search",
"dp",
"math"
] | null | null | You are given a sequence of integers *a*1,<=*a*2,<=...,<=*a**n*. Let , and for 1<=≤<=*i*<=<<=*n*. Here, denotes the modulus operation. Find the maximum value of *f*(*x*,<=1) over all nonnegative integers *x*. | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=200000) — the length of the sequence.
The second lines contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=1013) — the elements of the sequence. | Output a single integer — the maximum value of *f*(*x*,<=1) over all nonnegative integers *x*. | [
"2\n10 5\n",
"5\n5 4 3 2 1\n",
"4\n5 10 5 10\n"
] | [
"13\n",
"6\n",
"16\n"
] | In the first example you can choose, for example, *x* = 19.
In the second example you can choose, for example, *x* = 3 or *x* = 2. | [] | 2,000 | 0 | 0 | 11,426 | |
255 | Mr. Bender and Square | [
"binary search",
"implementation",
"math"
] | null | null | Mr. Bender has a digital table of size *n*<=×<=*n*, each cell can be switched on or off. He wants the field to have at least *c* switched on squares. When this condition is fulfilled, Mr Bender will be happy.
We'll consider the table rows numbered from top to bottom from 1 to *n*, and the columns — numbered from left to right from 1 to *n*. Initially there is exactly one switched on cell with coordinates (*x*,<=*y*) (*x* is the row number, *y* is the column number), and all other cells are switched off. Then each second we switch on the cells that are off but have the side-adjacent cells that are on.
For a cell with coordinates (*x*,<=*y*) the side-adjacent cells are cells with coordinates (*x*<=-<=1,<=*y*), (*x*<=+<=1,<=*y*), (*x*,<=*y*<=-<=1), (*x*,<=*y*<=+<=1).
In how many seconds will Mr. Bender get happy? | The first line contains four space-separated integers *n*,<=*x*,<=*y*,<=*c* (1<=≤<=*n*,<=*c*<=≤<=109; 1<=≤<=*x*,<=*y*<=≤<=*n*; *c*<=≤<=*n*2). | In a single line print a single integer — the answer to the problem. | [
"6 4 3 1\n",
"9 3 8 10\n"
] | [
"0\n",
"2\n"
] | Initially the first test has one painted cell, so the answer is 0. In the second test all events will go as is shown on the figure. <img class="tex-graphics" src="https://espresso.codeforces.com/51bd695513bdc59c6ded01f0d34daa5361285209.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "6 4 3 1",
"output": "0"
},
{
"input": "9 3 8 10",
"output": "2"
},
{
"input": "9 4 3 10",
"output": "2"
},
{
"input": "9 8 2 10",
"output": "2"
},
{
"input": "1 1 1 1",
"output": "0"
},
{
"input": "10 7 2 7",
"output": "2"
},
{
... | 60 | 0 | 0 | 11,443 | |
309 | Memory for Arrays | [
"binary search",
"bitmasks",
"greedy"
] | null | null | You get to work and turn on the computer. You start coding and give little thought to the RAM role in the whole process. In this problem your task is to solve one of the problems you encounter in your computer routine.
We'll consider the RAM as a sequence of cells that can contain data. Some cells already contain some data, some are empty. The empty cells form the so-called memory clusters. Thus, a memory cluster is a sequence of some consecutive empty memory cells.
You have exactly *n* memory clusters, the *i*-th cluster consists of *a**i* cells. You need to find memory for *m* arrays in your program. The *j*-th array takes 2*b**j* consecutive memory cells. There possibly isn't enough memory for all *m* arrays, so your task is to determine what maximum number of arrays can be located in the available memory clusters. Of course, the arrays cannot be divided between the memory clusters. Also, no cell can belong to two arrays. | The first line of the input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=106). The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109). The next line contains *m* integers *b*1,<=*b*2,<=...,<=*b**m* (1<=≤<=2*b**i*<=≤<=109). | Print a single integer — the answer to the problem. | [
"5 3\n8 4 3 2 2\n3 2 2\n",
"10 6\n1 1 1 1 1 1 1 1 1 1\n0 0 0 0 0 0\n"
] | [
"2\n",
"6\n"
] | In the first example you are given memory clusters with sizes 8, 4, 3, 2, 2 and arrays with sizes 8, 4, 4. There are few ways to obtain an answer equals 2: you can locate array with size 8 to the cluster with size 8, and one of the arrays with size 4 to the cluster with size 4. Another way is to locate two arrays with size 4 to the one cluster with size 8.
In the second example you are given 10 memory clusters with size 1 and 6 arrays with size 1. You can choose any 6 clusters and locate all given arrays to them. | [
{
"input": "5 3\n8 4 3 2 2\n3 2 2",
"output": "2"
},
{
"input": "10 6\n1 1 1 1 1 1 1 1 1 1\n0 0 0 0 0 0",
"output": "6"
},
{
"input": "5 10\n4 4 3 3 3\n0 0 0 0 0 2 0 2 0 0",
"output": "10"
},
{
"input": "5 10\n3 4 5 4 3\n1 2 1 0 1 2 1 0 2 0",
"output": "9"
},
{
"i... | 92 | 0 | 0 | 11,445 | |
463 | Caisa and Tree | [
"brute force",
"dfs and similar",
"math",
"number theory",
"trees"
] | null | null | Caisa is now at home and his son has a simple task for him.
Given a rooted tree with *n* vertices, numbered from 1 to *n* (vertex 1 is the root). Each vertex of the tree has a value. You should answer *q* queries. Each query is one of the following:
- Format of the query is "1 *v*". Let's write out the sequence of vertices along the path from the root to vertex *v*: *u*1,<=*u*2,<=...,<=*u**k* (*u*1<==<=1; *u**k*<==<=*v*). You need to output such a vertex *u**i* that *gcd*(*value* *of* *u**i*,<=*value* *of* *v*)<=><=1 and *i*<=<<=*k*. If there are several possible vertices *u**i* pick the one with maximum value of *i*. If there is no such vertex output -1. - Format of the query is "2 *v* *w*". You must change the value of vertex *v* to *w*.
You are given all the queries, help Caisa to solve the problem. | The first line contains two space-separated integers *n*, *q* (1<=≤<=*n*,<=*q*<=≤<=105).
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=2·106), where *a**i* represent the value of node *i*.
Each of the next *n*<=-<=1 lines contains two integers *x**i* and *y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*; *x**i*<=≠<=*y**i*), denoting the edge of the tree between vertices *x**i* and *y**i*.
Each of the next *q* lines contains a query in the format that is given above. For each query the following inequalities hold: 1<=≤<=*v*<=≤<=*n* and 1<=≤<=*w*<=≤<=2·106. Note that: there are no more than 50 queries that changes the value of a vertex. | For each query of the first type output the result of the query. | [
"4 6\n10 8 4 3\n1 2\n2 3\n3 4\n1 1\n1 2\n1 3\n1 4\n2 1 9\n1 4\n"
] | [
"-1\n1\n2\n-1\n1\n"
] | *gcd*(*x*, *y*) is greatest common divisor of two integers *x* and *y*. | [
{
"input": "4 6\n10 8 4 3\n1 2\n2 3\n3 4\n1 1\n1 2\n1 3\n1 4\n2 1 9\n1 4",
"output": "-1\n1\n2\n-1\n1"
},
{
"input": "4 46\n1826622 227609 1815373 691816\n4 1\n3 1\n1 2\n1 2\n1 3\n2 3 1226461\n1 2\n2 2 329857\n1 4\n1 2\n1 2\n1 3\n1 3\n1 1\n1 4\n1 1\n1 3\n1 3\n1 2\n1 4\n1 4\n1 3\n1 2\n1 2\n2 3 106992... | 1,777 | 123,289,600 | 0 | 11,451 | |
250 | Paper Work | [
"greedy"
] | null | null | Polycarpus has been working in the analytic department of the "F.R.A.U.D." company for as much as *n* days. Right now his task is to make a series of reports about the company's performance for the last *n* days. We know that the main information in a day report is value *a**i*, the company's profit on the *i*-th day. If *a**i* is negative, then the company suffered losses on the *i*-th day.
Polycarpus should sort the daily reports into folders. Each folder should include data on the company's performance for several consecutive days. Of course, the information on each of the *n* days should be exactly in one folder. Thus, Polycarpus puts information on the first few days in the first folder. The information on the several following days goes to the second folder, and so on.
It is known that the boss reads one daily report folder per day. If one folder has three or more reports for the days in which the company suffered losses (*a**i*<=<<=0), he loses his temper and his wrath is terrible.
Therefore, Polycarpus wants to prepare the folders so that none of them contains information on three or more days with the loss, and the number of folders is minimal.
Write a program that, given sequence *a**i*, will print the minimum number of folders. | The first line contains integer *n* (1<=≤<=*n*<=≤<=100), *n* is the number of days. The second line contains a sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=≤<=100), where *a**i* means the company profit on the *i*-th day. It is possible that the company has no days with the negative *a**i*. | Print an integer *k* — the required minimum number of folders. In the second line print a sequence of integers *b*1, *b*2, ..., *b**k*, where *b**j* is the number of day reports in the *j*-th folder.
If there are multiple ways to sort the reports into *k* days, print any of them. | [
"11\n1 2 3 -4 -5 -6 5 -5 -6 -7 6\n",
"5\n0 -1 100 -1 0\n"
] | [
"3\n5 3 3 ",
"1\n5 "
] | Here goes a way to sort the reports from the first sample into three folders:
In the second sample you can put all five reports in one folder. | [
{
"input": "11\n1 2 3 -4 -5 -6 5 -5 -6 -7 6",
"output": "3\n5 3 3 "
},
{
"input": "5\n0 -1 100 -1 0",
"output": "1\n5 "
},
{
"input": "1\n0",
"output": "1\n1 "
},
{
"input": "1\n-1",
"output": "1\n1 "
},
{
"input": "2\n0 0",
"output": "1\n2 "
},
{
"inp... | 122 | 6,963,200 | 0 | 11,454 | |
489 | Special Matrices | [
"combinatorics",
"dp"
] | null | null | An *n*<=×<=*n* square matrix is special, if:
- it is binary, that is, each cell contains either a 0, or a 1; - the number of ones in each row and column equals 2.
You are given *n* and the first *m* rows of the matrix. Print the number of special *n*<=×<=*n* matrices, such that the first *m* rows coincide with the given ones.
As the required value can be rather large, print the remainder after dividing the value by the given number *mod*. | The first line of the input contains three integers *n*, *m*, *mod* (2<=≤<=*n*<=≤<=500, 0<=≤<=*m*<=≤<=*n*, 2<=≤<=*mod*<=≤<=109). Then *m* lines follow, each of them contains *n* characters — the first rows of the required special matrices. Each of these lines contains exactly two characters '1', the rest characters are '0'. Each column of the given *m*<=×<=*n* table contains at most two numbers one. | Print the remainder after dividing the required value by number *mod*. | [
"3 1 1000\n011\n",
"4 4 100500\n0110\n1010\n0101\n1001\n"
] | [
"2\n",
"1\n"
] | For the first test the required matrices are:
In the second test the required matrix is already fully given, so the answer is 1. | [
{
"input": "3 1 1000\n011",
"output": "2"
},
{
"input": "4 4 100500\n0110\n1010\n0101\n1001",
"output": "1"
},
{
"input": "2 0 1000",
"output": "1"
},
{
"input": "2 1 1000\n11",
"output": "1"
},
{
"input": "5 0 13",
"output": "12"
},
{
"input": "5 3 19... | 171 | 18,227,200 | 3 | 11,468 | |
176 | Word Cut | [
"dp"
] | null | null | Let's consider one interesting word game. In this game you should transform one word into another through special operations.
Let's say we have word *w*, let's split this word into two non-empty parts *x* and *y* so, that *w*<==<=*xy*. A split operation is transforming word *w*<==<=*xy* into word *u*<==<=*yx*. For example, a split operation can transform word "wordcut" into word "cutword".
You are given two words *start* and *end*. Count in how many ways we can transform word *start* into word *end*, if we apply exactly *k* split operations consecutively to word *start*.
Two ways are considered different if the sequences of applied operations differ. Two operation sequences are different if exists such number *i* (1<=≤<=*i*<=≤<=*k*), that in the *i*-th operation of the first sequence the word splits into parts *x* and *y*, in the *i*-th operation of the second sequence the word splits into parts *a* and *b*, and additionally *x*<=≠<=*a* holds. | The first line contains a non-empty word *start*, the second line contains a non-empty word *end*. The words consist of lowercase Latin letters. The number of letters in word *start* equals the number of letters in word *end* and is at least 2 and doesn't exceed 1000 letters.
The third line contains integer *k* (0<=≤<=*k*<=≤<=105) — the required number of operations. | Print a single number — the answer to the problem. As this number can be rather large, print it modulo 1000000007 (109<=+<=7). | [
"ab\nab\n2\n",
"ababab\nababab\n1\n",
"ab\nba\n2\n"
] | [
"1\n",
"2\n",
"0\n"
] | The sought way in the first sample is:
ab → a|b → ba → b|a → ab
In the second sample the two sought ways are:
- ababab → abab|ab → ababab - ababab → ab|abab → ababab | [
{
"input": "ab\nab\n2",
"output": "1"
},
{
"input": "ababab\nababab\n1",
"output": "2"
},
{
"input": "ab\nba\n2",
"output": "0"
},
{
"input": "aaa\naaa\n0",
"output": "1"
},
{
"input": "hi\nhi\n1",
"output": "0"
},
{
"input": "abcd\ncbad\n5",
"outp... | 186 | 2,252,800 | 3 | 11,519 | |
52 | Circular RMQ | [
"data structures"
] | C. Circular RMQ | 1 | 256 | You are given circular array *a*0,<=*a*1,<=...,<=*a**n*<=-<=1. There are two types of operations with it:
- *inc*(*lf*,<=*rg*,<=*v*) — this operation increases each element on the segment [*lf*,<=*rg*] (inclusively) by *v*; - *rmq*(*lf*,<=*rg*) — this operation returns minimal value on the segment [*lf*,<=*rg*] (inclusively).
Assume segments to be circular, so if *n*<==<=5 and *lf*<==<=3,<=*rg*<==<=1, it means the index sequence: 3,<=4,<=0,<=1.
Write program to process given sequence of operations. | The first line contains integer *n* (1<=≤<=*n*<=≤<=200000). The next line contains initial state of the array: *a*0,<=*a*1,<=...,<=*a**n*<=-<=1 (<=-<=106<=≤<=*a**i*<=≤<=106), *a**i* are integer. The third line contains integer *m* (0<=≤<=*m*<=≤<=200000), *m* — the number of operartons. Next *m* lines contain one operation each. If line contains two integer *lf*,<=*rg* (0<=≤<=*lf*,<=*rg*<=≤<=*n*<=-<=1) it means *rmq* operation, it contains three integers *lf*,<=*rg*,<=*v* (0<=≤<=*lf*,<=*rg*<=≤<=*n*<=-<=1;<=-<=106<=≤<=*v*<=≤<=106) — *inc* operation. | For each *rmq* operation write result for it. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). | [
"4\n1 2 3 4\n4\n3 0\n3 0 -1\n0 1\n2 1\n"
] | [
"1\n0\n0\n"
] | none | [
{
"input": "4\n1 2 3 4\n4\n3 0\n3 0 -1\n0 1\n2 1",
"output": "1\n0\n0"
},
{
"input": "1\n-1\n10\n0 0 -1\n0 0\n0 0 1\n0 0\n0 0 1\n0 0\n0 0 0\n0 0\n0 0 -1\n0 0 1",
"output": "-2\n-1\n0\n0"
},
{
"input": "2\n-1 -1\n10\n0 0\n0 0\n0 0 1\n0 0\n1 1\n0 0 -1\n0 0 0\n0 0 1\n1 1 0\n0 0 -1",
"ou... | 826 | 36,044,800 | 3.519861 | 11,585 |
51 | Three Base Stations | [
"binary search",
"greedy"
] | C. Three Base Stations | 1 | 256 | The New Vasjuki village is stretched along the motorway and that's why every house on it is characterized by its shift relative to some fixed point — the *x**i* coordinate. The village consists of *n* houses, the *i*-th house is located in the point with coordinates of *x**i*.
TELE3, a cellular communication provider planned to locate three base stations so as to provide every house in the village with cellular communication. The base station having power *d* located in the point *t* provides with communication all the houses on the segment [*t*<=-<=*d*,<=*t*<=+<=*d*] (including boundaries).
To simplify the integration (and simply not to mix anything up) all the three stations are planned to possess the equal power of *d*. Which minimal value of *d* is enough to provide all the houses in the village with cellular communication. | The first line contains an integer *n* (1<=≤<=*n*<=≤<=2·105) which represents the number of houses in the village. The second line contains the coordinates of houses — the sequence *x*1,<=*x*2,<=...,<=*x**n* of integer numbers (1<=≤<=*x**i*<=≤<=109). It is possible that two or more houses are located on one point. The coordinates are given in a arbitrary order. | Print the required minimal power *d*. In the second line print three numbers — the possible coordinates of the base stations' location. Print the coordinates with 6 digits after the decimal point. The positions of the stations can be any from 0 to 2·109 inclusively. It is accepted for the base stations to have matching coordinates. If there are many solutions, print any of them. | [
"4\n1 2 3 4\n",
"3\n10 20 30\n",
"5\n10003 10004 10001 10002 1\n"
] | [
"0.500000\n1.500000 2.500000 3.500000\n",
"0\n10.000000 20.000000 30.000000\n",
"0.500000\n1.000000 10001.500000 10003.500000\n"
] | none | [
{
"input": "4\n1 2 3 4",
"output": "0.500000\n1.500000 2.500000 3.500000"
},
{
"input": "3\n10 20 30",
"output": "0\n10.000000 20.000000 30.000000"
},
{
"input": "5\n10003 10004 10001 10002 1",
"output": "0.500000\n1.000000 10001.500000 10003.500000"
},
{
"input": "1\n1",
... | 2,000 | 16,486,400 | 0 | 11,602 |
575 | Bulbo | [
"dp",
"greedy"
] | null | null | Bananistan is a beautiful banana republic. Beautiful women in beautiful dresses. Beautiful statues of beautiful warlords. Beautiful stars in beautiful nights.
In Bananistan people play this crazy game – Bulbo. There’s an array of bulbs and player at the position, which represents one of the bulbs. The distance between two neighboring bulbs is 1. Before each turn player can change his position with cost |*pos**new*<=-<=*pos**old*|. After that, a contiguous set of bulbs lights-up and player pays the cost that’s equal to the distance to the closest shining bulb. Then, all bulbs go dark again. The goal is to minimize your summed cost. I tell you, Bananistanians are spending their nights playing with bulbs.
Banana day is approaching, and you are hired to play the most beautiful Bulbo game ever. A huge array of bulbs is installed, and you know your initial position and all the light-ups in advance. You need to play the ideal game and impress Bananistanians, and their families. | The first line contains number of turns *n* and initial position *x*. Next *n* lines contain two numbers *l**start* and *l**end*, which represent that all bulbs from interval [*l**start*,<=*l**end*] are shining this turn.
- 1<=≤<=*n*<=≤<=5000 - 1<=≤<=*x*<=≤<=109 - 1<=≤<=*l**start*<=≤<=*l**end*<=≤<=109 | Output should contain a single number which represents the best result (minimum cost) that could be obtained by playing this Bulbo game. | [
"5 4\n2 7\n9 16\n8 10\n9 17\n1 6\n"
] | [
"8\n"
] | Before 1. turn move to position 5
Before 2. turn move to position 9
Before 5. turn move to position 8 | [] | 46 | 0 | 0 | 11,649 | |
372 | Drawing Circles is Fun | [
"combinatorics",
"geometry"
] | null | null | There are a set of points *S* on the plane. This set doesn't contain the origin *O*(0,<=0), and for each two distinct points in the set *A* and *B*, the triangle *OAB* has strictly positive area.
Consider a set of pairs of points (*P*1,<=*P*2),<=(*P*3,<=*P*4),<=...,<=(*P*2*k*<=-<=1,<=*P*2*k*). We'll call the set good if and only if:
- *k*<=≥<=2. - All *P**i* are distinct, and each *P**i* is an element of *S*. - For any two pairs (*P*2*i*<=-<=1,<=*P*2*i*) and (*P*2*j*<=-<=1,<=*P*2*j*), the circumcircles of triangles *OP*2*i*<=-<=1*P*2*j*<=-<=1 and *OP*2*i**P*2*j* have a single common point, and the circumcircle of triangles *OP*2*i*<=-<=1*P*2*j* and *OP*2*i**P*2*j*<=-<=1 have a single common point.
Calculate the number of good sets of pairs modulo 1000000007 (109<=+<=7). | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=1000) — the number of points in *S*. Each of the next *n* lines contains four integers *a**i*,<=*b**i*,<=*c**i*,<=*d**i* (0<=≤<=|*a**i*|,<=|*c**i*|<=≤<=50; 1<=≤<=*b**i*,<=*d**i*<=≤<=50; (*a**i*,<=*c**i*)<=≠<=(0,<=0)). These integers represent a point .
No two points coincide. | Print a single integer — the answer to the problem modulo 1000000007 (109<=+<=7). | [
"10\n-46 46 0 36\n0 20 -24 48\n-50 50 -49 49\n-20 50 8 40\n-15 30 14 28\n4 10 -4 5\n6 15 8 10\n-20 50 -3 15\n4 34 -16 34\n16 34 2 17\n",
"10\n30 30 -26 26\n0 15 -36 36\n-28 28 -34 34\n10 10 0 4\n-8 20 40 50\n9 45 12 30\n6 15 7 35\n36 45 -8 20\n-16 34 -4 34\n4 34 8 17\n",
"10\n0 20 38 38\n-30 30 -13 13\n-11 11 1... | [
"2\n",
"4\n",
"10\n"
] | none | [] | 0 | 0 | -1 | 11,652 | |
1,006 | Xor-Paths | [
"bitmasks",
"brute force",
"dp",
"meet-in-the-middle"
] | null | null | There is a rectangular grid of size $n \times m$. Each cell has a number written on it; the number on the cell ($i, j$) is $a_{i, j}$. Your task is to calculate the number of paths from the upper-left cell ($1, 1$) to the bottom-right cell ($n, m$) meeting the following constraints:
- You can move to the right or to the bottom only. Formally, from the cell ($i, j$) you may move to the cell ($i, j + 1$) or to the cell ($i + 1, j$). The target cell can't be outside of the grid. - The xor of all the numbers on the path from the cell ($1, 1$) to the cell ($n, m$) must be equal to $k$ (xor operation is the bitwise exclusive OR, it is represented as '^' in Java or C++ and "xor" in Pascal).
Find the number of such paths in the given grid. | The first line of the input contains three integers $n$, $m$ and $k$ ($1 \le n, m \le 20$, $0 \le k \le 10^{18}$) — the height and the width of the grid, and the number $k$.
The next $n$ lines contain $m$ integers each, the $j$-th element in the $i$-th line is $a_{i, j}$ ($0 \le a_{i, j} \le 10^{18}$). | Print one integer — the number of paths from ($1, 1$) to ($n, m$) with xor sum equal to $k$. | [
"3 3 11\n2 1 5\n7 10 0\n12 6 4\n",
"3 4 2\n1 3 3 3\n0 3 3 2\n3 0 1 1\n",
"3 4 1000000000000000000\n1 3 3 3\n0 3 3 2\n3 0 1 1\n"
] | [
"3\n",
"5\n",
"0\n"
] | All the paths from the first example:
- $(1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3)$; - $(1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3)$; - $(1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3)$.
All the paths from the second example:
- $(1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3) \rightarrow (3, 4)$; - $(1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (3, 2) \rightarrow (3, 3) \rightarrow (3, 4)$; - $(1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (3, 4)$; - $(1, 1) \rightarrow (1, 2) \rightarrow (2, 2) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4)$; - $(1, 1) \rightarrow (1, 2) \rightarrow (1, 3) \rightarrow (2, 3) \rightarrow (3, 3) \rightarrow (3, 4)$. | [
{
"input": "3 3 11\n2 1 5\n7 10 0\n12 6 4",
"output": "3"
},
{
"input": "3 4 2\n1 3 3 3\n0 3 3 2\n3 0 1 1",
"output": "5"
},
{
"input": "3 4 1000000000000000000\n1 3 3 3\n0 3 3 2\n3 0 1 1",
"output": "0"
},
{
"input": "1 1 1000000000000000000\n1000000000000000000",
"outpu... | 77 | 204,800 | -1 | 11,658 | |
11 | Jumping Jack | [
"math"
] | B. Jumping Jack | 1 | 64 | Jack is working on his jumping skills recently. Currently he's located at point zero of the number line. He would like to get to the point *x*. In order to train, he has decided that he'll first jump by only one unit, and each subsequent jump will be exactly one longer than the previous one. He can go either left or right with each jump. He wonders how many jumps he needs to reach *x*. | The input data consists of only one integer *x* (<=-<=109<=≤<=*x*<=≤<=109). | Output the minimal number of jumps that Jack requires to reach *x*. | [
"2\n",
"6\n",
"0\n"
] | [
"3\n",
"3\n",
"0\n"
] | none | [
{
"input": "2",
"output": "3"
},
{
"input": "6",
"output": "3"
},
{
"input": "0",
"output": "0"
},
{
"input": "-1000000000",
"output": "44723"
},
{
"input": "999961560",
"output": "44720"
},
{
"input": "999961561",
"output": "44721"
},
{
"i... | 122 | 30,310,400 | 3.71317 | 11,689 |
954 | String Typing | [
"implementation",
"strings"
] | null | null | You are given a string *s* consisting of *n* lowercase Latin letters. You have to type this string using your keyboard.
Initially, you have an empty string. Until you type the whole string, you may perform the following operation:
- add a character to the end of the string.
Besides, at most once you may perform one additional operation: copy the string and append it to itself.
For example, if you have to type string abcabca, you can type it in 7 operations if you type all the characters one by one. However, you can type it in 5 operations if you type the string abc first and then copy it and type the last character.
If you have to type string aaaaaaaaa, the best option is to type 4 characters one by one, then copy the string, and then type the remaining character.
Print the minimum number of operations you need to type the given string. | The first line of the input containing only one integer number *n* (1<=≤<=*n*<=≤<=100) — the length of the string you have to type. The second line containing the string *s* consisting of *n* lowercase Latin letters. | Print one integer number — the minimum number of operations you need to type the given string. | [
"7\nabcabca\n",
"8\nabcdefgh\n"
] | [
"5\n",
"8\n"
] | The first test described in the problem statement.
In the second test you can only type all the characters one by one. | [
{
"input": "7\nabcabca",
"output": "5"
},
{
"input": "8\nabcdefgh",
"output": "8"
},
{
"input": "100\nmhnzadklojbuumkrxjayikjhwuxihgkinllackcavhjpxlydxcmhnzadklojbuumkrxjayikjhwuxihgkinllackcavhjpxlydxc",
"output": "51"
},
{
"input": "99\ntrolnjmzxxrfxuexcqpjvefndwuxwsukxwmjh... | 61 | 1,536,000 | 3 | 11,698 | |
794 | Cutting Carrot | [
"geometry",
"math"
] | null | null | Igor the analyst has adopted *n* little bunnies. As we all know, bunnies love carrots. Thus, Igor has bought a carrot to be shared between his bunnies. Igor wants to treat all the bunnies equally, and thus he wants to cut the carrot into *n* pieces of equal area.
Formally, the carrot can be viewed as an isosceles triangle with base length equal to 1 and height equal to *h*. Igor wants to make *n*<=-<=1 cuts parallel to the base to cut the carrot into *n* pieces. He wants to make sure that all *n* pieces have the same area. Can you help Igor determine where to cut the carrot so that each piece have equal area? | The first and only line of input contains two space-separated integers, *n* and *h* (2<=≤<=*n*<=≤<=1000, 1<=≤<=*h*<=≤<=105). | The output should contain *n*<=-<=1 real numbers *x*1,<=*x*2,<=...,<=*x**n*<=-<=1. The number *x**i* denotes that the *i*-th cut must be made *x**i* units away from the apex of the carrot. In addition, 0<=<<=*x*1<=<<=*x*2<=<<=...<=<<=*x**n*<=-<=1<=<<=*h* must hold.
Your output will be considered correct if absolute or relative error of every number in your output doesn't exceed 10<=-<=6.
Formally, let your answer be *a*, and the jury's answer be *b*. Your answer is considered correct if . | [
"3 2\n",
"2 100000\n"
] | [
"1.154700538379 1.632993161855\n",
"70710.678118654752\n"
] | Definition of isosceles triangle: [https://en.wikipedia.org/wiki/Isosceles_triangle](https://en.wikipedia.org/wiki/Isosceles_triangle). | [
{
"input": "3 2",
"output": "1.154700538379 1.632993161855"
},
{
"input": "2 100000",
"output": "70710.678118654752"
},
{
"input": "1000 100000",
"output": "3162.277660168379 4472.135954999579 5477.225575051661 6324.555320336759 7071.067811865475 7745.966692414834 8366.600265340755 8... | 140 | 1,843,200 | 3 | 11,702 | |
631 | Messenger | [
"data structures",
"hashing",
"implementation",
"string suffix structures",
"strings"
] | null | null | Each employee of the "Blake Techologies" company uses a special messaging app "Blake Messenger". All the stuff likes this app and uses it constantly. However, some important futures are missing. For example, many users want to be able to search through the message history. It was already announced that the new feature will appear in the nearest update, when developers faced some troubles that only you may help them to solve.
All the messages are represented as a strings consisting of only lowercase English letters. In order to reduce the network load strings are represented in the special compressed form. Compression algorithm works as follows: string is represented as a concatenation of *n* blocks, each block containing only equal characters. One block may be described as a pair (*l**i*,<=*c**i*), where *l**i* is the length of the *i*-th block and *c**i* is the corresponding letter. Thus, the string *s* may be written as the sequence of pairs .
Your task is to write the program, that given two compressed string *t* and *s* finds all occurrences of *s* in *t*. Developers know that there may be many such occurrences, so they only ask you to find the number of them. Note that *p* is the starting position of some occurrence of *s* in *t* if and only if *t**p**t**p*<=+<=1...*t**p*<=+<=|*s*|<=-<=1<==<=*s*, where *t**i* is the *i*-th character of string *t*.
Note that the way to represent the string in compressed form may not be unique. For example string "aaaa" may be given as , , ... | The first line of the input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=200<=000) — the number of blocks in the strings *t* and *s*, respectively.
The second line contains the descriptions of *n* parts of string *t* in the format "*l**i*-*c**i*" (1<=≤<=*l**i*<=≤<=1<=000<=000) — the length of the *i*-th part and the corresponding lowercase English letter.
The second line contains the descriptions of *m* parts of string *s* in the format "*l**i*-*c**i*" (1<=≤<=*l**i*<=≤<=1<=000<=000) — the length of the *i*-th part and the corresponding lowercase English letter. | Print a single integer — the number of occurrences of *s* in *t*. | [
"5 3\n3-a 2-b 4-c 3-a 2-c\n2-a 2-b 1-c\n",
"6 1\n3-a 6-b 7-a 4-c 8-e 2-a\n3-a\n",
"5 5\n1-h 1-e 1-l 1-l 1-o\n1-w 1-o 1-r 1-l 1-d\n"
] | [
"1",
"6",
"0"
] | In the first sample, *t* = "aaabbccccaaacc", and string *s* = "aabbc". The only occurrence of string *s* in string *t* starts at position *p* = 2.
In the second sample, *t* = "aaabbbbbbaaaaaaacccceeeeeeeeaa", and *s* = "aaa". The occurrences of *s* in *t* start at positions *p* = 1, *p* = 10, *p* = 11, *p* = 12, *p* = 13 and *p* = 14. | [
{
"input": "5 3\n3-a 2-b 4-c 3-a 2-c\n2-a 2-b 1-c",
"output": "1"
},
{
"input": "6 1\n3-a 6-b 7-a 4-c 8-e 2-a\n3-a",
"output": "6"
},
{
"input": "5 5\n1-h 1-e 1-l 1-l 1-o\n1-w 1-o 1-r 1-l 1-d",
"output": "0"
},
{
"input": "9 3\n1-h 1-e 2-l 1-o 1-w 1-o 1-r 1-l 1-d\n2-l 1-o 1-w... | 2,000 | 8,908,800 | 0 | 11,710 | |
358 | Dima and Containers | [
"constructive algorithms",
"greedy",
"implementation"
] | null | null | Dima has a birthday soon! It's a big day! Saryozha's present to Dima is that Seryozha won't be in the room and won't disturb Dima and Inna as they celebrate the birthday. Inna's present to Dima is a stack, a queue and a deck.
Inna wants her present to show Dima how great a programmer he is. For that, she is going to give Dima commands one by one. There are two types of commands:
1. Add a given number into one of containers. For the queue and the stack, you can add elements only to the end. For the deck, you can add elements to the beginning and to the end. 1. Extract a number from each of at most three distinct containers. Tell all extracted numbers to Inna and then empty all containers. In the queue container you can extract numbers only from the beginning. In the stack container you can extract numbers only from the end. In the deck number you can extract numbers from the beginning and from the end. You cannot extract numbers from empty containers.
Every time Dima makes a command of the second type, Inna kisses Dima some (possibly zero) number of times. Dima knows Inna perfectly well, he is sure that this number equals the sum of numbers he extracts from containers during this operation.
As we've said before, Dima knows Inna perfectly well and he knows which commands Inna will give to Dima and the order of the commands. Help Dima find the strategy that lets him give as more kisses as possible for his birthday! | The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of Inna's commands. Then *n* lines follow, describing Inna's commands. Each line consists an integer:
1. Integer *a* (1<=≤<=*a*<=≤<=105) means that Inna gives Dima a command to add number *a* into one of containers. 1. Integer 0 shows that Inna asks Dima to make at most three extractions from different containers. | Each command of the input must correspond to one line of the output — Dima's action.
For the command of the first type (adding) print one word that corresponds to Dima's choice:
- pushStack — add to the end of the stack; - pushQueue — add to the end of the queue; - pushFront — add to the beginning of the deck; - pushBack — add to the end of the deck.
For a command of the second type first print an integer *k* (0<=≤<=*k*<=≤<=3), that shows the number of extract operations, then print *k* words separated by space. The words can be:
- popStack — extract from the end of the stack; - popQueue — extract from the beginning of the line; - popFront — extract from the beginning from the deck; - popBack — extract from the end of the deck.
The printed operations mustn't extract numbers from empty containers. Also, they must extract numbers from distinct containers.
The printed sequence of actions must lead to the maximum number of kisses. If there are multiple sequences of actions leading to the maximum number of kisses, you are allowed to print any of them. | [
"10\n0\n1\n0\n1\n2\n0\n1\n2\n3\n0\n",
"4\n1\n2\n3\n0\n"
] | [
"0\npushStack\n1 popStack\npushStack\npushQueue\n2 popStack popQueue\npushStack\npushQueue\npushFront\n3 popStack popQueue popFront\n",
"pushStack\npushQueue\npushFront\n3 popStack popQueue popFront\n"
] | none | [
{
"input": "10\n0\n1\n0\n1\n2\n0\n1\n2\n3\n0",
"output": "0\npushStack\n1 popStack\npushStack\npushQueue\n2 popStack popQueue\npushStack\npushQueue\npushFront\n3 popStack popQueue popFront"
},
{
"input": "4\n1\n2\n3\n0",
"output": "pushStack\npushQueue\npushFront\n3 popStack popQueue popFront"
... | 31 | 204,800 | 0 | 11,732 | |
0 | none | [
"none"
] | null | null | Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are *n* flights that must depart today, the *i*-th of them is planned to depart at the *i*-th minute of the day.
Metropolis airport is the main transport hub of Metropolia, so it is difficult to keep the schedule intact. This is exactly the case today: because of technical issues, no flights were able to depart during the first *k* minutes of the day, so now the new departure schedule must be created.
All *n* scheduled flights must now depart at different minutes between (*k*<=+<=1)-th and (*k*<=+<=*n*)-th, inclusive. However, it's not mandatory for the flights to depart in the same order they were initially scheduled to do so — their order in the new schedule can be different. There is only one restriction: no flight is allowed to depart earlier than it was supposed to depart in the initial schedule.
Helen knows that each minute of delay of the *i*-th flight costs airport *c**i* burles. Help her find the order for flights to depart in the new schedule that minimizes the total cost for the airport. | The first line contains two integers *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=300<=000), here *n* is the number of flights, and *k* is the number of minutes in the beginning of the day that the flights did not depart.
The second line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (1<=≤<=*c**i*<=≤<=107), here *c**i* is the cost of delaying the *i*-th flight for one minute. | The first line must contain the minimum possible total cost of delaying the flights.
The second line must contain *n* different integers *t*1,<=*t*2,<=...,<=*t**n* (*k*<=+<=1<=≤<=*t**i*<=≤<=*k*<=+<=*n*), here *t**i* is the minute when the *i*-th flight must depart. If there are several optimal schedules, print any of them. | [
"5 2\n4 2 1 10 2\n"
] | [
"20\n3 6 7 4 5 \n"
] | Let us consider sample test. If Helen just moves all flights 2 minutes later preserving the order, the total cost of delaying the flights would be (3 - 1)·4 + (4 - 2)·2 + (5 - 3)·1 + (6 - 4)·10 + (7 - 5)·2 = 38 burles.
However, the better schedule is shown in the sample answer, its cost is (3 - 1)·4 + (6 - 2)·2 + (7 - 3)·1 + (4 - 4)·10 + (5 - 5)·2 = 20 burles. | [
{
"input": "5 2\n4 2 1 10 2",
"output": "20\n3 6 7 4 5 "
},
{
"input": "3 2\n3 1 2",
"output": "11\n3 5 4 "
},
{
"input": "5 5\n5 5 9 100 3",
"output": "321\n9 8 7 6 10 "
},
{
"input": "1 1\n1",
"output": "1\n2 "
},
{
"input": "1 1\n10000000",
"output": "10000... | 46 | 0 | 0 | 11,738 | |
301 | Yaroslav and Algorithm | [
"constructive algorithms"
] | null | null | Yaroslav likes algorithms. We'll describe one of his favorite algorithms.
1. The algorithm receives a string as the input. We denote this input string as *a*. 1. The algorithm consists of some number of command. Сommand number *i* looks either as *s**i* >> *w**i*, or as *s**i* <> *w**i*, where *s**i* and *w**i* are some possibly empty strings of length at most 7, consisting of digits and characters "?". 1. At each iteration, the algorithm looks for a command with the minimum index *i*, such that *s**i* occurs in *a* as a substring. If this command is not found the algorithm terminates. 1. Let's denote the number of the found command as *k*. In string *a* the first occurrence of the string *s**k* is replaced by string *w**k*. If the found command at that had form *s**k* >> *w**k*, then the algorithm continues its execution and proceeds to the next iteration. Otherwise, the algorithm terminates. 1. The value of string *a* after algorithm termination is considered to be the output of the algorithm.
Yaroslav has a set of *n* positive integers, he needs to come up with his favorite algorithm that will increase each of the given numbers by one. More formally, if we consider each number as a string representing the decimal representation of the number, then being run on each of these strings separately, the algorithm should receive the output string that is a recording of the corresponding number increased by one.
Help Yaroslav. | The first line contains integer *n* (1<=≤<=*n*<=≤<=100) — the number of elements in the set. The next *n* lines contains one positive integer each. All the given numbers are less than 1025. | Print the algorithm which can individually increase each number of the set. In the *i*-th line print the command number *i* without spaces.
Your algorithm will be launched for each of these numbers. The answer will be considered correct if:
- Each line will a correct algorithm command (see the description in the problem statement). - The number of commands should not exceed 50. - The algorithm will increase each of the given numbers by one. - To get a respond, the algorithm will perform no more than 200 iterations for each number. | [
"2\n10\n79\n"
] | [
"10<>11\n79<>80\n"
] | none | [
{
"input": "2\n10\n79",
"output": "10<>11\n79<>80"
},
{
"input": "5\n9\n99\n999\n9999\n99999",
"output": "0??<>1\n1??<>2\n2??<>3\n3??<>4\n4??<>5\n5??<>6\n6??<>7\n7??<>8\n8??<>9\n9??>>??0\n??<>1\n?0>>0?\n?1>>1?\n?2>>2?\n?3>>3?\n?4>>4?\n?5>>5?\n?6>>6?\n?7>>7?\n?8>>8?\n?9>>9?\n?>>??\n>>?"
},
{
... | 248 | 0 | 3 | 11,741 | |
638 | Making Genome in Berland | [
"*special",
"dfs and similar",
"strings"
] | null | null | Berland scientists face a very important task - given the parts of short DNA fragments, restore the dinosaur DNA! The genome of a berland dinosaur has noting in common with the genome that we've used to: it can have 26 distinct nucleotide types, a nucleotide of each type can occur at most once. If we assign distinct English letters to all nucleotides, then the genome of a Berland dinosaur will represent a non-empty string consisting of small English letters, such that each letter occurs in it at most once.
Scientists have *n* genome fragments that are represented as substrings (non-empty sequences of consecutive nucleotides) of the sought genome.
You face the following problem: help scientists restore the dinosaur genome. It is guaranteed that the input is not contradictory and at least one suitable line always exists. When the scientists found out that you are a strong programmer, they asked you in addition to choose the one with the minimum length. If there are multiple such strings, choose any string. | The first line of the input contains a positive integer *n* (1<=≤<=*n*<=≤<=100) — the number of genome fragments.
Each of the next lines contains one descriptions of a fragment. Each fragment is a non-empty string consisting of distinct small letters of the English alphabet. It is not guaranteed that the given fragments are distinct. Fragments could arbitrarily overlap and one fragment could be a substring of another one.
It is guaranteed that there is such string of distinct letters that contains all the given fragments as substrings. | In the single line of the output print the genome of the minimum length that contains all the given parts. All the nucleotides in the genome must be distinct. If there are multiple suitable strings, print the string of the minimum length. If there also are multiple suitable strings, you can print any of them. | [
"3\nbcd\nab\ncdef\n",
"4\nx\ny\nz\nw\n"
] | [
"abcdef\n",
"xyzw\n"
] | none | [
{
"input": "3\nbcd\nab\ncdef",
"output": "abcdef"
},
{
"input": "4\nx\ny\nz\nw",
"output": "xyzw"
},
{
"input": "25\nef\nfg\ngh\nhi\nij\njk\nkl\nlm\nmn\nno\nab\nbc\ncd\nde\nop\npq\nqr\nrs\nst\ntu\nuv\nvw\nwx\nxy\nyz",
"output": "abcdefghijklmnopqrstuvwxyz"
},
{
"input": "1\nf... | 62 | 4,608,000 | 0 | 11,745 | |
665 | Four Divisors | [
"data structures",
"dp",
"math",
"number theory",
"sortings",
"two pointers"
] | null | null | If an integer *a* is divisible by another integer *b*, then *b* is called the divisor of *a*.
For example: 12 has positive 6 divisors. They are 1, 2, 3, 4, 6 and 12.
Let’s define a function *D*(*n*) — number of integers between 1 and *n* (inclusive) which has exactly four positive divisors.
Between 1 and 10 only the integers 6, 8 and 10 has exactly four positive divisors. So, *D*(10)<==<=3.
You are given an integer *n*. You have to calculate *D*(*n*). | The only line contains integer *n* (1<=≤<=*n*<=≤<=1011) — the parameter from the problem statement. | Print the only integer *c* — the number of integers between 1 and *n* with exactly four divisors. | [
"10\n",
"20\n"
] | [
"3\n",
"5\n"
] | none | [
{
"input": "10",
"output": "3"
},
{
"input": "20",
"output": "5"
},
{
"input": "1",
"output": "0"
},
{
"input": "27",
"output": "9"
},
{
"input": "100",
"output": "32"
},
{
"input": "1000",
"output": "292"
},
{
"input": "10000",
"output... | 4,726 | 13,516,800 | 3 | 11,757 | |
990 | Post Lamps | [
"brute force",
"greedy"
] | null | null | Adilbek's house is located on a street which can be represented as the OX axis. This street is really dark, so Adilbek wants to install some post lamps to illuminate it. Street has $n$ positions to install lamps, they correspond to the integer numbers from $0$ to $n - 1$ on the OX axis. However, some positions are blocked and no post lamp can be placed there.
There are post lamps of different types which differ only by their power. When placed in position $x$, post lamp of power $l$ illuminates the segment $[x; x + l]$. The power of each post lamp is always a positive integer number.
The post lamp shop provides an infinite amount of lamps of each type from power $1$ to power $k$. Though each customer is only allowed to order post lamps of exactly one type. Post lamps of power $l$ cost $a_l$ each.
What is the minimal total cost of the post lamps of exactly one type Adilbek can buy to illuminate the entire segment $[0; n]$ of the street? If some lamps illuminate any other segment of the street, Adilbek does not care, so, for example, he may place a lamp of power $3$ in position $n - 1$ (even though its illumination zone doesn't completely belong to segment $[0; n]$). | The first line contains three integer numbers $n$, $m$ and $k$ ($1 \le k \le n \le 10^6$, $0 \le m \le n$) — the length of the segment of the street Adilbek wants to illuminate, the number of the blocked positions and the maximum power of the post lamp available.
The second line contains $m$ integer numbers $s_1, s_2, \dots, s_m$ ($0 \le s_1 < s_2 < \dots s_m < n$) — the blocked positions.
The third line contains $k$ integer numbers $a_1, a_2, \dots, a_k$ ($1 \le a_i \le 10^6$) — the costs of the post lamps. | Print the minimal total cost of the post lamps of exactly one type Adilbek can buy to illuminate the entire segment $[0; n]$ of the street.
If illumintaing the entire segment $[0; n]$ is impossible, print -1. | [
"6 2 3\n1 3\n1 2 3\n",
"4 3 4\n1 2 3\n1 10 100 1000\n",
"5 1 5\n0\n3 3 3 3 3\n",
"7 4 3\n2 4 5 6\n3 14 15\n"
] | [
"6\n",
"1000\n",
"-1\n",
"-1\n"
] | none | [
{
"input": "6 2 3\n1 3\n1 2 3",
"output": "6"
},
{
"input": "4 3 4\n1 2 3\n1 10 100 1000",
"output": "1000"
},
{
"input": "5 1 5\n0\n3 3 3 3 3",
"output": "-1"
},
{
"input": "7 4 3\n2 4 5 6\n3 14 15",
"output": "-1"
},
{
"input": "1 0 1\n\n1000000",
"output": ... | 2,000 | 108,236,800 | 0 | 11,771 | |
58 | Coins | [
"greedy"
] | B. Coins | 2 | 256 | In Berland a money reform is being prepared. New coins are being introduced. After long economic calculations was decided that the most expensive coin should possess the denomination of exactly *n* Berland dollars. Also the following restriction has been introduced for comfort: the denomination of each coin should be divisible by the denomination of any cheaper coin. It is known that among all the possible variants the variant with the largest number of new coins will be chosen. Find this variant. Print in the order of decreasing of the coins' denominations. | The first and only line contains an integer *n* (1<=≤<=*n*<=≤<=106) which represents the denomination of the most expensive coin. | Print the denominations of all the coins in the order of decreasing. The number of coins must be the largest possible (with the given denomination *n* of the most expensive coin). Also, the denomination of every coin must be divisible by the denomination of any cheaper coin. Naturally, the denominations of all the coins should be different. If there are several solutins to that problem, print any of them. | [
"10\n",
"4\n",
"3\n"
] | [
"10 5 1\n",
"4 2 1\n",
"3 1\n"
] | none | [
{
"input": "10",
"output": "10 5 1"
},
{
"input": "4",
"output": "4 2 1"
},
{
"input": "3",
"output": "3 1"
},
{
"input": "2",
"output": "2 1"
},
{
"input": "5",
"output": "5 1"
},
{
"input": "6",
"output": "6 3 1"
},
{
"input": "7",
"o... | 154 | 0 | 3.9615 | 11,788 |
0 | none | [
"none"
] | null | null | Petya learned a new programming language CALPAS. A program in this language always takes one non-negative integer and returns one non-negative integer as well.
In the language, there are only three commands: apply a bitwise operation AND, OR or XOR with a given constant to the current integer. A program can contain an arbitrary sequence of these operations with arbitrary constants from 0 to 1023. When the program is run, all operations are applied (in the given order) to the argument and in the end the result integer is returned.
Petya wrote a program in this language, but it turned out to be too long. Write a program in CALPAS that does the same thing as the Petya's program, and consists of no more than 5 lines. Your program should return the same integer as Petya's program for all arguments from 0 to 1023. | The first line contains an integer *n* (1<=≤<=*n*<=≤<=5·105) — the number of lines.
Next *n* lines contain commands. A command consists of a character that represents the operation ("&", "|" or "^" for AND, OR or XOR respectively), and the constant *x**i* 0<=≤<=*x**i*<=≤<=1023. | Output an integer *k* (0<=≤<=*k*<=≤<=5) — the length of your program.
Next *k* lines must contain commands in the same format as in the input. | [
"3\n| 3\n^ 2\n| 1\n",
"3\n& 1\n& 3\n& 5\n",
"3\n^ 1\n^ 2\n^ 3\n"
] | [
"2\n| 3\n^ 2\n",
"1\n& 1\n",
"0\n"
] | You can read about bitwise operations in [https://en.wikipedia.org/wiki/Bitwise_operation](https://en.wikipedia.org/wiki/Bitwise_operation).
Second sample:
Let *x* be an input of the Petya's program. It's output is ((*x*&1)&3)&5 = *x*&(1&3&5) = *x*&1. So these two programs always give the same outputs. | [
{
"input": "3\n| 3\n^ 2\n| 1",
"output": "2\n| 3\n^ 2"
},
{
"input": "3\n& 1\n& 3\n& 5",
"output": "1\n& 1"
},
{
"input": "3\n^ 1\n^ 2\n^ 3",
"output": "0"
},
{
"input": "2\n| 999\n^ 689",
"output": "2\n| 999\n^ 689"
},
{
"input": "3\n& 242\n^ 506\n^ 522",
"ou... | 124 | 0 | 0 | 11,823 | |
268 | Wall Bars | [
"dp"
] | null | null | Manao is working for a construction company. Recently, an order came to build wall bars in a children's park. Manao was commissioned to develop a plan of construction, which will enable the company to save the most money.
After reviewing the formal specifications for the wall bars, Manao discovered a number of controversial requirements and decided to treat them to the company's advantage. His resulting design can be described as follows:
- Let's introduce some unit of length. The construction center is a pole of height *n*. - At heights 1,<=2,<=...,<=*n* exactly one horizontal bar sticks out from the pole. Each bar sticks in one of four pre-fixed directions. - A child can move from one bar to another if the distance between them does not exceed *h* and they stick in the same direction. If a child is on the ground, he can climb onto any of the bars at height between 1 and *h*. In Manao's construction a child should be able to reach at least one of the bars at heights *n*<=-<=*h*<=+<=1,<=*n*<=-<=*h*<=+<=2,<=...,<=*n* if he begins at the ground.
Manao is wondering how many distinct construction designs that satisfy his requirements exist. As this number can be rather large, print the remainder after dividing it by 1000000009 (109<=+<=9). Two designs are considered distinct if there is such height *i*, that the bars on the height *i* in these designs don't stick out in the same direction. | A single line contains two space-separated integers, *n* and *h* (1<=≤<=*n*<=≤<=1000, 1<=≤<=*h*<=≤<=*min*(*n*,<=30)). | In a single line print the remainder after dividing the number of designs by 1000000009 (109<=+<=9). | [
"5 1\n",
"4 2\n",
"4 3\n",
"5 2\n"
] | [
"4\n",
"148\n",
"256\n",
"376\n"
] | Consider several designs for *h* = 2. A design with the first bar sticked out in direction *d*<sub class="lower-index">1</sub>, the second — in direction *d*<sub class="lower-index">2</sub> and so on (1 ≤ *d*<sub class="lower-index">*i*</sub> ≤ 4) is denoted as string *d*<sub class="lower-index">1</sub>*d*<sub class="lower-index">2</sub>...*d*<sub class="lower-index">*n*</sub>.
Design "1231" (the first three bars are sticked out in different directions, the last one — in the same as first). A child can reach neither the bar at height 3 nor the bar at height 4.
Design "414141". A child can reach the bar at height 5. To do this, he should first climb at the first bar, then at the third and then at the fifth one. He can also reach bar at height 6 by the route second → fourth → sixth bars.
Design "123333". The child can't reach the upper two bars.
Design "323323". The bar at height 6 can be reached by the following route: first → third → fourth → sixth bars. | [] | 122 | 0 | 0 | 11,830 | |
59 | Title | [
"expression parsing"
] | C. Title | 2 | 256 | Vasya has recently finished writing a book. Now he faces the problem of giving it the title. Vasya wants the title to be vague and mysterious for his book to be noticeable among others. That's why the title should be represented by a single word containing at least once each of the first *k* Latin letters and not containing any other ones. Also, the title should be a palindrome, that is it should be read similarly from the left to the right and from the right to the left.
Vasya has already composed the approximate variant of the title. You are given the title template *s* consisting of lowercase Latin letters and question marks. Your task is to replace all the question marks by lowercase Latin letters so that the resulting word satisfies the requirements, described above. Each question mark should be replaced by exactly one letter, it is not allowed to delete characters or add new ones to the template. If there are several suitable titles, choose the first in the alphabetical order, for Vasya's book to appear as early as possible in all the catalogues. | The first line contains an integer *k* (1<=≤<=*k*<=≤<=26) which is the number of allowed alphabet letters. The second line contains *s* which is the given template. In *s* only the first *k* lowercase letters of Latin alphabet and question marks can be present, the length of *s* is from 1 to 100 characters inclusively. | If there is no solution, print IMPOSSIBLE. Otherwise, a single line should contain the required title, satisfying the given template. The title should be a palindrome and it can only contain the first *k* letters of the Latin alphabet. At that, each of those *k* letters must be present at least once. If there are several suitable titles, print the lexicographically minimal one.
The lexicographical comparison is performed by the standard < operator in modern programming languages. The line *a* is lexicographically smaller than the line *b*, if exists such an *i* (1<=≤<=*i*<=≤<=|*s*|), that *a**i*<=<<=*b**i*, and for any *j* (1<=≤<=*j*<=<<=*i*) *a**j*<==<=*b**j*. |*s*| stands for the length of the given template. | [
"3\na?c\n",
"2\na??a\n",
"2\n?b?a\n"
] | [
"IMPOSSIBLE\n",
"abba\n",
"abba\n"
] | none | [
{
"input": "3\na?c",
"output": "IMPOSSIBLE"
},
{
"input": "2\na??a",
"output": "abba"
},
{
"input": "2\n?b?a",
"output": "abba"
},
{
"input": "3\n????",
"output": "IMPOSSIBLE"
},
{
"input": "2\n????",
"output": "abba"
},
{
"input": "1\n?",
"output"... | 154 | 512,000 | 3.960546 | 11,859 |
413 | Jeopardy! | [
"greedy",
"math"
] | null | null | 'Jeopardy!' is an intellectual game where players answer questions and earn points. Company Q conducts a simplified 'Jeopardy!' tournament among the best IT companies. By a lucky coincidence, the old rivals made it to the finals: company R1 and company R2.
The finals will have *n* questions, *m* of them are auction questions and *n*<=-<=*m* of them are regular questions. Each question has a price. The price of the *i*-th question is *a**i* points. During the game the players chose the questions. At that, if the question is an auction, then the player who chose it can change the price if the number of his current points is strictly larger than the price of the question. The new price of the question cannot be less than the original price and cannot be greater than the current number of points of the player who chose the question. The correct answer brings the player the points equal to the price of the question. The wrong answer to the question reduces the number of the player's points by the value of the question price.
The game will go as follows. First, the R2 company selects a question, then the questions are chosen by the one who answered the previous question correctly. If no one answered the question, then the person who chose last chooses again.
All R2 employees support their team. They want to calculate what maximum possible number of points the R2 team can get if luck is on their side during the whole game (they will always be the first to correctly answer questions). Perhaps you are not going to be surprised, but this problem was again entrusted for you to solve. | The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100; *m*<=≤<=*min*(*n*,<=30)) — the total number of questions and the number of auction questions, correspondingly. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=107) — the prices of the questions. The third line contains *m* distinct integers *b**i* (1<=≤<=*b**i*<=≤<=*n*) — the numbers of auction questions. Assume that the questions are numbered from 1 to *n*. | In the single line, print the answer to the problem — the maximum points the R2 company can get if it plays optimally well. It is guaranteed that the answer fits into the integer 64-bit signed type. | [
"4 1\n1 3 7 5\n3\n",
"3 2\n10 3 8\n2 3\n",
"2 2\n100 200\n1 2\n"
] | [
"18\n",
"40\n",
"400\n"
] | none | [
{
"input": "4 1\n1 3 7 5\n3",
"output": "18"
},
{
"input": "3 2\n10 3 8\n2 3",
"output": "40"
},
{
"input": "2 2\n100 200\n1 2",
"output": "400"
},
{
"input": "1 1\n1\n1",
"output": "1"
},
{
"input": "2 2\n1 5\n1 2",
"output": "10"
},
{
"input": "5 3\n... | 202 | 2,457,600 | -1 | 11,868 | |
24 | Broken robot | [
"dp",
"math",
"probabilities"
] | D. Broken robot | 2 | 256 | You received as a gift a very clever robot walking on a rectangular board. Unfortunately, you understood that it is broken and behaves rather strangely (randomly). The board consists of *N* rows and *M* columns of cells. The robot is initially at some cell on the *i*-th row and the *j*-th column. Then at every step the robot could go to some another cell. The aim is to go to the bottommost (*N*-th) row. The robot can stay at it's current cell, move to the left, move to the right, or move to the cell below the current. If the robot is in the leftmost column it cannot move to the left, and if it is in the rightmost column it cannot move to the right. At every step all possible moves are equally probable. Return the expected number of step to reach the bottommost row. | On the first line you will be given two space separated integers *N* and *M* (1<=≤<=*N*,<=*M*<=≤<=1000). On the second line you will be given another two space separated integers *i* and *j* (1<=≤<=*i*<=≤<=*N*,<=1<=≤<=*j*<=≤<=*M*) — the number of the initial row and the number of the initial column. Note that, (1,<=1) is the upper left corner of the board and (*N*,<=*M*) is the bottom right corner. | Output the expected number of steps on a line of itself with at least 4 digits after the decimal point. | [
"10 10\n10 4\n",
"10 14\n5 14\n"
] | [
"0.0000000000\n",
"18.0038068653\n"
] | none | [
{
"input": "10 10\n10 4",
"output": "0.0000000000"
},
{
"input": "10 14\n5 14",
"output": "18.0038068653"
},
{
"input": "126 125\n115 22",
"output": "43.9999127943"
},
{
"input": "755 51\n205 12",
"output": "2178.8368031733"
},
{
"input": "385 978\n344 18",
"o... | 92 | 0 | 0 | 11,870 |
489 | Hiking | [
"binary search",
"dp"
] | null | null | A traveler is planning a water hike along the river. He noted the suitable rest points for the night and wrote out their distances from the starting point. Each of these locations is further characterized by its picturesqueness, so for the *i*-th rest point the distance from the start equals *x**i*, and its picturesqueness equals *b**i*. The traveler will move down the river in one direction, we can assume that he will start from point 0 on the coordinate axis and rest points are points with coordinates *x**i*.
Every day the traveler wants to cover the distance *l*. In practice, it turns out that this is not always possible, because he needs to end each day at one of the resting points. In addition, the traveler is choosing between two desires: cover distance *l* every day and visit the most picturesque places.
Let's assume that if the traveler covers distance *r**j* in a day, then he feels frustration , and his total frustration over the hike is calculated as the total frustration on all days.
Help him plan the route so as to minimize the relative total frustration: the total frustration divided by the total picturesqueness of all the rest points he used.
The traveler's path must end in the farthest rest point. | The first line of the input contains integers *n*,<=*l* (1<=≤<=*n*<=≤<=1000,<=1<=≤<=*l*<=≤<=105) — the number of rest points and the optimal length of one day path.
Then *n* lines follow, each line describes one rest point as a pair of integers *x**i*,<=*b**i* (1<=≤<=*x**i*,<=*b**i*<=≤<=106). No two rest points have the same *x**i*, the lines are given in the order of strictly increasing *x**i*. | Print the traveler's path as a sequence of the numbers of the resting points he used in the order he used them. Number the points from 1 to *n* in the order of increasing *x**i*. The last printed number must be equal to *n*. | [
"5 9\n10 10\n20 10\n30 1\n31 5\n40 10\n"
] | [
"1 2 4 5 "
] | In the sample test the minimum value of relative total frustration approximately equals 0.097549. This value can be calculated as <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bad16faba2aa8ac4e81ca909b5e927a7f644c23f.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "5 9\n10 10\n20 10\n30 1\n31 5\n40 10",
"output": "1 2 4 5 "
},
{
"input": "1 20\n9 1",
"output": "1 "
},
{
"input": "2 7\n1 9\n5 6",
"output": "2 "
},
{
"input": "3 2\n2 6\n3 9\n6 8",
"output": "1 2 3 "
},
{
"input": "4 3\n1 6\n5 10\n9 9\n10 6",
"o... | 30 | 0 | 0 | 11,879 | |
464 | The Classic Problem | [
"data structures",
"graphs",
"shortest paths"
] | null | null | You are given a weighted undirected graph on *n* vertices and *m* edges. Find the shortest path from vertex *s* to vertex *t* or else state that such path doesn't exist. | The first line of the input contains two space-separated integers — *n* and *m* (1<=≤<=*n*<=≤<=105; 0<=≤<=*m*<=≤<=105).
Next *m* lines contain the description of the graph edges. The *i*-th line contains three space-separated integers — *u**i*, *v**i*, *x**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*; 0<=≤<=*x**i*<=≤<=105). That means that vertices with numbers *u**i* and *v**i* are connected by edge of length 2*x**i* (2 to the power of *x**i*).
The last line contains two space-separated integers — the numbers of vertices *s* and *t*.
The vertices are numbered from 1 to *n*. The graph contains no multiple edges and self-loops. | In the first line print the remainder after dividing the length of the shortest path by 1000000007 (109<=+<=7) if the path exists, and -1 if the path doesn't exist.
If the path exists print in the second line integer *k* — the number of vertices in the shortest path from vertex *s* to vertex *t*; in the third line print *k* space-separated integers — the vertices of the shortest path in the visiting order. The first vertex should be vertex *s*, the last vertex should be vertex *t*. If there are multiple shortest paths, print any of them. | [
"4 4\n1 4 2\n1 2 0\n2 3 0\n3 4 0\n1 4\n",
"4 3\n1 2 4\n2 3 5\n3 4 6\n1 4\n",
"4 2\n1 2 0\n3 4 1\n1 4\n"
] | [
"3\n4\n1 2 3 4 \n",
"112\n4\n1 2 3 4 \n",
"-1\n"
] | A path from vertex *s* to vertex *t* is a sequence *v*<sub class="lower-index">0</sub>, ..., *v*<sub class="lower-index">*k*</sub>, such that *v*<sub class="lower-index">0</sub> = *s*, *v*<sub class="lower-index">*k*</sub> = *t*, and for any *i* from 0 to *k* - 1 vertices *v*<sub class="lower-index">*i*</sub> and *v*<sub class="lower-index">*i* + 1</sub> are connected by an edge.
The length of the path is the sum of weights of edges between *v*<sub class="lower-index">*i*</sub> and *v*<sub class="lower-index">*i* + 1</sub> for all *i* from 0 to *k* - 1.
The shortest path from *s* to *t* is the path which length is minimum among all possible paths from *s* to *t*. | [] | 61 | 512,000 | 0 | 11,880 | |
297 | Mystic Carvings | [
"data structures"
] | null | null | The polar bears have discovered a gigantic circular piece of floating ice with some mystic carvings on it. There are *n* lines carved on the ice. Each line connects two points on the boundary of the ice (we call these points endpoints). The endpoints are numbered 1,<=2,<=...,<=2*n* counter-clockwise along the circumference. No two lines share an endpoint.
Now a group of 6 polar bears (Alice, Bob, Carol, Dave, Eve, Frank) are going to build caves on the endpoints. Each polar bear would build a cave and live in it. No two polar bears can build a cave on the same endpoints. Alice and Bob is a pair of superstitious lovers. They believe the lines are carved by aliens (or humans, which are pretty much the same thing to polar bears), and have certain spiritual power. Therefore they want to build their caves on two endpoints which are connected by a line. The same for Carol and Dave, Eve and Frank.
The distance between two caves X and Y is defined as one plus minimum number of other caves one need to pass through in order to travel from X to Y along the boundary of the ice (endpoints without caves are not counted).
To ensure fairness, the distances between the three pairs of lovers have to be the same (that is, the distance between Alice and Bob, the distance between Carol and Dave, and the distance between Eve and Frank are the same).
The figures below show two different configurations, where the dots on the circle are the endpoints. The configuration on the left is not valid. Although each pair of lovers (A and B, C and D, E and F) is connected a line, the distance requirement is not satisfied. The distance between A and B is 2 (one can go from A to B in the clockwise direction passing through F). The distance between E and F is also 2. However, the distance between C and D is 1 (one can go from C to D in the counter-clockwise direction without passing through any other caves). The configuration on the right is valid. All three pairs have the same distance 1.
Count the number of ways to build the caves under the requirements. Two configurations are considered the same if the same set of 6 endpoints are used. | The first line contains integer *n*(3<=≤<=*n*<=≤<=105) — the number of lines.
Each of the following *n* lines contains two integers *a**i*,<=*b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=2*n*), which means that there is a line carved on the ice connecting the *a**i*–th and *b**i*–th endpoint.
It's guaranteed that each endpoints touches exactly one line. | Print the number of ways to build the caves.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"4\n5 4\n1 2\n6 7\n8 3\n",
"8\n1 7\n2 4\n3 9\n5 11\n6 8\n10 16\n13 15\n14 12\n"
] | [
"2\n",
"6\n"
] | The second sample corresponds to the figure in the problem statement. | [] | 60 | 0 | 0 | 11,890 | |
960 | Subsequence Counting | [
"bitmasks",
"constructive algorithms",
"greedy",
"implementation"
] | null | null | Pikachu had an array with him. He wrote down all the non-empty subsequences of the array on paper. Note that an array of size *n* has 2*n*<=-<=1 non-empty subsequences in it.
Pikachu being mischievous as he always is, removed all the subsequences in which Maximum_element_of_the_subsequence <=-<= Minimum_element_of_subsequence <=≥<=*d*
Pikachu was finally left with *X* subsequences.
However, he lost the initial array he had, and now is in serious trouble. He still remembers the numbers *X* and *d*. He now wants you to construct any such array which will satisfy the above conditions. All the numbers in the final array should be positive integers less than 1018.
Note the number of elements in the output array should not be more than 104. If no answer is possible, print <=-<=1. | The only line of input consists of two space separated integers *X* and *d* (1<=≤<=*X*,<=*d*<=≤<=109). | Output should consist of two lines.
First line should contain a single integer *n* (1<=≤<=*n*<=≤<=10<=000)— the number of integers in the final array.
Second line should consist of *n* space separated integers — *a*1,<=*a*2,<=... ,<=*a**n* (1<=≤<=*a**i*<=<<=1018).
If there is no answer, print a single integer -1. If there are multiple answers, print any of them. | [
"10 5\n",
"4 2\n"
] | [
"6\n5 50 7 15 6 100",
"4\n10 100 1000 10000"
] | In the output of the first example case, the remaining subsequences after removing those with Maximum_element_of_the_subsequence - Minimum_element_of_subsequence ≥ 5 are [5], [5, 7], [5, 6], [5, 7, 6], [50], [7], [7, 6], [15], [6], [100]. There are 10 of them. Hence, the array [5, 50, 7, 15, 6, 100] is valid.
Similarly, in the output of the second example case, the remaining sub-sequences after removing those with Maximum_element_of_the_subsequence - Minimum_element_of_subsequence ≥ 2 are [10], [100], [1000], [10000]. There are 4 of them. Hence, the array [10, 100, 1000, 10000] is valid. | [
{
"input": "10 5",
"output": "6\n1 1 1 7 13 19 "
},
{
"input": "4 2",
"output": "3\n1 1 4 "
},
{
"input": "4 1",
"output": "3\n1 1 3 "
},
{
"input": "1 1",
"output": "1\n1 "
},
{
"input": "63 1",
"output": "21\n1 1 1 1 1 3 3 3 3 5 5 5 7 7 9 11 13 15 17 19 21 "... | 77 | 0 | 0 | 11,913 | |
30 | Codeforces World Finals | [
"implementation"
] | B. Codeforces World Finals | 2 | 256 | The king Copa often has been reported about the Codeforces site, which is rapidly getting more and more popular among the brightest minds of the humanity, who are using it for training and competing. Recently Copa understood that to conquer the world he needs to organize the world Codeforces tournament. He hopes that after it the brightest minds will become his subordinates, and the toughest part of conquering the world will be completed.
The final round of the Codeforces World Finals 20YY is scheduled for *DD*.*MM*.*YY*, where *DD* is the day of the round, *MM* is the month and *YY* are the last two digits of the year. Bob is lucky to be the first finalist form Berland. But there is one problem: according to the rules of the competition, all participants must be at least 18 years old at the moment of the finals. Bob was born on *BD*.*BM*.*BY*. This date is recorded in his passport, the copy of which he has already mailed to the organizers. But Bob learned that in different countries the way, in which the dates are written, differs. For example, in the US the month is written first, then the day and finally the year. Bob wonders if it is possible to rearrange the numbers in his date of birth so that he will be at least 18 years old on the day *DD*.*MM*.*YY*. He can always tell that in his motherland dates are written differently. Help him.
According to another strange rule, eligible participant must be born in the same century as the date of the finals. If the day of the finals is participant's 18-th birthday, he is allowed to participate.
As we are considering only the years from 2001 to 2099 for the year of the finals, use the following rule: the year is leap if it's number is divisible by four. | The first line contains the date *DD*.*MM*.*YY*, the second line contains the date *BD*.*BM*.*BY*. It is guaranteed that both dates are correct, and *YY* and *BY* are always in [01;99].
It could be that by passport Bob was born after the finals. In this case, he can still change the order of numbers in date. | If it is possible to rearrange the numbers in the date of birth so that Bob will be at least 18 years old on the *DD*.*MM*.*YY*, output YES. In the other case, output NO.
Each number contains exactly two digits and stands for day, month or year in a date. Note that it is permitted to rearrange only numbers, not digits. | [
"01.01.98\n01.01.80\n",
"20.10.20\n10.02.30\n",
"28.02.74\n28.02.64\n"
] | [
"YES\n",
"NO\n",
"NO\n"
] | none | [
{
"input": "01.01.98\n01.01.80",
"output": "YES"
},
{
"input": "20.10.20\n10.02.30",
"output": "NO"
},
{
"input": "28.02.74\n28.02.64",
"output": "NO"
},
{
"input": "05.05.25\n06.02.71",
"output": "NO"
},
{
"input": "19.11.54\n29.11.53",
"output": "NO"
},
... | 60 | 0 | 0 | 11,925 |
535 | Tavas and Malekas | [
"greedy",
"hashing",
"string suffix structures",
"strings"
] | null | null | Tavas is a strange creature. Usually "zzz" comes out of people's mouth while sleeping, but string *s* of length *n* comes out from Tavas' mouth instead.
Today Tavas fell asleep in Malekas' place. While he was sleeping, Malekas did a little process on *s*. Malekas has a favorite string *p*. He determined all positions *x*1<=<<=*x*2<=<<=...<=<<=*x**k* where *p* matches *s*. More formally, for each *x**i* (1<=≤<=*i*<=≤<=*k*) he condition *s**x**i**s**x**i*<=+<=1... *s**x**i*<=+<=|*p*|<=-<=1<==<=*p* is fullfilled.
Then Malekas wrote down one of subsequences of *x*1,<=*x*2,<=... *x**k* (possibly, he didn't write anything) on a piece of paper. Here a sequence *b* is a subsequence of sequence *a* if and only if we can turn *a* into *b* by removing some of its elements (maybe no one of them or all).
After Tavas woke up, Malekas told him everything. He couldn't remember string *s*, but he knew that both *p* and *s* only contains lowercase English letters and also he had the subsequence he had written on that piece of paper.
Tavas wonders, what is the number of possible values of *s*? He asked SaDDas, but he wasn't smart enough to solve this. So, Tavas asked you to calculate this number for him.
Answer can be very large, so Tavas wants you to print the answer modulo 109<=+<=7. | The first line contains two integers *n* and *m*, the length of *s* and the length of the subsequence Malekas wrote down (1<=≤<=*n*<=≤<=106 and 0<=≤<=*m*<=≤<=*n*<=-<=|*p*|<=+<=1).
The second line contains string *p* (1<=≤<=|*p*|<=≤<=*n*).
The next line contains *m* space separated integers *y*1,<=*y*2,<=...,<=*y**m*, Malekas' subsequence (1<=≤<=*y*1<=<<=*y*2<=<<=...<=<<=*y**m*<=≤<=*n*<=-<=|*p*|<=+<=1). | In a single line print the answer modulo 1000<=000<=007. | [
"6 2\nioi\n1 3\n",
"5 2\nioi\n1 2\n"
] | [
"26\n",
"0\n"
] | In the first sample test all strings of form "ioioi?" where the question mark replaces arbitrary English letter satisfy.
Here |*x*| denotes the length of string x.
Please note that it's possible that there is no such string (answer is 0). | [
{
"input": "6 2\nioi\n1 3",
"output": "26"
},
{
"input": "5 2\nioi\n1 2",
"output": "0"
},
{
"input": "173700 6\nbcabcbcbcbaaacaccaacaccaabacabaacbcacbbccaccbcacbabcaccccccaacacabbbbbacabbaaacbcbbaccaccabbbbaabbacacbabccaabcabbbcacaaccbabbcaaaaaabccbbcabcacbcbcabcbcbbaabacaaccccabacaaacc... | 483 | 59,904,000 | -1 | 11,945 | |
616 | Sum of Remainders | [
"implementation",
"math",
"number theory"
] | null | null | Calculate the value of the sum: *n* mod 1 + *n* mod 2 + *n* mod 3 + ... + *n* mod *m*. As the result can be very large, you should print the value modulo 109<=+<=7 (the remainder when divided by 109<=+<=7).
The modulo operator *a* mod *b* stands for the remainder after dividing *a* by *b*. For example 10 mod 3 = 1. | The only line contains two integers *n*,<=*m* (1<=≤<=*n*,<=*m*<=≤<=1013) — the parameters of the sum. | Print integer *s* — the value of the required sum modulo 109<=+<=7. | [
"3 4\n",
"4 4\n",
"1 1\n"
] | [
"4\n",
"1\n",
"0\n"
] | none | [
{
"input": "3 4",
"output": "4"
},
{
"input": "4 4",
"output": "1"
},
{
"input": "1 1",
"output": "0"
},
{
"input": "20000000 10000000",
"output": "176305083"
},
{
"input": "10000000000000 10000000000000",
"output": "869957328"
},
{
"input": "1 1000000... | 2,000 | 1,843,200 | 0 | 11,948 | |
862 | Mahmoud and Ehab and the binary string | [
"binary search",
"divide and conquer",
"interactive"
] | null | null | Mahmoud and Ehab are in the fourth stage now.
Dr. Evil has a hidden binary string of length *n*. He guarantees that there is at least one '0' symbol and at least one '1' symbol in it. Now he wants Mahmoud and Ehab to find a position of any '0' symbol and any '1' symbol. In order to do this, Mahmoud and Ehab can ask Dr. Evil up to 15 questions. They tell Dr. Evil some binary string of length *n*, and Dr. Evil tells the Hamming distance between these two strings. Hamming distance between 2 binary strings of the same length is the number of positions in which they have different symbols. You can find the definition of Hamming distance in the notes section below.
Help Mahmoud and Ehab find these two positions.
You will get Wrong Answer verdict if
- Your queries doesn't satisfy interaction protocol described below. - You ask strictly more than 15 questions and your program terminated after exceeding queries limit. Please note, that you can do up to 15 ask queries and one answer query. - Your final answer is not correct.
If you exceed the maximum number of queries, You should terminate with 0, In this case you'll get Wrong Answer, If you don't terminate you may receive any verdict because you'll be reading from a closed stream . | The first line of input will contain a single integer *n* (2<=≤<=*n*<=≤<=1000) — the length of the hidden binary string. | To print the final answer, print "! pos0 pos1" (without quotes), where *pos*0 and *pos*1 are positions of some '0' and some '1' in the string (the string is 1-indexed). Don't forget to flush the output after printing the answer! | [
"3\n2\n1\n3\n2\n1\n0"
] | [
"? 000\n? 001\n? 010\n? 011\n? 100\n? 101\n! 2 1"
] | Hamming distance definition: [https://en.wikipedia.org/wiki/Hamming_distance](https://en.wikipedia.org/wiki/Hamming_distance)
In the first test case the hidden binary string is 101, The first query is 000, so the Hamming distance is 2. In the second query the hidden string is still 101 and query is 001, so the Hamming distance is 1.
After some queries you find that symbol at position 2 is '0' and symbol at position 1 is '1', so you print "! 2 1". | [
{
"input": "101",
"output": "3"
},
{
"input": "0011001100",
"output": "4"
},
{
"input": "01",
"output": "2"
},
{
"input": "0010100101101100001101110001110011000010011011001110010011101010011010100101101001111010111001000100",
"output": "8"
},
{
"input": "010101010... | 109 | 0 | 0 | 11,994 | |
886 | Restoration of string | [
"constructive algorithms",
"graphs",
"implementation"
] | null | null | A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring.
You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Restore the non-empty good string with minimum length. If several such strings exist, restore lexicographically minimum string. If there are no good strings, print "NO" (without quotes).
A substring of a string is a contiguous subsequence of letters in the string. For example, "ab", "c", "abc" are substrings of string "abc", while "ac" is not a substring of that string.
The number of occurrences of a substring in a string is the number of starting positions in the string where the substring occurs. These occurrences could overlap.
String *a* is lexicographically smaller than string *b*, if *a* is a prefix of *b*, or *a* has a smaller letter at the first position where *a* and *b* differ. | The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of strings in the set.
Each of the next *n* lines contains a non-empty string consisting of lowercase English letters. It is guaranteed that the strings are distinct.
The total length of the strings doesn't exceed 105. | Print the non-empty good string with minimum length. If several good strings exist, print lexicographically minimum among them. Print "NO" (without quotes) if there are no good strings. | [
"4\nmail\nai\nlru\ncf\n",
"3\nkek\npreceq\ncheburek\n"
] | [
"cfmailru\n",
"NO\n"
] | One can show that in the first sample only two good strings with minimum length exist: "cfmailru" and "mailrucf". The first string is lexicographically minimum. | [
{
"input": "4\nmail\nai\nlru\ncf",
"output": "cfmailru"
},
{
"input": "3\nkek\npreceq\ncheburek",
"output": "NO"
},
{
"input": "1\nz",
"output": "z"
},
{
"input": "2\nab\nba",
"output": "NO"
},
{
"input": "2\nac\nbc",
"output": "NO"
},
{
"input": "2\nc... | 2,000 | 307,200 | 0 | 12,010 | |
909 | Colorful Points | [
"data structures",
"greedy",
"implementation"
] | null | null | You are given a set of points on a straight line. Each point has a color assigned to it. For point *a*, its neighbors are the points which don't have any other points between them and *a*. Each point has at most two neighbors - one from the left and one from the right.
You perform a sequence of operations on this set of points. In one operation, you delete all points which have a neighbor point of a different color than the point itself. Points are deleted simultaneously, i.e. first you decide which points have to be deleted and then delete them. After that you can perform the next operation etc. If an operation would not delete any points, you can't perform it.
How many operations will you need to perform until the next operation does not have any points to delete? | Input contains a single string of lowercase English letters 'a'-'z'. The letters give the points' colors in the order in which they are arranged on the line: the first letter gives the color of the leftmost point, the second gives the color of the second point from the left etc.
The number of the points is between 1 and 106. | Output one line containing an integer - the number of operations which can be performed on the given set of points until there are no more points to delete. | [
"aabb\n",
"aabcaa\n"
] | [
"2\n",
"1\n"
] | In the first test case, the first operation will delete two middle points and leave points "ab", which will be deleted with the second operation. There will be no points left to apply the third operation to.
In the second test case, the first operation will delete the four points in the middle, leaving points "aa". None of them have neighbors of other colors, so the second operation can't be applied. | [
{
"input": "aabb",
"output": "2"
},
{
"input": "aabcaa",
"output": "1"
},
{
"input": "abbcccbba",
"output": "1"
},
{
"input": "aaaaaaaaaaa",
"output": "0"
},
{
"input": "aaaaaaaaabbbbbaaaabaaaaaaaaaaaaaaaaabaaaaaabbbbbbbaaabbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaa... | 2,000 | 9,625,600 | 0 | 12,064 | |
377 | Preparing for the Contest | [
"binary search",
"data structures",
"greedy",
"sortings"
] | null | null | Soon there will be held the world's largest programming contest, but the testing system still has *m* bugs. The contest organizer, a well-known university, has no choice but to attract university students to fix all the bugs. The university has *n* students able to perform such work. The students realize that they are the only hope of the organizers, so they don't want to work for free: the *i*-th student wants to get *c**i* 'passes' in his subjects (regardless of the volume of his work).
Bugs, like students, are not the same: every bug is characterized by complexity *a**j*, and every student has the level of his abilities *b**i*. Student *i* can fix a bug *j* only if the level of his abilities is not less than the complexity of the bug: *b**i*<=≥<=*a**j*, and he does it in one day. Otherwise, the bug will have to be fixed by another student. Of course, no student can work on a few bugs in one day. All bugs are not dependent on each other, so they can be corrected in any order, and different students can work simultaneously.
The university wants to fix all the bugs as quickly as possible, but giving the students the total of not more than *s* passes. Determine which students to use for that and come up with the schedule of work saying which student should fix which bug. | The first line contains three space-separated integers: *n*, *m* and *s* (1<=≤<=*n*,<=*m*<=≤<=105, 0<=≤<=*s*<=≤<=109) — the number of students, the number of bugs in the system and the maximum number of passes the university is ready to give the students.
The next line contains *m* space-separated integers *a*1, *a*2, ..., *a**m* (1<=≤<=*a**i*<=≤<=109) — the bugs' complexities.
The next line contains *n* space-separated integers *b*1, *b*2, ..., *b**n* (1<=≤<=*b**i*<=≤<=109) — the levels of the students' abilities.
The next line contains *n* space-separated integers *c*1, *c*2, ..., *c**n* (0<=≤<=*c**i*<=≤<=109) — the numbers of the passes the students want to get for their help. | If the university can't correct all bugs print "NO".
Otherwise, on the first line print "YES", and on the next line print *m* space-separated integers: the *i*-th of these numbers should equal the number of the student who corrects the *i*-th bug in the optimal answer. The bugs should be corrected as quickly as possible (you must spend the minimum number of days), and the total given passes mustn't exceed *s*. If there are multiple optimal answers, you can output any of them. | [
"3 4 9\n1 3 1 2\n2 1 3\n4 3 6\n",
"3 4 10\n2 3 1 2\n2 1 3\n4 3 6\n",
"3 4 9\n2 3 1 2\n2 1 3\n4 3 6\n",
"3 4 5\n1 3 1 2\n2 1 3\n5 3 6\n"
] | [
"YES\n2 3 2 3\n",
"YES\n1 3 1 3\n",
"YES\n3 3 2 3\n",
"NO\n"
] | Consider the first sample.
The third student (with level 3) must fix the 2nd and 4th bugs (complexities 3 and 2 correspondingly) and the second student (with level 1) must fix the 1st and 3rd bugs (their complexity also equals 1). Fixing each bug takes one day for each student, so it takes 2 days to fix all bugs (the students can work in parallel).
The second student wants 3 passes for his assistance, the third student wants 6 passes. It meets the university's capabilities as it is ready to give at most 9 passes. | [
{
"input": "3 4 9\n1 3 1 2\n2 1 3\n4 3 6",
"output": "YES\n2 3 2 3"
},
{
"input": "3 4 10\n2 3 1 2\n2 1 3\n4 3 6",
"output": "YES\n1 3 1 3"
},
{
"input": "3 4 9\n2 3 1 2\n2 1 3\n4 3 6",
"output": "YES\n3 3 2 3"
},
{
"input": "3 4 5\n1 3 1 2\n2 1 3\n5 3 6",
"output": "NO"
... | 61 | 2,867,200 | -1 | 12,100 | |
325 | Square and Rectangles | [
"implementation"
] | null | null | You are given *n* rectangles. The corners of rectangles have integer coordinates and their edges are parallel to the *Ox* and *Oy* axes. The rectangles may touch each other, but they do not overlap (that is, there are no points that belong to the interior of more than one rectangle).
Your task is to determine if the rectangles form a square. In other words, determine if the set of points inside or on the border of at least one rectangle is precisely equal to the set of points inside or on the border of some square. | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=5). Next *n* lines contain four integers each, describing a single rectangle: *x*1, *y*1, *x*2, *y*2 (0<=≤<=*x*1<=<<=*x*2<=≤<=31400,<=0<=≤<=*y*1<=<<=*y*2<=≤<=31400) — *x*1 and *x*2 are *x*-coordinates of the left and right edges of the rectangle, and *y*1 and *y*2 are *y*-coordinates of the bottom and top edges of the rectangle.
No two rectangles overlap (that is, there are no points that belong to the interior of more than one rectangle). | In a single line print "YES", if the given rectangles form a square, or "NO" otherwise. | [
"5\n0 0 2 3\n0 3 3 5\n2 0 5 2\n3 2 5 5\n2 2 3 3\n",
"4\n0 0 2 3\n0 3 3 5\n2 0 5 2\n3 2 5 5\n"
] | [
"YES\n",
"NO\n"
] | none | [
{
"input": "5\n0 0 2 3\n0 3 3 5\n2 0 5 2\n3 2 5 5\n2 2 3 3",
"output": "YES"
},
{
"input": "4\n0 0 2 3\n0 3 3 5\n2 0 5 2\n3 2 5 5",
"output": "NO"
},
{
"input": "5\n0 0 10000 20000\n10000 0 15000 19999\n10000 19999 14999 20000\n0 20000 15000 31400\n15000 0 31400 31400",
"output": "NO... | 62 | 0 | -1 | 12,114 | |
978 | Mentors | [
"binary search",
"data structures",
"implementation"
] | null | null | In BerSoft $n$ programmers work, the programmer $i$ is characterized by a skill $r_i$.
A programmer $a$ can be a mentor of a programmer $b$ if and only if the skill of the programmer $a$ is strictly greater than the skill of the programmer $b$ $(r_a > r_b)$ and programmers $a$ and $b$ are not in a quarrel.
You are given the skills of each programmers and a list of $k$ pairs of the programmers, which are in a quarrel (pairs are unordered). For each programmer $i$, find the number of programmers, for which the programmer $i$ can be a mentor. | The first line contains two integers $n$ and $k$ $(2 \le n \le 2 \cdot 10^5$, $0 \le k \le \min(2 \cdot 10^5, \frac{n \cdot (n - 1)}{2}))$ — total number of programmers and number of pairs of programmers which are in a quarrel.
The second line contains a sequence of integers $r_1, r_2, \dots, r_n$ $(1 \le r_i \le 10^{9})$, where $r_i$ equals to the skill of the $i$-th programmer.
Each of the following $k$ lines contains two distinct integers $x$, $y$ $(1 \le x, y \le n$, $x \ne y)$ — pair of programmers in a quarrel. The pairs are unordered, it means that if $x$ is in a quarrel with $y$ then $y$ is in a quarrel with $x$. Guaranteed, that for each pair $(x, y)$ there are no other pairs $(x, y)$ and $(y, x)$ in the input. | Print $n$ integers, the $i$-th number should be equal to the number of programmers, for which the $i$-th programmer can be a mentor. Programmers are numbered in the same order that their skills are given in the input. | [
"4 2\n10 4 10 15\n1 2\n4 3\n",
"10 4\n5 4 1 5 4 3 7 1 2 5\n4 6\n2 1\n10 8\n3 5\n"
] | [
"0 0 1 2 \n",
"5 4 0 5 3 3 9 0 2 5 \n"
] | In the first example, the first programmer can not be mentor of any other (because only the second programmer has a skill, lower than first programmer skill, but they are in a quarrel). The second programmer can not be mentor of any other programmer, because his skill is minimal among others. The third programmer can be a mentor of the second programmer. The fourth programmer can be a mentor of the first and of the second programmers. He can not be a mentor of the third programmer, because they are in a quarrel. | [
{
"input": "4 2\n10 4 10 15\n1 2\n4 3",
"output": "0 0 1 2 "
},
{
"input": "10 4\n5 4 1 5 4 3 7 1 2 5\n4 6\n2 1\n10 8\n3 5",
"output": "5 4 0 5 3 3 9 0 2 5 "
},
{
"input": "2 0\n3 1",
"output": "1 0 "
},
{
"input": "2 0\n1 1",
"output": "0 0 "
},
{
"input": "10 35... | 0 | 0 | -1 | 12,133 | |
498 | Array and Operations | [
"flows",
"graph matchings",
"number theory"
] | null | null | You have written on a piece of paper an array of *n* positive integers *a*[1],<=*a*[2],<=...,<=*a*[*n*] and *m* good pairs of integers (*i*1,<=*j*1),<=(*i*2,<=*j*2),<=...,<=(*i**m*,<=*j**m*). Each good pair (*i**k*,<=*j**k*) meets the following conditions: *i**k*<=+<=*j**k* is an odd number and 1<=≤<=*i**k*<=<<=*j**k*<=≤<=*n*.
In one operation you can perform a sequence of actions:
- take one of the good pairs (*i**k*,<=*j**k*) and some integer *v* (*v*<=><=1), which divides both numbers *a*[*i**k*] and *a*[*j**k*]; - divide both numbers by *v*, i. e. perform the assignments: and .
Determine the maximum number of operations you can sequentially perform on the given array. Note that one pair may be used several times in the described operations. | The first line contains two space-separated integers *n*, *m* (2<=≤<=*n*<=≤<=100, 1<=≤<=*m*<=≤<=100).
The second line contains *n* space-separated integers *a*[1],<=*a*[2],<=...,<=*a*[*n*] (1<=≤<=*a*[*i*]<=≤<=109) — the description of the array.
The following *m* lines contain the description of good pairs. The *k*-th line contains two space-separated integers *i**k*, *j**k* (1<=≤<=*i**k*<=<<=*j**k*<=≤<=*n*, *i**k*<=+<=*j**k* is an odd number).
It is guaranteed that all the good pairs are distinct. | Output the answer for the problem. | [
"3 2\n8 3 8\n1 2\n2 3\n",
"3 2\n8 12 8\n1 2\n2 3\n"
] | [
"0\n",
"2\n"
] | none | [
{
"input": "3 2\n8 3 8\n1 2\n2 3",
"output": "0"
},
{
"input": "3 2\n8 12 8\n1 2\n2 3",
"output": "2"
},
{
"input": "6 4\n35 33 46 58 7 61\n4 5\n3 6\n5 6\n1 6",
"output": "0"
},
{
"input": "10 25\n262144 262144 64 64 16 134217728 32 512 32 8192\n1 2\n3 10\n5 8\n9 10\n2 5\n5 1... | 186 | 2,150,400 | -1 | 12,151 | |
818 | Sofa Thief | [
"brute force",
"implementation"
] | null | null | Yet another round on DecoForces is coming! Grandpa Maks wanted to participate in it but someone has stolen his precious sofa! And how can one perform well with such a major loss?
Fortunately, the thief had left a note for Grandpa Maks. This note got Maks to the sofa storehouse. Still he had no idea which sofa belongs to him as they all looked the same!
The storehouse is represented as matrix *n*<=×<=*m*. Every sofa takes two neighbouring by some side cells. No cell is covered by more than one sofa. There can be empty cells.
Sofa *A* is standing to the left of sofa *B* if there exist two such cells *a* and *b* that *x**a*<=<<=*x**b*, *a* is covered by *A* and *b* is covered by *B*. Sofa *A* is standing to the top of sofa *B* if there exist two such cells *a* and *b* that *y**a*<=<<=*y**b*, *a* is covered by *A* and *b* is covered by *B*. Right and bottom conditions are declared the same way.
Note that in all conditions *A*<=≠<=*B*. Also some sofa *A* can be both to the top of another sofa *B* and to the bottom of it. The same is for left and right conditions.
The note also stated that there are *cnt**l* sofas to the left of Grandpa Maks's sofa, *cnt**r* — to the right, *cnt**t* — to the top and *cnt**b* — to the bottom.
Grandpa Maks asks you to help him to identify his sofa. It is guaranteed that there is no more than one sofa of given conditions.
Output the number of Grandpa Maks's sofa. If there is no such sofa that all the conditions are met for it then output -1. | The first line contains one integer number *d* (1<=≤<=*d*<=≤<=105) — the number of sofas in the storehouse.
The second line contains two integer numbers *n*, *m* (1<=≤<=*n*,<=*m*<=≤<=105) — the size of the storehouse.
Next *d* lines contains four integer numbers *x*1, *y*1, *x*2, *y*2 (1<=≤<=*x*1,<=*x*2<=≤<=*n*, 1<=≤<=*y*1,<=*y*2<=≤<=*m*) — coordinates of the *i*-th sofa. It is guaranteed that cells (*x*1,<=*y*1) and (*x*2,<=*y*2) have common side, (*x*1,<=*y*1) <=≠<= (*x*2,<=*y*2) and no cell is covered by more than one sofa.
The last line contains four integer numbers *cnt**l*, *cnt**r*, *cnt**t*, *cnt**b* (0<=≤<=*cnt**l*,<=*cnt**r*,<=*cnt**t*,<=*cnt**b*<=≤<=*d*<=-<=1). | Print the number of the sofa for which all the conditions are met. Sofas are numbered 1 through *d* as given in input. If there is no such sofa then print -1. | [
"2\n3 2\n3 1 3 2\n1 2 2 2\n1 0 0 1\n",
"3\n10 10\n1 2 1 1\n5 5 6 5\n6 4 5 4\n2 1 2 0\n",
"2\n2 2\n2 1 1 1\n1 2 2 2\n1 0 0 0\n"
] | [
"1\n",
"2\n",
"-1\n"
] | Let's consider the second example.
- The first sofa has 0 to its left, 2 sofas to its right ((1, 1) is to the left of both (5, 5) and (5, 4)), 0 to its top and 2 to its bottom (both 2nd and 3rd sofas are below). - The second sofa has *cnt*<sub class="lower-index">*l*</sub> = 2, *cnt*<sub class="lower-index">*r*</sub> = 1, *cnt*<sub class="lower-index">*t*</sub> = 2 and *cnt*<sub class="lower-index">*b*</sub> = 0. - The third sofa has *cnt*<sub class="lower-index">*l*</sub> = 2, *cnt*<sub class="lower-index">*r*</sub> = 1, *cnt*<sub class="lower-index">*t*</sub> = 1 and *cnt*<sub class="lower-index">*b*</sub> = 1.
So the second one corresponds to the given conditions.
In the third example
- The first sofa has *cnt*<sub class="lower-index">*l*</sub> = 1, *cnt*<sub class="lower-index">*r*</sub> = 1, *cnt*<sub class="lower-index">*t*</sub> = 0 and *cnt*<sub class="lower-index">*b*</sub> = 1. - The second sofa has *cnt*<sub class="lower-index">*l*</sub> = 1, *cnt*<sub class="lower-index">*r*</sub> = 1, *cnt*<sub class="lower-index">*t*</sub> = 1 and *cnt*<sub class="lower-index">*b*</sub> = 0.
And there is no sofa with the set (1, 0, 0, 0) so the answer is -1. | [
{
"input": "2\n3 2\n3 1 3 2\n1 2 2 2\n1 0 0 1",
"output": "1"
},
{
"input": "3\n10 10\n1 2 1 1\n5 5 6 5\n6 4 5 4\n2 1 2 0",
"output": "2"
},
{
"input": "2\n2 2\n2 1 1 1\n1 2 2 2\n1 0 0 0",
"output": "-1"
},
{
"input": "1\n1 2\n1 1 1 2\n0 0 0 0",
"output": "1"
},
{
... | 498 | 17,305,600 | 3 | 12,192 | |
506 | Mr. Kitayuta's Colorful Graph | [
"brute force",
"dfs and similar",
"dsu",
"graphs"
] | null | null | Mr. Kitayuta has just bought an undirected graph with *n* vertices and *m* edges. The vertices of the graph are numbered from 1 to *n*. Each edge, namely edge *i*, has a color *c**i*, connecting vertex *a**i* and *b**i*.
Mr. Kitayuta wants you to process the following *q* queries.
In the *i*-th query, he gives you two integers - *u**i* and *v**i*.
Find the number of the colors that satisfy the following condition: the edges of that color connect vertex *u**i* and vertex *v**i* directly or indirectly. | The first line of the input contains space-separated two integers - *n* and *m*(2<=≤<=*n*<=≤<=105,<=1<=≤<=*m*<=≤<=105), denoting the number of the vertices and the number of the edges, respectively.
The next *m* lines contain space-separated three integers - *a**i*, *b**i*(1<=≤<=*a**i*<=<<=*b**i*<=≤<=*n*) and *c**i*(1<=≤<=*c**i*<=≤<=*m*). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if *i*<=≠<=*j*,<=(*a**i*,<=*b**i*,<=*c**i*)<=≠<=(*a**j*,<=*b**j*,<=*c**j*).
The next line contains a integer- *q*(1<=≤<=*q*<=≤<=105), denoting the number of the queries.
Then follows *q* lines, containing space-separated two integers - *u**i* and *v**i*(1<=≤<=*u**i*,<=*v**i*<=≤<=*n*). It is guaranteed that *u**i*<=≠<=*v**i*. | For each query, print the answer in a separate line. | [
"4 5\n1 2 1\n1 2 2\n2 3 1\n2 3 3\n2 4 3\n3\n1 2\n3 4\n1 4\n",
"5 7\n1 5 1\n2 5 1\n3 5 1\n4 5 1\n1 2 2\n2 3 2\n3 4 2\n5\n1 5\n5 1\n2 5\n1 5\n1 4\n"
] | [
"2\n1\n0\n",
"1\n1\n1\n1\n2\n"
] | Let's consider the first sample.
- Vertex 1 and vertex 2 are connected by color 1 and 2. - Vertex 3 and vertex 4 are connected by color 3. - Vertex 1 and vertex 4 are not connected by any single color. | [
{
"input": "4 5\n1 2 1\n1 2 2\n2 3 1\n2 3 3\n2 4 3\n3\n1 2\n3 4\n1 4",
"output": "2\n1\n0"
},
{
"input": "5 7\n1 5 1\n2 5 1\n3 5 1\n4 5 1\n1 2 2\n2 3 2\n3 4 2\n5\n1 5\n5 1\n2 5\n1 5\n1 4",
"output": "1\n1\n1\n1\n2"
},
{
"input": "2 1\n1 2 1\n1\n1 2",
"output": "1"
},
{
"input... | 0 | 0 | -1 | 12,214 | |
0 | none | [
"none"
] | null | null | You are given *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Denote this list of integers as *T*.
Let *f*(*L*) be a function that takes in a non-empty list of integers *L*.
The function will output another integer as follows:
- First, all integers in *L* are padded with leading zeros so they are all the same length as the maximum length number in *L*. - We will construct a string where the *i*-th character is the minimum of the *i*-th character in padded input numbers. - The output is the number representing the string interpreted in base 10.
For example *f*(10,<=9)<==<=0, *f*(123,<=321)<==<=121, *f*(530,<=932,<=81)<==<=30.
Define the function
In other words, *G*(*x*) is the sum of squares of sum of elements of nonempty subsequences of *T* that evaluate to *x* when plugged into *f* modulo 1<=000<=000<=007, then multiplied by *x*. The last multiplication is not modded.
You would like to compute *G*(0),<=*G*(1),<=...,<=*G*(999<=999). To reduce the output size, print the value , where denotes the bitwise XOR operator. | The first line contains the integer *n* (1<=≤<=*n*<=≤<=1<=000<=000) — the size of list *T*.
The next line contains *n* space-separated integers, *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=999<=999) — the elements of the list. | Output a single integer, the answer to the problem. | [
"3\n123 321 555\n",
"1\n999999\n",
"10\n1 1 1 1 1 1 1 1 1 1\n"
] | [
"292711924\n",
"997992010006992\n",
"28160\n"
] | For the first sample, the nonzero values of *G* are *G*(121) = 144 611 577, *G*(123) = 58 401 999, *G*(321) = 279 403 857, *G*(555) = 170 953 875. The bitwise XOR of these numbers is equal to 292 711 924.
For example, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/53a1c46c7cd2d3d56e89fe99af6328601758b327.png" style="max-width: 100.0%;max-height: 100.0%;"/>, since the subsequences [123] and [123, 555] evaluate to 123 when plugged into *f*.
For the second sample, we have <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/330ab8a9387bb33a82877190f4439f4e9ccd8e92.png" style="max-width: 100.0%;max-height: 100.0%;"/>
For the last sample, we have <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/0e0eeb7662af22bf04ea1a2ea669b162d53ad7ba.png" style="max-width: 100.0%;max-height: 100.0%;"/>, where <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4f42013cbab8c31325f2cf28b60aa2ccc1cf89d3.png" style="max-width: 100.0%;max-height: 100.0%;"/> is the binomial coefficient. | [
{
"input": "3\n123 321 555",
"output": "292711924"
},
{
"input": "1\n999999",
"output": "997992010006992"
},
{
"input": "10\n1 1 1 1 1 1 1 1 1 1",
"output": "28160"
},
{
"input": "3\n534185 663311 372491",
"output": "1081839767962341"
},
{
"input": "10\n595436 647... | 3,000 | 11,980,800 | 0 | 12,219 | |
955 | Scissors | [
"brute force",
"strings"
] | null | null | Jenya has recently acquired quite a useful tool — *k*-scissors for cutting strings. They are generally used for cutting out two non-intersecting substrings of length *k* from an arbitrary string *s* (its length should be at least 2·*k* in order to perform this operation) and concatenating them afterwards (preserving the initial order). For example, with the help of 2-scissors you can cut *ab* and *de* out of *abcde* and concatenate them into *abde*, but not *ab* and *bc* since they're intersecting.
It's a nice idea to test this tool before using it in practice. After looking through the papers, Jenya came up with two strings *s* and *t*. His question is whether it is possible to apply his scissors to string *s* such that the resulting concatenation contains *t* as a substring? | The first line contains three integers *n*, *m*, *k* (2<=≤<=*m*<=≤<=2·*k*<=≤<=*n*<=≤<=5·105) — length of *s*, length of *t* and the aforementioned scissors' parameter correspondingly.
The next two lines feature *s* and *t* consisting of lowercase latin letters. | If there is no answer, print «No».
Otherwise print «Yes» and two integers *L* and *R* denoting the indexes where cutted substrings start (1-indexed). If there are several possible answers, output any. | [
"7 4 3\nbaabaab\naaaa\n",
"6 3 2\ncbcbcb\nbcc\n",
"7 5 3\naabbaaa\naaaaa\n"
] | [
"Yes\n1 5\n",
"Yes\n2 5\n",
"No\n"
] | In the first sample case you can cut out two substrings starting at 1 and 5. The resulting string baaaab contains aaaa as a substring.
In the second sample case the resulting string is bccb. | [
{
"input": "7 4 3\nbaabaab\naaaa",
"output": "Yes\n1 5"
},
{
"input": "6 3 2\ncbcbcb\nbcc",
"output": "Yes\n2 5"
},
{
"input": "7 5 3\naabbaaa\naaaaa",
"output": "No"
},
{
"input": "16 6 4\nabcdcadbbbcacdca\ndcadbc",
"output": "Yes\n4 10"
},
{
"input": "81 5 3\nac... | 78 | 6,963,200 | 0 | 12,240 | |
433 | Ryouko's Memory Note | [
"implementation",
"math",
"sortings"
] | null | null | Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory.
Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work.
Ryouko's notebook consists of *n* pages, numbered from 1 to *n*. To make life (and this problem) easier, we consider that to turn from page *x* to page *y*, |*x*<=-<=*y*| pages should be turned. During analyzing, Ryouko needs *m* pieces of information, the *i*-th piece of information is on page *a**i*. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is .
Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page *x* to page *y*, she would copy all the information on page *x* to *y* (1<=≤<=*x*,<=*y*<=≤<=*n*), and consequently, all elements in sequence *a* that was *x* would become *y*. Note that *x* can be equal to *y*, in which case no changes take place.
Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers. | The first line of input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105).
The next line contains *m* integers separated by spaces: *a*1,<=*a*2,<=...,<=*a**m* (1<=≤<=*a**i*<=≤<=*n*). | Print a single integer — the minimum number of pages Ryouko needs to turn. | [
"4 6\n1 2 3 4 3 2\n",
"10 5\n9 4 3 8 8\n"
] | [
"3\n",
"6\n"
] | In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence *a* becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3.
In the second sample, optimal solution is achieved by merging page 9 to 4. | [
{
"input": "4 6\n1 2 3 4 3 2",
"output": "3"
},
{
"input": "10 5\n9 4 3 8 8",
"output": "6"
},
{
"input": "5 10\n2 5 2 2 3 5 3 2 1 3",
"output": "7"
},
{
"input": "10 20\n6 3 9 6 1 9 1 9 8 2 7 6 9 8 4 7 1 2 4 2",
"output": "52"
},
{
"input": "100 100\n28 28 28 28 ... | 78 | 512,000 | 0 | 12,281 | |
959 | Mahmoud and Ehab and the xor-MST | [
"bitmasks",
"dp",
"graphs",
"implementation",
"math"
] | null | null | Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of *n* vertices numbered from 0 to *n*<=-<=1. For all 0<=≤<=*u*<=<<=*v*<=<<=*n*, vertex *u* and vertex *v* are connected with an undirected edge that has weight (where is the [bitwise-xor operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)). Can you find the weight of the minimum spanning tree of that graph?
You can read about complete graphs in [https://en.wikipedia.org/wiki/Complete_graph](https://en.wikipedia.org/wiki/Complete_graph)
You can read about the minimum spanning tree in [https://en.wikipedia.org/wiki/Minimum_spanning_tree](https://en.wikipedia.org/wiki/Minimum_spanning_tree)
The weight of the minimum spanning tree is the sum of the weights on the edges included in it. | The only line contains an integer *n* (2<=≤<=*n*<=≤<=1012), the number of vertices in the graph. | The only line contains an integer *x*, the weight of the graph's minimum spanning tree. | [
"4\n"
] | [
"4"
] | In the first sample: <img class="tex-graphics" src="https://espresso.codeforces.com/20e1655a0ec8e8d788bd2f5af92f93c968c65f3c.png" style="max-width: 100.0%;max-height: 100.0%;"/> The weight of the minimum spanning tree is 1+2+1=4. | [
{
"input": "4",
"output": "4"
},
{
"input": "10",
"output": "21"
},
{
"input": "2",
"output": "1"
},
{
"input": "1000000000000",
"output": "20140978692096"
},
{
"input": "999999999999",
"output": "20140978692095"
},
{
"input": "23131234",
"output":... | 46 | 0 | -1 | 12,320 | |
32 | Flea | [
"math"
] | C. Flea | 2 | 256 | It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to *s* centimeters. A flea has found herself at the center of some cell of the checked board of the size *n*<=×<=*m* centimeters (each cell is 1<=×<=1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.
The flea can count the amount of cells that she can reach from the starting position (*x*,<=*y*). Let's denote this amount by *d**x*,<=*y*. Your task is to find the number of such starting positions (*x*,<=*y*), which have the maximum possible value of *d**x*,<=*y*. | The first line contains three integers *n*, *m*, *s* (1<=≤<=*n*,<=*m*,<=*s*<=≤<=106) — length of the board, width of the board and length of the flea's jump. | Output the only integer — the number of the required starting positions of the flea. | [
"2 3 1000000\n",
"3 3 2\n"
] | [
"6\n",
"4\n"
] | none | [
{
"input": "2 3 1000000",
"output": "6"
},
{
"input": "3 3 2",
"output": "4"
},
{
"input": "1 2 3",
"output": "2"
},
{
"input": "4 5 6",
"output": "20"
},
{
"input": "9 8 7",
"output": "8"
},
{
"input": "1000 1000 1000",
"output": "1000000"
},
... | 92 | 0 | 3.977 | 12,358 |
87 | Trains | [
"implementation",
"math"
] | A. Trains | 2 | 256 | Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence.
When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time, waits the first train to come and rides on it to the end of the branch to the corresponding girl. However, the trains run with different frequencies: a train goes to Dasha's direction every *a* minutes, but a train goes to Masha's direction every *b* minutes. If two trains approach at the same time, Vasya goes toward the direction with the lower frequency of going trains, that is, to the girl, to whose directions the trains go less frequently (see the note to the third sample).
We know that the trains begin to go simultaneously before Vasya appears. That is the train schedule is such that there exists a moment of time when the two trains arrive simultaneously.
Help Vasya count to which girlfriend he will go more often. | The first line contains two integers *a* and *b* (*a*<=≠<=*b*,<=1<=≤<=*a*,<=*b*<=≤<=106). | Print "Dasha" if Vasya will go to Dasha more frequently, "Masha" if he will go to Masha more frequently, or "Equal" if he will go to both girlfriends with the same frequency. | [
"3 7\n",
"5 3\n",
"2 3\n"
] | [
"Dasha\n",
"Masha\n",
"Equal\n"
] | Let's take a look at the third sample. Let the trains start to go at the zero moment of time. It is clear that the moments of the trains' arrival will be periodic with period 6. That's why it is enough to show that if Vasya descends to the subway at a moment of time inside the interval (0, 6], he will go to both girls equally often.
If he descends to the subway at a moment of time from 0 to 2, he leaves for Dasha on the train that arrives by the second minute.
If he descends to the subway at a moment of time from 2 to 3, he leaves for Masha on the train that arrives by the third minute.
If he descends to the subway at a moment of time from 3 to 4, he leaves for Dasha on the train that arrives by the fourth minute.
If he descends to the subway at a moment of time from 4 to 6, he waits for both trains to arrive by the sixth minute and goes to Masha as trains go less often in Masha's direction.
In sum Masha and Dasha get equal time — three minutes for each one, thus, Vasya will go to both girlfriends equally often. | [
{
"input": "3 7",
"output": "Dasha"
},
{
"input": "5 3",
"output": "Masha"
},
{
"input": "2 3",
"output": "Equal"
},
{
"input": "31 88",
"output": "Dasha"
},
{
"input": "8 75",
"output": "Dasha"
},
{
"input": "32 99",
"output": "Dasha"
},
{
... | 124 | 0 | 3.969 | 12,385 |
999 | Reachability from the Capital | [
"dfs and similar",
"graphs",
"greedy"
] | null | null | There are $n$ cities and $m$ roads in Berland. Each road connects a pair of cities. The roads in Berland are one-way.
What is the minimum number of new roads that need to be built to make all the cities reachable from the capital?
New roads will also be one-way. | The first line of input consists of three integers $n$, $m$ and $s$ ($1 \le n \le 5000, 0 \le m \le 5000, 1 \le s \le n$) — the number of cities, the number of roads and the index of the capital. Cities are indexed from $1$ to $n$.
The following $m$ lines contain roads: road $i$ is given as a pair of cities $u_i$, $v_i$ ($1 \le u_i, v_i \le n$, $u_i \ne v_i$). For each pair of cities $(u, v)$, there can be at most one road from $u$ to $v$. Roads in opposite directions between a pair of cities are allowed (i.e. from $u$ to $v$ and from $v$ to $u$). | Print one integer — the minimum number of extra roads needed to make all the cities reachable from city $s$. If all the cities are already reachable from $s$, print 0. | [
"9 9 1\n1 2\n1 3\n2 3\n1 5\n5 6\n6 1\n1 8\n9 8\n7 1\n",
"5 4 5\n1 2\n2 3\n3 4\n4 1\n"
] | [
"3\n",
"1\n"
] | The first example is illustrated by the following:
For example, you can add roads ($6, 4$), ($7, 9$), ($1, 7$) to make all the cities reachable from $s = 1$.
The second example is illustrated by the following:
In this example, you can add any one of the roads ($5, 1$), ($5, 2$), ($5, 3$), ($5, 4$) to make all the cities reachable from $s = 5$. | [
{
"input": "9 9 1\n1 2\n1 3\n2 3\n1 5\n5 6\n6 1\n1 8\n9 8\n7 1",
"output": "3"
},
{
"input": "5 4 5\n1 2\n2 3\n3 4\n4 1",
"output": "1"
},
{
"input": "5000 0 2956",
"output": "4999"
},
{
"input": "2 0 2",
"output": "1"
},
{
"input": "2 1 1\n1 2",
"output": "0"... | 46 | 204,800 | 0 | 12,388 | |
883 | Downloading B++ | [
"binary search",
"implementation"
] | null | null | Only *T* milliseconds left before the start of well-known online programming contest Codehorses Round 2017.
Polycarp needs to download B++ compiler to take part in the contest. The size of the file is *f* bytes.
Polycarp's internet tariff allows to download data at the rate of one byte per *t*0 milliseconds. This tariff is already prepaid, and its use does not incur any expense for Polycarp. In addition, the Internet service provider offers two additional packages:
- download *a*1 bytes at the rate of one byte per *t*1 milliseconds, paying *p*1 burles for the package; - download *a*2 bytes at the rate of one byte per *t*2 milliseconds, paying *p*2 burles for the package.
Polycarp can buy any package many times. When buying a package, its price (*p*1 or *p*2) is prepaid before usage. Once a package is bought it replaces the regular tariff until package data limit is completely used. After a package is consumed Polycarp can immediately buy a new package or switch to the regular tariff without loosing any time. While a package is in use Polycarp can't buy another package or switch back to the regular internet tariff.
Find the minimum amount of money Polycarp has to spend to download an *f* bytes file no more than in *T* milliseconds.
Note that because of technical reasons Polycarp can download only integer number of bytes using regular tariff and both packages. I.e. in each of three downloading modes the number of downloaded bytes will be integer. It means that Polycarp can't download a byte partially using the regular tariff or/and both packages. | The first line contains three integer numbers *f*, *T* and *t*0 (1<=≤<=*f*,<=*T*,<=*t*0<=≤<=107) — size of the file to download (in bytes), maximal time to download the file (in milliseconds) and number of milliseconds to download one byte using the regular internet tariff.
The second line contains a description of the first additional package. The line contains three integer numbers *a*1, *t*1 and *p*1 (1<=≤<=*a*1,<=*t*1,<=*p*1<=≤<=107), where *a*1 is maximal sizes of downloaded data (in bytes), *t*1 is time to download one byte (in milliseconds), *p*1 is price of the package (in burles).
The third line contains a description of the second additional package. The line contains three integer numbers *a*2, *t*2 and *p*2 (1<=≤<=*a*2,<=*t*2,<=*p*2<=≤<=107), where *a*2 is maximal sizes of downloaded data (in bytes), *t*2 is time to download one byte (in milliseconds), *p*2 is price of the package (in burles).
Polycarp can buy any package many times. Once package is bought it replaces the regular tariff until package data limit is completely used. While a package is in use Polycarp can't buy another package or switch back to the regular internet tariff. | Print the minimum amount of money that Polycarp needs to pay to download B++ compiler no more than in *T* milliseconds. If there is no solution, print the only integer -1. | [
"120 964 20\n26 8 8\n13 10 4\n",
"10 200 20\n1 1 1\n2 2 3\n",
"8 81 11\n4 10 16\n3 10 12\n",
"8 79 11\n4 10 16\n3 10 12\n"
] | [
"40\n",
"0\n",
"28\n",
"-1\n"
] | In the first example Polycarp has to buy the first additional package 5 times and do not buy the second additional package. He downloads 120 bytes (of total 26·5 = 130 bytes) in 120·8 = 960 milliseconds (960 ≤ 964). He spends 8·5 = 40 burles on it.
In the second example Polycarp has enough time to download 10 bytes. It takes 10·20 = 200 milliseconds which equals to upper constraint on download time.
In the third example Polycarp has to buy one first additional package and one second additional package.
In the fourth example Polycarp has no way to download the file on time. | [
{
"input": "120 964 20\n26 8 8\n13 10 4",
"output": "40"
},
{
"input": "10 200 20\n1 1 1\n2 2 3",
"output": "0"
},
{
"input": "8 81 11\n4 10 16\n3 10 12",
"output": "28"
},
{
"input": "8 79 11\n4 10 16\n3 10 12",
"output": "-1"
},
{
"input": "62 10000 209\n95 106 ... | 46 | 0 | 0 | 12,395 | |
436 | Dungeons and Candies | [
"dsu",
"graphs",
"greedy",
"trees"
] | null | null | During the loading of the game "Dungeons and Candies" you are required to get descriptions of *k* levels from the server. Each description is a map of an *n*<=×<=*m* checkered rectangular field. Some cells of the field contain candies (each cell has at most one candy). An empty cell is denoted as "." on the map, but if a cell has a candy, it is denoted as a letter of the English alphabet. A level may contain identical candies, in this case the letters in the corresponding cells of the map will be the same.
When you transmit information via a network, you want to minimize traffic — the total size of the transferred data. The levels can be transmitted in any order. There are two ways to transmit the current level *A*:
1. You can transmit the whole level *A*. Then you need to transmit *n*·*m* bytes via the network. 1. You can transmit the difference between level *A* and some previously transmitted level *B* (if it exists); this operation requires to transmit *d**A*,<=*B*·*w* bytes, where *d**A*,<=*B* is the number of cells of the field that are different for *A* and *B*, and *w* is a constant. Note, that you should compare only the corresponding cells of levels *A* and *B* to calculate *d**A*,<=*B*. You cannot transform the maps of levels, i.e. rotate or shift them relatively to each other.
Your task is to find a way to transfer all the *k* levels and minimize the traffic. | The first line contains four integers *n*,<=*m*,<=*k*,<=*w* (1<=≤<=*n*,<=*m*<=≤<=10; 1<=≤<=*k*,<=*w*<=≤<=1000). Then follows the description of *k* levels. Each level is described by *n* lines, each line contains *m* characters. Each character is either a letter of the English alphabet or a dot ("."). Please note that the case of the letters matters. | In the first line print the required minimum number of transferred bytes.
Then print *k* pairs of integers *x*1,<=*y*1,<=*x*2,<=*y*2,<=...,<=*x**k*,<=*y**k*, describing the way to transfer levels. Pair *x**i*, *y**i* means that level *x**i* needs to be transferred by way *y**i*. If *y**i* equals 0, that means that the level must be transferred using the first way, otherwise *y**i* must be equal to the number of a previously transferred level. It means that you will transfer the difference between levels *y**i* and *x**i* to transfer level *x**i*. Print the pairs in the order of transferring levels. The levels are numbered 1 through *k* in the order they follow in the input.
If there are multiple optimal solutions, you can print any of them. | [
"2 3 3 2\nA.A\n...\nA.a\n..C\nX.Y\n...\n",
"1 1 4 1\nA\n.\nB\n.\n",
"1 3 5 2\nABA\nBBB\nBBA\nBAB\nABB\n"
] | [
"14\n1 0\n2 1\n3 1\n",
"3\n1 0\n2 0\n4 2\n3 0\n",
"11\n1 0\n3 1\n2 3\n4 2\n5 1\n"
] | none | [
{
"input": "1 1 4 1\nA\n.\nB\n.",
"output": "3\n1 0\n2 0\n4 2\n3 0"
},
{
"input": "1 3 5 2\nABA\nBBB\nBBA\nBAB\nABB",
"output": "11\n1 0\n3 1\n2 3\n4 2\n5 1"
},
{
"input": "2 2 5 1\n..\nBA\n.A\nB.\n..\nA.\nAB\n.B\n..\n..",
"output": "12\n1 0\n2 1\n3 1\n5 3\n4 5"
},
{
"input":... | 108 | 307,200 | 0 | 12,421 | |
237 | Young Table | [
"implementation",
"sortings"
] | null | null | You've got table *a*, consisting of *n* rows, numbered from 1 to *n*. The *i*-th line of table *a* contains *c**i* cells, at that for all *i* (1<=<<=*i*<=≤<=*n*) holds *c**i*<=≤<=*c**i*<=-<=1.
Let's denote *s* as the total number of cells of table *a*, that is, . We know that each cell of the table contains a single integer from 1 to *s*, at that all written integers are distinct.
Let's assume that the cells of the *i*-th row of table *a* are numbered from 1 to *c**i*, then let's denote the number written in the *j*-th cell of the *i*-th row as *a**i*,<=*j*. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions:
1. for all *i*,<=*j* (1<=<<=*i*<=≤<=*n*; 1<=≤<=*j*<=≤<=*c**i*) holds *a**i*,<=*j*<=><=*a**i*<=-<=1,<=*j*; 1. for all *i*,<=*j* (1<=≤<=*i*<=≤<=*n*; 1<=<<=*j*<=≤<=*c**i*) holds *a**i*,<=*j*<=><=*a**i*,<=*j*<=-<=1.
In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap.
Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than *s*. You do not have to minimize the number of operations. | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=50) that shows the number of rows in the table. The second line contains *n* space-separated integers *c**i* (1<=≤<=*c**i*<=≤<=50; *c**i*<=≤<=*c**i*<=-<=1) — the numbers of cells on the corresponding rows.
Next *n* lines contain table *а*. The *i*-th of them contains *c**i* space-separated integers: the *j*-th integer in this line represents *a**i*,<=*j*.
It is guaranteed that all the given numbers *a**i*,<=*j* are positive and do not exceed *s*. It is guaranteed that all *a**i*,<=*j* are distinct. | In the first line print a single integer *m* (0<=≤<=*m*<=≤<=*s*), representing the number of performed swaps.
In the next *m* lines print the description of these swap operations. In the *i*-th line print four space-separated integers *x**i*,<=*y**i*,<=*p**i*,<=*q**i* (1<=≤<=*x**i*,<=*p**i*<=≤<=*n*; 1<=≤<=*y**i*<=≤<=*c**x**i*; 1<=≤<=*q**i*<=≤<=*c**p**i*). The printed numbers denote swapping the contents of cells *a**x**i*,<=*y**i* and *a**p**i*,<=*q**i*. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. | [
"3\n3 2 1\n4 3 5\n6 1\n2\n",
"1\n4\n4 3 2 1\n"
] | [
"2\n1 1 2 2\n2 1 3 1\n",
"2\n1 1 1 4\n1 2 1 3\n"
] | none | [
{
"input": "3\n3 2 1\n4 3 5\n6 1\n2",
"output": "2\n1 1 2 2\n2 1 3 1"
},
{
"input": "1\n4\n4 3 2 1",
"output": "2\n1 1 1 4\n1 2 1 3"
},
{
"input": "5\n4 4 3 3 1\n14 13 4 15\n11 1 2 5\n7 6 10\n8 9 3\n12",
"output": "13\n1 1 2 2\n1 2 2 3\n1 3 4 3\n1 4 4 3\n2 1 2 4\n2 2 3 2\n2 3 3 1\n2 ... | 248 | 0 | 0 | 12,423 | |
393 | Three matrices | [] | null | null | Chubby Yang is studying linear equations right now. He came up with a nice problem. In the problem you are given an *n*<=×<=*n* matrix *W*, consisting of integers, and you should find two *n*<=×<=*n* matrices *A* and *B*, all the following conditions must hold:
- *A**ij*<==<=*A**ji*, for all *i*,<=*j* (1<=≤<=*i*,<=*j*<=≤<=*n*); - *B**ij*<==<=<=-<=*B**ji*, for all *i*,<=*j* (1<=≤<=*i*,<=*j*<=≤<=*n*); - *W**ij*<==<=*A**ij*<=+<=*B**ij*, for all *i*,<=*j* (1<=≤<=*i*,<=*j*<=≤<=*n*).
Can you solve the problem? | The first line contains an integer *n* (1<=≤<=*n*<=≤<=170). Each of the following *n* lines contains *n* integers. The *j*-th integer in the *i*-th line is *W**ij* (0<=≤<=|*W**ij*|<=<<=1717). | The first *n* lines must contain matrix *A*. The next *n* lines must contain matrix *B*. Print the matrices in the format equal to format of matrix *W* in input. It is guaranteed that the answer exists. If there are multiple answers, you are allowed to print any of them.
The answer will be considered correct if the absolute or relative error doesn't exceed 10<=-<=4. | [
"2\n1 4\n3 2\n",
"3\n1 2 3\n4 5 6\n7 8 9\n"
] | [
"1.00000000 3.50000000\n3.50000000 2.00000000\n0.00000000 0.50000000\n-0.50000000 0.00000000\n",
"1.00000000 3.00000000 5.00000000\n3.00000000 5.00000000 7.00000000\n5.00000000 7.00000000 9.00000000\n0.00000000 -1.00000000 -2.00000000\n1.00000000 0.00000000 -1.00000000\n2.00000000 1.00000000 0.00000000\n"
] | none | [
{
"input": "2\n1 4\n3 2",
"output": "1.00000000 3.50000000\n3.50000000 2.00000000\n0.00000000 0.50000000\n-0.50000000 0.00000000"
},
{
"input": "3\n1 2 3\n4 5 6\n7 8 9",
"output": "1.00000000 3.00000000 5.00000000\n3.00000000 5.00000000 7.00000000\n5.00000000 7.00000000 9.00000000\n0.00000000 -1... | 233 | 3,481,600 | 3 | 12,425 | |
682 | Alyona and Strings | [
"dp",
"strings"
] | null | null | After returned from forest, Alyona started reading a book. She noticed strings *s* and *t*, lengths of which are *n* and *m* respectively. As usual, reading bored Alyona and she decided to pay her attention to strings *s* and *t*, which she considered very similar.
Alyona has her favourite positive integer *k* and because she is too small, *k* does not exceed 10. The girl wants now to choose *k* disjoint non-empty substrings of string *s* such that these strings appear as disjoint substrings of string *t* and in the same order as they do in string *s*. She is also interested in that their length is maximum possible among all variants.
Formally, Alyona wants to find a sequence of *k* non-empty strings *p*1,<=*p*2,<=*p*3,<=...,<=*p**k* satisfying following conditions:
- *s* can be represented as concatenation *a*1*p*1*a*2*p*2... *a**k**p**k**a**k*<=+<=1, where *a*1,<=*a*2,<=...,<=*a**k*<=+<=1 is a sequence of arbitrary strings (some of them may be possibly empty); - *t* can be represented as concatenation *b*1*p*1*b*2*p*2... *b**k**p**k**b**k*<=+<=1, where *b*1,<=*b*2,<=...,<=*b**k*<=+<=1 is a sequence of arbitrary strings (some of them may be possibly empty); - sum of the lengths of strings in sequence is maximum possible.
Please help Alyona solve this complicated problem and find at least the sum of the lengths of the strings in a desired sequence.
A substring of a string is a subsequence of consecutive characters of the string. | In the first line of the input three integers *n*, *m*, *k* (1<=≤<=*n*,<=*m*<=≤<=1000, 1<=≤<=*k*<=≤<=10) are given — the length of the string *s*, the length of the string *t* and Alyona's favourite number respectively.
The second line of the input contains string *s*, consisting of lowercase English letters.
The third line of the input contains string *t*, consisting of lowercase English letters. | In the only line print the only non-negative integer — the sum of the lengths of the strings in a desired sequence.
It is guaranteed, that at least one desired sequence exists. | [
"3 2 2\nabc\nab\n",
"9 12 4\nbbaaababb\nabbbabbaaaba\n"
] | [
"2\n",
"7\n"
] | The following image describes the answer for the second sample case: | [
{
"input": "3 2 2\nabc\nab",
"output": "2"
},
{
"input": "9 12 4\nbbaaababb\nabbbabbaaaba",
"output": "7"
},
{
"input": "11 11 4\naaababbabbb\nbbbaaaabaab",
"output": "7"
},
{
"input": "15 9 4\nababaaabbaaaabb\nbbaababbb",
"output": "8"
},
{
"input": "2 7 1\nbb\nb... | 467 | 307,200 | 0 | 12,439 | |
774 | Stepan's Series | [
"*special",
"dp"
] | null | null | Well, the series which Stepan watched for a very long time, ended. In total, the series had *n* episodes. For each of them, Stepan remembers either that he definitely has watched it, or that he definitely hasn't watched it, or he is unsure, has he watched this episode or not.
Stepan's dissatisfaction is the maximum number of consecutive series that Stepan did not watch.
Your task is to determine according to Stepan's memories if his dissatisfaction could be exactly equal to *k*. | The first line contains two integers *n* and *k* (1<=≤<=*n*<=≤<=100, 0<=≤<=*k*<=≤<=*n*) — the number of episodes in the series and the dissatisfaction which should be checked.
The second line contains the sequence which consists of *n* symbols "Y", "N" and "?". If the *i*-th symbol equals "Y", Stepan remembers that he has watched the episode number *i*. If the *i*-th symbol equals "N", Stepan remembers that he hasn't watched the epizode number *i*. If the *i*-th symbol equals "?", Stepan doesn't exactly remember if he has watched the episode number *i* or not. | If Stepan's dissatisfaction can be exactly equal to *k*, then print "YES" (without qoutes). Otherwise print "NO" (without qoutes). | [
"5 2\nNYNNY\n",
"6 1\n????NN\n"
] | [
"YES\n",
"NO\n"
] | In the first test Stepan remembers about all the episodes whether he has watched them or not. His dissatisfaction is 2, because he hasn't watch two episodes in a row — the episode number 3 and the episode number 4. The answer is "YES", because *k* = 2.
In the second test *k* = 1, Stepan's dissatisfaction is greater than or equal to 2 (because he remembers that he hasn't watch at least two episodes in a row — number 5 and number 6), even if he has watched the episodes from the first to the fourth, inclusive. | [
{
"input": "5 2\nNYNNY",
"output": "YES"
},
{
"input": "6 1\n????NN",
"output": "NO"
},
{
"input": "100 8\nNYNNY?YNNNNNN?NNNNNYNY?YYNYNN?NNNY??NNYNYNNNYNNNYNNNNNNNNY?NNNYNYN?NNNY?YY?NNYNN?NNNYNNYNNYN?NNYNYNN",
"output": "YES"
},
{
"input": "10 1\nNY???NY?Y?",
"output": "Y... | 62 | 5,529,600 | 0 | 12,452 | |
55 | Smallest number | [
"brute force"
] | B. Smallest number | 2 | 256 | Recently, Vladimir got bad mark in algebra again. To avoid such unpleasant events in future he decided to train his arithmetic skills. He wrote four integer numbers *a*, *b*, *c*, *d* on the blackboard. During each of the next three minutes he took two numbers from the blackboard (not necessarily adjacent) and replaced them with their sum or their product. In the end he got one number. Unfortunately, due to the awful memory he forgot that number, but he remembers four original numbers, sequence of the operations and his surprise because of the very small result. Help Vladimir remember the forgotten number: find the smallest number that can be obtained from the original numbers by the given sequence of operations. | First line contains four integers separated by space: 0<=≤<=*a*,<=*b*,<=*c*,<=*d*<=≤<=1000 — the original numbers. Second line contains three signs ('+' or '*' each) separated by space — the sequence of the operations in the order of performing. ('+' stands for addition, '*' — multiplication) | Output one integer number — the minimal result which can be obtained.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d). | [
"1 1 1 1\n+ + *\n",
"2 2 2 2\n* * +\n",
"1 2 3 4\n* + +\n"
] | [
"3\n",
"8\n",
"9\n"
] | none | [
{
"input": "1 1 1 1\n+ + *",
"output": "3"
},
{
"input": "2 2 2 2\n* * +",
"output": "8"
},
{
"input": "1 2 3 4\n* + +",
"output": "9"
},
{
"input": "15 1 3 1\n* * +",
"output": "18"
},
{
"input": "8 1 7 14\n+ + +",
"output": "30"
},
{
"input": "7 17 3... | 62 | 0 | 0 | 12,482 |
53 | Little Frog | [
"constructive algorithms"
] | C. Little Frog | 2 | 256 | Once upon a time a little frog whose name was Vasya decided to travel around his home swamp. Overall there are *n* mounds on the swamp, located on one line. The distance between the neighboring mounds is one meter. Vasya wants to visit all the mounds in one day; besides, he wants to visit each one exactly once. For that he makes a route plan, to decide the order in which to jump on the mounds. Vasya can pick any mound as the first one. He thinks it boring to jump two times at the same distance. That's why he wants any two jumps on his route to have different lengths. Help Vasya the Frog and make the plan for him. | The single line contains a number *n* (1<=≤<=*n*<=≤<=104) which is the number of mounds. | Print *n* integers *p**i* (1<=≤<=*p**i*<=≤<=*n*) which are the frog's route plan.
- All the *p**i*'s should be mutually different. - All the |*p**i*–*p**i*<=+<=1|'s should be mutually different (1<=≤<=*i*<=≤<=*n*<=-<=1).
If there are several solutions, output any. | [
"2\n",
"3\n"
] | [
"1 2 ",
"1 3 2 "
] | none | [
{
"input": "2",
"output": "1 2 "
},
{
"input": "3",
"output": "1 3 2 "
},
{
"input": "4",
"output": "1 4 2 3 "
},
{
"input": "5",
"output": "1 5 2 4 3 "
},
{
"input": "6",
"output": "1 6 2 5 3 4 "
},
{
"input": "1",
"output": "1 "
},
{
"inp... | 92 | 0 | 0 | 12,509 |
1,005 | Median on Segments (Permutations Edition) | [
"sortings"
] | null | null | You are given a permutation $p_1, p_2, \dots, p_n$. A permutation of length $n$ is a sequence such that each integer between $1$ and $n$ occurs exactly once in the sequence.
Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of the median of $p_l, p_{l+1}, \dots, p_r$ is exactly the given number $m$.
The median of a sequence is the value of the element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used.
For example, if $a=[4, 2, 7, 5]$ then its median is $4$ since after sorting the sequence, it will look like $[2, 4, 5, 7]$ and the left of two middle elements is equal to $4$. The median of $[7, 1, 2, 9, 6]$ equals $6$ since after sorting, the value $6$ will be in the middle of the sequence.
Write a program to find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of the median of $p_l, p_{l+1}, \dots, p_r$ is exactly the given number $m$. | The first line contains integers $n$ and $m$ ($1 \le n \le 2\cdot10^5$, $1 \le m \le n$) — the length of the given sequence and the required value of the median.
The second line contains a permutation $p_1, p_2, \dots, p_n$ ($1 \le p_i \le n$). Each integer between $1$ and $n$ occurs in $p$ exactly once. | Print the required number. | [
"5 4\n2 4 5 3 1\n",
"5 5\n1 2 3 4 5\n",
"15 8\n1 15 2 14 3 13 4 8 12 5 11 6 10 7 9\n"
] | [
"4\n",
"1\n",
"48\n"
] | In the first example, the suitable pairs of indices are: $(1, 3)$, $(2, 2)$, $(2, 3)$ and $(2, 4)$. | [
{
"input": "5 4\n2 4 5 3 1",
"output": "4"
},
{
"input": "5 5\n1 2 3 4 5",
"output": "1"
},
{
"input": "15 8\n1 15 2 14 3 13 4 8 12 5 11 6 10 7 9",
"output": "48"
},
{
"input": "1 1\n1",
"output": "1"
},
{
"input": "2 1\n1 2",
"output": "2"
},
{
"input... | 77 | 3,072,000 | -1 | 12,519 | |
0 | none | [
"none"
] | null | null | We'll call an array of *n* non-negative integers *a*[1],<=*a*[2],<=...,<=*a*[*n*] interesting, if it meets *m* constraints. The *i*-th of the *m* constraints consists of three integers *l**i*, *r**i*, *q**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*) meaning that value should be equal to *q**i*.
Your task is to find any interesting array of *n* elements or state that such array doesn't exist.
Expression *x*&*y* means the bitwise AND of numbers *x* and *y*. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and". | The first line contains two integers *n*, *m* (1<=≤<=*n*<=≤<=105, 1<=≤<=*m*<=≤<=105) — the number of elements in the array and the number of limits.
Each of the next *m* lines contains three integers *l**i*, *r**i*, *q**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*, 0<=≤<=*q**i*<=<<=230) describing the *i*-th limit. | If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print *n* integers *a*[1],<=*a*[2],<=...,<=*a*[*n*] (0<=≤<=*a*[*i*]<=<<=230) decribing the interesting array. If there are multiple answers, print any of them.
If the interesting array doesn't exist, print "NO" (without the quotes) in the single line. | [
"3 1\n1 3 3\n",
"3 2\n1 3 3\n1 3 2\n"
] | [
"YES\n3 3 3\n",
"NO\n"
] | none | [
{
"input": "3 1\n1 3 3",
"output": "YES\n3 3 3"
},
{
"input": "3 2\n1 3 3\n1 3 2",
"output": "NO"
},
{
"input": "3 2\n1 2 536870912\n2 3 536870911",
"output": "YES\n536870912 1073741823 536870911"
},
{
"input": "1 1\n1 1 10",
"output": "YES\n10"
},
{
"input": "1 2... | 77 | 1,843,200 | 0 | 12,561 | |
140 | New Year Contest | [
"greedy",
"sortings"
] | null | null | As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are *n* problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the *i*-th problem will take *a**i* minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the *i*-th problem always equals *a**i* minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum. | The first line contains an integer *n* (1<=≤<=*n*<=≤<=100) — the number of the problems. The next line contains *n* space-separated integers *a**i* (1<=≤<=*a**i*<=≤<=720) — each number shows how much time in minutes Gennady will spend writing a solution to the problem. | Print two integers — the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy. | [
"3\n30 330 720\n"
] | [
"2 10\n"
] | In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
Competitions by the given rules are held annually on the site http://b23.ru/3wvc | [
{
"input": "3\n30 330 720",
"output": "2 10"
},
{
"input": "1\n720",
"output": "0 0"
},
{
"input": "5\n100 200 300 400 500",
"output": "3 250"
},
{
"input": "7\n120 110 100 110 120 120 50",
"output": "6 420"
},
{
"input": "3\n350 340 360",
"output": "2 340"
... | 92 | 0 | 0 | 12,582 | |
909 | AND-permutations | [
"constructive algorithms"
] | null | null | Given an integer *N*, find two permutations:
1. Permutation *p* of numbers from 1 to *N* such that *p**i*<=≠<=*i* and *p**i*<=&<=*i*<==<=0 for all *i*<==<=1,<=2,<=...,<=*N*. 1. Permutation *q* of numbers from 1 to *N* such that *q**i*<=≠<=*i* and *q**i*<=&<=*i*<=≠<=0 for all *i*<==<=1,<=2,<=...,<=*N*.
& is the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND). | The input consists of one line containing a single integer *N* (1<=≤<=*N*<=≤<=105). | For each subtask, if the required permutation doesn't exist, output a single line containing the word "NO"; otherwise output the word "YES" in the first line and *N* elements of the permutation, separated by spaces, in the second line. If there are several possible permutations in a subtask, output any of them. | [
"3\n",
"6\n"
] | [
"NO\nNO\n",
"YES\n6 5 4 3 2 1 \nYES\n3 6 2 5 1 4\n"
] | none | [
{
"input": "3",
"output": "NO\nNO"
},
{
"input": "6",
"output": "YES\n6 5 4 3 2 1 \nYES\n3 6 2 5 1 4"
},
{
"input": "100000",
"output": "YES\n30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 32 31 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 7... | 62 | 5,529,600 | -1 | 12,586 | |
915 | Physical Education Lessons | [
"data structures",
"implementation",
"sortings"
] | null | null | This year Alex has finished school, and now he is a first-year student of Berland State University. For him it was a total surprise that even though he studies programming, he still has to attend physical education lessons. The end of the term is very soon, but, unfortunately, Alex still hasn't attended a single lesson!
Since Alex doesn't want to get expelled, he wants to know the number of working days left until the end of the term, so he can attend physical education lessons during these days. But in BSU calculating the number of working days is a complicated matter:
There are *n* days left before the end of the term (numbered from 1 to *n*), and initially all of them are working days. Then the university staff sequentially publishes *q* orders, one after another. Each order is characterised by three numbers *l*, *r* and *k*:
- If *k*<==<=1, then all days from *l* to *r* (inclusive) become non-working days. If some of these days are made working days by some previous order, then these days still become non-working days; - If *k*<==<=2, then all days from *l* to *r* (inclusive) become working days. If some of these days are made non-working days by some previous order, then these days still become working days.
Help Alex to determine the number of working days left after each order! | The first line contains one integer *n*, and the second line — one integer *q* (1<=≤<=*n*<=≤<=109, 1<=≤<=*q*<=≤<=3·105) — the number of days left before the end of the term, and the number of orders, respectively.
Then *q* lines follow, *i*-th line containing three integers *l**i*, *r**i* and *k**i* representing *i*-th order (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*, 1<=≤<=*k**i*<=≤<=2). | Print *q* integers. *i*-th of them must be equal to the number of working days left until the end of the term after the first *i* orders are published. | [
"4\n6\n1 2 1\n3 4 1\n2 3 2\n1 3 2\n2 4 1\n1 4 2\n"
] | [
"2\n0\n2\n3\n1\n4\n"
] | none | [
{
"input": "4\n6\n1 2 1\n3 4 1\n2 3 2\n1 3 2\n2 4 1\n1 4 2",
"output": "2\n0\n2\n3\n1\n4"
},
{
"input": "3\n8\n2 2 1\n3 3 2\n1 1 1\n1 3 2\n2 3 2\n3 3 1\n1 2 1\n2 2 2",
"output": "2\n2\n1\n3\n3\n2\n0\n1"
},
{
"input": "7\n10\n5 7 1\n5 6 2\n7 7 2\n6 7 2\n5 5 1\n3 6 2\n1 3 2\n5 6 1\n1 3 1\n... | 31 | 0 | 0 | 12,605 | |
845 | Shortest Path Problem? | [
"dfs and similar",
"graphs",
"math"
] | null | null | You are given an undirected graph with weighted edges. The length of some path between two vertices is the bitwise xor of weights of all edges belonging to this path (if some edge is traversed more than once, then it is included in bitwise xor the same number of times). You have to find the minimum length of path between vertex 1 and vertex *n*.
Note that graph can contain multiple edges and loops. It is guaranteed that the graph is connected. | The first line contains two numbers *n* and *m* (1<=≤<=*n*<=≤<=100000, *n*<=-<=1<=≤<=*m*<=≤<=100000) — the number of vertices and the number of edges, respectively.
Then *m* lines follow, each line containing three integer numbers *x*, *y* and *w* (1<=≤<=*x*,<=*y*<=≤<=*n*, 0<=≤<=*w*<=≤<=108). These numbers denote an edge that connects vertices *x* and *y* and has weight *w*. | Print one number — the minimum length of path between vertices 1 and *n*. | [
"3 3\n1 2 3\n1 3 2\n3 2 0\n",
"2 2\n1 1 3\n1 2 3\n"
] | [
"2\n",
"0\n"
] | none | [
{
"input": "3 3\n1 2 3\n1 3 2\n3 2 0",
"output": "2"
},
{
"input": "2 2\n1 1 3\n1 2 3",
"output": "0"
},
{
"input": "10 20\n8 5 64\n5 6 48\n4 5 91\n10 1 2\n3 4 51\n8 2 74\n6 1 98\n3 10 24\n2 10 35\n8 7 52\n10 5 72\n5 9 25\n2 9 65\n7 4 69\n5 7 26\n7 2 44\n6 8 61\n3 5 43\n10 7 33\n4 2 28",... | 30 | 0 | 0 | 12,619 | |
0 | none | [
"none"
] | null | null | Родители Васи хотят, чтобы он как можно лучше учился. Поэтому если он получает подряд три положительные оценки («четвёрки» или «пятёрки»), они дарят ему подарок. Соответственно, оценки «единица», «двойка» и «тройка» родители Васи считают плохими. Когда Вася получает подряд три хорошие оценки, ему сразу вручают подарок, но для того, чтобы получить ещё один подарок, ему вновь надо получить подряд ещё три хорошие оценки.
Например, если Вася получит подряд пять «четвёрок» оценок, а потом «двойку», то ему дадут только один подарок, а вот если бы «четвёрок» было уже шесть, то подарков было бы два.
За месяц Вася получил *n* оценок. Вам предстоит посчитать количество подарков, которые получил Вася. Оценки будут даны именно в том порядке, в котором Вася их получал. | В первой строке входных данных следует целое положительное число *n* (3<=≤<=*n*<=≤<=1000) — количество оценок, полученных Васей.
Во второй строке входных данных следует последовательность из *n* чисел *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=5) — оценки, полученные Васей. Оценки заданы в том порядке, в котором Вася их получил. | Выведите одно целое число — количество подарков, полученных Васей. | [
"6\n4 5 4 5 4 4\n",
"14\n1 5 4 5 2 4 4 5 5 4 3 4 5 5\n"
] | [
"2\n",
"3\n"
] | В первом примере Вася получит два подарка — за первые три положительные оценки и за следующую тройку положительных оценок соответственно. | [
{
"input": "6\n4 5 4 5 4 4",
"output": "2"
},
{
"input": "14\n1 5 4 5 2 4 4 5 5 4 3 4 5 5",
"output": "3"
},
{
"input": "3\n4 5 4",
"output": "1"
},
{
"input": "3\n4 5 1",
"output": "0"
},
{
"input": "4\n5 4 3 5",
"output": "0"
},
{
"input": "10\n4 4 5... | 30 | 4,608,000 | -1 | 12,637 | |
988 | Substrings Sort | [
"sortings",
"strings"
] | null | null | You are given $n$ strings. Each string consists of lowercase English letters. Rearrange (reorder) the given strings in such a way that for every string, all strings that are placed before it are its substrings.
String $a$ is a substring of string $b$ if it is possible to choose several consecutive letters in $b$ in such a way that they form $a$. For example, string "for" is contained as a substring in strings "codeforces", "for" and "therefore", but is not contained as a substring in strings "four", "fofo" and "rof". | The first line contains an integer $n$ ($1 \le n \le 100$) — the number of strings.
The next $n$ lines contain the given strings. The number of letters in each string is from $1$ to $100$, inclusive. Each string consists of lowercase English letters.
Some strings might be equal. | If it is impossible to reorder $n$ given strings in required order, print "NO" (without quotes).
Otherwise print "YES" (without quotes) and $n$ given strings in required order. | [
"5\na\naba\nabacaba\nba\naba\n",
"5\na\nabacaba\nba\naba\nabab\n",
"3\nqwerty\nqwerty\nqwerty\n"
] | [
"YES\na\nba\naba\naba\nabacaba\n",
"NO\n",
"YES\nqwerty\nqwerty\nqwerty\n"
] | In the second example you cannot reorder the strings because the string "abab" is not a substring of the string "abacaba". | [
{
"input": "5\na\naba\nabacaba\nba\naba",
"output": "YES\na\nba\naba\naba\nabacaba"
},
{
"input": "5\na\nabacaba\nba\naba\nabab",
"output": "NO"
},
{
"input": "3\nqwerty\nqwerty\nqwerty",
"output": "YES\nqwerty\nqwerty\nqwerty"
},
{
"input": "1\nwronganswer",
"output": "Y... | 31 | 0 | 0 | 12,646 | |
267 | Berland Traffic | [
"math",
"matrices"
] | null | null | Berland traffic is very different from traffic in other countries. The capital of Berland consists of *n* junctions and *m* roads. Each road connects a pair of junctions. There can be multiple roads between a pair of junctions. For each road we know its capacity: value *c**i* is the maximum number of cars that can drive along a road in any direction per a unit of time. For each road, the cars can drive along it in one of two direction. That it, the cars can't simultaneously move in both directions. A road's traffic is the number of cars that goes along it per a unit of time. For road (*a**i*,<=*b**i*) this value is negative, if the traffic moves from *b**i* to *a**i*. A road's traffic can be a non-integer number.
The capital has two special junctions — the entrance to the city (junction 1) and the exit from the city (junction *n*). For all other junctions it is true that the traffic is not lost there. That is, for all junctions except for 1 and *n* the incoming traffic sum equals the outgoing traffic sum.
Traffic has an unusual peculiarity in the capital of Berland — for any pair of junctions (*x*,<=*y*) the sum of traffics along any path from *x* to *y* doesn't change depending on the choice of the path. Such sum includes traffic along all roads on the path (possible with the "minus" sign, if the traffic along the road is directed against the direction of the road on the path from *x* to *y*).
Your task is to find the largest traffic that can pass trough the city per one unit of time as well as the corresponding traffic for each road. | The first line contains a positive integer *n* — the number of junctions (2<=≤<=*n*<=≤<=100). The second line contains integer *m* (1<=≤<=*m*<=≤<=5000) — the number of roads. Next *m* lines contain the roads' descriptions. Each road contains a group of three numbers *a**i*, *b**i*, *c**i*, where *a**i*,<=*b**i* are the numbers of junctions, connected by the given road, and *c**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *a**i*<=≠<=*b**i*; 0<=≤<=*c**i*<=≤<=10000) is the largest permissible traffic along this road. | In the first line print the required largest traffic across the city. Then print *m* lines, on each line print the speed, at which the traffic moves along the corresponding road. If the direction doesn't match the order of the junctions, given in the input, then print the traffic with the minus sign. Print the numbers with accuracy of at least five digits after the decimal point.
If there are many optimal solutions, print any of them. | [
"2\n3\n1 2 2\n1 2 4\n2 1 1000\n",
"7\n11\n1 2 7\n1 2 7\n1 3 7\n1 4 7\n2 3 7\n2 5 7\n3 6 7\n4 7 7\n5 4 7\n5 6 7\n6 7 7\n"
] | [
"6.00000\n2.00000\n2.00000\n-2.00000\n",
"13.00000\n2.00000\n2.00000\n3.00000\n6.00000\n1.00000\n3.00000\n4.00000\n7.00000\n1.00000\n2.00000\n6.00000\n"
] | none | [] | 62 | 0 | 0 | 12,651 | |
671 | Ultimate Weirdness of an Array | [
"data structures",
"number theory"
] | null | null | Yasin has an array *a* containing *n* integers. Yasin is a 5 year old, so he loves ultimate weird things.
Yasin denotes weirdness of an array as maximum *gcd*(*a**i*,<=<=*a**j*) value among all 1<=≤<=*i*<=<<=*j*<=≤<=*n*. For *n*<=≤<=1 weirdness is equal to 0, *gcd*(*x*,<=<=*y*) is the greatest common divisor of integers *x* and *y*.
He also defines the ultimate weirdness of an array. Ultimate weirdness is where *f*(*i*,<=<=*j*) is weirdness of the new array *a* obtained by removing all elements between *i* and *j* inclusive, so new array is [*a*1... *a**i*<=-<=1,<=*a**j*<=+<=1... *a**n*].
Since 5 year old boys can't code, Yasin asks for your help to find the value of ultimate weirdness of the given array *a*! | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=200<=000) — the number of elements in *a*.
The next line contains *n* integers *a**i* (1<=≤<=*a**i*<=≤<=200<=000), where the *i*-th number is equal to the *i*-th element of the array *a*. It is guaranteed that all *a**i* are distinct. | Print a single line containing the value of ultimate weirdness of the array *a*. | [
"3\n2 6 3\n"
] | [
"6\n"
] | Consider the first sample.
- *f*(1, 1) is equal to 3. - *f*(2, 2) is equal to 1. - *f*(3, 3) is equal to 2. - *f*(1, 2), *f*(1, 3) and *f*(2, 3) are equal to 0. | [] | 0 | 0 | -1 | 12,698 | |
0 | none | [
"none"
] | null | null | Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded.
The problem is the follows. Given *n* points in the plane, find a pair of points between which the distance is minimized. Distance between (*x*1,<=*y*1) and (*x*2,<=*y*2) is .
The pseudo code of the unexpected code is as follows:
Here, *tot* can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, *tot* should not be more than *k* in order not to get Time Limit Exceeded.
You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? | A single line which contains two space-separated integers *n* and *k* (2<=≤<=*n*<=≤<=2000, 1<=≤<=*k*<=≤<=109). | If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print *n* lines, and the *i*-th line contains two integers *x**i*,<=*y**i* (|*x**i*|,<=|*y**i*|<=≤<=109) representing the coordinates of the *i*-th point.
The conditions below must be held:
- All the points must be distinct. - |*x**i*|,<=|*y**i*|<=≤<=109. - After running the given code, the value of *tot* should be larger than *k*. | [
"4 3\n",
"2 100\n"
] | [
"0 0\n0 1\n1 0\n1 1\n",
"no solution\n"
] | none | [
{
"input": "4 3",
"output": "0 0\n0 1\n1 0\n1 1"
},
{
"input": "2 100",
"output": "no solution"
},
{
"input": "5 6",
"output": "0 0\n0 1\n0 2\n0 3\n0 4"
},
{
"input": "8 20",
"output": "0 0\n0 1\n0 2\n0 3\n0 4\n0 5\n0 6\n0 7"
},
{
"input": "6 15",
"output": "n... | 124 | 512,000 | 3 | 12,708 | |
126 | Hot Bath | [
"binary search",
"brute force",
"math"
] | null | null | Bob is about to take a hot bath.
There are two taps to fill the bath: a hot water tap and a cold water tap. The cold water's temperature is *t*1, and the hot water's temperature is *t*2. The cold water tap can transmit any integer number of water units per second from 0 to *x*1, inclusive. Similarly, the hot water tap can transmit from 0 to *x*2 water units per second.
If *y*1 water units per second flow through the first tap and *y*2 water units per second flow through the second tap, then the resulting bath water temperature will be:
Bob wants to open both taps so that the bath water temperature was not less than *t*0. However, the temperature should be as close as possible to this value. If there are several optimal variants, Bob chooses the one that lets fill the bath in the quickest way possible.
Determine how much each tap should be opened so that Bob was pleased with the result in the end. | You are given five integers *t*1, *t*2, *x*1, *x*2 and *t*0 (1<=≤<=*t*1<=≤<=*t*0<=≤<=*t*2<=≤<=106, 1<=≤<=*x*1,<=*x*2<=≤<=106). | Print two space-separated integers *y*1 and *y*2 (0<=≤<=*y*1<=≤<=*x*1, 0<=≤<=*y*2<=≤<=*x*2). | [
"10 70 100 100 25\n",
"300 500 1000 1000 300\n",
"143 456 110 117 273\n"
] | [
"99 33",
"1000 0",
"76 54"
] | In the second sample the hot water tap shouldn't be opened, but the cold water tap should be opened at full capacity in order to fill the bath in the quickest way possible. | [
{
"input": "10 70 100 100 25",
"output": "99 33"
},
{
"input": "300 500 1000 1000 300",
"output": "1000 0"
},
{
"input": "143 456 110 117 273",
"output": "76 54"
},
{
"input": "10 20 5 5 13",
"output": "4 2"
},
{
"input": "1 3 1999 3444 2",
"output": "1999 199... | 216 | 1,536,000 | 0 | 12,712 | |
382 | Arithmetic Progression | [
"implementation",
"sortings"
] | null | null | Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers *a*1,<=*a*2,<=...,<=*a**n* of length *n*, that the following condition fulfills:
For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not.
Alexander has *n* cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting *n*<=+<=1 cards to make an arithmetic progression (Alexander has to use all of his cards).
Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. | The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 108. | If Arthur can write infinitely many distinct integers on the card, print on a single line -1.
Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 108 or even be negative (see test samples). | [
"3\n4 1 7\n",
"1\n10\n",
"4\n1 3 5 9\n",
"4\n4 3 4 5\n",
"2\n2 4\n"
] | [
"2\n-2 10\n",
"-1\n",
"1\n7\n",
"0\n",
"3\n0 3 6\n"
] | none | [
{
"input": "3\n4 1 7",
"output": "2\n-2 10"
},
{
"input": "1\n10",
"output": "-1"
},
{
"input": "4\n1 3 5 9",
"output": "1\n7"
},
{
"input": "4\n4 3 4 5",
"output": "0"
},
{
"input": "2\n2 4",
"output": "3\n0 3 6"
},
{
"input": "4\n1 3 4 5",
"outpu... | 109 | 0 | 0 | 12,713 | |
460 | Little Dima and Equation | [
"brute force",
"implementation",
"math",
"number theory"
] | null | null | Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment.
Find all integer solutions *x* (0<=<<=*x*<=<<=109) of the equation:
where *a*, *b*, *c* are some predetermined constant values and function *s*(*x*) determines the sum of all digits in the decimal representation of number *x*.
The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: *a*, *b*, *c*. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. | The first line contains three space-separated integers: *a*,<=*b*,<=*c* (1<=≤<=*a*<=≤<=5; 1<=≤<=*b*<=≤<=10000; <=-<=10000<=≤<=*c*<=≤<=10000). | Print integer *n* — the number of the solutions that you've found. Next print *n* integers in the increasing order — the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 109. | [
"3 2 8\n",
"1 2 -18\n",
"2 2 -1\n"
] | [
"3\n10 2008 13726 ",
"0\n",
"4\n1 31 337 967 "
] | none | [
{
"input": "3 2 8",
"output": "3\n10 2008 13726 "
},
{
"input": "1 2 -18",
"output": "0"
},
{
"input": "2 2 -1",
"output": "4\n1 31 337 967 "
},
{
"input": "1 1 0",
"output": "9\n1 2 3 4 5 6 7 8 9 "
},
{
"input": "1 37 963",
"output": "16\n1000 1111 1222 1333 ... | 46 | 0 | 3 | 12,750 | |
700 | Cool Slogans | [
"string suffix structures",
"strings"
] | null | null | Bomboslav set up a branding agency and now helps companies to create new logos and advertising slogans. In term of this problems, slogan of the company should be a non-empty substring of its name. For example, if the company name is "hornsandhoofs", then substrings "sand" and "hor" could be its slogans, while strings "e" and "hornss" can not.
Sometimes the company performs rebranding and changes its slogan. Slogan *A* is considered to be cooler than slogan *B* if *B* appears in *A* as a substring at least twice (this occurrences are allowed to overlap). For example, slogan *A*<==<= "abacaba" is cooler than slogan *B*<==<= "ba", slogan *A*<==<= "abcbcbe" is cooler than slogan *B*<==<= "bcb", but slogan *A*<==<= "aaaaaa" is not cooler than slogan *B*<==<= "aba".
You are given the company name *w* and your task is to help Bomboslav determine the length of the longest sequence of slogans *s*1,<=*s*2,<=...,<=*s**k*, such that any slogan in the sequence is cooler than the previous one. | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=200<=000) — the length of the company name that asks Bomboslav to help. The second line contains the string *w* of length *n*, that consists of lowercase English letters. | Print a single integer — the maximum possible length of the sequence of slogans of the company named *w*, such that any slogan in the sequence (except the first one) is cooler than the previous | [
"3\nabc\n",
"5\nddddd\n",
"11\nabracadabra\n"
] | [
"1\n",
"5\n",
"3\n"
] | none | [] | 31 | 0 | 0 | 12,778 | |
978 | Petya's Exams | [
"greedy",
"implementation",
"sortings"
] | null | null | Petya studies at university. The current academic year finishes with $n$ special days. Petya needs to pass $m$ exams in those special days. The special days in this problem are numbered from $1$ to $n$.
There are three values about each exam:
- $s_i$ — the day, when questions for the $i$-th exam will be published, - $d_i$ — the day of the $i$-th exam ($s_i < d_i$), - $c_i$ — number of days Petya needs to prepare for the $i$-th exam. For the $i$-th exam Petya should prepare in days between $s_i$ and $d_i-1$, inclusive.
There are three types of activities for Petya in each day: to spend a day doing nothing (taking a rest), to spend a day passing exactly one exam or to spend a day preparing for exactly one exam. So he can't pass/prepare for multiple exams in a day. He can't mix his activities in a day. If he is preparing for the $i$-th exam in day $j$, then $s_i \le j < d_i$.
It is allowed to have breaks in a preparation to an exam and to alternate preparations for different exams in consecutive days. So preparation for an exam is not required to be done in consecutive days.
Find the schedule for Petya to prepare for all exams and pass them, or report that it is impossible. | The first line contains two integers $n$ and $m$ $(2 \le n \le 100, 1 \le m \le n)$ — the number of days and the number of exams.
Each of the following $m$ lines contains three integers $s_i$, $d_i$, $c_i$ $(1 \le s_i < d_i \le n, 1 \le c_i \le n)$ — the day, when questions for the $i$-th exam will be given, the day of the $i$-th exam, number of days Petya needs to prepare for the $i$-th exam.
Guaranteed, that all the exams will be in different days. Questions for different exams can be given in the same day. It is possible that, in the day of some exam, the questions for other exams are given. | If Petya can not prepare and pass all the exams, print -1. In case of positive answer, print $n$ integers, where the $j$-th number is:
- $(m + 1)$, if the $j$-th day is a day of some exam (recall that in each day no more than one exam is conducted), - zero, if in the $j$-th day Petya will have a rest, - $i$ ($1 \le i \le m$), if Petya will prepare for the $i$-th exam in the day $j$ (the total number of days Petya prepares for each exam should be strictly equal to the number of days needed to prepare for it).Assume that the exams are numbered in order of appearing in the input, starting from $1$.If there are multiple schedules, print any of them. | [
"5 2\n1 3 1\n1 5 1\n",
"3 2\n1 3 1\n1 2 1\n",
"10 3\n4 7 2\n1 10 3\n8 9 1\n"
] | [
"1 2 3 0 3 \n",
"-1\n",
"2 2 2 1 1 0 4 3 4 4 \n"
] | In the first example Petya can, for example, prepare for exam $1$ in the first day, prepare for exam $2$ in the second day, pass exam $1$ in the third day, relax in the fourth day, and pass exam $2$ in the fifth day. So, he can prepare and pass all exams.
In the second example, there are three days and two exams. So, Petya can prepare in only one day (because in two other days he should pass exams). Then Petya can not prepare and pass all exams. | [
{
"input": "5 2\n1 3 1\n1 5 1",
"output": "1 2 3 0 3 "
},
{
"input": "3 2\n1 3 1\n1 2 1",
"output": "-1"
},
{
"input": "10 3\n4 7 2\n1 10 3\n8 9 1",
"output": "2 2 2 1 1 0 4 3 4 4 "
},
{
"input": "2 1\n1 2 1",
"output": "1 2 "
},
{
"input": "3 1\n1 2 2",
"outp... | 62 | 0 | 3 | 12,797 | |
930 | Coins Exhibition | [
"data structures",
"dp",
"math"
] | null | null | Arkady and Kirill visited an exhibition of rare coins. The coins were located in a row and enumerated from left to right from 1 to *k*, each coin either was laid with its obverse (front) side up, or with its reverse (back) side up.
Arkady and Kirill made some photos of the coins, each photo contained a segment of neighboring coins. Akrady is interested in obverses, so on each photo made by him there is at least one coin with obverse side up. On the contrary, Kirill is interested in reverses, so on each photo made by him there is at least one coin with its reverse side up.
The photos are lost now, but Arkady and Kirill still remember the bounds of the segments of coins each photo contained. Given this information, compute the remainder of division by 109<=+<=7 of the number of ways to choose the upper side of each coin in such a way, that on each Arkady's photo there is at least one coin with obverse side up, and on each Kirill's photo there is at least one coin with reverse side up. | The first line contains three integers *k*, *n* and *m* (1<=≤<=*k*<=≤<=109, 0<=≤<=*n*,<=*m*<=≤<=105) — the total number of coins, the number of photos made by Arkady, and the number of photos made by Kirill, respectively.
The next *n* lines contain the descriptions of Arkady's photos, one per line. Each of these lines contains two integers *l* and *r* (1<=≤<=*l*<=≤<=*r*<=≤<=*k*), meaning that among coins from the *l*-th to the *r*-th there should be at least one with obverse side up.
The next *m* lines contain the descriptions of Kirill's photos, one per line. Each of these lines contains two integers *l* and *r* (1<=≤<=*l*<=≤<=*r*<=≤<=*k*), meaning that among coins from the *l*-th to the *r*-th there should be at least one with reverse side up. | Print the only line — the number of ways to choose the side for each coin modulo 109<=+<=7<==<=1000000007. | [
"5 2 2\n1 3\n3 5\n2 2\n4 5\n",
"5 3 2\n1 3\n2 2\n3 5\n2 2\n4 5\n",
"60 5 7\n1 3\n50 60\n1 60\n30 45\n20 40\n4 5\n6 37\n5 18\n50 55\n22 27\n25 31\n44 45\n"
] | [
"8\n",
"0\n",
"732658600\n"
] | In the first example the following ways are possible ('O' — obverse, 'R' — reverse side):
- OROOR, - ORORO, - ORORR, - RROOR, - RRORO, - RRORR, - ORROR, - ORRRO.
In the second example the information is contradictory: the second coin should have obverse and reverse sides up at the same time, that is impossible. So, the answer is 0. | [] | 30 | 0 | 0 | 12,804 | |
198 | About Bacteria | [
"implementation",
"math"
] | null | null | Qwerty the Ranger took up a government job and arrived on planet Mars. He should stay in the secret lab and conduct some experiments on bacteria that have funny and abnormal properties. The job isn't difficult, but the salary is high.
At the beginning of the first experiment there is a single bacterium in the test tube. Every second each bacterium in the test tube divides itself into *k* bacteria. After that some abnormal effects create *b* more bacteria in the test tube. Thus, if at the beginning of some second the test tube had *x* bacteria, then at the end of the second it will have *kx*<=+<=*b* bacteria.
The experiment showed that after *n* seconds there were exactly *z* bacteria and the experiment ended at this point.
For the second experiment Qwerty is going to sterilize the test tube and put there *t* bacteria. He hasn't started the experiment yet but he already wonders, how many seconds he will need to grow at least *z* bacteria. The ranger thinks that the bacteria will divide by the same rule as in the first experiment.
Help Qwerty and find the minimum number of seconds needed to get a tube with at least *z* bacteria in the second experiment. | The first line contains four space-separated integers *k*, *b*, *n* and *t* (1<=≤<=*k*,<=*b*,<=*n*,<=*t*<=≤<=106) — the parameters of bacterial growth, the time Qwerty needed to grow *z* bacteria in the first experiment and the initial number of bacteria in the second experiment, correspondingly. | Print a single number — the minimum number of seconds Qwerty needs to grow at least *z* bacteria in the tube. | [
"3 1 3 5\n",
"1 4 4 7\n",
"2 2 4 100\n"
] | [
"2",
"3",
"0"
] | none | [
{
"input": "3 1 3 5",
"output": "2"
},
{
"input": "1 4 4 7",
"output": "3"
},
{
"input": "2 2 4 100",
"output": "0"
},
{
"input": "1 2 3 100",
"output": "0"
},
{
"input": "10 10 10 123456",
"output": "6"
},
{
"input": "847 374 283 485756",
"output"... | 280 | 1,331,200 | 3 | 12,843 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.