contestId
int64 0
1.01k
| name
stringlengths 2
58
| tags
sequencelengths 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
sequencelengths 0
7
| demo-output
sequencelengths 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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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": "293058929"
},
{
"input": "100000",
"output": "877968"
},
{
"input": "1024",
"output": "5120"
},
{
"input": "536870912",
"output": "7784628224"
},
{
"input": "536870911",
"output": "7784628223"
},
{
"input": "536870913",
"output": "8321499136"
},
{
"input": "123456789",
"output": "1680249144"
},
{
"input": "200",
"output": "844"
},
{
"input": "3",
"output": "3"
},
{
"input": "5",
"output": "8"
},
{
"input": "6",
"output": "9"
},
{
"input": "7",
"output": "11"
},
{
"input": "1000",
"output": "5052"
},
{
"input": "12000",
"output": "84624"
},
{
"input": "65536",
"output": "524288"
},
{
"input": "1048576",
"output": "10485760"
},
{
"input": "8",
"output": "12"
},
{
"input": "549755813888",
"output": "10720238370816"
},
{
"input": "549755813887",
"output": "10720238370815"
},
{
"input": "549755813889",
"output": "11269994184704"
}
] | 61 | 0 | 0 | 20,537 |
|
400 | Dima and Bacteria | [
"dsu",
"graphs",
"shortest paths"
] | null | null | Dima took up the biology of bacteria, as a result of his experiments, he invented *k* types of bacteria. Overall, there are *n* bacteria at his laboratory right now, and the number of bacteria of type *i* equals *c**i*. For convenience, we will assume that all the bacteria are numbered from 1 to *n*. The bacteria of type *c**i* are numbered from to .
With the help of special equipment Dima can move energy from some bacteria into some other one. Of course, the use of such equipment is not free. Dima knows *m* ways to move energy from some bacteria to another one. The way with number *i* can be described with integers *u**i*, *v**i* and *x**i* mean that this way allows moving energy from bacteria with number *u**i* to bacteria with number *v**i* or vice versa for *x**i* dollars.
Dima's Chef (Inna) calls the type-distribution correct if there is a way (may be non-direct) to move energy from any bacteria of the particular type to any other bacteria of the same type (between any two bacteria of the same type) for zero cost.
As for correct type-distribution the cost of moving the energy depends only on the types of bacteria help Inna to determine is the type-distribution correct? If it is, print the matrix *d* with size *k*<=×<=*k*. Cell *d*[*i*][*j*] of this matrix must be equal to the minimal possible cost of energy-moving from bacteria with type *i* to bacteria with type *j*. | The first line contains three integers *n*,<=*m*,<=*k* (1<=≤<=*n*<=≤<=105; 0<=≤<=*m*<=≤<=105; 1<=≤<=*k*<=≤<=500). The next line contains *k* integers *c*1,<=*c*2,<=...,<=*c**k* (1<=≤<=*c**i*<=≤<=*n*). Each of the next *m* lines contains three integers *u**i*,<=*v**i*,<=*x**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=105; 0<=≤<=*x**i*<=≤<=104). It is guaranteed that . | If Dima's type-distribution is correct, print string «Yes», and then *k* lines: in the *i*-th line print integers *d*[*i*][1],<=*d*[*i*][2],<=...,<=*d*[*i*][*k*] (*d*[*i*][*i*]<==<=0). If there is no way to move energy from bacteria *i* to bacteria *j* appropriate *d*[*i*][*j*] must equal to -1. If the type-distribution isn't correct print «No». | [
"4 4 2\n1 3\n2 3 0\n3 4 0\n2 4 1\n2 1 2\n",
"3 1 2\n2 1\n1 2 0\n",
"3 2 2\n2 1\n1 2 0\n2 3 1\n",
"3 0 2\n1 2\n"
] | [
"Yes\n0 2\n2 0\n",
"Yes\n0 -1\n-1 0\n",
"Yes\n0 1\n1 0\n",
"No\n"
] | none | [
{
"input": "4 4 2\n1 3\n2 3 0\n3 4 0\n2 4 1\n2 1 2",
"output": "Yes\n0 2\n2 0"
},
{
"input": "3 1 2\n2 1\n1 2 0",
"output": "Yes\n0 -1\n-1 0"
},
{
"input": "3 2 2\n2 1\n1 2 0\n2 3 1",
"output": "Yes\n0 1\n1 0"
},
{
"input": "3 0 2\n1 2",
"output": "No"
},
{
"input": "10 9 3\n4 4 2\n1 2 0\n2 4 0\n3 2 0\n5 6 0\n6 7 0\n7 8 0\n9 10 0\n1 5 2\n2 6 1",
"output": "Yes\n0 1 -1\n1 0 -1\n-1 -1 0"
},
{
"input": "10 9 1\n10\n1 2 0\n2 3 0\n3 4 0\n4 5 0\n5 6 0\n6 7 0\n7 8 0\n8 9 0\n9 10 0",
"output": "Yes\n0"
},
{
"input": "10 0 10\n1 1 1 1 1 1 1 1 1 1",
"output": "Yes\n0 -1 -1 -1 -1 -1 -1 -1 -1 -1\n-1 0 -1 -1 -1 -1 -1 -1 -1 -1\n-1 -1 0 -1 -1 -1 -1 -1 -1 -1\n-1 -1 -1 0 -1 -1 -1 -1 -1 -1\n-1 -1 -1 -1 0 -1 -1 -1 -1 -1\n-1 -1 -1 -1 -1 0 -1 -1 -1 -1\n-1 -1 -1 -1 -1 -1 0 -1 -1 -1\n-1 -1 -1 -1 -1 -1 -1 0 -1 -1\n-1 -1 -1 -1 -1 -1 -1 -1 0 -1\n-1 -1 -1 -1 -1 -1 -1 -1 -1 0"
},
{
"input": "16 20 4\n4 4 4 4\n1 2 0\n2 3 0\n3 4 0\n1 4 1\n5 6 0\n5 7 0\n5 8 0\n7 8 2\n9 10 0\n9 11 0\n11 12 0\n9 10 1\n13 14 0\n14 15 0\n15 16 0\n15 16 1\n1 5 2\n5 9 1\n1 15 1\n16 10 1",
"output": "Yes\n0 2 2 1\n2 0 1 2\n2 1 0 1\n1 2 1 0"
},
{
"input": "73 29 73\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n67 70 761\n60 34 641\n33 22 689\n67 35 556\n16 21 192\n1 62 75\n37 5 657\n8 7 606\n37 1 54\n53 1 805\n2 68 652\n20 39 701\n44 43 997\n65 57 202\n44 25 211\n67 56 402\n32 48 521\n30 23 321\n50 1 381\n44 32 963\n22 21 244\n49 46 691\n68 52 453\n24 41 973\n20 30 287\n57 46 921\n27 13 109\n60 70 31\n72 42 23",
"output": "Yes\n0 -1 -1 -1 711 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 54 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 381 -1 -1 805 -1 -1 -1 -1 -1 -1 -1 -1 75 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n-1 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1105 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 652 -1 -1 -1 -1 -1\n-1 -1 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ..."
},
{
"input": "3 2 2\n2 1\n1 3 0\n3 2 0",
"output": "Yes\n0 0\n0 0"
},
{
"input": "3 2 2\n2 1\n1 3 0\n2 3 0",
"output": "Yes\n0 0\n0 0"
},
{
"input": "6 2 2\n4 2\n1 2 0\n5 6 0",
"output": "No"
},
{
"input": "4 1 2\n2 2\n2 3 0",
"output": "No"
},
{
"input": "4 3 2\n2 2\n1 3 0\n2 3 0\n3 4 0",
"output": "Yes\n0 0\n0 0"
},
{
"input": "4 3 2\n3 1\n1 3 0\n1 4 1\n3 4 1",
"output": "No"
},
{
"input": "3 1 1\n3\n1 2 0",
"output": "No"
},
{
"input": "4 2 2\n3 1\n1 4 0\n2 4 0",
"output": "No"
},
{
"input": "4 2 3\n1 1 2\n1 3 0\n2 4 0",
"output": "No"
}
] | 1,700 | 75,161,600 | 0 | 20,561 |
|
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\n322022227 751269818 629795150 369443545 344607287 250044294 476897672 184054549 986884572 917181121\n6 3\n7 3\n1 9\n7 9\n10 7\n3 4\n8 6\n7 4\n6 10\n7 2\n3 5\n6 9\n3 10\n8 7\n6 5\n8 1\n8 5\n1 7\n8 10\n8 2\n1 5\n10 4\n6 7\n4 6\n2 6\n5 4\n9 10\n9 2\n4 8\n5 9\n4 1\n3 2\n2 1\n4 2\n9 8",
"output": "1 1 2 0 0 0 1 0 2 3 "
}
] | 872 | 56,934,400 | 3 | 20,562 |
|
725 | Hidden Word | [
"brute force",
"constructive algorithms",
"implementation",
"strings"
] | null | null | Let’s define a grid to be a set of tiles with 2 rows and 13 columns. Each tile has an English letter written in it. The letters don't have to be unique: there might be two or more tiles with the same letter written on them. Here is an example of a grid:
We say that two tiles are adjacent if they share a side or a corner. In the example grid above, the tile with the letter 'A' is adjacent only to the tiles with letters 'B', 'N', and 'O'. A tile is not adjacent to itself.
A sequence of tiles is called a path if each tile in the sequence is adjacent to the tile which follows it (except for the last tile in the sequence, which of course has no successor). In this example, "ABC" is a path, and so is "KXWIHIJK". "MAB" is not a path because 'M' is not adjacent to 'A'. A single tile can be used more than once by a path (though the tile cannot occupy two consecutive places in the path because no tile is adjacent to itself).
You’re given a string *s* which consists of 27 upper-case English letters. Each English letter occurs at least once in *s*. Find a grid that contains a path whose tiles, viewed in the order that the path visits them, form the string *s*. If there’s no solution, print "Impossible" (without the quotes). | The only line of the input contains the string *s*, consisting of 27 upper-case English letters. Each English letter occurs at least once in *s*. | Output two lines, each consisting of 13 upper-case English characters, representing the rows of the grid. If there are multiple solutions, print any of them. If there is no solution print "Impossible". | [
"ABCDEFGHIJKLMNOPQRSGTUVWXYZ\n",
"BUVTYZFQSNRIWOXXGJLKACPEMDH\n"
] | [
"YXWVUTGHIJKLM\nZABCDEFSRQPON\n",
"Impossible\n"
] | none | [
{
"input": "ABCDEFGHIJKLMNOPQRSGTUVWXYZ",
"output": "YXWVUTGHIJKLM\nZABCDEFSRQPON"
},
{
"input": "BUVTYZFQSNRIWOXXGJLKACPEMDH",
"output": "Impossible"
},
{
"input": "DYCEUXXKMGZOINVPHWQSRTABLJF",
"output": "Impossible"
},
{
"input": "UTEDBZRVWLOFUASHCYIPXGJMKNQ",
"output": "PIYCHSAUTEDBZ\nXGJMKNQFOLWVR"
},
{
"input": "ZWMFLTCQIAJEVUPODMSGXKHRNYB",
"output": "HKXGSMFLTCQIA\nRNYBZWDOPUVEJ"
},
{
"input": "QGZEMFKWLUHOVSXJTCPIYREDNAB",
"output": "ANDEMFKWLUHOV\nBQGZRYIPCTJXS"
},
{
"input": "BMVFGRNDOWTILZVHAKCQSXYEJUP",
"output": "XSQCKAHVFGRND\nYEJUPBMZLITWO"
},
{
"input": "MKNTKOBFLJSXWQPVUERDHIACYGZ",
"output": "VPQWXSJLFBOKN\nUERDHIACYGZMT"
},
{
"input": "YOFJVQSWBUZENPCXGQTHMDKAILR",
"output": "IAKDMHTQSWBUZ\nLRYOFJVGXCPNE"
},
{
"input": "GYCUAXSBNAWFIJPDQVETKZOMLHR",
"output": "TEVQDPJIFWAXS\nKZOMLHRGYCUNB"
},
{
"input": "BITCRJOKMPDDUSWAYXHQZEVGLFN",
"output": "Impossible"
},
{
"input": "XCDSLTYWJIGUBPHNFZWVMQARKOE",
"output": "OKRAQMVWJIGUB\nEXCDSLTYZFNHP"
},
{
"input": "XTSHBGLRJAMDUIPCWYOZVERNKQF",
"output": "XFQKNRJAMDUIP\nTSHBGLEVZOYWC"
},
{
"input": "RFKNZXHAIMVBWEBPTCSYOLJGDQU",
"output": "QDGJLOYSCTPBW\nURFKNZXHAIMVE"
},
{
"input": "HVDEBKMJTLKQORNWCZSGXYIPUAF",
"output": "XGSZCWNROQKMJ\nYIPUAFHVDEBLT"
},
{
"input": "XZTMCRBONHFIUVPKWSDLJQGAHYE",
"output": "TZXEYHFIUVPKW\nMCRBONAGQJLDS"
},
{
"input": "YAMVOHUJLEDCWZLXNRGPIQTBSKF",
"output": "SBTQIPGRNXLED\nKFYAMVOHUJZWC"
},
{
"input": "XECPFJBHINOWVLAGTUMRZYHQSDK",
"output": "XKDSQHINOWVLA\nECPFJBYZRMUTG"
},
{
"input": "UULGRBAODZENVCSMJTHXPWYKFIQ",
"output": "Impossible"
},
{
"input": "BADSLHIYGMZJQKTCOPRVUXFWENN",
"output": "Impossible"
},
{
"input": "TEGXHBUVZDPAMIJFQYCWRKSTNLO",
"output": "NTEGXHBUVZDPA\nLOSKRWCYQFJIM"
},
{
"input": "XQVBTCNIRFPLOHAYZUMKWEJSXDG",
"output": "DXQVBTCNIRFPL\nGSJEWKMUZYAHO"
},
{
"input": "MIDLBEUAGTNPYKFWHVSRJOXCZMQ",
"output": "MIDLBEUAGTNPY\nQZCXOJRSVHWFK"
},
{
"input": "NMGIFDZKBCVRYLTWOASXHEUQPJN",
"output": "NMGIFDZKBCVRY\nJPQUEHXSAOWTL"
},
{
"input": "AHGZCRJTKPMQUNBWSIYLDXEHFVO",
"output": "VFHGZCRJTKPMQ\nOAEXDLYISWBNU"
},
{
"input": "UNGHFQRCIPBZTEOAYJXLDMSKNWV",
"output": "WNGHFQRCIPBZT\nVUKSMDLXJYAOE"
},
{
"input": "MKBGVNDJRAWUEHFSYLIZCOPTXKQ",
"output": "QKBGVNDJRAWUE\nMXTPOCZILYSFH"
},
{
"input": "UTGDEJHCBKRWLYFSONAQVMPIXZT",
"output": "TGDEJHCBKRWLY\nUZXIPMVQANOSF"
},
{
"input": "BETRFOVLPCMWKHAXSGUDQYJTZIN",
"output": "IZTRFOVLPCMWK\nNBEJYQDUGSXAH"
},
{
"input": "HIDCLZUTPOQGEXFASJNYBVRMDKW",
"output": "WKDCLZUTPOQGE\nHIMRVBYNJSAFX"
},
{
"input": "CNHIKJWRLPXTQZVUGYDMBAOEFHS",
"output": "SHIKJWRLPXTQZ\nCNFEOABMDYGUV"
},
{
"input": "LCFNHUQWXBPOSJMYTGKDAZVREIF",
"output": "LFNHUQWXBPOSJ\nCIERVZADKGTYM"
},
{
"input": "OURNQJWMIXCLGSDVEKZAFBYNTPH",
"output": "HPTNQJWMIXCLG\nOURYBFAZKEVDS"
},
{
"input": "ZWFIRJNXVKHOUSTQBLEGYMAPIDC",
"output": "CDIRJNXVKHOUS\nZWFPAMYGELBQT"
},
{
"input": "UOWJXRKHZDNGLSAMEIYTQBVCFJP",
"output": "UPJXRKHZDNGLS\nOWFCVBQTYIEMA"
},
{
"input": "IHDTJLGRFUXQSOZEMVYKWCPANBT",
"output": "ITJLGRFUXQSOZ\nHDBNAPCWKYVME"
},
{
"input": "ABCDEFGHIJKLMNOPQRSTUVWXYZA",
"output": "ABCDEFGHIJKLM\nZYXWVUTSRQPON"
},
{
"input": "ABACDEFGHIJKLMNOPQRSTUVWXYZ",
"output": "NMLKJIHGFEDCA\nOPQRSTUVWXYZB"
},
{
"input": "ABCDEFGHIJKLMNOPQRSTUVWXYZG",
"output": "CBAGHIJKLMNOP\nDEFZYXWVUTSRQ"
},
{
"input": "ABCDEFGHGIJKLMNOPQRSTUVWXYZ",
"output": "TSRQPONMLKJIG\nUVWXYZABCDEFH"
},
{
"input": "ABCDEFGHIJKLMNOPQRSTUVWXYZX",
"output": "KJIHGFEDCBAXY\nLMNOPQRSTUVWZ"
},
{
"input": "ABCDEFGHIJKLMNOPQRSTUVWYXYZ",
"output": "KJIHGFEDCBAZY\nLMNOPQRSTUVWX"
},
{
"input": "BUVTYZFQSNRIWOXGJLKACPEMDHB",
"output": "BUVTYZFQSNRIW\nHDMEPCAKLJGXO"
},
{
"input": "QWERTYUIOPASDFGHJKLZXCVBNMQ",
"output": "QWERTYUIOPASD\nMNBVCXZLKJHGF"
},
{
"input": "ABCBDEFGHIJKLMNOPQRSTUVWXYZ",
"output": "ONMLKJIHGFEDB\nPQRSTUVWXYZAC"
},
{
"input": "ABCDEFGHIJKLMNOPQRSTUVWXAYZ",
"output": "YABCDEFGHIJKL\nZXWVUTSRQPONM"
},
{
"input": "ABCDEFGHIJKLMZYXWVUTSRQPONA",
"output": "ABCDEFGHIJKLM\nNOPQRSTUVWXYZ"
},
{
"input": "BACDEFGHIJKLMNOPQRSTUVWXYZA",
"output": "ACDEFGHIJKLMN\nBZYXWVUTSRQPO"
}
] | 46 | 307,200 | -1 | 20,692 |
|
356 | Bags and Coins | [
"bitmasks",
"constructive algorithms",
"dp",
"greedy"
] | null | null | When you were a child you must have been told a puzzle of bags and coins. Anyway, here's one of its versions:
A horse has three bags. The first bag has one coin, the second bag has one coin and the third bag has three coins. In total, the horse has three coins in the bags. How is that possible?
The answer is quite simple. The third bag contains a coin and two other bags.
This problem is a generalization of the childhood puzzle. You have *n* bags. You know that the first bag contains *a*1 coins, the second bag contains *a*2 coins, ..., the *n*-th bag contains *a**n* coins. In total, there are *s* coins. Find the way to arrange the bags and coins so that they match the described scenario or else state that it is impossible to do. | The first line contains two integers *n* and *s* (1<=≤<=*n*,<=*s*<=≤<=70000) — the number of bags and the total number of coins. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=70000), where *a**i* shows the number of coins in the *i*-th bag. | If the answer doesn't exist, print -1.
Otherwise, print *n* lines, on the *i*-th line print the contents of the *i*-th bag. The first number in the line, *c**i* (0<=≤<=*c**i*<=≤<=*a**i*), must represent the number of coins lying directly in the *i*-th bag (the coins in the bags that are in the *i*-th bag are not taken into consideration). The second number in the line, *k**i* (0<=≤<=*k**i*<=<<=*n*) must represent the number of bags that lie directly in the *i*-th bag (the bags that are inside the bags lying in the *i*-th bag are not taken into consideration). Next, the line must contain *k**i* integers — the numbers of the bags that are lying directly in the *i*-th bag.
The total number of coins in the solution must equal *s*. If we count the total number of coins the *i*-th bag in the solution has, we should get *a**i*.
No bag can directly lie in more than one bag. The bags can be nested in more than one level (see the second test case). If there are multiple correct answers, you can print any of them. | [
"3 3\n1 3 1\n",
"3 3\n1 3 1\n",
"1 2\n1\n",
"8 10\n2 7 3 4 1 3 1 2\n"
] | [
"1 0\n1 2 3 1\n1 0\n",
"1 0\n2 1 3\n0 1 1\n",
"-1\n",
"2 0\n1 2 1 4\n0 2 7 8\n0 2 5 6\n1 0\n3 0\n1 0\n2 0\n"
] | The pictures below show two possible ways to solve one test case from the statement. The left picture corresponds to the first test case, the right picture corresponds to the second one. | [] | 30 | 0 | 0 | 20,705 |
|
645 | Intellectual Inquiry | [
"dp",
"greedy",
"strings"
] | null | null | After getting kicked out of her reporting job for not knowing the alphabet, Bessie has decided to attend school at the Fillet and Eggs Eater Academy. She has been making good progress with her studies and now knows the first *k* English letters.
Each morning, Bessie travels to school along a sidewalk consisting of *m*<=+<=*n* tiles. In order to help Bessie review, Mr. Moozing has labeled each of the first *m* sidewalk tiles with one of the first *k* lowercase English letters, spelling out a string *t*. Mr. Moozing, impressed by Bessie's extensive knowledge of farm animals, plans to let her finish labeling the last *n* tiles of the sidewalk by herself.
Consider the resulting string *s* (|*s*|<==<=*m*<=+<=*n*) consisting of letters labeled on tiles in order from home to school. For any sequence of indices *p*1<=<<=*p*2<=<<=...<=<<=*p**q* we can define subsequence of the string *s* as string *s**p*1*s**p*2... *s**p**q*. Two subsequences are considered to be distinct if they differ as strings. Bessie wants to label the remaining part of the sidewalk such that the number of distinct subsequences of tiles is maximum possible. However, since Bessie hasn't even finished learning the alphabet, she needs your help!
Note that empty subsequence also counts. | The first line of the input contains two integers *n* and *k* (0<=≤<=*n*<=≤<=1<=000<=000, 1<=≤<=*k*<=≤<=26).
The second line contains a string *t* (|*t*|<==<=*m*,<=1<=≤<=*m*<=≤<=1<=000<=000) consisting of only first *k* lowercase English letters. | Determine the maximum number of distinct subsequences Bessie can form after labeling the last *n* sidewalk tiles each with one of the first *k* lowercase English letters. Since this number can be rather large, you should print it modulo 109<=+<=7.
Please note, that you are not asked to maximize the remainder modulo 109<=+<=7! The goal is to maximize the initial value and then print the remainder. | [
"1 3\nac\n",
"0 2\naaba\n"
] | [
"8\n",
"10\n"
] | In the first sample, the optimal labeling gives 8 different subsequences: "" (the empty string), "a", "c", "b", "ac", "ab", "cb", and "acb".
In the second sample, the entire sidewalk is already labeled. The are 10 possible different subsequences: "" (the empty string), "a", "b", "aa", "ab", "ba", "aaa", "aab", "aba", and "aaba". Note that some strings, including "aa", can be obtained with multiple sequences of tiles, but are only counted once. | [] | 2,000 | 3,072,000 | 0 | 20,708 |
|
0 | none | [
"none"
] | null | null | Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string *s*.
String *p* is called a substring of string *s* if you can read it starting from some position in the string *s*. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
Dr. Moriarty plans to take string *s* and cut out some substring from it, let's call it *t*. Then he needs to change the substring *t* zero or more times. As a result, he should obtain a fixed string *u* (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions:
- Insert one letter to any end of the string. - Delete one letter from any end of the string. - Change one letter into any other one.
Moriarty is very smart and after he chooses some substring *t*, he always makes the minimal number of changes to obtain *u*.
Help Moriarty choose the best substring *t* from all substrings of the string *s*. The substring *t* should minimize the number of changes Moriarty should make to obtain the string *u* from it. | The first line contains a non-empty string *s*, consisting of lowercase Latin letters. The second line contains a non-empty string *u*, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive. | Print the only integer — the minimum number of changes that Dr. Moriarty has to make with the string that you choose. | [
"aaaaa\naaa\n",
"abcabc\nbcd\n",
"abcdef\nklmnopq\n"
] | [
"0\n",
"1\n",
"7\n"
] | In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message *u*, so Moriarty won't have to make any changes.
In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character.
In the third sample the initial string *s* doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message. | [
{
"input": "aaaaa\naaa",
"output": "0"
},
{
"input": "abcabc\nbcd",
"output": "1"
},
{
"input": "abcdef\nklmnopq",
"output": "7"
},
{
"input": "aaabbbaaa\naba",
"output": "1"
},
{
"input": "a\na",
"output": "0"
},
{
"input": "z\nz",
"output": "0"
},
{
"input": "a\nz",
"output": "1"
},
{
"input": "d\nt",
"output": "1"
},
{
"input": "o\nu",
"output": "1"
},
{
"input": "a\nm",
"output": "1"
},
{
"input": "t\nv",
"output": "1"
},
{
"input": "n\ng",
"output": "1"
},
{
"input": "c\nh",
"output": "1"
},
{
"input": "r\ni",
"output": "1"
},
{
"input": "h\nb",
"output": "1"
},
{
"input": "r\na",
"output": "1"
},
{
"input": "c\np",
"output": "1"
},
{
"input": "wbdbzf\nfpvlerhsuf",
"output": "9"
},
{
"input": "zafsqbsu\nhl",
"output": "2"
},
{
"input": "juhlp\nycqugugk",
"output": "7"
},
{
"input": "ladfasxt\ncpvtd",
"output": "4"
},
{
"input": "ally\ncjidwuj",
"output": "7"
},
{
"input": "rgug\npgqwslo",
"output": "6"
},
{
"input": "wmjwu\nrfew",
"output": "3"
},
{
"input": "cpnwcdqff\nq",
"output": "0"
},
{
"input": "dkwh\nm",
"output": "1"
},
{
"input": "zfinrlju\nwiiegborjl",
"output": "9"
},
{
"input": "swconajiqpgziitbpwjsfcalqvmwbfed\nridfnsyumichlhpnurrnwkyjcdzchznpmno",
"output": "32"
},
{
"input": "vfjofvgkdwgqdlomtmcvedtmimdnxavhfirienxfdflldkbwjsynablhdvgaipvcghgaxipotwmmlzxekipgbvpfivlgzfwqz\njkdfjnessjfgcqpnxgtqdxtqimolbdlnipkoqht",
"output": "34"
},
{
"input": "dtvxepnxfkzcaoh\nkpdzbtwjitzlyzvsbwcsrfglaycrhzwsdtidrelndsq",
"output": "41"
},
{
"input": "sweaucynwsnduofyaqunoxttbipgrbfpssplfp\nuifmuxmczznobefdsyoclwzekewxmcwfqryuevnxxlgxsuhoytkaddorbdaygo",
"output": "57"
},
{
"input": "eohztfsxoyhirnzxgwaevfqstinlxeiyywmpmlbedkjihaxfdtsocof\nbloqrjbidxiqozvwregxxgmxuqcvhwzhytfckbafd",
"output": "37"
},
{
"input": "ybshzefoxkqdigcjafs\nnffvaxdmditsolfxbyquira",
"output": "19"
},
{
"input": "ytfqnuhqzbjjheejjbzcaorilcyvuxvviaiba\nxnhgkdfceialuujgcxmrhjbzvibcoknofafmdjnhij",
"output": "37"
},
{
"input": "ibdjtvgaveujdyidqldrxgwhsammmfpgxwljkptmeyejdvudhctmqjazalyzmzfgebetyqncu\nercdngwctdarcennbuqhsjlwfwrcqjbcjxqftycoulrhrimwhznogjmrrqdygtmllottpjgmkndraearezvxxmdhcuokhyngu",
"output": "90"
},
{
"input": "bwhvaanyxupicwobeevcwewhcriwowfovnylalpuhxzqxtzyjrzlxcmejujvliomdfedgtaioauwrcluhfxtzu\nplinvtsvytepojsecnjisxlmqkfhgknitvuw",
"output": "28"
},
{
"input": "sjxykdmrzpescabubcjflhnpckgytklc\nsxirpuqnmjqhlnvdeyvxvzzcygkpsujyifzgzmtvxsimddjahiephqlgfzngrzjtcrgrimewsxipczsgu",
"output": "76"
},
{
"input": "ksmbytfyhhnstlecripupiwdhbkhfpfmimrbqgszohcqnezcybvwasxmkxfupvuecsctcpadccnqexsglwaiyxcoyheefcjmdedesgjqdtqgrvfjonzesffousooutsjumrxl\nhgjqihcfbnmgufonaiudbjegexexthrzcdkuimwogpbyovemztzcmqnrbhabxyyxyfuzpyhjgnioexbezzupcxlyzuuncstiiqsjzdtqppqhxilvqimlpjejiqbwpeekzweeyvthvjffgfvqauqrugajjjzibgzhxphcvtncjzecbtupwkehcrgsgfgkvwwnifglyamjkzfvabybsstwrwugnmiwflhemgnfbrtskzfxcepqhtelgiowzeuujpkuzsfsipcvtfoeshawvryaubilcbwukdhlwamsqenzvr",
"output": "287"
},
{
"input": "abcd\ndabc",
"output": "1"
},
{
"input": "medxx\nahmed",
"output": "2"
},
{
"input": "ab\ndab",
"output": "1"
},
{
"input": "nasldkfnsb\nyyyynasld",
"output": "4"
},
{
"input": "abcde\ncabc",
"output": "1"
},
{
"input": "a\nzzzzzzzzzza",
"output": "10"
},
{
"input": "abcde\nabde",
"output": "2"
},
{
"input": "bac\ntbdca",
"output": "3"
},
{
"input": "abcdef\nxyzabc",
"output": "3"
},
{
"input": "abcdef\nbctsf",
"output": "2"
},
{
"input": "xxxabaxxx\nxxxaaxxx",
"output": "2"
},
{
"input": "bcd\nabc",
"output": "1"
},
{
"input": "d\nabcdef",
"output": "5"
}
] | 2,000 | 4,608,000 | 0 | 20,716 |
|
729 | Road to Cinema | [
"binary search",
"greedy",
"sortings"
] | null | null | Vasya is currently at a car rental service, and he wants to reach cinema. The film he has bought a ticket for starts in *t* minutes. There is a straight road of length *s* from the service to the cinema. Let's introduce a coordinate system so that the car rental service is at the point 0, and the cinema is at the point *s*.
There are *k* gas stations along the road, and at each of them you can fill a car with any amount of fuel for free! Consider that this operation doesn't take any time, i.e. is carried out instantly.
There are *n* cars in the rental service, *i*-th of them is characterized with two integers *c**i* and *v**i* — the price of this car rent and the capacity of its fuel tank in liters. It's not allowed to fuel a car with more fuel than its tank capacity *v**i*. All cars are completely fueled at the car rental service.
Each of the cars can be driven in one of two speed modes: normal or accelerated. In the normal mode a car covers 1 kilometer in 2 minutes, and consumes 1 liter of fuel. In the accelerated mode a car covers 1 kilometer in 1 minutes, but consumes 2 liters of fuel. The driving mode can be changed at any moment and any number of times.
Your task is to choose a car with minimum price such that Vasya can reach the cinema before the show starts, i.e. not later than in *t* minutes. Assume that all cars are completely fueled initially. | The first line contains four positive integers *n*, *k*, *s* and *t* (1<=≤<=*n*<=≤<=2·105, 1<=≤<=*k*<=≤<=2·105, 2<=≤<=*s*<=≤<=109, 1<=≤<=*t*<=≤<=2·109) — the number of cars at the car rental service, the number of gas stations along the road, the length of the road and the time in which the film starts.
Each of the next *n* lines contains two positive integers *c**i* and *v**i* (1<=≤<=*c**i*,<=*v**i*<=≤<=109) — the price of the *i*-th car and its fuel tank capacity.
The next line contains *k* distinct integers *g*1,<=*g*2,<=...,<=*g**k* (1<=≤<=*g**i*<=≤<=*s*<=-<=1) — the positions of the gas stations on the road in arbitrary order. | Print the minimum rent price of an appropriate car, i.e. such car that Vasya will be able to reach the cinema before the film starts (not later than in *t* minutes). If there is no appropriate car, print -1. | [
"3 1 8 10\n10 8\n5 7\n11 9\n3\n",
"2 2 10 18\n10 4\n20 6\n5 3\n"
] | [
"10\n",
"20\n"
] | In the first sample, Vasya can reach the cinema in time using the first or the third cars, but it would be cheaper to choose the first one. Its price is equal to 10, and the capacity of its fuel tank is 8. Then Vasya can drive to the first gas station in the accelerated mode in 3 minutes, spending 6 liters of fuel. After that he can full the tank and cover 2 kilometers in the normal mode in 4 minutes, spending 2 liters of fuel. Finally, he drives in the accelerated mode covering the remaining 3 kilometers in 3 minutes and spending 6 liters of fuel. | [
{
"input": "3 1 8 10\n10 8\n5 7\n11 9\n3",
"output": "10"
},
{
"input": "2 2 10 18\n10 4\n20 6\n5 3",
"output": "20"
},
{
"input": "2 1 1000000000 2000000000\n111 1000000000\n101 1000000000\n5",
"output": "101"
},
{
"input": "2 1 1000000000 2000000000\n111 999999998\n101 999999998\n1",
"output": "-1"
},
{
"input": "2 1 1000000000 2000000000\n111 999999999\n101 999999998\n1",
"output": "111"
},
{
"input": "1 15 100 200\n283 8\n30 58 16 45 80 82 55 95 24 23 86 28 51 47 20",
"output": "-1"
},
{
"input": "3 2 300 400\n24 68\n13 65\n15 113\n127 177",
"output": "-1"
},
{
"input": "4 13 400 600\n13 30\n1 19\n1 160\n1 113\n58 73 15 319 194 362 128 157 336 162 77 90 96",
"output": "1"
},
{
"input": "1 1 2 2\n1000000000 1000000000\n1",
"output": "1000000000"
},
{
"input": "1 1 2 1\n1 10\n1",
"output": "-1"
},
{
"input": "1 1 1000000000 1000000000\n1 1000000000\n1",
"output": "-1"
},
{
"input": "1 1 1000000000 1000000000\n100 1000000000\n1",
"output": "-1"
},
{
"input": "4 2 7 15\n10 9\n4 4\n9 3\n4 10\n1 6",
"output": "4"
},
{
"input": "1 1 10 18\n5 6\n5",
"output": "5"
}
] | 46 | 204,800 | 0 | 20,732 |
|
125 | MST Company | [
"binary search",
"graphs"
] | null | null | The MST (Meaningless State Team) company won another tender for an important state reform in Berland.
There are *n* cities in Berland, some pairs of the cities are connected by roads. Each road has its price. One can move along any road in any direction. The MST team should carry out the repair works on some set of roads such that one can get from any city to any other one moving only along the repaired roads. Moreover, this set should contain exactly *k* capital roads (that is, the roads that start or finish in the capital). The number of the capital is 1.
As the budget has already been approved, the MST Company will profit by finding the set with minimum lengths of roads. | The first input line contains three integers *n*,<=*m*,<=*k* (1<=≤<=*n*<=≤<=5000;0<=≤<=*m*<=≤<=105;0<=≤<=*k*<=<<=5000), where *n* is the number of cities in the country, *m* is the number of roads in the country, *k* is the number of capital roads in the required set. Then *m* lines enumerate the roads in question. Each road is specified by three numbers *a**i*,<=*b**i*,<=*w**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; 1<=≤<=*w*<=≤<=105), where *a**i*,<=*b**i* are the numbers of cities linked by a road and *w**i* is its length.
Between each pair of cities no more than one road exists. There are no roads that start and finish in one city. The capital's number is 1. | In the first line print the number of roads in the required set. The second line should contain the numbers of roads included in the sought set. If the sought set does not exist, print -1. | [
"4 5 2\n1 2 1\n2 3 1\n3 4 1\n1 3 3\n1 4 2\n"
] | [
"3\n1 5 2 "
] | none | [] | 62 | 0 | -1 | 20,746 |
|
58 | Expression | [
"dp"
] | E. Expression | 2 | 256 | One day Vasya was solving arithmetical problems. He wrote down an expression *a*<=+<=*b*<==<=*c* in his notebook. When the teacher checked Vasya's work it turned out that Vasya had solved the problem incorrectly. Now Vasya tries to find excuses. He says that he simply forgot to write down several digits in numbers *a*, *b* and *c*, but he can't remember what numbers they actually were. Help Vasya, find such numbers *x*, *y* and *z*, with which the following conditions are met:
- *x*<=+<=*y*<==<=*z*, - from the expression *x*<=+<=*y*<==<=*z* several digits can be erased in such a way that the result will be *a*<=+<=*b*<==<=*c*, - the expression *x*<=+<=*y*<==<=*z* should have the minimal length. | The first and only input line contains the expression *a*<=+<=*b*<==<=*c* (1<=≤<=*a*,<=*b*,<=*c*<=≤<=106, *a*, *b* and *c* don't contain leading zeroes) which is the expression Vasya wrote down. | Print the correct expression *x*<=+<=*y*<==<=*z* (*x*, *y* and *z* are non-negative numbers without leading zeroes). The expression *a*<=+<=*b*<==<=*c* must be met in *x*<=+<=*y*<==<=*z* as a subsequence. The printed solution should have the minimal possible number of characters. If there are several such solutions, you can print any of them. | [
"2+4=5\n",
"1+1=3\n",
"1+1=2\n"
] | [
"21+4=25\n",
"1+31=32\n",
"1+1=2\n"
] | none | [
{
"input": "2+4=5",
"output": "21+4=25"
},
{
"input": "1+1=3",
"output": "1+31=32"
},
{
"input": "1+1=2",
"output": "1+1=2"
},
{
"input": "323+123=287",
"output": "323+12553=12876"
},
{
"input": "878347+637216=542650",
"output": "847834370+6372160=854206530"
},
{
"input": "123122+765654=975632",
"output": "123192200+76565432=199757632"
},
{
"input": "100+10=96454",
"output": "86100+10354=96454"
},
{
"input": "111111+111111=999999",
"output": "888111111+111888111=999999222"
},
{
"input": "111111+999999=999999",
"output": "111111+999799988=999911099"
},
{
"input": "999999+111111=999999",
"output": "999799988+111111=999911099"
},
{
"input": "999999+999999=999999",
"output": "999999+8999999=9999998"
},
{
"input": "1000000+1000000=1000000",
"output": "10000000+1000000=11000000"
},
{
"input": "105126+379125=440715",
"output": "6105126+37971025=44076151"
},
{
"input": "166755+62051=70160",
"output": "1667550+6234051=7901601"
},
{
"input": "637089+915955=984094",
"output": "6837089+91595005=98432094"
},
{
"input": "615188+948759=924417",
"output": "961518658+948759=962467417"
},
{
"input": "593287+497915=864740",
"output": "51493287+34979153=86472440"
},
{
"input": "87738+530718=805063",
"output": "27877380+53073183=80950563"
},
{
"input": "65837+79874=229034",
"output": "65837160+7091874=72929034"
},
{
"input": "755991+187301=743241",
"output": "70559910+1873031=72432941"
},
{
"input": "417016+849549=39297",
"output": "4170160+849549137=853719297"
},
{
"input": "78042+511798=819002",
"output": "78041020+5117982=83159002"
},
{
"input": "255419+174046=115059",
"output": "2554190+117404869=119959059"
},
{
"input": "916444+352646=894764",
"output": "891264404+3526460=894790864"
},
{
"input": "577470+14895=190821",
"output": "577470+18489351=19066821"
},
{
"input": "754847+677143=486877",
"output": "7154847+67714030=74868877"
},
{
"input": "415873+855744=266582",
"output": "41500873+85157404=126658277"
},
{
"input": "593250+517992=562639",
"output": "593250+561769942=562363192"
},
{
"input": "254275+180240=858696",
"output": "20542725+18044240=38586965"
},
{
"input": "249035+655369=687650",
"output": "2490735+65536915=68027650"
}
] | 278 | 8,294,400 | 0 | 20,773 |
309 | Sheep | [
"binary search",
"greedy"
] | null | null | Information technologies are developing and are increasingly penetrating into all spheres of human activity. Incredible as it is, the most modern technology are used in farming!
A large farm has a meadow with grazing sheep. Overall there are *n* sheep and each of them contains a unique number from 1 to *n* — because the sheep need to be distinguished and you need to remember information about each one, and they are so much alike! The meadow consists of infinite number of regions numbered from 1 to infinity. It's known that sheep *i* likes regions from *l**i* to *r**i*.
There are two shepherds taking care of the sheep: First and Second. First wakes up early in the morning and leads the sheep graze on the lawn. Second comes in the evening and collects all the sheep.
One morning, First woke up a little later than usual, and had no time to lead the sheep graze on the lawn. So he tied together every two sheep if there is a region they both like. First thought that it would be better — Second would have less work in the evening, because sheep won't scatter too much, being tied to each other!
In the evening Second came on the lawn, gathered the sheep and tried to line them up in a row. But try as he might, the sheep wouldn't line up as Second want! Second had neither the strength nor the ability to untie the sheep so he left them as they are, but with one condition: he wanted to line up the sheep so that the maximum distance between two tied sheep was as small as possible. The distance between the sheep is the number of sheep in the ranks that are between these two.
Help Second find the right arrangement. | The first input line contains one integer *n* (1<=≤<=*n*<=≤<=2000). Each of the following *n* lines contains two integers *l**i* and *r**i* (1<=≤<=*l**i*,<=*r**i*<=≤<=109; *l**i*<=≤<=*r**i*). | In the single output line print *n* space-separated numbers — the sought arrangement of the sheep. The *i*-th value in the line must represent the number of the sheep that took the *i*-th place from left in the optimal arrangement line.
If there are multiple optimal arrangements, print any of them. | [
"3\n1 3\n5 7\n2 4\n",
"5\n1 5\n2 4\n3 6\n1 7\n2 6\n",
"4\n1 3\n4 6\n5 7\n2 3\n"
] | [
"1 3 2",
"2 1 3 5 4",
"1 4 2 3"
] | none | [] | 31 | 6,656,000 | 0 | 20,785 |
|
543 | Road Improvement | [
"dp",
"trees"
] | null | null | The country has *n* cities and *n*<=-<=1 bidirectional roads, it is possible to get from every city to any other one if you move only along the roads. The cities are numbered with integers from 1 to *n* inclusive.
All the roads are initially bad, but the government wants to improve the state of some roads. We will assume that the citizens are happy about road improvement if the path from the capital located in city *x* to any other city contains at most one bad road.
Your task is — for every possible *x* determine the number of ways of improving the quality of some roads in order to meet the citizens' condition. As those values can be rather large, you need to print each value modulo 1<=000<=000<=007 (109<=+<=7). | The first line of the input contains a single integer *n* (2<=≤<=*n*<=≤<=2·105) — the number of cities in the country. Next line contains *n*<=-<=1 positive integers *p*2,<=*p*3,<=*p*4,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=*i*<=-<=1) — the description of the roads in the country. Number *p**i* means that the country has a road connecting city *p**i* and city *i*. | Print *n* integers *a*1,<=*a*2,<=...,<=*a**n*, where *a**i* is the sought number of ways to improve the quality of the roads modulo 1<=000<=000<=007 (109<=+<=7), if the capital of the country is at city number *i*. | [
"3\n1 1\n",
"5\n1 2 3 4\n"
] | [
"4 3 3",
"5 8 9 8 5"
] | none | [
{
"input": "3\n1 1",
"output": "4 3 3"
},
{
"input": "5\n1 2 3 4",
"output": "5 8 9 8 5"
},
{
"input": "31\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
"output": "73741817 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913"
},
{
"input": "29\n1 2 2 4 4 6 6 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28",
"output": "191 380 191 470 236 506 254 506 504 500 494 486 476 464 450 434 416 396 374 350 324 296 266 234 200 164 126 86 44"
},
{
"input": "70\n1 2 2 4 4 6 6 8 9 9 11 11 13 13 15 15 17 17 19 19 21 22 22 24 24 26 27 27 29 29 31 31 33 34 34 36 37 37 39 39 41 42 42 44 44 46 47 47 49 50 50 52 52 54 54 56 57 57 59 60 60 62 63 63 65 65 67 68 68",
"output": "0 1000000005 0 499999996 249999999 749999986 374999994 874999963 999999938 499999970 62499881 531249945 93749781 546874895 109374581 554687295 117186681 558593345 121092131 560546070 123043656 124995179 562497594 125968539 562984274 126450416 126932291 563466150 127163621 563581815 127260071 563630040 127269866 127279659 563639834 127207694 127135727 563567868 126946019 563473014 126543716 126141411 563070710 125325359 562662684 123687534 122049707 561024858 118771194 115492679 557746344 108934221 55446711..."
},
{
"input": "59\n1 2 2 4 4 5 7 7 8 8 10 10 11 11 15 15 9 18 18 20 20 21 23 22 22 26 6 6 28 30 30 31 31 33 34 34 32 32 38 40 39 39 29 44 44 45 47 47 46 46 50 52 52 54 51 51 57 58",
"output": "0 1000000005 0 499999996 499259752 500131906 498519506 453903141 456877573 963122521 230821046 981561265 981561265 115410524 784656845 892328427 892328427 415235638 207617820 331951678 748963765 998815735 165975843 582987926 999407872 332543823 666271916 492735403 494450227 485338898 330005231 366989446 553336825 864004193 776668417 932002101 932002101 775242091 893591565 183494727 591747368 946795787 946795787 488768546 73973791 454675898 659179041 829589525 829589525 147841416 181934138 841006939 9205034..."
},
{
"input": "2\n1",
"output": "2 2"
},
{
"input": "3\n1 2",
"output": "3 4 3"
},
{
"input": "69\n1 1 3 3 5 5 7 8 8 10 10 12 12 14 14 16 16 18 18 20 21 21 23 23 25 26 26 28 28 30 30 32 33 33 35 36 36 38 38 40 41 41 43 43 45 46 46 48 49 49 51 51 53 53 55 56 56 58 59 59 61 62 62 64 64 66 67 67",
"output": "1000000006 500000004 499999999 750000004 749999993 875000001 874999978 999999961 999999985 62499920 31249961 93749852 46874927 109374716 54687359 117186944 58593473 121092650 60546326 123044687 124996722 62498362 125971106 62985554 126455031 126938954 63469478 127174380 63587191 127279022 63639512 127305201 127331378 63665690 127292181 127252982 63626492 127128810 63564406 126857579 126586346 63293174 126032438 63016220 124918901 123805362 61902682 121575425 119345486 59672744 114884180 57442091 105960854 ..."
},
{
"input": "137\n1 1 3 3 5 5 7 8 8 10 10 12 12 14 14 16 16 18 18 20 21 21 23 23 25 26 26 28 28 30 30 32 33 33 35 36 36 38 38 40 41 41 43 43 45 46 46 48 49 49 51 51 53 53 55 56 56 58 59 59 61 62 62 64 64 66 67 67 1 1 71 71 73 73 75 76 76 78 78 80 80 82 82 84 84 86 86 88 89 89 91 91 93 94 94 96 96 98 98 100 101 101 103 104 104 106 106 108 109 109 111 111 113 114 114 116 117 117 119 119 121 121 123 124 124 126 127 127 129 130 130 132 132 134 135 135",
"output": "1 500000005 500000005 750000007 750000007 875000008 875000008 0 1 62499998 31250000 93749994 46874998 109374986 54687494 117187470 58593736 121093688 60546845 123046749 124999808 62499905 125976240 62988121 126464261 126952280 63476141 127195898 63597950 127316924 63658463 127375871 127434816 63717409 127461155 127487492 63743747 127494392 63747197 127485305 127476216 63738109 127446596 63723299 127381635 127316672 63658337 127183887 127051100 63525551 126784098 63392050 126249380 63124691 125179587 124109..."
},
{
"input": "150\n1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 14 15 15 15 16 16 16 17 17 17 18 18 18 19 19 19 20 20 20 21 21 21 22 22 22 23 23 23 24 24 24 25 25 25 26 26 26 27 27 27 28 28 28 28 29 29 29 29 30 30 30 30 31 31 31 31 31 32 32 32 32 32 33 33 33 33 33 33 34 34 34 34 34 34 35 35 35 35 35 35 35 36 36 36 36 36 36 36 37 37 37 37 37 37 37 37 37",
"output": "0 1000000005 0 0 0 0 800000008 800000008 800000008 800000008 800000008 800000008 800000008 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 222222230 705882372 705882372 705882372 878787915 878787915 61538524 61538524 596899355 596899355 196881603 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 400000005 111111116 111111116 111111116 111111116 11..."
}
] | 452 | 21,913,600 | 3 | 20,900 |
|
68 | Contact | [
"geometry"
] | E. Contact | 3 | 256 | Little Petya is preparing for the first contact with aliens. He knows that alien spaceships have shapes of non-degenerate triangles and there will be exactly 4 ships. Landing platform for a ship can be made of 3 special columns located at some points of a Cartesian plane such that these 3 points form a triangle equal to the ship with respect to rotations, translations (parallel shifts along some vector) and reflections (symmetries along the edges). The ships can overlap after the landing.
Each column can be used to land more than one ship, for example, if there are two equal ships, we don't need to build 6 columns to land both ships, 3 will be enough. Petya wants to know what minimum number of columns will be enough to land all ships. | Each of 4 lines will contain 6 integers *x*1 *y*1 *x*2 *y*2 *x*3 *y*3 (0<=≤<=*x*1,<=*y*1,<=*x*2,<=*y*2,<=*x*3,<=*y*3<=≤<=20), representing 3 points that describe the shape of each of 4 ships. It is guaranteed that 3 points in each line will represent a non-degenerate triangle. | First line should contain minimum number of columns enough to land all spaceships. | [
"0 0 1 0 1 2\n0 0 0 2 2 2\n0 0 3 0 1 2\n0 0 3 0 2 2\n",
"0 0 0 1 1 1\n0 0 0 2 2 2\n0 0 0 5 5 5\n0 0 0 17 17 17\n"
] | [
"4\n",
"9\n"
] | In the first test case columns can be put in these points: (0, 0), (1, 0), (3, 0), (1, 2). Note that the second ship can land using last 3 columns.
In the second test case following points can be chosen: (0, 0), (0, 1), (1, 0), (0, 2), (2, 0), (0, 5), (5, 0), (0, 17), (17, 0). It is impossible to use less than 9 columns. | [] | 62 | 0 | 0 | 20,949 |
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\nacbbcccbcbcbbbabaabbbcbacbaabbabaacccaaaaaabbbaacbaabcbcacbbacacbaaaaccbbcacbbcba\ncbbab",
"output": "Yes\n1 30"
},
{
"input": "12 3 4\ndbcbabbbdcbb\nbba",
"output": "No"
},
{
"input": "18 3 4\ndcdbdbadddbddbbccc\nbba",
"output": "Yes\n1 6"
},
{
"input": "7 3 3\naaabbaa\naaa",
"output": "Yes\n1 4"
},
{
"input": "8 3 4\nbcbaaaax\nbcb",
"output": "Yes\n1 5"
},
{
"input": "30 17 9\ndbcdcdcdbddaabcbdcdbdacaddcdbc\nbcdcdcdbdabcbdcdb",
"output": "Yes\n2 13"
},
{
"input": "25 7 12\nbbabaabbabbabbbabbabaabba\nbabaabb",
"output": "Yes\n2 14"
},
{
"input": "28 14 9\ndeaeeabadeaecbebddaeaedebbdd\nbadeaecbeaeaed",
"output": "Yes\n7 19"
},
{
"input": "12 8 4\naaaaabbbaaab\naaaaaaaa",
"output": "No"
},
{
"input": "2 2 1\nab\naa",
"output": "No"
},
{
"input": "2 2 1\naa\naa",
"output": "Yes\n1 2"
},
{
"input": "4 3 2\naaba\naaa",
"output": "No"
},
{
"input": "4 2 2\nacba\naa",
"output": "No"
},
{
"input": "27 13 12\nbaababbabbbbaaaabaabbbbaaaa\nbaababbabbbba",
"output": "Yes\n1 13"
},
{
"input": "27 11 9\nbbaabababaaaabbbbabaababaab\nabababaabab",
"output": "Yes\n2 17"
},
{
"input": "7 3 2\naabcbaa\naaa",
"output": "Yes\n1 6"
}
] | 62 | 7,065,600 | 0 | 20,960 |
|
500 | New Year Permutation | [
"dfs and similar",
"dsu",
"graphs",
"greedy",
"math",
"sortings"
] | null | null | User ainta has a permutation *p*1,<=*p*2,<=...,<=*p**n*. As the New Year is coming, he wants to make his permutation as pretty as possible.
Permutation *a*1,<=*a*2,<=...,<=*a**n* is prettier than permutation *b*1,<=*b*2,<=...,<=*b**n*, if and only if there exists an integer *k* (1<=≤<=*k*<=≤<=*n*) where *a*1<==<=*b*1,<=*a*2<==<=*b*2,<=...,<=*a**k*<=-<=1<==<=*b**k*<=-<=1 and *a**k*<=<<=*b**k* all holds.
As known, permutation *p* is so sensitive that it could be only modified by swapping two distinct elements. But swapping two elements is harder than you think. Given an *n*<=×<=*n* binary matrix *A*, user ainta can swap the values of *p**i* and *p**j* (1<=≤<=*i*,<=*j*<=≤<=*n*, *i*<=≠<=*j*) if and only if *A**i*,<=*j*<==<=1.
Given the permutation *p* and the matrix *A*, user ainta wants to know the prettiest permutation that he can obtain. | The first line contains an integer *n* (1<=≤<=*n*<=≤<=300) — the size of the permutation *p*.
The second line contains *n* space-separated integers *p*1,<=*p*2,<=...,<=*p**n* — the permutation *p* that user ainta has. Each integer between 1 and *n* occurs exactly once in the given permutation.
Next *n* lines describe the matrix *A*. The *i*-th line contains *n* characters '0' or '1' and describes the *i*-th row of *A*. The *j*-th character of the *i*-th line *A**i*,<=*j* is the element on the intersection of the *i*-th row and the *j*-th column of A. It is guaranteed that, for all integers *i*,<=*j* where 1<=≤<=*i*<=<<=*j*<=≤<=*n*, *A**i*,<=*j*<==<=*A**j*,<=*i* holds. Also, for all integers *i* where 1<=≤<=*i*<=≤<=*n*, *A**i*,<=*i*<==<=0 holds. | In the first and only line, print *n* space-separated integers, describing the prettiest permutation that can be obtained. | [
"7\n5 2 4 3 6 7 1\n0001001\n0000000\n0000010\n1000001\n0000000\n0010000\n1001000\n",
"5\n4 2 1 5 3\n00100\n00011\n10010\n01101\n01010\n"
] | [
"1 2 4 3 6 7 5\n",
"1 2 3 4 5\n"
] | In the first sample, the swap needed to obtain the prettiest permutation is: (*p*<sub class="lower-index">1</sub>, *p*<sub class="lower-index">7</sub>).
In the second sample, the swaps needed to obtain the prettiest permutation is (*p*<sub class="lower-index">1</sub>, *p*<sub class="lower-index">3</sub>), (*p*<sub class="lower-index">4</sub>, *p*<sub class="lower-index">5</sub>), (*p*<sub class="lower-index">3</sub>, *p*<sub class="lower-index">4</sub>).
A permutation *p* is a sequence of integers *p*<sub class="lower-index">1</sub>, *p*<sub class="lower-index">2</sub>, ..., *p*<sub class="lower-index">*n*</sub>, consisting of *n* distinct positive integers, each of them doesn't exceed *n*. The *i*-th element of the permutation *p* is denoted as *p*<sub class="lower-index">*i*</sub>. The size of the permutation *p* is denoted as *n*. | [
{
"input": "7\n5 2 4 3 6 7 1\n0001001\n0000000\n0000010\n1000001\n0000000\n0010000\n1001000",
"output": "1 2 4 3 6 7 5"
},
{
"input": "5\n4 2 1 5 3\n00100\n00011\n10010\n01101\n01010",
"output": "1 2 3 4 5"
},
{
"input": "7\n1 7 6 4 2 3 5\n0000100\n0000010\n0000001\n0000000\n1000000\n0100000\n0010000",
"output": "1 3 5 4 2 7 6"
},
{
"input": "15\n6 1 2 7 9 13 14 8 4 5 3 12 10 15 11\n000100100100100\n000010010010010\n000001001001001\n100000100100100\n010000010010010\n001000001001001\n100100000100100\n010010000010010\n001001000001001\n100100100000100\n010010010000010\n001001001000001\n100100100100000\n010010010010000\n001001001001000",
"output": "5 1 2 6 3 4 7 8 11 10 9 12 14 15 13"
},
{
"input": "2\n2 1\n01\n10",
"output": "1 2"
},
{
"input": "2\n2 1\n00\n00",
"output": "2 1"
},
{
"input": "3\n3 1 2\n001\n000\n100",
"output": "2 1 3"
},
{
"input": "3\n3 2 1\n001\n000\n100",
"output": "1 2 3"
},
{
"input": "4\n1 3 2 4\n0000\n0010\n0100\n0000",
"output": "1 2 3 4"
},
{
"input": "10\n5 1 6 2 8 3 4 10 9 7\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000",
"output": "5 1 6 2 8 3 4 10 9 7"
},
{
"input": "10\n5 1 6 2 8 3 4 10 9 7\n0001000000\n0000000000\n0000000001\n1000000010\n0000010000\n0000100000\n0000000000\n0000000000\n0001000000\n0010000000",
"output": "2 1 6 5 3 8 4 10 9 7"
},
{
"input": "10\n5 1 6 2 8 3 4 10 9 7\n0000000000\n0000010000\n0000010000\n0000010000\n0000010000\n0111101011\n0000010000\n0000000000\n0000010000\n0000010000",
"output": "5 1 2 3 4 6 7 10 8 9"
},
{
"input": "1\n1\n0",
"output": "1"
},
{
"input": "3\n2 1 3\n001\n001\n110",
"output": "1 2 3"
},
{
"input": "3\n2 3 1\n011\n100\n100",
"output": "1 2 3"
},
{
"input": "3\n3 1 2\n011\n100\n100",
"output": "1 2 3"
},
{
"input": "3\n1 3 2\n011\n100\n100",
"output": "1 2 3"
},
{
"input": "4\n3 2 1 4\n0001\n0010\n0101\n1010",
"output": "1 2 3 4"
},
{
"input": "4\n3 4 2 1\n0100\n1010\n0101\n0010",
"output": "1 2 3 4"
},
{
"input": "7\n7 6 5 4 3 2 1\n0100000\n1010000\n0101000\n0010100\n0001010\n0000101\n0000010",
"output": "1 2 3 4 5 6 7"
},
{
"input": "6\n6 5 4 3 2 1\n010000\n101000\n010100\n001010\n000101\n000010",
"output": "1 2 3 4 5 6"
},
{
"input": "3\n3 2 1\n011\n100\n100",
"output": "1 2 3"
},
{
"input": "5\n5 4 3 2 1\n00001\n00001\n00001\n00001\n11110",
"output": "1 2 3 4 5"
},
{
"input": "3\n3 1 2\n001\n001\n110",
"output": "1 2 3"
},
{
"input": "4\n4 3 2 1\n0100\n1001\n0000\n0100",
"output": "1 3 2 4"
},
{
"input": "5\n5 4 3 2 1\n01000\n10100\n01010\n00101\n00010",
"output": "1 2 3 4 5"
},
{
"input": "4\n2 1 3 4\n0010\n0010\n1100\n0000",
"output": "1 2 3 4"
},
{
"input": "4\n3 4 1 2\n0110\n1000\n1001\n0010",
"output": "1 2 3 4"
}
] | 46 | 0 | 0 | 20,986 |
|
235 | Let's Play Osu! | [
"dp",
"math",
"probabilities"
] | null | null | You're playing a game called Osu! Here's a simplified version of it. There are *n* clicks in a game. For each click there are two outcomes: correct or bad. Let us denote correct as "O", bad as "X", then the whole play can be encoded as a sequence of *n* characters "O" and "X".
Using the play sequence you can calculate the score for the play as follows: for every maximal consecutive "O"s block, add the square of its length (the number of characters "O") to the score. For example, if your play can be encoded as "OOXOOOXXOO", then there's three maximal consecutive "O"s block "OO", "OOO", "OO", so your score will be 22<=+<=32<=+<=22<==<=17. If there are no correct clicks in a play then the score for the play equals to 0.
You know that the probability to click the *i*-th (1<=≤<=*i*<=≤<=*n*) click correctly is *p**i*. In other words, the *i*-th character in the play sequence has *p**i* probability to be "O", 1<=-<=*p**i* to be "X". You task is to calculate the expected score for your play. | The first line contains an integer *n* (1<=≤<=*n*<=≤<=105) — the number of clicks. The second line contains *n* space-separated real numbers *p*1,<=*p*2,<=...,<=*p**n* (0<=≤<=*p**i*<=≤<=1).
There will be at most six digits after the decimal point in the given *p**i*. | Print a single real number — the expected score for your play. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. | [
"3\n0.5 0.5 0.5\n",
"4\n0.7 0.2 0.1 0.9\n",
"5\n1 1 1 1 1\n"
] | [
"2.750000000000000\n",
"2.489200000000000\n",
"25.000000000000000\n"
] | For the first example. There are 8 possible outcomes. Each has a probability of 0.125.
- "OOO" → 3<sup class="upper-index">2</sup> = 9; - "OOX" → 2<sup class="upper-index">2</sup> = 4; - "OXO" → 1<sup class="upper-index">2</sup> + 1<sup class="upper-index">2</sup> = 2; - "OXX" → 1<sup class="upper-index">2</sup> = 1; - "XOO" → 2<sup class="upper-index">2</sup> = 4; - "XOX" → 1<sup class="upper-index">2</sup> = 1; - "XXO" → 1<sup class="upper-index">2</sup> = 1; - "XXX" → 0.
So the expected score is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/9aa477286058d4dd02d6f78cad11a658e4f37440.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "3\n0.5 0.5 0.5",
"output": "2.750000000000000"
},
{
"input": "4\n0.7 0.2 0.1 0.9",
"output": "2.489200000000000"
},
{
"input": "5\n1 1 1 1 1",
"output": "25.000000000000000"
},
{
"input": "10\n0.684846 0.156794 0.153696 0.714526 0.281868 0.628256 0.745339 0.123854 0.748936 0.856333",
"output": "10.721778814471227"
},
{
"input": "10\n0.684488 0.834971 0.834886 0.643646 0.162710 0.119851 0.659401 0.743950 0.220986 0.839665",
"output": "15.401334613504345"
},
{
"input": "10\n0.684416 0.170607 0.491124 0.469470 0.458879 0.658170 0.322214 0.707969 0.275396 0.836331",
"output": "11.404416796704293"
},
{
"input": "10\n0.684631 0.563700 0.722410 0.191998 0.370373 0.643213 0.533776 0.815911 0.112166 0.846332",
"output": "12.888929008957161"
},
{
"input": "10\n0.684559 0.699336 0.378648 0.817822 0.666542 0.381532 0.196589 0.779930 0.166576 0.842998",
"output": "14.036752909261951"
},
{
"input": "10\n0.999453 0.999188 0.998398 0.999609 0.999113 0.999426 0.998026 0.999244 0.998842 0.999807",
"output": "99.590738622894690"
},
{
"input": "10\n0.000733 0.000769 0.000772 0.000595 0.000930 0.000395 0.000596 0.000584 0.000496 0.000905",
"output": "0.006782723279203"
},
{
"input": "30\n0.684344 0.306242 0.147362 0.295294 0.755047 0.396489 0.785026 0.671988 0.329806 0.832998 0.106621 0.452498 0.125067 0.838169 0.869683 0.740625 0.449522 0.751800 0.272185 0.865612 0.272859 0.416162 0.339155 0.478441 0.401937 0.626148 0.305498 0.716523 0.734322 0.751335",
"output": "44.576745047411691"
},
{
"input": "30\n0.684273 0.441878 0.603600 0.121118 0.251216 0.134808 0.447839 0.636007 0.384215 0.829664 0.204397 0.627395 0.243031 0.424765 0.525065 0.585464 0.893844 0.377080 0.246110 0.356372 0.836239 0.670558 0.546182 0.310427 0.343287 0.868653 0.269521 0.432699 0.288850 0.848816",
"output": "36.478162706163317"
},
{
"input": "30\n0.683914 0.320055 0.484789 0.850238 0.132058 0.426403 0.361901 0.456102 0.656265 0.812996 0.693279 0.701878 0.832853 0.757747 0.401974 0.609660 0.715452 0.103482 0.115733 0.210174 0.453140 0.342538 0.781317 0.270359 0.850037 0.481183 0.889637 0.613578 0.461492 0.536221",
"output": "53.227679791398110"
},
{
"input": "30\n0.683843 0.455691 0.141027 0.676062 0.428227 0.164722 0.824714 0.420121 0.710675 0.809662 0.791055 0.876775 0.150817 0.344344 0.857356 0.454499 0.359774 0.528762 0.889658 0.500934 0.216520 0.596934 0.188344 0.102346 0.791387 0.723689 0.853660 0.329754 0.816020 0.633702",
"output": "49.054872575308515"
},
{
"input": "30\n0.684058 0.848784 0.372313 0.398590 0.339721 0.149765 0.236276 0.528064 0.547445 0.819663 0.497726 0.352085 0.596924 0.784554 0.291210 0.119982 0.626809 0.852921 0.167884 0.428653 0.126380 0.633746 0.367263 0.606386 0.167337 0.796171 0.161591 0.381226 0.552435 0.341259",
"output": "33.125615383310461"
},
{
"input": "30\n0.999453 0.998210 0.999812 0.998309 0.999333 0.999463 0.999490 0.998975 0.999248 0.999782 0.999233 0.999062 0.999530 0.998674 0.999608 0.999654 0.998426 0.998941 0.998104 0.999541 0.999467 0.999961 0.999180 0.998842 0.998022 0.998345 0.998064 0.999984 0.998017 0.998843",
"output": "891.219052952586820"
},
{
"input": "30\n0.000735 0.000533 0.000518 0.000044 0.000677 0.000571 0.000138 0.000707 0.000793 0.000018 0.000326 0.000635 0.000789 0.000298 0.000445 0.000077 0.000226 0.000128 0.000933 0.000961 0.000726 0.000405 0.000610 0.000102 0.000990 0.000989 0.000254 0.000580 0.000053 0.000142",
"output": "0.014416714297575"
}
] | 342 | 11,776,000 | 3 | 21,000 |
|
132 | Constants in the language of Shakespeare | [
"constructive algorithms",
"dp",
"greedy"
] | null | null | Shakespeare is a widely known esoteric programming language in which programs look like plays by Shakespeare, and numbers are given by combinations of ornate epithets. In this problem we will have a closer look at the way the numbers are described in Shakespeare.
Each constant in Shakespeare is created from non-negative powers of 2 using arithmetic operations. For simplicity we'll allow only addition and subtraction and will look for a representation of the given number which requires a minimal number of operations.
You are given an integer *n*. You have to represent it as *n*<==<=*a*1<=+<=*a*2<=+<=...<=+<=*a**m*, where each of *a**i* is a non-negative power of 2, possibly multiplied by -1. Find a representation which minimizes the value of *m*. | The only line of input contains a positive integer *n*, written as its binary notation. The length of the notation is at most 106. The first digit of the notation is guaranteed to be 1. | Output the required minimal *m*. After it output *m* lines. Each line has to be formatted as "+2^x" or "-2^x", where *x* is the power coefficient of the corresponding term. The order of the lines doesn't matter. | [
"1111\n",
"1010011\n"
] | [
"2\n+2^4\n-2^0\n",
"4\n+2^0\n+2^1\n+2^4\n+2^6\n"
] | none | [
{
"input": "1111",
"output": "2\n+2^4\n-2^0"
},
{
"input": "1010011",
"output": "4\n+2^0\n+2^1\n+2^4\n+2^6"
},
{
"input": "1",
"output": "1\n+2^0"
},
{
"input": "10110111",
"output": "4\n+2^8\n-2^6\n-2^3\n-2^0"
},
{
"input": "10101110",
"output": "4\n+2^8\n-2^6\n-2^4\n-2^1"
},
{
"input": "1011001",
"output": "4\n+2^0\n+2^3\n+2^4\n+2^6"
},
{
"input": "10001",
"output": "2\n+2^0\n+2^4"
},
{
"input": "10",
"output": "1\n+2^1"
},
{
"input": "11",
"output": "2\n+2^0\n+2^1"
},
{
"input": "100",
"output": "1\n+2^2"
},
{
"input": "100",
"output": "1\n+2^2"
},
{
"input": "111",
"output": "2\n+2^3\n-2^0"
},
{
"input": "1000000000",
"output": "1\n+2^9"
},
{
"input": "1011000000",
"output": "3\n+2^6\n+2^7\n+2^9"
},
{
"input": "1100010000",
"output": "3\n+2^4\n+2^8\n+2^9"
},
{
"input": "1000111001",
"output": "4\n+2^0\n+2^6\n-2^3\n+2^9"
},
{
"input": "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"output": "1\n+2^99"
},
{
"input": "1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"output": "1\n+2^99"
},
{
"input": "1000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000",
"output": "2\n+2^18\n+2^99"
},
{
"input": "1000000000000000000000000000010000000000000000100000000000100000000000000000000000000000000000000000",
"output": "4\n+2^41\n+2^53\n+2^70\n+2^99"
},
{
"input": "1000000000000000000100000001000000000000000000100000101000000000000000000000100000000000000100000000",
"output": "8\n+2^8\n+2^23\n+2^45\n+2^47\n+2^53\n+2^72\n+2^80\n+2^99"
},
{
"input": "1000100000101000000100100000000000000000100000000000000010000010000001010010100000000000000001001000",
"output": "15\n+2^3\n+2^6\n+2^23\n+2^25\n+2^28\n+2^30\n+2^37\n+2^43\n+2^59\n+2^77\n+2^80\n+2^87\n+2^89\n+2^95\n+2^99"
},
{
"input": "1000001001000011000010001000100110010110100000000101010101000100100010000100000100000100001000000000",
"output": "26\n+2^9\n+2^14\n+2^20\n+2^26\n+2^31\n+2^35\n+2^38\n+2^42\n+2^44\n+2^46\n+2^48\n+2^50\n+2^59\n+2^61\n+2^62\n+2^64\n+2^67\n+2^68\n+2^71\n+2^75\n+2^79\n+2^84\n+2^85\n+2^90\n+2^93\n+2^99"
},
{
"input": "1110000000001011100111001010000010101100010101001101001010101100011101100110000010000101110101000011",
"output": "37\n+2^0\n+2^1\n+2^6\n+2^8\n+2^15\n-2^13\n-2^10\n+2^19\n+2^25\n+2^26\n+2^35\n-2^31\n-2^29\n+2^38\n+2^39\n+2^41\n+2^43\n+2^45\n+2^48\n+2^50\n+2^51\n+2^54\n+2^56\n+2^58\n+2^62\n+2^63\n+2^65\n+2^67\n+2^73\n+2^75\n+2^81\n-2^78\n+2^88\n-2^86\n-2^83\n+2^100\n-2^97"
},
{
"input": "1001110101111100101111011111111111101010111111010111011111111111011111111111111100011011111111111101",
"output": "20\n+2^0\n+2^17\n-2^14\n-2^2\n+2^84\n-2^82\n-2^77\n-2^64\n-2^62\n-2^60\n-2^53\n-2^51\n-2^47\n-2^35\n-2^20\n+2^97\n-2^93\n-2^91\n-2^86\n+2^99"
},
{
"input": "1111111101111111101111111111101111111111111111111111101111111011111110011111101111111110111111111111",
"output": "11\n+2^29\n-2^22\n-2^12\n-2^0\n+2^100\n-2^91\n-2^82\n-2^70\n-2^46\n-2^38\n-2^31"
},
{
"input": "100000000000000000000000000000000",
"output": "1\n+2^32"
},
{
"input": "111111000111111000111111000111111",
"output": "8\n+2^6\n-2^0\n+2^15\n-2^9\n+2^24\n-2^18\n+2^33\n-2^27"
},
{
"input": "10001100000000000011011011",
"output": "7\n+2^8\n-2^5\n-2^2\n-2^0\n+2^20\n+2^21\n+2^25"
},
{
"input": "1101011",
"output": "4\n+2^7\n-2^4\n-2^2\n-2^0"
},
{
"input": "11000101010000101101101101111000100100001101001111000011011100",
"output": "21\n+2^8\n-2^5\n-2^2\n+2^16\n-2^12\n+2^18\n+2^20\n+2^21\n+2^26\n+2^29\n+2^48\n-2^46\n-2^43\n-2^40\n-2^37\n-2^33\n+2^52\n+2^54\n+2^56\n+2^60\n+2^61"
},
{
"input": "11101011",
"output": "4\n+2^8\n-2^4\n-2^2\n-2^0"
}
] | 46 | 0 | 0 | 21,040 |
|
0 | none | [
"none"
] | null | null | На тренировку по подготовке к соревнованиям по программированию пришли *n* команд. Тренер для каждой команды подобрал тренировку, комплект задач для *i*-й команды занимает *a**i* страниц. В распоряжении тренера есть *x* листов бумаги, у которых обе стороны чистые, и *y* листов, у которых только одна сторона чистая. При печати условия на листе первого типа можно напечатать две страницы из условий задач, а при печати на листе второго типа — только одну. Конечно, на листе нельзя печатать условия из двух разных комплектов задач. Обратите внимание, что при использовании листов, у которых обе стороны чистые, не обязательно печатать условие на обеих сторонах, одна из них может остаться чистой.
Вам предстоит определить максимальное количество команд, которым тренер сможет напечатать комплекты задач целиком. | В первой строке входных данных следуют три целых числа *n*, *x* и *y* (1<=≤<=*n*<=≤<=200<=000, 0<=≤<=*x*,<=*y*<=≤<=109) — количество команд, количество листов бумаги с двумя чистыми сторонами и количество листов бумаги с одной чистой стороной.
Во второй строке входных данных следует последовательность из *n* целых чисел *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=10<=000), где *i*-е число равно количеству страниц в комплекте задач для *i*-й команды. | Выведите единственное целое число — максимальное количество команд, которым тренер сможет напечатать комплекты задач целиком. | [
"2 3 5\n4 6\n",
"2 3 5\n4 7\n",
"6 3 5\n12 11 12 11 12 11\n"
] | [
"2\n",
"2\n",
"1\n"
] | В первом тестовом примере можно напечатать оба комплекта задач. Один из возможных ответов — напечатать весь первый комплект задач на листах с одной чистой стороной (после этого останется 3 листа с двумя чистыми сторонами и 1 лист с одной чистой стороной), а второй комплект напечатать на трех листах с двумя чистыми сторонами.
Во втором тестовом примере можно напечатать оба комплекта задач. Один из возможных ответов — напечатать первый комплект задач на двух листах с двумя чистыми сторонами (после этого останется 1 лист с двумя чистыми сторонами и 5 листов с одной чистой стороной), а второй комплект напечатать на одном листе с двумя чистыми сторонами и на пяти листах с одной чистой стороной. Таким образом, тренер использует все листы для печати.
В третьем тестовом примере можно напечатать только один комплект задач (любой из трёх 11-страничных). Для печати 11-страничного комплекта задач будет израсходована вся бумага. | [
{
"input": "2 3 5\n4 6",
"output": "2"
},
{
"input": "2 3 5\n4 7",
"output": "2"
},
{
"input": "6 3 5\n12 11 12 11 12 11",
"output": "1"
},
{
"input": "1 4 3\n12",
"output": "0"
},
{
"input": "10 1 17\n3 1 1 2 1 3 4 4 1 4",
"output": "8"
},
{
"input": "3 6 3\n2 5 9",
"output": "2"
},
{
"input": "7 26 8\n5 11 8 10 12 12 3",
"output": "6"
},
{
"input": "11 5 85\n19 20 6 7 6 2 1 5 8 15 6",
"output": "11"
},
{
"input": "7 7 2\n5 2 4 2 4 1 1",
"output": "6"
},
{
"input": "8 5 10\n1 7 2 5 2 1 6 5",
"output": "6"
},
{
"input": "10 27 34\n11 8 11 5 14 1 12 10 12 6",
"output": "9"
},
{
"input": "4 2 2\n1 2 3 1",
"output": "3"
},
{
"input": "5 1 45\n7 14 15 7 7",
"output": "4"
},
{
"input": "9 7 50\n10 9 10 10 8 3 5 10 2",
"output": "8"
},
{
"input": "5 0 0\n100 100 100 200 301",
"output": "0"
},
{
"input": "5 1000000000 1000000000\n100 200 300 400 501",
"output": "5"
},
{
"input": "1 1 0\n1",
"output": "1"
},
{
"input": "1 1 0\n3",
"output": "0"
},
{
"input": "1 0 0\n10000",
"output": "0"
},
{
"input": "1 0 1\n1",
"output": "1"
},
{
"input": "1 1 0\n2",
"output": "1"
},
{
"input": "1 0 0\n1",
"output": "0"
},
{
"input": "1 0 1\n2",
"output": "0"
},
{
"input": "5 4 1\n1 2 1 1 1",
"output": "5"
},
{
"input": "20 5 0\n9 4 1 2 1 1 4 4 9 1 9 3 8 1 8 9 4 1 7 4",
"output": "5"
},
{
"input": "100 1019 35\n34 50 60 47 49 49 59 60 37 51 3 86 93 33 78 31 75 87 26 74 32 30 52 57 44 10 33 52 78 16 36 77 53 49 98 82 93 85 16 86 19 57 17 24 73 93 37 46 27 87 35 76 33 91 96 55 34 65 97 66 7 30 45 68 18 51 77 43 99 76 35 47 6 1 83 49 67 85 89 17 20 7 49 33 43 59 53 71 86 71 3 47 65 59 40 34 35 44 46 64",
"output": "59"
},
{
"input": "2 1 0\n1 1",
"output": "1"
},
{
"input": "2 3 0\n3 3",
"output": "1"
},
{
"input": "1 1000000000 1000000000\n1",
"output": "1"
},
{
"input": "3 2 0\n1 1 1",
"output": "2"
},
{
"input": "2 2 0\n1 3",
"output": "1"
},
{
"input": "1 3 0\n3",
"output": "1"
},
{
"input": "2 2 0\n1 1",
"output": "2"
},
{
"input": "5 1 0\n1 1 1 1 1",
"output": "1"
},
{
"input": "4 2 0\n1 1 1 1",
"output": "2"
},
{
"input": "1 2 0\n3",
"output": "1"
},
{
"input": "4 1 0\n1 1 1 1",
"output": "1"
},
{
"input": "2 2 0\n3 1",
"output": "1"
},
{
"input": "6 1000000000 1000000000\n12 11 12 11 12 11",
"output": "6"
},
{
"input": "2 3 0\n5 1",
"output": "1"
},
{
"input": "1 3 0\n5",
"output": "1"
},
{
"input": "2 1000000000 1000000000\n10000 1000",
"output": "2"
},
{
"input": "5 1000000000 1000000000\n1 2 3 4 5",
"output": "5"
},
{
"input": "2 1 0\n2 2",
"output": "1"
},
{
"input": "2 1000000000 1000000000\n10000 10000",
"output": "2"
},
{
"input": "2 3 0\n3 4",
"output": "1"
},
{
"input": "3 4 0\n3 3 3",
"output": "2"
},
{
"input": "4 3 1\n3 1 1 1",
"output": "3"
},
{
"input": "1 2 0\n1",
"output": "1"
},
{
"input": "5 2 0\n1 1 1 1 1",
"output": "2"
},
{
"input": "2 1000000000 1000000000\n1 1",
"output": "2"
},
{
"input": "3 1 0\n1 1 1",
"output": "1"
}
] | 61 | 4,608,000 | 0 | 21,044 |
|
236 | Easy Number Challenge | [
"implementation",
"number theory"
] | null | null | Let's denote *d*(*n*) as the number of divisors of a positive integer *n*. You are given three integers *a*, *b* and *c*. Your task is to calculate the following sum:
Find the sum modulo 1073741824 (230). | The first line contains three space-separated integers *a*, *b* and *c* (1<=≤<=*a*,<=*b*,<=*c*<=≤<=100). | Print a single integer — the required sum modulo 1073741824 (230). | [
"2 2 2\n",
"5 6 7\n"
] | [
"20\n",
"1520\n"
] | For the first example.
- *d*(1·1·1) = *d*(1) = 1; - *d*(1·1·2) = *d*(2) = 2; - *d*(1·2·1) = *d*(2) = 2; - *d*(1·2·2) = *d*(4) = 3; - *d*(2·1·1) = *d*(2) = 2; - *d*(2·1·2) = *d*(4) = 3; - *d*(2·2·1) = *d*(4) = 3; - *d*(2·2·2) = *d*(8) = 4.
So the result is 1 + 2 + 2 + 3 + 2 + 3 + 3 + 4 = 20. | [
{
"input": "2 2 2",
"output": "20"
},
{
"input": "5 6 7",
"output": "1520"
},
{
"input": "91 42 25",
"output": "3076687"
},
{
"input": "38 47 5",
"output": "160665"
},
{
"input": "82 29 45",
"output": "3504808"
},
{
"input": "40 15 33",
"output": "460153"
},
{
"input": "35 5 21",
"output": "55282"
},
{
"input": "71 2 1",
"output": "811"
},
{
"input": "22 44 41",
"output": "1063829"
},
{
"input": "73 19 29",
"output": "1047494"
},
{
"input": "76 12 17",
"output": "330197"
},
{
"input": "16 10 49",
"output": "146199"
},
{
"input": "59 99 33",
"output": "7052988"
},
{
"input": "17 34 25",
"output": "306673"
},
{
"input": "21 16 9",
"output": "45449"
},
{
"input": "31 51 29",
"output": "1255099"
},
{
"input": "26 41 17",
"output": "402568"
},
{
"input": "85 19 5",
"output": "139747"
},
{
"input": "36 61 45",
"output": "3253358"
},
{
"input": "76 58 25",
"output": "3635209"
},
{
"input": "71 48 13",
"output": "1179722"
},
{
"input": "29 34 53",
"output": "1461871"
},
{
"input": "72 16 41",
"output": "1309118"
},
{
"input": "8 21 21",
"output": "54740"
},
{
"input": "11 51 5",
"output": "38092"
},
{
"input": "70 38 49",
"output": "4467821"
},
{
"input": "13 31 33",
"output": "274773"
},
{
"input": "53 29 17",
"output": "621991"
},
{
"input": "56 18 53",
"output": "1518698"
},
{
"input": "55 45 45",
"output": "3751761"
},
{
"input": "58 35 29",
"output": "1706344"
},
{
"input": "67 2 24",
"output": "45108"
},
{
"input": "62 96 8",
"output": "1257040"
},
{
"input": "21 22 100",
"output": "1274891"
},
{
"input": "64 12 36",
"output": "687986"
},
{
"input": "4 9 20",
"output": "7302"
},
{
"input": "7 99 4",
"output": "36791"
},
{
"input": "58 25 96",
"output": "4812548"
},
{
"input": "9 19 32",
"output": "91192"
},
{
"input": "45 16 12",
"output": "167557"
},
{
"input": "40 6 100",
"output": "558275"
},
{
"input": "46 93 44",
"output": "6945002"
},
{
"input": "49 31 28",
"output": "1158568"
},
{
"input": "89 28 8",
"output": "441176"
},
{
"input": "84 17 96",
"output": "4615400"
},
{
"input": "91 96 36",
"output": "12931148"
},
{
"input": "86 90 24",
"output": "6779764"
},
{
"input": "4 21 45",
"output": "58045"
},
{
"input": "100 7 28",
"output": "429933"
},
{
"input": "58 41 21",
"output": "1405507"
},
{
"input": "53 31 5",
"output": "144839"
},
{
"input": "41 28 36",
"output": "1135934"
},
{
"input": "44 18 24",
"output": "436880"
},
{
"input": "3 96 16",
"output": "70613"
},
{
"input": "98 34 100",
"output": "13589991"
},
{
"input": "82 31 32",
"output": "2502213"
},
{
"input": "85 25 20",
"output": "1142825"
},
{
"input": "35 12 8",
"output": "50977"
},
{
"input": "39 94 48",
"output": "6368273"
},
{
"input": "27 99 28",
"output": "2276216"
},
{
"input": "22 28 16",
"output": "198639"
},
{
"input": "80 15 4",
"output": "76139"
},
{
"input": "23 9 44",
"output": "170773"
},
{
"input": "33 16 36",
"output": "441858"
},
{
"input": "36 6 24",
"output": "88626"
},
{
"input": "98 92 12",
"output": "3475151"
},
{
"input": "90 82 100",
"output": "35482866"
},
{
"input": "77 79 31",
"output": "6870344"
},
{
"input": "81 21 19",
"output": "812886"
},
{
"input": "31 96 7",
"output": "458123"
},
{
"input": "34 89 95",
"output": "11308813"
},
{
"input": "18 86 27",
"output": "1116623"
},
{
"input": "13 76 11",
"output": "206844"
},
{
"input": "76 3 3",
"output": "6118"
},
{
"input": "15 93 87",
"output": "4007595"
},
{
"input": "63 90 23",
"output": "4384553"
},
{
"input": "58 83 7",
"output": "819473"
},
{
"input": "16 18 99",
"output": "702678"
},
{
"input": "60 8 35",
"output": "363723"
},
{
"input": "22 87 4",
"output": "133986"
},
{
"input": "73 25 44",
"output": "2478308"
},
{
"input": "36 3 32",
"output": "50842"
},
{
"input": "27 93 20",
"output": "1393947"
},
{
"input": "67 90 100",
"output": "27880104"
},
{
"input": "18 84 36",
"output": "1564297"
},
{
"input": "68 14 28",
"output": "646819"
},
{
"input": "71 8 12",
"output": "119311"
},
{
"input": "7 5 96",
"output": "46328"
},
{
"input": "50 95 32",
"output": "5324602"
},
{
"input": "13 22 24",
"output": "124510"
},
{
"input": "4 12 8",
"output": "3347"
},
{
"input": "100 9 88",
"output": "2334910"
},
{
"input": "95 2 28",
"output": "82723"
},
{
"input": "54 77 20",
"output": "2573855"
},
{
"input": "49 19 4",
"output": "55037"
},
{
"input": "58 86 99",
"output": "21920084"
},
{
"input": "9 76 83",
"output": "1554836"
},
{
"input": "64 2 27",
"output": "49141"
},
{
"input": "63 96 11",
"output": "1898531"
},
{
"input": "3 93 91",
"output": "555583"
},
{
"input": "100 100 100",
"output": "51103588"
},
{
"input": "1 5 1",
"output": "10"
}
] | 2,000 | 0 | 0 | 21,139 |
|
183 | Candy Shop | [
"greedy"
] | null | null | The prestigious Codeforces kindergarten consists of *n* kids, numbered 1 through *n*. Each of them are given allowance in rubles by their parents.
Today, they are going to the most famous candy shop in the town. The shop sells candies in packages: for all *i* between 1 and *m*, inclusive, it sells a package containing exactly *i* candies. A candy costs one ruble, so a package containing *x* candies costs *x* rubles.
The kids will purchase candies in turns, starting from kid 1. In a single turn, kid *i* will purchase one candy package. Due to the highly competitive nature of Codeforces kindergarten, during a turn, the number of candies contained in the package purchased by the kid will always be strictly greater than the number of candies contained in the package purchased by the kid in the preceding turn (an exception is in the first turn: the first kid may purchase any package). Then, the turn proceeds to kid *i*<=+<=1, or to kid 1 if it was kid *n*'s turn. This process can be ended at any time, but at the end of the purchase process, all the kids must have the same number of candy packages. Of course, the amount spent by each kid on the candies cannot exceed their allowance.
You work at the candy shop and would like to prepare the candies for the kids. Print the maximum number of candies that can be sold by the candy shop to the kids. If the kids cannot purchase any candy (due to insufficient allowance), print 0. | The first line contains two space-separated integers *n* and *m* (2<=≤<=*n*<=≤<=2·105,<=2<=≤<=*m*<=≤<=5·106,<=*n*<=≤<=*m*), denoting the number of kids and the maximum number of candies in a package sold by the candy shop, respectively.
Then *n* lines follow, each line will contain a single positive integer not exceeding denoting the allowance of a kid in rubles. The allowances are given in order from kid 1 to kid *n*.
Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is recommended to use cin, cout streams (also you may use %I64d specificator). | Print a single integer denoting the maximum number of candies that can be sold by the candy shop. | [
"2 5\n5\n10\n",
"3 8\n8\n16\n13\n",
"2 5000000\n12500002500000\n12500002500000\n"
] | [
"13\n",
"32\n",
"12500002500000\n"
] | For the first example, one of the scenarios that will result in 13 purchased candies is as follows.
- Turn 1. Kid 1 purchases 1 candy. - Turn 2. Kid 2 purchases 3 candies. - Turn 3. Kid 1 purchases 4 candies. - Turn 4. Kid 2 purchases 5 candies. | [] | 92 | 0 | 0 | 21,218 |
|
375 | Tree and Queries | [
"data structures",
"dfs and similar",
"trees"
] | null | null | You have a rooted tree consisting of *n* vertices. Each vertex of the tree has some color. We will assume that the tree vertices are numbered by integers from 1 to *n*. Then we represent the color of vertex *v* as *c**v*. The tree root is a vertex with number 1.
In this problem you need to answer to *m* queries. Each query is described by two integers *v**j*,<=*k**j*. The answer to query *v**j*,<=*k**j* is the number of such colors of vertices *x*, that the subtree of vertex *v**j* contains at least *k**j* vertices of color *x*.
You can find the definition of a rooted tree by the following link: http://en.wikipedia.org/wiki/Tree_(graph_theory). | The first line contains two integers *n* and *m* (2<=≤<=*n*<=≤<=105; 1<=≤<=*m*<=≤<=105). The next line contains a sequence of integers *c*1,<=*c*2,<=...,<=*c**n* (1<=≤<=*c**i*<=≤<=105). The next *n*<=-<=1 lines contain the edges of the tree. The *i*-th line contains the numbers *a**i*,<=*b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *a**i*<=≠<=*b**i*) — the vertices connected by an edge of the tree.
Next *m* lines contain the queries. The *j*-th line contains two integers *v**j*,<=*k**j* (1<=≤<=*v**j*<=≤<=*n*; 1<=≤<=*k**j*<=≤<=105). | Print *m* integers — the answers to the queries in the order the queries appear in the input. | [
"8 5\n1 2 2 3 3 2 3 3\n1 2\n1 5\n2 3\n2 4\n5 6\n5 7\n5 8\n1 2\n1 3\n1 4\n2 3\n5 3\n",
"4 1\n1 2 3 4\n1 2\n2 3\n3 4\n1 1\n"
] | [
"2\n2\n1\n0\n1\n",
"4\n"
] | A subtree of vertex *v* in a rooted tree with root *r* is a set of vertices {*u* : *dist*(*r*, *v*) + *dist*(*v*, *u*) = *dist*(*r*, *u*)}. Where *dist*(*x*, *y*) is the length (in edges) of the shortest path between vertices *x* and *y*. | [
{
"input": "8 5\n1 2 2 3 3 2 3 3\n1 2\n1 5\n2 3\n2 4\n5 6\n5 7\n5 8\n1 2\n1 3\n1 4\n2 3\n5 3",
"output": "2\n2\n1\n0\n1"
},
{
"input": "4 1\n1 2 3 4\n1 2\n2 3\n3 4\n1 1",
"output": "4"
}
] | 1,000 | 102,400 | 0 | 21,223 |
|
838 | Binary Blocks | [
"brute force"
] | null | null | You are given an image, that can be represented with a 2-d *n* by *m* grid of pixels. Each pixel of the image is either on or off, denoted by the characters "0" or "1", respectively. You would like to compress this image. You want to choose an integer *k*<=><=1 and split the image into *k* by *k* blocks. If *n* and *m* are not divisible by *k*, the image is padded with only zeros on the right and bottom so that they are divisible by *k*. Each pixel in each individual block must have the same value. The given image may not be compressible in its current state. Find the minimum number of pixels you need to toggle (after padding) in order for the image to be compressible for some *k*. More specifically, the steps are to first choose *k*, then the image is padded with zeros, then, we can toggle the pixels so it is compressible for this *k*. The image must be compressible in that state. | The first line of input will contain two integers *n*,<=*m* (2<=≤<=*n*,<=*m*<=≤<=2<=500), the dimensions of the image.
The next *n* lines of input will contain a binary string with exactly *m* characters, representing the image. | Print a single integer, the minimum number of pixels needed to toggle to make the image compressible. | [
"3 5\n00100\n10110\n11001\n"
] | [
"5\n"
] | We first choose *k* = 2.
The image is padded as follows:
We can toggle the image to look as follows:
We can see that this image is compressible for *k* = 2. | [
{
"input": "3 5\n00100\n10110\n11001",
"output": "5"
}
] | 2,000 | 29,491,200 | 0 | 21,235 |
|
0 | none | [
"none"
] | null | null | Leha plays a computer game, where is on each level is given a connected graph with *n* vertices and *m* edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer *d**i*, which can be equal to 0, 1 or <=-<=1. To pass the level, he needs to find a «good» subset of edges of the graph or say, that it doesn't exist. Subset is called «good», if by by leaving only edges from this subset in the original graph, we obtain the following: for every vertex i, *d**i*<==<=<=-<=1 or it's degree modulo 2 is equal to *d**i*. Leha wants to pass the game as soon as possible and ask you to help him. In case of multiple correct answers, print any of them. | The first line contains two integers *n*, *m* (1<=≤<=*n*<=≤<=3·105, *n*<=-<=1<=≤<=*m*<=≤<=3·105) — number of vertices and edges.
The second line contains *n* integers *d*1,<=*d*2,<=...,<=*d**n* (<=-<=1<=≤<=*d**i*<=≤<=1) — numbers on the vertices.
Each of the next *m* lines contains two integers *u* and *v* (1<=≤<=*u*,<=*v*<=≤<=*n*) — edges. It's guaranteed, that graph in the input is connected. | Print <=-<=1 in a single line, if solution doesn't exist. Otherwise in the first line *k* — number of edges in a subset. In the next *k* lines indexes of edges. Edges are numerated in order as they are given in the input, starting from 1. | [
"1 0\n1\n",
"4 5\n0 0 0 -1\n1 2\n2 3\n3 4\n1 4\n2 4\n",
"2 1\n1 1\n1 2\n",
"3 3\n0 -1 1\n1 2\n2 3\n1 3\n"
] | [
"-1\n",
"0\n",
"1\n1\n",
"1\n2\n"
] | In the first sample we have single vertex without edges. It's degree is 0 and we can not get 1. | [
{
"input": "1 0\n1",
"output": "-1"
},
{
"input": "4 5\n0 0 0 -1\n1 2\n2 3\n3 4\n1 4\n2 4",
"output": "0"
},
{
"input": "2 1\n1 1\n1 2",
"output": "1\n1"
},
{
"input": "3 3\n0 -1 1\n1 2\n2 3\n1 3",
"output": "1\n2"
},
{
"input": "10 10\n-1 -1 -1 -1 -1 -1 -1 -1 -1 -1\n6 7\n8 3\n6 4\n4 2\n9 2\n5 10\n9 8\n10 7\n5 1\n6 2",
"output": "0"
},
{
"input": "3 2\n1 0 1\n1 2\n2 3",
"output": "2\n2\n1"
}
] | 280 | 15,872,000 | 0 | 21,283 |
|
632 | Alice, Bob, Two Teams | [
"brute force",
"constructive algorithms"
] | null | null | Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are *n* pieces, and the *i*-th piece has a strength *p**i*.
The way to split up game pieces is split into several steps:
1. First, Alice will split the pieces into two different groups *A* and *B*. This can be seen as writing the assignment of teams of a piece in an *n* character string, where each character is *A* or *B*. 1. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change *A* to *B* and *B* to *A*). He can do this step at most once. 1. Alice will get all the pieces marked *A* and Bob will get all the pieces marked *B*.
The strength of a player is then the sum of strengths of the pieces in the group.
Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. | The first line contains integer *n* (1<=≤<=*n*<=≤<=5·105) — the number of game pieces.
The second line contains *n* integers *p**i* (1<=≤<=*p**i*<=≤<=109) — the strength of the *i*-th piece.
The third line contains *n* characters *A* or *B* — the assignment of teams after the first step (after Alice's step). | Print the only integer *a* — the maximum strength Bob can achieve. | [
"5\n1 2 3 4 5\nABABA\n",
"5\n1 2 3 4 5\nAAAAA\n",
"1\n1\nB\n"
] | [
"11\n",
"15\n",
"1\n"
] | In the first sample Bob should flip the suffix of length one.
In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5.
In the third sample Bob should do nothing. | [
{
"input": "5\n1 2 3 4 5\nABABA",
"output": "11"
},
{
"input": "5\n1 2 3 4 5\nAAAAA",
"output": "15"
},
{
"input": "1\n1\nB",
"output": "1"
},
{
"input": "10\n1 9 7 6 2 4 7 8 1 3\nABBABAABBB",
"output": "33"
},
{
"input": "100\n591 417 888 251 792 847 685 3 182 461 102 348 555 956 771 901 712 878 580 631 342 333 285 899 525 725 537 718 929 653 84 788 104 355 624 803 253 853 201 995 536 184 65 205 540 652 549 777 248 405 677 950 431 580 600 846 328 429 134 983 526 103 500 963 400 23 276 704 570 757 410 658 507 620 984 244 486 454 802 411 985 303 635 283 96 597 855 775 139 839 839 61 219 986 776 72 729 69 20 917\nBBBAAABBBABAAABBBBAAABABBBBAAABAAABBABABAAABABABBABBABABAAAABAABABBBBBBBABBAAAABAABABABAABABABAABBAB",
"output": "30928"
},
{
"input": "3\n1 1 1\nBAA",
"output": "3"
},
{
"input": "3\n2 1 2\nBAB",
"output": "4"
},
{
"input": "2\n1 1\nBB",
"output": "2"
},
{
"input": "1\n1\nA",
"output": "1"
},
{
"input": "2\n1 1\nAB",
"output": "2"
}
] | 1,356 | 43,315,200 | 3 | 21,305 |
|
39 | C*++ Calculations | [
"expression parsing",
"greedy"
] | A. C*++ Calculations | 2 | 64 | C*++ language is quite similar to C++. The similarity manifests itself in the fact that the programs written in C*++ sometimes behave unpredictably and lead to absolutely unexpected effects. For example, let's imagine an arithmetic expression in C*++ that looks like this (*expression* is the main term):
- *expression* ::= *summand* | *expression*<=+<=*summand* | *expression*<=-<=*summand* - *summand* ::= *increment* | *coefficient***increment* - *increment* ::= a++ | ++a - *coefficient* ::= 0|1|2|...|1000
For example, "5*a++-3*++a+a++" is a valid expression in C*++.
Thus, we have a sum consisting of several summands divided by signs "+" or "-". Every summand is an expression "a++" or "++a" multiplied by some integer coefficient. If the coefficient is omitted, it is suggested being equal to 1.
The calculation of such sum in C*++ goes the following way. First all the summands are calculated one after another, then they are summed by the usual arithmetic rules. If the summand contains "a++", then during the calculation first the value of the "a" variable is multiplied by the coefficient, then value of "a" is increased by 1. If the summand contains "++a", then the actions on it are performed in the reverse order: first "a" is increased by 1, then — multiplied by the coefficient.
The summands may be calculated in any order, that's why sometimes the result of the calculation is completely unpredictable! Your task is to find its largest possible value. | The first input line contains an integer *a* (<=-<=1000<=≤<=*a*<=≤<=1000) — the initial value of the variable "a". The next line contains an expression in C*++ language of the described type. The number of the summands in the expression does not exceed 1000. It is guaranteed that the line describing the expression contains no spaces and tabulation. | Output a single number — the maximal possible value of the expression. | [
"1\n5*a++-3*++a+a++\n",
"3\na+++++a\n"
] | [
"11\n",
"8\n"
] | Consider the second example. Initially *a* = 3. Suppose that at first the first summand is calculated, and then the second one is. The first summand gets equal to 3, and the value of *a* is increased by 1. At the calculation of the second summand *a* is increased once more (gets equal to 5). The value of the second summand is 5, and together they give 8. If we calculate the second summand first and the first summand later, then the both summands equals to 4, and the result is 8, too. | [
{
"input": "1\n5*a++-3*++a+a++",
"output": "11"
},
{
"input": "3\na+++++a",
"output": "8"
},
{
"input": "-668\n820*a+++402*++a-482*++a",
"output": "-492358"
},
{
"input": "902\n600*++a+411*a+++20*a++-340*++a-306*++a+485*a++-776*a+++417*a+++70*a++-703*a++",
"output": "-97296"
},
{
"input": "-215\n840*++a+183*++a-975*++a+301*a+++874*a++",
"output": "-256096"
},
{
"input": "-211\n849*a++-419*a+++720*++a-543*a+++193*a++-506*++a",
"output": "-55460"
},
{
"input": "-206\n859*a++-655*a+++466*++a-786*++a+512*a+++628*a+++747*a++",
"output": "-351932"
},
{
"input": "-441\n214*++a+30*++a-390*++a-112*++a-409*++a+287*a++-660*++a-740*++a-695*a++-830*++a+554*a++",
"output": "1211971"
},
{
"input": "875\n132*a+++960*++a+510*a++-37*++a-923*++a-892*a+++427*a+++384*a++-253*a++-82*a+++506*a+++815*a+++499*++a",
"output": "1829041"
},
{
"input": "399\n469*++a-935*++a-838*++a-468*++a+79*++a-89*++a-863*++a+531*a++-523*a++-583*++a-411*++a+301*++a+201*a++-108*a+++581*a+++938*++a-16*a++-632*a+++146*a++-230*++a+151*++a-618*a+++593*a+++320*++a+750*++a+185*a++-68*++a+839*a++-853*a+++761*a++-442*a++-385*a++-487*a++-573*a++-820*a++-123*a+++792*++a+95*++a+228*++a-945*a+++126*++a-888*++a-745*++a-217*a++-883*++a-632*++a+82*a++-371*++a-14*++a+528*a++",
"output": "-2184221"
},
{
"input": "-677\n490*++a",
"output": "-331240"
},
{
"input": "-744\n672*a++-975*a++-394*a+++968*a+++222*a+++988*++a+504*++a-782*a++-321*++a+980*++a-483*a++-554*++a-347*++a-180*++a-390*a+++403*a++-617*a+++378*a+++544*++a-978*a+++952*a++-618*a++-516*++a-990*++a+540*++a-398*++a-187*++a+401*++a+829*a++-187*++a-185*a++-529*a++",
"output": "1091591"
},
{
"input": "-496\n589*a+++507*++a+59*++a-507*a+++951*++a+99*++a-651*++a-985*a++-61*a+++588*a++-412*a++-756*a+++978*a+++58*++a-230*++a-391*++a-574*a++",
"output": "408226"
},
{
"input": "217\n828*a+++340*++a-450*a++-575*++a-821*++a+89*a++-543*++a-61*++a+629*++a-956*++a-685*++a-424*a++",
"output": "-565304"
},
{
"input": "189\n360*++a+889*a++-940*a++-272*a+++437*++a-495*++a+194*++a-339*++a-503*++a+335*++a-459*a++-285*a++-738*++a-554*a++-68*++a",
"output": "-447974"
},
{
"input": "-589\n561*a++-754*++a-577*a+++393*++a-636*++a-481*++a+645*a++-931*++a+198*a++-788*a++-925*a++-580*a++-506*++a-722*a++-33*++a+743*a++-506*++a+243*a++-416*a++-438*a+++23*++a-745*++a-865*a++",
"output": "4173855"
},
{
"input": "-693\n372*++a-195*a++-542*a+++15*a++-560*a+++268*a+++266*a+++476*a++-267*a+++608*a+++766*++a-861*a++-649*a++-973*++a-840*++a+586*++a-346*++a-940*a++-177*a++-925*a++-608*++a+816*++a+150*a+++494*++a",
"output": "2186029"
},
{
"input": "7\na+++a++-a++-a+++5*a++-2*a++",
"output": "50"
},
{
"input": "1000\na++",
"output": "1000"
},
{
"input": "1000\n++a",
"output": "1001"
},
{
"input": "332\n++a",
"output": "333"
},
{
"input": "4\n0*a++",
"output": "0"
},
{
"input": "584\n7*++a",
"output": "4095"
}
] | 0 | 0 | -1 | 21,456 |
743 | Chloe and pleasant prizes | [
"dfs and similar",
"dp",
"graphs",
"trees"
] | null | null | Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.
They took *n* prizes for the contestants and wrote on each of them a unique id (integer from 1 to *n*). A gift *i* is characterized by integer *a**i* — pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with *n* vertices.
The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.
Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.
Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible. | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=2·105) — the number of gifts.
The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=≤<=*a**i*<=≤<=109) — the pleasantness of the gifts.
The next (*n*<=-<=1) lines contain two numbers each. The *i*-th of these lines contains integers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*, *u**i*<=≠<=*v**i*) — the description of the tree's edges. It means that gifts with numbers *u**i* and *v**i* are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: *v**i* hangs on *u**i* or *u**i* hangs on *v**i*.
It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts. | If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer — the maximum possible sum of pleasantness they can get together.
Otherwise print Impossible. | [
"8\n0 5 -1 4 3 2 6 5\n1 2\n2 4\n2 5\n1 3\n3 6\n6 7\n6 8\n",
"4\n1 -5 1 1\n1 2\n1 4\n2 3\n",
"1\n-1\n"
] | [
"25",
"2",
"Impossible"
] | none | [
{
"input": "8\n0 5 -1 4 3 2 6 5\n1 2\n2 4\n2 5\n1 3\n3 6\n6 7\n6 8",
"output": "25"
},
{
"input": "4\n1 -5 1 1\n1 2\n1 4\n2 3",
"output": "2"
},
{
"input": "1\n-1",
"output": "Impossible"
},
{
"input": "10\n3 2 -4 2 0 3 3 3 3 4\n10 8\n4 2\n4 9\n3 5\n5 2\n7 4\n2 6\n1 8\n10 9",
"output": "6"
},
{
"input": "10\n-1 2 -2 -3 -1 -1 0 -4 -5 -4\n4 6\n6 9\n1 2\n6 2\n7 8\n7 9\n5 10\n6 3\n10 1",
"output": "-3"
},
{
"input": "10\n4 -4 2 5 -1 3 -1 1 4 5\n1 8\n7 1\n4 1\n9 6\n1 2\n5 10\n10 1\n9 3\n1 9",
"output": "14"
},
{
"input": "10\n29 -2 39 1 98 98 82 56 5 -2\n3 1\n7 9\n8 9\n7 3\n4 2\n5 10\n6 8\n10 6\n5 4",
"output": "Impossible"
},
{
"input": "10\n-3 0 1 -3 2 1 2 5 3 1\n6 2\n10 3\n10 5\n2 7\n3 4\n8 2\n8 10\n4 9\n1 9",
"output": "10"
},
{
"input": "10\n-5 0 1 -2 2 1 2 1 -1 -3\n10 4\n10 5\n4 1\n3 5\n2 8\n6 7\n9 7\n8 7\n6 3",
"output": "0"
},
{
"input": "10\n2 -4 5 4 -1 -5 -1 -5 3 -1\n8 6\n8 7\n10 2\n6 3\n5 2\n3 9\n10 1\n5 4\n9 4",
"output": "Impossible"
},
{
"input": "10\n-1 2 -5 -5 3 1 -4 0 3 -5\n2 10\n2 6\n8 4\n9 2\n10 7\n1 7\n9 5\n8 3\n9 4",
"output": "4"
},
{
"input": "10\n1 -4 -4 0 1 -3 1 -2 -4 2\n7 1\n7 6\n5 6\n4 2\n2 6\n3 9\n5 9\n10 6\n8 5",
"output": "2"
},
{
"input": "10\n-4 -1 -5 -1 -3 -1 -2 -2 -5 -3\n7 9\n3 6\n1 4\n7 2\n3 10\n8 9\n4 10\n3 5\n6 8",
"output": "-4"
},
{
"input": "10\n-21 -17 -16 -45 -93 -77 -73 -12 -81 -33\n9 5\n8 6\n1 10\n9 4\n3 2\n10 4\n3 8\n7 2\n5 6",
"output": "Impossible"
},
{
"input": "10\n-5 -2 -4 -1 -4 -5 -1 -4 -1 -3\n8 6\n7 2\n1 2\n10 4\n9 3\n6 10\n7 9\n5 4\n5 3",
"output": "Impossible"
},
{
"input": "10\n-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000\n7 8\n5 10\n8 6\n1 5\n7 9\n3 9\n2 10\n2 6\n4 3",
"output": "Impossible"
},
{
"input": "10\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n7 8\n8 3\n3 10\n2 10\n2 4\n9 6\n7 5\n6 5\n9 1",
"output": "Impossible"
},
{
"input": "3\n1 -1000000000 -1000000000\n1 2\n1 3",
"output": "-2000000000"
}
] | 31 | 0 | 0 | 21,461 |
|
49 | Game | [
"brute force",
"dp",
"implementation"
] | D. Game | 2 | 256 | Vasya and Petya have invented a new game. Vasya takes a stripe consisting of 1<=×<=*n* square and paints the squares black and white. After that Petya can start moves — during a move he may choose any two neighboring squares of one color and repaint these two squares any way he wants, perhaps in different colors. Petya can only repaint the squares in white and black colors. Petya’s aim is to repaint the stripe so that no two neighboring squares were of one color. Help Petya, using the given initial coloring, find the minimum number of moves Petya needs to win. | The first line contains number *n* (1<=≤<=*n*<=≤<=1000) which represents the stripe’s length. The second line contains exactly *n* symbols — the line’s initial coloring. 0 corresponds to a white square, 1 corresponds to a black one. | If Petya cannot win with such an initial coloring, print -1. Otherwise print the minimum number of moves Petya needs to win. | [
"6\n111010\n",
"5\n10001\n",
"7\n1100010\n",
"5\n00100\n"
] | [
"1\n",
"1\n",
"2\n",
"2\n"
] | In the first sample Petya can take squares 1 and 2. He repaints square 1 to black and square 2 to white.
In the second sample Petya can take squares 2 and 3. He repaints square 2 to white and square 3 to black. | [
{
"input": "6\n111010",
"output": "1"
},
{
"input": "5\n10001",
"output": "1"
},
{
"input": "7\n1100010",
"output": "2"
},
{
"input": "5\n00100",
"output": "2"
},
{
"input": "3\n101",
"output": "0"
},
{
"input": "6\n111111",
"output": "3"
},
{
"input": "6\n000000",
"output": "3"
},
{
"input": "10\n1000101001",
"output": "3"
},
{
"input": "100\n1001001000011010001101000011100101101110101001110110010001110011011100111000010010011011101000011101",
"output": "49"
},
{
"input": "100\n0000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"output": "49"
},
{
"input": "100\n1111111111111111111111111111111111111111111111111111111111111111111111111111101111111111111111111111",
"output": "49"
},
{
"input": "1\n0",
"output": "0"
},
{
"input": "2\n11",
"output": "1"
},
{
"input": "3\n111",
"output": "1"
},
{
"input": "3\n010",
"output": "0"
},
{
"input": "70\n0010011001010100000110011001011111101011010110110101110101111011101010",
"output": "32"
},
{
"input": "149\n11110101110111101111110110001111110101111011111111111111101111110000101101110110111101011111011111111000111011011110111111001011111111111010110111110",
"output": "73"
},
{
"input": "35\n11111111111111111111111111111111110",
"output": "17"
},
{
"input": "10\n1000000000",
"output": "4"
},
{
"input": "337\n0000000000000000000000000000000000000010000000000000000000000000000000010000000000001000000000100100000000000000000000000000000000000000000000000000000000100000000000000010000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000001000100000000000000000000000000000000000100010000000100",
"output": "160"
},
{
"input": "7\n0000000",
"output": "3"
},
{
"input": "7\n0100011",
"output": "2"
},
{
"input": "4\n0000",
"output": "2"
},
{
"input": "18\n110100000000000000",
"output": "8"
},
{
"input": "17\n00100000000000000",
"output": "8"
},
{
"input": "16\n1101010010000000",
"output": "7"
},
{
"input": "15\n110101101111111",
"output": "7"
},
{
"input": "14\n00101111111111",
"output": "6"
},
{
"input": "8\n10011001",
"output": "4"
},
{
"input": "4\n1001",
"output": "2"
},
{
"input": "8\n00000110",
"output": "4"
},
{
"input": "8\n10011001",
"output": "4"
},
{
"input": "12\n101101010010",
"output": "6"
},
{
"input": "1\n0",
"output": "0"
},
{
"input": "6\n110011",
"output": "3"
},
{
"input": "23\n10010101010101010101001",
"output": "4"
},
{
"input": "14\n10101001101010",
"output": "2"
},
{
"input": "4\n0110",
"output": "2"
},
{
"input": "4\n1100",
"output": "2"
}
] | 124 | 4,608,000 | 3.960417 | 21,619 |
592 | BCPC | [
"binary search",
"geometry",
"two pointers"
] | null | null | BCPC stands for Byteforces Collegiate Programming Contest, and is the most famous competition in Byteforces.
BCPC is a team competition. Each team is composed by a coach and three contestants. Blenda is the coach of the Bit State University(BSU), and she is very strict selecting the members of her team.
In BSU there are *n* students numbered from 1 to *n*. Since all BSU students are infinitely smart, the only important parameters for Blenda are their reading and writing speed. After a careful measuring, Blenda have found that the *i*-th student have a reading speed equal to *r**i* (words per minute), and a writing speed of *w**i* (symbols per minute). Since BSU students are very smart, the measured speeds are sometimes very big and Blenda have decided to subtract some constant value *c* from all the values of reading speed and some value *d* from all the values of writing speed. Therefore she considers *r**i*'<==<=*r**i*<=-<=*c* and *w**i*'<==<=*w**i*<=-<=*d*.
The student *i* is said to overwhelm the student *j* if and only if *r**i*'·*w**j*'<=><=*r**j*'·*w**i*'. Blenda doesn’t like fights in teams, so she thinks that a team consisting of three distinct students *i*,<=*j* and *k* is good if *i* overwhelms *j*, *j* overwhelms *k*, and *k* overwhelms *i*. Yes, the relation of overwhelming is not transitive as it often happens in real life.
Since Blenda is busy preparing a training camp in Codeforces, you are given a task to calculate the number of different good teams in BSU. Two teams are considered to be different if there is at least one student that is present in one team but is not present in the other. In other words, two teams are different if the sets of students that form these teams are different. | In the first line of the input three integers *n*, *c* and *d* (3<=≤<=*n*<=≤<=345678,<=1<=≤<=*c*,<=*d*<=≤<=109) are written. They denote the number of students Blenda can use to form teams, the value subtracted from all reading speeds and the value subtracted from all writing speeds respectively.
Each of the next *n* lines contains two integers *r**i* and *w**i* (0<=<<=*r**i*,<=*w**i*<=≤<=109,<=|*r**i*<=-<=*c*|<=+<=|*w**i*<=-<=*d*|<=><=0). There are no two students, such that both their reading and writing speeds coincide, i.e. for every *i*<=≠<=*j* condition |*r**i*<=-<=*r**j*|<=+<=|*w**i*<=-<=*w**j*|<=><=0 holds. | Print the number of different teams in BSU, that are good according to Blenda's definition. | [
"5 2 2\n1 1\n4 1\n2 3\n3 2\n3 4\n",
"7 6 6\n3 2\n1 7\n5 7\n3 7\n6 4\n8 9\n8 5\n"
] | [
"4\n",
"11\n"
] | In the first sample the following teams are good: (*i* = 1, *j* = 2, *k* = 3), (*i* = 2, *j* = 5, *k* = 1), (*i* = 1, *j* = 4, *k* = 3), (*i* = 5, *j* = 1, *k* = 4).
Note, that for example the team (*i* = 3, *j* = 1, *k* = 2) is also good, but is considered to be the same as the team (*i* = 1, *j* = 2, *k* = 3). | [] | 30 | 204,800 | -1 | 21,646 |
|
0 | none | [
"none"
] | null | null | All Berland residents are waiting for an unprecedented tour of wizard in his Blue Helicopter over the cities of Berland!
It is well-known that there are *n* cities in Berland, some pairs of which are connected by bidirectional roads. Each pair of cities is connected by no more than one road. It is not guaranteed that the road network is connected, i.e. it is possible that you can't reach some city from some other.
The tour will contain several episodes. In each of the episodes:
- the wizard will disembark at some city *x* from the Helicopter; - he will give a performance and show a movie for free at the city *x*; - he will drive to some neighboring city *y* using a road; - he will give a performance and show a movie for free at the city *y*; - he will drive to some neighboring to *y* city *z*; - he will give a performance and show a movie for free at the city *z*; - he will embark the Helicopter and fly away from the city *z*.
It is known that the wizard doesn't like to use roads, so he agrees to use each road at most once (regardless of direction). In other words, for road between *a* and *b* he only can drive once from *a* to *b*, or drive once from *b* to *a*, or do not use this road at all.
The wizards wants to plan as many episodes as possible without violation the above rules. Help the wizard!
Please note that the wizard can visit the same city multiple times, the restriction is on roads only. | The first line contains two integers *n*, *m* (1<=≤<=*n*<=≤<=2·105, 0<=≤<=*m*<=≤<=2·105) — the number of cities and the number of roads in Berland, respectively.
The roads description follow, one in each line. Each description is a pair of two integers *a**i*,<=*b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*, *a**i*<=≠<=*b**i*), where *a**i* and *b**i* are the ids of the cities connected by the *i*-th road. It is guaranteed that there are no two roads connecting the same pair of cities. Every road is bidirectional. The cities are numbered from 1 to *n*.
It is possible that the road network in Berland is not connected. | In the first line print *w* — the maximum possible number of episodes. The next *w* lines should contain the episodes in format *x*, *y*, *z* — the three integers denoting the ids of the cities in the order of the wizard's visits. | [
"4 5\n1 2\n3 2\n2 4\n3 4\n4 1\n",
"5 8\n5 3\n1 2\n4 5\n5 1\n2 5\n4 3\n1 4\n3 2\n"
] | [
"2\n1 4 2\n4 3 2\n",
"4\n1 4 5\n2 3 4\n1 5 3\n5 2 1\n"
] | none | [
{
"input": "4 5\n1 2\n3 2\n2 4\n3 4\n4 1",
"output": "2\n1 4 2\n4 3 2"
},
{
"input": "5 8\n5 3\n1 2\n4 5\n5 1\n2 5\n4 3\n1 4\n3 2",
"output": "4\n1 4 5\n2 3 4\n1 5 3\n5 2 1"
},
{
"input": "1 0",
"output": "0"
},
{
"input": "2 1\n1 2",
"output": "0"
},
{
"input": "3 2\n1 2\n3 2",
"output": "1\n3 2 1"
},
{
"input": "10 9\n9 4\n8 5\n3 5\n9 7\n10 7\n1 9\n5 2\n6 4\n2 7",
"output": "4\n6 4 9\n3 5 8\n5 2 7\n10 7 9"
},
{
"input": "7 3\n5 3\n2 5\n6 4",
"output": "1\n3 5 2"
},
{
"input": "9 7\n7 1\n5 4\n8 6\n6 4\n7 4\n7 6\n3 9",
"output": "3\n7 6 8\n5 4 6\n4 7 1"
},
{
"input": "5 5\n4 2\n2 1\n5 2\n3 1\n4 5",
"output": "2\n2 5 4\n4 2 1"
},
{
"input": "8 22\n6 1\n3 8\n8 5\n3 7\n1 3\n6 8\n3 5\n6 2\n2 8\n4 1\n1 5\n4 3\n8 1\n7 8\n2 7\n1 2\n2 5\n2 4\n5 4\n6 7\n4 7\n3 6",
"output": "11\n1 4 2\n3 4 7\n1 5 3\n4 5 8\n1 2 5\n6 2 8\n2 7 6\n8 7 3\n1 3 6\n1 8 3\n8 6 1"
},
{
"input": "19 22\n10 8\n1 14\n12 15\n1 16\n3 6\n6 2\n17 6\n2 10\n2 18\n8 2\n13 10\n8 7\n7 11\n9 15\n19 12\n18 14\n13 5\n12 18\n4 14\n2 19\n15 17\n10 18",
"output": "11\n2 19 12\n18 12 15\n9 15 17\n3 6 17\n11 7 8\n2 8 10\n5 13 10\n18 10 2\n6 2 18\n4 14 18\n14 1 16"
},
{
"input": "5 10\n3 5\n5 2\n1 3\n4 1\n2 3\n4 5\n2 1\n4 2\n1 5\n3 4",
"output": "5\n1 4 3\n5 4 2\n1 2 3\n1 5 2\n5 3 1"
},
{
"input": "7 8\n5 2\n4 5\n4 3\n4 1\n5 6\n6 4\n7 3\n5 1",
"output": "4\n4 6 5\n1 5 2\n7 3 4\n5 4 1"
},
{
"input": "5 10\n3 5\n5 1\n3 2\n2 4\n5 4\n4 3\n2 1\n3 1\n5 2\n4 1",
"output": "5\n1 4 3\n5 4 2\n1 2 5\n1 3 2\n3 5 1"
},
{
"input": "9 7\n5 9\n4 2\n6 3\n1 3\n1 7\n1 2\n7 8",
"output": "3\n6 3 1\n8 7 1\n4 2 1"
},
{
"input": "10 6\n9 2\n3 8\n10 8\n5 2\n1 9\n9 3",
"output": "3\n5 2 9\n10 8 3\n3 9 1"
},
{
"input": "9 10\n6 7\n4 3\n3 5\n9 4\n9 1\n5 9\n9 8\n6 2\n4 8\n9 2",
"output": "5\n9 5 3\n9 8 4\n3 4 9\n7 6 2\n2 9 1"
},
{
"input": "59 58\n53 2\n48 47\n59 2\n24 12\n53 55\n54 52\n44 57\n36 51\n10 26\n39 4\n19 22\n58 53\n3 11\n28 31\n16 26\n10 8\n34 25\n55 41\n46 21\n23 13\n9 39\n10 48\n15 37\n55 31\n14 40\n23 50\n25 45\n42 32\n49 8\n1 38\n55 50\n37 41\n38 49\n51 57\n46 52\n3 6\n29 40\n7 57\n11 12\n18 20\n26 52\n48 5\n5 45\n30 20\n35 58\n55 34\n31 18\n28 42\n11 2\n33 51\n32 43\n8 27\n36 17\n7 22\n57 41\n10 56\n9 47\n29 23",
"output": "29\n21 46 52\n54 52 26\n16 26 10\n4 39 9\n9 47 48\n6 3 11\n24 12 11\n11 2 59\n35 58 53\n2 53 55\n15 37 41\n17 36 51\n33 51 57\n19 22 7\n7 57 44\n57 41 55\n43 32 42\n42 28 31\n30 20 18\n18 31 55\n14 40 29\n13 23 29\n23 50 55\n55 34 25\n25 45 5\n5 48 10\n56 10 8\n27 8 49\n49 38 1"
},
{
"input": "73 72\n52 51\n12 47\n19 42\n44 22\n31 51\n61 29\n56 15\n58 42\n8 4\n6 62\n69 66\n3 18\n69 3\n7 47\n15 33\n1 20\n59 58\n58 61\n70 64\n21 14\n6 70\n32 71\n52 32\n23 34\n67 7\n26 13\n33 45\n22 21\n13 63\n53 26\n40 36\n6 27\n53 32\n30 49\n59 53\n49 54\n47 46\n28 69\n18 41\n36 49\n3 42\n16 49\n23 5\n47 54\n23 11\n24 36\n2 12\n44 48\n22 43\n45 68\n2 17\n68 30\n70 39\n37 62\n35 2\n71 35\n4 32\n49 5\n31 70\n17 48\n15 65\n9 57\n20 67\n61 73\n6 57\n10 58\n11 72\n44 50\n25 19\n44 55\n60 56\n38 24",
"output": "36\n14 21 22\n43 22 44\n50 44 55\n44 48 17\n37 62 6\n9 57 6\n27 6 70\n39 70 64\n70 31 51\n51 52 32\n63 13 26\n25 19 42\n41 18 3\n28 69 66\n69 3 42\n29 61 73\n10 58 42\n61 58 59\n26 53 59\n8 4 32\n53 32 71\n71 35 2\n17 2 12\n60 56 15\n65 15 33\n33 45 68\n68 30 49\n38 24 36\n40 36 49\n72 11 23\n34 23 5\n5 49 16\n49 54 47\n12 47 46\n47 7 67\n67 20 1"
}
] | 1,684 | 133,632,000 | -1 | 21,706 |
|
370 | Summer Reading | [
"dp",
"greedy"
] | null | null | At school Vasya got an impressive list of summer reading books. Unlike other modern schoolchildren, Vasya loves reading, so he read some book each day of the summer.
As Vasya was reading books, he was making notes in the Reader's Diary. Each day he wrote the orderal number of the book he was reading. The books in the list are numbered starting from 1 and Vasya was reading them in the order they go in the list. Vasya never reads a new book until he finishes reading the previous one. Unfortunately, Vasya wasn't accurate and some days he forgot to note the number of the book and the notes for those days remained empty.
As Vasya knows that the literature teacher will want to check the Reader's Diary, so he needs to restore the lost records. Help him do it and fill all the blanks. Vasya is sure that he spends at least two and at most five days for each book. Vasya finished reading all the books he had started. Assume that the reading list contained many books. So many, in fact, that it is impossible to read all of them in a summer. If there are multiple valid ways to restore the diary records, Vasya prefers the one that shows the maximum number of read books. | The first line contains integer *n* — the number of summer days (2<=≤<=*n*<=≤<=2·105). The second line contains *n* integers *a*1,<=*a*2,<=... *a**n* — the records in the diary in the order they were written (0<=≤<=*a**i*<=≤<=105). If Vasya forgot to write the number of the book on the *i*-th day, then *a**i* equals 0. | If it is impossible to correctly fill the blanks in the diary (the diary may contain mistakes initially), print "-1".
Otherwise, print in the first line the maximum number of books Vasya could have read in the summer if we stick to the diary. In the second line print *n* integers — the diary with correctly inserted records. If there are multiple optimal solutions, you can print any of them. | [
"7\n0 1 0 0 0 3 0\n",
"8\n0 0 0 0 0 0 0 0\n",
"4\n0 0 1 0\n",
"4\n0 0 0 3\n"
] | [
"3\n1 1 2 2 3 3 3 \n",
"4\n1 1 2 2 3 3 4 4 \n",
"1\n1 1 1 1 \n",
"-1\n"
] | none | [] | 31 | 0 | 0 | 21,731 |
|
0 | none | [
"none"
] | null | null | Old MacDonald has a farm and a large potato field, (1010<=+<=1)<=×<=(1010<=+<=1) square meters in size. The field is divided into square garden beds, each bed takes up one square meter.
Old McDonald knows that the Colorado potato beetle is about to invade his farm and can destroy the entire harvest. To fight the insects, Old McDonald wants to spray some beds with insecticides.
So Old McDonald went to the field, stood at the center of the central field bed and sprayed this bed with insecticides. Now he's going to make a series of movements and spray a few more beds. During each movement Old McDonald moves left, right, up or down the field some integer number of meters. As Old McDonald moves, he sprays all the beds he steps on. In other words, the beds that have any intersection at all with Old McDonald's trajectory, are sprayed with insecticides.
When Old McDonald finished spraying, he wrote out all his movements on a piece of paper. Now he wants to know how many beds won't be infected after the invasion of the Colorado beetles.
It is known that the invasion of the Colorado beetles goes as follows. First some bed on the field border gets infected. Than any bed that hasn't been infected, hasn't been sprayed with insecticides and has a common side with an infected bed, gets infected as well. Help Old McDonald and determine the number of beds that won't be infected by the Colorado potato beetle. | The first line contains an integer *n* (1<=≤<=*n*<=≤<=1000) — the number of Old McDonald's movements.
Next *n* lines contain the description of Old McDonald's movements. The *i*-th of these lines describes the *i*-th movement. Each movement is given in the format "*d**i* *x**i*", where *d**i* is the character that determines the direction of the movement ("L", "R", "U" or "D" for directions "left", "right", "up" and "down", correspondingly), and *x**i* (1<=≤<=*x**i*<=≤<=106) is an integer that determines the number of meters in the movement. | Print a single integer — the number of beds that won't be infected by the Colorado potato beetle.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"5\nR 8\nU 9\nL 9\nD 8\nL 2\n",
"7\nR 10\nD 2\nL 7\nU 9\nD 2\nR 3\nD 10\n"
] | [
"101",
"52"
] | none | [] | 60 | 0 | 0 | 21,735 |
|
335 | Buy One, Get One Free | [
"dp",
"greedy"
] | null | null | A nearby pie shop is having a special sale. For each pie you pay full price for, you may select one pie of a strictly lesser value to get for free. Given the prices of all the pies you wish to acquire, determine the minimum total amount you must pay for all of the pies. | Input will begin with an integer *n* (1<=≤<=*n*<=≤<=500000), the number of pies you wish to acquire. Following this is a line with *n* integers, each indicating the cost of a pie. All costs are positive integers not exceeding 109. | Print the minimum cost to acquire all the pies.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"6\n3 4 5 3 4 5\n",
"5\n5 5 5 5 5\n",
"4\n309999 6000 2080 2080\n"
] | [
"14\n",
"25\n",
"314159\n"
] | In the first test case you can pay for a pie with cost 5 and get a pie with cost 4 for free, then pay for a pie with cost 5 and get a pie with cost 3 for free, then pay for a pie with cost 4 and get a pie with cost 3 for free.
In the second test case you have to pay full price for every pie. | [
{
"input": "6\n3 4 5 3 4 5",
"output": "14"
},
{
"input": "5\n5 5 5 5 5",
"output": "25"
},
{
"input": "4\n309999 6000 2080 2080",
"output": "314159"
},
{
"input": "10\n1 1 1 1 1 2 3 4 5 6",
"output": "16"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "7\n10 9 5 5 4 3 2",
"output": "22"
},
{
"input": "30\n3 6 3 8 6 4 4 2 4 10 8 5 9 6 7 7 9 4 7 4 10 1 9 10 4 7 7 4 2 3",
"output": "96"
},
{
"input": "40\n2 6 5 5 6 6 7 8 6 5 3 9 9 1 8 3 7 3 7 2 3 1 3 1 5 8 1 3 8 2 3 2 2 1 4 4 4 3 5 5",
"output": "100"
},
{
"input": "50\n4 2 6 7 2 6 3 8 8 2 4 8 4 4 1 4 5 2 2 5 6 4 3 3 3 6 8 2 1 3 1 8 2 3 7 4 7 5 4 8 3 5 7 8 6 7 5 8 7 1",
"output": "130"
},
{
"input": "2\n505072434 3351179",
"output": "505072434"
},
{
"input": "1\n880460566",
"output": "880460566"
}
] | 124 | 307,200 | 0 | 21,822 |
|
507 | Guess Your Way Out! | [
"implementation",
"math",
"trees"
] | null | null | Amr bought a new video game "Guess Your Way Out!". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height *h*. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.
Let's index all the leaf nodes from the left to the right from 1 to 2*h*. The exit is located at some node *n* where 1<=≤<=*n*<=≤<=2*h*, the player doesn't know where the exit is so he has to guess his way out!
Amr follows simple algorithm to choose the path. Let's consider infinite command string "LRLRLRLRL..." (consisting of alternating characters 'L' and 'R'). Amr sequentially executes the characters of the string using following rules:
- Character 'L' means "go to the left child of the current node"; - Character 'R' means "go to the right child of the current node"; - If the destination node is already visited, Amr skips current command, otherwise he moves to the destination node; - If Amr skipped two consecutive commands, he goes back to the parent of the current node before executing next command; - If he reached a leaf node that is not the exit, he returns to the parent of the current node; - If he reaches an exit, the game is finished.
Now Amr wonders, if he follows this algorithm, how many nodes he is going to visit before reaching the exit? | Input consists of two integers *h*,<=*n* (1<=≤<=*h*<=≤<=50, 1<=≤<=*n*<=≤<=2*h*). | Output a single integer representing the number of nodes (excluding the exit node) Amr is going to visit before reaching the exit by following this algorithm. | [
"1 2\n",
"2 3\n",
"3 6\n",
"10 1024\n"
] | [
"2",
"5",
"10",
"2046"
] | A perfect binary tree of height *h* is a binary tree consisting of *h* + 1 levels. Level 0 consists of a single node called root, level *h* consists of 2<sup class="upper-index">*h*</sup> nodes called leaves. Each node that is not a leaf has exactly two children, left and right one.
Following picture illustrates the sample test number 3. Nodes are labeled according to the order of visit.
<img class="tex-graphics" src="https://espresso.codeforces.com/e9d0715dc8cd9b4f6ac7a0fb137563f857660adc.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "1 2",
"output": "2"
},
{
"input": "2 3",
"output": "5"
},
{
"input": "3 6",
"output": "10"
},
{
"input": "10 1024",
"output": "2046"
},
{
"input": "10 577",
"output": "1345"
},
{
"input": "11 550",
"output": "408"
},
{
"input": "19 12783",
"output": "503251"
},
{
"input": "28 72803174",
"output": "50649698"
},
{
"input": "39 457181784666",
"output": "830699159852"
},
{
"input": "12 955",
"output": "2871"
},
{
"input": "13 154",
"output": "7770"
},
{
"input": "14 2334",
"output": "9440"
},
{
"input": "15 15512",
"output": "14926"
},
{
"input": "16 21395",
"output": "2899"
},
{
"input": "17 80239",
"output": "177237"
},
{
"input": "18 153276",
"output": "328766"
},
{
"input": "20 589266",
"output": "1505684"
},
{
"input": "21 1687606",
"output": "3522472"
},
{
"input": "24 14428281",
"output": "26969983"
},
{
"input": "29 113463931",
"output": "347736449"
},
{
"input": "1 1",
"output": "1"
},
{
"input": "3 8",
"output": "14"
},
{
"input": "31 1819651953",
"output": "3412135549"
},
{
"input": "33 2599588275",
"output": "1357401405"
},
{
"input": "38 262402936512",
"output": "519008349260"
},
{
"input": "4 13",
"output": "27"
},
{
"input": "40 615535158153",
"output": "1572205271927"
},
{
"input": "42 1042128038474",
"output": "3195908899134"
},
{
"input": "45 17519319833295",
"output": "17381304930499"
},
{
"input": "46 34999315964173",
"output": "34646522010881"
},
{
"input": "49 295606900104348",
"output": "820858833984106"
},
{
"input": "50 905353992267944",
"output": "1871650493613618"
},
{
"input": "3 5",
"output": "11"
},
{
"input": "4 14",
"output": "26"
},
{
"input": "6 40",
"output": "88"
},
{
"input": "7 31",
"output": "95"
},
{
"input": "8 19",
"output": "205"
},
{
"input": "10 359",
"output": "91"
},
{
"input": "11 349",
"output": "1057"
},
{
"input": "13 4796",
"output": "10298"
},
{
"input": "20 742273",
"output": "1182599"
},
{
"input": "22 3343393",
"output": "7009189"
},
{
"input": "24 3543583",
"output": "10865127"
},
{
"input": "25 678676",
"output": "31527640"
},
{
"input": "27 109473899",
"output": "209022797"
},
{
"input": "29 19827102",
"output": "478963048"
},
{
"input": "8 204",
"output": "422"
},
{
"input": "30 414940886",
"output": "372407442"
},
{
"input": "32 3786259360",
"output": "8003335020"
},
{
"input": "35 31233562499",
"output": "60951693197"
},
{
"input": "38 99361414961",
"output": "28342263489"
},
{
"input": "9 43",
"output": "391"
},
{
"input": "40 874338951117",
"output": "1800799608767"
},
{
"input": "33 2696188969",
"output": "505562011"
},
{
"input": "35 12080044014",
"output": "2415167450"
},
{
"input": "12 2715",
"output": "4185"
},
{
"input": "29 524109003",
"output": "1052258991"
},
{
"input": "50 1",
"output": "1125899906842623"
},
{
"input": "50 1125899906842624",
"output": "2251799813685246"
},
{
"input": "50 562949953421312",
"output": "562949953421312"
},
{
"input": "50 844424930131968",
"output": "1407374883553280"
},
{
"input": "50 375299968947542",
"output": "50"
}
] | 1,000 | 1,228,800 | 0 | 21,837 |
|
922 | Robot Vacuum Cleaner | [
"greedy",
"sortings"
] | null | null | Pushok the dog has been chasing Imp for a few hours already.
Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner.
While moving, the robot generates a string *t* consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string *t* as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (*i*,<=*j*), that *i*<=<<=*j* and and .
The robot is off at the moment. Imp knows that it has a sequence of strings *t**i* in its memory, and he can arbitrary change their order. When the robot is started, it generates the string *t* as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation.
Help Imp to find the maximum noise he can achieve by changing the order of the strings. | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=105) — the number of strings in robot's memory.
Next *n* lines contain the strings *t*1,<=*t*2,<=...,<=*t**n*, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 105. | Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings. | [
"4\nssh\nhs\ns\nhhhs\n",
"2\nh\ns\n"
] | [
"18\n",
"1\n"
] | The optimal concatenation in the first sample is *ssshhshhhs*. | [
{
"input": "4\nssh\nhs\ns\nhhhs",
"output": "18"
},
{
"input": "2\nh\ns",
"output": "1"
},
{
"input": "6\nh\ns\nhhh\nh\nssssss\ns",
"output": "40"
},
{
"input": "1\ns",
"output": "0"
},
{
"input": "10\nsshshss\nhssssssssh\nhhhhhh\nhhhs\nhshhh\nhhhhshsh\nhh\nh\nshs\nsshhshhss",
"output": "613"
},
{
"input": "100\nh\nshh\nh\nhs\nshh\nhh\nh\nssh\nhss\nh\nhh\nh\nh\nh\nh\nh\nh\nh\nh\nh\nhh\nh\nh\nh\nh\nhhh\nh\nh\nhhh\nsh\nhh\nhs\nh\nh\nshs\nh\nh\nh\nshh\nhs\nhs\nh\nh\nh\nhhh\nh\nhhs\nhh\nh\nssh\nh\nh\nhh\nsh\nh\nhss\nh\nhh\nsh\nhh\nhh\nhhh\nhs\nh\nh\nh\nshs\nhs\nshs\nsh\nh\nh\nhs\nh\nh\nh\nh\nsh\nssh\nh\nh\nhh\nhhh\nssh\nh\nssh\nhs\nhh\nh\nhss\nhhh\nh\nhhh\nhss\nhhh\nh\nhhh\nsh\nh\nh",
"output": "5058"
}
] | 46 | 0 | 0 | 21,859 |
|
763 | Timofey and remoduling | [
"brute force",
"implementation",
"math",
"number theory"
] | null | null | Little Timofey likes integers a lot. Unfortunately, he is very young and can't work with very big integers, so he does all the operations modulo his favorite prime *m*. Also, Timofey likes to look for arithmetical progressions everywhere.
One of his birthday presents was a sequence of distinct integers *a*1,<=*a*2,<=...,<=*a**n*. Timofey wants to know whether he can rearrange the elements of the sequence so that is will be an arithmetical progression modulo *m*, or not.
Arithmetical progression modulo *m* of length *n* with first element *x* and difference *d* is sequence of integers *x*,<=*x*<=+<=*d*,<=*x*<=+<=2*d*,<=...,<=*x*<=+<=(*n*<=-<=1)·*d*, each taken modulo *m*. | The first line contains two integers *m* and *n* (2<=≤<=*m*<=≤<=109<=+<=7, 1<=≤<=*n*<=≤<=105, *m* is prime) — Timofey's favorite prime module and the length of the sequence.
The second line contains *n* distinct integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=<<=*m*) — the elements of the sequence. | Print -1 if it is not possible to rearrange the elements of the sequence so that is will be an arithmetical progression modulo *m*.
Otherwise, print two integers — the first element of the obtained progression *x* (0<=≤<=*x*<=<<=*m*) and its difference *d* (0<=≤<=*d*<=<<=*m*).
If there are multiple answers, print any of them. | [
"17 5\n0 2 4 13 15\n",
"17 5\n0 2 4 13 14\n",
"5 3\n1 2 3\n"
] | [
"13 2\n",
"-1\n",
"3 4\n"
] | none | [
{
"input": "17 5\n0 2 4 13 15",
"output": "13 2"
},
{
"input": "17 5\n0 2 4 13 14",
"output": "-1"
},
{
"input": "5 3\n1 2 3",
"output": "3 4"
},
{
"input": "17 8\n7 9 11 13 15 1 3 5",
"output": "1 2"
},
{
"input": "13 2\n0 11",
"output": "0 11"
},
{
"input": "3 1\n0",
"output": "0 1"
},
{
"input": "2 1\n1",
"output": "1 1"
},
{
"input": "241 120\n235 136 237 28 90 60 27 122 239 59 72 46 180 233 182 21 206 153 97 112 94 219 52 109 31 17 8 152 3 118 191 222 214 119 217 39 33 40 1 164 38 173 42 82 130 229 226 73 44 155 85 41 2 132 95 163 192 62 14 210 137 197 135 142 53 154 148 162 140 35 24 106 56 170 93 201 211 147 124 20 177 76 186 228 102 74 129 156 87 51 67 167 178 146 225 111 125 144 45 151 161 22 174 30 188 98 213 199 207 107 209 200 57 65 123 157 6 204 96 66",
"output": "-1"
},
{
"input": "241 120\n114 147 193 143 206 74 60 15 75 197 192 93 115 166 116 88 14 46 156 229 7 5 233 188 111 179 102 73 187 196 57 211 137 134 201 92 29 224 160 2 110 119 184 202 20 174 124 225 97 105 1 216 155 161 19 125 101 205 78 38 178 123 87 16 142 47 28 215 219 23 70 238 120 210 69 10 55 128 25 6 42 169 79 65 56 175 170 107 214 61 43 228 24 237 96 133 34 83 152 220 234 66 52 51 84 183 146 173 33 164 151 11 207 157 165 129 64 138 37 106",
"output": "173 191"
},
{
"input": "17 17\n15 1 6 3 7 8 10 13 12 16 11 9 14 2 0 4 5",
"output": "1 1"
},
{
"input": "7 3\n3 4 1",
"output": "-1"
},
{
"input": "7 3\n4 0 3",
"output": "4 3"
},
{
"input": "17 2\n5 13",
"output": "5 8"
},
{
"input": "17 2\n8 3",
"output": "3 5"
},
{
"input": "17 5\n15 6 9 8 3",
"output": "-1"
},
{
"input": "17 5\n2 5 14 8 11",
"output": "2 3"
},
{
"input": "17 8\n1 10 6 8 2 16 9 15",
"output": "-1"
},
{
"input": "17 8\n6 0 8 3 14 11 9 12",
"output": "8 3"
},
{
"input": "23 1\n4",
"output": "4 1"
},
{
"input": "23 1\n20",
"output": "20 1"
},
{
"input": "23 11\n11 7 15 20 18 5 13 0 8 3 19",
"output": "-1"
},
{
"input": "23 11\n11 4 0 3 2 14 22 12 13 15 1",
"output": "22 12"
},
{
"input": "239 100\n35 96 173 36 220 188 102 227 226 237 16 97 79 211 156 49 146 222 57 209 121 201 166 123 61 29 67 89 184 72 14 47 193 127 165 76 171 30 120 68 108 228 130 151 221 139 214 18 210 169 87 100 13 52 196 148 122 86 131 208 65 194 43 178 9 118 0 24 183 45 134 55 71 212 111 106 170 80 179 233 180 238 235 75 153 105 182 70 197 143 31 207 145 83 20 113 101 190 6 64",
"output": "-1"
},
{
"input": "239 100\n95 225 152 10 96 20 192 51 150 138 105 206 33 41 29 127 53 75 118 9 21 43 119 52 174 139 86 161 207 19 203 215 228 30 66 64 32 44 217 54 131 0 74 117 22 129 109 171 172 141 181 237 107 151 193 140 142 85 55 182 160 42 185 106 11 97 65 163 195 63 128 226 194 184 173 216 84 130 162 98 76 73 120 196 116 40 62 236 218 8 183 238 149 204 205 108 214 227 31 87",
"output": "66 76"
},
{
"input": "239 100\n178 74 144 43 201 189 40 175 51 31 202 114 12 17 86 78 53 196 235 158 95 224 143 198 170 117 79 81 23 197 73 165 133 166 21 50 148 34 121 223 184 45 54 228 9 238 187 19 218 169 104 62 106 46 209 182 221 61 59 48 160 27 212 123 5 107 82 102 208 151 7 180 35 191 11 70 168 18 145 89 193 87 91 215 42 139 0 29 140 69 194 41 153 231 154 111 157 122 119 127",
"output": "-1"
},
{
"input": "239 100\n9 107 11 203 36 34 35 91 20 3 42 19 225 169 99 41 43 28 113 83 193 219 153 129 155 90 123 17 209 97 235 51 73 27 234 138 49 98 81 106 58 186 139 187 226 25 224 18 65 210 131 121 67 82 137 89 60 145 232 227 179 44 33 1 194 59 195 177 2 74 233 170 50 202 10 201 162 52 105 57 26 161 218 146 171 163 66 115 217 154 122 178 130 75 211 4 12 185 147 114",
"output": "224 8"
},
{
"input": "7 4\n5 2 1 4",
"output": "2 3"
},
{
"input": "7 4\n5 2 1 6",
"output": "6 3"
},
{
"input": "17 16\n11 14 7 15 13 6 3 0 4 10 12 1 16 5 8 9",
"output": "3 1"
},
{
"input": "17 16\n10 0 7 5 13 3 16 14 1 6 8 15 2 9 12 11",
"output": "5 1"
}
] | 31 | 0 | 0 | 21,897 |
|
754 | Fedor and coupons | [
"binary search",
"data structures",
"greedy",
"sortings"
] | null | null | All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring supermarket.
The goods in the supermarket have unique integer ids. Also, for every integer there is a product with id equal to this integer. Fedor has *n* discount coupons, the *i*-th of them can be used with products with ids ranging from *l**i* to *r**i*, inclusive. Today Fedor wants to take exactly *k* coupons with him.
Fedor wants to choose the *k* coupons in such a way that the number of such products *x* that all coupons can be used with this product *x* is as large as possible (for better understanding, see examples). Fedor wants to save his time as well, so he asks you to choose coupons for him. Help Fedor! | The first line contains two integers *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=3·105) — the number of coupons Fedor has, and the number of coupons he wants to choose.
Each of the next *n* lines contains two integers *l**i* and *r**i* (<=-<=109<=≤<=*l**i*<=≤<=*r**i*<=≤<=109) — the description of the *i*-th coupon. The coupons can be equal. | In the first line print single integer — the maximum number of products with which all the chosen coupons can be used. The products with which at least one coupon cannot be used shouldn't be counted.
In the second line print *k* distinct integers *p*1,<=*p*2,<=...,<=*p**k* (1<=≤<=*p**i*<=≤<=*n*) — the ids of the coupons which Fedor should choose.
If there are multiple answers, print any of them. | [
"4 2\n1 100\n40 70\n120 130\n125 180\n",
"3 2\n1 12\n15 20\n25 30\n",
"5 2\n1 10\n5 15\n14 50\n30 70\n99 100\n"
] | [
"31\n1 2 \n",
"0\n1 2 \n",
"21\n3 4 \n"
] | In the first example if we take the first two coupons then all the products with ids in range [40, 70] can be bought with both coupons. There are 31 products in total.
In the second example, no product can be bought with two coupons, that is why the answer is 0. Fedor can choose any two coupons in this example. | [
{
"input": "4 2\n1 100\n40 70\n120 130\n125 180",
"output": "31\n1 2 "
},
{
"input": "3 2\n1 12\n15 20\n25 30",
"output": "0\n1 2 "
},
{
"input": "5 2\n1 10\n5 15\n14 50\n30 70\n99 100",
"output": "21\n3 4 "
},
{
"input": "7 6\n-8 6\n7 9\n-10 -5\n-6 10\n-7 -3\n5 8\n4 10",
"output": "0\n1 2 3 4 5 6 "
},
{
"input": "9 6\n-7 -3\n-3 10\n-6 1\n-1 8\n-9 4\n-7 -6\n-5 -3\n-10 -2\n3 4",
"output": "1\n1 2 3 5 7 8 "
},
{
"input": "7 7\n9 10\n-5 3\n-6 2\n1 6\n-9 6\n-10 7\n-7 -5",
"output": "0\n1 2 3 4 5 6 7 "
},
{
"input": "23 2\n-629722518 -626148345\n739975524 825702590\n-360913153 -208398929\n76588954 101603025\n-723230356 -650106339\n-117490984 -101920679\n-39187628 -2520915\n717852164 720343632\n-611281114 -579708833\n-141791522 -122348148\n605078929 699430996\n-873386085 -820238799\n-922404067 -873522961\n7572046 13337057\n975081176 977171682\n901338407 964254238\n325388219 346712972\n505189756 516497863\n-425326983 -422098946\n520670681 522544433\n-410872616 -367919621\n359488350 447471156\n-566203447 -488202136",
"output": "0\n1 2 "
},
{
"input": "24 21\n240694945 246896662\n240694930 246896647\n240695065 246896782\n240695050 246896767\n240695080 246896797\n240694960 246896677\n240694975 246896692\n240694825 246896542\n240694900 246896617\n240694915 246896632\n240694885 246896602\n240694855 246896572\n240694870 246896587\n240694795 246896512\n240695095 246896812\n240695125 246896842\n240695005 246896722\n240694990 246896707\n240695140 246896857\n240695020 246896737\n240695035 246896752\n240694840 246896557\n240694810 246896527\n240695110 246896827",
"output": "6201418\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 18 20 21 22 23 "
},
{
"input": "1 1\n2 2",
"output": "1\n1 "
},
{
"input": "1 1\n-1000000000 1000000000",
"output": "2000000001\n1 "
},
{
"input": "2 1\n-1000000000 -1000000000\n1000000000 1000000000",
"output": "1\n1 "
},
{
"input": "7 3\n3 3\n-6 -1\n6 7\n2 8\n3 10\n-8 0\n-3 10",
"output": "6\n4 5 7 "
},
{
"input": "5 4\n4 7\n-4 2\n-7 -7\n-5 -2\n-8 -8",
"output": "0\n1 2 3 4 "
},
{
"input": "7 7\n0 7\n9 9\n-10 -7\n5 8\n-10 4\n-7 0\n-3 5",
"output": "0\n1 2 3 4 5 6 7 "
},
{
"input": "9 2\n5 10\n-10 -10\n0 10\n-6 3\n-8 7\n6 10\n-8 1\n5 7\n2 2",
"output": "10\n5 7 "
},
{
"input": "9 5\n-2 1\n-6 9\n-7 -2\n5 7\n-10 -7\n-9 -2\n1 4\n-1 10\n4 8",
"output": "0\n1 2 3 4 5 "
},
{
"input": "54 7\n-98 -39\n14 60\n-23 -5\n58 75\n14 16\n-40 20\n-6 10\n11 60\n-47 54\n-71 -17\n-48 -25\n-87 -46\n-10 99\n-97 -88\n-14 94\n-25 29\n-96 -92\n68 75\n-75 2\n12 84\n-47 3\n-88 49\n-37 88\n-61 -25\n36 67\n30 54\n12 31\n-71 60\n-18 -15\n-61 -47\n-51 -41\n-67 51\n26 37\n18 94\n-67 52\n-16 56\n-5 26\n27 57\n36 91\n-61 61\n71 86\n27 73\n-57 -39\n54 71\n-16 14\n-97 81\n-32 49\n-18 50\n-63 93\n51 70\n8 66\n43 45\n-2 99\n11 98",
"output": "111\n22 28 32 35 40 46 49 "
},
{
"input": "52 18\n-50 54\n35 65\n67 82\n-87 -10\n-39 4\n-55 -18\n-27 90\n-42 73\n18 43\n70 85\n-85 -22\n-1 60\n-89 23\n-78 -75\n-14 69\n-69 50\n-93 74\n-10 45\n-81 -72\n-24 86\n-89 100\n25 70\n-65 -61\n-45 100\n-49 -23\n-74 -59\n-81 -15\n-58 47\n-65 -58\n-47 16\n-22 91\n-85 19\n-81 77\n79 87\n-31 88\n26 32\n11 90\n7 46\n64 83\n-51 -20\n-76 44\n-22 75\n45 84\n-98 46\n-20 78\n-88 -47\n-41 65\n2 93\n-66 69\n-73 94\n-85 -44\n-65 -23",
"output": "67\n1 7 8 16 17 20 21 24 28 31 33 35 41 42 44 47 49 50 "
}
] | 46 | 102,400 | 0 | 21,909 |
|
150 | Quantity of Strings | [
"combinatorics",
"dfs and similar",
"graphs",
"math"
] | null | null | Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, let's say there are strings with the length exactly *n*, based on the alphabet of size *m*. Any its substring with length equal to *k* is a palindrome. How many such strings exist? Your task is to find their quantity modulo 1000000007 (109<=+<=7). Be careful and don't miss a string or two!
Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. | The first and only line contains three integers: *n*, *m* and *k* (1<=≤<=*n*,<=*m*,<=*k*<=≤<=2000). | Print a single integer — the number of strings of the described type modulo 1000000007 (109<=+<=7). | [
"1 1 1\n",
"5 2 4\n"
] | [
"1\n",
"2\n"
] | In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a").
In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb". | [
{
"input": "1 1 1",
"output": "1"
},
{
"input": "5 2 4",
"output": "2"
},
{
"input": "7 4 20",
"output": "16384"
},
{
"input": "8 13 9",
"output": "815730721"
},
{
"input": "10 23 9",
"output": "529"
},
{
"input": "10 25 8",
"output": "25"
},
{
"input": "997 1752 1000",
"output": "184834849"
},
{
"input": "784 1 1999",
"output": "1"
},
{
"input": "341 9 342",
"output": "320920086"
},
{
"input": "777 1 777",
"output": "1"
},
{
"input": "542 13 542",
"output": "490685740"
},
{
"input": "1501 893 1501",
"output": "889854713"
},
{
"input": "1321 95 2",
"output": "95"
},
{
"input": "2000 1000 3",
"output": "1000000"
},
{
"input": "1769 849 1000",
"output": "849"
},
{
"input": "1000 2 1",
"output": "688423210"
},
{
"input": "345 1777 1",
"output": "756253754"
},
{
"input": "1999 2000 2000",
"output": "675798323"
},
{
"input": "1984 1847 1992",
"output": "345702953"
},
{
"input": "2000 2000 2000",
"output": "321179016"
},
{
"input": "1451 239 1451",
"output": "968856942"
},
{
"input": "2000 2000 1",
"output": "596636543"
},
{
"input": "1230 987 1",
"output": "890209975"
},
{
"input": "1764 305 843",
"output": "93025"
},
{
"input": "1999 98 132",
"output": "98"
},
{
"input": "2000 2 10",
"output": "2"
},
{
"input": "2000 1999 1999",
"output": "3996001"
},
{
"input": "1678 1999 1234",
"output": "1999"
},
{
"input": "7 10 7",
"output": "10000"
},
{
"input": "15 1 15",
"output": "1"
},
{
"input": "2000 2000 1000",
"output": "2000"
},
{
"input": "1 2000 2000",
"output": "2000"
},
{
"input": "10 10 90",
"output": "999999937"
},
{
"input": "100 100 1",
"output": "424090053"
},
{
"input": "6 6 6",
"output": "216"
},
{
"input": "10 10 1",
"output": "999999937"
},
{
"input": "100 10 100",
"output": "319300014"
},
{
"input": "5 4 5",
"output": "64"
},
{
"input": "5 2 5",
"output": "8"
},
{
"input": "1000 1000 1000",
"output": "850431726"
},
{
"input": "5 5 1",
"output": "3125"
},
{
"input": "1000 1000 1",
"output": "524700271"
},
{
"input": "4 256 1",
"output": "294967268"
},
{
"input": "5 5 5",
"output": "125"
},
{
"input": "10 10 10",
"output": "100000"
},
{
"input": "100 100 100",
"output": "226732710"
},
{
"input": "5 2 1",
"output": "32"
},
{
"input": "4 4 4",
"output": "16"
},
{
"input": "15 5 1",
"output": "517577915"
},
{
"input": "1000 2 1001",
"output": "688423210"
},
{
"input": "100 7 3",
"output": "49"
},
{
"input": "8 2 8",
"output": "16"
},
{
"input": "200 200 200",
"output": "104842676"
},
{
"input": "4 4 1",
"output": "256"
},
{
"input": "1999 1999 1999",
"output": "21610777"
},
{
"input": "17 5 1",
"output": "939447791"
},
{
"input": "100 5 1",
"output": "146981449"
},
{
"input": "10 5 1",
"output": "9765625"
},
{
"input": "11 2 11",
"output": "64"
},
{
"input": "100 1000 1",
"output": "327648028"
},
{
"input": "3 1000 3",
"output": "1000000"
},
{
"input": "3 3 3",
"output": "9"
},
{
"input": "3 5 3",
"output": "25"
},
{
"input": "20 3 3",
"output": "9"
},
{
"input": "10 2 1",
"output": "1024"
},
{
"input": "7 2 7",
"output": "16"
},
{
"input": "13 9 1",
"output": "865810542"
},
{
"input": "11 2 5",
"output": "4"
},
{
"input": "13 13 13",
"output": "62748517"
},
{
"input": "239 123 239",
"output": "221051222"
}
] | 15 | 0 | 0 | 21,913 |
|
515 | Drazil and His Happy Friends | [
"brute force",
"dsu",
"meet-in-the-middle",
"number theory"
] | null | null | Drazil has many friends. Some of them are happy and some of them are unhappy. Drazil wants to make all his friends become happy. So he invented the following plan.
There are *n* boys and *m* girls among his friends. Let's number them from 0 to *n*<=-<=1 and 0 to *m*<=-<=1 separately. In *i*-th day, Drazil invites -th boy and -th girl to have dinner together (as Drazil is programmer, *i* starts from 0). If one of those two people is happy, the other one will also become happy. Otherwise, those two people remain in their states. Once a person becomes happy (or if he/she was happy originally), he stays happy forever.
Drazil wants to know whether he can use this plan to make all his friends become happy at some moment. | The first line contains two integer *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100).
The second line contains integer *b* (0<=≤<=*b*<=≤<=*n*), denoting the number of happy boys among friends of Drazil, and then follow *b* distinct integers *x*1,<=*x*2,<=...,<=*x**b* (0<=≤<=*x**i*<=<<=*n*), denoting the list of indices of happy boys.
The third line conatins integer *g* (0<=≤<=*g*<=≤<=*m*), denoting the number of happy girls among friends of Drazil, and then follow *g* distinct integers *y*1,<=*y*2,<=... ,<=*y**g* (0<=≤<=*y**j*<=<<=*m*), denoting the list of indices of happy girls.
It is guaranteed that there is at least one person that is unhappy among his friends. | If Drazil can make all his friends become happy by this plan, print "Yes". Otherwise, print "No". | [
"2 3\n0\n1 0\n",
"2 4\n1 0\n1 2\n",
"2 3\n1 0\n1 1\n"
] | [
"Yes\n",
"No\n",
"Yes\n"
] | By <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/169ade208e6eb4f9263c57aaff716529d59c3288.png" style="max-width: 100.0%;max-height: 100.0%;"/> we define the remainder of integer division of *i* by *k*.
In first sample case:
- On the 0-th day, Drazil invites 0-th boy and 0-th girl. Because 0-th girl is happy at the beginning, 0-th boy become happy at this day. - On the 1-st day, Drazil invites 1-st boy and 1-st girl. They are both unhappy, so nothing changes at this day. - On the 2-nd day, Drazil invites 0-th boy and 2-nd girl. Because 0-th boy is already happy he makes 2-nd girl become happy at this day. - On the 3-rd day, Drazil invites 1-st boy and 0-th girl. 0-th girl is happy, so she makes 1-st boy happy. - On the 4-th day, Drazil invites 0-th boy and 1-st girl. 0-th boy is happy, so he makes the 1-st girl happy. So, all friends become happy at this moment. | [
{
"input": "2 3\n0\n1 0",
"output": "Yes"
},
{
"input": "2 4\n1 0\n1 2",
"output": "No"
},
{
"input": "2 3\n1 0\n1 1",
"output": "Yes"
},
{
"input": "16 88\n6 5 14 2 0 12 7\n30 21 64 35 79 74 39 63 44 81 73 0 27 33 69 12 86 46 20 25 55 52 7 58 23 5 60 32 41 50 82",
"output": "Yes"
},
{
"input": "52 91\n13 26 1 3 43 17 19 32 46 33 48 23 37 50\n25 78 26 1 40 2 67 42 4 56 30 70 84 32 20 85 59 8 86 34 73 23 10 88 24 11",
"output": "No"
},
{
"input": "26 52\n8 0 14 16 17 7 9 10 11\n15 39 15 2 41 42 30 17 18 31 6 21 35 48 50 51",
"output": "No"
},
{
"input": "50 50\n0\n0",
"output": "No"
},
{
"input": "27 31\n4 25 5 19 20\n26 5 28 17 2 1 0 26 23 12 29 6 4 25 19 15 13 20 24 8 27 22 30 3 10 9 7",
"output": "Yes"
},
{
"input": "55 79\n5 51 27 36 45 53\n30 15 28 0 5 38 3 34 30 35 1 32 12 27 42 39 69 33 10 63 16 29 76 19 60 70 67 31 78 68 45",
"output": "Yes"
},
{
"input": "79 23\n35 31 62 14 9 46 18 68 69 42 13 50 77 23 76 5 53 40 16 32 74 54 38 25 45 39 26 37 66 78 3 48 10 17 56 59\n13 16 0 8 6 18 14 21 11 20 4 15 13 22",
"output": "Yes"
},
{
"input": "7 72\n1 4\n3 49 32 28",
"output": "Yes"
},
{
"input": "100 50\n31 52 54 8 60 61 62 63 64 16 19 21 73 25 76 77 79 30 81 32 33 34 37 88 39 40 91 42 94 95 96 98\n18 0 1 3 5 6 7 9 15 18 20 22 24 28 35 36 43 47 49",
"output": "No"
},
{
"input": "98 49\n33 0 51 52 6 57 10 12 63 15 16 19 20 21 72 73 74 76 77 78 30 31 81 33 83 37 38 39 40 92 44 45 95 97\n15 4 5 7 9 11 13 17 18 22 26 35 36 41 42 47",
"output": "No"
},
{
"input": "50 50\n14 7 8 12 16 18 22 23 24 28 30 35 40 46 49\n35 0 1 2 3 4 5 6 9 10 11 13 14 15 17 19 20 21 25 26 27 29 31 32 33 34 36 37 38 39 41 43 44 45 47 48",
"output": "No"
},
{
"input": "30 44\n3 8 26 28\n6 2 30 38 26 8 6",
"output": "No"
},
{
"input": "69 72\n18 58 46 52 43 1 55 16 7 4 38 68 14 32 53 41 29 2 59\n21 22 43 55 13 70 4 7 31 10 23 56 44 62 17 50 53 5 41 11 65 32",
"output": "No"
},
{
"input": "76 28\n10 24 13 61 45 29 57 41 21 37 11\n2 12 9",
"output": "No"
},
{
"input": "65 75\n15 25 60 12 62 37 22 47 52 3 63 58 13 14 49 34\n18 70 10 2 52 22 47 72 57 38 48 13 73 3 19 4 74 49 34",
"output": "No"
},
{
"input": "6 54\n1 5\n14 13 49 31 37 44 2 15 51 52 22 28 10 35 47",
"output": "No"
},
{
"input": "96 36\n34 84 24 0 48 85 13 61 37 62 38 86 75 3 16 64 40 28 76 53 5 17 42 6 7 91 67 55 68 92 57 11 71 35 59\n9 1 14 15 17 18 30 6 8 35",
"output": "No"
},
{
"input": "40 40\n23 0 2 3 4 5 7 11 15 16 17 18 19 22 25 28 29 30 31 32 34 35 36 37\n16 1 6 8 9 10 12 13 14 20 21 23 24 26 27 38 39",
"output": "No"
},
{
"input": "66 66\n24 2 35 3 36 4 5 10 45 14 48 18 51 19 21 55 22 23 24 25 26 63 31 65 32\n21 0 1 37 6 40 7 8 42 45 13 15 16 50 53 23 24 60 28 62 63 31",
"output": "No"
},
{
"input": "20 20\n9 0 3 4 6 7 8 10 12 13\n10 1 2 5 9 11 14 15 16 18 19",
"output": "No"
},
{
"input": "75 30\n18 46 47 32 33 3 34 35 21 51 7 9 54 39 72 42 59 29 14\n8 0 17 5 6 23 26 27 13",
"output": "No"
},
{
"input": "100 50\n30 50 54 7 8 59 60 61 62 63 64 15 16 18 19 20 22 73 27 79 83 86 87 89 42 93 94 45 46 97 98\n20 1 2 3 5 6 17 21 24 25 26 28 30 31 32 34 35 38 40 41 49",
"output": "Yes"
},
{
"input": "98 98\n43 49 1 51 3 53 4 55 56 8 9 10 60 11 12 61 64 16 65 17 19 20 21 72 24 74 25 77 78 31 34 35 36 37 87 88 89 42 92 43 44 94 46 96\n34 50 2 52 5 54 9 62 63 15 18 68 70 22 72 75 26 27 77 30 81 82 83 35 36 37 87 88 89 90 41 93 95 96 48",
"output": "No"
},
{
"input": "100 100\n45 50 1 4 5 55 7 8 10 60 61 62 63 14 65 66 17 18 20 21 22 24 25 27 78 28 29 30 31 82 83 33 84 36 37 38 39 40 41 42 44 45 46 48 98 49\n34 50 1 2 52 3 54 56 7 9 59 61 14 16 67 18 69 22 73 24 76 79 81 82 84 35 36 38 39 90 43 44 45 47 49",
"output": "No"
},
{
"input": "76 72\n29 4 64 68 20 8 12 50 42 46 0 70 11 37 75 47 45 29 17 19 73 9 41 31 35 67 65 39 51 55\n25 60 32 48 42 8 6 9 7 31 19 25 5 33 51 61 67 55 49 27 29 53 39 65 35 13",
"output": "Yes"
},
{
"input": "39 87\n16 18 15 30 33 21 9 3 31 16 10 34 20 35 8 26 23\n36 33 75 81 24 42 54 78 39 57 60 30 36 63 4 76 25 1 40 73 22 58 49 85 31 74 59 20 44 83 65 23 41 71 47 14 35",
"output": "Yes"
},
{
"input": "36 100\n10 0 32 4 5 33 30 18 14 35 7\n29 60 32 20 4 16 69 5 38 50 46 74 94 18 82 2 66 22 42 55 51 91 67 75 35 95 43 79 3 27",
"output": "Yes"
},
{
"input": "90 25\n26 55 30 35 20 15 26 6 1 41 81 76 46 57 17 12 67 77 27 47 62 8 43 63 3 48 19\n9 10 16 21 7 17 12 13 19 9",
"output": "Yes"
},
{
"input": "66 66\n26 0 54 6 37 43 13 25 38 2 32 56 20 50 39 27 51 9 64 4 16 17 65 11 5 47 23\n15 6 24 43 49 25 20 14 63 27 3 58 52 53 11 41",
"output": "No"
},
{
"input": "24 60\n4 0 2 19 23\n15 12 24 49 2 14 3 52 28 5 6 19 32 33 34 35",
"output": "Yes"
},
{
"input": "80 40\n27 0 41 44 45 6 47 8 10 52 13 14 16 17 18 59 21 62 23 64 26 68 29 32 75 37 78 39\n13 2 3 9 11 15 20 25 27 30 31 33 34 36",
"output": "Yes"
},
{
"input": "66 99\n23 33 35 36 38 8 10 44 11 45 46 47 50 19 54 22 55 23 58 59 27 61 30 65\n32 33 67 69 4 70 38 6 39 7 74 42 9 43 12 13 14 15 81 82 84 85 20 87 89 90 24 58 59 27 95 97 31",
"output": "Yes"
},
{
"input": "100 40\n25 61 42 2 3 25 46 66 68 69 49 9 10 50 91 72 92 33 73 53 14 15 55 96 36 39\n12 0 22 3 23 4 6 27 11 35 37 38 39",
"output": "Yes"
},
{
"input": "90 30\n27 15 16 2 32 78 49 64 65 50 6 66 21 22 82 23 39 84 85 10 86 56 27 87 13 58 44 74\n7 19 4 20 24 25 12 27",
"output": "No"
},
{
"input": "75 75\n33 30 74 57 23 19 42 71 11 44 29 58 43 48 61 63 13 27 50 17 18 70 64 39 12 32 36 10 40 51 49 1 54 73\n8 43 23 0 7 63 47 74 28",
"output": "No"
},
{
"input": "98 98\n23 6 81 90 28 38 51 23 69 13 95 15 16 88 58 10 26 42 44 54 92 27 45 39\n18 20 70 38 82 72 61 37 78 74 23 15 56 59 35 93 64 28 57",
"output": "No"
},
{
"input": "75 75\n19 48 3 5 67 23 8 70 45 63 36 38 56 15 10 37 52 11 9 27\n21 13 9 45 28 59 36 30 43 5 38 27 40 50 17 41 71 8 51 63 1 33",
"output": "No"
},
{
"input": "3 20\n0\n1 19",
"output": "Yes"
},
{
"input": "41 2\n1 33\n0",
"output": "Yes"
},
{
"input": "50 49\n1 49\n0",
"output": "Yes"
},
{
"input": "3 50\n0\n1 49",
"output": "Yes"
},
{
"input": "100 100\n50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\n49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98",
"output": "No"
},
{
"input": "100 100\n50 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49\n50 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99",
"output": "Yes"
},
{
"input": "91 98\n78 0 1 2 3 4 5 7 8 9 10 11 12 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 33 35 36 37 38 39 40 42 43 44 45 46 47 49 50 51 52 53 54 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 79 80 81 82 84 85 86 87 88 89\n84 0 1 2 3 4 5 7 8 9 10 11 12 14 15 16 17 18 19 21 22 23 24 25 26 28 29 30 31 32 33 35 36 37 38 39 40 42 43 44 45 46 47 49 50 51 52 53 54 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 79 80 81 82 84 85 86 87 88 89 91 92 93 94 95 96",
"output": "No"
},
{
"input": "99 84\n66 0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83 84 86 87 89 90 92 93 95 96 98\n56 0 2 3 5 6 8 9 11 12 14 15 17 18 20 21 23 24 26 27 29 30 32 33 35 36 38 39 41 42 44 45 47 48 50 51 53 54 56 57 59 60 62 63 65 66 68 69 71 72 74 75 77 78 80 81 83",
"output": "No"
},
{
"input": "75 90\n60 0 2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20 22 23 24 25 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 47 48 49 50 52 53 54 55 57 58 59 60 62 63 64 65 67 68 69 70 72 73 74\n72 0 2 3 4 5 7 8 9 10 12 13 14 15 17 18 19 20 22 23 24 25 27 28 29 30 32 33 34 35 37 38 39 40 42 43 44 45 47 48 49 50 52 53 54 55 57 58 59 60 62 63 64 65 67 68 69 70 72 73 74 75 77 78 79 80 82 83 84 85 87 88 89",
"output": "No"
},
{
"input": "5 7\n1 0\n1 0",
"output": "Yes"
},
{
"input": "100 1\n1 99\n0",
"output": "Yes"
},
{
"input": "4 1\n1 3\n0",
"output": "Yes"
},
{
"input": "4 5\n3 0 1 3\n4 0 1 3 4",
"output": "Yes"
},
{
"input": "100 99\n1 99\n0",
"output": "Yes"
},
{
"input": "2 3\n1 0\n2 0 2",
"output": "Yes"
}
] | 77 | 6,963,200 | 0 | 21,922 |
|
251 | Two Sets | [
"bitmasks",
"math"
] | null | null | Little Petya likes numbers a lot. Recently his mother has presented him a collection of *n* non-negative integers. There's only one thing Petya likes more than numbers: playing with little Masha. He immediately decided to give a part of his new collection to her. To make the game even more interesting, Petya decided to give Masha such collection of numbers for which the following conditions fulfill:
- Let's introduce *x*1 to denote the *xor* of all numbers Petya has got left; and let's introduce *x*2 to denote the *xor* of all numbers he gave to Masha. Value (*x*1<=+<=*x*2) must be as large as possible. - If there are multiple ways to divide the collection so that the previous condition fulfilled, then Petya minimizes the value *x*1.
The *xor* operation is a bitwise excluding "OR", that is denoted as "xor" in the Pascal language and "^" in C/C++/Java.
Help Petya divide the collection as described above. If there are multiple suitable ways to divide it, find any of them. Please note that after Petya gives a part of his numbers to Masha, he may have no numbers left. The reverse situation is also possible, when Petya gives nothing to Masha. In both cases we must assume that the *xor* of an empty set of numbers equals 0. | The first line contains integer *n* (1<=≤<=*n*<=≤<=105), showing how many numbers Petya's mother gave him. The second line contains the actual space-separated numbers. They are all integer, non-negative and do not exceed 1018. | Print *n* space-separated integers, the *i*-th of them should equal either 1, if Petya keeps the number that follows *i*-th in his collection, or it should equal 2, if Petya gives the corresponding number to Masha. The numbers are indexed in the order in which they are given in the input. | [
"6\n1 2 3 4 5 6\n",
"3\n1000000000000 1000000000000 1000000000000\n",
"8\n1 1 2 2 3 3 4 4\n"
] | [
"2 2 2 2 2 2\n",
"2 2 2\n",
"1 2 1 2 2 2 1 2\n"
] | none | [] | 30 | 0 | 0 | 22,073 |
|
852 | Casinos and travel | [
"dp"
] | null | null | John has just bought a new car and is planning a journey around the country. Country has *N* cities, some of which are connected by bidirectional roads. There are *N*<=-<=1 roads and every city is reachable from any other city. Cities are labeled from 1 to *N*.
John first has to select from which city he will start his journey. After that, he spends one day in a city and then travels to a randomly choosen city which is directly connected to his current one and which he has not yet visited. He does this until he can't continue obeying these rules.
To select the starting city, he calls his friend Jack for advice. Jack is also starting a big casino business and wants to open casinos in some of the cities (max 1 per city, maybe nowhere). Jack knows John well and he knows that if he visits a city with a casino, he will gamble exactly once before continuing his journey.
He also knows that if John enters a casino in a good mood, he will leave it in a bad mood and vice versa. Since he is John's friend, he wants him to be in a good mood at the moment when he finishes his journey. John is in a good mood before starting the journey.
In how many ways can Jack select a starting city for John and cities where he will build casinos such that no matter how John travels, he will be in a good mood at the end? Print answer modulo 109<=+<=7. | In the first line, a positive integer *N* (1<=≤<=*N*<=≤<=100000), the number of cities.
In the next *N*<=-<=1 lines, two numbers *a*,<= *b* (1<=≤<=*a*,<=*b*<=≤<=*N*) separated by a single space meaning that cities *a* and *b* are connected by a bidirectional road. | Output one number, the answer to the problem modulo 109<=+<=7. | [
"2\n1 2\n",
"3\n1 2\n2 3\n"
] | [
"4\n",
"10\n"
] | Example 1: If Jack selects city 1 as John's starting city, he can either build 0 casinos, so John will be happy all the time, or build a casino in both cities, so John would visit a casino in city 1, become unhappy, then go to city 2, visit a casino there and become happy and his journey ends there because he can't go back to city 1. If Jack selects city 2 for start, everything is symmetrical, so the answer is 4.
Example 2: If Jack tells John to start from city 1, he can either build casinos in 0 or 2 cities (total 4 possibilities). If he tells him to start from city 2, then John's journey will either contain cities 2 and 1 or 2 and 3. Therefore, Jack will either have to build no casinos, or build them in all three cities. With other options, he risks John ending his journey unhappy. Starting from 3 is symmetric to starting from 1, so in total we have 4 + 2 + 4 = 10 options. | [
{
"input": "2\n1 2",
"output": "4"
},
{
"input": "3\n1 2\n2 3",
"output": "10"
},
{
"input": "4\n1 2\n2 3\n3 4",
"output": "24"
}
] | 78 | 0 | 0 | 22,217 |
|
995 | Number Clicker | [
"divide and conquer",
"graphs",
"meet-in-the-middle",
"number theory"
] | null | null | Allen is playing Number Clicker on his phone.
He starts with an integer $u$ on the screen. Every second, he can press one of 3 buttons.
1. Turn $u \to u+1 \pmod{p}$. 1. Turn $u \to u+p-1 \pmod{p}$. 1. Turn $u \to u^{p-2} \pmod{p}$.
Allen wants to press at most 200 buttons and end up with $v$ on the screen. Help him! | The first line of the input contains 3 positive integers: $u, v, p$ ($0 \le u, v \le p-1$, $3 \le p \le 10^9 + 9$). $p$ is guaranteed to be prime. | On the first line, print a single integer $\ell$, the number of button presses. On the second line, print integers $c_1, \dots, c_\ell$, the button presses. For $1 \le i \le \ell$, $1 \le c_i \le 3$.
We can show that the answer always exists. | [
"1 3 5\n",
"3 2 5\n"
] | [
"2\n1 1\n",
"1\n3\n"
] | In the first example the integer on the screen changes as $1 \to 2 \to 3$.
In the second example the integer on the screen changes as $3 \to 2$. | [
{
"input": "1 3 5",
"output": "2\n1 1 "
},
{
"input": "3 2 5",
"output": "1\n2 "
},
{
"input": "9382509 19872987 1000000007",
"output": "38\n3 2 2 3 2 2 2 3 2 2 2 2 2 2 3 2 2 2 2 2 2 3 2 3 2 3 2 2 2 3 2 2 2 3 1 1 1 1 "
},
{
"input": "1 1 3",
"output": "0"
},
{
"input": "13 12 17",
"output": "1\n2 "
},
{
"input": "244 821 991",
"output": "6\n2 2 2 2 2 3 "
},
{
"input": "287 303 503",
"output": "8\n1 3 2 2 2 3 1 1 "
},
{
"input": "619771201 494377834 942874897",
"output": "38\n1 1 1 1 1 1 3 1 1 1 1 1 3 1 1 1 1 3 2 2 2 3 1 1 1 1 3 2 2 2 2 3 1 1 1 1 1 1 "
},
{
"input": "406327795 273505277 953480621",
"output": "39\n1 1 1 1 3 2 2 2 2 2 3 2 2 2 3 2 2 3 2 2 2 2 3 2 2 2 3 1 1 1 3 1 1 1 1 1 1 3 1 "
},
{
"input": "161411379 280695163 869964743",
"output": "33\n3 1 1 1 3 2 2 2 3 1 1 1 1 1 1 3 2 2 2 2 2 2 3 2 2 2 2 3 1 1 1 1 3 "
},
{
"input": "17 3 23",
"output": "4\n1 3 2 3 "
},
{
"input": "1 0 3",
"output": "1\n2 "
},
{
"input": "13 15 17",
"output": "2\n1 1 "
},
{
"input": "201 781 953",
"output": "5\n1 1 1 1 3 "
},
{
"input": "310322257 279716309 386661139",
"output": "38\n2 3 2 2 2 3 2 2 2 2 2 2 3 1 1 3 1 1 1 3 2 2 2 2 3 2 3 2 3 2 2 2 3 1 1 1 3 2 "
},
{
"input": "126068657 109083704 251820617",
"output": "35\n2 3 1 1 1 1 3 1 1 1 1 1 1 3 1 1 3 1 1 1 1 1 1 3 1 1 3 1 3 1 1 1 1 3 1 "
},
{
"input": "106744267 156561352 172521799",
"output": "37\n1 3 1 1 3 1 1 1 1 1 1 1 1 3 2 2 2 2 2 2 2 3 2 3 2 2 2 2 2 2 3 2 2 2 3 1 1 "
},
{
"input": "34490466 26167681 52232113",
"output": "30\n3 1 1 3 1 1 3 1 1 1 1 3 1 1 1 3 1 1 1 3 2 2 2 2 2 3 2 2 2 3 "
},
{
"input": "28320405 18816765 30017753",
"output": "32\n2 2 3 1 1 1 3 2 2 2 2 2 2 2 2 2 2 2 2 2 3 1 1 1 1 1 3 1 1 1 1 3 "
},
{
"input": "130445610 273782594 516362461",
"output": "32\n1 1 3 2 2 2 3 1 1 1 1 3 1 1 3 1 1 1 3 2 2 2 2 2 2 3 1 1 1 1 1 1 "
},
{
"input": "594992010 836104056 970461161",
"output": "38\n1 1 3 1 3 1 3 1 1 3 1 1 1 3 1 1 3 1 1 1 3 2 2 2 2 2 2 2 2 2 2 2 3 1 1 1 1 1 "
},
{
"input": "251072155 414398479 468625081",
"output": "37\n2 2 3 2 3 2 2 2 3 2 2 2 3 2 2 2 3 2 2 2 3 2 2 3 2 2 2 2 3 1 1 1 1 3 2 2 2 "
},
{
"input": "208415518 131689736 367420219",
"output": "36\n2 2 3 2 2 3 2 2 3 2 2 2 2 3 1 1 1 3 2 2 2 2 2 3 2 2 2 2 2 2 3 2 2 2 2 2 "
},
{
"input": "525619238 277413604 867511691",
"output": "28\n2 2 3 2 2 3 2 2 3 2 2 2 3 2 2 3 2 2 2 2 2 2 3 2 2 3 2 3 "
},
{
"input": "165452923 679602640 739709917",
"output": "38\n2 3 1 1 1 3 2 2 2 2 2 2 2 2 2 2 2 3 1 1 1 3 1 1 3 1 1 3 2 2 2 2 3 2 2 2 3 2 "
},
{
"input": "445573326 471226202 609552373",
"output": "40\n1 3 1 1 1 3 1 1 3 1 1 3 1 1 1 3 1 1 1 3 1 3 1 1 1 1 1 3 1 1 3 1 1 3 1 1 1 1 1 3 "
},
{
"input": "813619651 576180150 844917077",
"output": "37\n3 2 2 2 2 2 3 1 1 3 1 3 1 1 1 1 3 1 1 3 1 1 1 3 1 1 3 2 2 2 3 2 2 2 3 1 1 "
},
{
"input": "276091328 569300721 628687517",
"output": "36\n2 3 2 2 2 2 3 1 1 3 1 1 1 1 1 3 2 2 2 2 3 2 2 2 3 2 2 2 2 3 2 2 3 2 3 2 "
},
{
"input": "10704524 4053898 21315919",
"output": "31\n2 2 2 2 3 2 2 3 2 2 3 2 2 3 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 3 2 "
},
{
"input": "215222334 336333000 617545127",
"output": "39\n1 1 3 1 1 1 3 2 2 3 2 2 2 2 2 3 2 2 3 2 2 2 2 2 2 2 3 2 3 2 2 2 2 2 2 2 2 3 1 "
},
{
"input": "554439429 472180190 795177017",
"output": "39\n1 1 1 1 3 2 2 2 3 2 2 2 2 3 2 2 2 2 3 2 2 2 2 2 2 3 1 1 1 3 1 1 1 1 1 1 3 2 2 "
},
{
"input": "328711343 84690411 340048367",
"output": "36\n3 2 2 3 2 2 2 2 2 3 1 1 3 1 1 1 1 1 1 3 2 2 2 2 2 2 2 2 2 2 2 3 2 2 3 1 "
},
{
"input": "189244535 265313513 278022359",
"output": "32\n3 1 1 1 3 1 1 3 1 1 1 1 1 3 2 2 2 3 2 2 2 3 2 2 2 3 2 2 2 2 3 2 "
},
{
"input": "234539405 71152890 317131601",
"output": "34\n1 1 3 1 1 1 3 2 2 3 2 3 2 2 2 3 2 2 2 3 2 2 3 2 2 3 2 2 2 2 2 3 2 2 "
},
{
"input": "205923958 239975594 417820957",
"output": "38\n2 2 2 3 2 2 3 2 2 3 2 2 2 2 3 1 1 3 1 3 1 1 1 3 1 1 3 1 1 1 3 1 1 3 1 1 3 2 "
},
{
"input": "284332909 310883155 483002431",
"output": "37\n3 1 1 1 1 3 1 1 1 3 1 1 1 1 1 3 1 1 3 1 1 3 1 1 3 1 1 3 1 1 1 1 3 1 3 2 2 "
},
{
"input": "427243888 734827659 763592639",
"output": "39\n1 1 1 1 1 3 1 1 1 3 1 1 3 1 1 3 1 1 1 1 3 1 1 1 1 3 1 1 1 1 1 3 2 2 2 2 3 2 2 "
},
{
"input": "110414818 315800918 504362743",
"output": "34\n3 2 2 3 2 2 2 2 3 2 2 2 3 2 2 2 3 2 3 2 2 3 2 2 2 3 2 2 2 3 2 2 3 2 "
},
{
"input": "47800186 311336510 848028367",
"output": "40\n2 3 2 2 3 2 3 2 2 2 3 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 3 2 2 2 2 3 2 "
},
{
"input": "89628711 211985129 238682387",
"output": "33\n2 2 2 2 3 1 1 1 3 1 1 3 1 3 1 3 1 1 1 1 3 2 2 2 2 2 3 2 2 3 2 2 2 "
},
{
"input": "324052946 52244647 397734559",
"output": "38\n2 2 2 3 1 1 1 1 1 1 1 1 3 1 1 1 3 1 1 3 1 1 3 1 1 1 1 1 3 1 3 1 3 1 1 1 3 2 "
},
{
"input": "90258372 208226046 267900751",
"output": "29\n1 3 2 2 2 3 2 2 2 3 2 2 2 3 2 2 3 2 3 2 2 2 3 2 2 2 3 1 1 "
},
{
"input": "274749520 37749369 274860181",
"output": "33\n3 1 3 1 1 1 1 1 3 1 1 3 1 1 1 1 1 1 1 3 1 1 1 1 3 1 1 1 3 1 1 1 3 "
},
{
"input": "140599001 3475675 314344439",
"output": "38\n3 1 1 1 3 1 1 1 3 1 1 3 1 1 1 1 3 1 1 1 1 1 1 3 2 2 2 2 3 1 1 1 1 1 1 3 1 1 "
},
{
"input": "102441042 8030255 660523531",
"output": "38\n2 2 2 2 3 1 1 1 3 2 2 3 2 2 2 2 2 3 1 1 1 1 3 1 3 1 1 1 1 3 2 2 2 2 3 2 2 2 "
},
{
"input": "361238064 66180671 686097001",
"output": "40\n2 2 2 2 3 2 2 3 2 2 3 2 2 3 2 2 3 2 2 3 2 2 3 2 2 2 2 3 1 1 1 3 2 2 2 3 2 2 2 2 "
},
{
"input": "300613783 426325786 884772299",
"output": "35\n1 1 3 1 1 1 1 1 1 3 2 2 2 2 3 2 2 2 3 2 2 3 2 2 2 2 3 2 2 2 3 2 2 3 2 "
},
{
"input": "157139253 68066325 462965543",
"output": "37\n2 3 1 1 1 1 1 3 1 1 1 1 3 1 1 3 1 3 1 1 1 1 1 1 1 3 1 1 1 3 1 1 3 2 2 2 2 "
},
{
"input": "179869515 581204545 608714231",
"output": "33\n2 3 2 2 3 2 2 3 2 3 2 3 2 2 2 2 2 2 2 2 2 2 3 2 2 2 3 1 1 1 1 3 2 "
},
{
"input": "163240633 271698627 281128777",
"output": "30\n2 3 2 2 2 2 3 1 1 1 1 3 2 2 3 2 2 2 2 2 3 1 1 1 3 1 1 3 1 1 "
},
{
"input": "334372395 10413386 568008893",
"output": "38\n2 2 3 1 1 1 1 1 1 3 1 1 1 1 1 3 1 3 1 1 3 1 1 1 3 2 2 2 2 2 3 1 1 1 1 1 1 3 "
},
{
"input": "201253549 34757959 448569467",
"output": "38\n1 1 1 1 1 1 1 3 2 2 2 3 1 1 3 1 1 3 1 1 1 1 1 1 1 1 3 2 2 2 3 2 2 2 2 2 2 3 "
},
{
"input": "478378911 392272095 691602089",
"output": "39\n2 2 3 2 2 3 2 2 2 2 3 2 2 2 3 2 2 2 3 2 2 2 2 2 3 2 2 3 2 2 2 2 2 2 2 3 2 3 2 "
},
{
"input": "312370973 255291910 367308481",
"output": "34\n2 2 2 2 2 2 3 2 2 2 2 3 2 2 3 2 2 2 2 2 3 2 2 2 2 2 2 2 3 2 2 2 3 2 "
},
{
"input": "119327588 76280465 222560231",
"output": "37\n1 3 1 1 1 3 2 2 2 2 2 3 1 1 1 3 1 3 1 1 1 1 1 3 2 2 2 2 2 2 2 3 2 2 2 2 2 "
},
{
"input": "268105737 398845435 406746107",
"output": "34\n2 2 3 1 1 1 1 1 1 1 1 3 1 1 3 1 1 1 1 3 1 1 1 3 2 2 2 2 2 2 3 2 2 2 "
},
{
"input": "251246017 203500261 276837523",
"output": "37\n1 1 1 3 1 1 3 1 1 1 3 1 1 1 3 1 1 3 1 3 1 1 1 1 3 1 1 1 1 1 1 1 1 1 3 1 1 "
},
{
"input": "248186184 62562140 574712843",
"output": "37\n3 2 2 3 2 3 2 2 2 2 2 2 2 2 3 2 2 3 2 2 2 2 2 2 3 2 2 3 1 1 1 1 3 1 1 1 1 "
},
{
"input": "55075837 65260966 912320797",
"output": "36\n3 1 1 1 3 1 1 1 3 2 2 2 2 2 2 3 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 "
},
{
"input": "117285415 2813193 270213023",
"output": "38\n2 2 3 2 2 2 2 2 2 2 2 3 2 2 2 2 3 2 2 2 2 3 2 3 2 2 3 1 1 1 1 3 1 1 1 1 3 2 "
},
{
"input": "489741679 577453298 629218159",
"output": "37\n2 2 2 2 2 3 2 2 3 2 2 2 2 2 2 2 3 2 2 2 3 2 2 3 2 2 3 2 2 2 2 2 3 1 1 1 1 "
},
{
"input": "156917952 497131002 982667113",
"output": "37\n2 2 2 3 2 2 2 3 2 2 2 2 3 2 2 3 2 3 2 2 3 2 2 3 2 2 2 2 2 3 1 1 1 1 3 2 2 "
},
{
"input": "273523056 581182602 712638193",
"output": "36\n1 1 1 1 1 1 1 1 1 3 1 1 1 3 1 1 1 1 3 2 2 2 2 2 2 3 1 1 1 1 3 2 2 2 3 1 "
},
{
"input": "123237337 347857796 396981853",
"output": "38\n1 1 3 2 2 2 3 1 1 1 3 1 1 3 1 1 3 1 1 1 1 1 3 1 1 1 1 3 2 2 2 3 2 2 3 2 2 2 "
},
{
"input": "557686882 132797512 603770891",
"output": "33\n3 1 1 1 3 1 1 3 1 1 3 1 3 1 3 1 1 1 1 1 1 3 2 2 2 3 2 2 2 3 2 2 2 "
},
{
"input": "266411371 211453240 421687867",
"output": "38\n2 2 2 2 3 2 2 2 2 3 1 1 1 3 2 2 2 2 2 2 3 2 2 2 3 2 2 2 2 2 2 2 2 2 3 2 2 2 "
},
{
"input": "800654973 31732155 830848583",
"output": "35\n2 3 2 2 3 2 2 2 3 1 1 1 1 1 3 1 1 3 1 1 1 1 1 1 1 3 1 1 3 2 2 2 2 2 2 "
},
{
"input": "100811825 79841905 360658729",
"output": "35\n3 1 1 1 1 3 2 2 3 2 2 2 3 1 1 1 1 1 1 1 3 1 1 1 1 1 1 3 1 1 1 1 3 2 2 "
},
{
"input": "47644843 58085820 64535483",
"output": "34\n1 1 3 1 3 1 1 1 1 3 1 1 3 1 1 1 1 1 3 1 1 1 3 1 1 3 1 1 3 1 1 3 1 1 "
},
{
"input": "95462857 237994474 558391573",
"output": "35\n1 1 1 3 1 1 1 1 1 1 1 1 1 3 2 2 3 2 2 2 2 3 2 2 2 2 2 2 3 2 2 3 2 2 2 "
},
{
"input": "111740623 97070774 133184621",
"output": "35\n1 1 1 1 1 1 1 3 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 3 2 2 2 3 2 2 3 1 1 1 1 "
},
{
"input": "137536396 129086364 619332541",
"output": "38\n1 3 1 1 1 3 2 2 2 3 2 2 2 2 2 3 2 2 2 2 2 2 2 3 2 2 3 2 2 2 2 2 2 2 3 2 2 2 "
},
{
"input": "560825047 346423525 741883789",
"output": "40\n3 2 2 2 3 2 2 2 2 2 2 3 2 2 2 3 2 2 3 2 2 3 2 2 2 3 2 3 2 2 2 2 3 1 1 1 1 1 3 1 "
},
{
"input": "58690741 50970663 311828449",
"output": "35\n1 1 1 3 1 1 3 1 1 1 3 1 1 1 3 1 1 1 1 1 3 2 2 2 2 3 2 2 3 2 2 2 2 3 2 "
},
{
"input": "76425961 303462631 479246993",
"output": "37\n2 2 2 2 3 2 2 2 3 2 3 2 2 2 2 3 2 2 2 2 2 2 3 2 2 3 1 1 1 1 3 1 1 1 1 3 2 "
},
{
"input": "461437124 71734749 496142431",
"output": "34\n1 3 2 2 2 3 2 2 2 3 2 2 2 2 2 2 3 2 2 2 2 3 2 2 3 2 3 2 2 3 1 1 1 1 "
},
{
"input": "90816136 111726321 545932241",
"output": "35\n3 1 1 3 1 1 1 1 1 1 3 1 1 1 1 1 3 2 2 2 2 3 1 1 1 3 1 1 1 1 1 3 1 1 3 "
},
{
"input": "365392090 584639075 704415773",
"output": "38\n3 2 2 2 2 3 1 1 1 1 1 3 1 1 1 3 2 2 2 2 3 2 3 2 3 2 2 2 2 2 2 2 2 2 3 1 1 1 "
},
{
"input": "175715557 96286033 198759131",
"output": "33\n2 2 2 2 2 3 2 2 2 2 2 2 3 1 1 1 3 1 1 3 1 1 1 3 1 3 1 1 3 2 2 2 3 "
},
{
"input": "320754651 275148330 397009763",
"output": "34\n2 2 2 2 2 3 2 2 2 2 2 2 3 2 2 2 3 2 2 2 3 1 1 1 1 1 1 3 2 2 2 3 1 1 "
},
{
"input": "162741725 52441861 559946423",
"output": "40\n1 1 1 3 1 1 1 1 1 1 1 3 2 2 2 2 3 2 2 3 2 2 3 1 1 1 1 1 1 1 1 1 3 1 1 1 1 3 1 1 "
},
{
"input": "24576706 314324526 631193177",
"output": "35\n2 3 2 2 2 2 2 2 2 2 2 3 2 2 3 2 2 2 3 2 3 2 2 2 3 1 1 1 1 1 3 2 2 2 2 "
},
{
"input": "283580041 340208115 545825537",
"output": "36\n2 3 1 1 3 1 1 1 1 3 2 2 2 2 2 3 1 1 1 1 1 1 1 3 2 2 2 2 3 2 2 2 2 3 2 2 "
},
{
"input": "46432238 87400941 648431027",
"output": "31\n3 1 1 3 1 1 3 1 1 3 1 1 1 1 1 1 1 1 3 1 1 3 1 1 1 3 1 1 3 1 3 "
},
{
"input": "54925608 329341045 814253591",
"output": "38\n1 1 1 1 3 2 2 2 2 2 2 2 2 2 2 2 3 2 2 3 2 2 2 3 1 1 1 1 3 1 1 1 1 1 1 3 2 2 "
},
{
"input": "83662801 75072692 88864289",
"output": "33\n3 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 3 2 2 2 3 2 2 2 2 3 2 2 2 2 2 "
},
{
"input": "32129725 101329630 130404041",
"output": "30\n3 2 2 2 2 2 3 2 2 2 3 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 3 1 1 "
},
{
"input": "482227319 886688494 938331311",
"output": "40\n3 1 1 3 1 1 3 1 1 3 1 1 3 1 1 1 1 1 1 1 3 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 3 1 1 1 "
},
{
"input": "922400524 736491346 997678651",
"output": "33\n1 3 1 1 1 1 1 1 1 1 3 1 1 1 3 2 2 3 2 2 3 1 1 1 3 2 2 2 2 2 2 3 2 "
},
{
"input": "3060470 29160347 168864979",
"output": "33\n2 2 3 1 1 3 1 3 1 1 1 3 1 1 1 3 1 1 3 1 1 1 1 3 1 1 3 2 2 2 2 3 2 "
},
{
"input": "115492562 118191161 149190799",
"output": "30\n3 2 2 2 3 2 2 2 2 2 3 1 1 3 1 1 1 3 1 3 1 1 3 1 1 3 2 2 2 3 "
},
{
"input": "492744485 101158602 585094469",
"output": "38\n1 3 2 2 2 2 2 3 1 1 1 1 1 1 1 3 1 3 1 1 3 1 1 3 1 1 1 1 3 1 1 3 1 1 3 1 1 1 "
},
{
"input": "34906186 20630646 44463871",
"output": "34\n2 2 2 2 3 1 1 3 1 1 1 1 3 1 1 3 1 1 1 1 1 1 1 3 1 1 3 2 2 2 2 3 1 1 "
},
{
"input": "120120134 546063296 729000143",
"output": "29\n3 1 1 1 1 1 3 2 2 2 3 2 3 2 2 2 3 2 2 3 2 2 3 1 1 1 1 3 1 "
},
{
"input": "365578375 241897078 572456809",
"output": "38\n1 1 1 1 3 2 2 2 3 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 3 2 2 2 2 2 2 "
},
{
"input": "184195322 66345451 846812903",
"output": "34\n1 1 1 3 2 2 2 3 1 1 3 1 1 3 1 1 1 1 1 1 1 3 2 2 2 3 2 2 3 2 2 2 2 3 "
},
{
"input": "118906016 155304502 168260539",
"output": "34\n2 3 2 2 3 2 2 3 2 2 3 2 2 3 2 2 2 3 1 1 1 1 3 1 3 1 1 1 3 2 2 2 3 2 "
},
{
"input": "214613230 346827239 401362417",
"output": "39\n1 3 1 1 1 1 3 2 2 3 2 2 2 2 2 3 1 1 3 1 1 1 1 3 2 2 2 2 2 2 3 2 2 3 1 1 1 3 2 "
},
{
"input": "64047668 151510504 201928589",
"output": "36\n3 2 2 3 2 2 2 2 2 3 1 1 1 3 2 2 2 3 2 2 2 3 2 2 2 3 2 2 3 2 3 2 2 2 3 1 "
},
{
"input": "98909414 48920036 132954187",
"output": "31\n1 3 1 1 1 3 1 1 1 1 1 3 2 2 2 2 3 2 2 2 3 2 3 2 2 3 2 2 2 3 2 "
},
{
"input": "299940828 138214059 360649661",
"output": "36\n1 1 1 3 1 1 1 1 1 3 1 1 1 1 3 1 1 1 3 1 1 3 1 3 1 1 1 1 1 1 3 1 1 3 2 2 "
},
{
"input": "154401390 175893348 431743469",
"output": "36\n2 2 3 1 1 3 1 3 1 1 1 1 1 1 3 1 1 1 1 1 1 3 1 1 3 1 1 1 1 3 1 1 1 1 3 2 "
},
{
"input": "338460603 70023808 560178517",
"output": "39\n2 2 2 2 2 2 3 1 1 3 1 1 3 1 1 3 1 1 3 2 2 2 3 2 2 2 2 2 3 1 1 1 1 3 2 2 2 2 2 "
},
{
"input": "261889349 72232973 456455821",
"output": "32\n3 1 1 1 1 3 2 2 2 3 2 2 3 2 2 2 2 3 2 2 2 2 2 3 1 1 1 1 1 1 3 1 "
},
{
"input": "68275961 475293451 557889209",
"output": "38\n2 2 2 3 2 2 2 2 2 3 1 1 1 3 1 1 1 1 3 1 1 1 1 1 1 3 1 1 3 1 1 3 1 1 1 1 3 1 "
},
{
"input": "712760198 765255006 933790259",
"output": "41\n2 2 2 2 2 2 2 3 2 2 2 3 2 2 2 2 2 2 3 2 2 2 2 2 2 3 2 3 2 2 3 2 2 2 2 2 3 2 2 3 2 "
},
{
"input": "485455737 472176696 617427523",
"output": "38\n1 1 1 3 2 2 2 3 2 3 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 3 1 1 1 3 2 2 2 2 2 3 1 1 "
},
{
"input": "746043441 91896626 902644241",
"output": "35\n2 2 2 2 2 2 3 2 2 2 2 2 2 3 2 2 2 2 2 2 3 2 3 2 2 3 1 1 1 3 1 1 1 3 2 "
},
{
"input": "394519738 27637489 560133487",
"output": "38\n3 2 2 3 2 2 2 2 3 2 2 2 2 2 3 1 1 1 1 3 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 3 2 "
},
{
"input": "23131925 23639747 216655567",
"output": "31\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 3 2 2 2 2 2 2 3 2 2 2 3 1 1 "
},
{
"input": "357048135 390834493 659494117",
"output": "35\n1 1 1 1 3 1 1 1 1 3 1 1 1 1 1 3 2 2 2 2 2 2 2 3 2 2 3 2 2 3 2 2 2 2 2 "
},
{
"input": "0 1000000000 1000000007",
"output": "7\n2 2 2 2 2 2 2 "
},
{
"input": "0 0 1000000007",
"output": "0"
},
{
"input": "0 1 5",
"output": "1\n1 "
}
] | 92 | 0 | 0 | 22,231 |
|
22 | Segments | [
"greedy",
"sortings"
] | D. Segments | 1 | 256 | You are given *n* segments on the Ox-axis. You can drive a nail in any integer point on the Ox-axis line nail so, that all segments containing this point, are considered nailed down. If the nail passes through endpoint of some segment, this segment is considered to be nailed too. What is the smallest number of nails needed to nail all the segments down? | The first line of the input contains single integer number *n* (1<=≤<=*n*<=≤<=1000) — amount of segments. Following *n* lines contain descriptions of the segments. Each description is a pair of integer numbers — endpoints coordinates. All the coordinates don't exceed 10000 by absolute value. Segments can degenarate to points. | The first line should contain one integer number — the smallest number of nails needed to nail all the segments down. The second line should contain coordinates of driven nails separated by space in any order. If the answer is not unique, output any. | [
"2\n0 2\n2 5\n",
"5\n0 3\n4 2\n4 8\n8 10\n7 7\n"
] | [
"1\n2 ",
"3\n7 10 3\n"
] | none | [
{
"input": "2\n0 2\n2 5",
"output": "1\n2 "
},
{
"input": "5\n0 3\n4 2\n4 8\n8 10\n7 7",
"output": "3\n3 7 10 "
},
{
"input": "3\n40 -83\n52 -80\n-21 -4",
"output": "1\n-4 "
},
{
"input": "4\n67 -88\n37 -62\n-26 91\n-99 -50",
"output": "2\n-50 91 "
},
{
"input": "5\n45 58\n22 6\n-32 36\n49 -37\n43 62",
"output": "2\n22 58 "
},
{
"input": "10\n47 -85\n71 40\n-87 64\n76 73\n52 -51\n-20 -57\n-14 -77\n-45 -11\n-56 -48\n81 20",
"output": "4\n-48 -11 71 76 "
},
{
"input": "35\n-47 61\n-73 -15\n9 43\n43 -49\n13 -6\n48 -65\n49 -84\n-6 76\n40 6\n-41 11\n-63 -42\n-7 -66\n47 -98\n-17 61\n79 99\n47 -98\n61 31\n85 72\n-50 -17\n-67 45\n14 -50\n-45 61\n-6 -96\n-47 -83\n56 -58\n-69 -15\n14 20\n-95 -82\n47 93\n-4 -7\n70 9\n91 -18\n-71 31\n55 -20\n81 -8",
"output": "6\n-82 -42 -4 20 61 85 "
},
{
"input": "1\n0 0",
"output": "1\n0 "
},
{
"input": "4\n1 1\n0 0\n10000 10000\n-10000 -10000",
"output": "4\n-10000 0 1 10000 "
},
{
"input": "3\n55 55\n55 55\n55 55",
"output": "1\n55 "
},
{
"input": "4\n55 55\n55 55\n55 55\n55 56",
"output": "1\n55 "
},
{
"input": "1\n-2244 5023",
"output": "1\n5023 "
}
] | 155 | 1,638,400 | 3.919448 | 22,239 |
367 | Sereja and Algorithm | [
"data structures",
"implementation"
] | null | null | Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as *q*<==<=*q*1*q*2... *q**k*. The algorithm consists of two steps:
1. Find any continuous subsequence (substring) of three characters of string *q*, which doesn't equal to either string "zyx", "xzy", "yxz". If *q* doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 1. Rearrange the letters of the found subsequence randomly and go to step 1.
Sereja thinks that the algorithm works correctly on string *q* if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string.
Sereja wants to test his algorithm. For that, he has string *s*<==<=*s*1*s*2... *s**n*, consisting of *n* characters. The boy conducts a series of *m* tests. As the *i*-th test, he sends substring *s**l**i**s**l**i*<=+<=1... *s**r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (*l**i*,<=*r**i*) determine if the algorithm works correctly on this test or not. | The first line contains non-empty string *s*, its length (*n*) doesn't exceed 105. It is guaranteed that string *s* only contains characters: 'x', 'y', 'z'.
The second line contains integer *m* (1<=≤<=*m*<=≤<=105) — the number of tests. Next *m* lines contain the tests. The *i*-th line contains a pair of integers *l**i*, *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*). | For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. | [
"zyxxxxxxyyz\n5\n5 5\n1 3\n1 11\n1 4\n3 6\n"
] | [
"YES\nYES\nNO\nYES\nNO\n"
] | In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. | [
{
"input": "zyxxxxxxyyz\n5\n5 5\n1 3\n1 11\n1 4\n3 6",
"output": "YES\nYES\nNO\nYES\nNO"
},
{
"input": "yxzyzxzzxyyzzxxxzyyzzyzxxzxyzyyzxyzxyxxyzxyxzyzxyzxyyxzzzyzxyyxyzxxy\n10\n17 67\n6 35\n12 45\n56 56\n14 30\n25 54\n1 1\n46 54\n3 33\n19 40",
"output": "NO\nNO\nNO\nYES\nYES\nNO\nYES\nNO\nNO\nYES"
},
{
"input": "xxxxyyxyyzzyxyxzxyzyxzyyyzyzzxxxxzyyzzzzyxxxxzzyzzyzx\n5\n4 4\n3 3\n1 24\n3 28\n18 39",
"output": "YES\nYES\nNO\nNO\nNO"
},
{
"input": "yzxyzxyzxzxzyzxyzyzzzyxzyz\n9\n4 6\n2 7\n3 5\n14 24\n3 13\n2 24\n2 5\n2 14\n3 15",
"output": "YES\nYES\nYES\nNO\nYES\nNO\nYES\nNO\nNO"
},
{
"input": "zxyzxyzyyzxzzxyzxyzx\n15\n7 10\n17 17\n6 7\n8 14\n4 7\n11 18\n12 13\n1 1\n3 8\n1 1\n9 17\n4 4\n5 11\n3 15\n1 1",
"output": "NO\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nNO\nYES\nNO\nYES\nYES\nNO\nYES"
},
{
"input": "x\n1\n1 1",
"output": "YES"
}
] | 93 | 0 | -1 | 22,291 |
|
958 | Encryption (easy) | [
"brute force"
] | null | null | Rebel spy Heidi has just obtained the plans for the Death Star from the Empire and, now on her way to safety, she is trying to break the encryption of the plans (of course they are encrypted – the Empire may be evil, but it is not stupid!). The encryption has several levels of security, and here is how the first one looks.
Heidi is presented with a screen that shows her a sequence of integers *A* and a positive integer *p*. She knows that the encryption code is a single number *S*, which is defined as follows:
Define the score of *X* to be the sum of the elements of *X* modulo *p*.
Heidi is given a sequence *A* that consists of *N* integers, and also given an integer *p*. She needs to split *A* into 2 parts such that:
- Each part contains at least 1 element of *A*, and each part consists of contiguous elements of *A*. - The two parts do not overlap. - The total sum *S* of the scores of those two parts is maximized. This is the encryption code.
Output the sum *S*, which is the encryption code. | The first line of the input contains two space-separated integer *N* and *p* (2<=≤<=*N*<=≤<=100<=000, 2<=≤<=*p*<=≤<=10<=000) – the number of elements in *A*, and the modulo for computing scores, respectively.
The second line contains *N* space-separated integers which are the elements of *A*. Each integer is from the interval [1,<=1<=000<=000]. | Output the number *S* as described in the problem statement. | [
"4 10\n3 4 7 2\n",
"10 12\n16 3 24 13 9 8 7 5 12 12\n"
] | [
"16\n",
"13\n"
] | In the first example, the score is maximized if the input sequence is split into two parts as (3, 4), (7, 2). It gives the total score of <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fc1f9aecea625f3be9a56917d3ba448d669ff278.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second example, the score is maximized if the first part consists of the first three elements, and the second part consists of the rest. Then, the score is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ffd0a0f4c65b0578601ae2fe11a80aec5192f53d.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "4 10\n3 4 7 2",
"output": "16"
},
{
"input": "10 12\n16 3 24 13 9 8 7 5 12 12",
"output": "13"
},
{
"input": "2 2\n9 9",
"output": "2"
},
{
"input": "2 2\n8 8",
"output": "0"
},
{
"input": "5 50\n1 1 1 1 1",
"output": "5"
},
{
"input": "5 50\n100 150 200 100 50",
"output": "0"
}
] | 46 | 3,379,200 | -1 | 22,300 |
|
960 | Alternating Tree | [
"combinatorics",
"dfs and similar",
"divide and conquer",
"dp",
"probabilities",
"trees"
] | null | null | Given a tree with $n$ nodes numbered from $1$ to $n$. Each node $i$ has an associated value $V_i$.
If the simple path from $u_1$ to $u_m$ consists of $m$ nodes namely $u_1 \rightarrow u_2 \rightarrow u_3 \rightarrow \dots u_{m-1} \rightarrow u_{m}$, then its alternating function $A(u_{1},u_{m})$ is defined as $A(u_{1},u_{m}) = \sum\limits_{i=1}^{m} (-1)^{i+1} \cdot V_{u_{i}}$. A path can also have $0$ edges, i.e. $u_{1}=u_{m}$.
Compute the sum of alternating functions of all unique simple paths. Note that the paths are directed: two paths are considered different if the starting vertices differ or the ending vertices differ. The answer may be large so compute it modulo $10^{9}+7$. | The first line contains an integer $n$ $(2 \leq n \leq 2\cdot10^{5} )$ — the number of vertices in the tree.
The second line contains $n$ space-separated integers $V_1, V_2, \ldots, V_n$ ($-10^9\leq V_i \leq 10^9$) — values of the nodes.
The next $n-1$ lines each contain two space-separated integers $u$ and $v$ $(1\leq u, v\leq<=n, u \neq v)$ denoting an edge between vertices $u$ and $v$. It is guaranteed that the given graph is a tree. | Print the total sum of alternating functions of all unique simple paths modulo $10^{9}+7$. | [
"4\n-4 1 5 -2\n1 2\n1 3\n1 4\n",
"8\n-2 6 -4 -4 -9 -3 -7 23\n8 2\n2 3\n1 4\n6 5\n7 6\n4 7\n5 8\n"
] | [
"40\n",
"4\n"
] | Consider the first example.
A simple path from node $1$ to node $2$: $1 \rightarrow 2$ has alternating function equal to $A(1,2) = 1 \cdot (-4)+(-1) \cdot 1 = -5$.
A simple path from node $1$ to node $3$: $1 \rightarrow 3$ has alternating function equal to $A(1,3) = 1 \cdot (-4)+(-1) \cdot 5 = -9$.
A simple path from node $2$ to node $4$: $2 \rightarrow 1 \rightarrow 4$ has alternating function $A(2,4) = 1 \cdot (1)+(-1) \cdot (-4)+1 \cdot (-2) = 3$.
A simple path from node $1$ to node $1$ has a single node $1$, so $A(1,1) = 1 \cdot (-4) = -4$.
Similarly, $A(2, 1) = 5$, $A(3, 1) = 9$, $A(4, 2) = 3$, $A(1, 4) = -2$, $A(4, 1) = 2$, $A(2, 2) = 1$, $A(3, 3) = 5$, $A(4, 4) = -2$, $A(3, 4) = 7$, $A(4, 3) = 7$, $A(2, 3) = 10$, $A(3, 2) = 10$. So the answer is $(-5) + (-9) + 3 + (-4) + 5 + 9 + 3 + (-2) + 2 + 1 + 5 + (-2) + 7 + 7 + 10 + 10 = 40$.
Similarly $A(1,4)=-2, A(2,2)=1, A(2,1)=5, A(2,3)=10, A(3,3)=5, A(3,1)=9, A(3,2)=10, A(3,4)=7, A(4,4)=-2, A(4,1)=2, A(4,2)=3 , A(4,3)=7$ which sums upto 40. | [
{
"input": "4\n-4 1 5 -2\n1 2\n1 3\n1 4",
"output": "40"
},
{
"input": "8\n-2 6 -4 -4 -9 -3 -7 23\n8 2\n2 3\n1 4\n6 5\n7 6\n4 7\n5 8",
"output": "4"
},
{
"input": "15\n-4 6 1 6 -9 -8 -10 9 -2 -9 7 2 -3 1 13\n11 2\n10 3\n15 4\n7 5\n2 6\n9 7\n6 8\n12 9\n4 10\n1 11\n3 12\n8 13\n5 14\n13 15",
"output": "128"
},
{
"input": "20\n15 -37 -17 -23 -27 8 -12 35 23 -29 -15 -4 -21 34 -27 -16 -13 -44 -20 190\n7 2\n5 3\n19 4\n14 5\n16 6\n11 7\n2 8\n8 9\n6 10\n4 11\n18 12\n17 13\n9 14\n12 15\n1 16\n15 17\n20 18\n13 19\n10 20",
"output": "2318"
},
{
"input": "30\n45 50 -45 -28 21 -38 11 -22 -19 -19 -17 -2 8 13 31 -45 -3 -39 -36 43 47 33 -10 -41 -8 37 30 -2 0 5\n18 2\n14 3\n21 4\n22 5\n30 6\n22 7\n13 8\n29 9\n27 10\n20 11\n14 12\n15 13\n26 14\n5 15\n30 16\n1 17\n19 18\n5 19\n4 20\n25 21\n6 22\n30 23\n10 24\n17 25\n22 26\n20 27\n7 28\n24 29\n10 30",
"output": "999997285"
},
{
"input": "50\n25 23 0 17 47 -40 -35 -13 20 46 -16 20 5 4 43 -17 42 -11 30 -34 25 -37 17 48 -8 -49 50 47 20 45 -15 35 22 41 8 -40 -37 -25 -42 -9 48 48 33 -15 22 38 -37 14 25 -428\n43 2\n42 3\n34 4\n15 5\n28 6\n20 7\n29 8\n4 9\n40 10\n23 11\n3 12\n9 13\n38 14\n32 15\n26 16\n35 17\n48 18\n47 19\n44 20\n46 21\n1 22\n50 23\n2 24\n6 25\n39 26\n49 27\n14 28\n5 29\n19 30\n17 31\n27 32\n8 33\n12 34\n7 35\n41 36\n45 37\n13 38\n22 39\n30 40\n16 41\n37 42\n18 43\n24 44\n31 45\n36 46\n21 47\n11 48\n10 49\n33 50",
"output": "999997857"
},
{
"input": "50\n-71 19 -69 -27 21 -6 69 9 -2 10 -66 -4 11 59 2 -63 28 74 -67 48 75 -27 24 65 -84 52 36 -56 21 62 83 32 4 -24 -9 43 -53 41 -42 -10 -35 -51 -24 77 -55 -57 57 41 -88 -73\n1 2\n44 3\n42 4\n26 5\n38 6\n17 7\n19 8\n32 9\n6 10\n27 11\n40 12\n33 13\n31 14\n4 15\n45 16\n32 17\n16 18\n35 19\n21 20\n47 21\n48 22\n43 23\n22 24\n31 25\n34 26\n30 27\n20 28\n13 29\n23 30\n3 31\n10 32\n28 33\n15 34\n50 35\n2 36\n18 37\n14 38\n8 39\n49 40\n9 41\n37 42\n36 43\n29 44\n7 45\n37 46\n12 47\n5 48\n39 49\n11 50",
"output": "1342"
},
{
"input": "2\n461663513 -693562127\n1 2",
"output": "768101393"
},
{
"input": "2\n-939812330 -324524739\n1 2",
"output": "735662945"
},
{
"input": "2\n-289410670 -7364852\n1 2",
"output": "703224485"
}
] | 421 | 23,961,600 | 0 | 22,319 |
|
115 | Lawnmower | [
"greedy",
"sortings"
] | null | null | You have a garden consisting entirely of grass and weeds. Your garden is described by an *n*<=×<=*m* grid, with rows numbered 1 to *n* from top to bottom, and columns 1 to *m* from left to right. Each cell is identified by a pair (*r*,<=*c*) which means that the cell is located at row *r* and column *c*. Each cell may contain either grass or weeds. For example, a 4<=×<=5 garden may look as follows (empty cells denote grass):
You have a land-mower with you to mow all the weeds. Initially, you are standing with your lawnmower at the top-left corner of the garden. That is, at cell (1,<=1). At any moment of time you are facing a certain direction — either left or right. And initially, you face right.
In one move you can do either one of these:
1) Move one cell in the direction that you are facing.
- if you are facing right: move from cell (*r*,<=*c*) to cell (*r*,<=*c*<=+<=1) - if you are facing left: move from cell (*r*,<=*c*) to cell (*r*,<=*c*<=-<=1) - if you were facing right previously, you will face left - if you were facing left previously, you will face right
You are not allowed to leave the garden. Weeds will be mowed if you and your lawnmower are standing at the cell containing the weeds (your direction doesn't matter). This action isn't counted as a move.
What is the minimum number of moves required to mow all the weeds? | The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=150) — the number of rows and columns respectively. Then follow *n* lines containing *m* characters each — the content of the grid. "G" means that this cell contains grass. "W" means that this cell contains weeds.
It is guaranteed that the top-left corner of the grid will contain grass. | Print a single number — the minimum number of moves required to mow all the weeds. | [
"4 5\nGWGGW\nGGWGG\nGWGGG\nWGGGG\n",
"3 3\nGWW\nWWW\nWWG\n",
"1 1\nG\n"
] | [
"11\n",
"7\n",
"0\n"
] | For the first example, this is the picture of the initial state of the grid:
A possible solution is by mowing the weeds as illustrated below: | [
{
"input": "4 5\nGWGGW\nGGWGG\nGWGGG\nWGGGG",
"output": "11"
},
{
"input": "3 3\nGWW\nWWW\nWWG",
"output": "7"
},
{
"input": "1 1\nG",
"output": "0"
},
{
"input": "4 3\nGWW\nWWW\nWWW\nWWG",
"output": "11"
},
{
"input": "6 5\nGWWWW\nWWWWW\nWWWWW\nWWWWW\nWWWWW\nWWWWW",
"output": "29"
},
{
"input": "3 5\nGGWWW\nWWWWW\nWWWGG",
"output": "12"
},
{
"input": "20 1\nG\nG\nW\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nW\nG\nW\nG\nG",
"output": "17"
},
{
"input": "2 2\nGG\nGW",
"output": "2"
},
{
"input": "1 20\nGGGGWGGGGWWWWGGGWGGG",
"output": "16"
},
{
"input": "3 112\nGGWGGWWGGGWWGWWGWGGGGGGWGGGWGGGGGGGWGGGGWGGGGGGGGGWWGGWWWGWGGWGWGWGGGGWWGGWGWWWGGWWWGGGGWGWGGWGGGWGGGGGGGWWWGGWG\nWWWGGGGWGWGWGGWGGGGWGWGGGWGWGGGWWWGGGGGWGWWGGWGGGGGGGWGGGGGGGGGWGGGGWGGGGGGGGGGGWWGWGGGWGGGWGGWWGWGWGGGGGWGGGGGG\nWWWGGWGGWWGGGWWGGGGGWGGGWWGWGWWGWGGWWWWGGGGGGWGGGGGWGGWGGGGWGGWGGGWGGGWGGWGWGGGGGGGWGGWGGWGWGWWWGGWWGGGWGGWGWWWW",
"output": "333"
},
{
"input": "3 150\nGGGWGGGGWWGGGGGGGGGGGGGWGGWGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGWGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGWGGGGGGGWGGGGGGGGGGGGGGGGGWGGGGGGGGGGGGGGGGGGW\nGGGGGGGGGGGGWGGGGGGGGGWGGGGGGGGGGGGWGGGGGWGGGGGGGWGGGGGGGWGGGGGWGGGGGGGGGGGGGGGGGGGGGGGGWGGGGWGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGWGGWGGG\nGGGGGGGWGGWWGWGGWGGGGWGWGGGGGGGWGGGGGGGGGGGGGGGGGGGGGGGGWGGGGGGWGGGWGGGGGGGGGGGGGGGGWGGGGGGGGGGWGWGGGGGGGGGGGGGGGGGGGGGGGGGWGGGGGGGGGGGGGGGGGGGGGGGGGW",
"output": "435"
},
{
"input": "3 150\nGWWWGWGWWWWGGWWWGWGWWGWWWWWGGWGGWWWWWWWWWWWWWGGWGWWWWWGWGWGWWWWWWGWWWWGWGWWGWGWWWWWWWGWWWGGWWGWWWGWWGGWWGGGWGWWWWWWWGWGWWWGWWGWWWWWGWGWWWGGWGGGGWGWWWW\nWWWGGWWGWWWGGGWWWGWWWWWWWGGWGGWWGWWWWWWWWWGWGWWWWGGWWWWGGGGWWWWGWGGGWWGGWWWGWWGWWWWWGGWGWGGWGWWWGGWWWGWWGWGWGWWGWGGWGWWWGGGGWWGGGGWWWWGWWGGWGWWWWWGWWW\nWWGWWGWWWWGWGGGWWWGWWWGGWWWWWWGGWWGWWWWWWGWGWWWGGWWWWWWWGGWWWGGGWWWGWWWWWGWWWGGWWWWWGWWWGWGGWGWWGWGWWWGWGWWWGWWWWWWWGGWGWWWWWWWWGWWWWWWGGGWWWWWWGGWGGW",
"output": "449"
},
{
"input": "1 150\nGGWGGGGGGGGGGGGGGGGGGGWGGGGGGWGGGGGGWGGGGGGGGGGGGGGGGGGGGGGGGGGGGWGGGWGGGGGGGGGGGGGGGGGGGGWGGGGGGGGGGGGGGGGGGGGGGGWGGGGGGGGWGGGGGGGGGWWGGGGGWGGGGGGGGG",
"output": "140"
},
{
"input": "150 1\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nW\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nW\nG\nG\nG\nW\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nW\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nG\nW\nG\nG\nG\nG\nG\nG\nG\nG\nG\nW\nG\nG\nG\nG",
"output": "145"
},
{
"input": "1 150\nGGGWGGGWWWWWWWGWWWGGWWWWWGGWWGGWWWWWWWWGWWGWWWWWWGWGWGWWWWWGWGWWGWWWWGWWGWGWWWWWWWWGGGGWWWWWGGGWWWWGGGWWWWGGWWWWGWWWWGGGWWWWWWWGWGGWGWWWGWGGWWGWGWWWGW",
"output": "149"
},
{
"input": "2 124\nGGGGWGGGWGGGGGWWWGWWWGWGWGGGWGGWWGGGGWGGGWGGGGGWGGGGWWGGGGWGWGWWWGGGGGWGGGGGGGWGWGGGGWGGWGGGGWGGWWGWGGWWGGWWGGGGWWGGGGGGGWGG\nGGGGGGGGWGGGWWWGWGGGGGGGWWGGGGWWGGGWGWGGWGGWGGGWGGWGGGGGWWGGWGGGGGWWGWWGWGGWWWGWWWWGGGGWGGWGGGWGGGWWWWWGGGGGWGGGGGGGWGGGWWGW",
"output": "239"
},
{
"input": "1 1\nG",
"output": "0"
},
{
"input": "1 1\nG",
"output": "0"
},
{
"input": "1 150\nGGGGWWGGWWWGGWGWGGWWGGWGGGGGWWWGWWGWWGWWWGWGGWGWGWWGWGGWWGWGGWWWGGWGGGWGWGWGGGWGWWGGWGWGWWWWGGWWGWWWGWGWGGGWGWGGWWGWGGGWWGGGWWWWWWWWWWWGGGGWGWWGWGWGGG",
"output": "146"
},
{
"input": "124 2\nGG\nGG\nGG\nGW\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nGW\nGG\nWW\nGG\nGG\nWG\nGG\nWW\nGG\nGG\nGW\nGG\nGG\nGG\nGG\nGG\nGW\nWG\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nWG\nGG\nGG\nWG\nWW\nWG\nGW\nGG\nGW\nGG\nWG\nGG\nWG\nGG\nGW\nGG\nGW\nGG\nWW\nGG\nGG\nGG\nGG\nGG\nGW\nGG\nGG\nGG\nWG\nGG\nWG\nGG\nGG\nGG\nGG\nGW\nGG\nGG\nGG\nWG\nWW\nWG\nWG\nGG\nGG\nWW\nGG\nGG\nGG\nGW\nGW\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nWG\nGG\nGG\nGG\nGG\nGG\nGG\nGG\nGW\nWG\nWG\nGG\nGG\nGG\nGG\nGW",
"output": "144"
},
{
"input": "150 1\nG\nW\nG\nW\nG\nG\nG\nG\nW\nG\nW\nG\nG\nW\nG\nG\nW\nG\nW\nG\nW\nG\nW\nG\nW\nW\nW\nW\nG\nG\nW\nW\nG\nG\nG\nG\nG\nG\nG\nG\nW\nW\nW\nW\nG\nW\nG\nW\nG\nG\nW\nW\nG\nG\nG\nG\nG\nW\nG\nW\nG\nG\nG\nG\nG\nG\nG\nW\nW\nW\nG\nG\nG\nG\nG\nW\nG\nW\nW\nG\nW\nW\nW\nW\nW\nW\nG\nW\nG\nW\nG\nW\nW\nG\nW\nW\nG\nG\nW\nG\nG\nG\nW\nW\nW\nW\nW\nG\nG\nG\nW\nW\nG\nG\nG\nW\nG\nW\nW\nG\nG\nG\nW\nW\nW\nW\nG\nW\nG\nW\nW\nW\nG\nG\nW\nG\nW\nW\nG\nW\nW\nW\nG\nW\nW\nW\nW\nW\nW\nW",
"output": "149"
},
{
"input": "2 150\nGGGGGGGGWWGGGGGGGWGGGGGWGGGGGGGGWGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGWWGGGGGGGGGGGGGWGGGGGGGGGGGGGGGGGGWGGGGGGGGGGGGGGGWGGGGGGGGGGGGGGW\nGGGGGGGGGGGGGGGGGGGGGGWGGGWGGGGGGGGGGGGGGGGGGGGGGWGWGGGGGGGGGGGGWGGGGGGGGGGGGGGWGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGWGGWGGGGGWGGGGWGGWGGGGGGWGGWGGGGWGGGGGGG",
"output": "277"
},
{
"input": "2 150\nGWWWWGWGWGWGWGWWWWWWWWWWGWWWGGWWWGGWWWWGWWGGGGGWWWGWWWGWWWWWWWWWWWWWWGWGWWWWWWWWGWWGWWGWWGWWGWWWWWWGGGWWWWWWWWGWWGWGWGWGWWWGGWWWWWGGGWWWGWWGGWGWWGGWWW\nWGGGGWWWWWWGWWGWGGWGWGWWWWWGWWWGWWWWWWGGWGWWWWGGWWWWWWGGGWGGWGWWGWGWWGWWWWWWGGWGGGWWWGWWWGWWWWWGGGWWWWGGGWWGGGGWWWWWGWWWWGGWWWWWWWGGGGWWWWGWWGGWWGWWWG",
"output": "299"
},
{
"input": "3 3\nGWG\nGGG\nGGW",
"output": "4"
},
{
"input": "3 3\nGGG\nGGG\nGGG",
"output": "0"
},
{
"input": "2 4\nGWWG\nGGWW",
"output": "5"
},
{
"input": "5 2\nGG\nGG\nWW\nGW\nWG",
"output": "6"
},
{
"input": "2 5\nGWGGG\nGWGGW",
"output": "8"
},
{
"input": "3 3\nGGG\nGGG\nGGW",
"output": "4"
},
{
"input": "6 3\nGGW\nGGG\nGGG\nGGG\nGGG\nWGG",
"output": "9"
},
{
"input": "3 3\nGWG\nWGW\nGWG",
"output": "7"
},
{
"input": "6 4\nGWWW\nWWGG\nGGGG\nGGGG\nGGGW\nWWGG",
"output": "17"
},
{
"input": "2 3\nGGG\nGGG",
"output": "0"
},
{
"input": "10 10\nGGGGGGGGGG\nGGGGGGGGGG\nGGGGGGGGGG\nGGGGGGGGGG\nGGGGGGGGGG\nGGGGGGGGGG\nGGGGGGGGGG\nGGGGGGGGGG\nGGGGGGGGGG\nGGGGGGGGGG",
"output": "0"
},
{
"input": "3 3\nGWW\nGWW\nWWG",
"output": "7"
},
{
"input": "5 5\nGWGGG\nGGGGG\nGGGGG\nWGGGW\nGGGGG",
"output": "11"
},
{
"input": "3 5\nGWGGW\nGGGGG\nWGGWG",
"output": "13"
}
] | 31 | 0 | 0 | 22,354 |
|
367 | Sereja ans Anagrams | [
"binary search",
"data structures"
] | null | null | Sereja has two sequences *a* and *b* and number *p*. Sequence *a* consists of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Similarly, sequence *b* consists of *m* integers *b*1,<=*b*2,<=...,<=*b**m*. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions *q* (*q*<=+<=(*m*<=-<=1)·*p*<=≤<=*n*; *q*<=≥<=1), such that sequence *b* can be obtained from sequence *a**q*,<=*a**q*<=+<=*p*,<=*a**q*<=+<=2*p*,<=...,<=*a**q*<=+<=(*m*<=-<=1)*p* by rearranging elements.
Sereja needs to rush to the gym, so he asked to find all the described positions of *q*. | The first line contains three integers *n*, *m* and *p* (1<=≤<=*n*,<=*m*<=≤<=2·105,<=1<=≤<=*p*<=≤<=2·105). 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<=≤<=*b**i*<=≤<=109). | In the first line print the number of valid *q*s. In the second line, print the valid values in the increasing order. | [
"5 3 1\n1 2 3 2 1\n1 2 3\n",
"6 3 2\n1 3 2 2 3 1\n1 2 3\n"
] | [
"2\n1 3\n",
"2\n1 2\n"
] | none | [
{
"input": "5 3 1\n1 2 3 2 1\n1 2 3",
"output": "2\n1 3"
},
{
"input": "6 3 2\n1 3 2 2 3 1\n1 2 3",
"output": "2\n1 2"
},
{
"input": "68 16 3\n5 3 4 3 3 3 2 2 2 3 2 4 2 2 2 2 4 3 5 1 1 2 2 2 3 1 5 1 2 2 1 5 1 5 3 2 3 5 2 1 1 4 2 3 4 3 4 3 3 1 3 4 1 5 2 5 3 4 4 1 4 5 5 1 1 2 2 2\n5 4 4 3 5 1 1 2 3 2 2 1 3 3 2 2",
"output": "3\n2 13 15"
},
{
"input": "44 11 4\n4 3 3 3 4 3 4 5 1 3 4 2 4 4 2 2 1 5 3 1 5 2 3 2 4 4 5 3 2 2 2 4 2 2 2 5 4 2 3 5 4 3 1 1\n4 4 1 4 4 1 2 4 2 5 4",
"output": "1\n1"
},
{
"input": "54 6 4\n5 4 1 2 2 2 1 3 3 1 5 5 2 2 2 5 4 4 1 3 4 3 4 2 1 4 2 2 4 3 3 2 5 5 3 5 2 2 1 4 2 3 5 3 5 5 5 5 1 2 5 2 4 5\n2 3 5 5 3 2",
"output": "3\n4 8 30"
},
{
"input": "75 54 1\n1 1 1 5 5 4 2 1 1 1 5 1 5 1 2 1 5 3 1 2 1 3 2 3 4 1 3 5 1 1 3 5 4 1 3 4 3 3 1 2 3 3 1 4 1 4 1 4 3 2 4 3 3 1 2 4 4 4 1 3 4 1 3 1 5 4 4 1 2 3 5 1 4 4 4\n1 3 3 4 3 4 1 4 4 3 1 3 1 4 3 3 5 3 1 4 5 4 3 2 2 4 3 1 4 1 2 3 3 3 2 5 1 3 1 4 5 1 1 1 4 2 1 2 3 1 1 1 5 1",
"output": "3\n10 11 12"
},
{
"input": "31 28 1\n1 4 1 2 5 1 1 4 2 2 5 2 4 5 5 2 4 1 5 3 5 4 1 2 4 3 1 2 5 2 1\n2 4 1 2 1 4 4 5 5 4 4 5 3 2 5 1 4 2 2 1 1 2 5 2 5 1 5 3",
"output": "1\n2"
},
{
"input": "59 2 3\n2 4 5 3 2 4 4 5 5 5 4 3 4 2 5 4 5 4 5 2 4 1 2 5 3 1 4 4 5 3 4 3 1 2 5 4 2 5 4 1 5 3 4 4 1 5 5 3 1 1 1 1 5 3 4 3 5 1 1\n5 4",
"output": "12\n3 6 7 8 10 15 16 21 24 41 43 44"
},
{
"input": "74 33 1\n4 5 5 2 1 2 2 2 2 2 3 2 3 4 2 2 1 4 4 4 5 4 1 2 4 5 4 2 4 2 5 1 2 1 5 3 5 4 1 4 1 1 2 4 4 5 1 4 2 4 2 3 3 2 5 5 4 3 5 1 3 5 5 4 4 4 2 5 4 2 2 3 4 4\n2 5 4 4 2 1 4 4 4 5 4 1 2 1 5 2 4 3 4 1 4 1 2 5 1 4 5 4 2 1 2 5 3",
"output": "1\n20"
},
{
"input": "70 7 3\n4 2 5 5 2 3 1 1 2 5 3 1 5 1 1 4 2 4 3 4 4 5 2 3 2 3 5 3 5 5 1 2 2 4 5 1 3 1 2 2 1 5 4 2 4 1 5 1 1 4 5 2 3 4 4 2 4 2 1 3 5 5 1 3 3 2 1 5 5 5\n4 1 1 3 2 3 4",
"output": "2\n6 28"
},
{
"input": "82 50 1\n4 2 2 4 4 3 2 2 3 3 4 3 4 1 4 1 4 3 5 3 4 2 4 4 5 2 5 4 2 4 2 5 3 2 3 4 2 5 4 3 3 4 3 4 1 1 5 2 4 1 3 3 2 4 1 2 2 5 4 4 5 3 3 1 2 3 4 5 3 1 2 3 3 5 5 2 4 1 4 2 3 1\n4 3 3 3 4 1 4 4 5 5 5 2 5 4 1 2 2 1 3 3 3 3 2 5 3 2 4 3 2 2 4 4 4 3 4 2 1 2 3 2 1 1 5 2 3 5 4 5 3 4",
"output": "2\n24 28"
},
{
"input": "41 6 1\n1 3 1 1 3 4 2 3 1 5 2 3 5 2 3 1 4 4 3 4 2 5 1 3 3 4 2 3 1 3 4 3 1 5 2 3 4 2 1 5 3\n3 2 3 1 4 5",
"output": "10\n5 12 19 20 21 22 30 31 32 36"
},
{
"input": "3 5 1\n1 1 1\n1 1 1 1 1",
"output": "0"
},
{
"input": "3 5 1\n1 1 1\n1 2 3 4 5",
"output": "0"
},
{
"input": "1 1 1\n1\n1",
"output": "1\n1"
},
{
"input": "1 1 2\n1\n2",
"output": "0"
},
{
"input": "2 2 1\n1 2\n2 1",
"output": "1\n1"
},
{
"input": "2 2 1\n1 2\n1 2",
"output": "1\n1"
},
{
"input": "5 10 1\n1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1",
"output": "0"
},
{
"input": "5 10 1\n1 1 1 1 2\n1 1 1 1 1 1 1 1 1 2",
"output": "0"
},
{
"input": "10 3 3\n999999991 999999991 999999991 999999992 999999992 999999993 999999993 999999993 999999993 999999991\n999999991 999999992 999999993",
"output": "3\n1 2 4"
}
] | 61 | 2,867,200 | -1 | 22,390 |
|
919 | Seat Arrangements | [
"brute force",
"implementation"
] | null | null | Suppose that you are in a campus and have to go for classes day by day. As you may see, when you hurry to a classroom, you surprisingly find that many seats there are already occupied. Today you and your friends went for class, and found out that some of the seats were occupied.
The classroom contains $n$ rows of seats and there are $m$ seats in each row. Then the classroom can be represented as an $n \times m$ matrix. The character '.' represents an empty seat, while '*' means that the seat is occupied. You need to find $k$ consecutive empty seats in the same row or column and arrange those seats for you and your friends. Your task is to find the number of ways to arrange the seats. Two ways are considered different if sets of places that students occupy differs. | The first line contains three positive integers $n,m,k$ ($1 \leq n, m, k \leq 2\,000$), where $n,m$ represent the sizes of the classroom and $k$ is the number of consecutive seats you need to find.
Each of the next $n$ lines contains $m$ characters '.' or '*'. They form a matrix representing the classroom, '.' denotes an empty seat, and '*' denotes an occupied seat. | A single number, denoting the number of ways to find $k$ empty seats in the same row or column. | [
"2 3 2\n**.\n...\n",
"1 2 2\n..\n",
"3 3 4\n.*.\n*.*\n.*.\n"
] | [
"3\n",
"1\n",
"0\n"
] | In the first sample, there are three ways to arrange those seats. You can take the following seats for your arrangement.
- $(1,3)$, $(2,3)$ - $(2,2)$, $(2,3)$ - $(2,1)$, $(2,2)$ | [
{
"input": "1 2 2\n..",
"output": "1"
},
{
"input": "3 3 4\n.*.\n*.*\n.*.",
"output": "0"
},
{
"input": "1 1 1\n.",
"output": "1"
},
{
"input": "1 1 1\n*",
"output": "0"
},
{
"input": "2 2 2\n.*\n*.",
"output": "0"
},
{
"input": "1 1 1000\n.",
"output": "0"
},
{
"input": "1 1 2000\n*",
"output": "0"
},
{
"input": "3 3 1\n.*.\n.*.\n.*.",
"output": "6"
},
{
"input": "2 2 1\n..\n..",
"output": "4"
}
] | 1,000 | 9,728,000 | 0 | 22,423 |
|
0 | none | [
"none"
] | null | null | You are given a rectangular parallelepiped with sides of positive integer lengths $A$, $B$ and $C$.
Find the number of different groups of three integers ($a$, $b$, $c$) such that $1\leq a\leq b\leq c$ and parallelepiped $A\times B\times C$ can be paved with parallelepipeds $a\times b\times c$. Note, that all small parallelepipeds have to be rotated in the same direction.
For example, parallelepiped $1\times 5\times 6$ can be divided into parallelepipeds $1\times 3\times 5$, but can not be divided into parallelepipeds $1\times 2\times 3$. | The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases.
Each of the next $t$ lines contains three integers $A$, $B$ and $C$ ($1 \leq A, B, C \leq 10^5$) — the sizes of the parallelepiped. | For each test case, print the number of different groups of three points that satisfy all given conditions. | [
"4\n1 1 1\n1 6 1\n2 2 2\n100 100 100\n"
] | [
"1\n4\n4\n165\n"
] | In the first test case, rectangular parallelepiped $(1, 1, 1)$ can be only divided into rectangular parallelepiped with sizes $(1, 1, 1)$.
In the second test case, rectangular parallelepiped $(1, 6, 1)$ can be divided into rectangular parallelepipeds with sizes $(1, 1, 1)$, $(1, 1, 2)$, $(1, 1, 3)$ and $(1, 1, 6)$.
In the third test case, rectangular parallelepiped $(2, 2, 2)$ can be divided into rectangular parallelepipeds with sizes $(1, 1, 1)$, $(1, 1, 2)$, $(1, 2, 2)$ and $(2, 2, 2)$. | [
{
"input": "4\n1 1 1\n1 6 1\n2 2 2\n100 100 100",
"output": "1\n4\n4\n165"
},
{
"input": "10\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1",
"output": "1\n1\n1\n1\n1\n1\n1\n1\n1\n1"
},
{
"input": "10\n9 6 8\n5 5 2\n8 9 2\n2 7 9\n6 4 10\n1 1 8\n2 8 1\n10 6 3\n7 5 2\n9 5 4",
"output": "41\n6\n21\n12\n39\n4\n7\n26\n8\n18"
},
{
"input": "1\n100000 100000 100000",
"output": "8436"
}
] | 46 | 0 | 0 | 22,429 |
|
379 | New Year Present | [
"constructive algorithms",
"implementation"
] | null | null | The New Year is coming! That's why many people today are busy preparing New Year presents. Vasily the Programmer is no exception.
Vasily knows that the best present is (no, it's not a contest) money. He's put *n* empty wallets from left to right in a row and decided how much money to put in what wallet. Vasily decided to put *a**i* coins to the *i*-th wallet from the left.
Vasily is a very busy man, so the money are sorted into the bags by his robot. Initially, the robot stands by the leftmost wallet in the row. The robot can follow instructions of three types: go to the wallet that is to the left of the current one (if such wallet exists), go to the wallet that is to the right of the current one (if such wallet exists), put a coin to the current wallet. Due to some technical malfunctions the robot cannot follow two "put a coin" instructions in a row.
Vasily doesn't want to wait for long, so he wants to write a program for the robot that contains at most 106 operations (not necessarily minimum in length) the robot can use to put coins into the wallets. Help him. | The first line contains integer *n* (2<=≤<=*n*<=≤<=300) — the number of wallets. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=300).
It is guaranteed that at least one *a**i* is positive. | Print the sequence that consists of *k* (1<=≤<=*k*<=≤<=106) characters, each of them equals: "L", "R" or "P". Each character of the sequence is an instruction to the robot. Character "L" orders to move to the left, character "R" orders to move to the right, character "P" orders the robot to put a coin in the wallet. The robot is not allowed to go beyond the wallet line. In other words, you cannot give instructions "L" if the robot is at wallet 1, or "R" at wallet *n*.
As a result of the performed operations, the *i*-th wallet from the left must contain exactly *a**i* coins. If there are multiple answers, you can print any of them. | [
"2\n1 2\n",
"4\n0 2 0 2\n"
] | [
"PRPLRP",
"RPRRPLLPLRRRP"
] | none | [
{
"input": "2\n1 2",
"output": "PRPLRP"
},
{
"input": "4\n0 2 0 2",
"output": "RPRRPLLPLRRRP"
},
{
"input": "10\n2 3 4 0 0 1 1 3 4 2",
"output": "PRPRPRRRPRPRPRPRPLPLPLLLLLPLPLPRPRPRRRRRPRPRPLPLLLLLLPLL"
},
{
"input": "10\n0 0 0 0 0 0 0 0 1 0",
"output": "RRRRRRRRPR"
},
{
"input": "5\n2 2 2 2 2",
"output": "PRPRPRPRPLPLPLPLPRRRRP"
},
{
"input": "2\n6 0",
"output": "PRLPRLPRLPRLPRLP"
}
] | 77 | 3,072,000 | -1 | 22,525 |
|
0 | none | [
"none"
] | null | null | One day Polycarpus got hold of two non-empty strings *s* and *t*, consisting of lowercase Latin letters. Polycarpus is quite good with strings, so he immediately wondered, how many different pairs of "*x* *y*" are there, such that *x* is a substring of string *s*, *y* is a subsequence of string *t*, and the content of *x* and *y* is the same. Two pairs are considered different, if they contain different substrings of string *s* or different subsequences of string *t*. Read the whole statement to understand the definition of different substrings and subsequences.
The length of string *s* is the number of characters in it. If we denote the length of the string *s* as |*s*|, we can write the string as *s*<==<=*s*1*s*2... *s*|*s*|.
A substring of *s* is a non-empty string *x*<==<=*s*[*a*... *b*]<==<=*s**a**s**a*<=+<=1... *s**b* (1<=≤<=*a*<=≤<=*b*<=≤<=|*s*|). For example, "code" and "force" are substrings or "codeforces", while "coders" is not. Two substrings *s*[*a*... *b*] and *s*[*c*... *d*] are considered to be different if *a*<=≠<=*c* or *b*<=≠<=*d*. For example, if *s*="codeforces", *s*[2...2] and *s*[6...6] are different, though their content is the same.
A subsequence of *s* is a non-empty string *y*<==<=*s*[*p*1*p*2... *p*|*y*|]<==<=*s**p*1*s**p*2... *s**p*|*y*| (1<=≤<=*p*1<=<<=*p*2<=<<=...<=<<=*p*|*y*|<=≤<=|*s*|). For example, "coders" is a subsequence of "codeforces". Two subsequences *u*<==<=*s*[*p*1*p*2... *p*|*u*|] and *v*<==<=*s*[*q*1*q*2... *q*|*v*|] are considered different if the sequences *p* and *q* are different. | The input consists of two lines. The first of them contains *s* (1<=≤<=|*s*|<=≤<=5000), and the second one contains *t* (1<=≤<=|*t*|<=≤<=5000). Both strings consist of lowercase Latin letters. | Print a single number — the number of different pairs "*x* *y*" such that *x* is a substring of string *s*, *y* is a subsequence of string *t*, and the content of *x* and *y* is the same. As the answer can be rather large, print it modulo 1000000007 (109<=+<=7). | [
"aa\naa\n",
"codeforces\nforceofcode\n"
] | [
"5\n",
"60\n"
] | Let's write down all pairs "*x* *y*" that form the answer in the first sample: "*s*[1...1] *t*[1]", "*s*[2...2] *t*[1]", "*s*[1...1] *t*[2]","*s*[2...2] *t*[2]", "*s*[1...2] *t*[1 2]". | [] | 62 | 0 | 0 | 22,539 |
|
332 | Theft of Blueprints | [
"graphs",
"math"
] | null | null | Insurgents accidentally got hold of the plan of a top secret research polygon created on a distant planet for the needs of the Galaxy Empire. The insurgents suppose that this polygon is developing new deadly weapon. The polygon consists of *n* missile silos connected by bidirectional underground passages. The passages are linked to laboratories where research is conducted. Naturally, the passages are guarded severely: the passage between silos *i* and *j* is patrolled by *c**i*,<=*j* war droids.
The insurgents studied the polygon plan and noticed its unusual structure. As it turned out, for any *k*-element set of silos *S* there is exactly one silo that is directly connected by a passage with each silo from *S* (we'll call this silo adjacent with *S*). Having considered that, the insurgents decided to act as follows:
1. they choose a *k*-element set of silos *S*; 1. a group of scouts lands from the air into each silo from *S*; 1. each group moves along the corresponding passage to the silo, adjacent with *S* (as the scouts move, they check out the laboratories and watch for any signs of weapon blueprints); 1. in the silo, adjacent with *S*, the groups get on the ship and fly away.
The danger of the operation is the total number of droids that patrol the passages through which the scouts will go. The danger of the operation obviously only depends on the way to choose set *S*. The insurgents haven't yet decided on the exact silos to send the scouts to. However, they already want to start preparing the weapons for the scout groups. To do that, the insurgents need to know the mathematical average of the dangers of the operations that correspond to all possible ways to choose set *S*. Solve this problem to help the insurgents protect the ideals of the Republic! | The first line contains two integers *n* and *k* (2<=≤<=*n*<=≤<=2000, 1<=≤<=*k*<=≤<=*n*<=-<=1) — the number of silos and the number of scout groups, correspondingly. The next *n*<=-<=1 lines describe the polygon plan: the *i*-th of these lines contains *n*<=-<=*i* integers *c**i*,<=*i*<=+<=1,<=*c**i*,<=*i*<=+<=2,<=...,<=*c**i*,<=*n* — the number of droids that patrol the corresponding passages (-1<=≤<=*c**i*,<=*j*<=≤<=109; if *c**i*,<=*j*<==<= -1, then silos *i* and *j* don't have a passage between them). All passages are bidirectional, that is, we can assume that *c**i*,<=*j*<==<=*c**j*,<=*i*. No passages connect a silo with itself. It is guaranteed that the polygon plan meets the conditions of the problem statement. | Print the average danger of the scouting operation, rounded down to an integer. Note that at the given limits the answer to the problem always fits into the standard integer 64-bit data type.
Please do not use the %lld specifier to write 64-bit integers in С++. It is preferred to use the cout stream or the %I64d specifier. | [
"6 1\n-1 -1 -1 8 -1\n-1 5 -1 -1\n-1 -1 3\n-1 -1\n-1\n",
"3 2\n10 0\n11\n"
] | [
"5\n",
"14\n"
] | In the first sample there are 6 one-element sets of silos. For sets {1}, {5} the operation danger will equal 8, for sets {3}, {6} — 3, for sets {2}, {4} — 5. The mathematical average equals <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3bc833659595c25e73150ed7f23907011961ceca.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second sample there are 3 two-elements sets of silos: {1, 3} (danger equals 21), {1, 2} (danger equals 11), {2, 3} (danger equals 10). The average operation danger equals <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/02946ba8c525c97041629189ab4acc976074d8d1.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "6 1\n-1 -1 -1 8 -1\n-1 5 -1 -1\n-1 -1 3\n-1 -1\n-1",
"output": "5"
},
{
"input": "3 2\n10 0\n11",
"output": "14"
},
{
"input": "4 3\n15 1 3\n5 8\n9",
"output": "20"
},
{
"input": "5 2\n-1 -1 14 3\n19 -1 1\n-1 6\n0",
"output": "10"
},
{
"input": "10 9\n999999996 999999991 999999997 999999994 999999991 999999992 999999992 1000000000 999999995\n999999999 999999992 1000000000 999999993 999999996 999999996 999999996 999999991\n999999990 999999996 999999998 999999996 999999993 999999995 999999991\n999999998 999999994 999999996 999999994 999999990 999999993\n999999993 999999998 999999993 999999991 999999996\n999999995 999999990 999999992 999999999\n1000000000 999999995 999999991\n999999996 999999998\n999999991",
"output": "8999999950"
},
{
"input": "2 1\n0",
"output": "0"
},
{
"input": "2 1\n1000000000",
"output": "1000000000"
}
] | 92 | 0 | 0 | 22,540 |
|
14 | Camels | [
"dp"
] | E. Camels | 2 | 64 | Bob likes to draw camels: with a single hump, two humps, three humps, etc. He draws a camel by connecting points on a coordinate plane. Now he's drawing camels with *t* humps, representing them as polylines in the plane. Each polyline consists of *n* vertices with coordinates (*x*1,<=*y*1), (*x*2,<=*y*2), ..., (*x**n*,<=*y**n*). The first vertex has a coordinate *x*1<==<=1, the second — *x*2<==<=2, etc. Coordinates *y**i* might be any, but should satisfy the following conditions:
- there should be *t* humps precisely, i.e. such indexes *j* (2<=≤<=*j*<=≤<=*n*<=-<=1), so that *y**j*<=-<=1<=<<=*y**j*<=><=*y**j*<=+<=1, - there should be precisely *t*<=-<=1 such indexes *j* (2<=≤<=*j*<=≤<=*n*<=-<=1), so that *y**j*<=-<=1<=><=*y**j*<=<<=*y**j*<=+<=1, - no segment of a polyline should be parallel to the *Ox*-axis, - all *y**i* are integers between 1 and 4.
For a series of his drawings of camels with *t* humps Bob wants to buy a notebook, but he doesn't know how many pages he will need. Output the amount of different polylines that can be drawn to represent camels with *t* humps for a given number *n*. | The first line contains a pair of integers *n* and *t* (3<=≤<=*n*<=≤<=20, 1<=≤<=*t*<=≤<=10). | Output the required amount of camels with *t* humps. | [
"6 1\n",
"4 2\n"
] | [
"6\n",
"0\n"
] | In the first sample test sequences of *y*-coordinates for six camels are: 123421, 123431, 123432, 124321, 134321 и 234321 (each digit corresponds to one value of *y*<sub class="lower-index">*i*</sub>). | [
{
"input": "6 1",
"output": "6"
},
{
"input": "4 2",
"output": "0"
},
{
"input": "3 1",
"output": "14"
},
{
"input": "3 2",
"output": "0"
},
{
"input": "3 3",
"output": "0"
},
{
"input": "3 10",
"output": "0"
},
{
"input": "4 1",
"output": "22"
},
{
"input": "4 3",
"output": "0"
},
{
"input": "4 9",
"output": "0"
},
{
"input": "5 1",
"output": "16"
},
{
"input": "5 2",
"output": "70"
},
{
"input": "5 3",
"output": "0"
},
{
"input": "5 5",
"output": "0"
},
{
"input": "5 9",
"output": "0"
},
{
"input": "5 10",
"output": "0"
},
{
"input": "6 1",
"output": "6"
},
{
"input": "6 2",
"output": "232"
},
{
"input": "6 3",
"output": "0"
},
{
"input": "6 4",
"output": "0"
},
{
"input": "6 10",
"output": "0"
},
{
"input": "19 1",
"output": "0"
},
{
"input": "19 2",
"output": "0"
},
{
"input": "19 3",
"output": "1"
},
{
"input": "19 4",
"output": "32632"
},
{
"input": "19 5",
"output": "4594423"
},
{
"input": "19 6",
"output": "69183464"
},
{
"input": "19 7",
"output": "197939352"
},
{
"input": "19 8",
"output": "109824208"
},
{
"input": "19 9",
"output": "5846414"
},
{
"input": "19 10",
"output": "0"
},
{
"input": "20 1",
"output": "0"
},
{
"input": "20 2",
"output": "0"
},
{
"input": "20 3",
"output": "0"
},
{
"input": "20 4",
"output": "12628"
},
{
"input": "20 5",
"output": "3715462"
},
{
"input": "20 6",
"output": "96046590"
},
{
"input": "20 7",
"output": "468541040"
},
{
"input": "20 8",
"output": "503245466"
},
{
"input": "20 9",
"output": "90700276"
},
{
"input": "20 10",
"output": "0"
}
] | 60 | 0 | 0 | 22,548 |
311 | Cats Transport | [
"data structures",
"dp"
] | null | null | Zxr960115 is owner of a large farm. He feeds *m* cute cats and employs *p* feeders. There's a straight road across the farm and *n* hills along the road, numbered from 1 to *n* from left to right. The distance between hill *i* and (*i*<=-<=1) is *d**i* meters. The feeders live in hill 1.
One day, the cats went out to play. Cat *i* went on a trip to hill *h**i*, finished its trip at time *t**i*, and then waited at hill *h**i* for a feeder. The feeders must take all the cats. Each feeder goes straightly from hill 1 to *n* without waiting at a hill and takes all the waiting cats at each hill away. Feeders walk at a speed of 1 meter per unit time and are strong enough to take as many cats as they want.
For example, suppose we have two hills (*d*2<==<=1) and one cat that finished its trip at time 3 at hill 2 (*h*1<==<=2). Then if the feeder leaves hill 1 at time 2 or at time 3, he can take this cat, but if he leaves hill 1 at time 1 he can't take it. If the feeder leaves hill 1 at time 2, the cat waits him for 0 time units, if the feeder leaves hill 1 at time 3, the cat waits him for 1 time units.
Your task is to schedule the time leaving from hill 1 for each feeder so that the sum of the waiting time of all cats is minimized. | The first line of the input contains three integers *n*,<=*m*,<=*p* (2<=≤<=*n*<=≤<=105,<=1<=≤<=*m*<=≤<=105,<=1<=≤<=*p*<=≤<=100).
The second line contains *n*<=-<=1 positive integers *d*2,<=*d*3,<=...,<=*d**n* (1<=≤<=*d**i*<=<<=104).
Each of the next *m* lines contains two integers *h**i* and *t**i* (1<=≤<=*h**i*<=≤<=*n*,<=0<=≤<=*t**i*<=≤<=109). | Output an integer, the minimum sum of waiting time of all cats.
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 6 2\n1 3 5\n1 0\n2 1\n4 9\n1 10\n2 10\n3 12\n"
] | [
"3\n"
] | none | [
{
"input": "4 6 2\n1 3 5\n1 0\n2 1\n4 9\n1 10\n2 10\n3 12",
"output": "3"
}
] | 78 | 409,600 | 0 | 22,582 |
|
796 | Police Stations | [
"constructive algorithms",
"dfs and similar",
"dp",
"graphs",
"shortest paths",
"trees"
] | null | null | Inzane finally found Zane with a lot of money to spare, so they together decided to establish a country of their own.
Ruling a country is not an easy job. Thieves and terrorists are always ready to ruin the country's peace. To fight back, Zane and Inzane have enacted a very effective law: from each city it must be possible to reach a police station by traveling at most *d* kilometers along the roads.
There are *n* cities in the country, numbered from 1 to *n*, connected only by exactly *n*<=-<=1 roads. All roads are 1 kilometer long. It is initially possible to travel from a city to any other city using these roads. The country also has *k* police stations located in some cities. In particular, the city's structure satisfies the requirement enforced by the previously mentioned law. Also note that there can be multiple police stations in one city.
However, Zane feels like having as many as *n*<=-<=1 roads is unnecessary. The country is having financial issues, so it wants to minimize the road maintenance cost by shutting down as many roads as possible.
Help Zane find the maximum number of roads that can be shut down without breaking the law. Also, help him determine such roads. | The first line contains three integers *n*, *k*, and *d* (2<=≤<=*n*<=≤<=3·105, 1<=≤<=*k*<=≤<=3·105, 0<=≤<=*d*<=≤<=*n*<=-<=1) — the number of cities, the number of police stations, and the distance limitation in kilometers, respectively.
The second line contains *k* integers *p*1,<=*p*2,<=...,<=*p**k* (1<=≤<=*p**i*<=≤<=*n*) — each denoting the city each police station is located in.
The *i*-th of the following *n*<=-<=1 lines contains two integers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*, *u**i*<=≠<=*v**i*) — the cities directly connected by the road with index *i*.
It is guaranteed that it is possible to travel from one city to any other city using only the roads. Also, it is possible from any city to reach a police station within *d* kilometers. | In the first line, print one integer *s* that denotes the maximum number of roads that can be shut down.
In the second line, print *s* distinct integers, the indices of such roads, in any order.
If there are multiple answers, print any of them. | [
"6 2 4\n1 6\n1 2\n2 3\n3 4\n4 5\n5 6\n",
"6 3 2\n1 5 6\n1 2\n1 3\n1 4\n1 5\n5 6\n"
] | [
"1\n5\n",
"2\n4 5 "
] | In the first sample, if you shut down road 5, all cities can still reach a police station within *k* = 4 kilometers.
In the second sample, although this is the only largest valid set of roads that can be shut down, you can print either 4 5 or 5 4 in the second line. | [
{
"input": "6 2 4\n1 6\n1 2\n2 3\n3 4\n4 5\n5 6",
"output": "1\n3 "
},
{
"input": "6 3 2\n1 5 6\n1 2\n1 3\n1 4\n1 5\n5 6",
"output": "2\n4 5 "
},
{
"input": "10 1 5\n5\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10",
"output": "0"
},
{
"input": "11 1 5\n6\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11",
"output": "0"
},
{
"input": "2 1 1\n1\n1 2",
"output": "0"
}
] | 2,000 | 105,472,000 | 0 | 22,587 |
|
0 | none | [
"none"
] | 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 1999"
},
{
"input": "100 110 2 2 109",
"output": "0 2"
},
{
"input": "3746 3797 485 485 3747",
"output": "450 9"
},
{
"input": "900000 1000000 50000 50000 960000",
"output": "33332 49998"
},
{
"input": "1 3 100 100 2",
"output": "100 100"
},
{
"input": "1 3 100 100 3",
"output": "0 100"
},
{
"input": "1 1 100 100 1",
"output": "100 100"
},
{
"input": "1 1 1 1 1",
"output": "1 1"
},
{
"input": "10 14 1 1 12",
"output": "1 1"
},
{
"input": "10 14 1 1 13",
"output": "0 1"
},
{
"input": "10 14 1 1 14",
"output": "0 1"
},
{
"input": "10 14 1 1 11",
"output": "1 1"
},
{
"input": "10 14 1 1 10",
"output": "1 0"
},
{
"input": "1 1000000 1000000 1000000 500000",
"output": "1000000 999998"
},
{
"input": "1 1000000 1000000 1000000 2",
"output": "999998 1"
},
{
"input": "1 1000000 1000000 1000000 999999",
"output": "1 999998"
},
{
"input": "3 9 9 2 5",
"output": "4 2"
},
{
"input": "7 9 481 961 9",
"output": "0 961"
},
{
"input": "5 10 6361 6643 9",
"output": "1660 6640"
},
{
"input": "3 10 202534 204124 7",
"output": "153093 204124"
},
{
"input": "4 7 990105 993245 7",
"output": "0 993245"
},
{
"input": "167 6430 3 2 4879",
"output": "0 2"
},
{
"input": "59039 78548 8 5 68239",
"output": "5 5"
},
{
"input": "99065 826220 9 3 659285",
"output": "0 3"
},
{
"input": "973058 995844 1 10 973658",
"output": "1 1"
},
{
"input": "983534 987908 2 7 984750",
"output": "2 1"
},
{
"input": "127873 889327 5550 623544 491743",
"output": "4953 4533"
},
{
"input": "146692 953585 99505 406219 259334",
"output": "92031 14932"
},
{
"input": "61097 812001 384947 188893 662044",
"output": "41007 164334"
},
{
"input": "581106 975502 703094 487920 637713",
"output": "675578 113214"
},
{
"input": "663155 979777 797049 494787 951112",
"output": "28665 287957"
},
{
"input": "129630 805489 631548 761110 577559",
"output": "227930 447929"
},
{
"input": "499637 716156 949694 543785 663905",
"output": "156753 492804"
},
{
"input": "522321 902347 10945 842811 630561",
"output": "9052 3605"
},
{
"input": "285510 831681 329092 849678 821409",
"output": "13696 714532"
},
{
"input": "176902 815637 847541 412251 587604",
"output": "228033 410702"
},
{
"input": "690136 947897 137581 128882 932136",
"output": "6612 101523"
},
{
"input": "122316 918901 393457 621754 907250",
"output": "9025 608019"
},
{
"input": "345903 808776 240052 245730 365687",
"output": "231914 10355"
},
{
"input": "483180 855922 224311 233776 855647",
"output": "141 190974"
},
{
"input": "353408 572330 154358 165573 557017",
"output": "11080 147325"
},
{
"input": "632076 716031 914 915 662639",
"output": "856 490"
},
{
"input": "668704 747356 945 949 696258",
"output": "790 426"
},
{
"input": "463050 509065 994 994 489428",
"output": "737 990"
},
{
"input": "77909 251377 937 952 115397",
"output": "798 220"
},
{
"input": "13612 793764 96 76 398584",
"output": "78 76"
},
{
"input": "1 5 3 5 5",
"output": "0 5"
},
{
"input": "99 99 99 99 99",
"output": "99 99"
},
{
"input": "100 100 100 100 100",
"output": "100 100"
},
{
"input": "1000000 1000000 1000000 1000000 1000000",
"output": "1000000 1000000"
},
{
"input": "1000000 1000000 999999 999998 1000000",
"output": "999999 999998"
},
{
"input": "5 5 5 5 5",
"output": "5 5"
},
{
"input": "10 10 100 100 10",
"output": "100 100"
},
{
"input": "1000 1000 1000 1000 1000",
"output": "1000 1000"
},
{
"input": "10 10 5 5 10",
"output": "5 5"
},
{
"input": "1 2 100 100 2",
"output": "0 100"
},
{
"input": "100 100 1000 1000 100",
"output": "1000 1000"
},
{
"input": "1000 1000000 1000000 1000000 1000000",
"output": "0 1000000"
},
{
"input": "50 100 100 100 100",
"output": "0 100"
},
{
"input": "10 10 20 20 10",
"output": "20 20"
},
{
"input": "1 100000 1000 1 2",
"output": "1000 1"
},
{
"input": "1000 1000000 100000 1000000 1000000",
"output": "0 1000000"
},
{
"input": "1 10 10 10 10",
"output": "0 10"
},
{
"input": "1000000 1000000 50 50 1000000",
"output": "50 50"
},
{
"input": "300 300 1000 1000 300",
"output": "1000 1000"
},
{
"input": "5 5 123 1234 5",
"output": "123 1234"
}
] | 216 | 2,867,200 | 0 | 22,609 |
|
4 | Mysterious Present | [
"dp",
"sortings"
] | D. Mysterious Present | 1 | 64 | Peter decided to wish happy birthday to his friend from Australia and send him a card. To make his present more mysterious, he decided to make a chain. Chain here is such a sequence of envelopes *A*<==<={*a*1,<=<=*a*2,<=<=...,<=<=*a**n*}, where the width and the height of the *i*-th envelope is strictly higher than the width and the height of the (*i*<=<=-<=<=1)-th envelope respectively. Chain size is the number of envelopes in the chain.
Peter wants to make the chain of the maximum size from the envelopes he has, the chain should be such, that he'll be able to put a card into it. The card fits into the chain if its width and height is lower than the width and the height of the smallest envelope in the chain respectively. It's forbidden to turn the card and the envelopes.
Peter has very many envelopes and very little time, this hard task is entrusted to you. | The first line contains integers *n*, *w*, *h* (1<=<=≤<=*n*<=≤<=5000, 1<=≤<=*w*,<=<=*h*<=<=≤<=106) — amount of envelopes Peter has, the card width and height respectively. Then there follow *n* lines, each of them contains two integer numbers *w**i* and *h**i* — width and height of the *i*-th envelope (1<=≤<=*w**i*,<=<=*h**i*<=≤<=106). | In the first line print the maximum chain size. In the second line print the numbers of the envelopes (separated by space), forming the required chain, starting with the number of the smallest envelope. Remember, please, that the card should fit into the smallest envelope. If the chain of maximum size is not unique, print any of the answers.
If the card does not fit into any of the envelopes, print number 0 in the single line. | [
"2 1 1\n2 2\n2 2\n",
"3 3 3\n5 4\n12 11\n9 8\n"
] | [
"1\n1 \n",
"3\n1 3 2 \n"
] | none | [
{
"input": "2 1 1\n2 2\n2 2",
"output": "1\n1 "
},
{
"input": "3 3 3\n5 4\n12 11\n9 8",
"output": "3\n1 3 2 "
},
{
"input": "5 10 10\n22 23\n17 19\n13 17\n8 12\n2 6",
"output": "3\n3 2 1 "
},
{
"input": "5 13 13\n4 4\n10 10\n7 7\n1 1\n13 13",
"output": "0"
},
{
"input": "4 12 140\n172 60\n71 95\n125 149\n53 82",
"output": "1\n3 "
},
{
"input": "3 500 789\n56 32\n64 42\n74 55",
"output": "0"
},
{
"input": "4 100 100\n332 350\n232 250\n32 50\n132 150",
"output": "3\n4 2 1 "
},
{
"input": "2 10 10\n15 15\n16 16",
"output": "2\n1 2 "
},
{
"input": "6 1 1\n900000 900000\n902400 902400\n901200 901200\n903600 903600\n906000 906000\n904800 904800",
"output": "6\n1 3 2 4 6 5 "
},
{
"input": "5 1000 998\n5002 5005\n5003 5004\n5003 5002\n5002 5001\n5002 5002",
"output": "2\n4 3 "
},
{
"input": "3 5 5\n6 2\n7 8\n10 2",
"output": "1\n2 "
},
{
"input": "14 12 800\n166 847\n205 889\n223 907\n93 785\n110 803\n136 829\n189 871\n149 839\n40 740\n48 750\n180 857\n76 777\n125 820\n63 766",
"output": "9\n5 13 6 8 1 11 7 2 3 "
},
{
"input": "15 600 875\n1200 451\n1664 852\n1763 1355\n1374 1724\n1374 1587\n1003 1513\n1636 1002\n431 367\n1632 690\n1257 778\n410 1632\n1045 1279\n1762 1763\n841 576\n1165 705",
"output": "3\n6 5 13 "
},
{
"input": "30 900 15\n1396 562\n1265 475\n3329 2605\n1016 340\n2369 1595\n2085 1245\n2677 1934\n1953 1154\n3002 2199\n1688 855\n1147 407\n2762 2064\n2202 1359\n700 30\n3265 2488\n1884 1034\n2571 1836\n3067 2269\n882 212\n3505 2793\n1602 785\n2292 1460\n2442 1712\n2889 2127\n3187 2361\n1770 932\n3624 2891\n783 104\n3410 2704\n1521 696",
"output": "27\n4 11 2 1 30 21 10 26 16 8 6 13 22 5 23 17 7 12 24 9 18 25 15 3 29 20 27 "
}
] | 46 | 0 | 0 | 22,624 |
118 | Caesar's Legions | [
"dp"
] | null | null | Gaius Julius Caesar, a famous general, loved to line up his soldiers. Overall the army had *n*1 footmen and *n*2 horsemen. Caesar thought that an arrangement is not beautiful if somewhere in the line there are strictly more that *k*1 footmen standing successively one after another, or there are strictly more than *k*2 horsemen standing successively one after another. Find the number of beautiful arrangements of the soldiers.
Note that all *n*1<=+<=*n*2 warriors should be present at each arrangement. All footmen are considered indistinguishable among themselves. Similarly, all horsemen are considered indistinguishable among themselves. | The only line contains four space-separated integers *n*1, *n*2, *k*1, *k*2 (1<=≤<=*n*1,<=*n*2<=≤<=100,<=1<=≤<=*k*1,<=*k*2<=≤<=10) which represent how many footmen and horsemen there are and the largest acceptable number of footmen and horsemen standing in succession, correspondingly. | Print the number of beautiful arrangements of the army modulo 100000000 (108). That is, print the number of such ways to line up the soldiers, that no more than *k*1 footmen stand successively, and no more than *k*2 horsemen stand successively. | [
"2 1 1 10\n",
"2 3 1 2\n",
"2 4 1 1\n"
] | [
"1\n",
"5\n",
"0\n"
] | Let's mark a footman as 1, and a horseman as 2.
In the first sample the only beautiful line-up is: 121
In the second sample 5 beautiful line-ups exist: 12122, 12212, 21212, 21221, 22121 | [
{
"input": "2 1 1 10",
"output": "1"
},
{
"input": "2 3 1 2",
"output": "5"
},
{
"input": "2 4 1 1",
"output": "0"
},
{
"input": "10 10 5 7",
"output": "173349"
},
{
"input": "12 15 7 2",
"output": "171106"
},
{
"input": "20 8 4 8",
"output": "162585"
},
{
"input": "15 8 2 6",
"output": "156"
},
{
"input": "100 100 10 10",
"output": "950492"
},
{
"input": "20 15 10 9",
"output": "26057516"
},
{
"input": "18 4 3 1",
"output": "0"
},
{
"input": "19 12 5 7",
"output": "77429711"
},
{
"input": "20 4 9 4",
"output": "5631"
},
{
"input": "24 30 5 1",
"output": "0"
},
{
"input": "56 37 4 1",
"output": "84920121"
},
{
"input": "28 65 5 9",
"output": "83961789"
},
{
"input": "67 26 6 1",
"output": "89553795"
},
{
"input": "57 30 5 9",
"output": "17123805"
},
{
"input": "56 40 3 2",
"output": "69253068"
},
{
"input": "34 57 1 1",
"output": "0"
},
{
"input": "78 21 10 1",
"output": "96098560"
},
{
"input": "46 46 2 5",
"output": "84310381"
},
{
"input": "34 55 2 9",
"output": "13600171"
},
{
"input": "46 51 4 5",
"output": "25703220"
},
{
"input": "64 23 3 6",
"output": "7467801"
},
{
"input": "67 24 6 3",
"output": "3793964"
},
{
"input": "78 14 3 9",
"output": "0"
},
{
"input": "56 34 8 10",
"output": "92618496"
},
{
"input": "57 25 10 4",
"output": "4458038"
},
{
"input": "1 2 1 1",
"output": "1"
},
{
"input": "1 1 1 1",
"output": "2"
},
{
"input": "2 1 1 1",
"output": "1"
},
{
"input": "99 100 10 10",
"output": "65210983"
},
{
"input": "100 99 10 10",
"output": "65210983"
},
{
"input": "100 100 9 10",
"output": "67740290"
},
{
"input": "1 2 10 10",
"output": "3"
},
{
"input": "1 3 10 10",
"output": "4"
},
{
"input": "2 2 10 10",
"output": "6"
},
{
"input": "2 2 1 2",
"output": "3"
}
] | 154 | 2,252,800 | -1 | 22,643 |
|
756 | Nikita and stack | [
"data structures"
] | null | null | Nikita has a stack. A stack in this problem is a data structure that supports two operations. Operation push(x) puts an integer *x* on the top of the stack, and operation pop() deletes the top integer from the stack, i. e. the last added. If the stack is empty, then the operation pop() does nothing.
Nikita made *m* operations with the stack but forgot them. Now Nikita wants to remember them. He remembers them one by one, on the *i*-th step he remembers an operation he made *p**i*-th. In other words, he remembers the operations in order of some permutation *p*1,<=*p*2,<=...,<=*p**m*. After each step Nikita wants to know what is the integer on the top of the stack after performing the operations he have already remembered, in the corresponding order. Help him! | The first line contains the integer *m* (1<=≤<=*m*<=≤<=105) — the number of operations Nikita made.
The next *m* lines contain the operations Nikita remembers. The *i*-th line starts with two integers *p**i* and *t**i* (1<=≤<=*p**i*<=≤<=*m*, *t**i*<==<=0 or *t**i*<==<=1) — the index of operation he remembers on the step *i*, and the type of the operation. *t**i* equals 0, if the operation is pop(), and 1, is the operation is push(x). If the operation is push(x), the line also contains the integer *x**i* (1<=≤<=*x**i*<=≤<=106) — the integer added to the stack.
It is guaranteed that each integer from 1 to *m* is present exactly once among integers *p**i*. | Print *m* integers. The integer *i* should equal the number on the top of the stack after performing all the operations Nikita remembered on the steps from 1 to *i*. If the stack is empty after performing all these operations, print -1. | [
"2\n2 1 2\n1 0\n",
"3\n1 1 2\n2 1 3\n3 0\n",
"5\n5 0\n4 0\n3 1 1\n2 1 1\n1 1 2\n"
] | [
"2\n2\n",
"2\n3\n2\n",
"-1\n-1\n-1\n-1\n2\n"
] | In the first example, after Nikita remembers the operation on the first step, the operation push(2) is the only operation, so the answer is 2. After he remembers the operation pop() which was done before push(2), answer stays the same.
In the second example, the operations are push(2), push(3) and pop(). Nikita remembers them in the order they were performed.
In the third example Nikita remembers the operations in the reversed order. | [] | 46 | 0 | 0 | 22,667 |
|
600 | Make Palindrome | [
"constructive algorithms",
"greedy",
"strings"
] | null | null | A string is called palindrome if it reads the same from left to right and from right to left. For example "kazak", "oo", "r" and "mikhailrubinchikkihcniburliahkim" are palindroms, but strings "abb" and "ij" are not.
You are given string *s* consisting of lowercase Latin letters. At once you can choose any position in the string and change letter in that position to any other lowercase letter. So after each changing the length of the string doesn't change. At first you can change some letters in *s*. Then you can permute the order of letters as you want. Permutation doesn't count as changes.
You should obtain palindrome with the minimal number of changes. If there are several ways to do that you should get the lexicographically (alphabetically) smallest palindrome. So firstly you should minimize the number of changes and then minimize the palindrome lexicographically. | The only line contains string *s* (1<=≤<=|*s*|<=≤<=2·105) consisting of only lowercase Latin letters. | Print the lexicographically smallest palindrome that can be obtained with the minimal number of changes. | [
"aabc\n",
"aabcd\n"
] | [
"abba\n",
"abcba\n"
] | none | [
{
"input": "aabc",
"output": "abba"
},
{
"input": "aabcd",
"output": "abcba"
},
{
"input": "u",
"output": "u"
},
{
"input": "ttttt",
"output": "ttttt"
},
{
"input": "xxxvvvxxvv",
"output": "vvvxxxxvvv"
},
{
"input": "wrwrwfrrfrffrrwwwffffwrfrrwfrrfrwwfwfrwfwfwffwrrwfrrrwwwfrrrwfrrfwrwwrwrrrffffwrrrwrwfffwrffrwwwrwww",
"output": "fffffffffffffffrrrrrrrrrrrrrrrrrrwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrrrrrrrrrrrrrrrrrrfffffffffffffff"
},
{
"input": "aabbcccdd",
"output": "abcdcdcba"
},
{
"input": "baaab",
"output": "ababa"
},
{
"input": "aaabbbhhlhlugkjgckj",
"output": "aabbghjklclkjhgbbaa"
},
{
"input": "aabcc",
"output": "acbca"
},
{
"input": "bbbcccddd",
"output": "bbcdcdcbb"
},
{
"input": "zzzozzozozozoza",
"output": "aoozzzzozzzzooa"
},
{
"input": "aaabb",
"output": "ababa"
},
{
"input": "zza",
"output": "zaz"
},
{
"input": "azzzbbb",
"output": "abzbzba"
},
{
"input": "bbaaccddc",
"output": "abcdcdcba"
},
{
"input": "aaabbbccc",
"output": "aabcbcbaa"
},
{
"input": "aaaaabbccdd",
"output": "aabcdadcbaa"
},
{
"input": "aaabbbcccdd",
"output": "aabcdbdcbaa"
},
{
"input": "aaaabbcccccdd",
"output": "aabccdcdccbaa"
},
{
"input": "aaacccb",
"output": "aacbcaa"
},
{
"input": "abcd",
"output": "abba"
},
{
"input": "abb",
"output": "bab"
},
{
"input": "abababccc",
"output": "aabcbcbaa"
},
{
"input": "aaadd",
"output": "adada"
},
{
"input": "qqqqaaaccdd",
"output": "acdqqaqqdca"
},
{
"input": "affawwzzw",
"output": "afwzwzwfa"
},
{
"input": "hack",
"output": "acca"
},
{
"input": "bbaaa",
"output": "ababa"
},
{
"input": "ababa",
"output": "ababa"
},
{
"input": "aaazzzz",
"output": "azzazza"
},
{
"input": "aabbbcc",
"output": "abcbcba"
},
{
"input": "successfullhack",
"output": "accelsufuslecca"
},
{
"input": "aaabbccdd",
"output": "abcdadcba"
},
{
"input": "zaz",
"output": "zaz"
},
{
"input": "aaabbbcccdddeee",
"output": "aabbcdecedcbbaa"
},
{
"input": "zaaz",
"output": "azza"
},
{
"input": "acc",
"output": "cac"
},
{
"input": "abbbzzz",
"output": "abzbzba"
},
{
"input": "zzzzazazazazazznnznznnznnznznzaajzjajjjjanaznnzanzppnzpaznnpanz",
"output": "aaaaaaajjjnnnnnnnnppzzzzzzzzzzznzzzzzzzzzzzppnnnnnnnnjjjaaaaaaa"
},
{
"input": "aaaaabbbcccdddd",
"output": "aaabcddbddcbaaa"
},
{
"input": "aaaaabbccdddd",
"output": "aabcddaddcbaa"
},
{
"input": "abababa",
"output": "aabbbaa"
},
{
"input": "azz",
"output": "zaz"
},
{
"input": "abbbccc",
"output": "abcbcba"
},
{
"input": "aaacccddd",
"output": "aacdcdcaa"
},
{
"input": "asbbsha",
"output": "abshsba"
},
{
"input": "bababab",
"output": "abbabba"
},
{
"input": "aaabbccddbbccddaaaaaaaaaaaaaaaa",
"output": "aaaaaaaaabbccddaddccbbaaaaaaaaa"
},
{
"input": "aaabbccddbbccddaaaaaaaaaaaaaa",
"output": "aaaaaaaabbccddaddccbbaaaaaaaa"
},
{
"input": "aaabbccddbbccddaaaaaaaaaaaa",
"output": "aaaaaaabbccddaddccbbaaaaaaa"
},
{
"input": "ooooo",
"output": "ooooo"
},
{
"input": "aaabbccddbbccddaaaaaaaaaa",
"output": "aaaaaabbccddaddccbbaaaaaa"
},
{
"input": "aaabbccddbbccddaaaaaaaa",
"output": "aaaaabbccddaddccbbaaaaa"
},
{
"input": "aaabbccddbbccddaa",
"output": "aabbccddaddccbbaa"
}
] | 31 | 307,200 | 0 | 22,680 |
|
453 | Little Pony and Harmony Chest | [
"bitmasks",
"brute force",
"dp"
] | null | null | Princess Twilight went to Celestia and Luna's old castle to research the chest from the Elements of Harmony.
A sequence of positive integers *b**i* is harmony if and only if for every two elements of the sequence their greatest common divisor equals 1. According to an ancient book, the key of the chest is a harmony sequence *b**i* which minimizes the following expression:
You are given sequence *a**i*, help Princess Twilight to find the key. | The first line contains an integer *n* (1<=≤<=*n*<=≤<=100) — the number of elements of the sequences *a* and *b*. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=30). | Output the key — sequence *b**i* that minimizes the sum described above. If there are multiple optimal sequences, you can output any of them. | [
"5\n1 1 1 1 1\n",
"5\n1 6 4 2 8\n"
] | [
"1 1 1 1 1 ",
"1 5 3 1 8 "
] | none | [
{
"input": "5\n1 1 1 1 1",
"output": "1 1 1 1 1 "
},
{
"input": "5\n1 6 4 2 8",
"output": "1 5 3 1 8 "
},
{
"input": "10\n16 3 16 10 12 5 14 14 15 27",
"output": "19 1 17 7 11 1 16 13 15 29 "
},
{
"input": "10\n8 7 11 5 17 24 28 18 7 8",
"output": "9 7 11 5 17 23 29 19 1 8 "
},
{
"input": "10\n22 17 28 14 14 26 20 28 21 27",
"output": "23 17 31 13 11 25 19 29 16 27 "
},
{
"input": "10\n28 13 14 9 26 21 25 16 4 22",
"output": "29 13 14 11 27 19 25 17 1 23 "
},
{
"input": "100\n11 27 18 3 26 1 23 2 28 21 28 18 7 26 13 4 12 11 1 29 9 23 6 27 15 6 26 25 11 21 26 29 29 8 18 29 3 24 2 28 7 7 11 23 26 29 30 18 30 23 17 24 25 12 16 26 2 4 30 2 19 2 27 16 17 21 30 17 10 8 16 7 1 3 27 22 23 28 16 9 15 28 10 15 26 8 20 6 21 12 24 16 13 7 12 8 23 16 29 24",
"output": "11 27 17 1 25 1 23 1 19 13 1 1 1 1 1 1 1 1 1 53 1 1 1 1 1 1 1 1 1 1 1 49 47 1 1 43 1 1 1 1 1 1 1 1 1 29 41 1 37 1 1 1 1 1 1 1 1 1 32 1 1 1 1 1 1 1 31 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "
},
{
"input": "10\n26 25 10 6 28 11 23 22 8 23",
"output": "27 25 13 7 29 11 23 19 8 17 "
},
{
"input": "30\n24 11 1 17 15 11 12 8 6 25 22 23 9 3 30 3 10 28 15 8 4 28 3 28 26 14 24 1 6 19",
"output": "43 11 1 17 13 7 1 1 1 25 19 23 1 1 41 1 1 37 1 1 1 31 1 29 27 1 16 1 1 1 "
},
{
"input": "30\n11 20 1 17 6 20 22 16 20 22 21 8 3 28 30 2 27 14 10 14 29 21 13 3 13 27 11 18 2 15",
"output": "11 19 1 17 1 16 25 13 7 23 1 1 1 41 37 1 31 1 1 1 29 1 1 1 1 27 1 1 1 1 "
},
{
"input": "30\n17 16 18 4 3 22 28 4 4 17 9 30 2 11 29 12 8 17 9 12 13 11 13 18 28 17 10 12 14 20",
"output": "17 16 19 1 1 25 37 1 1 13 7 31 1 11 29 1 1 1 1 1 1 1 1 1 27 1 1 1 1 23 "
},
{
"input": "30\n9 26 5 7 29 17 19 22 1 28 5 6 9 8 13 9 3 4 16 16 11 24 22 20 12 9 16 22 2 11",
"output": "7 41 1 1 37 17 19 31 1 29 1 1 1 1 13 1 1 1 16 11 1 27 25 1 1 1 1 23 1 1 "
},
{
"input": "30\n15 1 29 2 20 5 3 2 15 17 1 28 9 20 26 5 28 7 7 7 26 15 23 14 19 23 15 19 23 8",
"output": "13 1 43 1 19 1 1 1 11 17 1 41 7 16 37 1 31 1 1 1 29 1 27 1 1 25 1 1 23 1 "
},
{
"input": "30\n15 3 16 26 17 7 8 6 29 12 27 4 16 25 10 2 9 24 6 19 10 12 23 15 3 7 13 13 27 6",
"output": "13 1 16 41 17 7 1 1 37 11 31 1 1 29 1 1 1 25 1 19 1 1 23 1 1 1 1 1 27 1 "
},
{
"input": "30\n21 13 3 13 22 2 30 16 26 23 22 3 7 7 23 20 13 13 13 10 24 25 10 9 2 13 20 24 23 3",
"output": "19 13 1 11 17 1 43 16 41 37 7 1 1 1 31 1 1 1 1 1 29 27 1 1 1 1 1 25 23 1 "
},
{
"input": "100\n17 7 25 13 9 17 18 17 29 7 9 25 26 3 18 25 21 14 21 21 27 30 30 24 29 30 2 14 17 17 8 19 6 20 11 9 19 21 20 25 21 11 29 26 22 8 1 30 27 20 10 16 20 10 17 2 14 21 18 15 19 6 26 8 15 19 20 8 1 23 10 11 30 14 9 9 17 9 11 22 13 5 19 26 9 10 3 9 6 24 26 27 6 14 9 24 20 13 14 23",
"output": "17 7 25 13 1 11 1 1 53 1 1 23 19 1 1 1 1 1 1 1 27 47 43 1 41 37 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 29 1 1 1 1 32 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 31 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "
},
{
"input": "100\n9 4 19 29 6 27 23 5 13 19 27 23 25 7 24 22 24 9 4 25 11 21 17 17 14 14 1 16 29 14 7 20 25 2 22 19 16 1 7 6 3 21 28 7 2 18 10 18 20 7 13 4 12 23 29 23 1 22 18 7 19 14 18 19 24 9 5 26 18 14 23 2 10 10 29 19 3 29 29 10 18 13 8 26 22 30 29 2 19 29 13 25 2 13 13 17 18 25 1 7",
"output": "1 1 19 53 1 27 23 1 13 17 25 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 49 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 47 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 43 1 1 41 37 1 1 1 1 1 1 32 31 1 1 29 1 1 1 1 1 1 1 1 1 1 "
},
{
"input": "100\n15 6 12 24 10 29 15 23 27 22 9 15 24 4 15 19 5 4 11 24 17 4 18 11 20 6 29 13 11 11 6 21 7 30 24 29 5 18 8 1 1 22 10 4 20 28 28 30 27 10 24 8 5 11 5 22 3 22 9 13 11 1 25 14 10 22 3 22 28 11 19 29 19 6 19 20 3 10 18 20 1 7 11 18 14 6 3 1 26 5 30 22 5 25 18 26 8 23 26 29",
"output": "13 1 11 23 7 53 1 19 27 17 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 47 1 1 1 1 1 1 43 1 41 1 1 1 1 1 1 1 1 1 25 1 37 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 32 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 31 1 1 1 1 1 1 1 1 29 "
},
{
"input": "100\n15 2 7 11 7 24 21 19 2 3 5 20 2 25 21 7 8 29 10 14 23 17 18 5 5 4 28 7 29 24 27 14 4 12 13 23 10 28 10 29 28 1 1 1 17 22 16 4 4 28 13 4 5 1 3 5 4 23 25 13 10 2 25 1 20 5 2 24 15 2 9 20 15 16 22 13 4 22 29 8 7 15 23 18 13 12 7 24 25 2 17 20 1 8 22 3 28 4 21 29",
"output": "13 1 7 11 1 23 19 17 1 1 1 1 1 25 1 1 1 53 1 1 1 1 1 1 1 1 47 1 43 1 27 1 1 1 1 1 1 41 1 37 32 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 31 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 29 "
},
{
"input": "100\n26 9 21 8 11 14 9 17 4 18 24 30 19 10 6 13 3 5 1 16 21 5 22 18 26 24 13 8 1 27 18 4 30 7 10 2 3 26 24 4 20 28 20 17 29 13 25 11 4 18 26 20 16 24 13 16 21 30 7 13 24 16 1 20 1 1 24 6 20 7 27 22 8 19 13 17 9 3 4 19 22 12 21 13 8 30 22 22 4 24 30 13 13 21 26 10 29 24 7 12",
"output": "25 7 19 1 11 13 1 17 1 1 23 53 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 27 1 1 47 1 1 1 1 1 1 1 1 43 1 1 41 1 1 1 1 1 1 1 1 1 1 1 1 37 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 32 1 1 1 1 31 1 1 1 1 1 29 1 1 1 "
},
{
"input": "100\n30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30",
"output": "53 49 47 43 41 37 32 31 29 27 25 23 19 17 13 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "
},
{
"input": "1\n1",
"output": "1 "
},
{
"input": "1\n30",
"output": "30 "
},
{
"input": "30\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
"output": "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "
},
{
"input": "15\n2 2 2 1 1 1 1 1 1 1 1 1 1 1 1",
"output": "2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "
},
{
"input": "1\n6",
"output": "6 "
},
{
"input": "3\n2 2 15",
"output": "2 1 15 "
}
] | 202 | 6,656,000 | 0 | 22,724 |
|
522 | Chicken or Fish? | [
"greedy"
] | null | null | Polycarp is flying in the airplane. Finally, it is his favorite time — the lunchtime. The BerAvia company stewardess is giving food consecutively to all the passengers from the 1-th one to the last one. Polycarp is sitting on seat *m*, that means, he will be the *m*-th person to get food.
The flight menu has *k* dishes in total and when Polycarp boarded the flight, he had time to count the number of portions of each dish on board. Thus, he knows values *a*1,<=*a*2,<=...,<=*a**k*, where *a**i* is the number of portions of the *i*-th dish.
The stewardess has already given food to *m*<=-<=1 passengers, gave Polycarp a polite smile and asked him what he would prefer. That's when Polycarp realized that they might have run out of some dishes by that moment. For some of the *m*<=-<=1 passengers ahead of him, he noticed what dishes they were given. Besides, he's heard some strange mumbling from some of the *m*<=-<=1 passengers ahead of him, similar to phrase 'I'm disappointed'. That happened when a passenger asked for some dish but the stewardess gave him a polite smile and said that they had run out of that dish. In that case the passenger needed to choose some other dish that was available. If Polycarp heard no more sounds from a passenger, that meant that the passenger chose his dish at the first try.
Help Polycarp to find out for each dish: whether they could have run out of the dish by the moment Polyarp was served or that dish was definitely available. | Each test in this problem consists of one or more input sets. First goes a string that contains a single integer *t* (1<=≤<=*t*<=≤<=100<=000) — the number of input data sets in the test. Then the sets follow, each set is preceded by an empty line.
The first line of each set of the input contains integers *m*, *k* (2<=≤<=*m*<=≤<=100<=000, 1<=≤<=*k*<=≤<=100<=000) — the number of Polycarp's seat and the number of dishes, respectively.
The second line contains a sequence of *k* integers *a*1,<=*a*2,<=...,<=*a**k* (1<=≤<=*a**i*<=≤<=100<=000), where *a**i* is the initial number of portions of the *i*-th dish.
Then *m*<=-<=1 lines follow, each line contains the description of Polycarp's observations about giving food to a passenger sitting in front of him: the *j*-th line contains a pair of integers *t**j*,<=*r**j* (0<=≤<=*t**j*<=≤<=*k*,<=0<=≤<=*r**j*<=≤<=1), where *t**j* is the number of the dish that was given to the *j*-th passenger (or 0, if Polycarp didn't notice what dish was given to the passenger), and *r**j* — a 1 or a 0, depending on whether the *j*-th passenger was or wasn't disappointed, respectively.
We know that sum *a**i* equals at least *m*, that is,Polycarp will definitely get some dish, even if it is the last thing he wanted. It is guaranteed that the data is consistent.
Sum *m* for all input sets doesn't exceed 100<=000. Sum *k* for all input sets doesn't exceed 100<=000. | For each input set print the answer as a single line. Print a string of *k* letters "Y" or "N". Letter "Y" in position *i* should be printed if they could have run out of the *i*-th dish by the time the stewardess started serving Polycarp. | [
"2\n\n3 4\n2 3 2 1\n1 0\n0 0\n\n5 5\n1 2 1 3 1\n3 0\n0 0\n2 1\n4 0\n"
] | [
"YNNY\nYYYNY\n"
] | In the first input set depending on the choice of the second passenger the situation could develop in different ways:
- If he chose the first dish, then by the moment the stewardess reaches Polycarp, they will have run out of the first dish; - If he chose the fourth dish, then by the moment the stewardess reaches Polycarp, they will have run out of the fourth dish; - Otherwise, Polycarp will be able to choose from any of the four dishes.
Thus, the answer is "YNNY".
In the second input set there is, for example, the following possible scenario. First, the first passenger takes the only third dish, then the second passenger takes the second dish. Then, the third passenger asks for the third dish, but it is not available, so he makes disappointed muttering and ends up with the second dish. Then the fourth passenger takes the fourth dish, and Polycarp ends up with the choice between the first, fourth and fifth dish.
Likewise, another possible scenario is when by the time the stewardess comes to Polycarp, they will have run out of either the first or the fifth dish (this can happen if one of these dishes is taken by the second passenger). It is easy to see that there is more than enough of the fourth dish, so Polycarp can always count on it. Thus, the answer is "YYYNY". | [
{
"input": "2\n\n3 4\n2 3 2 1\n1 0\n0 0\n\n5 5\n1 2 1 3 1\n3 0\n0 0\n2 1\n4 0",
"output": "YNNY\nYYYNY"
},
{
"input": "4\n\n2 1\n42\n0 0\n\n2 1\n2\n0 0\n\n2 1\n42\n1 0\n\n2 1\n2\n1 0",
"output": "N\nN\nN\nN"
},
{
"input": "5\n\n3 3\n1 1 1\n0 0\n0 1\n\n3 3\n1 1 1\n1 0\n2 1\n\n3 3\n1 1 1\n1 0\n0 1\n\n3 3\n1 1 1\n0 0\n1 0\n\n3 3\n1 1 1\n0 0\n1 1",
"output": "YYY\nYYN\nYYY\nYYY\nYYY"
},
{
"input": "1\n\n4 2\n2 2\n0 0\n0 0\n1 1",
"output": "NY"
}
] | 46 | 0 | -1 | 22,765 |
|
0 | none | [
"none"
] | null | null | Innokenty is a president of a new football league in Byteland. The first task he should do is to assign short names to all clubs to be shown on TV next to the score. Of course, the short names should be distinct, and Innokenty wants that all short names consist of three letters.
Each club's full name consist of two words: the team's name and the hometown's name, for example, "DINAMO BYTECITY". Innokenty doesn't want to assign strange short names, so he wants to choose such short names for each club that:
1. the short name is the same as three first letters of the team's name, for example, for the mentioned club it is "DIN", 1. or, the first two letters of the short name should be the same as the first two letters of the team's name, while the third letter is the same as the first letter in the hometown's name. For the mentioned club it is "DIB".
Apart from this, there is a rule that if for some club *x* the second option of short name is chosen, then there should be no club, for which the first option is chosen which is the same as the first option for the club *x*. For example, if the above mentioned club has short name "DIB", then no club for which the first option is chosen can have short name equal to "DIN". However, it is possible that some club have short name "DIN", where "DI" are the first two letters of the team's name, and "N" is the first letter of hometown's name. Of course, no two teams can have the same short name.
Help Innokenty to choose a short name for each of the teams. If this is impossible, report that. If there are multiple answer, any of them will suit Innokenty. If for some team the two options of short name are equal, then Innokenty will formally think that only one of these options is chosen. | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=1000) — the number of clubs in the league.
Each of the next *n* lines contains two words — the team's name and the hometown's name for some club. Both team's name and hometown's name consist of uppercase English letters and have length at least 3 and at most 20. | It it is not possible to choose short names and satisfy all constraints, print a single line "NO".
Otherwise, in the first line print "YES". Then print *n* lines, in each line print the chosen short name for the corresponding club. Print the clubs in the same order as they appeared in input.
If there are multiple answers, print any of them. | [
"2\nDINAMO BYTECITY\nFOOTBALL MOSCOW\n",
"2\nDINAMO BYTECITY\nDINAMO BITECITY\n",
"3\nPLAYFOOTBALL MOSCOW\nPLAYVOLLEYBALL SPB\nGOGO TECHNOCUP\n",
"3\nABC DEF\nABC EFG\nABD OOO\n"
] | [
"YES\nDIN\nFOO\n",
"NO\n",
"YES\nPLM\nPLS\nGOG\n",
"YES\nABD\nABE\nABO\n"
] | In the first sample Innokenty can choose first option for both clubs.
In the second example it is not possible to choose short names, because it is not possible that one club has first option, and the other has second option if the first options are equal for both clubs.
In the third example Innokenty can choose the second options for the first two clubs, and the first option for the third club.
In the fourth example note that it is possible that the chosen short name for some club *x* is the same as the first option of another club *y* if the first options of *x* and *y* are different. | [
{
"input": "2\nDINAMO BYTECITY\nFOOTBALL MOSCOW",
"output": "YES\nDIN\nFOO"
},
{
"input": "2\nDINAMO BYTECITY\nDINAMO BITECITY",
"output": "NO"
},
{
"input": "3\nPLAYFOOTBALL MOSCOW\nPLAYVOLLEYBALL SPB\nGOGO TECHNOCUP",
"output": "YES\nPLM\nPLS\nGOG"
},
{
"input": "3\nABC DEF\nABC EFG\nABD OOO",
"output": "YES\nABD\nABE\nABO"
},
{
"input": "3\nABC DEF\nABC EFG\nABC EEEEE",
"output": "NO"
},
{
"input": "3\nABC DEF\nABC EFG\nABD CABA",
"output": "YES\nABD\nABE\nABC"
},
{
"input": "3\nABC DEF\nABC EFG\nABD EABA",
"output": "NO"
},
{
"input": "1\nAAA AAA",
"output": "YES\nAAA"
},
{
"input": "1\nAAAAAAAAAAAAAAAAAAAA ZZZZZZZZZZZZZZZZZZZZ",
"output": "YES\nAAA"
},
{
"input": "5\nADAC BABC\nABB DCB\nABB BCDC\nDBAC BAC\nDBBC DBC",
"output": "YES\nADA\nABD\nABB\nDBA\nDBB"
},
{
"input": "5\nIAH HJIE\nIAH FJK\nIAH BIAA\nIAH AFG\nIAH DEFF",
"output": "YES\nIAH\nIAF\nIAB\nIAA\nIAD"
},
{
"input": "10\nIJGDI KHB\nHBI CKKCG\nFHE GCAA\nEDCGH HHICE\nGFH AIHD\nHED KIK\nDCK BCFIJ\nFFIHE FDB\nJGB AKKI\nIJD CAG",
"output": "YES\nIJG\nHBI\nFHE\nEDC\nGFH\nHED\nDCK\nFFI\nJGB\nIJD"
},
{
"input": "10\nEDBG IGGAC\nEDBG GIKAG\nEDBG IKGEI\nEDBG AJEG\nEDBG HAD\nEDBG ACKK\nEDBG FEDE\nEDBG DAB\nEDBG CCJBD\nEDBG KKGFB",
"output": "NO"
},
{
"input": "10\nADE GBH\nJJDGJ AAF\nJJDGJ BBKG\nADE FKH\nADE CIA\nAIE JCBJ\nAIE BBJB\nEBAK JDB\nJJDGJ IDBG\nCEJE FIG",
"output": "YES\nADG\nJJA\nJJB\nADF\nADC\nAIJ\nAIB\nEBA\nJJI\nCEJ"
},
{
"input": "4\nABA DEF\nABB DEF\nABC DEF\nABE DEF",
"output": "YES\nABA\nABB\nABC\nABE"
},
{
"input": "2\nABC CCC\nABE CCC",
"output": "YES\nABC\nABE"
},
{
"input": "2\nABS SSS\nABD SSD",
"output": "YES\nABS\nABD"
}
] | 171 | 23,654,400 | 0 | 22,815 |
|
755 | PolandBall and Gifts | [
"bitmasks",
"dp",
"greedy"
] | null | null | It's Christmas time! PolandBall and his friends will be giving themselves gifts. There are *n* Balls overall. Each Ball has someone for whom he should bring a present according to some permutation *p*, *p**i*<=≠<=*i* for all *i*.
Unfortunately, Balls are quite clumsy. We know earlier that exactly *k* of them will forget to bring their gift. A Ball number *i* will get his present if the following two constraints will hold:
1. Ball number *i* will bring the present he should give. 1. Ball *x* such that *p**x*<==<=*i* will bring his present.
What is minimum and maximum possible number of kids who will not get their present if exactly *k* Balls will forget theirs? | The first line of input contains two integers *n* and *k* (2<=≤<=*n*<=≤<=106, 0<=≤<=*k*<=≤<=*n*), representing the number of Balls and the number of Balls who will forget to bring their presents.
The second line contains the permutation *p* of integers from 1 to *n*, where *p**i* is the index of Ball who should get a gift from the *i*-th Ball. For all *i*, *p**i*<=≠<=*i* holds. | You should output two values — minimum and maximum possible number of Balls who will not get their presents, in that order. | [
"5 2\n3 4 1 5 2\n",
"10 1\n2 3 4 5 6 7 8 9 10 1\n"
] | [
"2 4",
"2 2"
] | In the first sample, if the third and the first balls will forget to bring their presents, they will be th only balls not getting a present. Thus the minimum answer is 2. However, if the first ans the second balls will forget to bring their presents, then only the fifth ball will get a present. So, the maximum answer is 4. | [
{
"input": "5 2\n3 4 1 5 2",
"output": "2 4"
},
{
"input": "10 1\n2 3 4 5 6 7 8 9 10 1",
"output": "2 2"
},
{
"input": "5 4\n3 1 4 5 2",
"output": "5 5"
},
{
"input": "3 0\n2 3 1",
"output": "0 0"
},
{
"input": "4 3\n2 3 4 1",
"output": "4 4"
},
{
"input": "2 0\n2 1",
"output": "0 0"
},
{
"input": "2 1\n2 1",
"output": "2 2"
},
{
"input": "2 2\n2 1",
"output": "2 2"
},
{
"input": "3 0\n2 3 1",
"output": "0 0"
},
{
"input": "3 1\n2 3 1",
"output": "2 2"
},
{
"input": "3 2\n2 3 1",
"output": "3 3"
},
{
"input": "3 3\n2 3 1",
"output": "3 3"
},
{
"input": "5 1\n2 3 4 5 1",
"output": "2 2"
},
{
"input": "6 3\n2 3 1 5 6 4",
"output": "3 5"
},
{
"input": "9 5\n3 4 5 2 7 8 1 9 6",
"output": "5 9"
},
{
"input": "6 2\n4 5 6 2 1 3",
"output": "2 4"
},
{
"input": "2 2\n2 1",
"output": "2 2"
},
{
"input": "9 2\n8 6 7 5 2 3 1 9 4",
"output": "3 4"
},
{
"input": "8 2\n2 3 4 5 6 7 8 1",
"output": "3 4"
},
{
"input": "9 2\n2 3 1 5 6 4 8 9 7",
"output": "3 4"
},
{
"input": "4 2\n2 1 4 3",
"output": "2 4"
},
{
"input": "28 24\n18 24 12 27 13 9 28 2 10 4 17 21 16 25 19 7 1 14 20 11 22 6 3 26 23 5 8 15",
"output": "25 28"
},
{
"input": "24 12\n23 16 1 3 15 19 14 21 18 17 5 7 20 12 24 4 8 10 9 22 6 11 13 2",
"output": "13 23"
},
{
"input": "26 21\n14 7 2 10 22 24 19 6 17 3 8 4 1 15 23 20 5 12 18 26 25 21 16 9 11 13",
"output": "22 26"
},
{
"input": "21 17\n2 3 4 13 6 7 8 9 10 11 12 5 14 15 16 17 18 19 20 21 1",
"output": "18 21"
},
{
"input": "25 4\n2 3 4 5 1 7 8 9 10 6 12 13 14 15 11 17 18 19 20 16 22 23 24 25 21",
"output": "5 8"
},
{
"input": "24 15\n2 1 4 3 7 5 6 9 8 11 10 14 12 13 17 15 16 19 18 21 20 24 22 23",
"output": "15 24"
},
{
"input": "100 36\n25 22 57 55 38 95 26 85 60 90 92 51 15 76 45 74 67 35 72 18 44 96 16 46 48 21 99 41 53 13 87 20 81 64 52 30 17 33 4 79 19 10 59 82 54 39 61 14 50 75 70 88 29 2 100 68 73 69 28 36 3 37 77 40 91 93 71 24 7 56 1 42 9 47 31 62 89 83 98 27 43 5 34 66 63 8 97 6 12 94 65 58 78 84 86 80 32 23 11 49",
"output": "37 72"
},
{
"input": "97 9\n29 67 51 27 85 54 86 38 84 7 2 93 36 81 50 32 31 55 18 77 69 66 26 80 90 10 44 96 17 45 79 87 64 8 13 3 91 12 42 19 37 68 48 30 76 47 53 43 97 94 60 78 88 92 4 39 65 15 33 73 59 23 1 74 9 24 75 40 25 52 95 20 72 34 62 22 56 61 49 21 46 6 89 71 16 63 57 58 82 70 5 35 41 14 28 11 83",
"output": "10 18"
},
{
"input": "96 30\n74 60 50 24 36 8 12 55 27 53 83 28 21 33 75 78 90 71 96 44 88 57 94 38 86 41 11 58 19 40 54 56 89 72 26 68 52 14 31 10 37 84 7 66 87 47 80 79 51 29 4 18 42 1 2 59 63 34 65 13 92 73 6 46 61 77 70 45 15 95 16 69 49 64 93 81 67 35 39 85 20 25 9 30 82 3 91 17 76 5 22 48 62 32 23 43",
"output": "31 60"
},
{
"input": "95 53\n2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 39 25 26 27 28 29 30 31 32 33 34 35 36 37 38 24 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 1",
"output": "54 95"
},
{
"input": "100 9\n2 3 4 5 6 7 8 9 10 1 12 13 14 15 16 17 18 19 20 11 22 23 24 25 26 27 28 29 30 21 32 33 34 35 36 37 38 39 40 31 42 43 44 45 46 47 48 49 50 41 52 53 54 55 56 57 58 59 60 51 62 63 64 65 66 67 68 69 70 61 72 73 74 75 76 77 78 79 80 71 82 83 84 85 86 87 88 89 90 81 92 93 94 95 96 97 98 99 100 91",
"output": "10 18"
},
{
"input": "93 89\n3 1 2 6 4 5 8 7 10 9 13 11 12 16 14 15 19 17 18 21 20 24 22 23 27 25 26 29 28 32 30 31 34 33 37 35 36 39 38 42 40 41 44 43 46 45 48 47 50 49 52 51 55 53 54 58 56 57 60 59 63 61 62 66 64 65 69 67 68 71 70 74 72 73 77 75 76 80 78 79 83 81 82 85 84 88 86 87 91 89 90 93 92",
"output": "89 93"
},
{
"input": "7 4\n2 1 4 3 6 7 5",
"output": "4 7"
}
] | 0 | 0 | -1 | 22,887 |
|
55 | Pie or die | [
"games"
] | C. Pie or die | 2 | 256 | Volodya and Vlad play the following game. There are *k* pies at the cells of *n*<=<=×<=<=*m* board. Each turn Volodya moves one pie to the neighbouring (by side) cell. If the pie lies at the border of the board then Volodya can move it outside the board, get the pie and win. After Volodya's move, Vlad bans some edge at the border of the board of length 1 (between two knots of the board) so that Volodya is not able to move the pie outside the board through this edge anymore. The question is: will Volodya win this game? We suppose both players follow the optimal strategy. | First line contains 3 integers, separated by space: 1<=≤<=*n*,<=*m*<=≤<=100 — dimensions of the board and 0<=≤<=*k*<=≤<=100 — the number of pies. Each of the next *k* lines contains 2 integers, separated by space: 1<=≤<=*x*<=≤<=*n*, 1<=≤<=*y*<=≤<=*m* — coordinates of the corresponding pie. There could be more than one pie at a cell. | Output only one word: "YES" — if Volodya wins, "NO" — otherwise. | [
"2 2 1\n1 2\n",
"3 4 0\n",
"100 50 2\n50 25\n50 25\n"
] | [
"YES",
"NO",
"NO"
] | none | [
{
"input": "2 2 1\n1 2",
"output": "YES"
},
{
"input": "3 4 0",
"output": "NO"
},
{
"input": "100 50 2\n50 25\n50 25",
"output": "NO"
},
{
"input": "20 20 4\n10 10\n10 10\n10 10\n10 10",
"output": "NO"
},
{
"input": "15 15 1\n8 8",
"output": "NO"
},
{
"input": "8 8 2\n4 4\n5 5",
"output": "YES"
},
{
"input": "100 100 2\n50 96\n51 96",
"output": "YES"
},
{
"input": "100 100 2\n50 95\n51 95",
"output": "NO"
},
{
"input": "20 20 1\n16 10",
"output": "YES"
},
{
"input": "20 20 4\n15 9\n15 10\n15 11\n15 12",
"output": "NO"
},
{
"input": "11 11 1\n6 6",
"output": "NO"
},
{
"input": "11 11 1\n6 5",
"output": "YES"
},
{
"input": "35 13 20\n13 8\n19 8\n24 7\n20 6\n23 7\n23 6\n30 7\n29 7\n7 7\n6 8\n9 8\n29 6\n20 7\n25 6\n19 6\n23 8\n26 6\n12 6\n15 7\n6 8",
"output": "NO"
},
{
"input": "50 17 27\n17 8\n19 6\n25 8\n30 10\n22 10\n30 9\n25 8\n27 6\n19 7\n29 11\n39 8\n31 8\n39 8\n40 7\n11 8\n30 11\n32 8\n31 11\n36 12\n10 11\n32 8\n8 7\n7 12\n17 11\n27 7\n8 8\n23 12",
"output": "NO"
},
{
"input": "24 29 22\n16 6\n14 22\n7 15\n11 17\n12 22\n10 13\n12 22\n12 13\n6 16\n12 21\n11 11\n9 13\n18 22\n7 20\n13 6\n6 14\n17 10\n9 13\n7 23\n14 11\n7 22\n8 12",
"output": "NO"
},
{
"input": "32 45 3\n12 30\n27 9\n14 27",
"output": "NO"
},
{
"input": "35 15 63\n6 6\n14 9\n7 6\n25 6\n25 8\n13 9\n18 7\n20 8\n30 10\n25 10\n7 7\n18 8\n11 10\n12 6\n8 8\n6 9\n21 9\n27 10\n28 8\n28 9\n7 9\n28 9\n10 10\n29 10\n25 8\n28 7\n22 6\n13 9\n14 7\n23 9\n20 8\n28 10\n22 7\n12 8\n13 7\n27 9\n17 8\n10 8\n19 10\n6 10\n26 6\n19 8\n28 9\n15 9\n14 7\n25 10\n17 8\n21 8\n29 6\n7 6\n16 10\n7 10\n25 7\n9 9\n30 9\n23 8\n28 8\n7 10\n12 6\n20 9\n24 8\n6 6\n26 7",
"output": "NO"
},
{
"input": "41 50 37\n21 24\n20 32\n10 12\n35 7\n8 19\n30 22\n21 11\n35 12\n7 8\n16 10\n13 39\n6 43\n31 12\n16 14\n25 32\n27 21\n6 34\n22 26\n7 41\n18 13\n24 19\n9 44\n36 21\n17 16\n36 24\n6 31\n19 20\n12 19\n27 36\n6 31\n11 13\n19 9\n20 12\n25 25\n18 27\n17 36\n8 16",
"output": "NO"
},
{
"input": "96 95 31\n14 23\n70 47\n11 77\n53 66\n63 87\n3 14\n57 44\n65 69\n80 74\n49 6\n57 86\n75 8\n2 32\n75 21\n14 51\n56 46\n77 6\n17 89\n87 3\n21 18\n70 67\n47 64\n13 47\n61 33\n56 30\n28 2\n65 18\n17 90\n44 77\n54 26\n32 70",
"output": "YES"
},
{
"input": "80 51 47\n67 41\n74 7\n68 41\n6 2\n19 38\n37 28\n65 4\n6 25\n39 11\n19 34\n47 36\n62 26\n27 44\n70 45\n24 33\n41 2\n13 10\n3 17\n78 35\n53 46\n62 47\n33 17\n17 49\n2 3\n47 38\n72 35\n4 8\n32 21\n52 43\n67 12\n28 22\n53 34\n36 11\n45 45\n32 12\n5 11\n6 3\n55 21\n73 4\n55 21\n36 13\n48 18\n19 8\n70 24\n43 45\n59 50\n58 7",
"output": "YES"
},
{
"input": "25 92 38\n21 36\n20 18\n9 29\n18 77\n10 58\n10 39\n5 3\n21 51\n11 78\n16 32\n24 71\n15 17\n23 23\n25 59\n18 57\n11 2\n16 35\n1 47\n20 59\n19 54\n11 55\n4 33\n15 41\n17 18\n16 67\n4 15\n5 23\n3 24\n20 70\n5 87\n11 1\n23 66\n21 83\n2 32\n17 22\n2 26\n16 42\n24 15",
"output": "YES"
},
{
"input": "67 41 68\n35 16\n66 14\n1 15\n43 6\n26 17\n30 13\n42 11\n32 20\n66 14\n15 35\n35 6\n12 11\n25 9\n39 37\n31 14\n52 11\n4 32\n17 14\n32 1\n58 31\n30 20\n7 23\n13 3\n27 25\n60 27\n56 39\n60 39\n11 5\n33 14\n29 12\n13 34\n30 16\n25 16\n64 25\n47 6\n33 36\n14 40\n19 38\n57 34\n67 8\n10 13\n7 36\n22 24\n6 33\n23 40\n13 19\n65 6\n14 37\n37 21\n27 12\n41 36\n60 15\n27 11\n23 33\n67 40\n45 39\n1 41\n50 21\n28 38\n20 24\n41 34\n43 35\n51 5\n59 37\n27 4\n28 17\n63 20\n1 9",
"output": "YES"
},
{
"input": "14 95 49\n11 48\n9 12\n1 18\n7 54\n11 20\n9 82\n12 1\n12 84\n1 13\n2 13\n12 57\n13 15\n12 18\n9 47\n13 14\n10 14\n13 94\n7 46\n14 14\n6 46\n7 95\n9 29\n13 15\n6 76\n8 60\n6 27\n9 63\n5 39\n5 70\n10 59\n5 75\n3 19\n9 32\n13 59\n5 13\n4 5\n13 80\n10 62\n13 65\n5 25\n4 81\n7 12\n10 94\n8 55\n7 61\n11 58\n7 77\n12 14\n12 47",
"output": "YES"
},
{
"input": "15 96 22\n4 7\n7 40\n13 30\n8 53\n6 78\n5 9\n15 35\n3 13\n5 31\n2 9\n13 50\n11 17\n4 2\n10 91\n11 74\n14 49\n8 30\n10 66\n12 44\n6 19\n9 62\n15 50",
"output": "YES"
},
{
"input": "19 19 50\n11 16\n4 11\n5 12\n19 19\n7 16\n15 10\n8 17\n8 1\n11 10\n5 19\n5 14\n17 6\n12 15\n18 17\n17 14\n10 5\n15 11\n8 8\n5 8\n18 18\n7 11\n8 4\n11 9\n6 16\n1 15\n19 13\n5 12\n10 10\n4 19\n12 4\n8 14\n19 9\n7 1\n19 11\n15 8\n4 19\n19 9\n6 7\n15 7\n2 16\n12 9\n3 18\n17 10\n3 5\n11 7\n12 6\n4 15\n19 4\n17 15\n3 10",
"output": "YES"
},
{
"input": "93 40 43\n14 15\n58 9\n72 15\n40 40\n46 20\n17 26\n31 26\n91 36\n24 28\n32 27\n51 10\n2 35\n73 7\n6 33\n59 21\n59 39\n33 8\n22 21\n77 20\n30 38\n76 35\n40 6\n48 31\n67 29\n30 24\n6 16\n39 27\n24 29\n14 16\n5 25\n76 14\n61 25\n85 13\n60 9\n80 7\n49 19\n35 20\n90 31\n57 40\n67 27\n3 27\n21 16\n21 38",
"output": "YES"
},
{
"input": "70 50 62\n31 22\n41 21\n31 47\n2 46\n22 8\n6 4\n45 32\n40 29\n10 11\n62 40\n70 26\n48 25\n13 44\n53 22\n3 8\n41 19\n13 8\n21 41\n66 20\n34 34\n41 48\n9 35\n23 26\n29 30\n39 27\n58 11\n35 2\n67 3\n59 23\n41 10\n54 9\n10 18\n23 44\n5 2\n37 30\n31 24\n2 21\n2 36\n34 5\n59 44\n7 4\n23 22\n47 27\n14 50\n54 50\n6 4\n64 1\n29 5\n5 37\n60 50\n58 45\n70 4\n4 46\n68 43\n62 34\n15 12\n16 2\n70 21\n59 8\n13 27\n25 41\n13 20",
"output": "YES"
},
{
"input": "61 96 15\n27 36\n19 64\n27 53\n59 63\n48 56\n55 30\n10 23\n6 79\n32 74\n7 51\n29 65\n60 16\n43 74\n40 80\n14 31",
"output": "YES"
},
{
"input": "87 50 62\n34 31\n42 21\n2 23\n20 25\n57 39\n46 26\n59 46\n29 33\n32 35\n79 41\n54 19\n65 7\n41 6\n40 23\n8 41\n2 31\n56 5\n37 33\n63 23\n79 4\n85 27\n53 38\n58 21\n16 11\n15 46\n33 39\n38 6\n27 41\n6 15\n25 47\n58 16\n28 50\n43 38\n48 20\n5 48\n31 6\n8 18\n40 10\n32 29\n44 20\n42 46\n63 21\n18 10\n28 49\n66 26\n64 28\n73 23\n16 29\n48 12\n23 21\n84 14\n10 45\n75 37\n80 3\n75 24\n31 25\n8 42\n67 22\n80 45\n8 31\n16 28\n49 34",
"output": "YES"
},
{
"input": "23 100 53\n16 63\n16 31\n8 31\n4 86\n8 43\n8 27\n21 6\n13 49\n11 54\n5 86\n1 41\n19 14\n2 98\n15 76\n6 25\n6 57\n2 45\n6 98\n10 27\n16 74\n22 72\n22 13\n22 20\n15 63\n18 17\n14 32\n14 32\n2 28\n7 46\n23 16\n20 64\n18 17\n3 69\n22 77\n2 98\n11 20\n22 17\n21 8\n19 77\n19 13\n18 25\n9 24\n18 83\n19 27\n7 37\n16 19\n9 60\n11 70\n3 30\n4 84\n9 54\n22 33\n3 22",
"output": "YES"
},
{
"input": "36 89 27\n21 66\n3 60\n11 32\n10 81\n30 31\n27 62\n11 81\n24 41\n30 6\n13 45\n34 86\n26 46\n9 62\n8 86\n17 56\n4 86\n25 36\n23 72\n18 55\n18 87\n22 67\n18 12\n19 75\n21 60\n16 49\n33 63\n26 12",
"output": "YES"
},
{
"input": "93 93 50\n7 5\n73 91\n66 55\n12 24\n82 46\n38 49\n86 72\n51 69\n17 73\n9 85\n86 69\n65 2\n40 88\n92 26\n45 80\n74 45\n4 55\n57 93\n80 70\n49 69\n29 46\n67 38\n46 12\n16 87\n62 3\n79 62\n29 45\n58 30\n48 4\n76 73\n14 68\n31 8\n49 85\n73 78\n18 7\n87 56\n82 54\n52 73\n29 71\n87 74\n75 84\n45 28\n47 57\n44 53\n21 5\n86 5\n57 51\n45 9\n93 8\n82 43",
"output": "YES"
},
{
"input": "11 38 21\n2 21\n2 28\n7 19\n9 18\n7 25\n8 4\n3 23\n2 32\n5 34\n10 36\n8 21\n4 6\n6 6\n4 35\n8 34\n10 18\n11 4\n10 2\n10 13\n4 37\n2 29",
"output": "YES"
},
{
"input": "26 11 59\n13 6\n18 6\n12 6\n18 6\n21 6\n19 6\n12 6\n7 6\n6 6\n16 6\n7 6\n9 6\n19 6\n19 6\n15 6\n16 6\n16 6\n18 6\n17 6\n8 6\n13 6\n18 6\n11 6\n21 6\n9 6\n19 6\n20 6\n8 6\n20 6\n14 6\n11 6\n18 6\n7 6\n16 6\n19 6\n6 6\n6 6\n7 6\n13 6\n9 6\n16 6\n9 6\n15 6\n12 6\n17 6\n16 6\n9 6\n11 6\n10 6\n16 6\n14 6\n15 6\n7 6\n20 6\n7 6\n8 6\n17 6\n14 6\n14 6",
"output": "NO"
},
{
"input": "30 84 35\n20 60\n23 21\n14 24\n24 72\n13 76\n25 35\n11 64\n15 57\n9 55\n14 66\n10 24\n13 68\n11 8\n19 43\n11 14\n16 26\n11 22\n10 26\n15 66\n17 65\n21 34\n7 61\n24 64\n18 16\n22 18\n12 9\n10 40\n8 24\n16 52\n10 9\n7 17\n21 78\n18 75\n10 45\n16 29",
"output": "NO"
},
{
"input": "100 77 53\n62 72\n23 51\n42 8\n66 33\n62 16\n28 53\n72 54\n71 34\n30 26\n91 28\n27 37\n81 47\n22 40\n42 23\n92 46\n36 37\n86 70\n62 22\n20 9\n46 36\n86 67\n46 61\n33 30\n68 49\n44 57\n34 7\n89 36\n48 39\n47 62\n76 56\n22 41\n7 52\n16 8\n70 50\n52 27\n27 17\n44 30\n66 44\n62 10\n95 37\n94 39\n91 68\n12 49\n85 55\n63 28\n64 15\n75 31\n93 26\n53 51\n53 55\n66 65\n38 36\n40 15",
"output": "NO"
},
{
"input": "66 94 26\n11 75\n46 72\n55 74\n34 10\n33 84\n25 11\n13 23\n27 73\n45 22\n54 34\n53 63\n28 8\n57 46\n26 78\n52 46\n32 38\n22 55\n17 71\n56 18\n9 60\n31 54\n6 84\n59 57\n60 81\n51 49\n41 77",
"output": "NO"
},
{
"input": "68 100 18\n17 85\n10 77\n59 55\n29 46\n25 74\n55 11\n37 16\n57 61\n26 11\n11 88\n19 18\n28 38\n32 12\n36 49\n32 6\n57 45\n30 6\n59 95",
"output": "NO"
},
{
"input": "28 61 4\n12 18\n21 31\n14 52\n6 36",
"output": "NO"
},
{
"input": "11 73 1\n4 67",
"output": "YES"
},
{
"input": "11 79 0",
"output": "NO"
},
{
"input": "11 23 1\n11 9",
"output": "YES"
},
{
"input": "25 11 0",
"output": "NO"
},
{
"input": "39 11 1\n18 3",
"output": "YES"
},
{
"input": "69 11 0",
"output": "NO"
},
{
"input": "18 15 45\n6 7\n7 14\n12 3\n17 1\n15 3\n7 11\n9 3\n7 11\n15 4\n8 1\n12 2\n17 7\n14 15\n2 9\n12 4\n14 9\n18 8\n2 2\n17 1\n7 9\n2 4\n16 1\n12 7\n17 10\n4 1\n18 13\n10 13\n9 12\n14 1\n1 6\n3 10\n6 2\n15 3\n4 8\n14 6\n5 14\n8 11\n8 13\n6 7\n16 9\n2 7\n17 14\n17 11\n7 9\n15 8",
"output": "YES"
},
{
"input": "16 18 70\n14 17\n16 8\n14 1\n7 1\n5 3\n7 5\n15 15\n15 2\n8 17\n12 12\n8 7\n10 16\n16 6\n14 7\n2 7\n12 4\n1 9\n6 9\n1 10\n10 13\n7 11\n2 2\n9 5\n3 10\n14 7\n4 5\n2 7\n7 16\n5 7\n7 14\n14 6\n10 16\n8 1\n4 14\n3 15\n8 11\n3 16\n12 1\n10 12\n13 3\n14 17\n5 5\n6 8\n13 10\n11 13\n3 5\n15 7\n10 3\n6 12\n13 15\n7 5\n3 8\n7 18\n6 7\n15 1\n9 6\n6 17\n11 2\n2 17\n7 16\n6 6\n2 18\n2 10\n5 16\n7 17\n3 8\n15 2\n11 11\n5 13\n16 1",
"output": "YES"
},
{
"input": "14 20 68\n6 7\n2 15\n4 6\n10 18\n6 9\n14 14\n5 18\n9 15\n5 15\n2 9\n9 13\n10 17\n4 2\n12 12\n6 19\n7 13\n10 11\n1 1\n3 16\n7 6\n8 16\n10 17\n1 13\n12 11\n13 13\n2 20\n14 12\n11 18\n10 8\n12 4\n13 7\n13 11\n1 1\n10 6\n14 17\n1 2\n11 5\n6 12\n13 2\n4 3\n8 19\n12 8\n8 7\n5 1\n2 10\n11 10\n12 19\n2 10\n8 4\n12 13\n3 15\n8 8\n5 9\n14 15\n5 19\n7 7\n1 16\n6 12\n11 18\n5 13\n1 12\n10 14\n4 5\n2 8\n3 20\n14 7\n6 3\n4 18",
"output": "YES"
},
{
"input": "19 13 83\n5 2\n12 11\n5 6\n3 11\n17 8\n10 8\n3 10\n9 10\n16 3\n15 12\n14 2\n11 8\n18 6\n15 10\n11 12\n2 1\n15 3\n16 3\n1 7\n15 7\n2 9\n11 13\n18 9\n4 7\n13 4\n7 4\n3 1\n14 8\n4 5\n5 7\n8 3\n17 2\n18 2\n16 3\n10 12\n6 2\n3 6\n5 2\n10 3\n18 9\n14 3\n3 6\n6 5\n12 8\n7 12\n2 11\n6 6\n18 6\n14 4\n3 10\n3 2\n13 3\n12 9\n2 10\n15 6\n1 5\n9 12\n6 12\n4 6\n18 3\n7 2\n9 13\n3 10\n19 13\n6 7\n5 1\n4 10\n12 13\n8 12\n15 1\n4 3\n3 8\n4 8\n3 7\n4 13\n8 7\n7 13\n2 8\n14 6\n12 1\n16 8\n9 4\n5 8",
"output": "YES"
},
{
"input": "13 19 1\n6 10",
"output": "NO"
},
{
"input": "14 17 0",
"output": "NO"
},
{
"input": "20 19 5\n7 14\n14 12\n7 12\n15 9\n12 6",
"output": "NO"
},
{
"input": "17 15 3\n10 7\n12 6\n8 6",
"output": "NO"
},
{
"input": "14 17 4\n9 9\n8 7\n8 12\n7 9",
"output": "NO"
},
{
"input": "15 11 0",
"output": "NO"
},
{
"input": "14 16 4\n6 11\n6 8\n8 6\n6 7",
"output": "NO"
},
{
"input": "16 16 0",
"output": "NO"
},
{
"input": "19 20 2\n10 14\n8 11",
"output": "NO"
},
{
"input": "13 15 1\n7 10",
"output": "NO"
},
{
"input": "11 100 4\n6 10\n6 20\n6 30\n6 80",
"output": "NO"
},
{
"input": "100 11 2\n40 6\n70 6",
"output": "NO"
},
{
"input": "100 11 5\n20 6\n30 6\n43 7\n78 6\n89 6",
"output": "YES"
},
{
"input": "20 20 5\n10 6\n6 8\n16 11\n11 11\n7 15",
"output": "YES"
},
{
"input": "30 30 5\n7 15\n24 11\n15 15\n8 24\n9 6",
"output": "NO"
}
] | 122 | 0 | 3.9695 | 22,899 |
383 | Propagating tree | [
"data structures",
"dfs and similar",
"trees"
] | null | null | Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of *n* nodes numbered from 1 to *n*, each node *i* having an initial value *a**i*. The root of the tree is node 1.
This tree has a special property: when a value *val* is added to a value of node *i*, the value -*val* is added to values of all the children of node *i*. Note that when you add value -*val* to a child of node *i*, you also add -(-*val*) to all children of the child of node *i* and so on. Look an example explanation to understand better how it works.
This tree supports two types of queries:
- "1 *x* *val*" — *val* is added to the value of node *x*; - "2 *x*" — print the current value of node *x*.
In order to help Iahub understand the tree better, you must answer *m* queries of the preceding type. | The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=200000). The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≤<=*a**i*<=≤<=1000). Each of the next *n*–1 lines contains two integers *v**i* and *u**i* (1<=≤<=*v**i*,<=*u**i*<=≤<=*n*), meaning that there is an edge between nodes *v**i* and *u**i*.
Each of the next *m* lines contains a query in the format described above. It is guaranteed that the following constraints hold for all queries: 1<=≤<=*x*<=≤<=*n*,<=1<=≤<=*val*<=≤<=1000. | For each query of type two (print the value of node *x*) you must print the answer to the query on a separate line. The queries must be answered in the order given in the input. | [
"5 5\n1 2 1 1 2\n1 2\n1 3\n2 4\n2 5\n1 2 3\n1 1 2\n2 1\n2 2\n2 4\n"
] | [
"3\n3\n0\n"
] | The values of the nodes are [1, 2, 1, 1, 2] at the beginning.
Then value 3 is added to node 2. It propagates and value -3 is added to it's sons, node 4 and node 5. Then it cannot propagate any more. So the values of the nodes are [1, 5, 1, - 2, - 1].
Then value 2 is added to node 1. It propagates and value -2 is added to it's sons, node 2 and node 3. From node 2 it propagates again, adding value 2 to it's sons, node 4 and node 5. Node 3 has no sons, so it cannot propagate from there. The values of the nodes are [3, 3, - 1, 0, 1].
You can see all the definitions about the tree at the following link: http://en.wikipedia.org/wiki/Tree_(graph_theory) | [
{
"input": "5 5\n1 2 1 1 2\n1 2\n1 3\n2 4\n2 5\n1 2 3\n1 1 2\n2 1\n2 2\n2 4",
"output": "3\n3\n0"
},
{
"input": "10 10\n137 197 856 768 825 894 86 174 218 326\n7 8\n4 7\n8 9\n7 10\n1 2\n2 4\n3 6\n3 5\n2 3\n1 9 624\n2 1\n2 4\n1 6 505\n1 8 467\n1 3 643\n2 1\n1 8 631\n2 4\n1 7 244",
"output": "137\n768\n137\n768"
},
{
"input": "10 10\n418 45 865 869 745 901 177 773 854 462\n4 8\n1 4\n3 6\n1 5\n1 10\n5 9\n1 2\n4 7\n1 3\n2 2\n1 6 246\n1 4 296\n1 2 378\n1 8 648\n2 6\n1 5 288\n1 6 981\n1 2 868\n2 7",
"output": "45\n1147\n-119"
}
] | 1,107 | 130,252,800 | 3 | 22,952 |
|
868 | El Toll Caves | [
"math"
] | null | null | The prehistoric caves of El Toll are located in Moià (Barcelona). You have heard that there is a treasure hidden in one of *n* possible spots in the caves. You assume that each of the spots has probability 1<=/<=*n* to contain a treasure.
You cannot get into the caves yourself, so you have constructed a robot that can search the caves for treasure. Each day you can instruct the robot to visit exactly *k* distinct spots in the caves. If none of these spots contain treasure, then the robot will obviously return with empty hands. However, the caves are dark, and the robot may miss the treasure even when visiting the right spot. Formally, if one of the visited spots does contain a treasure, the robot will obtain it with probability 1<=/<=2, otherwise it will return empty. Each time the robot searches the spot with the treasure, his success probability is independent of all previous tries (that is, the probability to miss the treasure after searching the right spot *x* times is 1<=/<=2*x*).
What is the expected number of days it will take to obtain the treasure if you choose optimal scheduling for the robot? Output the answer as a rational number modulo 109<=+<=7. Formally, let the answer be an irreducible fraction *P*<=/<=*Q*, then you have to output . It is guaranteed that *Q* is not divisible by 109<=+<=7. | The first line contains the number of test cases *T* (1<=≤<=*T*<=≤<=1000).
Each of the next *T* lines contains two integers *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=5·108). | For each test case output the answer in a separate line. | [
"3\n1 1\n2 1\n3 2\n"
] | [
"2\n500000007\n777777786\n"
] | In the first case the robot will repeatedly search in the only spot. The expected number of days in this case is 2. Note that in spite of the fact that we know the treasure spot from the start, the robot still has to search there until he succesfully recovers the treasure.
In the second case the answer can be shown to be equal to 7 / 2 if we search the two spots alternatively. In the third case the answer is 25 / 9. | [] | 2,000 | 7,065,600 | 0 | 23,010 |
|
776 | The Door Problem | [
"2-sat",
"dfs and similar",
"dsu",
"graphs"
] | null | null | Moriarty has trapped *n* people in *n* distinct rooms in a hotel. Some rooms are locked, others are unlocked. But, there is a condition that the people in the hotel can only escape when all the doors are unlocked at the same time. There are *m* switches. Each switch control doors of some rooms, but each door is controlled by exactly two switches.
You are given the initial configuration of the doors. Toggling any switch, that is, turning it ON when it is OFF, or turning it OFF when it is ON, toggles the condition of the doors that this switch controls. Say, we toggled switch 1, which was connected to room 1, 2 and 3 which were respectively locked, unlocked and unlocked. Then, after toggling the switch, they become unlocked, locked and locked.
You need to tell Sherlock, if there exists a way to unlock all doors at the same time. | First line of input contains two integers *n* and *m* (2<=≤<=*n*<=≤<=105, 2<=≤<=*m*<=≤<=105) — the number of rooms and the number of switches.
Next line contains *n* space-separated integers *r*1,<=*r*2,<=...,<=*r**n* (0<=≤<=*r**i*<=≤<=1) which tell the status of room doors. The *i*-th room is locked if *r**i*<==<=0, otherwise it is unlocked.
The *i*-th of next *m* lines contains an integer *x**i* (0<=≤<=*x**i*<=≤<=*n*) followed by *x**i* distinct integers separated by space, denoting the number of rooms controlled by the *i*-th switch followed by the room numbers that this switch controls. It is guaranteed that the room numbers are in the range from 1 to *n*. It is guaranteed that each door is controlled by exactly two switches. | Output "YES" without quotes, if it is possible to open all doors at the same time, otherwise output "NO" without quotes. | [
"3 3\n1 0 1\n2 1 3\n2 1 2\n2 2 3\n",
"3 3\n1 0 1\n3 1 2 3\n1 2\n2 1 3\n",
"3 3\n1 0 1\n3 1 2 3\n2 1 2\n1 3\n"
] | [
"NO",
"YES",
"NO"
] | In the second example input, the initial statuses of the doors are [1, 0, 1] (0 means locked, 1 — unlocked).
After toggling switch 3, we get [0, 0, 0] that means all doors are locked.
Then, after toggling switch 1, we get [1, 1, 1] that means all doors are unlocked.
It can be seen that for the first and for the third example inputs it is not possible to make all doors unlocked. | [
{
"input": "3 3\n1 0 1\n2 1 3\n2 1 2\n2 2 3",
"output": "NO"
},
{
"input": "3 3\n1 0 1\n3 1 2 3\n1 2\n2 1 3",
"output": "YES"
},
{
"input": "3 3\n1 0 1\n3 1 2 3\n2 1 2\n1 3",
"output": "NO"
},
{
"input": "11 10\n0 0 1 0 0 0 0 1 1 0 1\n3 2 3 11\n1 3\n2 6 7\n1 5\n1 11\n1 10\n5 4 6 8 9 10\n2 1 5\n1 7\n5 1 2 4 8 9",
"output": "NO"
},
{
"input": "10 9\n1 0 1 1 0 1 0 0 1 0\n4 2 3 9 10\n4 3 4 5 8\n2 1 6\n2 7 8\n1 7\n1 5\n1 10\n2 2 4\n3 1 6 9",
"output": "NO"
},
{
"input": "13 11\n0 1 1 0 1 0 0 0 1 0 0 1 1\n6 2 8 9 11 12 13\n3 1 3 11\n1 12\n1 7\n3 6 10 13\n3 1 3 8\n2 7 9\n1 4\n1 2\n2 5 10\n3 4 5 6",
"output": "NO"
},
{
"input": "7 6\n0 0 1 0 1 0 0\n1 7\n4 1 2 5 7\n2 4 6\n2 4 5\n3 1 3 6\n2 2 3",
"output": "NO"
},
{
"input": "2 2\n1 0\n2 1 2\n2 1 2",
"output": "NO"
},
{
"input": "2 2\n0 0\n2 1 2\n2 1 2",
"output": "YES"
},
{
"input": "4 4\n0 1 0 1\n2 1 2\n2 2 3\n2 3 4\n2 1 4",
"output": "YES"
},
{
"input": "2 2\n1 1\n2 1 2\n2 1 2",
"output": "YES"
},
{
"input": "4 4\n0 0 1 1\n2 1 3\n2 2 3\n2 1 4\n2 2 4",
"output": "YES"
},
{
"input": "4 4\n0 1 0 1\n2 1 2\n2 2 3\n2 3 4\n2 4 1",
"output": "YES"
},
{
"input": "2 3\n1 0\n1 1\n2 1 2\n1 2",
"output": "YES"
},
{
"input": "4 5\n0 0 0 1\n2 1 2\n1 1\n2 2 3\n2 3 4\n1 4",
"output": "YES"
},
{
"input": "3 6\n0 0 0\n0\n0\n0\n2 1 2\n2 2 3\n2 1 3",
"output": "NO"
},
{
"input": "3 3\n1 0 0\n2 1 2\n2 1 3\n2 2 3",
"output": "YES"
},
{
"input": "4 4\n0 0 0 0\n2 1 2\n2 1 2\n2 3 4\n2 3 4",
"output": "YES"
},
{
"input": "3 3\n0 1 0\n2 1 3\n2 1 2\n2 2 3",
"output": "YES"
},
{
"input": "3 3\n0 1 0\n2 1 3\n2 2 3\n2 1 2",
"output": "YES"
},
{
"input": "4 4\n1 1 1 0\n2 1 2\n2 1 2\n2 3 4\n2 3 4",
"output": "NO"
},
{
"input": "3 4\n1 1 0\n2 1 2\n2 1 2\n1 3\n1 3",
"output": "YES"
},
{
"input": "2 4\n0 0\n1 1\n1 1\n1 2\n1 2",
"output": "YES"
},
{
"input": "3 3\n0 0 0\n2 1 2\n2 2 3\n2 1 3",
"output": "NO"
}
] | 733 | 38,195,200 | -1 | 23,029 |
|
825 | Five-In-a-Row | [
"brute force",
"implementation"
] | null | null | Alice and Bob play 5-in-a-row game. They have a playing field of size 10<=×<=10. In turns they put either crosses or noughts, one at a time. Alice puts crosses and Bob puts noughts.
In current match they have made some turns and now it's Alice's turn. She wonders if she can put cross in such empty cell that she wins immediately.
Alice wins if some crosses in the field form line of length not smaller than 5. This line can be horizontal, vertical and diagonal. | You are given matrix 10<=×<=10 (10 lines of 10 characters each) with capital Latin letters 'X' being a cross, letters 'O' being a nought and '.' being an empty cell. The number of 'X' cells is equal to the number of 'O' cells and there is at least one of each type. There is at least one empty cell.
It is guaranteed that in the current arrangement nobody has still won. | Print 'YES' if it's possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print 'NO'. | [
"XX.XX.....\n.....OOOO.\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n",
"XXOXX.....\nOO.O......\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n"
] | [
"YES\n",
"NO\n"
] | none | [
{
"input": "O.......O.\n.....O.X..\n......O...\n....X.O...\n.O.O.....X\n.XO.....XX\n...X...X.O\n........O.\n........O.\n.X.X.....X",
"output": "NO"
},
{
"input": "....OX....\n..........\n.O..X...X.\nXXO..XO..O\nO.......X.\n...XX.....\n..O.O...OX\n.........X\n.....X..OO\n........O.",
"output": "NO"
},
{
"input": "..O..X.X..\n.O..X...O.\n........O.\n...O..O...\nX.XX....X.\n..O....O.X\n..X.X....O\n......X..X\nO.........\n..X.O...OO",
"output": "NO"
},
{
"input": "..........\n..........\n..........\n..........\n..........\nX.........\n.........X\n..........\n..O.......\n.O...X...O",
"output": "NO"
},
{
"input": ".OXXOOOXXO\nXOX.O.X.O.\nXX.X...OXX\nOOOX......\nX.OX.X.O..\nX.O...O.O.\n.OXOXOO...\nOO.XOOX...\nO..XX...XX\nXX.OXXOOXO",
"output": "YES"
},
{
"input": ".OX.XX.OOO\n..OXXOXOO.\nX..XXXOO.X\nXOX.O.OXOX\nO.O.X.XX.O\nOXXXOXXOXX\nO.OOO...XO\nO.X....OXX\nXO...XXO.O\nXOX.OOO.OX",
"output": "YES"
},
{
"input": "....X.....\n...X......\n..........\n.X........\nX.........\n..........\n..........\n..........\n..........\n......OOOO",
"output": "YES"
},
{
"input": "..........\n..........\n..........\n..........\n..........\n....X.....\n...X.....O\n.........O\n.X.......O\nX........O",
"output": "YES"
},
{
"input": "OOOO......\n..........\n..........\n..........\n..........\n..........\n......X...\n.......X..\n........X.\n.........X",
"output": "YES"
},
{
"input": "..........\n..........\n..........\n..........\n..........\n..........\n......X...\nOOOO...X..\n........X.\n.........X",
"output": "YES"
},
{
"input": "..........\n.........X\n........X.\n.......X..\n......X...\n..........\n..........\n..........\n..........\n......OOOO",
"output": "YES"
},
{
"input": "..........\n......OOO.\n..........\n..........\n..........\n.....O....\n......X...\n.......X..\n........X.\n.........X",
"output": "NO"
},
{
"input": ".........X\n........X.\n.......X..\n......X...\n..........\n..........\n..........\n..........\n..........\n......OOOO",
"output": "YES"
},
{
"input": "..........\n..........\n..........\n.....X....\n....X.....\n...X......\n.........O\n.X.......O\n.........O\n.........O",
"output": "YES"
},
{
"input": ".X........\n..........\n...X......\n....X.....\n.....X....\n..........\n..........\n..........\n..........\n......OOOO",
"output": "YES"
},
{
"input": "O.........\nOO........\nOOO.......\nOOO.......\n..........\n......O.OO\n.....OXXXX\n.....OXXXX\n.....OXXXX\n.....OXXXX",
"output": "YES"
},
{
"input": ".XX.....X.\n.X...O.X..\n.O........\n.....X....\n.X..XO.O..\n.X........\n.X.......O\n.........O\n..O.......\n..O....O.O",
"output": "YES"
},
{
"input": ".........X\n........X.\n.......X..\n..........\n.....X....\n..........\n..........\n..........\n..........\n......OOOO",
"output": "YES"
},
{
"input": "..........\n.....OOOO.\n..........\n..........\n..........\n..........\n.........X\n.........X\n.........X\n.........X",
"output": "YES"
},
{
"input": "..........\n.....OOOO.\n..........\n..........\n..........\n..........\n......X...\n.......X..\n........X.\n.........X",
"output": "YES"
},
{
"input": ".XX.....X.\n.X...O.X.X\n.O........\n.....X....\n.X..XO.O..\n.X........\n.X.......O\nO........O\n..O.......\n..O....O.O",
"output": "YES"
},
{
"input": "..........\n..........\n..........\n..........\n..........\n..O......X\n..O......X\n..O.......\n..O......X\n.........X",
"output": "YES"
},
{
"input": "..........\n..........\n..O.......\n...O......\n....O.....\n.....O....\n......X...\n.......X..\n........X.\n.........X",
"output": "NO"
},
{
"input": "OOO...O...\n.X...X.O..\n...O.XXX.O\n.O..XOX.X.\n..O.XXX.O.\n..X.OO.O..\n.OOXXOXXO.\n.OOX.OX.X.\n.XXX....XX\n.OO...OXO.",
"output": "YES"
},
{
"input": "..........\n.........O\n.........O\n.........O\n.........O\n..........\n.........X\n.........X\n.........X\n.........X",
"output": "YES"
},
{
"input": ".....OXXXX\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n......OOO.",
"output": "NO"
},
{
"input": "..........\n.....OOOO.\n.......OO.\n..........\n..........\n..........\n..........\n.......X..\n........X.\n......XXXX",
"output": "YES"
},
{
"input": "X.XX..XXXX\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\nOOO.O.O.OO",
"output": "YES"
},
{
"input": ".....OXXXX\n..........\n..........\n..........\n..........\n.....O....\nOOO...X...\nOOOO...X..\n........X.\n....X....X",
"output": "NO"
},
{
"input": "..........\n.....OOOO.\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n......XXXX",
"output": "YES"
},
{
"input": "..........\n.....OOOO.\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n...X.X.X.X",
"output": "NO"
},
{
"input": ".....O....\n....X..O.O\n...X.....O\n..X.......\n.X.......O\n..........\n..........\n..........\n..........\n.........X",
"output": "YES"
},
{
"input": "....X.....\n...X......\n..X.......\n.X........\n..........\n..........\n..........\n..........\n..........\n......OOOO",
"output": "YES"
},
{
"input": ".......XXX\nX.........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n......OOOO",
"output": "NO"
}
] | 124 | 0 | 0 | 23,074 |
|
691 | Couple Cover | [
"brute force",
"dp",
"number theory"
] | null | null | Couple Cover, a wildly popular luck-based game, is about to begin! Two players must work together to construct a rectangle. A bag with *n* balls, each with an integer written on it, is placed on the table. The first player reaches in and grabs a ball randomly (all balls have equal probability of being chosen) — the number written on this ball is the rectangle's width in meters. This ball is not returned to the bag, and the second player reaches into the bag and grabs another ball — the number written on this ball is the rectangle's height in meters. If the area of the rectangle is greater than or equal some threshold *p* square meters, the players win. Otherwise, they lose.
The organizers of the game are trying to select an appropriate value for *p* so that the probability of a couple winning is not too high and not too low, but they are slow at counting, so they have hired you to answer some questions for them. You are given a list of the numbers written on the balls, the organizers would like to know how many winning pairs of balls exist for different values of *p*. Note that two pairs are different if either the first or the second ball is different between the two in pair, and two different balls with the same number are considered different. | The input begins with a single positive integer *n* in its own line (1<=≤<=*n*<=≤<=106).
The second line contains *n* positive integers — the *i*-th number in this line is equal to *a**i* (1<=≤<=*a**i*<=≤<=3·106), the number written on the *i*-th ball.
The next line contains an integer *m* (1<=≤<=*m*<=≤<=106), the number of questions you are being asked.
Then, the following line contains *m* positive integers — the *j*-th number in this line is equal to the value of *p* (1<=≤<=*p*<=≤<=3·106) in the *j*-th question you are being asked. | For each question, print the number of winning pairs of balls that exist for the given value of *p* in the separate line. | [
"5\n4 2 6 1 3\n4\n1 3 5 8\n",
"2\n5 6\n2\n30 31\n"
] | [
"20\n18\n14\n10\n",
"2\n0\n"
] | none | [
{
"input": "5\n4 2 6 1 3\n4\n1 3 5 8",
"output": "20\n18\n14\n10"
},
{
"input": "2\n5 6\n2\n30 31",
"output": "2\n0"
},
{
"input": "2\n2000000 2000000\n1\n2000000",
"output": "2"
},
{
"input": "1\n1\n1\n5",
"output": "0"
},
{
"input": "10\n18 34 3 49 40 50 53 30 23 44\n10\n246 305 45 212 788 621 449 876 459 899",
"output": "72\n72\n90\n72\n60\n66\n70\n58\n70\n56"
}
] | 46 | 0 | 0 | 23,089 |
|
707 | Bakery | [
"graphs"
] | null | null | Masha wants to open her own bakery and bake muffins in one of the *n* cities numbered from 1 to *n*. There are *m* bidirectional roads, each of whose connects some pair of cities.
To bake muffins in her bakery, Masha needs to establish flour supply from some storage. There are only *k* storages, located in different cities numbered *a*1,<=*a*2,<=...,<=*a**k*.
Unforunately the law of the country Masha lives in prohibits opening bakery in any of the cities which has storage located in it. She can open it only in one of another *n*<=-<=*k* cities, and, of course, flour delivery should be paid — for every kilometer of path between storage and bakery Masha should pay 1 ruble.
Formally, Masha will pay *x* roubles, if she will open the bakery in some city *b* (*a**i*<=≠<=*b* for every 1<=≤<=*i*<=≤<=*k*) and choose a storage in some city *s* (*s*<==<=*a**j* for some 1<=≤<=*j*<=≤<=*k*) and *b* and *s* are connected by some path of roads of summary length *x* (if there are more than one path, Masha is able to choose which of them should be used).
Masha is very thrifty and rational. She is interested in a city, where she can open her bakery (and choose one of *k* storages and one of the paths between city with bakery and city with storage) and pay minimum possible amount of rubles for flour delivery. Please help Masha find this amount. | The first line of the input contains three integers *n*, *m* and *k* (1<=≤<=*n*,<=*m*<=≤<=105, 0<=≤<=*k*<=≤<=*n*) — the number of cities in country Masha lives in, the number of roads between them and the number of flour storages respectively.
Then *m* lines follow. Each of them contains three integers *u*, *v* and *l* (1<=≤<=*u*,<=*v*<=≤<=*n*, 1<=≤<=*l*<=≤<=109, *u*<=≠<=*v*) meaning that there is a road between cities *u* and *v* of length of *l* kilometers .
If *k*<=><=0, then the last line of the input contains *k* distinct integers *a*1,<=*a*2,<=...,<=*a**k* (1<=≤<=*a**i*<=≤<=*n*) — the number of cities having flour storage located in. If *k*<==<=0 then this line is not presented in the input. | Print the minimum possible amount of rubles Masha should pay for flour delivery in the only line.
If the bakery can not be opened (while satisfying conditions) in any of the *n* cities, print <=-<=1 in the only line. | [
"5 4 2\n1 2 5\n1 2 3\n2 3 4\n1 4 10\n1 5\n",
"3 1 1\n1 2 3\n3\n"
] | [
"3",
"-1"
] | Image illustrates the first sample case. Cities with storage located in and the road representing the answer are darkened. | [
{
"input": "5 4 2\n1 2 5\n1 2 3\n2 3 4\n1 4 10\n1 5",
"output": "3"
},
{
"input": "3 1 1\n1 2 3\n3",
"output": "-1"
},
{
"input": "2 3 1\n1 2 3\n1 2 18\n1 2 13\n2",
"output": "3"
},
{
"input": "3 7 0\n1 3 9\n1 2 5\n1 2 21\n1 2 12\n1 2 13\n2 3 19\n2 3 8",
"output": "-1"
},
{
"input": "4 13 1\n1 4 10\n1 3 6\n1 4 3\n3 4 1\n1 3 2\n1 2 15\n1 4 21\n1 4 20\n2 4 13\n1 4 7\n2 4 2\n1 2 8\n1 3 17\n1",
"output": "2"
},
{
"input": "5 7 3\n2 3 20\n1 2 10\n1 2 11\n4 5 15\n2 3 3\n1 5 19\n1 2 3\n5 3 2",
"output": "3"
},
{
"input": "6 7 4\n5 6 21\n3 6 18\n1 6 5\n4 6 4\n1 2 13\n3 4 7\n1 2 15\n6 1 3 2",
"output": "4"
},
{
"input": "7 39 2\n2 7 10\n5 6 18\n2 7 13\n4 5 11\n3 6 14\n1 2 16\n3 4 2\n1 3 13\n1 5 1\n1 2 20\n1 5 11\n1 4 14\n3 6 21\n1 2 18\n1 4 13\n2 3 4\n3 6 12\n2 5 18\n4 7 17\n1 2 3\n2 3 6\n1 2 21\n1 7 18\n4 6 13\n1 2 13\n1 7 17\n2 3 16\n5 6 5\n2 4 17\n1 2 9\n1 2 21\n4 5 9\n1 2 18\n2 6 6\n2 3 9\n1 4 7\n2 5 7\n3 7 21\n4 5 2\n6 2",
"output": "3"
},
{
"input": "8 57 3\n1 3 15\n2 3 1\n1 7 21\n1 2 8\n2 5 16\n1 6 4\n1 3 2\n3 7 17\n5 8 3\n1 3 18\n1 4 3\n1 2 1\n2 8 14\n1 4 17\n4 5 21\n2 3 6\n3 5 11\n2 8 11\n3 4 1\n1 3 9\n1 4 3\n2 3 12\n1 5 9\n2 3 15\n1 2 14\n1 2 10\n1 4 19\n5 7 7\n5 8 20\n5 8 1\n1 4 3\n4 5 8\n5 7 2\n1 2 14\n4 5 9\n6 7 2\n2 6 9\n2 6 4\n3 7 4\n3 5 11\n4 8 19\n3 7 15\n1 8 21\n6 7 11\n4 6 2\n2 3 21\n6 7 2\n6 8 4\n1 3 21\n3 4 1\n4 5 15\n4 7 21\n2 6 2\n5 6 16\n5 8 9\n2 5 6\n1 7 17\n1 4 8",
"output": "1"
},
{
"input": "350 10 39\n2 13 693\n6 31 482\n72 312 617\n183 275 782\n81 123 887\n26 120 1205\n135 185 822\n64 219 820\n74 203 874\n19 167 1422\n252 332 204 334 100 350 26 14 134 213 32 84 331 215 181 158 99 190 206 265 343 241 287 74 113 15 12 338 27 110 98 132 35 95 51 315 297 69 163",
"output": "874"
},
{
"input": "7 7 3\n1 2 1\n2 4 1\n3 4 1\n1 3 1\n5 7 2\n6 7 10\n5 6 5\n5 6 7",
"output": "-1"
},
{
"input": "7 8 3\n1 2 1\n2 4 1\n3 4 1\n1 3 1\n5 7 2\n6 7 10\n5 6 5\n2 5 31246\n5 6 7",
"output": "31246"
},
{
"input": "5 5 5\n1 2 1\n2 3 2\n3 4 3\n4 5 5\n1 5 6\n1 2 3 4 5",
"output": "-1"
},
{
"input": "10 10 3\n1 2 1000000000\n2 3 1000000000\n3 4 1000000000\n4 5 1000000000\n5 6 1000000000\n6 7 1000000000\n7 8 1000000000\n8 9 1000000000\n9 10 1000000000\n10 1 1000000000\n1 2 3",
"output": "1000000000"
},
{
"input": "2 1 1\n1 2 1000000000\n1",
"output": "1000000000"
},
{
"input": "99999 1 0\n1 2 3",
"output": "-1"
},
{
"input": "99999 1 2\n1 2 3\n2 4",
"output": "3"
},
{
"input": "2 1 1\n1 2 99999999\n1",
"output": "99999999"
},
{
"input": "2 1 1\n1 2 999999\n2",
"output": "999999"
}
] | 46 | 134,860,800 | 0 | 23,141 |
|
69 | Dot | [
"dp",
"games"
] | D. Dot | 3 | 256 | Anton and Dasha like to play different games during breaks on checkered paper. By the 11th grade they managed to play all the games of this type and asked Vova the programmer to come up with a new game. Vova suggested to them to play a game under the code name "dot" with the following rules:
- On the checkered paper a coordinate system is drawn. A dot is initially put in the position (*x*,<=*y*). - A move is shifting a dot to one of the pre-selected vectors. Also each player can once per game symmetrically reflect a dot relatively to the line *y*<==<=*x*. - Anton and Dasha take turns. Anton goes first. - The player after whose move the distance from the dot to the coordinates' origin exceeds *d*, loses.
Help them to determine the winner. | The first line of the input file contains 4 integers *x*, *y*, *n*, *d* (<=-<=200<=≤<=*x*,<=*y*<=≤<=200,<=1<=≤<=*d*<=≤<=200,<=1<=≤<=*n*<=≤<=20) — the initial coordinates of the dot, the distance *d* and the number of vectors. It is guaranteed that the initial dot is at the distance less than *d* from the origin of the coordinates. The following *n* lines each contain two non-negative numbers *x**i* and *y**i* (0<=≤<=*x**i*,<=*y**i*<=≤<=200) — the coordinates of the i-th vector. It is guaranteed that all the vectors are nonzero and different. | You should print "Anton", if the winner is Anton in case of both players play the game optimally, and "Dasha" otherwise. | [
"0 0 2 3\n1 1\n1 2\n",
"0 0 2 4\n1 1\n1 2\n"
] | [
"Anton",
"Dasha"
] | In the first test, Anton goes to the vector (1;2), and Dasha loses. In the second test Dasha with her first move shifts the dot so that its coordinates are (2;3), and Anton loses, as he has the only possible move — to reflect relatively to the line *y* = *x*. Dasha will respond to it with the same move and return the dot in position (2;3). | [
{
"input": "0 0 2 3\n1 1\n1 2",
"output": "Anton"
},
{
"input": "0 0 2 4\n1 1\n1 2",
"output": "Dasha"
},
{
"input": "0 0 5 100\n12 105\n15 59\n21 1\n27 6\n27 76",
"output": "Anton"
},
{
"input": "0 0 5 100\n16 24\n29 6\n44 24\n66 37\n102 19",
"output": "Anton"
},
{
"input": "0 0 5 100\n4 108\n5 170\n7 30\n7 101\n21 117",
"output": "Anton"
},
{
"input": "0 0 5 100\n30 9\n53 14\n84 7\n94 18\n121 16",
"output": "Anton"
},
{
"input": "0 0 5 100\n52 144\n55 58\n56 103\n98 65\n134 16",
"output": "Anton"
},
{
"input": "0 0 5 100\n17 3\n42 24\n72 22\n72 25\n120 25",
"output": "Dasha"
},
{
"input": "0 0 5 100\n21 38\n43 42\n59 29\n69 3\n84 52",
"output": "Anton"
},
{
"input": "0 0 5 100\n2 164\n23 107\n30 167\n46 178\n66 148",
"output": "Dasha"
},
{
"input": "3 -1 20 200\n2 27\n12 61\n14 76\n16 20\n19 72\n20 22\n30 27\n39 61\n42 44\n45 8\n46 23\n57 13\n62 56\n64 67\n80 30\n94 34\n94 77\n100 36\n101 13\n107 9",
"output": "Anton"
},
{
"input": "3 -1 20 200\n1 139\n8 76\n10 97\n25 99\n26 147\n29 51\n48 79\n56 164\n67 80\n71 35\n89 90\n108 16\n108 127\n127 54\n137 13\n140 156\n146 104\n160 155\n164 138\n172 102",
"output": "Anton"
},
{
"input": "3 -1 20 200\n1 28\n9 80\n20 92\n29 82\n38 65\n42 9\n50 65\n67 57\n71 60\n73 51\n78 89\n86 31\n90 39\n97 96\n104 27\n115 49\n119 59\n125 18\n132 37\n133 9",
"output": "Anton"
},
{
"input": "3 -1 20 200\n3 51\n6 75\n7 105\n8 109\n12 59\n12 90\n15 71\n17 150\n18 161\n19 106\n23 71\n26 68\n34 95\n36 47\n38 29\n38 153\n41 91\n43 128\n43 164\n44 106",
"output": "Anton"
},
{
"input": "3 -1 20 200\n19 12\n24 121\n25 32\n28 19\n28 87\n29 49\n32 88\n33 70\n37 77\n54 33\n56 27\n61 59\n67 42\n73 15\n76 40\n80 73\n83 39\n91 34\n91 112\n95 95",
"output": "Anton"
},
{
"input": "-3 -14 20 200\n6 90\n7 12\n15 24\n16 67\n26 35\n34 63\n35 48\n36 30\n48 28\n56 35\n59 91\n60 34\n76 43\n77 90\n77 95\n79 34\n87 69\n93 6\n99 10\n99 41",
"output": "Anton"
},
{
"input": "-3 -14 20 200\n5 54\n10 62\n20 43\n20 79\n21 47\n32 75\n33 48\n40 61\n44 65\n52 7\n52 28\n55 65\n55 67\n59 78\n68 52\n70 20\n71 72\n76 50\n90 100\n99 9",
"output": "Dasha"
},
{
"input": "-3 -14 20 200\n1 60\n5 47\n10 6\n14 17\n14 32\n34 93\n40 9\n43 85\n44 47\n49 59\n57 85\n68 50\n69 93\n71 42\n71 57\n73 5\n74 70\n83 41\n83 83\n89 8",
"output": "Anton"
},
{
"input": "-3 -14 20 200\n14 51\n26 54\n30 50\n38 41\n40 68\n47 12\n50 86\n63 4\n65 52\n67 83\n70 88\n71 61\n79 82\n82 53\n89 84\n90 16\n92 79\n97 37\n100 37\n100 93",
"output": "Dasha"
},
{
"input": "-3 -14 20 200\n11 24\n13 8\n14 8\n15 44\n15 54\n20 79\n24 72\n27 7\n28 6\n30 18\n46 34\n51 5\n64 83\n69 48\n78 76\n79 2\n89 43\n92 31\n94 76\n99 64",
"output": "Anton"
},
{
"input": "12 -11 20 200\n12 147\n14 181\n14 198\n33 51\n34 93\n43 29\n47 44\n56 161\n66 111\n96 119\n102 71\n117 184\n133 69\n151 189\n152 28\n173 27\n173 120\n176 12\n183 1\n188 196",
"output": "Anton"
},
{
"input": "12 -11 20 200\n6 108\n14 188\n23 60\n28 44\n35 151\n36 82\n58 49\n65 81\n97 100\n104 26\n114 143\n136 156\n139 112\n142 119\n147 184\n148 46\n149 152\n175 178\n184 85\n187 12",
"output": "Anton"
},
{
"input": "12 -11 20 200\n11 189\n12 108\n19 190\n21 27\n24 193\n26 86\n26 123\n31 180\n39 196\n107 193\n122 46\n129 103\n131 129\n132 135\n142 51\n157 22\n161 27\n195 163\n198 55\n199 64",
"output": "Anton"
},
{
"input": "12 -11 20 200\n8 176\n11 162\n25 130\n32 124\n58 175\n59 170\n61 98\n66 37\n78 5\n87 150\n94 172\n99 171\n121 11\n121 31\n124 172\n131 71\n134 190\n162 50\n182 99\n194 119",
"output": "Anton"
},
{
"input": "12 -11 20 200\n6 80\n12 62\n14 15\n16 133\n41 28\n43 47\n79 136\n90 196\n99 151\n99 187\n119 42\n121 11\n147 132\n149 166\n161 102\n174 4\n182 122\n194 50\n200 182\n200 197",
"output": "Anton"
},
{
"input": "0 0 19 27\n1 25\n11 3\n12 38\n27 52\n35 111\n36 51\n44 7\n45 106\n58 104\n63 108\n75 4\n76 84\n89 2\n89 44\n92 23\n98 66\n111 58\n113 9\n114 76",
"output": "Anton"
},
{
"input": "0 0 15 98\n5 14\n9 133\n10 128\n15 140\n17 53\n33 43\n50 15\n69 55\n74 134\n77 100\n99 82\n100 140\n102 12\n110 65\n128 110",
"output": "Anton"
},
{
"input": "0 0 19 34\n0 116\n6 11\n6 32\n9 84\n14 3\n27 85\n42 58\n46 31\n52 104\n65 83\n66 37\n68 130\n69 69\n78 7\n78 23\n81 66\n90 27\n91 39\n96 10",
"output": "Anton"
},
{
"input": "0 0 17 141\n9 30\n9 55\n11 64\n18 37\n20 94\n23 37\n23 140\n28 134\n36 43\n38 77\n50 47\n54 42\n70 32\n74 151\n85 68\n87 53\n88 91",
"output": "Anton"
},
{
"input": "0 0 17 160\n31 75\n32 149\n49 132\n54 98\n54 100\n57 48\n65 20\n67 177\n72 76\n74 25\n99 49\n105 86\n128 116\n147 176\n156 130\n160 26\n178 177",
"output": "Anton"
},
{
"input": "-100 -100 10 200\n0 1\n1 0\n1 1\n31 41\n3 4\n5 2\n1 2\n3 3\n9 8\n10 2",
"output": "Anton"
},
{
"input": "-140 -140 2 200\n1 0\n0 1",
"output": "Dasha"
},
{
"input": "-130 -130 20 200\n0 1\n1 0\n1 1\n31 41\n3 4\n5 2\n1 2\n3 3\n9 8\n10 2\n0 2\n0 3\n0 4\n0 5\n0 6\n2 0\n3 0\n4 0\n5 0\n6 0",
"output": "Anton"
},
{
"input": "-133 -133 20 200\n1 0\n0 1\n1 1\n2 0\n0 2\n2 1\n1 2\n3 0\n0 3\n3 1\n3 2\n3 3\n2 2\n2 3\n1 3\n4 0\n0 4\n4 1\n1 4\n2 4",
"output": "Anton"
},
{
"input": "-12 -34 5 200\n1 0\n2 0\n3 1\n10 3\n11 4",
"output": "Dasha"
}
] | 872 | 22,220,800 | 3.813277 | 23,160 |
0 | none | [
"none"
] | null | null | For each string *s* consisting of characters '0' and '1' one can define four integers *a*00, *a*01, *a*10 and *a*11, where *a**xy* is the number of subsequences of length 2 of the string *s* equal to the sequence {*x*,<=*y*}.
In these problem you are given four integers *a*00, *a*01, *a*10, *a*11 and have to find any non-empty string *s* that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1<=000<=000. | The only line of the input contains four non-negative integers *a*00, *a*01, *a*10 and *a*11. Each of them doesn't exceed 109. | If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1<=000<=000. | [
"1 2 3 4\n",
"1 2 2 1\n"
] | [
"Impossible\n",
"0110\n"
] | none | [
{
"input": "1 2 3 4",
"output": "Impossible"
},
{
"input": "1 2 2 1",
"output": "0110"
},
{
"input": "10 7 28 21",
"output": "011111110000"
},
{
"input": "0 0 0 0",
"output": "0"
},
{
"input": "499928010 820999488 178951395 499991253",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "49995000 11667 4308334 93096",
"output": "Impossible"
},
{
"input": "499928010 601314341 398636540 499991253",
"output": "Impossible"
},
{
"input": "0 2548 1752 650",
"output": "Impossible"
},
{
"input": "4950 53524 2876 158766",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "20946628 20410736 263003096 958497436",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111..."
},
{
"input": "49995000 1061 8939 0",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "0 0 0 45",
"output": "1111111111"
},
{
"input": "49995000 302076 4017924 93096",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "105 2 598 780",
"output": "1111111111111111111111111111111111111101100000000000000"
},
{
"input": "1 0 0 0",
"output": "00"
},
{
"input": "0 0 0 1",
"output": "11"
},
{
"input": "0 0 1 0",
"output": "10"
},
{
"input": "0 1 0 0",
"output": "01"
},
{
"input": "487577 9219238 1758432 61721604",
"output": "Impossible"
},
{
"input": "0 0 0 49995000",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "49995000 0 0 0",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "0 29083 917 449985000",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "449985000 27522 2478 0",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "49995000 49710535 50289465 49995000",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "1000000000 0 0 0",
"output": "Impossible"
},
{
"input": "0 1000000000 0 0",
"output": "Impossible"
},
{
"input": "0 0 1000000000 0",
"output": "Impossible"
},
{
"input": "0 0 0 1000000000",
"output": "Impossible"
},
{
"input": "0 1 1 1",
"output": "101"
},
{
"input": "1 1 1 0",
"output": "010"
},
{
"input": "0 10000 10000 199990000",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "1 20000 20001 200010000",
"output": "Impossible"
},
{
"input": "800020000 0 0 0",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "0 0 0 800020000",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "1 1 1 3",
"output": "Impossible"
},
{
"input": "199990000 202805432 197214568 200010000",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "449985000 449414656 450735344 450135010",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "450015000 147860287 152299718 50045010",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "76935810 186858014 185577301 450675253",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499959631 500684485 499266399 499959631",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "999961560 0 0 0",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "1000000000 1000000000 1000000000 1000000000",
"output": "Impossible"
},
{
"input": "321451234 456748672 987936461 785645414",
"output": "Impossible"
},
{
"input": "123456789 987654321 123456789 1",
"output": "Impossible"
},
{
"input": "0 0 0 999961560",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "999961560 31417 13304 0",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "0 22769 21952 999961560",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "4950 512066 487934 49995000",
"output": "0000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "49995000 479093 520907 4950",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "999961560 999983920 999983921 999961560",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "999916840 999939200 999939200 999916840",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "999872121 999894480 999894481 999872121",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "999827403 999849762 999849762 999827403",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "4950 51367 5033 158766",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "20946628 10679306 272734526 958497438",
"output": "Impossible"
},
{
"input": "49995000 584 9417 0",
"output": "Impossible"
},
{
"input": "105 14 588 780",
"output": "Impossible"
},
{
"input": "4950 15963710 22674282 11352058",
"output": "Impossible"
},
{
"input": "12497500 129337096 141847619 166302785",
"output": "Impossible"
},
{
"input": "511984000 6502095 8718005 17279400",
"output": "Impossible"
},
{
"input": "0 36 7 2",
"output": "Impossible"
},
{
"input": "496 41513 39598 43143",
"output": "Impossible"
},
{
"input": "127765 3290 752 1579",
"output": "Impossible"
},
{
"input": "93096 1351848 3613069 9687152",
"output": "Impossible"
},
{
"input": "89253 254334 736333 2741312",
"output": "Impossible"
},
{
"input": "499928010 25040 6580 2",
"output": "Impossible"
},
{
"input": "999961560 0 0 999961560",
"output": "Impossible"
},
{
"input": "999961560 0 0 0",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499959631 14240 17384 2",
"output": "Impossible"
},
{
"input": "450014999 312276555 663878 54397664",
"output": "Impossible"
},
{
"input": "999961560 69018 20424 1",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "0 43292 1429 999961560",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "999961560 7712000 4675717 38226",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "38226 6041195 6346522 999961560",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "124750 22337010 22490 999872121",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111..."
},
{
"input": "499928010 251220459 748730424 499991253",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499959631 82474061 917476823 499959631",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "45 0 100 45",
"output": "11111111110000000000"
},
{
"input": "449985000 0 900000000 449985000",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "449985000 900000000 0 449985000",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "799980000 0 400000000 49995000",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "49995000 400000000 0 799980000",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "49995000 0 400000000 799980000",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "799980000 400000000 0 49995000",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "799980000 0 400000 45",
"output": "1111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "45 400000 0 799980000",
"output": "0000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "45 0 400000 799980000",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "799980000 400000 0 45",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "199990000 300000000 300000000 449985000",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "449985000 300000000 300000000 199990000",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "312487500 250000000 375000000 312487500",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "312487500 375000000 250000000 312487500",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "647982000 324000000 648000000 364486500",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "364486500 648000000 324000000 647982000",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499264200 0 998560000 499264200",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499295800 0 998623201 499295800",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499327401 0 998686404 499327401",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499359003 0 998749609 499359003",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499390606 0 998812816 499390606",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499422210 0 998876025 499422210",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499453815 0 998939236 499453815",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499485421 0 999002449 499485421",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499517028 0 999065664 499517028",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499264200 0 998876000 499580245",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499295800 0 998939211 499611855",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499327401 0 999002424 499643466",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499359003 0 999065639 499675078",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499390606 0 999128856 499706691",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499422210 0 999192075 499738305",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499453815 0 999255296 499769920",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499485421 0 999318519 499801536",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499517028 0 999381744 499833153",
"output": "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111..."
},
{
"input": "499580245 998876000 0 499264200",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499611855 998939211 0 499295800",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499643466 999002424 0 499327401",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499675078 999065639 0 499359003",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499706691 999128856 0 499390606",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499738305 999192075 0 499422210",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499769920 999255296 0 499453815",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499801536 999318519 0 499485421",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499833153 999381744 0 499517028",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499264200 998560000 0 499264200",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499295800 998623201 0 499295800",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499327401 998686404 0 499327401",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499359003 998749609 0 499359003",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499390606 998812816 0 499390606",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499422210 998876025 0 499422210",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499453815 998939236 0 499453815",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499485421 999002449 0 499485421",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499517028 999065664 0 499517028",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "737260800 998400000 215193600 499390606",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "750761875 999750000 224866250 499359003",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "762822270 999936000 234477180 499359003",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "780697855 999729500 249063045 499359003",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "799980000 1000000000 264000000 499264200",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "750761875 999750000 224788750 499295800",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "762822270 999936000 234438120 499327401",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "780697855 999729500 249142075 499422210",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "799980000 1000000000 264160000 499390606",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "799980000 1000000000 240000000 480484500",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "799980000 264000000 1000000000 499264200",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499295800 264040000 1000000000 799980000",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "865592028 998592000 316220800 499264200",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499264200 373308800 998384000 942105528",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "499264200 396072000 994580800 968330028",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "968022000 924021000 704016000 684481500",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "968066001 704032000 686431200 499264200",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "968814171 730759419 660329019 499327401",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "0 0 0 2",
"output": "Impossible"
},
{
"input": "544483500 0 0 0",
"output": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000..."
},
{
"input": "10 0 0 10",
"output": "Impossible"
},
{
"input": "6 4 0 0",
"output": "00001"
},
{
"input": "3 0 0 0",
"output": "000"
},
{
"input": "3 10 10 0",
"output": "Impossible"
},
{
"input": "0 0 0 4",
"output": "Impossible"
},
{
"input": "6 0 0 0",
"output": "0000"
},
{
"input": "1 4 4 1",
"output": "Impossible"
},
{
"input": "10 0 0 6",
"output": "Impossible"
},
{
"input": "0 0 4 6",
"output": "11110"
},
{
"input": "3 0 0 3",
"output": "Impossible"
},
{
"input": "0 0 0 6",
"output": "1111"
},
{
"input": "0 1 1 0",
"output": "Impossible"
},
{
"input": "1 0 2 0",
"output": "100"
},
{
"input": "0 0 2 1",
"output": "110"
}
] | 31 | 0 | 0 | 23,176 |
|
225 | Unsolvable | [
"math",
"number theory"
] | null | null | Consider the following equation:
Let's find all integer *z* (*z*<=><=0), for which this equation is unsolvable in positive integers. The phrase "unsolvable in positive integers" means that there are no such positive integers *x* and *y* (*x*,<=*y*<=><=0), for which the given above equation holds.
Let's write out all such *z* in the increasing order: *z*1,<=*z*2,<=*z*3, and so on (*z**i*<=<<=*z**i*<=+<=1). Your task is: given the number *n*, find the number *z**n*. | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=40). | Print a single integer — the number *z**n* modulo 1000000007 (109<=+<=7). It is guaranteed that the answer exists. | [
"1\n",
"2\n",
"3\n"
] | [
"1",
"3",
"15"
] | none | [
{
"input": "1",
"output": "1"
},
{
"input": "2",
"output": "3"
},
{
"input": "3",
"output": "15"
},
{
"input": "4",
"output": "63"
},
{
"input": "5",
"output": "4095"
},
{
"input": "6",
"output": "65535"
},
{
"input": "7",
"output": "262143"
},
{
"input": "8",
"output": "73741816"
},
{
"input": "9",
"output": "536396503"
},
{
"input": "10",
"output": "140130950"
},
{
"input": "11",
"output": "487761805"
},
{
"input": "12",
"output": "319908070"
},
{
"input": "13",
"output": "106681874"
},
{
"input": "14",
"output": "373391776"
},
{
"input": "15",
"output": "317758023"
},
{
"input": "16",
"output": "191994803"
},
{
"input": "17",
"output": "416292236"
},
{
"input": "18",
"output": "110940209"
},
{
"input": "19",
"output": "599412198"
},
{
"input": "20",
"output": "383601260"
},
{
"input": "21",
"output": "910358878"
},
{
"input": "22",
"output": "532737550"
},
{
"input": "23",
"output": "348927936"
},
{
"input": "24",
"output": "923450985"
},
{
"input": "25",
"output": "470083777"
},
{
"input": "26",
"output": "642578561"
},
{
"input": "27",
"output": "428308066"
},
{
"input": "28",
"output": "485739298"
},
{
"input": "29",
"output": "419990027"
},
{
"input": "30",
"output": "287292016"
},
{
"input": "31",
"output": "202484167"
},
{
"input": "32",
"output": "389339971"
},
{
"input": "33",
"output": "848994100"
},
{
"input": "34",
"output": "273206869"
},
{
"input": "35",
"output": "853092282"
},
{
"input": "36",
"output": "411696552"
},
{
"input": "37",
"output": "876153853"
},
{
"input": "38",
"output": "90046024"
},
{
"input": "39",
"output": "828945523"
},
{
"input": "40",
"output": "697988359"
}
] | 248 | 0 | 0 | 23,197 |
|
732 | Cormen --- The Best Friend Of a Man | [
"dp",
"greedy"
] | null | null | Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk.
Empirically Polycarp learned that the dog needs at least *k* walks for any two consecutive days in order to feel good. For example, if *k*<==<=5 and yesterday Polycarp went for a walk with Cormen 2 times, today he has to go for a walk at least 3 times.
Polycarp analysed all his affairs over the next *n* days and made a sequence of *n* integers *a*1,<=*a*2,<=...,<=*a**n*, where *a**i* is the number of times Polycarp will walk with the dog on the *i*-th day while doing all his affairs (for example, he has to go to a shop, throw out the trash, etc.).
Help Polycarp determine the minimum number of walks he needs to do additionaly in the next *n* days so that Cormen will feel good during all the *n* days. You can assume that on the day before the first day and on the day after the *n*-th day Polycarp will go for a walk with Cormen exactly *k* times.
Write a program that will find the minumum number of additional walks and the appropriate schedule — the sequence of integers *b*1,<=*b*2,<=...,<=*b**n* (*b**i*<=≥<=*a**i*), where *b**i* means the total number of walks with the dog on the *i*-th day. | The first line contains two integers *n* and *k* (1<=≤<=*n*,<=*k*<=≤<=500) — the number of days and the minimum number of walks with Cormen for any two consecutive days.
The second line contains integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=500) — the number of walks with Cormen on the *i*-th day which Polycarp has already planned. | In the first line print the smallest number of additional walks that Polycarp should do during the next *n* days so that Cormen will feel good during all days.
In the second line print *n* integers *b*1,<=*b*2,<=...,<=*b**n*, where *b**i* — the total number of walks on the *i*-th day according to the found solutions (*a**i*<=≤<=*b**i* for all *i* from 1 to *n*). If there are multiple solutions, print any of them. | [
"3 5\n2 0 1\n",
"3 1\n0 0 0\n",
"4 6\n2 4 3 5\n"
] | [
"4\n2 3 2\n",
"1\n0 1 0\n",
"0\n2 4 3 5\n"
] | none | [
{
"input": "3 5\n2 0 1",
"output": "4\n2 3 2"
},
{
"input": "3 1\n0 0 0",
"output": "1\n0 1 0"
},
{
"input": "4 6\n2 4 3 5",
"output": "0\n2 4 3 5"
},
{
"input": "5 1\n0 0 0 0 1",
"output": "2\n0 1 0 1 1"
},
{
"input": "10 500\n164 44 238 205 373 249 87 30 239 90",
"output": "903\n164 336 238 262 373 249 251 249 251 249"
},
{
"input": "1 1\n1",
"output": "0\n1"
},
{
"input": "5 1\n0 0 0 0 0",
"output": "2\n0 1 0 1 0"
},
{
"input": "5 1\n0 0 0 0 1",
"output": "2\n0 1 0 1 1"
},
{
"input": "5 2\n0 0 0 1 0",
"output": "3\n0 2 0 2 0"
},
{
"input": "5 5\n1 4 0 0 0",
"output": "6\n1 4 1 4 1"
},
{
"input": "5 10\n1 2 1 0 1",
"output": "16\n1 9 1 9 1"
},
{
"input": "5 10\n0 1 0 1 0",
"output": "18\n0 10 0 10 0"
},
{
"input": "10 5\n0 2 3 0 0 1 0 2 3 1",
"output": "13\n0 5 3 2 3 2 3 2 3 2"
},
{
"input": "10 1\n0 0 0 0 0 0 0 0 1 0",
"output": "4\n0 1 0 1 0 1 0 1 1 0"
},
{
"input": "10 436\n13 16 45 9 10 17 5 26 10 12",
"output": "2017\n13 423 45 391 45 391 45 391 45 391"
},
{
"input": "10 438\n71 160 43 326 128 35 41 247 30 49",
"output": "1060\n71 367 71 367 128 310 128 310 128 310"
},
{
"input": "10 431\n121 24 93 59 243 147 1 254 75 168",
"output": "1036\n121 310 121 310 243 188 243 254 177 254"
},
{
"input": "10 10\n0 0 0 0 0 0 0 0 0 0",
"output": "50\n0 10 0 10 0 10 0 10 0 10"
},
{
"input": "10 10\n0 0 1 0 0 0 1 0 0 0",
"output": "48\n0 10 1 9 1 9 1 9 1 9"
},
{
"input": "10 10\n0 0 0 1 0 0 1 0 0 0",
"output": "48\n0 10 0 10 0 10 1 9 1 9"
},
{
"input": "10 10\n1 1 0 2 0 1 1 1 2 0",
"output": "41\n1 9 1 9 1 9 1 9 2 8"
},
{
"input": "10 10\n1 2 2 0 0 2 0 1 0 0",
"output": "42\n1 9 2 8 2 8 2 8 2 8"
},
{
"input": "10 10\n1 0 1 0 0 5 2 0 0 1",
"output": "40\n1 9 1 9 1 9 2 8 2 8"
},
{
"input": "10 10\n2 3 5 0 2 0 15 6 5 0",
"output": "23\n2 8 5 5 5 5 15 6 5 5"
},
{
"input": "10 10\n16 15 4 10 14 2 18 11 24 5",
"output": "0\n16 15 4 10 14 2 18 11 24 5"
},
{
"input": "100 100\n48 19 63 8 18 22 5 5 12 7 9 37 17 22 58 14 53 25 24 16 22 36 4 2 9 63 52 43 22 72 0 9 12 26 50 1 21 9 40 9 5 6 2 24 1 88 50 7 9 1 3 16 0 17 3 32 47 9 32 87 20 3 45 41 16 43 41 31 28 30 2 31 72 16 74 59 20 34 25 18 48 10 34 20 22 16 3 32 8 34 8 4 45 65 48 42 1 45 11 15",
"output": "2588\n48 52 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 37 63 63 52 48 52 72 28 72 28 72 50 50 50 50 50 50 50 50 50 50 50 88 50 50 50 50 50 50 50 50 50 50 50 50 50 87 20 80 45 55 45 55 45 55 45 55 45 55 72 28 74 59 41 59 41 59 48 52 48 52 48 52 48 52 48 52 48 52 48 65 48 52 48 52 48 52"
},
{
"input": "100 200\n28 52 65 37 1 64 13 57 44 12 37 0 9 68 17 5 28 4 2 12 8 47 7 33 1 27 50 59 9 0 4 27 31 31 49 1 35 43 36 12 5 0 49 40 19 12 39 3 41 25 19 15 57 24 3 9 4 31 42 55 11 13 1 8 0 25 34 52 47 59 74 43 36 47 2 3 1 13 56 48 42 24 4 32 12 3 33 12 14 14 84 32 1 3 8 49 9 18 43 43",
"output": "7390\n28 172 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 65 135 74 126 74 126 74 126 74 126 74 126 74 126 74 126 74 126 74 126 74 126 84 116 84 116 84 116 84 116 84 116"
},
{
"input": "100 10\n1 2 7 0 2 0 0 0 2 5 3 2 2 1 0 7 1 6 1 1 5 1 2 3 5 0 0 0 0 0 1 0 1 0 2 1 3 0 1 1 0 0 3 1 6 3 2 2 1 3 1 0 9 1 3 2 3 0 5 1 0 5 5 5 2 1 3 0 1 3 5 2 4 4 1 2 3 0 2 1 3 6 4 3 1 0 9 1 0 3 3 6 7 2 5 2 2 6 0 2",
"output": "288\n1 9 7 3 7 3 7 3 7 5 5 5 5 5 5 7 3 7 3 7 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 4 6 4 6 4 6 4 9 1 9 2 8 2 8 2 8 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 4 6 4 6 9 1 9 3 7 6 7 3 7 3 7 6 4 6"
},
{
"input": "100 500\n207 27 83 171 129 204 11 55 58 115 43 280 208 169 23 79 36 59 132 28 13 136 246 134 29 135 176 21 155 175 127 288 68 68 41 156 194 31 44 131 30 31 89 46 180 184 12 29 2 58 70 157 329 294 126 55 79 19 125 15 39 30 2 137 36 151 5 246 176 1 158 31 4 99 192 200 124 66 10 195 180 165 8 79 257 68 5 175 43 141 0 106 38 32 0 56 33 221 144 226",
"output": "14863\n207 293 207 293 207 293 207 293 207 293 207 293 208 292 208 292 208 292 208 292 208 292 246 254 246 254 246 254 246 254 246 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 212 288 329 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 206 294 257 243 257 243 257 243 257 243 257 243 257 243 257 243 257 243"
},
{
"input": "100 500\n64 140 15 221 24 106 73 30 275 97 296 55 5 30 47 199 130 44 72 170 7 204 359 40 128 117 45 192 344 112 0 11 196 78 73 53 222 93 88 151 99 283 60 71 4 87 226 46 66 74 23 89 77 60 397 181 0 101 358 54 124 155 19 218 9 140 161 130 308 85 103 85 300 128 19 108 225 136 100 54 30 24 129 245 128 88 160 120 51 154 19 129 114 32 256 30 102 207 115 49",
"output": "13634\n64 436 64 436 64 436 73 427 275 225 296 204 296 204 296 204 296 204 296 204 296 204 359 141 359 141 359 192 344 156 344 156 344 156 344 156 344 156 344 156 344 283 217 283 217 283 226 274 226 274 226 274 226 274 397 181 319 181 358 142 358 155 345 218 282 218 282 218 308 192 308 192 308 192 308 192 308 192 308 192 308 192 308 245 255 245 255 245 255 245 255 245 255 245 256 244 256 244 256 244"
},
{
"input": "1 500\n500",
"output": "0\n500"
},
{
"input": "2 1\n0 0",
"output": "1\n0 1"
},
{
"input": "1 10\n1",
"output": "0\n1"
},
{
"input": "1 4\n2",
"output": "0\n2"
},
{
"input": "1 10\n2",
"output": "0\n2"
},
{
"input": "1 10\n0",
"output": "0\n0"
},
{
"input": "1 5\n1",
"output": "0\n1"
},
{
"input": "1 2\n1",
"output": "0\n1"
},
{
"input": "1 5\n2",
"output": "0\n2"
},
{
"input": "1 3\n0",
"output": "0\n0"
},
{
"input": "1 3\n1",
"output": "0\n1"
},
{
"input": "1 5\n0",
"output": "0\n0"
},
{
"input": "1 7\n0",
"output": "0\n0"
},
{
"input": "1 7\n1",
"output": "0\n1"
},
{
"input": "1 3\n2",
"output": "0\n2"
},
{
"input": "1 5\n3",
"output": "0\n3"
},
{
"input": "1 4\n3",
"output": "0\n3"
},
{
"input": "1 6\n1",
"output": "0\n1"
},
{
"input": "1 6\n2",
"output": "0\n2"
},
{
"input": "1 500\n0",
"output": "0\n0"
},
{
"input": "3 7\n2 3 1",
"output": "3\n2 5 2"
},
{
"input": "1 10\n5",
"output": "0\n5"
},
{
"input": "5 10\n1 2 3 4 5",
"output": "10\n1 9 3 7 5"
},
{
"input": "2 6\n1 2",
"output": "3\n1 5"
},
{
"input": "1 10\n3",
"output": "0\n3"
},
{
"input": "1 6\n3",
"output": "0\n3"
},
{
"input": "1 100\n1",
"output": "0\n1"
},
{
"input": "1 7\n2",
"output": "0\n2"
},
{
"input": "2 10\n1 2",
"output": "7\n1 9"
},
{
"input": "1 9\n1",
"output": "0\n1"
}
] | 62 | 0 | 3 | 23,202 |
|
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\n5 8 7 1 9\n2 5 3",
"output": "60"
},
{
"input": "5 5\n9 1 6 2 1\n3 1 4 5 2",
"output": "144"
},
{
"input": "25 5\n66 41 91 33 86 67 38 79 49 7 77 54 29 19 22 48 63 37 11 100 8 6 47 27 26\n12 14 1 23 18",
"output": "29056"
},
{
"input": "50 10\n19098 20847 65754 94580 54808 57092 23130 15638 43645 52323 52822 65193 90139 69196 83680 70109 96772 35102 56685 6692 30738 74558 57144 24054 44447 51959 22847 18735 23534 821 5540 39948 7552 72425 23213 2770 98496 81096 84868 167 36408 26572 19351 82775 23225 35377 63193 58352 45111 60889\n8 20 32 17 11 44 39 30 36 16",
"output": "1880325120"
},
{
"input": "2 1\n19 4\n1",
"output": "23"
},
{
"input": "3 1\n65 81 6\n2",
"output": "152"
},
{
"input": "5 1\n72 32 17 46 82\n2",
"output": "434"
},
{
"input": "100 1\n9 9 72 55 14 8 55 58 35 67 3 18 73 92 41 49 15 60 18 66 9 26 97 47 43 88 71 97 19 34 48 96 79 53 8 24 69 49 12 23 77 12 21 88 66 9 29 13 61 69 54 77 41 13 4 68 37 74 7 6 29 76 55 72 89 4 78 27 29 82 18 83 12 4 32 69 89 85 66 13 92 54 38 5 26 56 17 55 29 4 17 39 29 94 3 67 85 98 21 14\n13",
"output": "8834"
},
{
"input": "25 24\n1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 1\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24",
"output": "70368752566272"
},
{
"input": "30 30\n6074511 9621540 9853685 9073323 6897794 9366449 1648254 3848902 8729661 9330982 9970280 1886362 5605123 3406494 501290 3140164 2406173 346072 1520895 441795 5271130 7576116 337766 6666108 953354 5085881 2876195 8036150 1251715 4952594\n30 5 10 28 21 18 6 13 29 23 17 24 14 25 3 27 20 26 12 2 4 11 16 15 22 7 8 19 1 9",
"output": "5352753316495360"
},
{
"input": "50 30\n6015200 8643865 4116771 6555197 304415 8580071 8414182 3089212 5684567 7595481 1272699 7127763 3309618 1410297 4349070 2027355 136702 6863504 1800751 5585842 5924142 5188269 4805201 9313209 8941399 5137060 4983630 8467668 1646260 7804684 8646497 7067118 6896291 9109696 6197162 1366002 1703718 3852639 8427694 552915 5001315 5238093 9152085 7288325 8115109 3800240 5658858 4392321 8244056 3275379\n30 25 34 8 31 50 48 19 49 26 9 24 22 6 44 14 27 43 3 28 35 10 21 17 45 12 40 47 1 33",
"output": "96888048737845248"
},
{
"input": "1 1\n1846236\n1",
"output": "1846236"
},
{
"input": "2 1\n8912260 7309391\n1",
"output": "16221651"
},
{
"input": "3 1\n9949628 37460 9989934\n3",
"output": "19977022"
},
{
"input": "5 3\n1819638 2087365 162773 9531053 130978\n3 1 4",
"output": "46997584"
},
{
"input": "10 4\n886062 1016649 67899 9999839 98342 64636 816991 263 1050987 1858\n1 9 7 4",
"output": "89995888"
},
{
"input": "10 10\n1 652210 1 1 1 1 1 1 1 1\n10 1 6 7 9 8 4 3 5 2",
"output": "333931520"
},
{
"input": "50 5\n223036 65304 301127 8945 10289 15638 260 246 68 14 23 6 3 2 8 2 1 392212 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 242747 1 1 1 243737 1 1 1 1 1 1 10000000 572890\n18 50 38 42 49",
"output": "170000000"
},
{
"input": "50 10\n1103 17631 1582 250 6933 26 14434 6 2 1 1 1 1 1 3625 1 5909 1 1 1 1 1 1 1 1 1 1 1 1 7196 14842 1 1 1 1 1 1 12053 9999991 1 10502 1 1 1 1 1 1 1 1 1\n41 15 17 1 5 31 7 38 30 39",
"output": "5129995264"
},
{
"input": "50 15\n369 139 49 15 4 5 1 1 1 365 1 1 1 1 484 165 105 1 1 1 382 105 1 1 1 72 1 1 91 96 1 1 1 1 1 133 9997031 1 1 31 1 1 1 291 558 1 1 1 464 1\n49 26 40 21 45 30 16 10 15 44 22 29 36 17 37",
"output": "163801350144"
},
{
"input": "50 18\n20 23 54 4 1 1 15 50 56 1 1 71 1 1 1 1 1 15 8 1 12 1 1 1 1 1 76 1 19 11 55 42 1 1 1 1 1 9 1 30 5 1 1 1 20 1 1 1 1 9975634\n9 18 7 45 27 32 12 41 31 8 3 30 21 19 40 38 29 50",
"output": "1307536261120"
},
{
"input": "100 1\n954110 7577191 694644 113513 467690 71415 25351 26000 37902 29150 2015 94 741 20 71 9 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10000000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n78",
"output": "20000000"
},
{
"input": "100 5\n502646 93620 4203 12132 2444 9620 6 201 4 20 10000000 1 6 9 472804 2 2 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 276285 518136 1 1 1 1 1 1 1 1 1 1 1 1 1 1 189005 1 1 1 1 1 1 1 1 1 1 1 1\n73 72 15 88 11",
"output": "170000000"
},
{
"input": "100 10\n9999984 1396 8798 4760 3138 310 840 41 37 79 45 1 7 2 1 1 1 1 11032 1 1 1 11226 1 1 1 1 1 1 1 12147 1 1 1 1 1 1 16512 1 1 1 1 1 1 1 1 1 1 1 2658 1 1 1 1 7562 1 1 1 1 6207 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3334 1 1 1 1 1 1 1310 1 1 1 1 1 1 1 1 1\n19 55 91 50 31 23 60 84 38 1",
"output": "5129991680"
},
{
"input": "100 15\n380 122 2 18 5 2 3 242 1 1 1 1 1 64 1 1 1 1 1 198 323 284 1 419 1 225 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 584 1 1 1 55 9999036 1 1 1 1 1 1 1 1 447 1 1 471 1 1 1 1 1 1 1 374 1 1 1 1 1 1 1 1 1 1 1 273 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 68 1\n22 45 49 24 26 62 70 82 21 20 59 14 99 8 50",
"output": "163834200064"
},
{
"input": "100 16\n15 18 54 132 138 1 1 45 164 1 1 1 1 1 1 1 1 1 1 1 1 9999567 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 188 1 1 1 213 1 1 27 1 1 1 1 1 1 1 1 1 1 1 200 1 140 221 1 1 1 1 1 1 1 1 132 1 78 1 1 1 1 1 243 1 1 1 1 1 1 1 1 1 1 240 1 1 290 1 34 1 1 1 1 1 1\n92 46 8 58 94 39 9 89 61 60 4 70 78 72 43 22",
"output": "327675805696"
},
{
"input": "3 1\n1 2 4\n1",
"output": "12"
},
{
"input": "3 1\n1 2 4\n2",
"output": "10"
},
{
"input": "3 1\n1 2 4\n3",
"output": "7"
},
{
"input": "2 1\n1 2\n1",
"output": "4"
},
{
"input": "2 1\n1 2\n2",
"output": "3"
},
{
"input": "3 2\n1 2 4\n1 2",
"output": "16"
},
{
"input": "3 2\n1 2 4\n3 2",
"output": "10"
},
{
"input": "3 2\n1 2 4\n3 1",
"output": "12"
},
{
"input": "3 3\n4 2 1\n1 3 2",
"output": "16"
},
{
"input": "5 4\n1 2 2 4 8\n1 2 4 5",
"output": "80"
},
{
"input": "3 2\n10 7 1000\n2 3",
"output": "2020"
},
{
"input": "4 2\n2 2 4 8\n3 4",
"output": "24"
},
{
"input": "3 2\n1 3 5\n1 3",
"output": "16"
},
{
"input": "3 2\n10 1 12\n2 3",
"output": "44"
},
{
"input": "4 2\n1 2 3 100\n2 4",
"output": "208"
},
{
"input": "3 2\n10 5 200\n2 3",
"output": "420"
},
{
"input": "3 2\n3 5 3\n2 3",
"output": "16"
},
{
"input": "3 2\n5 4 100\n2 3",
"output": "210"
},
{
"input": "5 4\n100 200 300 400 500\n1 2 3 5",
"output": "7200"
},
{
"input": "3 2\n100 200 180\n1 2",
"output": "760"
},
{
"input": "4 3\n2 5 17 4\n1 2 3",
"output": "84"
},
{
"input": "5 2\n2 2 4 7 15\n4 5",
"output": "46"
},
{
"input": "3 2\n200 100 1000\n2 3",
"output": "2400"
},
{
"input": "4 2\n2 2 2 7\n1 4",
"output": "22"
},
{
"input": "8 4\n2 2 2 2 1 2 3 9\n5 6 7 8",
"output": "136"
},
{
"input": "3 2\n2 1 5\n2 3",
"output": "14"
}
] | 62 | 0 | 0 | 23,214 |
|
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"
},
{
"input": "5 10\n4 3 3 4 3\n0 0 1 2 0 0 0 1 2 2",
"output": "9"
},
{
"input": "1 1\n536870912\n29",
"output": "1"
},
{
"input": "5 10\n4 3 3 3 4\n1 2 2 0 1 2 1 1 0 2",
"output": "7"
},
{
"input": "5 10\n5 4 4 5 4\n1 2 2 0 2 2 1 0 1 2",
"output": "8"
},
{
"input": "5 10\n5 5 5 3 3\n1 2 0 2 0 1 2 0 2 2",
"output": "8"
},
{
"input": "5 10\n3 3 3 4 3\n2 1 2 1 2 1 1 2 1 2",
"output": "5"
},
{
"input": "5 10\n4 3 4 4 4\n2 1 1 0 1 2 1 1 0 1",
"output": "9"
},
{
"input": "5 10\n4 5 5 3 3\n2 1 1 0 1 1 2 0 2 1",
"output": "8"
},
{
"input": "5 10\n4 3 4 4 3\n2 1 0 1 0 1 2 2 2 1",
"output": "8"
},
{
"input": "5 10\n4 5 5 4 5\n2 1 0 2 0 2 0 2 2 1",
"output": "9"
},
{
"input": "5 10\n5 3 4 3 3\n2 0 1 1 2 1 0 0 1 0",
"output": "9"
},
{
"input": "5 10\n4 5 5 4 5\n0 1 2 2 1 1 0 2 2 1",
"output": "9"
},
{
"input": "5 10\n5 4 5 5 4\n1 0 0 0 2 1 1 2 1 1",
"output": "10"
},
{
"input": "5 10\n5 5 5 4 4\n1 1 0 2 0 0 1 2 2 1",
"output": "10"
},
{
"input": "5 10\n5 4 5 3 3\n0 0 1 1 2 1 2 2 2 0",
"output": "8"
},
{
"input": "5 10\n5 3 5 4 4\n1 1 2 1 1 0 2 2 1 1",
"output": "8"
},
{
"input": "5 10\n4 5 5 3 3\n1 2 1 1 1 0 1 1 1 0",
"output": "9"
},
{
"input": "5 10\n3 4 5 5 4\n1 1 1 1 0 2 2 0 0 0",
"output": "10"
},
{
"input": "5 10\n4 5 3 4 4\n2 2 0 2 2 0 2 0 2 0",
"output": "8"
},
{
"input": "5 10\n4 3 4 5 5\n2 0 0 1 2 0 0 2 0 0",
"output": "10"
},
{
"input": "5 10\n5 4 5 4 5\n2 2 1 2 1 2 0 2 1 0",
"output": "8"
},
{
"input": "5 10\n4 4 4 5 5\n0 2 2 0 0 1 0 2 2 1",
"output": "9"
},
{
"input": "5 10\n5 3 5 5 5\n2 2 2 2 0 0 0 1 0 0",
"output": "10"
},
{
"input": "5 10\n5 4 3 3 4\n2 2 0 1 0 0 1 0 2 0",
"output": "9"
},
{
"input": "5 10\n5 4 5 5 3\n2 2 1 1 2 0 1 0 2 0",
"output": "9"
},
{
"input": "5 10\n5 3 3 3 3\n0 2 2 0 2 2 2 2 1 0",
"output": "5"
},
{
"input": "5 10\n4 3 5 4 4\n0 2 0 1 0 2 1 1 2 2",
"output": "8"
},
{
"input": "1 2\n128\n5 6",
"output": "2"
},
{
"input": "1 2\n2\n0 0",
"output": "2"
},
{
"input": "1 4\n4\n0 0 0 0",
"output": "4"
},
{
"input": "1 2\n9\n3 0",
"output": "2"
},
{
"input": "3 4\n1 2 3\n0 0 1 1",
"output": "4"
},
{
"input": "1 3\n8\n3 2 2",
"output": "2"
},
{
"input": "2 3\n4 5\n0 2 2",
"output": "3"
},
{
"input": "1 3\n8\n3 0 0",
"output": "2"
},
{
"input": "1 2\n4\n0 1",
"output": "2"
},
{
"input": "1 8\n8\n0 0 0 0 0 0 0 0",
"output": "8"
},
{
"input": "1 3\n2\n0 0 1",
"output": "2"
},
{
"input": "2 12\n8 8\n0 0 0 0 0 0 0 0 1 1 1 1",
"output": "12"
},
{
"input": "8 8\n1 1 1 1 1 1 8 8\n1 1 1 2 2 2 3 3",
"output": "5"
},
{
"input": "1 6\n8\n0 0 0 0 0 1",
"output": "6"
},
{
"input": "1 3\n2\n1 0 0",
"output": "2"
},
{
"input": "1 3\n4\n0 0 1",
"output": "3"
},
{
"input": "2 10\n4 4\n2 2 0 0 0 0 0 0 0 0",
"output": "8"
}
] | 1,154 | 108,236,800 | 0 | 23,282 |
|
813 | Bipartite Checking | [
"data structures",
"dsu",
"graphs"
] | null | null | You are given an undirected graph consisting of *n* vertices. Initially there are no edges in the graph. Also you are given *q* queries, each query either adds one undirected edge to the graph or removes it. After each query you have to check if the resulting graph is bipartite (that is, you can paint all vertices of the graph into two colors so that there is no edge connecting two vertices of the same color). | The first line contains two integers *n* and *q* (2<=≤<=*n*,<=*q*<=≤<=100000).
Then *q* lines follow. *i*th line contains two numbers *x**i* and *y**i* (1<=≤<=*x**i*<=<<=*y**i*<=≤<=*n*). These numbers describe *i*th query: if there is an edge between vertices *x**i* and *y**i*, then remove it, otherwise add it. | Print *q* lines. *i*th line must contain YES if the graph is bipartite after *i*th query, and NO otherwise. | [
"3 5\n2 3\n1 3\n1 2\n1 2\n1 2\n"
] | [
"YES\nYES\nNO\nYES\nNO\n"
] | none | [
{
"input": "3 5\n2 3\n1 3\n1 2\n1 2\n1 2",
"output": "YES\nYES\nNO\nYES\nNO"
},
{
"input": "5 10\n1 5\n2 5\n2 4\n1 4\n4 5\n2 4\n2 5\n1 4\n2 3\n1 2",
"output": "YES\nYES\nYES\nYES\nNO\nNO\nNO\nYES\nYES\nYES"
},
{
"input": "10 20\n1 10\n5 7\n1 2\n3 5\n3 6\n4 9\n3 4\n6 9\n4 8\n6 9\n7 8\n3 8\n7 10\n2 7\n3 7\n5 9\n6 7\n4 6\n2 10\n8 10",
"output": "YES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO"
},
{
"input": "10 30\n5 6\n5 9\n4 9\n6 7\n7 9\n3 10\n5 6\n5 7\n6 10\n2 4\n2 6\n2 5\n3 7\n1 8\n8 9\n3 4\n3 5\n1 9\n6 7\n4 8\n4 5\n1 5\n2 3\n4 10\n1 7\n2 8\n3 10\n1 7\n1 7\n3 8",
"output": "YES\nYES\nYES\nYES\nYES\nYES\nYES\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO"
},
{
"input": "10 40\n6 9\n1 5\n2 6\n2 5\n7 9\n7 9\n5 6\n5 8\n6 9\n1 7\n5 6\n1 7\n1 9\n4 5\n4 6\n6 8\n7 8\n1 8\n5 7\n1 7\n8 9\n5 6\n6 7\n1 4\n3 7\n9 10\n1 7\n4 7\n4 10\n3 8\n7 10\n3 6\n1 10\n6 10\n8 9\n8 10\n7 10\n2 5\n1 9\n3 6",
"output": "YES\nYES\nYES\nYES\nYES\nYES\nNO\nNO\nNO\nNO\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO"
},
{
"input": "30 40\n5 15\n13 16\n12 17\n19 23\n1 27\n16 25\n20 21\n6 18\n10 17\n7 13\n20 24\n4 17\n8 12\n12 25\n25 29\n4 7\n1 14\n2 21\n4 26\n2 13\n20 24\n23 24\n8 16\n16 18\n8 10\n25 28\n4 22\n11 25\n13 24\n19 22\n18 20\n22 30\n4 13\n28 29\n6 13\n18 22\n18 28\n4 20\n14 21\n5 6",
"output": "YES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO"
},
{
"input": "50 60\n7 36\n43 45\n12 17\n10 40\n30 47\n18 30\n3 9\n5 6\n13 49\n5 26\n4 20\n5 50\n27 41\n3 21\n15 43\n24 41\n6 30\n40 50\n8 13\n9 21\n2 47\n23 26\n21 22\n15 31\n28 38\n1 50\n24 35\n2 13\n4 33\n14 42\n10 28\n3 5\n18 19\n9 40\n11 21\n22 36\n6 11\n36 44\n20 35\n7 38\n9 33\n29 31\n6 14\n22 32\n27 48\n19 31\n39 47\n12 50\n8 38\n35 36\n1 43\n7 49\n10 25\n10 21\n14 15\n1 44\n8 32\n17 50\n42 45\n13 44",
"output": "YES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nYES\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO"
}
] | 46 | 204,800 | -1 | 23,292 |
|
731 | Socks | [
"dfs and similar",
"dsu",
"graphs",
"greedy"
] | null | null | Arseniy is already grown-up and independent. His mother decided to leave him alone for *m* days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes.
Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular clothes to wear on each of the days she will be absent. Arseniy's family is a bit weird so all the clothes is enumerated. For example, each of Arseniy's *n* socks is assigned a unique integer from 1 to *n*. Thus, the only thing his mother had to do was to write down two integers *l**i* and *r**i* for each of the days — the indices of socks to wear on the day *i* (obviously, *l**i* stands for the left foot and *r**i* for the right). Each sock is painted in one of *k* colors.
When mother already left Arseniy noticed that according to instruction he would wear the socks of different colors on some days. Of course, that is a terrible mistake cause by a rush. Arseniy is a smart boy, and, by some magical coincidence, he posses *k* jars with the paint — one for each of *k* colors.
Arseniy wants to repaint some of the socks in such a way, that for each of *m* days he can follow the mother's instructions and wear the socks of the same color. As he is going to be very busy these days he will have no time to change the colors of any socks so he has to finalize the colors now.
The new computer game Bota-3 was just realised and Arseniy can't wait to play it. What is the minimum number of socks that need their color to be changed in order to make it possible to follow mother's instructions and wear the socks of the same color during each of *m* days. | The first line of input contains three integers *n*, *m* and *k* (2<=≤<=*n*<=≤<=200<=000, 0<=≤<=*m*<=≤<=200<=000, 1<=≤<=*k*<=≤<=200<=000) — the number of socks, the number of days and the number of available colors respectively.
The second line contain *n* integers *c*1, *c*2, ..., *c**n* (1<=≤<=*c**i*<=≤<=*k*) — current colors of Arseniy's socks.
Each of the following *m* lines contains two integers *l**i* and *r**i* (1<=≤<=*l**i*,<=*r**i*<=≤<=*n*, *l**i*<=≠<=*r**i*) — indices of socks which Arseniy should wear during the *i*-th day. | Print one integer — the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors. | [
"3 2 3\n1 2 3\n1 2\n2 3\n",
"3 2 2\n1 1 2\n1 2\n2 1\n"
] | [
"2\n",
"0\n"
] | In the first sample, Arseniy can repaint the first and the third socks to the second color.
In the second sample, there is no need to change any colors. | [
{
"input": "3 2 3\n1 2 3\n1 2\n2 3",
"output": "2"
},
{
"input": "3 2 2\n1 1 2\n1 2\n2 1",
"output": "0"
},
{
"input": "3 3 3\n1 2 3\n1 2\n2 3\n3 1",
"output": "2"
},
{
"input": "4 2 4\n1 2 3 4\n1 2\n3 4",
"output": "2"
},
{
"input": "10 3 2\n2 1 1 2 1 1 2 1 2 2\n4 10\n9 3\n5 7",
"output": "2"
},
{
"input": "10 3 3\n2 2 1 3 1 2 1 2 2 2\n10 8\n9 6\n8 10",
"output": "0"
},
{
"input": "4 3 2\n1 1 2 2\n1 2\n3 4\n2 3",
"output": "2"
},
{
"input": "4 3 4\n1 2 3 4\n1 2\n3 4\n4 1",
"output": "3"
}
] | 2,000 | 89,907,200 | 0 | 23,325 |
|
183 | Zoo | [
"brute force",
"geometry"
] | null | null | The Zoo in the Grid Kingdom is represented by an infinite grid. The Zoo has *n* observation binoculars located at the *OX* axis. For each *i* between 1 and *n*, inclusive, there exists a single binocular located at the point with coordinates (*i*,<=0). There are *m* flamingos in the Zoo, located at points with positive coordinates. The flamingos are currently sleeping and you can assume that they don't move.
In order to get a good view over the flamingos, each of the binoculars can be independently rotated to face any angle (not necessarily integer). Then, the binocular can be used to observe all flamingos that is located at the straight line passing through the binocular at the angle it is set. In other words, you can assign each binocular a direction corresponding to any straight line passing through the binocular, and the binocular will be able to see all flamingos located on that line.
Today, some kids from the prestigious Codeforces kindergarten went on a Field Study to the Zoo. Their teacher would like to set each binocular an angle to maximize the number of flamingos that can be seen by the binocular. The teacher is very interested in the sum of these values over all binoculars. Please help him find this sum. | The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*<=≤<=106,<=1<=≤<=*m*<=≤<=250), denoting the number of binoculars and the number of flamingos, respectively.
Then *m* lines follow, the *i*-th line will contain two space-separated integers *x**i* and *y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=109), which means that the *i*-th flamingo is located at point (*x**i*,<=*y**i*).
All flamingos will be located at distinct points. | Print a single integer denoting the maximum total number of flamingos that can be seen by all the binoculars. | [
"5 5\n2 1\n4 1\n3 2\n4 3\n4 4\n"
] | [
"11\n"
] | This picture shows the answer to the example test case. | [
{
"input": "5 5\n2 1\n4 1\n3 2\n4 3\n4 4",
"output": "11"
},
{
"input": "3 3\n1 1\n2 10\n3 100",
"output": "3"
},
{
"input": "1 2\n450000001 500000000\n900000001 1000000000",
"output": "2"
},
{
"input": "3 6\n1 1\n1 2\n1 3\n2 1\n2 2\n3 1",
"output": "7"
},
{
"input": "3 3\n227495634 254204506\n454991267 508409012\n715803819 799841973",
"output": "4"
},
{
"input": "3 3\n96684705 23204141\n193369409 46408282\n217792636 52269809",
"output": "4"
},
{
"input": "1000000 2\n136395332 110293751\n568110113 459392523",
"output": "1000000"
},
{
"input": "3 3\n227495634 254204506\n454991267 508409012\n217792637 799841973",
"output": "4"
},
{
"input": "3 3\n333333334 1\n666666667 2\n1000000000 3",
"output": "5"
},
{
"input": "3 3\n333333334 1\n666666667 2\n999999999 3",
"output": "5"
},
{
"input": "3 3\n2 333333333\n3 666666666\n4 999999999",
"output": "5"
},
{
"input": "3 3\n2 333333333\n3 666666666\n4 1000000000",
"output": "4"
},
{
"input": "3 3\n2 333333333\n3 666666666\n4 999999998",
"output": "4"
},
{
"input": "1000000 2\n136395332 110293751\n568110113 459392523",
"output": "1000000"
},
{
"input": "1000000 2\n881456674 979172365\n878302062 975668042",
"output": "1000000"
},
{
"input": "3 10\n1000000000 1000000000\n1000000000 999999999\n1000000000 999999998\n1000000000 999999997\n1000000000 999999996\n1000000000 999999995\n1000000000 999999994\n1000000000 999999993\n1000000000 999999992\n1000000000 999999991",
"output": "3"
},
{
"input": "1000000 2\n194305 1024\n4388610 1023",
"output": "1000000"
},
{
"input": "4 5\n1 3\n2 2\n3 1\n4 2\n4 3",
"output": "7"
},
{
"input": "5 5\n2 1\n1 1\n3 1\n4 1\n4 4",
"output": "6"
}
] | 92 | 307,200 | 0 | 23,355 |
|
859 | Desk Disorder | [
"combinatorics",
"dfs and similar",
"dsu",
"graphs",
"trees"
] | null | null | A new set of desks just arrived, and it's about time! Things were getting quite cramped in the office. You've been put in charge of creating a new seating chart for the engineers. The desks are numbered, and you sent out a survey to the engineering team asking each engineer the number of the desk they currently sit at, and the number of the desk they would like to sit at (which may be the same as their current desk). Each engineer must either remain where they sit, or move to the desired seat they indicated in the survey. No two engineers currently sit at the same desk, nor may any two engineers sit at the same desk in the new seating arrangement.
How many seating arrangements can you create that meet the specified requirements? The answer may be very large, so compute it modulo 1000000007<==<=109<=+<=7. | Input will begin with a line containing *N* (1<=≤<=*N*<=≤<=100000), the number of engineers.
*N* lines follow, each containing exactly two integers. The *i*-th line contains the number of the current desk of the *i*-th engineer and the number of the desk the *i*-th engineer wants to move to. Desks are numbered from 1 to 2·*N*. It is guaranteed that no two engineers sit at the same desk. | Print the number of possible assignments, modulo 1000000007<==<=109<=+<=7. | [
"4\n1 5\n5 2\n3 7\n7 3\n",
"5\n1 10\n2 10\n3 10\n4 10\n5 5\n"
] | [
"6\n",
"5\n"
] | These are the possible assignments for the first example:
- 1 5 3 7 - 1 2 3 7 - 5 2 3 7 - 1 5 7 3 - 1 2 7 3 - 5 2 7 3 | [
{
"input": "4\n1 5\n5 2\n3 7\n7 3",
"output": "6"
},
{
"input": "5\n1 10\n2 10\n3 10\n4 10\n5 5",
"output": "5"
},
{
"input": "1\n1 2",
"output": "2"
},
{
"input": "30\n22 37\n12 37\n37 58\n29 57\n43 57\n57 58\n58 53\n45 4\n1 4\n4 51\n35 31\n21 31\n31 51\n51 53\n53 48\n60 55\n52 55\n55 33\n36 9\n10 9\n9 33\n33 19\n5 23\n47 23\n23 32\n50 44\n26 44\n44 32\n32 19\n19 48",
"output": "31"
},
{
"input": "50\n73 1\n65 73\n16 65\n57 65\n33 16\n34 57\n98 16\n84 98\n55 34\n64 84\n80 55\n75 64\n28 75\n20 75\n42 75\n88 42\n50 20\n48 28\n32 48\n58 88\n92 76\n76 53\n53 15\n15 1\n1 10\n10 71\n71 37\n37 95\n95 63\n63 92\n45 97\n97 51\n51 96\n96 12\n12 62\n62 31\n31 5\n5 29\n29 19\n19 49\n49 6\n6 40\n40 18\n18 22\n22 17\n17 46\n46 72\n72 82\n82 14\n14 14",
"output": "2"
},
{
"input": "10\n15 8\n8 13\n13 3\n1 4\n14 3\n11 17\n9 10\n10 18\n19 20\n17 20",
"output": "120"
},
{
"input": "4\n5 6\n6 7\n7 8\n8 5",
"output": "2"
},
{
"input": "5\n1 2\n2 3\n3 4\n4 5\n5 1",
"output": "2"
}
] | 93 | 4,710,400 | 0 | 23,503 |
|
986 | Fair | [
"graphs",
"greedy",
"number theory",
"shortest paths"
] | null | null | Some company is going to hold a fair in Byteland. There are $n$ towns in Byteland and $m$ two-way roads between towns. Of course, you can reach any town from any other town using roads.
There are $k$ types of goods produced in Byteland and every town produces only one type. To hold a fair you have to bring at least $s$ different types of goods. It costs $d(u,v)$ coins to bring goods from town $u$ to town $v$ where $d(u,v)$ is the length of the shortest path from $u$ to $v$. Length of a path is the number of roads in this path.
The organizers will cover all travel expenses but they can choose the towns to bring goods from. Now they want to calculate minimum expenses to hold a fair in each of $n$ towns. | There are $4$ integers $n$, $m$, $k$, $s$ in the first line of input ($1 \le n \le 10^{5}$, $0 \le m \le 10^{5}$, $1 \le s \le k \le min(n, 100)$) — the number of towns, the number of roads, the number of different types of goods, the number of different types of goods necessary to hold a fair.
In the next line there are $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_{i} \le k$), where $a_i$ is the type of goods produced in the $i$-th town. It is guaranteed that all integers between $1$ and $k$ occur at least once among integers $a_{i}$.
In the next $m$ lines roads are described. Each road is described by two integers $u$ $v$ ($1 \le u, v \le n$, $u \ne v$) — the towns connected by this road. It is guaranteed that there is no more than one road between every two towns. It is guaranteed that you can go from any town to any other town via roads. | Print $n$ numbers, the $i$-th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town $i$. Separate numbers with spaces. | [
"5 5 4 3\n1 2 4 3 2\n1 2\n2 3\n3 4\n4 1\n4 5\n",
"7 6 3 2\n1 2 3 3 2 2 1\n1 2\n2 3\n3 4\n2 5\n5 6\n6 7\n"
] | [
"2 2 2 2 3 \n",
"1 1 1 2 2 1 1 \n"
] | Let's look at the first sample.
To hold a fair in town $1$ you can bring goods from towns $1$ ($0$ coins), $2$ ($1$ coin) and $4$ ($1$ coin). Total numbers of coins is $2$.
Town $2$: Goods from towns $2$ ($0$), $1$ ($1$), $3$ ($1$). Sum equals $2$.
Town $3$: Goods from towns $3$ ($0$), $2$ ($1$), $4$ ($1$). Sum equals $2$.
Town $4$: Goods from towns $4$ ($0$), $1$ ($1$), $5$ ($1$). Sum equals $2$.
Town $5$: Goods from towns $5$ ($0$), $4$ ($1$), $3$ ($2$). Sum equals $3$. | [] | 2,000 | 93,900,800 | 0 | 23,512 |
|
264 | Choosing Balls | [
"dp"
] | null | null | There are *n* balls. They are arranged in a row. Each ball has a color (for convenience an integer) and an integer value. The color of the *i*-th ball is *c**i* and the value of the *i*-th ball is *v**i*.
Squirrel Liss chooses some balls and makes a new sequence without changing the relative order of the balls. She wants to maximize the value of this sequence.
The value of the sequence is defined as the sum of following values for each ball (where *a* and *b* are given constants):
- If the ball is not in the beginning of the sequence and the color of the ball is same as previous ball's color, add (the value of the ball) <=×<= *a*. - Otherwise, add (the value of the ball) <=×<= *b*.
You are given *q* queries. Each query contains two integers *a**i* and *b**i*. For each query find the maximal value of the sequence she can make when *a*<==<=*a**i* and *b*<==<=*b**i*.
Note that the new sequence can be empty, and the value of an empty sequence is defined as zero. | The first line contains two integers *n* and *q* (1<=≤<=*n*<=≤<=105; 1<=≤<=*q*<=≤<=500). The second line contains *n* integers: *v*1,<=*v*2,<=...,<=*v**n* (|*v**i*|<=≤<=105). The third line contains *n* integers: *c*1,<=*c*2,<=...,<=*c**n* (1<=≤<=*c**i*<=≤<=*n*).
The following *q* lines contain the values of the constants *a* and *b* for queries. The *i*-th of these lines contains two integers *a**i* and *b**i* (|*a**i*|,<=|*b**i*|<=≤<=105).
In each line integers are separated by single spaces. | For each query, output a line containing an integer — the answer to the query. The *i*-th line contains the answer to the *i*-th query in the input order.
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. | [
"6 3\n1 -2 3 4 0 -1\n1 2 1 2 1 1\n5 1\n-2 1\n1 0\n",
"4 1\n-3 6 -1 2\n1 2 3 1\n1 -1\n"
] | [
"20\n9\n4\n",
"5\n"
] | In the first example, to achieve the maximal value:
- In the first query, you should select 1st, 3rd, and 4th ball. - In the second query, you should select 3rd, 4th, 5th and 6th ball. - In the third query, you should select 2nd and 4th ball.
Note that there may be other ways to achieve the maximal value. | [] | 124 | 4,812,800 | 0 | 23,513 |
|
10 | Digital Root | [
"number theory"
] | C. Digital Root | 2 | 256 | Not long ago Billy came across such a problem, where there were given three natural numbers *A*, *B* and *C* from the range [1,<=*N*], and it was asked to check whether the equation *AB*<==<=*C* is correct. Recently Billy studied the concept of a digital root of a number. We should remind you that a digital root *d*(*x*) of the number *x* is the sum *s*(*x*) of all the digits of this number, if *s*(*x*)<=≤<=9, otherwise it is *d*(*s*(*x*)). For example, a digital root of the number 6543 is calculated as follows: *d*(6543)<==<=*d*(6<=+<=5<=+<=4<=+<=3)<==<=*d*(18)<==<=9. Billy has counted that the digital root of a product of numbers is equal to the digital root of the product of the factors' digital roots, i.e. *d*(*xy*)<==<=*d*(*d*(*x*)*d*(*y*)). And the following solution to the problem came to his mind: to calculate the digital roots and check if this condition is met. However, Billy has doubts that this condition is sufficient. That's why he asks you to find out the amount of test examples for the given problem such that the algorithm proposed by Billy makes mistakes. | The first line contains the only number *N* (1<=≤<=*N*<=≤<=106). | Output one number — the amount of required *A*, *B* and *C* from the range [1,<=*N*]. | [
"4\n",
"5\n"
] | [
"2\n",
"6\n"
] | For the first sample the required triples are (3, 4, 3) and (4, 3, 3). | [
{
"input": "4",
"output": "2"
},
{
"input": "5",
"output": "6"
},
{
"input": "6",
"output": "14"
},
{
"input": "7",
"output": "25"
},
{
"input": "1",
"output": "0"
},
{
"input": "8",
"output": "40"
},
{
"input": "10",
"output": "82"
},
{
"input": "15",
"output": "328"
},
{
"input": "16",
"output": "395"
},
{
"input": "20",
"output": "801"
},
{
"input": "50",
"output": "13439"
},
{
"input": "100",
"output": "110267"
},
{
"input": "127",
"output": "226374"
},
{
"input": "202",
"output": "911772"
},
{
"input": "333",
"output": "4100905"
},
{
"input": "404",
"output": "7300516"
},
{
"input": "411",
"output": "7699373"
},
{
"input": "571",
"output": "20657840"
},
{
"input": "600",
"output": "23969924"
},
{
"input": "771",
"output": "50875182"
},
{
"input": "10000",
"output": "111107314481"
},
{
"input": "20000",
"output": "888859064010"
},
{
"input": "30000",
"output": "2999966359410"
},
{
"input": "50000",
"output": "13888610595721"
},
{
"input": "99999",
"output": "111107776644397"
},
{
"input": "200000",
"output": "888885923513074"
},
{
"input": "400000",
"output": "7111099254185938"
},
{
"input": "600000",
"output": "23999973325791164"
},
{
"input": "800000",
"output": "56888794064261806"
},
{
"input": "1000000",
"output": "111111074060178115"
},
{
"input": "828282",
"output": "63138169426585853"
},
{
"input": "729761",
"output": "43181611460546198"
},
{
"input": "653451",
"output": "31002458021265725"
},
{
"input": "987572",
"output": "107019643962508968"
},
{
"input": "167590",
"output": "522999083675296"
},
{
"input": "358712",
"output": "5128538168895562"
},
{
"input": "498138",
"output": "13734280958577573"
},
{
"input": "983242",
"output": "105618162572849728"
},
{
"input": "312433",
"output": "3388650642180089"
},
{
"input": "783472",
"output": "53435215015075583"
}
] | 218 | 1,433,600 | 3.94283 | 23,582 |
388 | Fox and Card Game | [
"games",
"greedy",
"sortings"
] | null | null | Fox Ciel is playing a card game with her friend Fox Jiro. There are *n* piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game? | The first line contain an integer *n* (1<=≤<=*n*<=≤<=100). Each of the next *n* lines contains a description of the pile: the first integer in the line is *s**i* (1<=≤<=*s**i*<=≤<=100) — the number of cards in the *i*-th pile; then follow *s**i* positive integers *c*1, *c*2, ..., *c**k*, ..., *c**s**i* (1<=≤<=*c**k*<=≤<=1000) — the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. | Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. | [
"2\n1 100\n2 1 10\n",
"1\n9 2 8 6 5 9 4 7 1 3\n",
"3\n3 1 3 2\n3 5 4 6\n2 8 7\n",
"3\n3 1000 1000 1000\n6 1000 1000 1000 1000 1000 1000\n5 1000 1000 1000 1000 1000\n"
] | [
"101 10\n",
"30 15\n",
"18 18\n",
"7000 7000\n"
] | In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. | [
{
"input": "2\n1 100\n2 1 10",
"output": "101 10"
},
{
"input": "1\n9 2 8 6 5 9 4 7 1 3",
"output": "30 15"
},
{
"input": "3\n3 1 3 2\n3 5 4 6\n2 8 7",
"output": "18 18"
},
{
"input": "3\n3 1000 1000 1000\n6 1000 1000 1000 1000 1000 1000\n5 1000 1000 1000 1000 1000",
"output": "7000 7000"
},
{
"input": "1\n1 1",
"output": "1 0"
},
{
"input": "5\n1 3\n1 2\n1 8\n1 1\n1 4",
"output": "12 6"
},
{
"input": "3\n5 1 2 3 4 5\n4 1 2 3 4\n8 1 2 3 4 5 6 7 8",
"output": "19 42"
},
{
"input": "5\n5 1 1 1 1 1\n4 1 1 1 1\n3 1 1 1\n2 1 1\n1 1",
"output": "8 7"
},
{
"input": "6\n2 1 1\n2 2 2\n2 3 3\n2 4 4\n2 5 5\n2 6 6",
"output": "21 21"
},
{
"input": "2\n2 200 1\n3 1 100 2",
"output": "301 3"
},
{
"input": "2\n3 1 1000 2\n3 2 1 1",
"output": "1003 4"
},
{
"input": "4\n3 1 5 100\n3 1 5 100\n3 100 1 1\n3 100 1 1",
"output": "208 208"
}
] | 0 | 0 | -1 | 23,588 |
|
392 | Blocked Points | [
"math"
] | null | null | Imagine you have an infinite 2D plane with Cartesian coordinate system. Some of the integral points are blocked, and others are not. Two integral points *A* and *B* on the plane are 4-connected if and only if:
- the Euclidean distance between *A* and *B* is one unit and neither *A* nor *B* is blocked; - or there is some integral point *C*, such that *A* is 4-connected with *C*, and *C* is 4-connected with *B*.
Let's assume that the plane doesn't contain blocked points. Consider all the integral points of the plane whose Euclidean distance from the origin is no more than *n*, we'll name these points special. Chubby Yang wants to get the following property: no special point is 4-connected to some non-special point. To get the property she can pick some integral points of the plane and make them blocked. What is the minimum number of points she needs to pick? | The first line contains an integer *n* (0<=≤<=*n*<=≤<=4·107). | Print a single integer — the minimum number of points that should be blocked. | [
"1\n",
"2\n",
"3\n"
] | [
"4\n",
"8\n",
"16\n"
] | none | [
{
"input": "1",
"output": "4"
},
{
"input": "2",
"output": "8"
},
{
"input": "3",
"output": "16"
},
{
"input": "4",
"output": "20"
},
{
"input": "0",
"output": "1"
},
{
"input": "30426905",
"output": "172120564"
},
{
"input": "38450759",
"output": "217510336"
},
{
"input": "743404",
"output": "4205328"
},
{
"input": "3766137",
"output": "21304488"
},
{
"input": "19863843",
"output": "112366864"
},
{
"input": "24562258",
"output": "138945112"
},
{
"input": "24483528",
"output": "138499748"
},
{
"input": "25329968",
"output": "143287936"
},
{
"input": "31975828",
"output": "180882596"
},
{
"input": "2346673",
"output": "13274784"
},
{
"input": "17082858",
"output": "96635236"
},
{
"input": "22578061",
"output": "127720800"
},
{
"input": "17464436",
"output": "98793768"
},
{
"input": "18855321",
"output": "106661800"
},
{
"input": "614109",
"output": "3473924"
},
{
"input": "3107977",
"output": "17581372"
},
{
"input": "39268638",
"output": "222136960"
},
{
"input": "31416948",
"output": "177721092"
},
{
"input": "34609610",
"output": "195781516"
},
{
"input": "17590047",
"output": "99504332"
},
{
"input": "12823666",
"output": "72541608"
},
{
"input": "34714265",
"output": "196373536"
},
{
"input": "2870141",
"output": "16235968"
},
{
"input": "15012490",
"output": "84923464"
},
{
"input": "31988776",
"output": "180955840"
},
{
"input": "1059264",
"output": "5992100"
},
{
"input": "5626785",
"output": "31829900"
},
{
"input": "33146037",
"output": "187502300"
},
{
"input": "17",
"output": "96"
},
{
"input": "40000000",
"output": "226274168"
},
{
"input": "5",
"output": "28"
},
{
"input": "6",
"output": "32"
},
{
"input": "7",
"output": "36"
},
{
"input": "8",
"output": "44"
},
{
"input": "9",
"output": "48"
},
{
"input": "10",
"output": "56"
},
{
"input": "11",
"output": "60"
},
{
"input": "12",
"output": "64"
},
{
"input": "13",
"output": "72"
},
{
"input": "14",
"output": "76"
},
{
"input": "15",
"output": "84"
},
{
"input": "16",
"output": "88"
},
{
"input": "25",
"output": "140"
},
{
"input": "39999999",
"output": "226274164"
},
{
"input": "39999998",
"output": "226274156"
},
{
"input": "39999997",
"output": "226274152"
},
{
"input": "39999996",
"output": "226274144"
},
{
"input": "39099999",
"output": "221182992"
},
{
"input": "46340",
"output": "262136"
},
{
"input": "46341",
"output": "262144"
},
{
"input": "395938",
"output": "2239760"
}
] | 109 | 0 | 0 | 23,613 |
|
215 | Periodical Numbers | [
"combinatorics",
"dp",
"number theory"
] | null | null | A non-empty string *s* is called binary, if it consists only of characters "0" and "1". Let's number the characters of binary string *s* from 1 to the string's length and let's denote the *i*-th character in string *s* as *s**i*.
Binary string *s* with length *n* is periodical, if there is an integer 1<=≤<=*k*<=<<=*n* such that:
- *k* is a divisor of number *n* - for all 1<=≤<=*i*<=≤<=*n*<=-<=*k*, the following condition fulfills: *s**i*<==<=*s**i*<=+<=*k*
For example, binary strings "101010" and "11" are periodical and "10" and "10010" are not.
A positive integer *x* is periodical, if its binary representation (without leading zeroes) is a periodic string.
Your task is to calculate, how many periodic numbers are in the interval from *l* to *r* (both ends are included). | The single input line contains two integers *l* and *r* (1<=≤<=*l*<=≤<=*r*<=≤<=1018). The numbers are separated by a space.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | Print a single integer, showing how many periodic numbers are in the interval from *l* to *r* (both ends are included). | [
"1 10\n",
"25 38\n"
] | [
"3\n",
"2\n"
] | In the first sample periodic numbers are 3, 7 and 10.
In the second sample periodic numbers are 31 and 36. | [
{
"input": "1 10",
"output": "3"
},
{
"input": "25 38",
"output": "2"
},
{
"input": "7 9",
"output": "1"
},
{
"input": "12 20",
"output": "1"
},
{
"input": "7 49",
"output": "7"
},
{
"input": "28 97",
"output": "6"
},
{
"input": "65 72",
"output": "0"
},
{
"input": "49 116",
"output": "2"
},
{
"input": "883 947",
"output": "2"
},
{
"input": "1 1000000000000000000",
"output": "932234960"
},
{
"input": "6967967 165556564",
"output": "8453"
},
{
"input": "74 99",
"output": "0"
},
{
"input": "883 947",
"output": "2"
},
{
"input": "3165 7131",
"output": "18"
},
{
"input": "31958 81314",
"output": "129"
},
{
"input": "134285 645597",
"output": "398"
},
{
"input": "7404389 8927556",
"output": "141"
},
{
"input": "29906716 35911991",
"output": "292"
},
{
"input": "168183636 812682195",
"output": "14809"
},
{
"input": "856218974 3052460231",
"output": "20661"
},
{
"input": "29374454626 62592774235",
"output": "109379"
},
{
"input": "288565475053 662099878640",
"output": "111059"
},
{
"input": "2812400704972 4018154546667",
"output": "579418"
},
{
"input": "46017661651072 51016144673308",
"output": "595866"
},
{
"input": "208170109961052 582944028089477",
"output": "4982263"
},
{
"input": "4644682781404278 9958408561221547",
"output": "7100878"
},
{
"input": "10339710010772692 73118299589861909",
"output": "191515094"
},
{
"input": "301180038343176710 553123999745170565",
"output": "0"
},
{
"input": "580416 34018624576",
"output": "132608"
},
{
"input": "55795878 9393826256809",
"output": "2249141"
},
{
"input": "72008 90484456049439993",
"output": "268766132"
},
{
"input": "281 195999930590991",
"output": "11728514"
},
{
"input": "2316699 57740665489369",
"output": "6915162"
},
{
"input": "39 62829",
"output": "275"
},
{
"input": "6937109 214001924643",
"output": "410178"
},
{
"input": "41751157 81619803901641136",
"output": "268728801"
},
{
"input": "94889574706765 302945996536537188",
"output": "528975174"
},
{
"input": "844 773163",
"output": "790"
},
{
"input": "39 62829",
"output": "275"
},
{
"input": "4 63489939",
"output": "8027"
},
{
"input": "4941728 1044573059727",
"output": "1002391"
},
{
"input": "1870 77928372376127898",
"output": "268720705"
},
{
"input": "9209325 530298582",
"output": "14524"
},
{
"input": "4173501391 7904052815",
"output": "2717"
},
{
"input": "680405 494044979780664129",
"output": "537396259"
},
{
"input": "59 8401",
"output": "73"
},
{
"input": "815041 1158613950585175",
"output": "33621881"
}
] | 92 | 0 | -1 | 23,615 |
|
814 | An overnight dance in discotheque | [
"dfs and similar",
"dp",
"geometry",
"greedy",
"trees"
] | null | null | The crowdedness of the discotheque would never stop our friends from having fun, but a bit more spaciousness won't hurt, will it?
The discotheque can be seen as an infinite *xy*-plane, in which there are a total of *n* dancers. Once someone starts moving around, they will move only inside their own movement range, which is a circular area *C**i* described by a center (*x**i*,<=*y**i*) and a radius *r**i*. No two ranges' borders have more than one common point, that is for every pair (*i*,<=*j*) (1<=≤<=*i*<=<<=*j*<=≤<=*n*) either ranges *C**i* and *C**j* are disjoint, or one of them is a subset of the other. Note that it's possible that two ranges' borders share a single common point, but no two dancers have exactly the same ranges.
Tsukihi, being one of them, defines the spaciousness to be the area covered by an odd number of movement ranges of dancers who are moving. An example is shown below, with shaded regions representing the spaciousness if everyone moves at the same time.
But no one keeps moving for the whole night after all, so the whole night's time is divided into two halves — before midnight and after midnight. Every dancer moves around in one half, while sitting down with friends in the other. The spaciousness of two halves are calculated separately and their sum should, of course, be as large as possible. The following figure shows an optimal solution to the example above.
By different plans of who dances in the first half and who does in the other, different sums of spaciousness over two halves are achieved. You are to find the largest achievable value of this sum. | The first line of input contains a positive integer *n* (1<=≤<=*n*<=≤<=1<=000) — the number of dancers.
The following *n* lines each describes a dancer: the *i*-th line among them contains three space-separated integers *x**i*, *y**i* and *r**i* (<=-<=106<=≤<=*x**i*,<=*y**i*<=≤<=106, 1<=≤<=*r**i*<=≤<=106), describing a circular movement range centered at (*x**i*,<=*y**i*) with radius *r**i*. | Output one decimal number — the largest achievable sum of spaciousness over two halves of the night.
The output is considered correct if it has a relative or absolute error of at most 10<=-<=9. Formally, let your answer be *a*, and the jury's answer be *b*. Your answer is considered correct if . | [
"5\n2 1 6\n0 4 1\n2 -1 3\n1 -2 1\n4 -1 1\n",
"8\n0 0 1\n0 0 2\n0 0 3\n0 0 4\n0 0 5\n0 0 6\n0 0 7\n0 0 8\n"
] | [
"138.23007676\n",
"289.02652413\n"
] | The first sample corresponds to the illustrations in the legend. | [
{
"input": "5\n2 1 6\n0 4 1\n2 -1 3\n1 -2 1\n4 -1 1",
"output": "138.23007676"
},
{
"input": "8\n0 0 1\n0 0 2\n0 0 3\n0 0 4\n0 0 5\n0 0 6\n0 0 7\n0 0 8",
"output": "289.02652413"
},
{
"input": "4\n1000000 -1000000 2\n1000000 -1000000 3\n-1000000 1000000 2\n-1000000 1000000 1000000",
"output": "3141592653643.20020000"
},
{
"input": "15\n-848 0 848\n-758 0 758\n-442 0 442\n-372 0 372\n-358 0 358\n-355 0 355\n-325 0 325\n-216 0 216\n-74 0 74\n-14 0 14\n-13 0 13\n51 0 51\n225 0 225\n272 0 272\n664 0 664",
"output": "5142746.33322199"
},
{
"input": "1\n72989 14397 49999",
"output": "7853667477.85071660"
},
{
"input": "2\n281573 0 281573\n706546 0 706546",
"output": "1817381833095.13090000"
},
{
"input": "2\n425988 -763572 27398\n425988 -763572 394103",
"output": "490301532522.57819000"
},
{
"input": "4\n-1000000 -1000000 1000000\n-1000000 1000000 1000000\n1000000 -1000000 1000000\n1000000 1000000 1000000",
"output": "12566370614359.17200000"
},
{
"input": "20\n-961747 0 961747\n-957138 0 957138\n-921232 0 921232\n-887450 0 887450\n-859109 0 859109\n-686787 0 686787\n-664613 0 664613\n-625553 0 625553\n-464803 0 464803\n-422784 0 422784\n-49107 0 49107\n-37424 0 37424\n134718 0 134718\n178903 0 178903\n304415 0 304415\n335362 0 335362\n365052 0 365052\n670652 0 670652\n812251 0 812251\n986665 0 986665",
"output": "8507336011516.24610000"
},
{
"input": "2\n-1000000 1000000 1000000\n1000000 -1000000 1000000",
"output": "6283185307179.58590000"
}
] | 920 | 6,144,000 | 3 | 23,684 |
|
301 | Yaroslav and Divisors | [
"data structures"
] | null | null | Yaroslav has an array *p*<==<=*p*1,<=*p*2,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=*n*), consisting of *n* distinct integers. Also, he has *m* queries:
- Query number *i* is represented as a pair of integers *l**i*, *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*). - The answer to the query *l**i*,<=*r**i* is the number of pairs of integers *q*, *w* (*l**i*<=≤<=*q*,<=*w*<=≤<=*r**i*) such that *p**q* is the divisor of *p**w*.
Help Yaroslav, answer all his queries. | The first line contains the integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=2·105). The second line contains *n* distinct integers *p*1,<=*p*2,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=*n*). The following *m* lines contain Yaroslav's queries. The *i*-th line contains integers *l**i*,<=*r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*). | Print *m* integers — the answers to Yaroslav's queries in the order they appear in the input.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"1 1\n1\n1 1\n",
"10 9\n1 2 3 4 5 6 7 8 9 10\n1 10\n2 9\n3 8\n4 7\n5 6\n2 2\n9 10\n5 10\n4 10\n"
] | [
"1\n",
"27\n14\n8\n4\n2\n1\n2\n7\n9\n"
] | none | [] | 248 | 0 | 0 | 23,692 |
|
190 | Counter Attack | [
"data structures",
"dsu",
"graphs",
"hashing",
"sortings"
] | null | null | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has *n* cities, numbered from 1 to *n*, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. | The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*<=≤<=5·105,<=0<=≤<=*m*<=≤<=106) — the number of cities and the number of roads marked on the flatland map, correspondingly.
Next *m* lines contain descriptions of the cities on the map. The *i*-th line contains two integers *a**i* and *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*,<=*a**i*<=≠<=*b**i*) — the numbers of cities that are connected by the *i*-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once. | On the first line print number *k* — the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following *k* lines first print *t**i* (1<=≤<=*t**i*<=≤<=*n*) — the number of vertexes in the *i*-th group. Then print space-separated numbers of cities in the *i*-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum *t**i* for all *k* groups must equal *n*. | [
"4 4\n1 2\n1 3\n4 2\n4 3\n",
"3 1\n1 2\n"
] | [
"2\n2 1 4 \n2 2 3 \n",
"1\n3 1 2 3 \n"
] | In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | [
{
"input": "4 4\n1 2\n1 3\n4 2\n4 3",
"output": "2\n2 1 4 \n2 2 3 "
},
{
"input": "3 1\n1 2",
"output": "1\n3 1 2 3 "
},
{
"input": "8 14\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n5 6\n6 7",
"output": "2\n2 1 2 \n6 3 4 5 6 7 8 "
},
{
"input": "6 9\n1 4\n1 5\n1 6\n2 4\n2 5\n2 6\n3 4\n3 5\n3 6",
"output": "2\n3 1 2 3 \n3 4 5 6 "
},
{
"input": "4 6\n3 4\n2 3\n2 4\n1 3\n2 1\n4 1",
"output": "4\n1 1 \n1 2 \n1 3 \n1 4 "
},
{
"input": "4 4\n2 3\n1 2\n3 4\n1 3",
"output": "2\n1 3 \n3 1 2 4 "
},
{
"input": "5 8\n5 1\n5 2\n5 3\n3 1\n1 4\n4 2\n3 2\n5 4",
"output": "3\n2 1 2 \n2 3 4 \n1 5 "
},
{
"input": "5 10\n3 5\n5 1\n1 3\n1 4\n2 3\n4 5\n4 3\n2 4\n2 1\n5 2",
"output": "5\n1 1 \n1 2 \n1 3 \n1 4 \n1 5 "
},
{
"input": "100000 0",
"output": "1\n100000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 1..."
},
{
"input": "100000 15\n27289 90938\n5080 32762\n12203 86803\n27118 17073\n27958 9409\n94031 28265\n80805 28920\n42943 9112\n60485 7552\n13666 57510\n68452 61810\n96704 97517\n73523 28376\n7364 47737\n28037 87216",
"output": "1\n100000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 1..."
},
{
"input": "100 0",
"output": "1\n100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 "
},
{
"input": "1 0",
"output": "1\n1 1 "
},
{
"input": "2 0",
"output": "1\n2 1 2 "
},
{
"input": "2 1\n1 2",
"output": "2\n1 1 \n1 2 "
},
{
"input": "3 2\n1 2\n1 3",
"output": "2\n1 1 \n2 2 3 "
},
{
"input": "3 0",
"output": "1\n3 1 2 3 "
},
{
"input": "3 3\n2 3\n1 2\n1 3",
"output": "3\n1 1 \n1 2 \n1 3 "
},
{
"input": "4 3\n1 3\n1 4\n1 2",
"output": "2\n1 1 \n3 2 3 4 "
},
{
"input": "4 3\n1 2\n3 4\n2 3",
"output": "1\n4 1 2 3 4 "
}
] | 3,000 | 56,934,400 | 0 | 23,697 |
|
504 | Misha and XOR | [
"bitmasks"
] | null | null | After Misha's birthday he had many large numbers left, scattered across the room. Now it's time to clean up and Misha needs to put them in a basket. He ordered this task to his pet robot that agreed to complete the task at certain conditions. Before the robot puts a number *x* to the basket, Misha should answer the question: is it possible to choose one or multiple numbers that already are in the basket, such that their XOR sum equals *x*?
If the answer is positive, you also need to give the indexes of these numbers. If there are multiple options of choosing numbers, you are allowed to choose any correct option. After Misha's answer the robot puts the number to the basket.
Initially the basket is empty. Each integer you put in the basket takes some number. The first integer you put into the basket take number 0, the second integer takes number 1 and so on.
Misha needs to clean up the place as soon as possible but unfortunately, he isn't that good at mathematics. He asks you to help him. | The first line contains number *m* (1<=≤<=*m*<=≤<=2000), showing how many numbers are scattered around the room.
The next *m* lines contain the numbers in the order in which the robot puts them in the basket. Each number is a positive integer strictly less than 10600 that doesn't contain leading zeroes. | For each number either print a 0 on the corresponding line, if the number cannot be represented as a XOR sum of numbers that are in the basket, or print integer *k* showing how many numbers are in the representation and the indexes of these numbers. Separate the numbers by spaces. Each number can occur in the representation at most once. | [
"7\n7\n6\n5\n4\n3\n2\n1\n",
"2\n5\n5\n"
] | [
"0\n0\n0\n3 0 1 2\n2 1 2\n2 0 2\n2 0 1\n",
"0\n1 0\n"
] | The XOR sum of numbers is the result of bitwise sum of numbers modulo 2. | [
{
"input": "7\n7\n6\n5\n4\n3\n2\n1",
"output": "0\n0\n0\n3 0 1 2\n2 1 2\n2 0 2\n2 0 1"
},
{
"input": "2\n5\n5",
"output": "0\n1 0"
},
{
"input": "10\n81\n97\n12\n2\n16\n96\n80\n99\n6\n83",
"output": "0\n0\n0\n0\n0\n0\n3 0 1 5\n2 1 3\n0\n2 0 3"
},
{
"input": "10\n15106\n13599\n69319\n33224\n26930\n94490\n85089\n60931\n23137\n62868",
"output": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0"
},
{
"input": "10\n5059464500\n8210395556\n3004213265\n248593357\n5644084048\n9359824793\n8120649160\n4288978422\n183848555\n8135845959",
"output": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0"
},
{
"input": "10\n4\n12\n28\n29\n31\n31\n31\n31\n31\n31",
"output": "0\n0\n0\n0\n0\n1 4\n1 4\n1 4\n1 4\n1 4"
},
{
"input": "10\n16\n24\n28\n30\n31\n31\n31\n31\n31\n31",
"output": "0\n0\n0\n0\n0\n1 4\n1 4\n1 4\n1 4\n1 4"
},
{
"input": "10\n16\n8\n4\n2\n1\n31\n31\n31\n31\n31",
"output": "0\n0\n0\n0\n0\n5 0 1 2 3 4\n5 0 1 2 3 4\n5 0 1 2 3 4\n5 0 1 2 3 4\n5 0 1 2 3 4"
},
{
"input": "10\n1\n2\n4\n8\n16\n31\n31\n31\n31\n31",
"output": "0\n0\n0\n0\n0\n5 0 1 2 3 4\n5 0 1 2 3 4\n5 0 1 2 3 4\n5 0 1 2 3 4\n5 0 1 2 3 4"
}
] | 31 | 0 | 0 | 23,759 |
|
0 | none | [
"none"
] | null | null | You must have heard of the two brothers dreaming of ruling the world. With all their previous plans failed, this time they decided to cooperate with each other in order to rule the world.
As you know there are *n* countries in the world. These countries are connected by *n*<=-<=1 directed roads. If you don't consider direction of the roads there is a unique path between every pair of countries in the world, passing through each road at most once.
Each of the brothers wants to establish his reign in some country, then it's possible for him to control the countries that can be reached from his country using directed roads.
The brothers can rule the world if there exists at most two countries for brothers to choose (and establish their reign in these countries) so that any other country is under control of at least one of them. In order to make this possible they want to change the direction of minimum number of roads. Your task is to calculate this minimum number of roads. | The first line of input contains an integer *n* (1<=≤<=*n*<=≤<=3000). Each of the next *n*<=-<=1 lines contains two space-separated integers *a**i* and *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *a**i*<=≠<=*b**i*) saying there is a road from country *a**i* to country *b**i*.
Consider that countries are numbered from 1 to *n*. It's guaranteed that if you don't consider direction of the roads there is a unique path between every pair of countries in the world, passing through each road at most once. | In the only line of output print the minimum number of roads that their direction should be changed so that the brothers will be able to rule the world. | [
"4\n2 1\n3 1\n4 1\n",
"5\n2 1\n2 3\n4 3\n4 5\n"
] | [
"1\n",
"0\n"
] | none | [] | 92 | 0 | 0 | 23,785 |
|
138 | Literature Lesson | [
"implementation"
] | null | null | Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes.
Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels.
Two lines rhyme if their suffixes that start from the *k*-th vowels (counting from the end) match. If a line has less than *k* vowels, then such line can't rhyme with any other line. For example, if *k*<==<=1, lines *commit* and *hermit* rhyme (the corresponding suffixes equal *it*), and if *k*<==<=2, they do not rhyme (*ommit*<=≠<=*ermit*).
Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines):
- Clerihew (*aabb*); - Alternating (*abab*); - Enclosed (*abba*).
If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by *aaaa*).
If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is *aaaa*. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme.
Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task. | The first line contains two integers *n* and *k* (1<=≤<=*n*<=≤<=2500, 1<=≤<=*k*<=≤<=5) — the number of quatrains in the poem and the vowel's number, correspondingly. Next 4*n* lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104.
If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on. | Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes. | [
"1 1\nday\nmay\nsun\nfun\n",
"1 1\nday\nmay\ngray\nway\n",
"2 1\na\na\na\na\na\na\ne\ne\n",
"2 1\nday\nmay\nsun\nfun\ntest\nhill\nfest\nthrill\n"
] | [
"aabb\n",
"aaaa\n",
"aabb\n",
"NO\n"
] | In the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO". | [
{
"input": "1 1\nday\nmay\nsun\nfun",
"output": "aabb"
},
{
"input": "1 1\nday\nmay\ngray\nway",
"output": "aaaa"
},
{
"input": "2 1\na\na\na\na\na\na\ne\ne",
"output": "aabb"
},
{
"input": "2 1\nday\nmay\nsun\nfun\ntest\nhill\nfest\nthrill",
"output": "NO"
},
{
"input": "2 5\na\na\na\na\na\na\ne\ne",
"output": "NO"
},
{
"input": "1 1\nrezwbgy\nxakgmv\njogezwbgy\napezwbgy",
"output": "NO"
},
{
"input": "2 1\nnuqfxwrb\napqfkw\nuqfxwrb\nnhcuqfxwrb\nogkznwncmt\nevf\nogkznwncmt\nogkznwncmt",
"output": "NO"
},
{
"input": "1 1\naawjvkxx\nawjvkxx\nxawjvkxx\nawjvkxx",
"output": "aaaa"
},
{
"input": "2 2\nrhcujgxabk\nnjgdqpurul\nueoedt\ncpcfhbyvo\nzmfwnieog\npkpylassbf\nhrfeod\ncdwuil",
"output": "NO"
},
{
"input": "2 1\nol\nol\nol\nzol\nek\nek\nek\nqek",
"output": "aaaa"
},
{
"input": "3 2\nexdaoao\nrdwunurp\ndunurp\ntyqzuxao\ndupocgsps\nzsiravcm\nnqiravcm\nlnupocgsps\niwashk\neepkqcykbv\nyviwashk\neepkqcykbv",
"output": "NO"
},
{
"input": "2 1\ndaihacbnhgfts\nsqihpntjvczkw\nmihpntjvczkw\nvyacbnhgfts\ntsvovdpqajmgvcj\ncexqkwrvctomb\njxbomb\ngnpajmgvcj",
"output": "abba"
},
{
"input": "3 2\netba\ntfecetba\nzkitbgcuuy\nuuy\nbuxeoi\nmekxoi\nblviwoehy\niwoehy\njyfpaqntiz\nqvaqntiz\nhciak\niak",
"output": "aabb"
},
{
"input": "4 3\niixxiojrrdytjcbkvymw\nbjqixxiojrrdytjcbkvymw\nogjixxiojrrdytjcbkvymw\nevixxpfxpgicpg\njkotitixiughfhphliuurx\ngyubkqtonejprfjzvqxbdpn\ndpudxfoqnhekjytbwiuurx\noubkqtonejprfjzvqxbdpn\npgzaendrxjhsfzjmijv\npomuaendrxjhsfzjmijv\nafyuyxueaendrxjhsfzjmijv\naendrxjhsfzjmijv\nyubweicj\ntbnsuxqigmxdfnmbipubweicj\nfuftydlmoo\nmdkuftydlmoo",
"output": "NO"
},
{
"input": "5 2\nqurcmcbxyoddgyyccsmb\nlsdzsqoa\neurcmcbxyoddgyyccsmb\noa\nutyxmdhcvaclynmstwsx\nmkyycelbmkmdrilmbvr\nutyxmdhcvaclynmstwsx\nrduyelbmkmdrilmbvr\nhmguhvqswwciowwgu\nnoe\nzmyncuwrowwgu\nqrhymghavvbmigzsjoe\nbvofhknbzusykztlxwms\nbpbfmvjaimkdeddy\neofhknbzusykztlxwms\nmhivpkxkpazimkdeddy\negvywnhmfngllaknmn\nmblkvhenlggoftwjgk\nzegvywnhmfngllaknmn\ngrdenlggoftwjgk",
"output": "abab"
},
{
"input": "7 3\nferwljzwakxedlgwl\noerwljzwakxedlgwl\nhyqombizhuhxedprb\netptjrizhuhxedprb\nurtuckar\ndkartmwramklcmi\nrurtuckar\nnurartmwramklcmi\niraziomsv\nsaziomsv\nbprapiqpayzurgij\nusyemayzurgij\nztstmeecvmkvuu\nquexlecvmkvuu\nrlhwecvmkvuu\nzecvmkvuu\niikymgbncljtub\nqiikymgbncljtub\nbcavhexqamyszgfya\nojexqamyszgfya\nieyxqinjinjv\nxtiudieyxqinjinjv\nthtceyxqinjinjv\nmuneyxqinjinjv\nwreae\nqylcjhjzfhteae\nozcjthgyuchqo\nfcjozcjthgyuchqo",
"output": "NO"
},
{
"input": "16 1\ni\ni\ni\ni\ni\nu\ni\ni\no\na\na\no\na\ni\na\na\ni\ni\no\no\ni\ni\ni\ni\nu\nu\nu\nu\no\ne\ne\ne\no\ni\no\ni\na\na\na\na\nu\no\no\nu\ni\no\no\ni\na\na\ne\ne\na\na\na\na\na\no\na\na\nu\na\nu\nu",
"output": "NO"
},
{
"input": "16 1\neb\neb\nfe\nce\ner\ner\new\new\nu\ncu\nu\nu\nud\nik\nud\nik\nve\niw\niw\nne\nel\nob\nel\nob\no\neo\no\nyo\nav\nav\nei\nmi\nu\noh\noh\nzu\niw\niw\na\nma\ni\nu\nku\ngi\nac\no\no\nac\ni\ner\nai\ner\nyu\nuf\nuf\nhu\nef\nef\nef\nef\nmu\nu\nqe\nie",
"output": "NO"
},
{
"input": "25 1\nw\ni\nv\nx\nh\ns\nz\ny\no\nn\nh\ni\nf\nf\ny\nr\nb\nu\no\np\nz\nh\nt\no\nw\nx\nh\no\nj\ny\nw\nj\ny\nh\nh\nr\ns\nb\ny\nr\nw\no\nl\nl\nh\nh\nw\nu\na\nv\no\nx\nd\nw\nc\nf\ni\ne\nj\nq\nk\na\ne\nl\nw\nm\nf\na\nc\na\nb\nf\nj\nb\nx\ni\nx\ne\nu\nh\nm\no\ni\nq\nm\nk\nn\nd\nl\np\nc\nw\nu\nz\nc\nk\ng\ny\nj\ny",
"output": "NO"
},
{
"input": "1 1\ne\ne\ne\ne",
"output": "aaaa"
},
{
"input": "1 1\na\ne\ne\ne",
"output": "NO"
},
{
"input": "1 1\ne\na\ne\ne",
"output": "NO"
},
{
"input": "1 1\na\na\ne\ne",
"output": "aabb"
},
{
"input": "1 1\ne\ne\na\ne",
"output": "NO"
},
{
"input": "1 1\na\ne\na\ne",
"output": "abab"
},
{
"input": "1 1\ne\na\na\ne",
"output": "abba"
},
{
"input": "1 1\na\na\na\ne",
"output": "NO"
},
{
"input": "1 1\ne\ne\ne\na",
"output": "NO"
},
{
"input": "1 1\na\ne\ne\na",
"output": "abba"
},
{
"input": "1 1\ne\na\ne\na",
"output": "abab"
},
{
"input": "1 1\na\na\ne\na",
"output": "NO"
},
{
"input": "1 1\ne\ne\na\na",
"output": "aabb"
},
{
"input": "1 1\na\ne\na\na",
"output": "NO"
},
{
"input": "1 1\ne\na\na\na",
"output": "NO"
},
{
"input": "1 1\na\na\na\na",
"output": "aaaa"
},
{
"input": "1 2\neraub\nbee\naab\nttbee",
"output": "NO"
},
{
"input": "10 1\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\ny",
"output": "NO"
},
{
"input": "1 2\neeereaatktb\nbee\niaattb\nottbee",
"output": "NO"
},
{
"input": "1 1\nab\nac\nad\naf",
"output": "NO"
},
{
"input": "1 1\nar\nat\nay\naw",
"output": "NO"
},
{
"input": "2 1\na\ne\na\ne\na\na\na\na",
"output": "abab"
},
{
"input": "1 1\na\ne\na\ni",
"output": "NO"
},
{
"input": "1 1\na\ne\na\ne",
"output": "abab"
},
{
"input": "1 1\nabbbbbbbbbbbbbbbbcbbbbbbbbbbbbbbbb\nabbbbbbbbbbbbbbbbfbbbbbbbbbbbbbbbb\nabbbbbbbbbbbbbbbbxbbbbbbbbbbbbbbbb\nabbbbbbbbbbbbbbbbdbbbbbbbbbbbbbbbb",
"output": "NO"
},
{
"input": "2 1\na\ne\ne\na\na\na\na\na",
"output": "abba"
},
{
"input": "1 1\nbug\nsuy\nluh\ngut",
"output": "NO"
},
{
"input": "1 1\nam\nat\nan\nag",
"output": "NO"
},
{
"input": "2 1\na\na\ne\ne\na\na\na\na",
"output": "aabb"
},
{
"input": "1 4\naieoabcd\naeioabcd\naoeiabcd\naoieabcd",
"output": "NO"
},
{
"input": "1 2\naec\naed\naek\naem",
"output": "NO"
},
{
"input": "1 1\nar\nab\nak\naz",
"output": "NO"
},
{
"input": "2 1\na\na\na\na\na\nb\nb\nb",
"output": "NO"
}
] | 62 | 0 | 0 | 23,818 |
|
0 | none | [
"none"
] | null | null | Let's assume that
- *v*(*n*) is the largest prime number, that does not exceed *n*;- *u*(*n*) is the smallest prime number strictly greater than *n*.
Find . | The first line contains integer *t* (1<=≤<=*t*<=≤<=500) — the number of testscases.
Each of the following *t* lines of the input contains integer *n* (2<=≤<=*n*<=≤<=109). | Print *t* lines: the *i*-th of them must contain the answer to the *i*-th test as an irreducible fraction "*p*/*q*", where *p*,<=*q* are integers, *q*<=><=0. | [
"2\n2\n3\n"
] | [
"1/6\n7/30\n"
] | none | [
{
"input": "2\n2\n3",
"output": "1/6\n7/30"
},
{
"input": "1\n1000000000",
"output": "999999941999999673/1999999887999999118"
},
{
"input": "5\n3\n6\n9\n10\n5",
"output": "7/30\n5/14\n61/154\n9/22\n23/70"
},
{
"input": "5\n5\n8\n18\n17\n17",
"output": "23/70\n59/154\n17/38\n287/646\n287/646"
},
{
"input": "5\n7\n40\n37\n25\n4",
"output": "57/154\n39/82\n1437/3034\n615/1334\n3/10"
},
{
"input": "5\n72\n72\n30\n75\n11",
"output": "71/146\n71/146\n29/62\n5615/11534\n119/286"
},
{
"input": "5\n79\n149\n136\n194\n124",
"output": "6393/13114\n22199/44998\n135/274\n37631/76042\n14121/28702"
},
{
"input": "6\n885\n419\n821\n635\n63\n480",
"output": "781453/1566442\n175559/352798\n674039/1351366\n403199/808942\n3959/8174\n232303/466546"
},
{
"input": "1\n649580447",
"output": "421954771415489597/843909545429301074"
}
] | 46 | 0 | 0 | 23,824 |
|
201 | Guess That Car! | [
"math",
"ternary search"
] | null | null | A widely known among some people Belarusian sport programmer Yura possesses lots of information about cars. That is why he has been invited to participate in a game show called "Guess That Car!".
The game show takes place on a giant parking lot, which is 4*n* meters long from north to south and 4*m* meters wide from west to east. The lot has *n*<=+<=1 dividing lines drawn from west to east and *m*<=+<=1 dividing lines drawn from north to south, which divide the parking lot into *n*·*m* 4 by 4 meter squares. There is a car parked strictly inside each square. The dividing lines are numbered from 0 to *n* from north to south and from 0 to *m* from west to east. Each square has coordinates (*i*,<=*j*) so that the square in the north-west corner has coordinates (1,<=1) and the square in the south-east corner has coordinates (*n*,<=*m*). See the picture in the notes for clarifications.
Before the game show the organizers offer Yura to occupy any of the (*n*<=+<=1)·(*m*<=+<=1) intersection points of the dividing lines. After that he can start guessing the cars. After Yura chooses a point, he will be prohibited to move along the parking lot before the end of the game show. As Yura is a car expert, he will always guess all cars he is offered, it's just a matter of time. Yura knows that to guess each car he needs to spend time equal to the square of the euclidean distance between his point and the center of the square with this car, multiplied by some coefficient characterizing the machine's "rarity" (the rarer the car is, the harder it is to guess it). More formally, guessing a car with "rarity" *c* placed in a square whose center is at distance *d* from Yura takes *c*·*d*2 seconds. The time Yura spends on turning his head can be neglected.
It just so happened that Yura knows the "rarity" of each car on the parking lot in advance. Help him choose his point so that the total time of guessing all cars is the smallest possible. | The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=1000) — the sizes of the parking lot. Each of the next *n* lines contains *m* integers: the *j*-th number in the *i*-th line describes the "rarity" *c**ij* (0<=≤<=*c**ij*<=≤<=100000) of the car that is located in the square with coordinates (*i*,<=*j*). | In the first line print the minimum total time Yura needs to guess all offered cars. In the second line print two numbers *l**i* and *l**j* (0<=≤<=*l**i*<=≤<=*n*,<=0<=≤<=*l**j*<=≤<=*m*) — the numbers of dividing lines that form a junction that Yura should choose to stand on at the beginning of the game show. If there are multiple optimal starting points, print the point with smaller *l**i*. If there are still multiple such points, print the point with smaller *l**j*.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"2 3\n3 4 5\n3 9 1\n",
"3 4\n1 0 0 0\n0 0 3 0\n0 0 5 5\n"
] | [
"392\n1 1\n",
"240\n2 3\n"
] | In the first test case the total time of guessing all cars is equal to 3·8 + 3·8 + 4·8 + 9·8 + 5·40 + 1·40 = 392.
The coordinate system of the field: | [
{
"input": "2 3\n3 4 5\n3 9 1",
"output": "392\n1 1"
},
{
"input": "3 4\n1 0 0 0\n0 0 3 0\n0 0 5 5",
"output": "240\n2 3"
},
{
"input": "4 7\n91811 50656 97940 70982 88108 31856 80371\n94488 4672 15935 10209 22935 55803 57593\n79510 213 92494 7670 49727 97141 54221\n89004 67886 99994 22224 65646 24548 25509",
"output": "148553968\n2 3"
},
{
"input": "7 4\n0 0 0 0\n0 1 0 3\n0 1 0 0\n0 0 3 0\n0 0 0 2\n0 3 0 1\n0 0 2 6",
"output": "1552\n5 3"
},
{
"input": "5 5\n0 0 15380 0 0\n0 0 26291 0 0\n0 0 0 0 0\n0 0 0 0 0\n0 0 0 0 0",
"output": "333368\n1 2"
},
{
"input": "10 10\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 1585 1564 3603 1095 1364 1029 3189 1331\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 1749\n0 3495 1540 641 1093 702 2249 983 1218 2108\n0 1397 1105 3119 1194 839 1286 2979 2941 1911\n0 1818 2264 631 3342 3511 2434 2960 667 2411",
"output": "14833240\n7 6"
},
{
"input": "10 10\n0 0 0 0 0 0 0 0 0 0\n0 87000 0 0 0 0 0 0 0 0\n96879 0 0 0 80529 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 82797 0 0\n0 0 0 0 0 0 0 0 0 0\n97471 0 0 0 0 0 43355 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 80167 0 0\n0 0 0 0 0 0 0 0 0 0",
"output": "138865744\n4 4"
},
{
"input": "8 8\n0 0 0 0 0 0 0 0\n0 0 0 0 0 0 807 825\n0 0 0 0 0 0 1226 1489\n0 0 0 0 0 0 908 502\n0 0 0 0 0 0 527 765\n0 0 0 0 0 0 1642 757\n0 0 0 0 0 0 1502 1760\n0 0 0 0 0 0 1243 884",
"output": "1046376\n5 7"
},
{
"input": "1 1\n92660",
"output": "741280\n0 0"
},
{
"input": "1 2\n19378 47762",
"output": "537120\n0 1"
},
{
"input": "2 1\n2157\n33017",
"output": "281392\n1 0"
},
{
"input": "6 5\n0 0 0 0 0\n0 0 0 0 0\n0 3 3 3 0\n0 3 3 3 0\n0 3 3 3 0\n0 0 0 0 0",
"output": "792\n3 2"
},
{
"input": "1 1\n1",
"output": "8\n0 0"
},
{
"input": "100 1\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n4\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1",
"output": "2434000\n35 0"
},
{
"input": "1 1\n0",
"output": "0\n0 0"
},
{
"input": "2 3\n0 0 0\n0 0 0",
"output": "0\n0 0"
}
] | 124 | 0 | 0 | 23,839 |
|
201 | Clear Symmetry | [
"constructive algorithms",
"dp",
"math"
] | null | null | Consider some square matrix *A* with side *n* consisting of zeros and ones. There are *n* rows numbered from 1 to *n* from top to bottom and *n* columns numbered from 1 to *n* from left to right in this matrix. We'll denote the element of the matrix which is located at the intersection of the *i*-row and the *j*-th column as *A**i*,<=*j*.
Let's call matrix *A* clear if no two cells containing ones have a common side.
Let's call matrix *A* symmetrical if it matches the matrices formed from it by a horizontal and/or a vertical reflection. Formally, for each pair (*i*,<=*j*) (1<=≤<=*i*,<=*j*<=≤<=*n*) both of the following conditions must be met: *A**i*,<=*j*<==<=*A**n*<=-<=*i*<=+<=1,<=*j* and *A**i*,<=*j*<==<=*A**i*,<=*n*<=-<=*j*<=+<=1.
Let's define the sharpness of matrix *A* as the number of ones in it.
Given integer *x*, your task is to find the smallest positive integer *n* such that there exists a clear symmetrical matrix *A* with side *n* and sharpness *x*. | The only line contains a single integer *x* (1<=≤<=*x*<=≤<=100) — the required sharpness of the matrix. | Print a single number — the sought value of *n*. | [
"4\n",
"9\n"
] | [
"3\n",
"5\n"
] | The figure below shows the matrices that correspond to the samples: | [
{
"input": "4",
"output": "3"
},
{
"input": "9",
"output": "5"
},
{
"input": "10",
"output": "5"
},
{
"input": "12",
"output": "5"
},
{
"input": "1",
"output": "1"
},
{
"input": "19",
"output": "7"
},
{
"input": "3",
"output": "5"
},
{
"input": "2",
"output": "3"
},
{
"input": "5",
"output": "3"
},
{
"input": "6",
"output": "5"
},
{
"input": "7",
"output": "5"
},
{
"input": "8",
"output": "5"
},
{
"input": "11",
"output": "5"
},
{
"input": "13",
"output": "5"
},
{
"input": "14",
"output": "7"
},
{
"input": "15",
"output": "7"
},
{
"input": "16",
"output": "7"
},
{
"input": "17",
"output": "7"
},
{
"input": "18",
"output": "7"
},
{
"input": "20",
"output": "7"
},
{
"input": "21",
"output": "7"
},
{
"input": "22",
"output": "7"
},
{
"input": "23",
"output": "7"
},
{
"input": "24",
"output": "7"
},
{
"input": "25",
"output": "7"
},
{
"input": "26",
"output": "9"
},
{
"input": "27",
"output": "9"
},
{
"input": "28",
"output": "9"
},
{
"input": "29",
"output": "9"
},
{
"input": "30",
"output": "9"
},
{
"input": "31",
"output": "9"
},
{
"input": "32",
"output": "9"
},
{
"input": "33",
"output": "9"
},
{
"input": "34",
"output": "9"
},
{
"input": "35",
"output": "9"
},
{
"input": "36",
"output": "9"
},
{
"input": "37",
"output": "9"
},
{
"input": "38",
"output": "9"
},
{
"input": "39",
"output": "9"
},
{
"input": "40",
"output": "9"
},
{
"input": "41",
"output": "9"
},
{
"input": "42",
"output": "11"
},
{
"input": "43",
"output": "11"
},
{
"input": "44",
"output": "11"
},
{
"input": "45",
"output": "11"
},
{
"input": "46",
"output": "11"
},
{
"input": "47",
"output": "11"
},
{
"input": "48",
"output": "11"
},
{
"input": "49",
"output": "11"
},
{
"input": "50",
"output": "11"
},
{
"input": "51",
"output": "11"
},
{
"input": "52",
"output": "11"
},
{
"input": "53",
"output": "11"
},
{
"input": "54",
"output": "11"
},
{
"input": "55",
"output": "11"
},
{
"input": "56",
"output": "11"
},
{
"input": "57",
"output": "11"
},
{
"input": "58",
"output": "11"
},
{
"input": "59",
"output": "11"
},
{
"input": "60",
"output": "11"
},
{
"input": "61",
"output": "11"
},
{
"input": "62",
"output": "13"
},
{
"input": "63",
"output": "13"
},
{
"input": "64",
"output": "13"
},
{
"input": "65",
"output": "13"
},
{
"input": "66",
"output": "13"
},
{
"input": "67",
"output": "13"
},
{
"input": "68",
"output": "13"
},
{
"input": "69",
"output": "13"
},
{
"input": "70",
"output": "13"
},
{
"input": "71",
"output": "13"
},
{
"input": "72",
"output": "13"
},
{
"input": "73",
"output": "13"
},
{
"input": "74",
"output": "13"
},
{
"input": "75",
"output": "13"
},
{
"input": "76",
"output": "13"
},
{
"input": "77",
"output": "13"
},
{
"input": "78",
"output": "13"
},
{
"input": "79",
"output": "13"
},
{
"input": "80",
"output": "13"
},
{
"input": "81",
"output": "13"
},
{
"input": "82",
"output": "13"
},
{
"input": "83",
"output": "13"
},
{
"input": "84",
"output": "13"
},
{
"input": "85",
"output": "13"
},
{
"input": "86",
"output": "15"
},
{
"input": "87",
"output": "15"
},
{
"input": "88",
"output": "15"
},
{
"input": "89",
"output": "15"
},
{
"input": "90",
"output": "15"
},
{
"input": "91",
"output": "15"
},
{
"input": "92",
"output": "15"
},
{
"input": "93",
"output": "15"
},
{
"input": "94",
"output": "15"
},
{
"input": "95",
"output": "15"
},
{
"input": "96",
"output": "15"
},
{
"input": "97",
"output": "15"
},
{
"input": "98",
"output": "15"
},
{
"input": "99",
"output": "15"
},
{
"input": "100",
"output": "15"
}
] | 248 | 0 | 0 | 23,880 |
|
908 | New Year and Buggy Bot | [
"brute force",
"implementation"
] | null | null | Bob programmed a robot to navigate through a 2d maze.
The maze has some obstacles. Empty cells are denoted by the character '.', where obstacles are denoted by '#'.
There is a single robot in the maze. Its start position is denoted with the character 'S'. This position has no obstacle in it. There is also a single exit in the maze. Its position is denoted with the character 'E'. This position has no obstacle in it.
The robot can only move up, left, right, or down.
When Bob programmed the robot, he wrote down a string of digits consisting of the digits 0 to 3, inclusive. He intended for each digit to correspond to a distinct direction, and the robot would follow the directions in order to reach the exit. Unfortunately, he forgot to actually assign the directions to digits.
The robot will choose some random mapping of digits to distinct directions. The robot will map distinct digits to distinct directions. The robot will then follow the instructions according to the given string in order and chosen mapping. If an instruction would lead the robot to go off the edge of the maze or hit an obstacle, the robot will crash and break down. If the robot reaches the exit at any point, then the robot will stop following any further instructions.
Bob is having trouble debugging his robot, so he would like to determine the number of mappings of digits to directions that would lead the robot to the exit. | The first line of input will contain two integers *n* and *m* (2<=≤<=*n*,<=*m*<=≤<=50), denoting the dimensions of the maze.
The next *n* lines will contain exactly *m* characters each, denoting the maze.
Each character of the maze will be '.', '#', 'S', or 'E'.
There will be exactly one 'S' and exactly one 'E' in the maze.
The last line will contain a single string *s* (1<=≤<=|*s*|<=≤<=100) — the instructions given to the robot. Each character of *s* is a digit from 0 to 3. | Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit. | [
"5 6\n.....#\nS....#\n.#....\n.#....\n...E..\n333300012\n",
"6 6\n......\n......\n..SE..\n......\n......\n......\n01232123212302123021\n",
"5 3\n...\n.S.\n###\n.E.\n...\n3\n"
] | [
"1\n",
"14\n",
"0\n"
] | For the first sample, the only valid mapping is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/87a55361bde12e4223a96f0e1d83b94428f26f02.png" style="max-width: 100.0%;max-height: 100.0%;"/>, where *D* is down, *L* is left, *U* is up, *R* is right. | [
{
"input": "5 6\n.....#\nS....#\n.#....\n.#....\n...E..\n333300012",
"output": "1"
},
{
"input": "6 6\n......\n......\n..SE..\n......\n......\n......\n01232123212302123021",
"output": "14"
},
{
"input": "5 3\n...\n.S.\n###\n.E.\n...\n3",
"output": "0"
},
{
"input": "10 10\n.#......#.\n#.........\n#.........\n....#.#..E\n.......#..\n....##....\n....S.....\n....#.....\n.........#\n...##...#.\n23323332313123221123020122221313323310313122323233",
"output": "0"
},
{
"input": "8 9\n.........\n.........\n.........\n.E.#.....\n.........\n.........\n...#.S...\n.........\n10001100111000010121100000110110110100000100000100",
"output": "2"
},
{
"input": "15 13\n.............\n.............\n.............\n.........#...\n..#..........\n.............\n..........E..\n.............\n.............\n.#...........\n.....#.......\n..........#..\n..........S..\n.............\n.........#...\n32222221111222312132110100022020202131222103103330",
"output": "2"
},
{
"input": "5 5\n.....\n.....\n..SE.\n.....\n.....\n012330213120031231022103231013201032301223011230102320130231321012030321213002133201130201322031",
"output": "24"
},
{
"input": "2 2\nS.\n.E\n23",
"output": "4"
},
{
"input": "2 2\nS.\n.E\n03",
"output": "4"
},
{
"input": "2 2\nSE\n..\n22",
"output": "6"
},
{
"input": "2 2\nS.\nE.\n11",
"output": "6"
},
{
"input": "2 2\n#E\nS.\n01",
"output": "2"
},
{
"input": "10 10\n####S.####\n#####.####\n#####.####\n#####.####\n#####..###\n######.###\n######.###\n######.E##\n##########\n##########\n0111101110",
"output": "2"
},
{
"input": "10 10\n#####..E##\n#####.S.##\n#####...##\n##########\n##########\n##########\n##########\n##########\n##########\n##########\n20",
"output": "4"
},
{
"input": "10 10\n#####ES.##\n######.###\n##########\n##########\n##########\n##########\n##########\n##########\n##########\n##########\n3",
"output": "6"
},
{
"input": "2 10\nS........E\n..........\n33333333333333333",
"output": "6"
},
{
"input": "2 2\n..\nSE\n0",
"output": "6"
},
{
"input": "2 2\nSE\n##\n0",
"output": "6"
},
{
"input": "2 2\nS.\nE.\n012",
"output": "8"
},
{
"input": "2 3\nS.E\n###\n1222",
"output": "0"
},
{
"input": "2 5\nS...E\n.....\n133330",
"output": "1"
},
{
"input": "5 5\n.....\n.....\n.S.E.\n.....\n.....\n001111",
"output": "6"
},
{
"input": "3 5\n....S\n....#\n....E\n0112",
"output": "1"
},
{
"input": "2 2\nSE\n..\n123",
"output": "8"
},
{
"input": "2 10\n........ES\n..........\n123",
"output": "8"
},
{
"input": "2 2\nS.\n.E\n2311",
"output": "4"
},
{
"input": "2 2\nS.\n.E\n0012",
"output": "0"
},
{
"input": "2 7\nS.....E\n#######\n01111111",
"output": "0"
},
{
"input": "2 2\nS.\n.E\n1123",
"output": "0"
},
{
"input": "2 3\nS.E\n...\n0111",
"output": "0"
},
{
"input": "2 50\n.................................................E\nS.................................................\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"output": "0"
},
{
"input": "5 2\n..\n..\n..\n..\nSE\n0",
"output": "6"
},
{
"input": "3 3\nE..\n.S.\n...\n001123110023221103",
"output": "0"
},
{
"input": "2 2\nS#\nE#\n012",
"output": "6"
},
{
"input": "2 2\nES\n..\n011",
"output": "6"
},
{
"input": "2 2\nSE\n..\n011",
"output": "6"
},
{
"input": "2 2\nS.\nE.\n102",
"output": "8"
},
{
"input": "3 2\nE#\n##\nS#\n0112",
"output": "0"
}
] | 46 | 0 | 3 | 23,922 |
|
958 | Maximum Control (medium) | [
"data structures",
"dfs and similar",
"graphs",
"greedy",
"trees"
] | null | null | The Resistance is trying to take control over as many planets of a particular solar system as possible. Princess Heidi is in charge of the fleet, and she must send ships to some planets in order to maximize the number of controlled planets.
The Galaxy contains *N* planets, connected by bidirectional hyperspace tunnels in such a way that there is a unique path between every pair of the planets.
A planet is controlled by the Resistance if there is a Resistance ship in its orbit, or if the planet lies on the shortest path between some two planets that have Resistance ships in their orbits.
Heidi has not yet made up her mind as to how many ships to use. Therefore, she is asking you to compute, for every *K*<==<=1,<=2,<=3,<=...,<=*N*, the maximum number of planets that can be controlled with a fleet consisting of *K* ships. | The first line of the input contains an integer *N* (1<=≤<=*N*<=≤<=105) – the number of planets in the galaxy.
The next *N*<=-<=1 lines describe the hyperspace tunnels between the planets. Each of the *N*<=-<=1 lines contains two space-separated integers *u* and *v* (1<=≤<=*u*,<=*v*<=≤<=*N*) indicating that there is a bidirectional hyperspace tunnel between the planets *u* and *v*. It is guaranteed that every two planets are connected by a path of tunnels, and that each tunnel connects a different pair of planets. | On a single line, print *N* space-separated integers. The *K*-th number should correspond to the maximum number of planets that can be controlled by the Resistance using a fleet of *K* ships. | [
"3\n1 2\n2 3\n",
"4\n1 2\n3 2\n4 2\n"
] | [
"1 3 3 ",
"1 3 4 4 "
] | Consider the first example. If *K* = 1, then Heidi can only send one ship to some planet and control it. However, for *K* ≥ 2, sending ships to planets 1 and 3 will allow the Resistance to control all planets. | [
{
"input": "3\n1 2\n2 3",
"output": "1 3 3 "
},
{
"input": "4\n1 2\n3 2\n4 2",
"output": "1 3 4 4 "
},
{
"input": "19\n2 19\n7 15\n8 10\n16 1\n12 5\n11 5\n6 18\n12 14\n14 15\n2 6\n9 14\n4 17\n16 10\n4 2\n7 18\n3 2\n9 13\n11 10",
"output": "1 14 16 17 18 19 19 19 19 19 19 19 19 19 19 19 19 19 19 "
},
{
"input": "20\n12 10\n5 19\n12 18\n4 2\n3 16\n11 8\n3 2\n17 4\n14 7\n6 5\n10 20\n14 9\n16 12\n9 13\n13 4\n1 15\n11 6\n15 19\n14 8",
"output": "1 17 18 19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 "
},
{
"input": "21\n10 12\n12 9\n16 8\n18 11\n17 15\n4 11\n7 19\n10 14\n10 19\n8 5\n1 5\n20 3\n19 18\n2 20\n19 15\n9 16\n6 4\n4 13\n20 6\n13 21",
"output": "1 14 16 18 19 20 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 "
},
{
"input": "23\n5 14\n6 7\n6 17\n2 4\n6 13\n9 17\n14 10\n23 9\n1 18\n3 12\n15 11\n21 15\n10 23\n4 3\n8 15\n22 18\n22 16\n15 5\n12 22\n4 6\n19 16\n20 10",
"output": "1 15 17 18 19 20 21 22 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 "
},
{
"input": "24\n19 14\n8 15\n13 4\n18 16\n1 17\n10 3\n22 21\n10 14\n6 11\n9 12\n15 22\n11 3\n21 7\n2 12\n7 4\n4 19\n16 9\n24 17\n5 15\n8 2\n23 10\n20 6\n18 24",
"output": "1 21 22 23 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 "
}
] | 140 | 512,000 | 0 | 23,964 |
|
825 | Minimal Labels | [
"data structures",
"dfs and similar",
"graphs",
"greedy"
] | null | null | You are given a directed acyclic graph with *n* vertices and *m* edges. There are no self-loops or multiple edges between any pair of vertices. Graph can be disconnected.
You should assign labels to all vertices in such a way that:
- Labels form a valid permutation of length *n* — an integer sequence such that each integer from 1 to *n* appears exactly once in it. - If there exists an edge from vertex *v* to vertex *u* then *label**v* should be smaller than *label**u*. - Permutation should be lexicographically smallest among all suitable.
Find such sequence of labels to satisfy all the conditions. | The first line contains two integer numbers *n*, *m* (2<=≤<=*n*<=≤<=105,<=1<=≤<=*m*<=≤<=105).
Next *m* lines contain two integer numbers *v* and *u* (1<=≤<=*v*,<=*u*<=≤<=*n*,<=*v*<=≠<=*u*) — edges of the graph. Edges are directed, graph doesn't contain loops or multiple edges. | Print *n* numbers — lexicographically smallest correct permutation of labels of vertices. | [
"3 3\n1 2\n1 3\n3 2\n",
"4 5\n3 1\n4 1\n2 3\n3 4\n2 4\n",
"5 4\n3 1\n2 1\n2 3\n4 5\n"
] | [
"1 3 2 \n",
"4 1 2 3 \n",
"3 1 2 4 5 \n"
] | none | [
{
"input": "3 3\n1 2\n1 3\n3 2",
"output": "1 3 2 "
},
{
"input": "4 5\n3 1\n4 1\n2 3\n3 4\n2 4",
"output": "4 1 2 3 "
},
{
"input": "5 4\n3 1\n2 1\n2 3\n4 5",
"output": "3 1 2 4 5 "
},
{
"input": "2 1\n2 1",
"output": "2 1 "
},
{
"input": "5 10\n5 2\n4 1\n2 1\n3 4\n2 4\n3 2\n5 4\n3 5\n3 1\n5 1",
"output": "5 3 1 4 2 "
},
{
"input": "100 10\n73 55\n29 76\n15 12\n94 46\n77 67\n76 16\n72 50\n41 40\n89 75\n27 22",
"output": "1 2 3 4 5 6 7 8 9 10 11 13 14 15 12 18 19 20 21 22 23 25 26 27 28 29 24 30 16 31 32 33 34 35 36 37 38 39 40 42 41 43 44 45 46 48 49 50 51 53 54 55 56 57 59 60 61 62 63 64 65 66 67 68 69 70 72 73 74 75 76 52 58 77 79 17 71 80 81 82 83 84 85 86 87 88 89 90 78 91 92 93 94 47 95 96 97 98 99 100 "
},
{
"input": "100000 10\n4412 787\n97243 62644\n78549 66107\n43440 41961\n39621 35680\n87055 17210\n98544 2391\n74105 40774\n62295 1028\n76471 9423",
"output": "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155..."
}
] | 390 | 30,924,800 | 3 | 24,040 |
|
172 | BHTML+BCSS | [
"*special",
"dfs and similar",
"expression parsing"
] | null | null | This problem is about imaginary languages BHTML and BCSS, which slightly resemble HTML and CSS. Read the problem statement carefully as the resemblance is rather slight and the problem uses very simplified analogs.
You are given a BHTML document that resembles HTML but is much simpler. It is recorded as a sequence of opening and closing tags. A tag that looks like "<tagname>" is called an opening tag and a tag that looks like "</tagname>" is called a closing tag. Besides, there are self-closing tags that are written as "<tagname/>" and in this problem they are fully equivalent to "<tagname></tagname>". All tagnames in this problem are strings consisting of lowercase Latin letters with length from 1 to 10 characters. Tagnames of different tags may coincide.
The document tags form a correct bracket sequence, that is, we can obtain an empty sequence from the given one using the following operations:
- remove any self-closing tag "<tagname/>", - remove a pair of an opening and a closing tag that go consecutively (in this order) and have the same names. In other words, remove substring "<tagname></tagname>".
For example, you may be given such document: "<header><p><a/><b></b></p></header><footer></footer>" but you may not be given documents "<a>", "<a></b>", "</a><a>" or "<a><b></a></b>".
Obviously, for any opening tag there is the only matching closing one — each such pair is called an element. A self-closing tag also is an element. Let's consider that one element is nested inside another one, if tags of the first element are between tags of the second one. An element is not nested to itself. For instance, in the example above element "b" is nested in "header" and in "p", but it isn't nested in "a" and "footer", also it isn't nested to itself ("b"). Element "header" has three elements nested in it, and "footer" has zero.
We need the BCSS rules to apply styles when displaying elements of the BHTML documents. Each rule is recorded as a subsequence of words "*x*1 *x*2 ... *x**n*". This rule has effect over all such elements *t*, which satisfy both conditions from the list:
- there is a sequence of nested elements with tagnames "*x*1", "*x*2", ..., "*x**n*" (that is, the second element is nested in the first one, the third element is nested in the second one and so on), - this sequence ends with element *t* (i.e. tagname of element *t* equals "*x**n*").
For example, element "b" meets the conditions of the rule "a b" if for element "b" exists element "a" in which it is nested. Element "c" meets the conditions of the rule "a b b c", if three elements exist: "a", "b", "b", and in the chain "a"-"b"-"b"-"c" each following element is nested in the previous one.
Given a BHTML document and a set of BCSS rules, write a program that determines the number of elements that meet the conditions of each rule. | The first line of the input contains a BHTML-document. The document has length from 4 to 106 characters. The document has a correct structure, doesn't contain spaces or any other unnecessary characters. Tagnames consist of lowercase Latin letters, their lengths are from 1 to 10 characters.
The second line contains an integer *m* (1<=≤<=*m*<=≤<=200) — the number of queries. Then *m* lines contain the queries, one per line. Each query is a sequence *x*1,<=*x*2,<=...,<=*x**n*, where *x**i* is the *i*-th element of the query, and *n* (1<=≤<=*n*<=≤<=200) is the number of elements in the query. The elements are separated by single spaces. Each query doesn't begin with and doesn't end with a space. Each query element is a sequence of lowercase Latin letters with length from 1 to 10. | Print *m* lines, the *j*-th line should contain the number of elements of the document that correspond to the *j*-th BCSS-rule. If there are no such elements at all, print on the line 0. | [
"<a><b><b></b></b></a><a><b></b><b><v/></b></a><b></b>\n4\na\na b b\na b\nb a\n",
"<b><aa/></b><aa><b/><b/></aa>\n5\naa b\nb\naa\nb aa\na\n"
] | [
"2\n1\n4\n0\n",
"2\n3\n2\n1\n0\n"
] | none | [] | 46 | 0 | 0 | 24,057 |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.