IUT Eid Salami Programming Contest 2026 - Powered by Okkhor Technology (Online Mirror)
A. Obsession With Functions
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Yeamin loves to play with functions. Recently, he has been experimenting with functions defined on integers.

The function $$$s(x)$$$ denotes the sum of the decimal digits of a positive integer $$$x$$$. For example, $$$s(125) = 1 + 2 + 5 = 8$$$.

Another function $$$h(x)$$$ repeatedly uses $$$s(x)$$$ until the result is a single-digit integer. Formally, $$$$$$ h(x) = \begin{cases} x, & x \lt 10,\\ h(s(x)), & x \ge 10. \end{cases} $$$$$$ For example, $$$h(987) = h(9+8+7) = h(24) = h(2+4) = h(6) = 6$$$.

To make things more interesting, Yeamin defines a new function $$$\displaystyle f(L,R) = \sum_{x=L}^{R} h(x^2)$$$ for integers $$$L$$$ and $$$R$$$ with $$$1 \le L \le R$$$. For example, $$$f(2,4) = h(2^2) + h(3^2) + h(4^2) = h(4) + h(9) + h(16) = 4 + 9 + 7 = 20$$$.

Now, Yeamin wants to compute $$$f(L, R)$$$ for multiple pairs of $$$L$$$ and $$$R$$$. Given the values of $$$L$$$ and $$$R$$$, determine $$$f(L,R)$$$.

Input

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases.

Each test case consists of a single line containing two space-separated integers $$$L$$$ and $$$R$$$ ($$$1 \le L \le R \le 10^{16}$$$).

Output

For each test case, output a single integer in a line — $$$f(L, R)$$$.

Example
Input
4
2 4
10 12
1 9999999999999999
10000000000000000 10000000000000000
Output
20
14
56666666666666661
1

B. Does The Universe Really Exist?
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In mathematical logic, a quantifier specifies how many elements in a domain satisfy a given property. The two most common quantifiers are the universal quantifier $$$\forall$$$, meaning "for all", and the existential quantifier $$$\exists$$$, meaning "there exists".

A universal quantifier states that a proposition must hold for every value in the domain. For example, the proposition $$$\forall x \,(x^2 \ge 0)$$$ is true over the integers because the square of every integer is non-negative. On the other hand, the proposition $$$\forall x \,(x^2 \gt x)$$$ is false over the integers, because it does not hold for certain values, such as $$$x = 0$$$ and $$$x = 1$$$.

An existential quantifier states that there exists at least one value in the domain for which the proposition holds. For example, the proposition $$$\exists x \,(x^2 - 5x + 6 = 0)$$$ is true over the integers because the equality is satisfied for $$$x = 2$$$ and $$$x = 3$$$. On the other hand, the proposition $$$\exists x \,(x^2 = 5)$$$ is false over the integers because there is no integer whose square equals $$$5$$$.

A statement may also contain multiple quantifiers. For example, the proposition $$$\forall x \, \exists y \,(x + y = 0)$$$ states that for every integer $$$x$$$, there exists an integer $$$y$$$ such that $$$x + y = 0$$$. Quantifiers are interpreted from left to right: each quantifier binds the variable that follows it, and inner quantifiers are evaluated within the scope determined by the preceding ones.

Let the domain of all variables be the set of all integers. The function $$$f(s)$$$ is defined for a binary string $$$s$$$ of length $$$n$$$. The function constructs the following proposition:

$$$$$$ \mathcal{Q}_1 x_1 \;\mathcal{Q}_2 x_2 \;\dots\; \mathcal{Q}_n x_n \;\bigl(x_1 + x_2 + \dots + x_n = 0\bigr), $$$$$$

where each $$$\mathcal{Q}_i$$$ is a quantifier determined by the $$$i$$$-th character of $$$s$$$: $$$$$$ \mathcal{Q}_i = \begin{cases} \exists & \text{if } s_i = 0, \\ \forall & \text{if } s_i = 1. \end{cases} $$$$$$

Given a binary string $$$s$$$, your task is to determine whether $$$f(s)$$$ is true or not.

Input

The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 10^5)$$$ — the number of test cases.

The first line of each test case contains a single integer $$$n$$$ $$$(1 \le n \le 10^5)$$$ — the length of the binary string.

The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output

For each test case, output a single string in a line — "YES" if the proposition $$$f(s)$$$ is true, and "NO" if it is not.

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Example
Input
4
3
000
3
111
2
10
3
011
Output
YES
NO
YES
NO
Note

In the first test case, $$$s = 000$$$. The formal statement of $$$f(s)$$$ is $$$\exists x_1 \, \exists x_2 \, \exists x_3 \,(x_1 + x_2 + x_3 = 0)$$$, which in words reads: there exists an integer $$$x_1$$$ such that there exists an integer $$$x_2$$$ such that there exists an integer $$$x_3$$$ satisfying $$$x_1 + x_2 + x_3 = 0$$$. This statement is true. For example, choosing $$$(x_1, x_2, x_3) = (0,0,0)$$$ satisfies the sum.

In the second test case, $$$s = 111$$$. The formal statement of $$$f(s)$$$ is $$$\forall x_1 \, \forall x_2 \, \forall x_3 \,(x_1 + x_2 + x_3 = 0)$$$, which reads: for every integer $$$x_1$$$, for every integer $$$x_2$$$, for every integer $$$x_3$$$, the sum $$$x_1 + x_2 + x_3$$$ equals $$$0$$$. This statement is false. For instance, in the case of $$$(x_1, x_2, x_3) = (0,0,1)$$$, $$$x_1 + x_2 + x_3 = 0 + 0 + 1 = 1 \ne 0$$$.

In the third test case, $$$s = 10$$$. The formal statement of $$$f(s)$$$ is $$$\forall x_1 \, \exists x_2 \,(x_1 + x_2 = 0)$$$, which reads: for every integer $$$x_1$$$, there exists an integer $$$x_2$$$ such that $$$x_1 + x_2 = 0$$$. This statement is true. For example, if $$$x_1 = 5$$$, choosing $$$x_2 = -5$$$ satisfies the equality. Similarly, for any integer $$$x_1$$$, choosing $$$x_2 = -x_1$$$ works.

C. Roads in Laurasia
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In the vast Plains of Laurasia, there are $$$N$$$ villages numbered from $$$1$$$ to $$$N$$$, connected by exactly $$$N - 1$$$ bidirectional roads. The road network is designed such that there is exactly one $$$^{\dagger}$$$simple path between any two villages.

Merchants regularly travel between these villages to sell their goods. However, bandits have begun to learn the merchants' schedules in advance. Since there is currently only a single path connecting any two villages, an ambush is easy to arrange. If a bandit knows which villages the merchants plan to travel between, they can lie in wait along one of the roads in that path and rob them.

To reduce the chances of merchants being attacked by the bandits, the mayor has decided to build additional roads so that for every pair of villages, there exist at least two $$$^*$$$distinct simple paths between them.

Since constructing roads is expensive, the mayor wants to add as few new roads as possible.

Your task is to determine the minimum number of new roads that must be built so that for every pair of villages, there exists at least two distinct simple paths between them.

$$$^{\dagger}$$$A path between village $$$u$$$ and village $$$v$$$ is defined as a sequence of villages $$$x_1, x_2, \dots, x_k$$$ such that $$$x_1 = u$$$, $$$x_k = v$$$, and for each $$$1 \le i \lt k$$$, there exists a bidirectional road connecting village $$$x_i$$$ and village $$$x_{i+1}$$$. A path is called simple if it does not visit the same village twice.

$$$^{*}$$$Two paths are considered distinct if they differ by at least one road.

Input

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.

The first line of each test case contains a single integer $$$N$$$ ($$$3 \le N \le 2 \cdot 10^5$$$) — the number of villages.

Each of the next $$$N - 1$$$ lines contains two space-separated integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le N$$$, $$$u \ne v$$$) — describing a road connecting villages $$$u$$$ and $$$v$$$.

For each test case, it is guaranteed that any village is reachable from any other village using the roads.

It is guaranteed that the sum of $$$N$$$ over all test cases does not exceed $$$5 \cdot 10^5$$$.

Output

For each test case, output a single integer — the minimum number of roads that need to be built so that between every pair of villages there exist at least two distinct paths.

Example
Input
2
4
1 2
1 3
1 4
8
1 3
2 3
3 4
4 5
5 6
6 7
6 8
Output
2
2

D. Disaster Walker
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You live in a city that is modeled as a rectangular grid. Each cell in the city is a unit square, and the grid follows a standard Cartesian coordinate system where the $$$x$$$-axis increases from left to right and the $$$y$$$-axis increases from bottom to top.

A cyclone named Gigantomachia has formed at the bottom-left corner of the city, located at the point $$$(0, 0)$$$. According to meteorological forecasts, the cyclone will travel in a straight line towards the top-right corner of the city, located at the point $$$(x, y)$$$. The cyclone is immensely powerful, and every cell that it enters along its path will be damaged.

Your task is to determine the number of cells in the city that will be damaged by the cyclone.

Input

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases.

Each test case consists of a single line containing two space-separated integers $$$x$$$ and $$$y$$$ ($$$1 \le x, y \le 10^9$$$) — the width and height of the city.

Output

For each test case, output a single integer in a line — the number of cells that will be damaged by the cyclone.

Example
Input
2
2 3
5 5
Output
4
5
Note

Illustrations of the sample test cases:

E. Race in Laurasia
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In the vast Plains of Laurasia, there lives a foolish horse named Doddo, who is about to take part in an obstacle race.

The race track consists of $$$N$$$ lanes, numbered from $$$1$$$ to $$$N$$$, arranged side by side.

Each lane contains $$$M+2$$$ positions, labeled from $$$0$$$ to $$$M+1$$$. Positions $$$1$$$ through $$$M$$$ form the actual track. Position $$$0$$$ is the starting point, and position $$$M+1$$$ is the finishing point of each lane; these two positions are not considered part of the track.

There are $$$K$$$ obstacles. Each obstacle is described by a triple $$$(i, l, r)$$$, meaning that in lane $$$i$$$, every position $$$x$$$ such that $$$l \le x \le r$$$ is blocked by this obstacle.

It is guaranteed that the starting and finishing points in every lane are never blocked, and that no two obstacles in the same lane overlap.

Doddo starts at the starting point (position $$$0$$$) in a chosen lane. Each second of the race, the following steps occur in order:

  1. If Doddo is already at the finishing point (position $$$M+1$$$) in any lane, the race ends immediately.

  2. Suppose Doddo is currently at position $$$x$$$ in lane $$$i$$$. He checks whether position $$$x+1$$$ in lane $$$i$$$ is blocked.

  3. If position $$$x+1$$$ is not blocked, Doddo stays in the same lane.

  4. Otherwise, Doddo attempts to switch to an adjacent lane ($$$i-1$$$ or $$$i+1$$$), if such a lane exists and position $$$x$$$ in that lane is not blocked. Note that only position $$$x$$$ is checked for availability; even if position $$$x+1$$$ in the new lane is blocked, Doddo can still switch to that lane.

    In this situation, Doddo behaves in the following way:

    • If no adjacent lane is available, Doddo stays in the same lane.
    • If exactly one adjacent lane is available, Doddo switches to that lane.
    • If both adjacent lanes are available, Doddo becomes confused and chooses one of them uniformly at random.
    Doddo can switch lanes even at the starting point(position $$$0$$$). Doddo can change lanes at most once per second.

  5. After that, if any obstacle does not block position $$$x+1$$$ in Doddo's current lane, Doddo moves to position $$$x+1$$$ in that lane; otherwise, Doddo crashes into the obstacle, and the race ends immediately.

If Doddo reaches the finishing point in any lane before the race ends, he wins the race. Otherwise, if he crashes at any point, he loses.

For each $$$i$$$ $$$(1 \le i \le N)$$$, determine the probability that Doddo wins the race if he starts at the starting point in lane $$$i$$$.

Input

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.

The first line of each test case contains three space-separated integers $$$N$$$, $$$M$$$, and $$$K$$$ ($$$1 \le N \le 2 \cdot 10^5$$$, $$$1 \le M \le 10^9$$$, $$$0 \le K \le 2 \cdot 10^5$$$) — the number of lanes, the length of each lane, and the number of obstacles.

Each of the next $$$K$$$ lines contains three space-separated integers $$$i$$$, $$$l$$$, and $$$r$$$ ($$$1 \le i \le N$$$, $$$1 \le l \le r \le M$$$) — describing an obstacle on lane $$$i$$$ blocking the segment $$$[l, r]$$$.

It is guaranteed that for each lane, no two obstacles overlap with each other.

It is guaranteed that the sum of $$$N$$$ over all test cases does not exceed $$$5 \cdot 10^5$$$, and the sum of $$$K$$$ over all test cases does not exceed $$$5 \cdot 10^5$$$.

Output

For each test case, output $$$N$$$ space-separated integers in a single line. The $$$i$$$-th of those integers should represent the probability that Doddo wins the race if he starts in lane $$$i$$$, modulo $$$998244353$$$.

Output all probabilities modulo $$$998244353$$$. Formally, if a probability can be expressed as an irreducible fraction $$$\displaystyle\frac{x}{y}$$$, you have to output $$$p = xy^{-1} \bmod 998244353$$$, where $$$y^{-1}$$$ is an integer such that $$$yy^{-1} \bmod 998244353 = 1$$$.

Example
Input
1
6 5 4
2 2 2
3 2 2
4 4 4
5 4 4
Output
1 499122177 748683265 499122177 499122177 1
Note

The following diagram illustrates the race track for the first test case:

The probability of winning the race from each lane is $$$\displaystyle 1, \tfrac{1}{2}, \tfrac{1}{4}, \tfrac{1}{2}, \tfrac{1}{2}, 1$$$, respectively.

F. Thesis Group Selection
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
"In IUT, CGPA matters!"

It is almost the end of the 3-1 semester for batch 22, and before the Eid holidays, $$$N$$$ students must be assigned to research groups for their upcoming thesis. There are $$$K$$$ distinct research groups available, and the university has decided on a unique procedure to assign students to these groups based on their academic standing.

Each student has a Face–Value in the department and a CGPA (ranging from 0.00 to 4.00). The assignment process works as follows:

  1. All students are first sorted by their CGPA in descending order. (If multiple students have the exact same CGPA, they are sorted by their Face Value in descending order).
  2. The students are then processed in batches of size $$$K$$$, strictly following this sorted order:
    • The first $$$K$$$ students are randomly and uniformly assigned to $$$K$$$ distinct groups.
    • The next $$$K$$$ students are then randomly and uniformly assigned to $$$K$$$ distinct groups, and this process repeats.
    • The final batch may contain fewer than $$$K$$$ students. If so, they are still randomly and uniformly assigned to distinct groups (meaning no two students from this final batch will end up in the same group).
  3. A research group's score is the average of the CGPAs of all the students assigned to it.

Labib is one of these students. His information is always provided as the first entry in the university's records. He is curious about his academic future and wants to know: what is the expected score of the research group he will eventually be placed in?

Note: The expected value (or expectation) of a random variable is the probability-weighted average of all its possible values. Formally, if a random variable $$$X$$$ can take values $$$x_1, x_2, \dots, x_m$$$ with probabilities $$$p_1, p_2, \dots, p_m$$$ respectively, then its expected value $$$E[X]$$$ is defined as: $$$$$$E[X] = \sum_{i=1}^{m} x_i \cdot p_i$$$$$$

Input

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10000$$$) — the number of test cases.

The first line of each test case contains two integers $$$N$$$ and $$$K$$$ ($$$1 \le K \le N \le 10^5$$$) — the number of students and the number of research groups.

The second line contains $$$N$$$ integers $$$F_1, F_2, \dots, F_N$$$ ($$$1 \le F_i \le 10^9$$$) — the Face Values of the students. All $$$F_i$$$ are distinct.

The third line contains $$$N$$$ real numbers $$$C_1, C_2, \dots, C_N$$$ ($$$0.00 \le C_i \le 4.00$$$) — the CGPAs of the students. Each $$$C_i$$$ has exactly two digits after the decimal point.

Note: Labib is always the first student in the input, i.e., his Face Value is $$$F_1$$$ and his CGPA is $$$C_1$$$.

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 a single real number — the expected score of Labib's research group.

Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$.

Example
Input
2
4 2
1 2 3 4
3.00 4.00 4.00 3.00
3 1
124 220 217
3.90 3.97 3.88
Output
3.500000000
3.916666667
Note

Explanation of the first case:

Let $$$S_i$$$ denote the $$$i$$$-th student in the input. Labib is $$$\mathbf{S}_1$$$. After sorting, the order is: $$$S_3(4.00, 3) \to S_2(4.00, 2) \to S_4(3.00, 4) \to \mathbf{S}_1(3.00, 1)$$$.

With $$$N=4$$$ students and $$$K=2$$$ groups, each group receives exactly 2 students. The assignment batches are Batch 1 ($$$S_3, S_2$$$) and Batch 2 ($$$S_4, \mathbf{S}_1$$$). Because each batch is split uniformly across the 2 groups, Labib ($$$\mathbf{S}_1$$$) will be paired with exactly one random student from Batch 1.

$$$$$$ \begin{array}{|c|c|c|c|} \hline \textbf{Batch 1 Teammate} & \textbf{Labib (Batch 2)} & \textbf{Group Score} & \textbf{Prob.} \\ \hline S_3 \text{ (4.00)} & S_1 \text{ (3.00)} & (4.00 + 3.00) / 2 = 3.50 & 1/2 \\ \hline S_2 \text{ (4.00)} & S_1 \text{ (3.00)} & (4.00 + 3.00) / 2 = 3.50 & 1/2 \\ \hline \end{array} $$$$$$

$$$$$$\text{Expected Score} = \left(3.50 \times \frac{1}{2}\right) + \left(3.50 \times \frac{1}{2}\right) = 3.50$$$$$$

G. Treasure Hunt in Laurasia
time limit per test
1 second
memory limit per test
128 megabytes
input
standard input
output
standard output

In the vast Plains of Laurasia, a clever but greedy donkey named Doru loves collecting treasures. The village market contains $$$N$$$ unique items, each with a weight and a profit value (how valuable it is to Doru).

Doru carries a bag with limited capacity. For a bag of capacity $$$c$$$, he can pick items whose total weight does not exceed $$$c$$$. Each item can be picked at most once.

Being ambitious, Doru wants to know: for every bag capacity from $$$1$$$ to $$$K$$$, what is the maximum total profit he can collect?

Input

The first line contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.

The first line of each test case contains two space-separated integers $$$N$$$ and $$$K$$$ ($$$1 \le N \le 10^6$$$, $$$1 \le K \le 5000$$$) — the number of items in the market and the maximum bag capacity.

The second line of each test case contains $$$N$$$ space-separated integers $$$w_1, w_2, \dots, w_N$$$ ($$$1 \le w_i \le 5000$$$) — the weights of the items.

The third line of each test case contains $$$N$$$ space-separated integers $$$p_1, p_2, \dots, p_N$$$ ($$$1 \le p_i \le 10^9$$$) — the profits of the items.

It is guaranteed that the sum of $$$N$$$ over all test cases does not exceed $$$10^6$$$, and the sum of $$$K^2$$$ over all test cases does not exceed $$$5000^2$$$.

Output

For each test case, output $$$K$$$ space-separated integers in a single line.

The $$$i$$$-th integer should be the maximum total profit obtainable using a bag of capacity $$$i$$$.

If no item can be taken, the profit is $$$0$$$.

Example
Input
2
4 9
1 1 2 5
32 21 50 60
3 3
1 2 3
5 2 3
Output
32 53 82 103 103 103 113 142 163
5 5 7
Note

In the first test case, if the capacity of the bag is $$$3$$$, it is optimal to pick the first and the third item.

H. Devils of Pestilence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In Dhaka, the mosquito population has risen to unprecedented levels this year. The threat of an epidemic outbreak of Dengue or Chikungunya feels more serious than ever. As dusk settles over the city, a relentless buzzing fills the air. Sleep is no longer peaceful; every night feels like a siege.

Trying to protect himself, Ishraque finds an old mosquito net. Though worn, it represents a vital barrier – a wall between him and the swarm of killers. However, even as he tucks it in, he wonders in fear: how many enemies are already inside the wall?

The upper structure of the net is defined by $$$n$$$ points $$$(ax_1, ay_1), (ax_2, ay_2), \dots, (ax_n, ay_n)$$$. The net is anchored to the bed at $$$(ax_1, 0)$$$ and $$$(ax_n, 0)$$$. The boundary forms a polygon consisting of straight line segments consisting of points $$$(ax_i, ay_i), (ax_{i+1}, ay_{i+1})$$$ for all $$$i$$$ such that $$$1 \le i \lt n$$$, vertical segments connecting the first and last points to their respective anchors on the bed and a horizontal segment along the bed from $$$(ax_1, 0)$$$ to $$$(ax_n, 0)$$$.

There are currently $$$m$$$ mosquitoes in his room located at points $$$(bx_1, by_1), (bx_2, by_2), \dots, (bx_m, by_m)$$$. Your task is to determine how many mosquitoes are inside the net or on its boundary.

Input

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases.

The first line of each test case contains two space-separated integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 2 \times 10^5$$$, $$$1 \le m \le 2 \times 10^5$$$) — the number of points describing the net and the number of mosquitoes.

The second line of each test case contains $$$n$$$ space-separated integers $$$ax_1, ax_2, \dots, ax_n$$$ ($$$0 \le ax_1 \lt ax_2 \lt \dots \lt ax_n \le 10^9$$$) — the $$$x$$$-coordinates of the net points.

The third line of each test case contains $$$n$$$ space-separated integers $$$ay_1, ay_2, \dots, ay_n$$$ ($$$ay_i \gt 0$$$ for any $$$i$$$ such that $$$1 \le i \le n$$$) — the $$$y$$$-coordinates of the net points.

The fourth line of each test case contains $$$m$$$ space-separated integers $$$bx_1, bx_2, \dots, bx_m$$$ ($$$0 \le bx_i \le 10^9$$$ for any $$$i$$$ such that $$$1 \le i \le n$$$) — the $$$x$$$-coordinates of the mosquitoes.

The fifth line of each test case contains $$$m$$$ space-separated integers $$$by_1, by_2, \dots, by_m$$$ ($$$0 \lt by_i \le 10^9$$$ for any $$$i$$$ such that $$$1 \le i \le n$$$) — the $$$y$$$-coordinates of the mosquitoes.

It is guaranteed that the sum of n over all test cases and the sum of m over all test cases do not exceed $$$2 \times 10^5$$$.

Output

For each test case, output a single integer in a line — the number of mosquitoes that lie inside the net or on its boundary.

Example
Input
2
6 5
1 3 5 6 9 10
4 6 7 8 6 7
2 4 6 7 8
6 8 7 3 7
2 5
10 20
10 10
7 12 15 17 20
5 12 8 10 9
Output
2
3
Note

The following is the illustration for the first test case: