The Institute of Geography and Territorial Organization of the University of Lisbon (IGOT, in Portuguese), is conducting research projects at Pico Island. One of the research projects concerns the level curves of Mount Pico, the highest mountain in Portugal. Using image processing software, the researchers transform such curves into simple polygons (that do not self intersect).
To understand how the terrain changes with respect to height, IGOT is interested in the monotonicity of a level curve with respect to a set of directions (vectors). Given a vector $$$v$$$, we say that a simple polygon is $$$v$$$-monotone if any non-empty intersection of a straight line orthogonal to $$$v$$$ with the polygon is a segment (or a single point). You are one of the IGOT developers and they entrusted you with the following task. Given a simple polygon and a set of vectors, determine for each vector if the polygon is monotone with respect to this vector.
The first line has two integers $$$N$$$ and $$$Q$$$, the number of vertices of the polygon and the number of queries, respectively. Each of the next $$$N+Q$$$ lines has two integers. The $$$i$$$th of these lines represents the point $$$(x_i,y_i)$$$ if $$$i \leq N$$$; otherwise it represents the vector $$$(v_x, v_y)$$$. The polygon vertices are listed in counter clockwise order.
Constraints
For each query, print "Y" (without quotes) if the polygon is monotone with respect to the corresponding vector, otherwise print "N".
6 2
4 0
4 2
1 2
1 4
0 4
0 0
0 1
3 2
Y
N
6 4
0 0
16 2
0 10
-10 0
0 -10
20 -4
10 10
10 -10
0 -20
-20 10
N
N
Y
N
In Figure $$$1$$$ we display the polygon of the first example and the vector $$$\overrightarrow{DG}$$$ that represents the second query.
One of the greatest poets in Portuguese language was Luiz Vaz de Camões, born in Lisbon circa 1524. Camões works are fundamental reading for high school students in countries such as Brazil or Portugal. Many consider his 5- and 7-syllable poems and his sonnets (with 14 verses) to be the most important lyrical work in Portuguese of all time.
Camões was a visionary in his own time. His work has been studied from many aspects: historical, cultural, symbolical, and so on. Carlitos "the efficient" Nunes is studying aesthetics in Camões' poetry. He is especially interested in a metric based on the size of the verses of a poem. We define this metric as follows. Suppose we have a poem made of $$$N$$$ verses. We say that the poem is $$$K$$$-elegant if $$$K \gt 1$$$, $$$N$$$ is a multiple of $$$K$$$ and, moreover, there are exactly $$$N/K$$$ verses whose sizes, when divided by $$$K$$$, have remainders equal to $$$i$$$, for $$$i=0,1, \ldots, K-1$$$. Note that a single poem may be $$$K$$$-elegant for several distinct values of $$$K$$$.
Carlitos already processed the data from poems by Camões and now he needs your help to determine the smallest elegance in each of the poems.
The input has two lines. The first line has an integer $$$N$$$, the number of verses in the poem. In the second line you are given $$$N$$$ integers, say $$$\ell_1, \ell_2, \ldots, \ell_N$$$, such that $$$\ell_i$$$ represents the size of the $$$i$$$th verse of the poem.
Constraints
A single integer $$$K$$$, the smallest integer such that the poem is $$$K$$$-elegant. If no such integer exists, print "-1".
6
3 6 1 7 8 14
2
5
17 21 14 35 13
5
In the first case, if $$$K = 2$$$, then $$$6, 8$$$ and $$$14$$$ (respectively, $$$3$$$, $$$1$$$ and $$$7$$$) have remainder $$$0$$$ (respectively, remainder $$$1$$$) when divided by $$$K$$$.
The city of Porto will host the ICPC World Finals in 2019. One of the secret touristic spots in the city is the so-called "lake of the thousand bridges". Mr. Manoel Pontes (Pontes stands for "bridges" in Portuguese; this is amazingly his real name$$$\ldots$$$) built this wonder in the lake with a lifetime of hard work. The lake has many small islands. Mr. Manoel built small wooden bridges connecting the islands. In some cases, there are multiple bridges connecting some pairs of islands. Visitors enjoy themselves while promenading through the bridges and small islands. Besides, by walking through the bridges the visitors can go from one island to any other island.
Mr. Manoel wants to enhance this touristic attraction to get even more visitors. One of this ideas is to organize visitors games. He plans to have the following challenge: is it possible to start to walk from outside the lake, pass through all the bridges without repetition, and go back to the starting point (that is, back out of the lake)?
Before creating the attraction, he himself tried it over and over but could not find out if this was possible. To make the challenge even more interesting, Mr. Manoel will allow adding some bridges between some islands. He wants to know if, by adding some of these bridges, the walk he wants the game to have becomes possible. Your task in this problem is to decide if it is possible to choose some (possibly none) of these bridges that, when added, allow people to walk as described.
The first line has three integers $$$N$$$, $$$M$$$ and $$$K$$$, where $$$N$$$ is the number of islands, $$$M$$$ is the number of already built bridges and $$$K$$$ is the number of bridges that is possible to add. The islands are represented by distinct integers from $$$1$$$ to $$$N$$$. Each one of the following $$$M+K$$$ lines describes a bridge. Each one of them has two integers, $$$a$$$ and $$$b$$$, that represent the existence or possibility of adding a bridge between islands $$$a$$$ and $$$b$$$. The first $$$M$$$ lines describe bridges that already exist and the next $$$K$$$ lines describe bridges that you may add.
Constraints
In the first line print "YES" (without the quotes), if such a walk is possible or "NO" otherwise. If there is a solution, print in the second line an integer $$$R$$$, $$$0 \leq R \leq K$$$ that is the number of bridges that need to be added. Next print $$$R$$$ lines, each one with two integers, the islands that are connected by this bridge, in any order. If there are multiple solutions, any one will be accepted.
4 3 4
1 2
2 3
3 4
1 4
1 4
1 3
3 4
YES
1
1 4
4 3 2
1 2
2 3
3 4
1 2
3 4
NO
In Portugal there are very popular political parties, the Social Democrat Party (PSD, in Portuguese) and the Socialist Party (PS, in Portuguese). There will be presidential elections in Portugal in 2019 and the political parties are working on the best way to run advertising campaigns to reach the maximum number of voters.
Due to the high cost of elections advertising, PSD and PS wish to broker a deal and split the costs of advertising. The people in charge of the negotiation have data about political preference of some people in Portugal. They have been processing this information and have thus obtained the geographical position of people that are likely voters for PSD and PS. This position is represented by a point in the plane. To calculate the expenses, they wish to find two disjoint areas that, when summing likely voters for these parties, the result is the largest possible. To facilitate the computations, they decided to consider both areas to be rectangles with sides parallel to the axes. In other words, they wish to find two rectangles, one representing PSD and the other representing PS, such that the sum of the number of likely voters of PSD in the first rectangle and the number of the likely voters of PS in the second rectangle is maximum. You were hired to develop a software that fulfills this task.
The first line has an integer $$$N$$$, the number of people whose data will be considered. The next $$$N$$$ lines describe each person's data. The $$$i$$$th of these $$$N$$$ lines has two integers and a character, say $$$x_i$$$, $$$y_i$$$ and $$$p_i$$$. The integers represent a point $$$(x_i, y_i)$$$ and $$$p_i$$$ represents the political preference of the $$$i$$$th person.
Constraints
The maximum value that can be obtained when summing the number of likely voters in each rectangle. In this problem, we consider that points in the boundary are part of the rectangle.
3
0 0 b
0 1 b
1 1 b
3
4
0 0 b
1 1 w
2 2 b
3 3 w
3
4
-1 -1 b
0 0 b
1 1 w
2 2 w
4
Recent studies in Education show great pedagogical benefits of working in groups, especially in the freshman and sophomore years in high school. Different groups of various sizes are great and allow students to understand their differences and to train in teamwork. A teacher at a modern school wants to know how many different groups he can form in this class of $$$N$$$ students. The only requirement is that a group must have at least 2 students, and two groups are distinct if at least one student is in one of them and no in the other.
Your task in this problem is, given $$$N$$$, compute the number of distinct groups that is possible to create in the class.
A single line with an integer $$$N$$$.
Constraints
A single line with the number of distinct groups that is possible to have in the class.
2
1
3
4
In the first example we have two students, therefore we can only have one group. In the second example we have three students, say $$$A$$$, $$$B$$$ and $$$C$$$. We can form the groups $$$AB$$$, $$$AC$$$, $$$BC$$$ and $$$ABC$$$.
The Ministry of Construction, Transportation and Communications of Portugal is planning to improve transit routes throughout the country. The ministry's main concern is the delay everyone suffers daily in traffic when some road gets blocked. A key part of the ministry's plan is to identify roads that harm people's commute time the most. Hence, they contacted the Institute of Efficient Mobility (IME, in Portuguese).
IME is developing a software that, when given a description of the transit routes in Portugal and their usage information, finds the routes that harm the population the most when blocked. The person in charge of this important project is Marcel "the optimizer" Saito. Marcel is currently busy with administrative tasks and organizing the workforce. He has faith in you and so he is entrusting you with designing the key part of the software. You must solve the following problem: given two locations in a city, say $$$s$$$ and $$$t$$$, one wants to find the worst delay incurred when a person travels from $$$s$$$ to $$$t$$$, when exactly one road in the city gets blocked. As a pragmatic choice, you may assume that people always choose a path that takes the least time to travel from one place to another.
The first line of input has two integers, $$$N$$$ and $$$M$$$, the number of locations in the city and the number of roads connecting these locations. The locations are numbers $$$1$$$ to $$$N$$$. The second line has two integers, $$$s$$$ and $$$t$$$. Each one of the following $$$M$$$ lines has three integers, say $$$u$$$, $$$v$$$ and $$$w$$$, that describe a road that connects locations $$$u$$$ and $$$v$$$ that has a traversal time of $$$w$$$. It is guaranteed that there exists a way to reach $$$t$$$ from $$$s$$$.
Constraints
A single integer, the greatest time it takes to go from $$$s$$$ to $$$t$$$ through a shortest path when exactly one road in the city gets blocked. If there is a road that disconnects $$$s$$$ and $$$t$$$ when blocked, print "-1" (without the quotes).
10 12
1 5
1 4 1
1 2 6
1 3 2
2 3 2
3 5 3
3 8 3
4 6 5
5 7 1
5 9 5
6 10 20
7 8 7
9 10 3
13
6 7
2 3
1 2 2
1 3 3
2 3 1
3 4 1
4 5 5
4 6 4
5 6 0
5
6 7
1 6
1 2 2
1 3 3
2 3 1
3 4 1
4 5 5
4 6 4
5 6 0
-1
The penitentiary at Viana do Castelo is one of the largest ones in Europe. The building has thousands of jail cells, laid out linearly in several levels, and numbered so that adjacent numbers are given to adjacent cells, be it adjacent cells on the same level, or one on top of the other, or connected by an adjacent stairway. It is not really known why, but the jail cells are numbered starting from a negative number, so that cell number zero is right in the middle of the penitentiary, exactly at the center of the middle level of the building. Probably the first warden, who assigned the cells numbering, did this for entertainment $$$\ldots$$$
Each guard in the penitentiary gets an interval of cells to watch. He must go over these cells and check that the inmates did not escape, are doing okay, and so on. The warden does two operations depending on the intervals of cells assigned to each guard:
Your task in this problem is to read the number $$$N$$$ of guards in the penitentiary and, for each guard, the interval of cells he is in charge of watching over. Next, you will read the $$$Q$$$ operations carried out by the warden, by executing an operation of the first type or by answering the query for operations of the second type.
The first line has two integers $$$N$$$ and $$$Q$$$, the number of guards and the number of operations to carry out, respectively. The next $$$N$$$ lines describe the guards information. The $$$i$$$th of these lines has two integers, $$$L_i$$$ and $$$R_i$$$, the interval of cells assigned to the $$$i$$$th guard. Finally, the next $$$Q$$$ lines describe the operations chosen by the warden. Each one of these lines represents an operation as follows
Note that, at each moment, each guard has a single interval of cells assigned to him.
Constraints
For each operation of type '?', print a line with the answer.
3 4
1 100
5 10
7 20
? 1 3
? 2 3
C 1 8 8
? 1 3
4
4
1
2 3
1 2
9 10
? 1 1
? 2 2
? 1 2
2
2
0
Porto is known worldwide for its wine production. These wines are produced in the outskirts of the city and then aged in oak barrels to obtain their unique flavor. The wineries must follow strict production standards to get the seal of approval that helps with sales. To attain these very high standards, the temperatures at the vineyards are measured daily and must satisfy very specific requirements, according to studies by experts.
In order for a grape to reach optimum development, it is important to known how many different temperatures it faced during its growth and in how many days each of these temperatures were repeated. Recent studies show that the quality of the wine is deeply correlated with the following number. Consider the growth period for the grape and count, in those days, how many distinct temperatures we had, and for each temperature, how many times it was repeated in the period. We say that the grapevine has quality $$$x$$$ if we have at least $$$x$$$ distinct temperatures that were repeated in $$$x$$$ days during the growth of its grapes.
Your task is to compute the quality of production of each grapevine in the vineyard. You are given the temperatures measured in $$$N$$$ days in the vineyard. Next, for each one of the $$$Q$$$ grapevines in the vineyard, you are given the start and end days of their growth periods. For each one of these grapevines, you must compute the maximum quality of their production.
The first line has two integers $$$N$$$ and $$$Q$$$, the number of days of temperature measurements in the vineyard and the number of grapevines, respectively. The second line has $$$N$$$ blank-separated integers. The $$$i$$$th integer is the temperature measured on day $$$i$$$. Finally, the next $$$Q$$$ lines describe each one of the grapevines. The $$$i$$$th of these lines has two integers, $$$\ell_i$$$ and $$$r_i$$$, the starting and ending day of growth of the grapes in the $$$i$$$th grapevine.
Constraints
Print $$$Q$$$ lines, the $$$i$$$th of which is the maximum quality of grapevine $$$i$$$.
6 3
1 2 3 1 2 1
1 6
2 4
1 5
2
1
2
In the first grapevine we had 3 distinct temperatures. Only one gets repeated at least three times and two of them are repeated twice, so the maximum quality of the grapevine is $$$2$$$.
In the second grapevine the temperatures were all distinct, so the maximum quality is $$$1$$$. Finally, the maximum quality of the third grapevine is 2, since we had two temperatures that were repeated twice.
Tea is second most consumed beverage in the world after water, especially in the United Kingdom, where about $$$84$$$% of the population has tea every day. This British tradition with tea is a well known fact. What few people know is that English tea has its origins in Portugal. In 1662, when Catarina de Bragança married King Charles II of England, she traveled to London and took with her some tea leaves. The story goes that the boxes where these leaves were carried were labeled as "Transport of Aromatic Herbs", later abbreviated in Portuguese to TEA.
The historian Gaia "the Curious" Fontenelle is studying some old documents that describe how tea was transported from Portugal to England. Initially, $$$N$$$ boats were anchored in Portugal. Each boat was assigned a distinct integer between $$$1$$$ and $$$N$$$. The documents say that the tea was to be transported in a total of $$$K$$$ trips. These trips would use ports in Portugal, China and England. After many ruminations, Gaia reached these conclusions:
Gaia quickly noticed that, with these rules, each port looks like a stack, where a trip corresponds to moving the smallest boat (in the top) of the origin stack to the destination stack (while satisfying constraint $$$1$$$). Gaia was used to dealing with imprecise or downright false sources of information, so she started playing in her notebooks and she realized that, depending on the values of $$$N$$$ and $$$K$$$, sometimes it was impossible to move all $$$N$$$ boats (even while using the port in China). For this reason, for each report about tea transport, she wants to find out whether it was possible or not.
Gaia got hungry and left for coffee, chocolate and taking pictures of the sunset. You are an intern in her team and wish to impress her (maybe you will get the job this way). Thus, you decided to solve the problem before she returns.
The input has a single input line with integers $$$N$$$ and $$$K$$$, the number of boats and the total number of trips by the boats, respectively.
Constraints
In the first line print "Y" (without quotes) if it is possible to move the $$$N$$$ boats from Portugal to England, while respecting the constraints described above. Otherwise, print "N". If the answer is affirmative, print $$$K$$$ lines that describe a itinerary of trips that moves the $$$N$$$ boats. To represent a trip from port $$$X$$$ to port $$$Y$$$, print "$$$X$$$ $$$Y$$$". Each port is represented by a character as follows:
2 3
Y
A B
A C
B C
2 1
N
2 6
Y
A B
B A
A C
C B
A C
B C
The output of the first example described the following itinerary:
Altercations between Brazil and Portugal go a long way back. The most recent such skirmish happened in 2016 when these two countries had a "meme war". The journalist Fiorentina "sagacious" Azzellini is studying this event in her work of describing ways in which social networks have changed how people interact.
Brazil flooded the internet with a huge amount of memes, and won the contest in three days. Fiorentina, while investigating how such a huge amount of images could be created, found that some of them could be constructed very simply. The meme was made of an image that had the national flags of Brazil and Portugal, and one word. The word $$$S(x)$$$ was created from a letter $$$x$$$ and has the following composition:
where $$$p(x)$$$ is the letter that precedes $$$x$$$ in the alphabet and $$$A \cdot B$$$ denotes the concatenation of words $$$A$$$ and $$$B$$$. Fiorentina started playing with distinct letters and wrote in her notebook
$$$S($$$'c'$$$)$$$ = 'abacaba',
...
Sagacious as she is, Fiorentina quickly noticed that the word $$$S($$$'z'$$$)$$$ is huge. Not willing to write it down in her notebook, but still curious about some of the letters in this word, she asked for you to help. She would like to know what the letter in the $$$n$$$th position of the word $$$S($$$'z'$$$)$$$ is.
A single integer $$$n$$$.
Constraints
A single character, the letter in the $$$n$$$-th position of the word $$$S($$$'z'$$$)$$$.
1
a
Portuguese children's pastimes are very sophisticated. Some of them have very complex rules and their description can get even a grown up dumbfounded, but somehow the kids play with them and have lots of fun. One of them goes by the name of "spin-and-turn". In this game, one of the players writes down an ordering of the participants, and this ordering remains secret. Some other player then tries to guess this ordering with spin-and-turn moves. The idea is to apply several times the same permutation of the participants, starting from the ordering of the children by their ages. If at some point the secret permutation is found, the player wins. Otherwise, the kid who came up with the secret ordering wins.
Let's see an example. Imagine that the secret ordering is $$$(4, 2, 1, 3)$$$, that is, the oldest child comes first, next the second youngest, followed by the youngest child and then the third one. One of the players could play with the permutation $$$(3, 1, 4, 2)$$$. If we apply this permutation from the age ordering $$$(1, 2, 3, 4)$$$ we get $$$(3, 1, 4, 2)$$$. If we do this again on this ordering, we get $$$(4, 3, 2, 1)$$$. And then $$$(2, 4, 1, 3)$$$ and then we get back to the starting ordering $$$(1, 2, 3, 4)$$$. So this player has lost, and the player that chose the secret ordering won. In the game, the kids keep moving, spinning and turning, hence the name$$$\ldots$$$
This very sophisticated game intrigued a famous Portuguese mathematician who decided to study it. Your task in this problem is to help this mathematician. Given a secret ordering $$$p$$$ of the players, we want to find how many permutations $$$q$$$ may result in $$$p$$$ when applied a finite number $$$k \geq 0$$$ of times, as in the example above.
The first line has two integers $$$N$$$ and $$$k$$$, the number of children and the number of times we want to apply a permutation $$$q$$$ to obtain $$$p$$$, respectively. The second line has $$$N$$$ integers separated by a blank space, which gives the ordering represented by permutation $$$p$$$.
Constraints
Print an integer, the number of permutation $$$q$$$ that, when applied $$$k$$$ times, end up with permutation $$$p$$$. Print this answer modulo $$$10^9 + 7$$$.
5 60
1 2 3 4 5
120
5 2
3 5 1 4 2
2