Given an array $$$a$$$ of length $$$n$$$, perform the following operation exactly once:
— Delete an element from the array, then concatenate the resulting parts (i.e., if you remove the $$$i$$$-th element, the array becomes $$$a_1, a_2, \dots, a_{i - 1}, a_{i + 1}, \dots, a_n$$$).
Let $$$b_1, b_2, \dots, b_{n - 1}$$$ be the resulting array after performing the above operation exactly once on the array $$$a$$$.
Find the maximum possible sum of the values of GCD of each prefix in the array $$$b$$$.
More formally, find the maximum value of: $$$$$$\sum_{i = 1}^{n - 1} gcd(b_1, b_2, \dots, b_i)$$$$$$
Over all the arrays $$$b$$$ resulting from applying the mentioned operation on the array $$$a$$$ exactly once.
Note: $$$gcd(x_1, x_2, \dots, x_k)$$$ is the greatest integer that divides all the values $$$x_1, x_2, \dots, x_k$$$ evenly without a remainder.
The first line of the input contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the size of the array.
The second line of the input consists of $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).
Output a single integer — the answer to the problem.
44 3 2 1
7
318 3 9
27
3100 100 100
200
Given a tree consisting of $$$n$$$ nodes.
Is there a walk that visits all the nodes of the tree at least once and at most twice?
Note that the node you start the walk at is considered visited at the beginning.
A walk is a sequence of nodes $$$v_1, v_2, \dots, v_k$$$ such that for each $$$i$$$ ($$$1 \le i \lt k$$$), $$$v_i$$$ and $$$v_{i + 1}$$$ are connected by an edge.
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of testcases.
The first line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of nodes in the tree.
Each of the following $$$n - 1$$$ lines of each testcase consists of two space-separated integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$) denoting an edge between nodes $$$u$$$ and $$$v$$$ in the tree.
It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of $$$n$$$ over all the testcases will not exceed $$$2 \cdot 10^5$$$.
For each testcase, print "YES" (without quotes) if there exists a walk that satisfies the conditions, otherwise print "NO" (without quotes).
You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
321 251 21 33 43 552 11 31 41 5
YES YES NO
Explanation of the first testcase: we can start at node $$$1$$$ then go to node $$$2$$$ and stop there.
Explanation of the second testcase: a valid walk may look like this: $$$5$$$ — $$$3$$$ — $$$4$$$ — $$$3$$$ — $$$1$$$ — $$$2$$$ — $$$1$$$.
Notice that it is not necessary to start at node $$$1$$$.
Nodes $$$1$$$ and $$$3$$$ were visited twice, whereas nodes $$$2$$$, $$$4$$$, and $$$5$$$ were visited once. Thus, each node is visited at least once and at most twice and the walk is valid.
Explanation of the third testcase: it can be shown that there does not exist a walk that satisfies the conditions.
Define the power of a string $$$t$$$ of length $$$m$$$ as follows:
$$$$$$\prod_{i = 1}^{m} i^{f_t(t_i)}$$$$$$
Where:
— $$$t_i$$$ is the character at position $$$i$$$ of the string $$$t$$$.
— $$$f_t(c)$$$ is the number of occurrences of the character $$$c$$$ in the string $$$t$$$.
For example, if $$$t = $$$ "abca", then $$$f($$$'a'$$$) = 2$$$, $$$f($$$'b'$$$) = 1$$$, and $$$f($$$'c'$$$) = 1$$$, and the power of $$$t$$$ is equal to $$$1^2 \times 2^1 \times 3^1 \times 4^2 = 96$$$.
Given a string $$$s$$$ of length $$$n$$$, and $$$q$$$ queries of the following form:
— "$$$l, r$$$": determine whether it is possible to swap two characters in $$$t[l \dots r]$$$ such that the power of $$$t[l \dots r]$$$ increases.
The first line of the input contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of the string $$$s$$$.
The second line of the input contains a string $$$s$$$ of length $$$n$$$ consisting of lowercase English letters.
The third line of the input contains a single integer $$$q$$$ ($$$1 \le q \le 10^5$$$) — the number of queries.
Each of the following $$$q$$$ lines consists of two space-separated integers $$$l$$$ and $$$r$$$ ($$$1 \le l \lt r \le n$$$).
Output $$$q$$$ lines, the $$$i$$$-th of which contains the answer of the $$$i$$$-th query as follows:
Print "YES" (without quotes) if it is possible to increase the power in the $$$i$$$-th query, and print "NO"(without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
5ababa21 21 3
NO YES
2aa11 2
NO
A grid of size $$$n \times n$$$ is constructed in the following way:
The first row of the grid consists of the numbers $$$1, 2, 3, \dots, n$$$ (in that order).
Then, each other cell in the grid will be filled with the square of the number in the cell above it.
Given an integer $$$n$$$, count the number of distinct integers in an $$$n \times n$$$ grid.
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of testcases.
The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) – the number of rows and columns in the grid.
For each testcase, output a single integer — the number of distinct integers in the grid.
223
3 7
The grid in the first testcase:
| 1 | 2 |
| 1 | 4 |
The grid in the second testcase:
| 1 | 2 | 3 |
| 1 | 4 | 9 |
| 1 | 16 | 81 |
Define $$$mex(b)$$$ as the minimum non-negative integer that doesn't appear in the array $$$b$$$. For example, $$$mex([1, 0, 2]) = 3$$$ and $$$mex([1]) = 0$$$.
Given an array $$$a$$$ of length $$$n$$$. You can do the following operation at most once:
What is the maximum value of $$$mex(a)$$$ after performing the above operation at most once.
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 10^6$$$) — the size of the array.
The second line of the input consists of $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le n$$$).
Output a single integer — the maximum possible value of $$$mex(a)$$$.
41 2 3 0
4
22 1
2
Given a string $$$s$$$ of length $$$n$$$ consisting of lowercase English letters.
You have to answer $$$q$$$ queries of the following form:
— "$$$l$$$ $$$r$$$": Find the minimum size of substring $$$s[i \dots j]$$$ ($$$l \le i \le j \le r$$$) such that the number of distinct characters in $$$s[i \dots j]$$$ is equal to the number of distinct characters in $$$s[l \dots r]$$$.
The first line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the string.
The second line of the input contains a string $$$s$$$ of length $$$n$$$ consisting of lowercase English letters.
The third line of the input contains a single integer $$$q$$$ ($$$1 \le q \le 10^5$$$) — the number of queries.
Each of the following $$$q$$$ lines of the input consists of two space-separated integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$).
Output $$$q$$$ lines, the $$$i$$$-th of which contains a single integer — the answer to the $$$i$$$-th query.
3aba11 3
2
Given a string $$$s$$$ consisting of lowercase English letters.
You can do the following operation on $$$s$$$:
— Choose some substring $$$s[l \dots r]$$$, and let $$$x$$$ be the number of distinct characters in $$$s[l \dots r]$$$.
— Change all characters in $$$s[l \dots r]$$$ to the $$$x$$$-th character in the alphabet (e.g., the first character is 'a', the second is 'b', and so on).
You apply the above operation any number of times (possibly zero) such that $$$\textbf{no index gets changed more than once}$$$.
What is the lexicographically maximum string that could be obtained?
The first and only line of the input contains a string $$$s$$$ consisting of lowercase English letters ($$$1 \le |s| \le 10^6$$$).
The lexicographically maximum string that could be obtained.
ab
bb
adbbab
cccccc
adbad
ccccd
The city of Banis has a hotel consisting of $$$n$$$ floors.
The $$$i$$$-th floor (from the bottom) can withstand a weight of $$$w_i$$$ (i.e., there should be no more than $$$w_i$$$ clients on the $$$i$$$-th floor $$$\textbf{or above}$$$, otherwise, the hotel would collapse).
The hotel manager expects $$$m$$$ clients to come to the hotel this month, and each client has a certain rank.
If the rank of client $$$i$$$ is $$$r_i$$$ ($$$1 \le r_i \le n$$$), then the manager would want them to be on the $$$r_i$$$-th floor or lower.
However, he does not know the ranks of the clients. Therefore, $$$r_i$$$ can be from $$$1$$$ to $$$n$$$ with equal probability.
If a client is placed on the $$$i$$$-th floor, they would give the hotel a rating of $$$i$$$.
As a part of the hotel's staff, you need to place the clients optimally to get the maximum possible sum of ratings (but while still satisfying the conditions mentioned above).
Let $$$X$$$ be the sum of the ratings that each client gives.
What is the expected value of $$$X$$$ if you place the $$$m$$$ clients optimally? You should print the answer modulo $$$10 ^ 9 + 7$$$.
The first line of the input consists of two space-separated integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2000$$$) — the number of floors and the number of clients, respectively.
The second line of the input consists of $$$n$$$ space-separated integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \le w_i \le 2000$$$) — the $$$i$$$-th of which denotes the weight that the $$$i$$$-th floor can withstand.
It's guaranteed that $$$w_1 \ge m$$$ and $$$w_1 \ge w_2 \ge \dots \ge w_n$$$.
Output one line containing a single integer — the expected value of the sum of ratings that each client would give to the hotel if they were placed optimally, taken modulo $$$10 ^ 9 + 7$$$.
It is guaranteed that the answer can always be represented as an irreducible fraction $$$\frac{a}{b}$$$, where $$$b \mod (10^9 + 7) \ne 0$$$; you have to print $$$a \cdot b^{-1} \mod (10^9 + 7)$$$.
5 25 5 4 4 4
6
5 11 1 1 1 1
3
3 77 3 1
888888906
Given an array $$$a$$$ of length $$$n$$$, and you should answer $$$q$$$ queries of the following form:
— "$$$x$$$": Find the minimum value of $$$a_i \oplus a_j$$$ ($$$1 \le i \lt j \le n$$$) such that ($$$a_i \: \: | \: \: a_j \: \: | \: \: x \: = \: x$$$).
Note that $$$\oplus$$$ and $$$|$$$ denote the bitwise $$$XOR$$$ and the bitwise $$$OR$$$ operations, respectively.
The first line of the input contains a single integer $$$n$$$ ($$$2 \le n \le 10^6$$$) — the length of the array $$$a$$$.
The second line of the input consists of $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$).
The third line of the input contains a single integer $$$q$$$ ($$$1 \le q \le 10^6$$$) — the number of queries.
Each of the following $$$q$$$ lines contains a single integer $$$x$$$ ($$$1 \le x \le 10^6$$$) — the description of the $$$i$$$-th query.
Output $$$q$$$ lines, the $$$i$$$-th of which contains a single integer — the answer to the query, or $$$-1$$$ if there is no such pair that satisfies the conditions.
31 2 43364
3 6 -1
It's hard to predict the winner when legends face each other, especially in chess.
Ahmad and Ahmad are playing a series of $$$n$$$ chess matches, and one of them will win more games than the other, as there will be no ties and $$$n$$$ is odd.
Can you predict the winner?
The first and only line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 10^9 - 1$$$) — the number of matches.
It is guaranteed that $$$n$$$ is odd.
Output one line containing the name of the winner.
1
Ahmad
You are given $$$n$$$ distinct points $$$(x_1, y_1), (x_2, y_2), \dots, (x_n, y_n)$$$.
Each point $$$i$$$ has a color $$$c_i$$$.
Find the $$$\textbf{squared}$$$ Euclidean distance between the farthest pair of points that are not from the same color.
Note: The Euclidean distance between two points $$$(a_1, b_1)$$$ and $$$(a_2, b_2)$$$ is defined as follows: $$$\sqrt{(a_1 - a_2)^2 + (b_1 - b_2)^2}$$$.
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^5$$$) — the number of testcases.
The first line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 10^6$$$) — the number of points.
Each of the next $$$n$$$ lines of each testcase consists of three space-separated integers, $$$x_i$$$, $$$y_i$$$, and $$$c_i$$$ ($$$|x_i|, |y_i| \le 10^9$$$ and $$$1 \le c_i \le n$$$).
It is guaranteed that if $$$i \ne j$$$ then $$$(x_i, y_i, c_i) \ne (x_j, y_j, c_j)$$$.
It is also guaranteed that there will be at least two different colors among the colors of the $$$n$$$ points.
It is also guaranteed that the sum of $$$n$$$ over all testcases will not exceed $$$10^6$$$.
For each testcase, output a single line containing a single integer, the squared Euclidean distance between the farthest pair of points that are from different colors.
240 0 10 1 21 0 22 2 130 0 11 1 22 2 3
5 8
12-1 -1 1-3 -4 2
13
Define the median of an array $$$b_1, b_2, \dots b_k$$$ as $$$b_{\lfloor \frac{k + 1}{2} \rfloor}$$$ after sorting the array $$$b$$$.
For example, the median of the array $$$b = [5, 4, 6, 1, 1]$$$ is $$$4$$$ because after sorting the array becomes $$$b = [1, 1, 4, 5, 6]$$$ and $$$b_{\lfloor \frac{5 + 1}{2} \rfloor} = b_{\lfloor \frac{6}{2} \rfloor} = b_3 = 4$$$. In the same way, the median of the array $$$[3, 5, 2, 7]$$$ is $$$3$$$.
Given an array $$$a_1, a_2, \dots, a_n$$$, is it possible to divide it into two non-empty arrays $$$b$$$ and $$$c$$$ (not necessarily of the same length), such that each element of $$$a$$$ is either in $$$b$$$ or in $$$c$$$, and the medians of both $$$b$$$ and $$$c$$$ are equal?
Note: $$$\lfloor x \rfloor$$$ denotes the integer part of $$$x$$$ (e.g., $$$\lfloor 5.3 \rfloor = 5$$$ and $$$\lfloor 2.7 \rfloor = 2$$$).
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 testcase contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the length of the array $$$a$$$.
The second line of each testcase consists of $$$n$$$ space-separated integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9)$$$.
It is guaranteed that the sum of $$$n$$$ over all testcases will not exceed $$$2 \cdot 10^5$$$.
Output $$$t$$$ lines. For each testcase, print "YES" (without quotes) if there exists two non-empty arrays that satisfy the conditions, and print "NO"(without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
322 132 3 266 4 3 9 3 3
NO YES YES
Explanation of the first testcase:
The possible ways to divide the array $$$a$$$ into two non-empty arrays $$$b$$$ and $$$c$$$ are:
$$$b = [2]$$$, $$$c = [1]$$$
$$$b = [1]$$$, $$$c = [2]$$$
In both ways, the medians of $$$b$$$ and $$$c$$$ are not equal.
Explanation of the second testcase:
A possible way to divide the array $$$a$$$ into two non-empty arrays $$$b$$$ and $$$c$$$ is:
$$$b = [2]$$$, $$$c = [3, 2]$$$
In this case, the medians of $$$b$$$ and $$$c$$$ are equal to $$$2$$$, and remember that when calculating the median you should first sort the array. That's why, the median of $$$c$$$ is $$$2$$$.
Explanation of the third testcase:
Some possible ways to divide the array $$$a$$$ into two non-empty arrays $$$b$$$ and $$$c$$$ with equal medians are:
$$$b = [4, 9, 3, 3]$$$, $$$c = [6, 3]$$$
$$$b = [6, 4, 3, 3]$$$, $$$c = [3, 9]$$$
$$$b = [4, 3]$$$, $$$c = [6, 9, 3, 3]$$$
Kaito has $$$n$$$ Zingers, and he wants to distribute them between the Kaito kciD team members.
Kaito kciD team consists of 3 members, one of them is greedy Taim.
In order to distribute the $$$n$$$ Zingers between them, Kaito will follow the steps below in order:
— If $$$n$$$ is divisible by $$$3$$$, each of them will get $$$\frac{n}{3}$$$ Zingers.
— Otherwise, if $$$n$$$ is divisible by $$$2$$$, Taim will get $$$\frac{N}{2}$$$ Zingers, and the other two will get the rest.
— Otherwise, Taim will get all the $$$n$$$ Zingers.
Taim can steal at most $$$k$$$ Zingers before Kaito distributes them among the team.
What is the maximum number of Zingers Taim could have (stolen Zingers included)?
The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases.
The first line of each testcase consists of two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 10^9$$$) — the number of Zingers Kaito will give, and the number of Zingers Taim can steal from Kaito before distributing the Zingers.
Output $$$t$$$ lines. For each testcase, print a single integer — the maximum number of Zingers Taim could have.
36 110 120 2
6 5 20