Sunny has been practicing his violin a lot, preparing for his recital with his older sister Mari; his violin has $$$2\times 10^{18} +1$$$ micro-strings indexed from $$$-10^{18}$$$ to $$$10^{18}$$$ from left to right.
There are $$$n$$$ vibrations on the strings; the $$$i$$$-th vibration $$$(1 \le i \le n)$$$ is located on the string with index $$$a_i$$$. No two vibrations are located on the same string. Since Sunny has been practicing a lot, he can precisely choose one string $$$k$$$ $$$(-10^{18} \le k \le 10^{18})$$$ and play a note on it.
When Sunny plays a note on string $$$k$$$, every vibration existing to the right of this string gets shifted to the next string to its right, and every vibration to the left of this string gets shifted to the next string to its left, and if a vibration exists on string $$$k$$$, it stays on string $$$k$$$. More formally, when Sunny hits string $$$k$$$, the following happens simultaneously for each vibration $$$i$$$:
Now, Mari wants Sunny to make the vibrations on the violin have indices $$$b_1, b_2, ..., b_n$$$; since Sunny wants to make his sister feel proud, he wants to know if it is possible to change the initial positions $$$a$$$ of the $$$n$$$ vibrations to the desired positions $$$b$$$. Sunny can play an arbitrary number of notes (including not playing any note).
The first line of input contains one integer $$$n$$$ $$$(1 \le n \le 2\times 10^5)$$$ — the number of vibrations currently on Sunny's violin.
The next line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ $$$(1 \le a_1 \lt a_2 \lt ... \lt a_n \le 10^9)$$$ separated by spaces — the initial positions of the vibrations on the violin.
The next line contains $$$n$$$ integers $$$b_1, b_2, ..., b_n$$$ $$$(1 \le b_1 \lt b_2 \lt ... \lt b_n \le 10^9)$$$ separated by spaces — the desired positions of the vibrations on the violin.
Output one line containing the word "YES" (without quotes) if it is possible for Sunny to convert the positions of the vibrations to the desired positions by his sister by playing notes on his violin's strings, and print "NO" (without quotes) otherwise.
4 3 4 5 6 3 5 6 8
YES
4 3 4 5 6 3 4 7 8
NO
In the first example, Sunny can play the following notes to convert array $$$a$$$ into $$$b$$$:
Sunny practicing on his violin
School is starting, and Aubrey is excited for the activities she is going to perform at school. During the break, Aubrey enjoys her favorite hobby of hunting sprout moles.
The school yard has a tree containing $$$n$$$ nodes rooted at some node (let that node be $$$r$$$). The $$$i$$$-th $$$(1 \le i \le n)$$$ node has $$$a_i$$$ sprout moles. Aubrey starts her hunt by choosing a node $$$i$$$, and then chooses a subset of nodes in the subtree of node $$$i$$$, and starts hunting the sprout moles in each node in the chosen subset.
When Aubrey hunts a group of $$$a_i$$$ sprout moles in a single node $$$i$$$, her hunting score gets XORed by $$$a_i$$$, and Aubrey initially has a hunting score of $$$0$$$.
Now consider the set of all possible distinct final scores Aubrey can achieve by choosing a subset of nodes from the subtree of node $$$i$$$, name it $$$S_i$$$. $$$f(i)$$$ is equal to the summation of the elements of $$$S_i$$$.
Now Aubrey does not know where the tree is rooted at, so she wants to know, from every root $$$r$$$ from $$$1$$$ to $$$n$$$ inclusive, what is the summation of $$$f(i)$$$ over all nodes $$$i$$$ $$$(1 \le i \le n)$$$ assuming the tree is rooted at $$$r$$$? Since $$$\displaystyle\sum_{i=1}^n f(i)$$$ can be very large, Aubrey wants to find its value modulo $$$998244353$$$.
The first line of input contains one integer $$$n$$$ $$$(1 \le n \le 2\times 10^5)$$$ — the number of nodes in the tree.
The next $$$n-1$$$ lines each contain $$$2$$$ integers $$$u_i, v_i$$$ $$$(1 \le u_i, v_i \le n)$$$ separated by a space — the edges of the tree.
The next line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ $$$(0 \le a_i \lt 2^{30})$$$ separated by spaces — the number of sprout moles in each node.
Output one line containing $$$n$$$ integers. The $$$r$$$-th $$$(1 \le r \le n)$$$ of them contains $$$\displaystyle\sum_{i=1}^n f(i)$$$ modulo $$$998244353$$$ when the tree is rooted at node $$$r$$$.
3 1 2 1 3 1 2 3
11 15 14
3 1 2 1 3 1 1 1
3 3 3
In the first sample, there are three possible roots :
Aubrey being excited to start her sprout mole hunt
After achieving the title "Employee of the Month" three times, Mr. Jawsum promoted Hero to "Manager of Cryptocurrencies" as a reward for his hard work. Hero then found an opportunity to earn money using a new coin called "NEAR".
To gain NEAR, you need to work on some tasks related to training "Artificial Intelligence" to solve problems related to "Competitive Programming"; there are $$$n$$$ types of tasks to work on, the $$$i$$$-th type of tasks $$$(1 \le i \le n)$$$ has a "Profit value" equal to $$$a_i$$$.
Since Hero is currently the "Manager of Cryptocurrencies", he has enough power to hire people to work for him. Due to "The Last Resort" policies, the employees he hires can only work in a specific range $$$[l, r]$$$, meaning they can only work on tasks of type $$$i$$$ such that $$$(l \le i \le r)$$$.
The way Hero hires employees is as follows: he first chooses a range $$$[l, r]$$$ $$$(1 \le l \le r \le n)$$$, and then hires $$$\frac{x(x+1)}{2}$$$ employees such that $$$x$$$ is equal to the size of the range $$$(r - l + 1)$$$; he hires each of the $$$\frac{x(x+1)}{2}$$$ to work on all distinct sub-ranges in the range $$$[l, r]$$$. If an employee works on a range $$$[L, R]$$$, the total amount of money he achieves after working for one day is equal to $$$\text{LCM}(a_L, a_{L+1}, a_{L+2}, ..., a_{R-1}, a_R) \times L \times R$$$ worth of NEAR, where $$$\text{LCM}$$$ is the Least Common Multiple.
At the end of the day, Hero collects all money gained from all of his hired employees. Now since Hero wants to maximize his profit, he thought that he needed to choose the whole array of types of tasks as the initial range he hires people in. But he forgot that in "Dreamspace", every bank account has a limit of $$$998244352$$$ NEAR coins. When the number of coins in an account exceeds that number, it overflows and bounces back to $$$0$$$ NEAR coins. So the actual profit Hero gets is equal to the gained profit taken under modulo $$$998244353$$$.
Hero chose $$$Q$$$ candidate ranges as initial ranges to hire people in; the $$$j$$$-th $$$(1 \le j \le Q)$$$ range is equal to $$$[l_j, r_j]$$$ , and he wants to find the final profit he gets at the end of the day if he chooses that range, modulo $$$998244353$$$.
More formally, the answer to the $$$j$$$-th query is equal to $$$\displaystyle\sum_{L = l_j}^{r_j}\sum_{R = L}^{r_j} \text{LCM}(a_L, a_{L+1}, a_{L+2}, ..., a_{R-1}, a_R) \times L \times R$$$ taken under modulo $$$998244353$$$.
The first line of input contains one integer $$$n$$$ $$$(1 \le n \le 10^5)$$$ — the number of types of tasks.
The next line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ $$$(1 \le a_i \le 10^6)$$$ separated by spaces — the profit values of each type of the tasks.
The next line contains one integer $$$Q$$$ $$$(1 \le Q \le 10^5)$$$ — the number of candidate ranges Hero chose.
The $$$j$$$-th line of the next $$$Q$$$ lines each contains two integers $$$l_j, r_j$$$ $$$(1 \le l_j \le r_j \le n)$$$ — the boundaries of the $$$j$$$-th candidate range.
Output $$$Q$$$ lines; the $$$j$$$-th of them should contain the final profit obtained from choosing the $$$j$$$-th range taken under modulo $$$998244353$$$.
3 1 2 3 3 1 2 2 3 1 3
13 71 94
4 2 2 3 3 4 1 1 1 2 1 3 1 4
2 14 95 251
In the third query of the first sample, there are $$$6$$$ subranges in $$$[1,3]$$$, which are $$$\{ \{1\}, \{2\}, \{3\}, \{1, 2\}, \{2, 3\}, \{1, 2, 3\}\}$$$; the accumulative $$$\text{LCM}$$$ of each of them is equal to $$$\{1, 2, 3, 2, 6, 6\}$$$; the sum of each subarray $$$\text{lcm}$$$ multiplied by both its boundaries is equal to : $$$1\times 1 \times 1 + 2 \times 2 \times 2 + 3 \times 3 \times 3 + 2 \times 1 \times 2 + 6 \times 2 \times 3 + 6 \times 1 \times 3 = 94$$$
Mr. Jawsum congratulating Hero after promoting him to "Manager of Cryptocurrencies"
Kel was sitting bored on Mari's picnic blanket as everyone else was doing something, but after sometime he had an idea how to spend his time! He asked Basil to borrow his camera, and Basil agreed.
Mari's picnic blanket consists of $$$4$$$ different rectangular pieces each of dimensions $$$L \times W$$$, linked in a way such that the whole blanket has dimensions $$$2L \times 2W$$$.
Basil's camera has a laser like technique which generates laser from both sides (frontview and back view) to take $$$1\text{D}$$$ pictures; the laser can be thought of as a straight line (even though the actual camera can not generate an infinite straight line, Kel assumes it is a straight line as the blanket's dimensions are not too big).
Kel is trying to place and orient the camera in a way such that the generated laser passes by all $$$4$$$ rectangles in the blanket. A straight line is said to pass by a rectangle if a line segment of non-zero length — which is a subset of that straight line — intersects the rectangle.
Now Kel wants to find the number of different straight lines generated from his placement and orientation that can pass by all $$$4$$$ rectangular pieces in the blanket. Since this number can be large, you are asked to output it modulo $$$10^9+7$$$.
The first line of input contains two integers $$$L, W$$$ $$$(1 \le L, W \le 10^6)$$$ — The dimensions of each rectangle.
Output one line containing one integer, the number of different straight lines passing through all $$$4$$$ rectangles modulo $$$10^9+7$$$.
1 1
2
2 2
2
There are two possible straight lines for this blanket ![]() | ![]() |
Kel trying to orient the camera to get all $$$4$$$ rectangles
Mari is currently baking cookies for OMORI's quest (as they need a lot of juice and heart in this one). She currently has a cookie dough with nutrition value $$$a_1$$$.
There are $$$n-1$$$ ovens numbered from $$$1$$$ to $$$n-1$$$. Mari puts each cookie in every oven by order, starting from oven $$$1$$$ till oven $$$n-1$$$. Ovens in dreams are different from casual ovens; they have two modes: Heating mode and Freezing mode, and also each oven $$$i$$$ has a power value $$$a_{i+1}$$$.
When baking a cookie with nutrition $$$x$$$ in the $$$i$$$-th oven, Mari can do one of the following options :
Mari will try every possible configuration of modes to all $$$n-1$$$ ovens, and she wants to find the summation of the nutrition values of all final cookies over every possible configuration of ovens. Since this sum can be large, Mari wants its value taken under modulo $$$10^9+7$$$.
Two configurations of ovens are considered different if there exists an oven $$$j$$$ $$$(1 \le j \lt n)$$$ such that oven $$$j$$$ is set on heating mode in one configuration and set on freezing mode in the other one.
The first line of input contains one integer $$$n$$$ $$$(1 \le n \le 2\times 10^5)$$$ — the number of ovens $$$+ 1$$$.
The next line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ $$$(1 \le a_i \le 10^9)$$$ separated by spaces — the initial nutrition value of the cookie dough and the power values of each oven.
Output one line containing the summation of all final cookies' nutrition values, taken under modulo $$$10^9 + 7$$$.
3 1 2 1
5
5 2 2 2 2 2
32
In the first example, there are $$$4$$$ possibilities:
So the summation of all final nutrition values will be : $$$2+1+1+1 = 5$$$.
A poster showing Mari's pieces of advice about baking cookies
Basil wants to play a game with his best friend Sunny; he starts by explaining the rules to him.
The game is played on an array of $$$n$$$ flower pots, where the $$$i$$$-th pot $$$(1 \le i \le n)$$$ has two values $$$a_i$$$, $$$t_i$$$, denoting the number of seeds initially in the $$$i$$$-th flower pot and the type of seeds in this flower pot, respectively.
During a move, the player has to choose a flower pot which has a non-zero number of seeds; let the index of this flower pot be $$$i$$$ $$$(1 \le i \le n)$$$, then he will do two things:
The player who can not make a move loses, and Sunny goes first.
Sunny and Basil are going to play $$$q$$$ matches; the $$$j$$$-th match $$$(1 \le j \le q)$$$ is going to be played on the subarray from index $$$l_j$$$ to index $$$r_j$$$, inclusive.
Assuming both Sunny and Basil are playing optimally, your task is to find which player wins in each of the $$$q$$$ matches.
The first line of input contains one integer $$$n$$$ $$$(1 \le n \le 2\times 10^5)$$$ — The number of flower pots.
The next line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ $$$(0 \le a_i \le 10^9)$$$ separated by spaces — The number of seeds in each flower pot.
The next line contains $$$n$$$ integers $$$t_1, t_2, ..., t_n$$$ $$$(1 \le t_i \le n)$$$ separated by spaces — The type of seeds in each flower pot.
The next line contains one integer $$$q$$$ $$$(1 \le q \le 2\times 10^5)$$$ — The number of matches played.
The $$$j$$$-th of the next $$$q$$$ lines contains two integers $$$l_j, r_j$$$ $$$(1 \le l_j \le r_j \le n)$$$ — The boundaries of the subarray on which the $$$j$$$-th match will be played.
For each of the $$$q$$$ matches, output one line containing the name of the winner; output "SUNNY" (without quotes) if the winner was Sunny and "BASIL" (without quotes) otherwise.
6 1 2 3 3 2 1 1 1 1 1 1 1 6 3 4 2 5 1 6 1 3 4 6 5 6
BASIL BASIL BASIL BASIL BASIL SUNNY
3 1 1 1 1 2 3 2 1 2 1 3
SUNNY SUNNY
In the first match in the second sample, there are two flower pots each having only one seed; one of them is of type $$$1$$$ and the other is of type $$$2$$$. Sunny can choose to take the seed from the flower pot of type $$$2$$$ and then set the number of seeds in the flower pot of type $$$1$$$ to $$$0$$$, making every pot empty and thus winning as Basil will not be able to do a move after Sunny's turn.
Basil explaining the rules of the game to Sunny
OMORI is bored in white space; he has been living there for as long as he can remember... He creates a game out of boredom with his dolls.
There are $$$n$$$ dolls in the possession of OMORI, exactly one of these dolls resembles OMORI (has no emotion); the other $$$n-1$$$ dolls can be of three types:
How does the game work? Since OMORI has been living alone for a lot of time, he started to make imaginary fights with dolls; he decides the winner according to the following chart:
Happy beats Angry - Angry beats Sad - Sad beats Happy So, when two dolls fight, OMORI decides the result according to the following rules:
After explaining, OMORI then plays the game with you; the game is as follows: OMORI shuffles the $$$n$$$ dolls and asks you to find the emotionless doll (the one that resembles OMORI). You can do up to $$$3n$$$ questions of the form i,j, $$$(1 \le i,j \le n)$$$, and OMORI will answer with either $$$0$$$ or $$$1$$$.
When you ask OMORI about two dolls i,j, he will return $$$1$$$ only if doll $$$i$$$ beats doll $$$j$$$ in a fight; otherwise, he will return $$$0$$$ (whether it was a tie or doll $$$j$$$ won).
The first line of input contains a single integer $$$n$$$ $$$(4 \le n \le 20000)$$$ — the number of dolls OMORI has.
The description of the interaction follows.
To ask OMORI a question, you can output no more than $$$3n$$$ queries of the following format:
In response to this query, you will receive $$$1$$$ if doll $$$i$$$ beats doll $$$j$$$ in a fight, and $$$0$$$ otherwise.
Once you have found the answer, output a query of the following format:
If you make an invalid query, you will receive $$$−1$$$ instead of a response from OMORI. If you exceed the limit of $$$3n$$$ questions, you will receive $$$−2$$$ instead of a response from OMORI. If you return an incorrect answer, you will receive $$$−3$$$ instead of a response from OMORI. In all of these cases you will get the verdict Wrong answer. In this case, your program should terminate immediately to avoid undefined verdicts.
After outputting the queries/answer, do not forget to output a newline character and flush the output buffer. Otherwise, you will receive the verdict Solution "hung" (Idleness limit exceeded). To flush the buffer, use:
4 1 0 0 0 0 1
? 1 2 ? 1 3 ? 1 4 ? 3 2 ? 4 2 ? 3 1 ! 4
4 0 0 0
? 1 2 ? 1 3 ? 1 4 ! 1
In the first sample, the emotions of the dolls in order are: {"Happy", "Angry", "Sad", "Emotionless"}.
In the second sample, the emotions of the dolls in order are: {"Emotionless", "Happy", "Angry", "Sad"}.
OMORI spending time in white space thinking about a game