Astronomers have recently found a cosmic signal from a distant planet. World's best scientists have all confirmed that this is a communication signal sent by aliens. Moreover, they have established that these aliens have only nine fingers, hence, they use a numeral system in base 9, in other words, digits from $$$0$$$ to $$$8$$$. To challenge humans, these aliens came up with a puzzle. Many scientists solved the puzzle, but since the stakes are very high, they want you to confirm that their solution is correct as you are the smartest computer scientist on the planet. The puzzle is explained below.
Let $$$n$$$ be some positive integer, and $$$f(n)$$$ be a function that returns another positive integer that can be represented as $$$123456781011...n$$$ in base 9. In other words, $$$f(n)$$$ is a number which can be obtained by concatenating all the positive integers from 1 to n in their base 9 representations. The obtained number is also in base 9.
For example, for $$$n = 10$$$ (in base 10), $$$f(n) = 123456781011$$$, since $$$n = 11$$$ (in base 9). You need to find the remainder of dividing $$$f(n)$$$ by $$$4$$$.
Input consists of a single integer $$$n$$$ $$$(1 \leq n \leq 10^{18})$$$ given in base 10.
Print a single integer equal to $$$f(n)$$$ modulo 4.
5
3
You are a rider of a two-headed snake! It has two heads, one on each end, respectively.
You are traveling in a country that can be represented as a tree with $$$n$$$ vertices. Each vertex of the tree represents a city in the country.
You have such a long snake that its body takes up a certain simple path in the tree. Its heads are located on the vertices $$$h_1$$$ and $$$h_2$$$, respectively.
In one move, you can choose one of the heads, and move it to a neighboring vertex that does not have a body part of the snake on it. All of the other parts of the body will move in the same direction. Note that you are not allowed to stretch or compress the snake (it hurts)!
Find the minimum number of moves required for the snake to reach the given vertex $$$tar$$$, i.e. to make one of the heads be located on $$$tar$$$.
The first line of the input contains an integer $$$n$$$ ($$$1 \leq n \leq 10^6$$$) — the number of vertices in the tree.
The next $$$n-1$$$ lines contain two integers $$$u$$$ and $$$v$$$ ($$$1 \leq u, v \leq n$$$; $$$u \neq v$$$) — denoting an edge between vertices $$$u$$$ and $$$v$$$.
The next line contains two integers $$$h_1$$$ and $$$h_2$$$ ($$$1 \leq h_1, h_2 \leq n$$$; $$$h_1 \neq h_2$$$) — denoting the vertices where the heads of the snake are located.
The last line contains an integer $$$tar$$$ ($$$1 \leq tar \leq n$$$) — the target vertex.
Print the minimum number of moves required for the snake to reach the vertex $$$tar$$$ or print $$$-1$$$ if it is impossible.
4 1 2 2 3 2 4 3 4 1
-1
4 1 2 2 3 3 4 1 4 1
0
7 1 2 1 3 1 7 3 4 3 5 5 6 4 5 7
3
Mura is a tree-lover. Unfortunately, recently, he has lost his favorite rooted tree $$$t$$$ of size $$$n$$$, each node $$$v$$$ of which was assigned a value ($$$v.val$$$).
$$$t$$$ satisfies the following:
Array to_array(Tree t):
Array res = [t.root.val]
for each child v of t.root:
res = res + to_array(v.subtree)
return res
Mura knows, that he will never see his favorite tree again. But, at least, he wants to find the value of the sum of $$$f(v)$$$ over all nodes $$$v$$$ of $$$t$$$. Can you help him?
The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 1000$$$) — the size of the tree $$$t$$$ and the array $$$a$$$.
The second line contains $$$n$$$ integers $$$a_1, a_2,...a_{n - 1}, a_{n}$$$ ($$$0 \leq a_i \leq 10^6$$$) — the array $$$a$$$.
Output the sum of $$$f(v)$$$ over all nodes $$$v$$$ of $$$t$$$.
3 4 5 2
5
You are given a sequence of delivery points $$$x$$$ of length $$$n$$$ and the integer number $$$k$$$.
Delivery point is an integer number $$$x_i$$$ in the range of $$$[-10^9; 10^9]$$$ (i.e. $$$-10^9 \le x_i \le 10^9$$$).
Find the minimum distance of delivering any $$$k$$$ orders. Initially you are at origin $$$0$$$.
The distance between $$$x_i$$$ and $$$x_j$$$ is the absolute difference between them |$$$x_i$$$ - $$$x_j$$$|.
The first line of the input contains integer numbers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^6$$$, $$$1 \le k \le n$$$).
The second line of the input contains $$$n$$$ integer numbers $$$x_1, x_2, \dots, x_n$$$ ($$$-10^9 \le x_i \le 10^9$$$).
Print the minimum distance of delivering any $$$k$$$ orders.
4 3 -5 -4 3 7
11
The path (0, 3, -4, -5) gives the minimum distance: |3-0|+|-4-3|+|-5-(-4)| = 11
You're given an undirected graph with $$$n$$$ nodes and $$$m + n$$$ edges ($$$m$$$ red and $$$n$$$ black edges). Nodes are numbered from $$$1$$$ to $$$n$$$ in the order around the circle. There are $$$n$$$ black edges between adjacent nodes. Nodes $$$i$$$ and $$$(i + 1)$$$ are adjacent for $$$(1 \le i \le n - 1)$$$, nodes $$$1$$$ and $$$n$$$ are also adjacent.
You are given $$$m$$$ red edges.
The path is considered valid if and only if the given path contains at most $$$1$$$ red edge.
Find the length of the shortest valid path between two vertices $$$a_i$$$ and $$$b_i$$$.
Solve this problem for $$$Q$$$ independent queries.
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$3 \le n \le 200\ 000$$$ and $$$0 \le m \le 200\ 000$$$).
The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$, $$$u_i \neq v_i$$$), that mean that there's a red edge between nodes $$$u_i$$$ and $$$v_i$$$.
The next line contains one integer $$$Q$$$ ($$$1 \le Q \le 200\ 000$$$) - the number of queries. Each of the next $$$Q$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le n$$$, $$$a_i \neq b_i$$$).
For each query print the length of the shortest valid path between two vertices $$$a_i$$$ and $$$b_i$$$.
6 1 3 5 4 3 6 3 5 1 2 1 4
2 1 1 3
The graph of the first testcase
You are a pizza delivery courier at Yankeks. The region you are working in can be represented as an $$$n \times m$$$ grid. Each cell of the grid is either empty or occupied.
You can move only between neighboring empty cells. Two cells are considered as neighboring if they share a side.
You are given $$$q$$$ queries of the following types:
Your task is to answer all queries of type 2.
The first line contains two integers $$$n$$$, $$$m$$$ ($$$1 \leq n, m \leq 1000$$$) — the dimensions of the grid.
$$$n$$$ lines follow. The $$$i$$$-th line contains $$$m$$$ characters, the $$$j$$$-th of which is "1" if the cell on the intersection of the $$$i$$$-th row and the $$$j$$$-th column is occupied and "0" if it is empty.
The next line contains an integer $$$q$$$ ($$$1 \leq q \leq 20000$$$) — the number of queries.
The next q lines contain queries in the format described above.
For each query of type 2, print "YES" (without brackets) if it is possible to deliver a pizza, or "NO" (without brackets) otherwise.
5 5 00100 00100 11111 00000 11100 8 2 1 1 5 5 2 1 5 5 5 0 3 1 3 2 0 3 5 3 5 2 1 2 4 3 2 2 5 4 3 1 2 1 5 5 5
NO NO YES YES NO
13 13 0000000000000 0001100011000 0001110011000 0001101011000 0001100111000 0001100011000 0000000000000 0001100011000 0001100011000 0001100011000 0001100011000 0001111111000 0000000000000 1 2 1 1 13 13
YES
This problem is interactive. That means you can make queries and get responses in the online mode. Please be sure to use the stream flushing operation after each query's output in order not to leave part of your output in some buffer. For example, in C++ you've got to use the fflush(stdout) function, in Java — call System.out.flush(), and in Pascal — flush(output).
Ulzhan is a young researcher at NU. She wants to find out what number is the most popular favorite number amongst Kazakh people. But since there are millions of Kazakh people, she asked for your help. Ulzhan already has a list of people who agreed to participate in this research. Your job is to simply go through the list and ask each person what his or her favorite number is. It is guaranteed that one of the numbers appears strictly more than 50% of the time. In other words, the most popular number accounts for the majority of participant responses. Your task is to find that number after having received all the responses.
It is guaranteed that each participant's response is an integer within the range $$$[-10^{18}, 10^{18}]$$$, and the total number of participants is within the range $$$[1, 10^6]$$$.
Pay attention to the unusual time and memory constraints!
Use standard input and output for the interaction.
1. To start the interaction, output a single line consisting of a string "next".
2. Read a line of input consisting of a single integer, a participant's response.
3. Keep repeating steps 1 and 2, until you read -1 in step 2.
4. If you read -1, it means you have received all the responses. This -1 is not a participant response and should not be counted towards the answer!
5. Output a single integer, the number that appeared more than half of the times in step 2, excluding the last -1.
After step 5, your program should terminate immediately.
7 7 4 9 7 -1
next next next next next next 7
It is the last week before NU Open takes place. The greatest problemsetter of all time $$$krauch$$$ does not have time and money for new challenging problems. So, $$$krauch$$$ asks their boss $$$CMaster$$$ for help. But there is a thing, $$$CMaster$$$ accepts only positive whole square amount of dollars $$$\geq X$$$. $$$Krauch$$$ is out of pocket. Luckily, $$$NUBANK$$$ provides loans with low percentages, but, of course, has its own terms, the loan amount may only be the product of some bank-supported prime numbers.
More specifically, $$$NUBANK$$$ has an array of supported prime numbers, $$$p[]$$$ (not necessarily distinct) and another array $$$cost[]$$$, both of them having lengths of $$$N$$$. If you choose indices $$$j_1$$$, $$$j_2$$$ ... $$$j_m$$$, the loan amount will be $$$p_{j_1}$$$ $$$\cdot$$$ $$$p_{j_1}$$$ $$$\cdot$$$ ... $$$\cdot$$$ $$$p_{j_m}$$$ and the return cost will be $$$cost_{j_1}$$$ $$$+$$$ $$$cost_{j_1}$$$ $$$+$$$ ... $$$+$$$ $$$cost_{j_m}$$$.
Can you help $$$krauch$$$ to take a loan from $$$NUBANK$$$ to pay $$$CMaster$$$ with the minimal return cost (as $$$krauch$$$ is out of money)? Keep in mind that both the $$$NUBANK$$$ and $$$CMaster$$$ has their own terms and conditions.
The first line contains two integers $$$N$$$ and $$$X$$$, $$$1 \leq N \leq 100$$$, $$$1 \leq X \leq 10^{18}$$$.
The second line contains an array of bank-supported prime numbers of length $$$N$$$, $$$p_i \leq 10^5$$$ and $$$p_i$$$ is a prime number.
The third line contains an array of $$$N$$$ numbers, cost of each bank-supported number, $$$1 \leq cost_i \leq 10^5$$$.
Print the minimal possible value of the return of cost of a loan such that the loan is accepted by $$$CMaster$$$ or $$$-1$$$ if payment conditions cannot be satisfied.
7 20 2 3 23 5 3 2 5 2 1 1 6 5 2 5
10
3 5 2 3 2 1 2 5
-1
For the first sample test case, if you choose indices $$$[1, 2, 5, 6]$$$ you will have a loan of $$$36$$$ ($$$2 \cdot 3 \cdot 3 \cdot 2$$$) dollars which is both whole square and $$$\geq X$$$ thus accepted by $$$CMaster$$$, and it has a return cost of 10 dollars which is minimal among other options.
We have a track that is infinite in one direction.
At the begging of the track, we have a car with an initial speed of $$$v=0$$$ m/s. Also, the car has an acceleration of $$$a$$$ m$$$/s^2$$$, which we can change at any moment of time to a value in the range [1, -1] m$$$/s^2$$$. The speed of the car can reach any value in the range (-$$$\infty$$$, $$$\infty$$$) m/s
We are given $$$n$$$ requests. In each request, a new restriction $$$(t, r)$$$ is added: At the moment when $$$t$$$ seconds have passed from the start of the race, the speed of the car should satisfy $$$v \leq r$$$ (at that exact moment).
After each request, you need to print the maximum distance that the car can reach at the moment when all restrictions are passed.
The first line of the input contains a single integer $$$ n $$$ ($$$ 1 \leq n \leq 2 00 000 $$$) — the number of requests.
Each of the next n lines contains two integers $$$ t_i $$$, $$$ r_i $$$ ($$$ 1 \leq t_i \leq 10 ^ 9 $$$), ($$$ 0 \leq r_i \leq 10 ^ 9 $$$) - a moment in time, and a speed limit.
For each request, print the answer to how far the car can go at the moment when it passes all the restrictions. The absolute error of your answer must not exceed ($$$10^{-6}$$$).
3 1 3 3 3 1 0
0.500000000 4.500000000 2.250000000
You are given two arrays of integers $$$a$$$ and $$$b$$$.
You can perform the following operation on the array $$$a$$$:
1. Pick some subarray of length k.
2. Replace it with the sum of the elements in the subarray.
For example, if the initial array was $$$[3, 6, 9]$$$ and we have $$$k$$$ = 2, then in a single operation you can replace the last two elements by their sum, yielding an array $$$[3, 15]$$$, or replace the first two elements to get an array $$$[9, 9]$$$.
For some fixed $$$k$$$, let us denote it as valid, if it is possible to get the array $$$b$$$ from $$$a$$$ by performing the given operation some number of times (possibly zero).
Find the valid values of $$$k$$$ ($$$1 \le k \le n$$$) and print them in an increasing order.
The first line contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of array $$$a$$$.
The second line contains $$$n$$$ integers — the elements of array $$$a$$$ ($$$1 \le a_i \le 10^9$$$).
The third line contains an integer $$$m$$$ ($$$1 \le m \le 2 \cdot 10^5$$$) — the size of array $$$b$$$.
The fourth line contains $$$m$$$ integers — the elements of array $$$b$$$ ($$$1 \le b_j \le 10^9$$$).
In the first line print one integer $$$p$$$ ($$$0 \le p \le n$$$) - the number of valid values of $$$k$$$.
In the second line print $$$p$$$ integers - valid values of $$$k$$$.
5 4 3 1 2 8 2 8 10
1 2
8 4 9 1 5 3 1 2 4 2 14 15
2 2 3
3 3 9 1 2 10 11
0