National Yang Ming Chiao Tung University 2025 Individual Programming Contest
A. Range Affine Update and Modulo Query
time limit per test
5 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Given an array of $$$n$$$ integers, your task is to process $$$q$$$ queries of the following types:

  • Update: For a given interval $$$[l, r]$$$, update each element $$$a_i$$$ in the interval to $$$(a_i \times c + d)$$$, where $$$c$$$ and $$$d$$$ are given integers.
  • Query: Given integers $$$l, r, x$$$, report the number of elements in the interval $$$[l, r]$$$ whose value modulo $$$10^5 + 3$$$ is equal to $$$x$$$, i.e., $$$$$$ \begin{aligned} \#\{i \mid l \le i \le r, \ a_i \bmod (10^5 + 3) = x\} \end{aligned} $$$$$$
Note that $$$10^5 + 3$$$ is prime.
Input

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:

  • 1 l r c d — representing an Update query.
  • 2 l r x — representing a Query query.
  • $$$1 \leq n \le 10^6$$$
  • $$$1 \leq q \le 10^4$$$
  • $$$1 \leq a_i, c, d, x \leq 10^5$$$
  • $$$1 \leq l \leq r \leq n$$$
Output

Print the result of each 2 l r x query.

Example
Input
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
Output
3
2
0

B. Number of Inversions
time limit per test
5 с
memory limit per test
1024 megabytes
input
standard input
output
standard output

An array of $$$n$$$ integers and $$$q$$$ queries are given. Each query consists of two integers $$$(l, r)$$$:

  1. Reverse the subarray from index $$$l$$$ to $$$r$$$.
  2. Output the number of inversions in the entire array.
  3. Restore the array to its original state (i.e., the array as it was before this query)
Note: An inversion in an array is a pair of indices $$$(i, j)$$$ such that $$$i \lt j$$$ and $$$a_i \gt a_j$$$.
Input

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]$$$

  • $$$1 \leq n, q \leq 10^5$$$
  • $$$1 \leq a_i \leq n$$$
  • $$$1 \leq l \leq r \leq n$$$
Output

For each query, output a single integer on a separate line — the total number of inversions in the array after this operation.

Examples
Input
5 3
1 3 5 2 4
2 4
1 5
2 3
Output
2
7
4
Input
5 3
2 1 2 1 1
2 4
1 5
1 3
Output
5
1
5

C. Construct Permutation
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

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$$$.

Input

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$$$.

  • $$$2 \leq n \leq 2\times10^5$$$
  • $$$1 \leq M \leq 10^9$$$
  • $$$0 \leq a_i, K \lt M$$$
Output

If such a permutation exists, print 'YES' in the first line, followed by the permutation in the second line. Otherwise, print 'NO'.

Examples
Input
3 10 4
1 2 3
Output
NO
Input
10 1 0
0 0 0 0 0 0 0 0 0 0
Output
YES
0 0 0 0 0 0 0 0 0 0 

D. Permutation Swaps
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

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]$$$.

Input

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$$$.

  • $$$1 \le t \le 10^4$$$
  • $$$1 \le n \le 2 \cdot 10^5$$$
  • $$$1 \le a_i \le n$$$
  • It is guaranteed that $$$a_1, a_2, \ldots, a_n$$$ is a permutation of $$$1,2,\ldots, n$$$.
  • It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
Output

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'.

Example
Input
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
Output
Yes
No
Yes
No
Yes

E. Expression Evaluation
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

In the enchanted kingdom of Arithmora, wizards cast spells using arithmetic expressions. Each spell is a string consisting only of:

  • single-digit numbers ("0"–"9"),
  • the addition operator ("+"), and
  • the multiplication operator ("*").

When a wizard chants the spell, the kingdom's ancient laws dictate that:

  1. Multiplication ("*") has higher precedence than addition ("+"), and
  2. Both operators are left-associative (evaluated from left to right if they have equal precedence).

For example:

  • The spell 2+3*4 is evaluated as $$$2+(3*4)=14$$$.
  • The spell 9*1+5*2 is evaluated as $$$(9*1)+(5*2)=19$$$.

Your task is to compute the final value of the given spell expression.

Input

The input contains a single line string $$$S$$$, the spell expression.

  • $$$S$$$ only contains digits ("0"–"9"), "+" and "*".
  • $$$1 \leq |S| \leq 1000$$$.
  • The expression is valid (no leading/trailing operators, no consecutive operators).
  • the results can be greater than $$$2^{64}$$$.
Output

Output a single integer: the value of the expression.

Examples
Input
2*3+4
Output
10
Input
3*4+5*6*7
Output
222
Input
2*3*0*1*5+5*5*5*5*5
Output
3125

F. LCS of DFS orders
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

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:

  • $$$S_1 = \{[1,2,3,4,5],[1,2,3,5,4],[1,3,4,5,2],[1,3,5,4,2]\}$$$
  • $$$S_4 = \{[4,3,5,1,2], [4,3,1,2,5]\}$$$

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)$$$.

Input

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.

  • $$$1 \le n \le 2 \cdot 10^5$$$
  • $$$1 \le q \le 2 \cdot 10^5$$$
  • $$$1 \le u_i, v_i\le n$$$ and $$$u_i \neq v_i$$$ for $$$i = 1,2,\ldots,n$$$.
  • $$$1 \le x_i, y_i \le n$$$ for $$$i = 1,2,\ldots,q$$$.
  • It is guaranteed that the given edges form a tree.
Output

For each query, output a single integer in a new line — the value of $$$f(x_i,y_i)$$$.

Examples
Input
5 2
1 2
1 3
3 4
3 5
1 4
3 3
Output
2
3
Input
7 2
1 2
2 3
2 5
1 4
4 6
4 7
5 6
3 5
Output
2
4

G. Torque Transmission
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

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:

  • Each gear is a node.
  • Each shaft is a directed edge with an efficiency percentage (between 0 and 100).

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.

Input

The first line contains three integers $$$n, m, q$$$:

  • $$$n$$$: number of gears.
  • $$$m$$$: number of shafts.
  • $$$q$$$: number of queries.

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$$$.

  • $$$1 \le n \le 1000$$$.
  • $$$1 \le m \le 5000$$$.
  • $$$1 \le q \le 100$$$.
  • $$$1 \leq u,v,s,t \leq n$$$.
  • $$$u \neq v$$$.
  • $$$1 \leq e \leq 100$$$.
  • $$$1 \leq T \leq 10^6$$$.
  • An output is considered correct if it has an absolute or relative error of at most $$$10^{-6}$$$.
Output

For each query, output the maximum torque that can be delivered to the target gear. If the target gear is unreachable, output 0.

Example
Input
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
Output
612.000000
0

H. Prescription Dosage
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

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:

  • "Too Low" if it is less than the safe range,
  • "Too High" if it is more than the safe range,
  • "Safe" if it is within the safe range.
Input

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.

  • $$$1 \le w \le 200$$$.
  • $$$1 \le d \le 10000$$$.
Output

Print one of the following strings:

  • Too Low
  • Too High
  • Safe
Examples
Input
60
500
Output
Too Low
Input
50
800
Output
Safe
Input
70
1500
Output
Too High