Two pawns found themselves stuck in an $$$N$$$ by $$$M$$$ matrix consisting of arrows (up, right, down, left), where no arrow points to the outside of the matrix. The only way they can escape is if they manage to meet in a matrix cell at the same time.
Each second, both pawns will make one move in the direction shown by the arrow from their current cell.
Your job is to make exactly one change to an arrow by orientating it to another direction than the initial one (no arrow should point to the outside of the matrix). A change is valid, if when the pawns start moving from their initial positions, they will meet in the same matrix cell at some point in time.
You need to calculate the number of different valid changes. Two changes are different, if the resulting matrices are different from each other.
The first line of the input contains the integers $$$N$$$ and $$$M$$$ ($$$1 \leq N*M \leq 10^6$$$), denoting the number of lines and columns of the matrix.
The second line contains two integers $$$X_1$$$ and $$$Y_1$$$ ($$$1 \leq X_1 \leq N$$$, $$$1 \leq Y_1 \leq M$$$), denoting the initial position of the first pawn.
The third line contains two integers $$$X_2$$$ and $$$Y_2$$$ ($$$1 \leq X_2 \leq N$$$, $$$1 \leq Y_2 \leq M$$$), denoting the initial position of the second pawn.
Each of the following $$$N$$$ lines contains a string of $$$M$$$ characters, describing the configuration of the arrows of that line. Thus, the $$$j$$$-th character of the $$$i$$$-th line ($$$1 \leq i \leq N$$$, $$$1 \leq j \leq M$$$) describes the orientation of the arrow from the line $$$i$$$ and column $$$j$$$ in the matrix:
It is guaranteed that no arrow points to the outside of the matrix.
Output the total number of different valid changes.
2 3 1 2 2 1 222 113
7
5 3 2 1 1 2 222 222 222 222 113
22
The initial matrix of the first example is:
$$$$$$ \begin{matrix} \downarrow & \downarrow & \downarrow\\ \rightarrow & \rightarrow & \leftarrow \end{matrix} $$$$$$
For the sake of having a problem statement, Bob has a large collection of $$$N$$$($$$1 \leq N \leq 10^5$$$) balls of radius $$$R$$$ $$$(1 \leq R \leq 10)$$$. He wants to roll them from one side of a table to the other. You might imagine the table as an 1D axis of infinite length.
A ball will roll from a given position $$$p_i$$$ ($$$0 \leq p_i \leq 10^9)$$$ with a non-zero speed $$$s_i (-10^9 \leq s_i \leq 10^9)$$$ to the left (negative speeds) or to the right (positive speeds) edge of the table, without ever falling down (the table is magical and has infinite length!). All balls start rolling at the same point in time, 0.
Now, Bob is wondering: given these balls, and the fact that they move from left to right or from right to left, what is their position during a moment in time, $$$T$$$ $$$(0 \leq T \leq 10^9)$$$? Of course, you can consider that the collision between two balls happens instantly, without friction, hence, after a collision, the balls simply swap their speeds.
The first line of input contains three integers: N($$$1 \leq N \leq 10^5$$$), R($$$1 \leq R \leq 10$$$), T($$$0\leq T \leq 10^9$$$), representing the number of balls, the radius they have and the moment in time during which you must know where the balls are.
$$$N$$$ more lines will follow. Line $$$i$$$ will contain two positive integers, $$$p_i$$$($$$0 \leq p_i \leq 10^9$$$) and $$$s_i$$$($$$0 \lt |s_i| \leq 10^9$$$), denoting the initial position and the speed of ball $$$i$$$. If a ball rolls to the left, its speed will be $$$s_i$$$, where $$$-10^9 \leq s_i \leq -1$$$. Similarly, if a ball rolls to the right, its speed will be $$$s_i$$$, with $$$1 \leq s_i \leq 10^9$$$. It is guaranteed that initially the balls do not overlap, but they can touch each other.
On a line, print $$$N$$$ values $$$p_i$$$ representing the position of all balls at moment of time $$$T$$$. Note that the positions should be printed in a non-decreasing order.
3 1 5 3 -1 8 -2 1 3
-6 -2 20
After getting sick of being defeated in Battleships by his friend, Johnny invites him to play a new game of his own design - Mega Battleships. Mega Battleships plays exactly like Battleships, but the board is a lot bigger. The board in this game is $$$10^{6}$$$ by $$$10^{6}$$$.
Because the board is so big, he needs a little help to keep track of what is going on. Given the layout of his boats and the shots his opponent took, can you output the number of boats that were sunk? As a reminder, a boat is considered sunk, if all its cells have been hit.
The first line of the input consists of $$$K$$$ ($$$1 \leq K \leq 10^{6}$$$) pairs of coordinates for the battleships, $$$c_1k$$$ and $$$c_2k$$$, representing the top left corner and bottom right corner respectively. Each pair is separated by a comma. We guarantee that the ships do not overlap.
The second line of the input is the list of distinct coordinates of the shots that were performed by the second player. There are at most $$$10^{6}$$$ such coordinates. They are separated by commas.
All the coordinates are provided in row-column order, with numbers for rows and letters for columns. For example, the top left corner's coordinate is 1A, the cell to its right is 1B, and the cell underneath it is 2A. Generally, the structure of a coordinate $$$c_k$$$ is $$$n_k l_k$$$ where $$$1 \leq n_k \leq 10^{6}$$$ and $$$A \leq l_k \leq BDWGN$$$ (after Z, the next column is AA and so on (and after ZZ, AAA); BDWGN is equivalent to $$$10^{6}$$$).
The output file should consist of one integer which represents the number of ships that were sunk.
1A 2B,3C 3C,1C 2C 1C,2C,3C
2
Having a nice garden is hard, especially when you are obsessed about the $$$aesthetics$$$. Such is the life of Ionel, who wants to have the prettiest garden he ever could. He also has a quite narrow point of view - in his eyes, flowers are either ugly or pretty - no in-between.
Therefore, to establish how beautiful a garden is, Ionel has patented a beauty coefficient. This coefficient is calculated as the number of squares that a garden has, such that the squares' perimeter is filled solely by beautiful flowers. In order to figure out how pretty his garden is, Ionel has tasked you with finding this coefficient.
Formally, you are given a matrix of size $$$N \times N$$$, where $$$1 \leq N \leq 3 \times 10^3$$$. Each cell of this matrix can either have value $$$0$$$, denoting an non-aesthetically pleasing flower, or value $$$1$$$, referring to a beautiful flower.
It is asked of you to compute the beauty coefficient $$$B$$$ - the number of square submatrices in the matrix whose perimeter consists solely of the value $$$1$$$.
The first line of input contains one integers $$$N$$$, denoting the length of the matrix. Note that $$$1 \leq N \leq 3 \times 10^3$$$.
The next $$$N$$$ lines will contain $$$N$$$ integers $$$a_{ij}$$$, denoting whether the flower at cell $$$(i, j)$$$ is pretty or not. Note that $$$a_{ij}$$$ may either be $$$0$$$ or $$$1$$$.
On a single line, print $$$B$$$, the number of squares in the matrix whose perimeter is surrounded only by $$$1$$$s.
5 1 1 1 0 0 1 0 1 0 0 1 1 1 1 1 0 0 1 0 1 0 0 1 1 1
17
Captain Nix is passionate about languages. Programming languages especially. Now there's this new cool programming language called AGM (A Great programming not Markup language) with the following rules:
A valid instruction is either of the following:
A <name> is defined as follows:
An Expression (<expression>) is defined recursively as follows:
An <unary_operator> is either of the characters in this set {+, -, $$$\sim$$$}. A binary operator is any of the characters in this set {+, -, *, /, %, **, ^, &, |}.
The list of operators in them of descending priority is:
The list of keywords: ['BEG', 'END', 'BN', 'BZ', 'GOTO', 'PRINT']
Please note that:
Captain Nix has some programs written in this amazing programming language and wonders what they do. Can you help out?
The input files will contain characters on at most $$$100$$$ lines, representing the source code. It is guaranteed that there will be at most $$$10^3$$$ characters in the input and there will be at most $$$10^4$$$ instructions executed.
Output the result of each print instruction on a separate line if there no error of any kind in the code (syntax or runtime error). Otherwise output a single line containing the text "error" with none of the potential output.
PRINT $_5a; GOTO BEG $b = 10 */- 7; END;
error
BEG; PRINT 100 ** 2 * (3 ^ 1); END;
20000
BEG; $a; $b; $c; $a := 0; $b := 1; start_loop; $c := $a + $b; BG ($c - 100) GOTO END; PRINT $c; $a := $b; $b := $c; GOTO start_loop; END;
1 2 3 5 8 13 21 34 55 89
The first example outputs fibonacci terms less than 100.
The second example outputs the result of $$$100^2 \times (3 \oplus 1)$$$.
The third example has multiple errors. Not starting with BEG, improper variable name, use of undeclared variables, jump to non-declared labels, use of the wrong assignment operator, wrong expression formatting, instruction not ending with ; .
After losing a lot of money recently, you are desperately looking for ways to earn some. Today is your lucky day! Your generous friend proposes a game: He has a bag of coins, of which $$$N$$$ are green and $$$M$$$ are red. With equal probability, you draw one coin. If it is green, you earn $$$X$$$ euros, if it is red, you lose $$$Y$$$ euros. You can play this game as long as there are coins in the bag, or you can stop whenever you want and leave with your current earnings (or losses).
Given that you play optimally (you want to earn as much as you can), what is the maximum expected value of money you can earn?
The first line of the input contains the integers $$$N$$$ ($$$1 \leq N \leq 2000$$$), $$$M$$$ ($$$1 \leq M \leq 2000$$$), $$$X$$$ ($$$1 \leq X \leq 10^9$$$) and $$$Y$$$ ($$$1 \leq Y \leq 10^9$$$), denoting the number of green coins, the number of red coins, how much money you earn if you draw a green coin and how much money you lose if you draw a red coin.
On a single line, print the maximum expected value you can get if you play optimally. Your answer will be considered correct if the absolute or relative difference between your answer and judge's answer is less than $$$10^{-7}$$$.
1 2 4 3
1.00000000
You just moved in your new country A.G.M. Lands and the mayor already called you in to give you a task. The elections are coming and the mayor heard that you are an amazing programmer, therefore he wants you to help him build a program that will let him understand what are his chances of success. You know that the country can be represented as a tree, that initially everyone loves the mayor and would 100% vote for him, and that both the mayor and his opponent are able to send their employees out in the country to try and persuade the residents to change their vote. Formally, you are given a tree containing $$$N$$$ nodes (rooted at node $$$1$$$), where each node has an initial probability of $$$1$$$ to vote for the Mayor, and $$$Q$$$ operations of the following types:
On the first line, you are given an integer $$$N$$$ ($$$1 \le N \le 10^5$$$) denoting the number of residents. On the next $$$N - 1$$$ lines, there are two integers, $$$x$$$ ($$$1 \le x \le N$$$) and $$$y$$$ ($$$1 \le y \le N$$$), representing that there is a road between resident $$$x$$$ and resident $$$y$$$. On the next line, you are given an integer $$$Q$$$ ($$$1 \le Q \le 10^5$$$) denoting the number of operations. The next $$$Q$$$ lines will each contain the description of an operation as described above. Please note that for the operations $$$1$$$ and $$$2$$$, you should also take into account what the previous probability was and update accordingly. For example, if someone had a probability of $$$1$$$ to vote for the Mayor, and operation $$$2$$$ happens, the probability would remain $$$1$$$ since that person was already voting for the Mayor, therefore persuasion work was redundant.
For each question from the mayor, print the answer modulo $$$998244353$$$ on a new line.
6 1 2 1 3 1 4 4 5 4 6 5 3 1 1 6 5 1 3 3 1 1 3 2 2 3 3 1
6 5 3
Johnny found a set of points in the euclidean plane and noticed that each of the points has a certain weight $$$w_i$$$ associated with it. Johnny likes to optimize absolutely everything! This is why today he is trying to minimize anything he possibly can.
He started thinking about connecting those points with straight lines in order to form a tree that covers the entire set such that the total sum of the lengths of the segments connecting the points is the smallest possible. On this tree, node $$$i$$$ corresponds to point $$$i$$$. Then, he came up with a strange cost function that is computed in the following way:
Johnny quickly realized that this cost functions grows very quickly and he wouldn't be able to compute it. He then remembered from his competitive programming course that when the answer to a problem is too big to be computed, he usually computed it modulo some number $$$MOD$$$. This is exactly what Johnny did! He additionally picks a new number $$$MOD$$$ and will do all calculations modulo $$$MOD$$$.
Despite having a good idea, Johnny makes a crucial mistake: instead of calculating the residue of the cost function modulo $$$MOD$$$, he instead calculates the minimum possible residue modulo $$$MOD$$$, for each of the $$$c_i$$$.
Compute a program to find the answer produced by Johnny's algorithm. Given a set of points and a number $$$MOD$$$, you are asked to do the following:
The first line of the input contains two integers: $$$N$$$ $$$(1 \le N \le 5000)$$$ and $$$MOD$$$ $$$(1 \le MOD \le 2*10^9)$$$. The next $$$N$$$ lines each contain three integers: $$$x_i$$$, $$$y_i$$$, $$$w_i$$$ $$$(-10^9 \le x_i, y_i \le 10^9\ and\ 0 \le w_i \le 10^9)$$$, coordinates and weight of point $$$i$$$. It is guaranteed that no two points share the same coordinates.
The first $$$N-1$$$ lines of the output should contain two integers $$$u$$$ and $$$v$$$, representing the edges of the tree you picked. This tree must have the total sum of lengths of its edges the minimum among all possible trees covering the entire set of points. If there are multiple such trees, you may pick any of them. The next line must contain $$$N$$$ integers $$$c_i$$$, where $$$c_i$$$ is the cost function computed for node $$$i$$$ in the tree that you previously picked.
3 10 0 0 2 -1 -1 3 1 1 2
1 2 1 3 3 3 2
5 19 -2 -2 1 -3 -2 16 5 0 14 1 -4 18 -5 3 17
2 5 1 2 4 3 1 4 1 14 14 13 17
Recently, Max decided to become a house flipper. Because this is a hard business to start, he went to the bank to inquire about their offers.
The bank has $$$N$$$ house offers defined by the following attributes:
This means that $$$X$$$ months after Max buys a house, the house will be valued at $$$p_i + X*inc_i$$$ dollars and he will have payed the bank $$$d_i + min(X, m_i)*r_i$$$ dollars. Max starts with an infinite amount of money (so he can put any deposit needed), and a final moment $$$T$$$ when he wants to end all business. Also, he is restricted to only be able to own at most one house at a time. This means that he will be able to accept a bank offer only if he does not own any houses at that time (he can also decide to skip some of the offers).
At a moment in time, Max can decide to sell his current house (if he owns one). When a house is sold, Max will need to pay all the remaining monthly rates to the bank, so that all $$$m_i$$$ are payed. This action happens instantaneously.
Help Max find what is the maximum profit he can make using the bank offers. The profit is described as the total money he has made selling the houses subtracted by the total money used to buy the houses.
The first line of input contains $$$N$$$ ($$$1 \leq N \leq 10^5$$$) and $$$T$$$ ($$$1 \leq T \leq 10^9$$$), denoting the number of houses and the end time.
Each of the following $$$N$$$ lines contains $$$6$$$ positive integers describing the houses:
On a single line, print the maximum profit Max can make in the time given.
9 50 1 1 1 2 1 4 10 1 1 2 2 5 12 4 1 2 5 2 21 5 4 5 1 2 22 3 2 5 5 2 28 3 2 5 1 1 31 1 1 3 1 2 39 3 3 2 1 2 49 4 1 3 2 3
230
4 55 2 3 2 3 1 1 3 2 2 3 1 5 5 1 1 5 3 3 48 2 1 5 5 5
257
10 33 1 2 1 4 4 3 2 1 1 3 4 5 5 2 1 2 4 1 8 4 4 1 4 1 13 3 2 5 2 3 16 3 3 1 2 1 19 2 2 3 3 4 21 2 2 1 5 4 24 1 1 5 2 5 26 5 3 4 4 5
143
Your mom was cleaning the house and she found your old Nintendo console back from the 90s when you were just a kid. You remember that you used to like the "Spaceship" game so much that you started playing it again.
The game is played on the 2D plane and consists of a spaceship that starts in the point $$$(X_{start}, Y_{start})$$$ and has to get to the point $$$(X_{finish}, Y_{finish})$$$. We consider the spaceship a single point in a plane with area 0. You can move the spaceship in any direction (ex: it can also curve) but only towards the right (i.e., the $$$X$$$ coordinate can never decrease). On the plane there are $$$N$$$ segments sorted from left to right. That is, $$$X_{start} \lt X^1_1 \lt X^2_1 \lt X^1_2 \lt X^2_2 \lt ... \lt X^1_N \lt X^2_N \lt X_{finish}$$$, where $$$(X^1_i, Y^1_i)$$$ and $$$(X^2_i, Y^2_i)$$$ are the ends of the $$$i^{\text{th}}$$$ segment ($$$1 \leq i \leq N$$$). Each segment is of one of the following two types:
The final score of the game is the total distance traversed by the spaceship minus the total magic points accumulated. The goal of the game is to minimize the score. Because we are now in the $$$21^{\text{st}}$$$ century, you decide to write a program to compute the minimum score to move the spaceship from $$$(X_{start}, Y_{start})$$$ to $$$(X_{finish}, Y_{finish})$$$.
The first line of the input contains the integer $$$N$$$ ($$$1 \leq N \leq 2000$$$), denoting the number of segments. Each of the following $$$N$$$ lines contains an integer $$$type_i$$$ ($$$0 \leq type_i \leq 1$$$). If $$$type_i = 0$$$, then this segment is a Wall. If $$$type_i = 1$$$, then this segment is a Magic gate and the next integer in the input line is $$$P_i$$$ ($$$0 \leq P_i \leq 10^9$$$), denoting the magic points corresponding to this segment. In both cases, the input line contains $$$4$$$ more integers $$$X^1_i, Y^1_i, X^2_i, Y^2_i$$$ ($$$0 \leq X^1_i, Y^1_i, X^2_i, Y^2_i \leq 10^9$$$), denoting the ends of this segment. The last line of the input contains 4 integers $$$X_{start}, Y_{start}, X_{finish}, Y_{finish}$$$ ($$$0 \leq X_{start}, Y_{start}, X_{finish}, Y_{finish} \leq 10^9$$$), denoting the start point and the finish point.
Among all the $$$2 * (N + 1)$$$ points from the input, there aren't any $$$3$$$ collinear points.
On a single line, print the minimum score to move the spaceship from $$$(X_{start}, Y_{start})$$$ to $$$(X_{finish}, Y_{finish})$$$. Your answer will be considered correct if the absolute or relative difference between your answer and judge's answer is less than $$$10^{-8}$$$.
3 0 2 3 3 5 1 3 4 1 5 2 0 6 1 7 8 1 3 9 6
8.216116701980
The picture bellow corresponds to the example. The red segments are walls and the green segments are magic gates. The optimal path for the spaceship is to move in a straight line from the start point $$$(1, 3)$$$ to point $$$(6, 1)$$$, and from there in a straight line to the finish point $$$(9, 6)$$$. This path does not pass through any wall. Also, it passes through the magic gate from which we earn $$$3$$$ points. The score of the path is $$$\sqrt{29} + \sqrt{34} - 3$$$, and this is the minimum possible score.

There are only a few hours left before the AGM finals and still many problems to complete. Currently there are $$$N$$$ members in the jury numbered from $$$0$$$ to $$$N - 1$$$ and so far neither of them has completed any problem. In order to increase their productivity and to better keep track of the finished problems, the members of the scientific committee have decided to split into several teams.
Over the course of the night there are 2 main events that can happen:
After each event of the $$$2^{nd}$$$ type the jury is wondering how many of the already formed teams have finished their job.
Please note that one member of the jury can be part of multiple teams, and all the problems he develops will contribute to all the teams that he belongs to.
The first line of the input contains the integers $$$N$$$ ($$$1 \leq N \leq 200.000$$$) and $$$Q$$$ ($$$1 \leq Q \leq 200.000$$$), denoting the number of members of the jury and the number of events that happen during the night. The next $$$Q$$$ lines have the following structure:
In order to retrieve the actual parameters of the operation you have to xor each one of them with $$$last\_answer$$$, where $$$last\_answer$$$ is the most recent result of a query (initially $$$last\_answer$$$ is set to 0). The decoded lines will look as follows:
where $$$0 \leq C, P \leq 1.000.000$$$, $$$1 \leq k \leq 3$$$ and $$$0 \leq i, i_1, .., i_k \leq N - 1$$$ ($$$i_1$$$, ..., $$$i_k$$$ are distinct)
For each operation of the $$$2^{nd}$$$ type print the number of teams that finished their job.
3 6 1 2 2 0 1 2 0 1 2 1 1 0 4 2 1 0 3 3 3 2 0 2 3
0 1 2 2
Decoded version of the sample:
3 6 1 2 2 0 1 2 0 1 2 1 1 1 5 3 0 1 2 2 2 3 2 0 1
Bibita found a windfield shaped like an $$$N \times N$$$ matrix. There are very strong winds in the windfield. Anything in cell $$$(i,j)$$$ ($$$0 \le i, j \lt N$$$) would move after one second to one of the adjacent cells, with a given probability. If the winds are not strong enough, it will stay in the same cell.
Bibita likes odd and even numbers. She throws an object (could be anything) in the windfield, which will fall randomly in one of the cells at second $$$0$$$. She now wonders what is the probability that after $$$K$$$ seconds, the object will be on a cell $$$(i, j)$$$ where $$$j$$$ has the same parity as $$$K$$$. Can you help her find this answer?
Bibita considers that the coordinats in the windfield are 0 indexed (eg. the first cell (0, 0) is on an even column).
The first line of the input contains an integer $$$N$$$ ($$$2 \leq N \leq 500$$$), denoting the size of the windield matrix.
On each of the following $$$N \times N$$$ lines you'll be given at least $$$3$$$ and at most $$$5$$$ integers $$$a_p$$$ ($$$0 \le a_p \lt 998244353$$$), denoting the probabilities for the $$$ith$$$ cell in the matrix (numbered from top left to bottom right).
For a cell having all $$$4$$$ neighbours you will be given $$$5$$$ values $$$a_s$$$, $$$a_t$$$, $$$a_r$$$, $$$a_b$$$, $$$a_l$$$ corresponding in this order to the probability of: staying in the same cell, moving up, moving right, moving down, moving left. If the cell is a corner or an edge, the missing neighbours are omitted (eg. for the top-left corner you'll be given only $$$a_s$$$, $$$a_r$$$, $$$a_b$$$). The probability is a fraction $$$\frac{a}{b}$$$ given as $$$a \times b^{−1}$$$ (modulo $$$998244353$$$), where $$$b^{−1}$$$ is the modular inverse of $$$b$$$ modulo $$$998244353$$$.
The next line contains an integer $$$Q$$$ ($$$1 \le q \le 10^3$$$), denoting the number of testcases.
Each of the following $$$Q$$$ lines contain an integer $$$K$$$ ($$$1 \le K \le 10^9$$$).
It is guaranteed that for $$$N \gt 10$$$, $$$N^2 \times K \le 2 \times 10^6$$$
You should output the result for each of the $$$Q$$$ question, modulo $$$998244353$$$, on a separate line.
2 489139733 389315298 119789323 768648152 109806879 119789323 658841273 559016838 778630596 958314579 708753491 329420637 2 31 19
888372042 963737898