In this task, you are going to determine whether Nai is ambidextrous, i.e. whether he can write equally well with both hands.
You have learned that Nai writes at speed $$$L$$$ characters per minute with his left hand and at speed $$$R$$$ characters per minute with his right hand. If $$$L = R$$$, then obviously Nai writes equally well with both hands; but we allow some leniency.
Given a parameter $$$P$$$, where $$$50 \le P \le 100$$$, Nai is considered ambidextrous if the writing speed of his weaker (slower) hand is at least $$$P\%$$$ that of his dominant (faster) hand, or if $$$L = R$$$, in which case it is hard to tell which hand is dominant. Otherwise, he is either left- or right-handed.
This simple task should not take you more than a few minutes, right?
The first and only line of input consists of three space-separated integers $$$L, R, $$$ and $$$P$$$.
For all test cases, $$$50 \le P \le 100$$$, $$$1 \le L, R \le 100$$$.
If Nai is ambidextrous, output Ambidextrous.
Otherwise, if Nai's dominant hand is his left hand, output Left-handed. Otherwise, output Right-handed.
18 37 50
Right-handed
19 37 50
Ambidextrous
100 99 100
Left-handed
Bob the builder wants to build the most exciting roller coaster in the world!
Due to the limited space, Bob can only build the railroad without turning left or right. Also, Bob thinks the only interesting part of roller coaster is going down, so he will not consider any parts of the railroad going upward as well.
Under the above conditions, you may consider the railroad as a polyline (broken line) on a 2D Cartesian coordinate plane. Formally, Bob has to build the railroad from $$$x=0$$$ to $$$x=N$$$. For each integer $$$x=i$$$ ($$$0\le i\le N$$$), Bob can choose the height $$$H_i$$$ of the railroad as any non-negative integer. In other words, the railroad is a polyline formed by the points $$$(0, H_0), (1, H_1), \dots, (N, H_N)$$$ (in this order), with the constraints that:
An example with $$$N=9$$$ and $$$H_{0..9}=\{4, 4, 3, 3, 3, 2, 2, 1, 1, 0\}$$$ To build an exciting roller coaster, Bob defines the exciting index as the number of times the passengers seeing new furthest part of the railroad. Formally:
In this example, passengers at the point $$$(1, H_1)$$$ cannot see the point $$$(6, H_6)$$$ as there exists point $$$(4, H_4)$$$ lying strictly above the line segment formed by $$$(1, H_1)$$$ and $$$(6, H_6)$$$. However, they can see the point $$$(4, H_4)$$$ as there are no points lying strictly above the line segment formed by $$$(1, H_1)$$$ and $$$(4, H_4)$$$.
An example with $$$f_{0..9}=\{1, 4, 4, 4, 8, 6, 8, 8, 9, 9\}$$$ and exciting index = 4(Note that $$$f_4=8$$$ as $$$(6, 2)$$$ is just lying on the line segment formed by $$$(4, 3)$$$ and $$$(8, 1)$$$, but NOT "strictly above")
Given the value of $$$N$$$, please write a program to help Bob finding any valid design maximizing the exciting index.
The only line contains a single integer $$$N$$$ ($$$1\le N\le 10^6$$$).
On the first line, output the maximum exciting index.
On the next line, output $$$H_0, H_1, \dots H_N$$$, representing the design of the railroad.
9
4
4 4 3 3 3 2 2 1 1 0
"Captain Teemo on duty!"
"Hut two three four" Perhaps Teemo loves consecutive numbers as many people do.
Tristana, as a lover of consecutive numbers (and also Teemo), asked you $$$Q$$$ questions. For each question, you have to determine whether the given number $$$X$$$ can be represented as a sum of at least two consecutive positive integers. For example, the answer for $$$X=9$$$ is Yes as $$$9=2+3+4$$$ while the answer for $$$2$$$ is No (note that $$$(-1)+0+1+2=2$$$ is invalid as only positive integers are allowed).
The first line contains a single integer, $$$Q$$$, denoting the number of questions Timo asked ($$$1\le Q\le 10^5$$$).
In each of the next $$$Q$$$ lines, a positive integer $$$X$$$ is given ($$$1\le X\le 10^9$$$).
For each of the $$$Q$$$ questions, print the answer in a single line: either Yes or No.
6
1
2
10
20
100
200
No
No
Yes
Yes
Yes
Yes
Write a program to differentiate a polynomial in x.
Here's how to differentiate a polynomial in the form
$$$c_nx^n+c_{n-1}x^{n-1}+\cdots+c_1x+c_0$$$
Each term $$$c_ix^i$$$ where $$$i \ge 1$$$ can be handled individually:
Finally, join the results of the terms together to form the final polynomial. Also, the derivative of the constant term is 0.
For example, the derivative of $$$-3x^3+5x^2-7x+4$$$ is $$$(-3\times 3)x^{3-1}+(5\times 2)x^{2-1}+(7\times 1)x^{1-1}+0$$$ = $$$-9x^2+10x-7$$$
The polynomial's format is similar to the requirements for math homework:
The input consists of a string: the polynomial. The maximum degree of $$$x$$$ is 99 and the coefficients are integers between -99 and 99, including -99 and 99 but excluding 0. The length of the string is at most 100.
It's guaranteed that the string strictly follows the format, i.e. +0x1-1x2+0 is not a possible input.
Output the derivative of the polynomial. It must strictly follow the format.
4x5-x2
20x4-2x
-6x+3
-6
5
0
-3x3+5x2-7x+4
-9x2+10x-7
Percy is a primary school student. He is learning how to do addition and subtraction. Being a smart boy, he decided to use a calculator instead of doing it by hand.
The calculator can only store and display non-negative integers of up to 8 digits, i.e. integers between 0 and 99999999 inclusive. If at any time the result of an operation exceeds this range, the calculator will display Error and stops functioning. Therefore, Percy tries to rearrange the terms in the expression he is trying to calculate.
Specifically, the expression contains $$$N$$$ numbers between -99999999 and 99999999 and Percy needs to sum them up using the calculator one by one. Since numbers can be summed up in any order, help Percy find an order that won't cause any error.
The first line contains an integer $$$N$$$, the number of integers that Percy needs to sum up ($$$1 \le N \le 10$$$).
Each of the next $$$N$$$ lines contains an integer between -99999999 and 99999999 inclusive.
If it is not possible to calculate the sum of the integers without causing calculator error, output Error.
Otherwise, in separate lines output ANY order of the $$$N$$$ integers that would not cause calculator error. The integers shall be added to / subtracted from the calculator one by one from top to bottom.
4
33334444
87654321
-10000000
-20000000
33334444
-10000000
-20000000
87654321
3
-100
-200
-300
Error
3
-400
10000
-7000
10000
-7000
-400
In sample 1, one of the possible order is +33334444 → -10000000 → -20000000 → +87654321. The results after the operations are 33334444, 23334444, 3334444 and 90988765. All of them are between 0 and 99999999 inclusive. Note that +87654321 → -20000000 → -10000000 → +33334444 is also acceptable. However, +87654321 → -20000000 → +33334444 → -10000000 is not an acceptable answer because the result after +33334444 is 100988765 which exceeds 99999999.
In sample 2, it will cause Error no matter how Percy reorders the operations.
Like in many countries, there is a football league in Hackerland. The top league (called Tourist League) has $$$N$$$ teams, where $$$N$$$ is an even number. The teams are conveniently numbered from $$$1$$$ to $$$N$$$.
For those unfamiliar with football leagues, here is the basic information that you need to know for this task. Three statistics determine a team's standing: points, goals scored, and goals conceded. For every match a team plays, they score goals and concede goals.
For two teams $$$A$$$ and $$$B$$$, let $$$P_A$$$ be the total points team $$$A$$$ has, $$$S_A$$$ be the total number of goals scored by team $$$A$$$, and $$$C_A$$$ be the total number of goals conceded by team $$$A$$$. Likewise for $$$P_B$$$, $$$S_B$$$, and $$$C_B$$$. Team $$$A$$$ is ranked higher than team $$$B$$$ if and only if one of the following is true:
There is no further tiebreaker. Therefore, it is possible for two teams to have the same rank. A team's rank is given by $$$1 + $$$ (number of teams ranked higher than it).
The final fixture in the league is always the most exciting one, especially at the top and at the bottom of the table, where teams fight fiercely for their respective targets. More important, all matches will start at the same time, and a team's fate may depend on scorelines of matches played hundreds of miles away!
For all $$$N$$$ teams in the Tourist League, you know their points, goals scored, and goals conceded, right before the final fixture. You also know the pairing for the final fixture.
Here is your task: based on your data, determine each team's best and worst final rank. For the sake of this task, there is no upper limit on the number of goals a team can score within a match; even a $$$10^9-0$$$ scoreline is considered possible.
The given data (points, goals scored, goals conceded) may be inconsistent, since they come from a questionable source. Nevertheless, you are to make predictions based on the data.
The pairing is guaranteed to be consistent, i.e. each team will play against exactly one other team.
The first line of input consists of an even integer $$$N$$$ ($$$2 \le N \le 5000$$$).
$$$\frac{N}{2}$$$ lines follow. The $$$i$$$-th line consists of two space-separated integers $$$A_i$$$ and $$$B_i$$$, meaning that team $$$A_i$$$ will play against team $$$B_i$$$ in the final fixture. Each of $$$1, 2, \dots, N$$$ appears exactly once in $$$\{A_1, \dots, A_{\frac{N}{2}}, B_1, \dots, B_{\frac{N}{2}}\}$$$.
$$$N$$$ lines follow. The $$$i$$$-th line consists of three space-separated integers $$$P_i$$$, $$$S_i$$$, and $$$C_i$$$, representing the points of, goals scored by, and goals conceded by team $$$i$$$ ($$$0 \le P_i \le 30000$$$; $$$0 \le S_i, C_i \le 10^5$$$).
Output $$$N$$$ lines. On the $$$i$$$-th line, output the best final rank of team $$$i$$$, followed by the worst final rank of team $$$i$$$. Separate the two numbers by one space.
2
1 2
3 100000 0
0 0 100000
1 2
1 2
4
1 3
2 4
4 4 1
2 3 3
4 4 3
0 1 5
1 3
1 4
1 3
3 4
2
1 2
1 1 0
5 2 7
2 2
1 1
10
2 3
9 10
1 6
7 8
4 5
47 65 12
34 34 20
34 36 24
28 32 22
23 25 25
23 21 28
19 23 34
15 21 32
14 17 39
5 11 49
1 1
2 3
2 3
4 4
5 6
5 6
7 7
8 9
8 9
10 10
Sample input 1 is an example of extreme scorelines. Team $$$2$$$ can still take first place, for example by winning the final game $$$262144 - 131072$$$.
Sample input 3 is an example of inconsistent data.
Sample input 4 is from this season's Hong Kong Premier League. (It turns out that the ranking is unchanged after the final fixture.)
One day, GFRIEND members went to a Go club to perform their song and learn how to play Go.
[video]output.mp4[/video]
To teach them the basic rules of Go, the master gave them $$$N$$$ white stones and $$$M$$$ black stones. He then ask them to use the white stones to surround the black stones on the standard 19x19 Go board. The arrangement must satisfy the followings:
The above rules can also be expressed by the followings:
Help GFRIEND by writing a program to find out if there is a possible arrangement, and if there is, output any one.
The input consists of 2 integers $$$N$$$ and $$$M$$$.
$$$1 \le N, M \le 80$$$.
If there is no possible arrangement that satisfies the rules, output Impossible.
Otherwise, output a 19x19 grid that contains any valid arrangement. Use . to represent empty space, o to represent white stone and x to represent black stone.
20 25
...................
...................
...................
...................
...................
...................
...................
...................
...................
...................
..........ooooo....
.........oxxxxxo...
.........oxxxxxo...
.........oxxxxxo...
.........oxxxxxo...
.........oxxxxxo...
..........ooooo....
...................
...................
21 10
...................
...................
...................
...ooo.............
..oxxxo............
...ooxo............
.....o.............
.........o..o......
........oxooxo.....
........oxxxxo.....
.........oooo......
...................
...................
...................
...................
...................
...................
...................
...................
5 2
Impossible
In Hackerland, sports programming is the most popular activity. More than half of its residents has a rating on Hackforces, the official programming competition platform there.
What is so unique about Hackforces is that 1-versus-1 matches are available! Two players can match online and enjoy a battle of wits anytime. The match can be boring though, if, for example, an absolute beginner with rating $$$1$$$ fights a Legendary Grandmaster with rating $$$10^9$$$ - everyone knows the outcome before the match starts.
Therefore, Hackforces has a handicap system, allowing the higher-rated player to play with some disadvantages, thus making the match more even. There are $$$N$$$ types of handicaps, and the $$$i$$$-th one is equivalent to reducing one's rating by $$$D[i]$$$ points. Here are some of them:
Seeing that many strong programmers do not voluntarily select a suitable handicap, Hackforces would like to install an auto-handicap feature. Given the ratings of two players $$$X$$$ and $$$Y$$$ ($$$X \le Y$$$), auto-handicap should automatically pick an index $$$i$$$ between $$$1$$$ and $$$N$$$ (inclusive), such that $$$Y - D[i] \gt 0$$$ and $$$|Y - D[i] - X|$$$ is minimized. In other words, the new rating $$$Y_{new} := Y - D[i]$$$ of the second player should be positive and as close to $$$X$$$ as possible.
There are three subtle details to keep in mind. First, if playing without handicap yields the smallest rating difference, then it is preferred to play without handicap. Second, if none of the handicaps can make $$$Y_{new}$$$ positive, then playing without handicap is the only choice. Third, if two or more different valid handicaps yield the same rating difference, then the one with the smallest index is preferred.
You are to implement this feature to pick the best handicap for $$$Q$$$ matches, according to the rules above.
The first line of input consists of a single integer $$$N$$$.
The second line of input consists of $$$N$$$ space-separated integers $$$D[1], D[2], \dots, D[N]$$$.
The third line of input consists of a single integer $$$Q$$$.
For the next $$$Q$$$ lines of input, the $$$i$$$-th line consists of a pair of integers $$$X_i$$$ and $$$Y_i$$$, the ratings of the two players in the $$$i$$$-th match. It is guaranteed that $$$X_i \le Y_i$$$.
For all test cases, $$$1 \le N, Q \le 10^5$$$, $$$1 \le D[1] \lt D[2] \lt \dots \lt D[N] \lt 10^9$$$, $$$1 \le X_i \le Y_i \le 10^9$$$.
Output $$$N$$$ lines, the $$$i$$$-th line corresponding to the $$$i$$$-th match.
If the best choice for the $$$i$$$-th match is to play without handicap, output $$$0$$$.
Otherwise, output the index corresponding to the chosen handicap.
5
2 3 5 10 100
5
1 1
1500 1501
1500 1502
1500 1504
10 65
0
0
1
2
4
Byteland is a country consisting of $$$N$$$ cities, numbered from $$$1$$$ to $$$N$$$, and they are connected by $$$M$$$ bidirectional roads. It is possible to reach each city from any other. Each road connects two cities $$$a$$$ and $$$b$$$. Two cities are neighbor if and only if they are directly connected by a road.
Scientists in Byteland found out that there will be an outbreak of Virus B in the coming weeks. The virus will first appear in one of the $$$N$$$ cities, make it into an infected city. Then, on each day the virus will spread to exactly one uninfected city which is neighbor to an infected city. the The infection process will end when all $$$N$$$ cities become infected.
As Virus B is a fatal and strong virus, they want to make some precautions against Virus B to minimize the impacts. Luckily, the scientists found out that Virus B has similar DNA structure with another Virus A, which appeared in Byteland 100 years ago, so the scientists predict that Virus B will infect different cities in a similar way as Virus A. More precisely, let the order of cities infected by Virus A be $$$A_1, A_2,\dots ,A_N$$$ and the order of cities infected by Virus B be $$$B_1, B_2, \dots, B_N$$$, the scientists predict that $$$B$$$ will be the lexicographically smallest ordering such that is also lexicographically greater than $$$A$$$.
An ordering $$$P_1, P_2, \dots, P_N$$$ is considered lexicographically smaller than another ordering $$$Q_1, Q_2, \dots, Q_N$$$, if $$$P_i \lt Q_i$$$, for the first $$$i$$$ where $$$P_i$$$ and $$$Q_i$$$ differ.
Given the structure of Byteland and the infection log of Virus A, please help the scientists to predict the order of cities infected by Virus B or print $$$-1$$$ if such ordering does not exist.
The first line contains two integers $$$N$$$ and $$$M$$$, the number of cities and roads in Byteland $$$(1 \le N, M \le 10^5)$$$.
The following $$$M$$$ line contains two integers $$$u_i$$$ and $$$v_i$$$, describing the $$$i^{th}$$$ road connecting city $$$u_i$$$ and $$$v_i$$$ $$$(1 \le u_i, v_i \le N)$$$.
The last line contains $$$N$$$ integers $$$A_i$$$, describing the order of cities infected by Virus A 100 years ago.
It is guaranteed that it is possible to reach each city from any other in Byteland. Also, it is guaranteed that the given ordering must be a valid infection ordering.
Output $$$N$$$ integers in the first line, the predicted infection ordering of Virus B. If such ordering does not exist, output $$$-1$$$ in the first line.
4 4
1 2
1 3
1 4
3 4
3 1 2 4
3 1 4 2
4 3
1 2
2 3
3 4
4 3 2 1
-1
In the first sample, the order of cities infected by Virus A is as follow:

Therefore, the lexicographically smallest infection order for Virus B, which is lexicographically greater than infection order for Virus A, is $$$3, 1, 4, 2$$$
In East Hackerland, there is a country called Jakanda which possesses highly advanced technology. It consists of $$$N$$$ cities and $$$N - 1$$$ bidirectional roads. Each road has its own length and connects two cities. It is possible to travel from a city to any other city via these roads.
The Black Panda, king of Jakanda, is facing a problem. Some of the cities are having riot. A city under riot would send out a rebel army and try to capture one other city ("targeted city"). Black Panda is not going to let it happen. He would send a scout from the targeted city to the city under riot. Both the rebel and scout will walk for 1 mile toward their own destination during the day and rest at night. The scout would only spot the rebel army only if they are at the same location and it is at night. If the scout fails to spot the army, the rebel army would capture its target city.
Other than the riot, the length of some of the roads in Jakanda would change due to damage of the war.
Now there are $$$Q$$$ events in chronological order. Each event is one of the two types:
1 $$$u$$$ $$$v$$$, which denote city $$$u$$$ is now under riot and is sending a rebel army to capture city $$$v$$$. Note that city $$$v$$$ might have rebellion or captured before, but in these cases we would consider Black Panda had already suppressed it.
2 $$$i$$$ $$$l$$$, which denote the $$$i^{th}$$$ road is now changed to length $$$l$$$.
For each type 1 event, output JAKANDA FOREVER if the scout could spot the rebels, output WE NEED BLACK PANDA otherwise.
The first line contains a single integer $$$N$$$, the number of cities ($$$1\le N\le 5\times 10^5$$$).
The following $$$N - 1$$$ lines contains the roads information, each line contains three integers: $$$u_i$$$, $$$v_i$$$ and $$$l_i$$$, which means the $$$i^{th}$$$ road connect city $$$u_i$$$ and $$$v_i$$$ with length of $$$l_i$$$ miles ($$$1\le u_i, v_i\le N$$$; $$$u_i\neq v_i$$$; $$$1\le l_i\le 10^9$$$).
Then a single line is followed, which contains a single integer $$$Q$$$ ($$$1 \le Q\le 5\times 10^5$$$).
The following $$$Q$$$ lines contains the event information, each line contains three integer. The first integer $$$type$$$ denote the event type.
For $$$type = 1$$$, two integers, $$$u$$$ $$$v$$$ is followed. $$$u$$$ is the city under riot and $$$v$$$ is the target city ($$$1\le u, v\le N$$$; $$$u\neq v$$$).
For $$$type = 2$$$, two integers, $$$i$$$ $$$l$$$ is followed. It means the $$$i^{th}$$$ road's length is changed to $$$l$$$ miles.
For each type 1 event, output JAKANDA FOREVER if the scout is going to spot the rebels, WE NEED BLACK PANDA otherwise.
4
1 2 3
1 3 4
4 1 5
5
1 1 2
1 2 3
2 1 2
1 2 1
1 4 2
WE NEED BLACK PANDA
WE NEED BLACK PANDA
JAKANDA FOREVER
WE NEED BLACK PANDA
The structure of Jakanda before the third event: 
The structure of Jakanda after the third event: 
What do kids do when they go dine at a Chinese restaurant ("yum cha")? Back when portable game consoles and smartphones were not available, some kids may opt to play with toothpicks, forming figures on the dining table.
Hand-written digits contain curly strokes, but by using the seven-segment display, digits can be represented neatly using only straight segments. The diagram below shows digits $$$0$$$ to $$$9$$$ in seven-segment display:
Alex wants to form a number between $$$0$$$ and $$$99$$$ (inclusive), by using toothpicks as segments in the seven-segment display (one toothpick per segment, naturally). He insists on forming exactly using two digits, so $$$0$$$ will be written as $$$00$$$, $$$1$$$ will be written as $$$01$$$, and so on.
Assume the tens digit is $$$X$$$ and the ones digit is $$$Y$$$. To use fewer toothpicks, Alex invents a rule for merging $$$X$$$ and $$$Y$$$. First, Alex defines the left part of a digit as the two vertical segments on its left. Similar for right part. Refer to the following diagram.
If the right part of $$$X$$$ and the left part of $$$Y$$$ are the same, then the parts can be merged, allowing a more compact and toothpick-friendly representation. See the following example.
There is one exception to this rule. As you may have noticed, if $$$X = 1$$$, then after merging it will look like one single digit. Therefore, Alex will not merge the two digits if the tens digit $$$X$$$ equals $$$1$$$.
Alex is trying to form $$$T$$$ numbers. For each number, determine the number of toothpicks needed to form the number. Whenever digit-merging is possible, Alex will merge the digits.
The first line of input consists of an integer $$$T$$$, the number of queries.
$$$T$$$ lines follow. On the $$$i$$$-th line, there is an integer in the range $$$[0, 99]$$$, representing the $$$i$$$-th number that Alex wants to form. Leading zeroes will be added to integers smaller than $$$10$$$, so that each line consists of two digits.
Other than the sample, your program will be judged on exactly one other test case. For that test case, $$$T = 100$$$.
Output $$$T$$$ lines. On the $$$i$$$-th line, output one integer, the number of toothpicks needed to form the $$$i$$$-th number.
8
10
00
24
88
89
75
33
11
8
10
8
12
13
8
10
4
Hackerland's Theme Park has an exciting attraction: a labyrinth that can be treated as a $$$3 \times W$$$ grid. It has three special squares: $$$A = (1, a)$$$, $$$B = (3, b)$$$, and $$$X = (2, 1)$$$. Two players will start at $$$A$$$ and $$$B$$$ respectively and will try to reach $$$X$$$ as quickly as possible. In one step, a person can move one square up, down, left, or right. It is possible for both of them to be located at the same square at the same time.
For convenience, let $$$dist(U, V)$$$ denote the distance between squares $$$U$$$ and $$$V$$$, i.e. the number of steps needed to reach $$$V$$$ from $$$U$$$. If $$$V$$$ is not reachable from $$$U$$$, then $$$dist(U, V) = \infty$$$.
You are concerned that the game may be unfair, if $$$dist(A, X) \neq dist(B, X)$$$. Therefore, you are going to place some (possibly zero) obstacles at some of the squares, so that $$$dist(A, X) = dist(B, X) \lt \infty$$$.
Originally, the grid has no obstacles. You cannot add obstacles to squares $$$A$$$, $$$B$$$, and $$$X$$$. Find a way to create a fair labyrinth!
The first and only line of input consists of three space-separated integers $$$W$$$, $$$a$$$, and $$$b$$$.
For all test cases, $$$1 \le a, b \le W \le 100$$$.
If there is no way of adding obstacles so that $$$dist(A, X) = dist(B, X) \lt \infty$$$, output Impossible.
Otherwise, output Possible followed by three lines. Output $$$W$$$ characters on each of the next three lines. The $$$j$$$-th character of the $$$i$$$-th line should be:
If there are multiple solutions, output any one of them.
1 1 1
Possible
A
X
B
4 3 2
Impossible
3 3 1
Impossible
9 3 7
Possible
**A....**
X.**...**
.....*B**