Given an array of $$$n$$$ integers, your task is to process $$$q$$$ queries of the following types:
The first line contains two integers $$$n$$$ and $$$q$$$.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ The next $$$q$$$ lines each describe a query in one of the following formats:
Print the result of each 2 l r x query.
5 5 1 1 2 3 3 1 1 3 2 1 2 1 4 3 1 2 4 2 3 2 1 5 9 2 2 3 3
3 2 0
An array of $$$n$$$ integers and $$$q$$$ queries are given. Each query consists of two integers $$$(l, r)$$$:
The first line contains two integers $$$n$$$ and $$$q$$$ — the size of the array and the number of queries.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the initial elements of the array.
Each of the next $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ — the interval $$$[l, r]$$$
For each query, output a single integer on a separate line — the total number of inversions in the array after this operation.
5 3 1 3 5 2 4 2 4 1 5 2 3
2 7 4
5 3 2 1 2 1 1 2 4 1 5 1 3
5 1 5
You are given an array $$$A$$$ of length $$$n$$$ and two integers $$$M$$$ and $$$K$$$. Determine if there exists a sequence $$$P$$$ that is permutation of $$$A$$$ such that for every consecutive pair, $$$$$$ \begin{aligned} (p_i + p_{i+1}^2) \bmod M = K \notag \end{aligned} $$$$$$ If a valid permutation exists, output any one; otherwise, output $$$-1$$$.
The first line contains three integers $$$n$$$, $$$M$$$, and $$$K$$$, indicating the size of the array, the modulus, and the target value, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, representing the array $$$A$$$.
If such a permutation exists, print 'YES' in the first line, followed by the permutation in the second line. Otherwise, print 'NO'.
3 10 4 1 2 3
NO
10 1 0 0 0 0 0 0 0 0 0 0 0
YES 0 0 0 0 0 0 0 0 0 0
Consider the following recursive procedure, written in pseudocode, which transforms an input array into a permutation of its elements:
function generate(sequence b):
if size of b is 1:
return b
else:
split b into two non-empty contiguous subarrays, b_1 and b_2
optionally, swap b_1 and b_2
b_1 = generate(b_1)
b_2 = generate(b_2)
concatenate b_1 and b_2 into b', in this order
return b'
end if
In other words, at each step, the array is split into two contiguous subarrays (not necessarily of equal length). You may optionally swap the order of the two subarrays before recursing on each. After both subarrays are recursively processed using the same procedure, they are concatenated in their current order to form the result.
Note that the final output of the procedure may vary depending on how the array is split at each step and whether or not a swap is applied.
You are given an array $$$a$$$ of $$$n$$$ positive integers $$$a_1, a_2, \ldots, a_n$$$, which is a permutation of integers from $$$1$$$ to $$$n$$$. Your task is to determine if it is possible for array $$$a$$$ to be the output of generate function when applied to the identity permutation $$$[1,2,\ldots, n]$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$. The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ — the length of the array $$$a$$$.
The second line of each test case contains $$$n$$$ non-negative integers $$$a_1,a_2,\ldots,a_n$$$ — the elements of the array $$$a$$$.
For each test case, output 'Yes' if it is possible for array $$$a$$$ to be the output of the generate function when applied to the identity permutation $$$[1,2,\ldots, n]$$$. Otherwise, output 'No'.
5 5 2 1 5 3 4 4 3 1 4 2 1 1 10 4 2 9 1 10 5 8 3 7 6 8 4 5 2 1 3 8 6 7
Yes No Yes No Yes
In the enchanted kingdom of Arithmora, wizards cast spells using arithmetic expressions. Each spell is a string consisting only of:
When a wizard chants the spell, the kingdom's ancient laws dictate that:
For example:
Your task is to compute the final value of the given spell expression.
The input contains a single line string $$$S$$$, the spell expression.
Output a single integer: the value of the expression.
2*3+4
10
3*4+5*6*7
222
2*3*0*1*5+5*5*5*5*5
3125
You are given a tree with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. Consider the following pseudocode, which performs a preorder traversal of the tree:
a = []
function preorder_traversal(u):
append u to the back of a
for each v that is adjacent to u and not visited:
preorder_traversal(v)
Note that the resulting array $$$a$$$, representing the preorder traversal of the tree starting from node $$$u$$$, may vary depending on the order in which adjacent vertices are visited.
Let $$$S_u$$$ denote the set of all possible arrays $$$a$$$ that can be obtained by running the above traversal starting from vertex $$$u$$$, allowing all possible orderings of adjacent nodes during traversal.
For any two vertices $$$u$$$ and $$$v$$$, define the function $$$f(u, v)$$$ as follows: $$$$$$\min_{a_1 \in S_u, a_2 \in S_v} \text{LCS}(a_1, a_2)$$$$$$ where $$$\text{LCS}(a_1, a_2)$$$ denotes the length of the longest common subsequence between the two arrays $$$a_1$$$ and $$$a_2$$$.
For example, consider a tree with $$$5$$$ vertices and edges $$$(1,2),(1,3), (3,4), (3,5)$$$. Then we have:
Then we have $$$f(1,4) = 2$$$, because the smallest possible LCS length among all combinations of arrays from $$$S_1$$$ and $$$S_4$$$ is 2.
You are given $$$q$$$ queries. Each query consists of two (not necessarily distinct) vertices $$$x_i$$$ and $$$y_i$$$. For each query, compute the value of $$$f(x_i, y_i)$$$.
The first line of each test case contains two integers $$$n$$$ and $$$q$$$ — the number of vertices of the tree and the number of queries.
The $$$i$$$-th of the following $$$n-1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ — the ends of the $$$i$$$-th edge.
The $$$i$$$-th of the following $$$q$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ — the vertices in the query.
For each query, output a single integer in a new line — the value of $$$f(x_i,y_i)$$$.
5 2 1 2 1 3 3 4 3 5 1 4 3 3
2 3
7 2 1 2 2 3 2 5 1 4 4 6 4 7 5 6 3 5
2 4
In a mechanical power transmission system, several gears are connected by shafts. Each gear transmits torque to the next one. However, due to frictional losses and imperfect contact, only a certain percentage of torque is actually transferred from one gear to another.
The system can be modeled as a directed graph:
The torque delivered to the next gear is: $$$$$$ T_{next} = T_{current} \times \frac{\text{efficiency}}{100}. $$$$$$
Your job is to compute the maximum possible torque that can be delivered to a specified target gear, starting from the input gear.
The first line contains three integers $$$n, m, q$$$:
The next $$$m$$$ lines each contain three integers: $$$u, v, e$$$ meaning there is a shaft from gear $$$u$$$ to gear $$$v$$$ with efficiency $$$e\%$$$.
Each of the next $$$q$$$ lines contains three integers: $$$s, t, T$$$, where torque $$$T$$$ is applied to gear $$$s$$$. You must compute the maximum torque that can reach gear $$$t$$$.
For each query, output the maximum torque that can be delivered to the target gear. If the target gear is unreachable, output 0.
5 6 2 1 2 90 2 3 80 1 4 50 4 5 70 2 5 60 3 5 85 1 5 1000 2 4 500
612.000000 0
At the university hospital, doctors often prescribe medication to patients based on their weight. To avoid mistakes, prescriptions must be checked against standard safe dosage ranges.
For example, the safe dosage of a certain drug is 10 to 20 milligrams per kilogram of body weight. If a doctor prescribes too little, the treatment may be ineffective; if too much, the patient may suffer side effects.
You are asked to help write a small program that checks whether prescriptions are safe. You are given the weight of a patient (in kilograms) and the prescribed dosage (in milligrams). The safe dosage range for this drug is between 10 and 20 milligrams per kilogram of body weight, inclusive.
Determine whether the prescribed dosage is:
The first line contains an integer $$$w$$$ — the patient's weight in kilograms.
The second line contains an integer $$$d$$$ — the prescribed dosage in milligrams.
Print one of the following strings:
60 500
Too Low
50 800
Safe
70 1500
Too High