All integers from $$$l$$$ to $$$r$$$ inclusive are written on the board, each exactly once.
We call a number on the board beautiful if it is equal to the sum of all numbers on the board, including this number itself. For example, if the numbers $$$-4$$$, $$$-1$$$, $$$2$$$, $$$3$$$, $$$5$$$ are written on the board, then the number $$$5$$$ is beautiful.
You may erase any numbers from the board, but at least one number must remain. Find the minimum number of numbers that need to be erased so that after that there is at least one beautiful number on the board. It is not necessary to erase any numbers if there is already a beautiful number on the board.
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
Each test case consists of one line containing two integers $$$l$$$ and $$$r$$$ ($$$-100 \le l \le r \le 100$$$).
For each test case, print one integer — the minimum number of numbers that need to be erased from the board.
80 02 6-5 -3-2 2-2 3-2 5-6 20 4
04200233
For the test cases from the statement, you should do the following:
We call a binary string $$$s$$$ of length $$$m$$$ periodic if there exists an integer $$$k$$$ ($$$1 \le k \lt m$$$) such that $$$s_i = s_{i+k}$$$ for every $$$i$$$ from $$$1$$$ to $$$m-k$$$ inclusive. The characters of the string are numbered starting from $$$1$$$. The smallest such $$$k$$$ is called the period of the string.
We call a string $$$s$$$ rhythmic if it is periodic and among the first $$$p$$$ characters of the string (where $$$p$$$ is the period of the string), there is exactly one 1.
For example, the string 0010010 is periodic with period $$$p=3$$$ and rhythmic: among the first three characters there is exactly one 1. The string 0110110 is also periodic with period $$$p=3$$$, but it is not rhythmic: among the first three characters there are two 1s.
You are given an array $$$a$$$ of $$$n$$$ integers and a string $$$s$$$, initially consisting of $$$10^{18}$$$ zeros. For each element $$$a_i$$$, all characters of the string whose indices are divisible by $$$a_i$$$ are replaced by ones. In other words, the characters at positions $$$a_i,2a_i,3a_i,\ldots$$$, not exceeding the length of the string, become ones. Replacing a character with one again does nothing.
For example, if $$$a=[3,7,5,3]$$$, then the string $$$s$$$ will begin with 001011101101011...
Before performing these changes, you may remove any elements from array $$$a$$$. Find the minimum number of elements that need to be removed so that the resulting string $$$s$$$ is rhythmic.
The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases.
The following descriptions of the test cases follow.
The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 5000$$$) — the length of array $$$a$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of the array.
Additional constraints on the input:
For each test case, output one integer — the minimum number of elements that need to be removed from the array so that string $$$s$$$ is rhythmic.
443 7 5 334 8 1246 10 20 3032 3 5
2012
In the first test case, you can remove elements $$$7$$$ and $$$5$$$. Then the string will begin with 001001001... and will be rhythmic with period $$$3$$$.
In the second test case, nothing needs to be removed. The string will begin with 000100010001... and will be rhythmic with period $$$4$$$.
In the third test case, it is enough to remove element $$$6$$$. The resulting string will be rhythmic with period $$$10$$$.
In the fourth test case, you need to keep any one element of the array.
You are given an integer $$$n$$$. Consider the array $$$a = [1, 2, \ldots, n]$$$. You may choose any elements of the array and multiply each chosen element by $$$2$$$. Each element can be doubled at most once. You may choose any subset of elements, including all of them or none of them. Denote the resulting array by $$$b$$$.
Let $$$k$$$ be the bitwise OR of all elements of the original array $$$a$$$, and let $$$m$$$ be the bitwise XOR of all elements of the resulting array $$$b$$$:
$$$$$$ k = a_1 \mathbin{|} a_2 \mathbin{|} \cdots \mathbin{|} a_n, \qquad m = b_1 \oplus b_2 \oplus \cdots \oplus b_n. $$$$$$
Choose which elements to double so that $$$m \ge 2k$$$. It can be shown that this is possible for every valid value of $$$n$$$. You do not need to minimize the number of doubled elements.
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
The only line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the array.
Additional constraints on the input:
For each test case, print $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$. Each $$$d_i$$$ must be either $$$0$$$ or $$$1$$$: if $$$d_i = 1$$$, the element $$$a_i$$$ should be doubled; if $$$d_i = 0$$$, it should be left unchanged.
If there are multiple valid answers, output any of them.
5369511
1 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 1 0 0 0 1 0 1 1 0 1 1 0 1
In the first test case, $$$k = 3$$$. After doubling the chosen elements, $$$b = [2, 2, 6]$$$, and $$$m = 6$$$.
In the second test case, $$$k = 7$$$. After doubling the chosen elements, $$$b = [1, 4, 6, 4, 5, 12]$$$, and $$$m = 14$$$.
You are given a tree with $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$. Initially, all vertices are white.
You must choose a single integer $$$k$$$ from $$$1$$$ to $$$2n$$$, inclusive. Then, for each $$$i$$$ from $$$1$$$ to $$$n$$$ in increasing order, if the tree contains a vertex numbered $$$k-i$$$ and $$$k-i \ne i$$$, the following operation is performed:
You must choose $$$k$$$ to maximize the number of black vertices (higher priority) and the number of gray vertices (lower priority). In other words, choose $$$k$$$ so that the number of black vertices in the tree is as large as possible, and among the values of $$$k$$$ that give the maximum number of black vertices, choose one that gives as many gray vertices as possible.
Find any optimal value of $$$k$$$. If there are multiple suitable values of $$$k$$$, print any of them.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 3 \cdot 10^5$$$) — the number of vertices in the tree.
Each of the following $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n$$$) — the vertices connected by an edge.
Additional constraints on the input:
For each test case, print one integer $$$k$$$ ($$$1 \le k \le 2n$$$) that satisfies the requirements of the problem. If there are multiple optimal values, output any of them.
551 21 33 44 561 22 32 44 54 655 11 33 22 455 15 25 35 453 44 11 22 5
67755
In the first test case, $$$k=6$$$ is chosen. After all operations, vertices $$$1$$$, $$$2$$$, $$$4$$$, and $$$5$$$ are black, and vertex $$$3$$$ is gray.
In the second test case, $$$k=7$$$ is chosen. After all operations, all six vertices are black, and there are no gray vertices.
You are given a binary string $$$s$$$ of length $$$n$$$. The characters of the string are numbered from $$$1$$$ to $$$n$$$.
You have to partition all integers from $$$1$$$ to $$$n$$$ into nonempty sets so that each integer belongs to exactly one set. For each pair of distinct integers $$$x$$$ and $$$y$$$ in the same set, at least one of the following conditions must hold:
For a set $$$M$$$, let $$$f(M)$$$ be the number of distinct characters among $$$s_x$$$ over all $$$x \in M$$$. The cost of the set $$$M$$$ is $$$|M| \cdot f(M)$$$, where $$$|M|$$$ is the number of elements in $$$M$$$. The cost of a partition is the sum of the costs of all its sets.
For example, if the string $$$s$$$ is 10001000, the partition into the sets $$$\{1, 2\}, \{3, 8\}, \{4, 5, 6\}, \{7\}$$$ is valid, and its cost is $$$2 \cdot 2 + 2 \cdot 1 + 3 \cdot 2 + 1 \cdot 1 = 13$$$.
Find the maximum possible cost of a partition and the number of partitions with this cost. Partitions that differ only in the order of the sets or the order of elements within the sets are considered the same. Output the number of partitions modulo $$$998\,244\,353$$$.
The first line contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases.
The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 5000$$$) — the length of the string.
The second line of each test case contains one string $$$s$$$ of length $$$n$$$, consisting only of the characters 0 and 1.
For each test case, print two integers: the maximum cost of a partition and the number of partitions with this cost (taken modulo $$$998\,244\,353$$$).
38100010006000111801010101
13 58 416 4
You are given a directed graph with $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$. The graph is constructed as follows: for each vertex $$$i$$$ from $$$2$$$ to $$$n$$$, you are given two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \lt i$$$). For each integer $$$j \in [l_i, r_i]$$$, the graph contains a directed edge from $$$j$$$ to $$$i$$$. There are no other edges in the graph.
Consider a second directed graph on the same $$$n$$$ vertices. For any two distinct vertices $$$x$$$ and $$$y$$$, a path from $$$x$$$ to $$$y$$$ must exist in the second graph if and only if it exists in the first graph.
Find the minimum possible number of edges in the second graph.
The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
The first line of each test case contains one integer $$$n$$$ ($$$2 \le n \le 3 \cdot 10^5$$$) — the number of vertices.
The next $$$n-1$$$ lines describe the edges of the graph. The $$$i$$$-th of these lines contains two integers $$$l_{i+1}$$$ and $$$r_{i+1}$$$ ($$$1 \le l_{i+1} \le r_{i+1} \le i$$$).
Additional constraints on the input:
For each test case, print one integer — the minimum possible number of edges in the second graph.
551 11 22 32 461 11 12 32 34 551 11 21 31 461 11 11 12 42 461 11 21 12 43 5
48496
In the first test case, you can add the edges $$$1 \to 2$$$, $$$2 \to 3$$$, $$$3 \to 4$$$, and $$$4 \to 5$$$ to the second graph.
In the second test case, you can add the edges $$$1 \to 2$$$, $$$1 \to 3$$$, $$$2 \to 4$$$, $$$3 \to 4$$$, $$$2 \to 5$$$, $$$3 \to 5$$$, $$$4 \to 6$$$, and $$$5 \to 6$$$ to the second graph.
You are given a directed graph with $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$. Each vertex has at least one incoming edge. The graph may contain self-loops, but it has no multiple edges. For each vertex $$$v$$$, an integer $$$k_v$$$ is given, ranging from $$$1$$$ to the number of incoming edges of that vertex.
Each vertex has a number written on it. Every second, all the numbers are updated as follows. For a vertex $$$v$$$, take the numbers at the vertices $$$u$$$ for which there is an edge $$$u\to v$$$, sort them in non-decreasing order, and update vertex $$$v$$$ with the number at the position $$$k_v$$$. Positions in the sorted list are numbered starting from $$$1$$$. In other words, the new number at vertex $$$v$$$ is the $$$k_v$$$-th order statistic of the array consisting of the numbers at the vertices $$$u$$$ for which there is an edge $$$u \to v$$$.
All vertices are updated simultaneously: only the numbers from before the current update are used to compute the new numbers. If a vertex has a self-loop, its own number also affects the update.
You have to process $$$q$$$ queries of the following form:
The first line contains three integers $$$n$$$, $$$m$$$, and $$$q$$$ ($$$1\le n\le20$$$; $$$n\le m\le n^2$$$; $$$1\le q\le10^5$$$) — the number of vertices, edges, and queries, respectively.
The second line contains $$$n$$$ integers $$$k_1,k_2,\ldots,k_n$$$. For each vertex $$$v$$$, it holds that $$$1\le k_v\le d_v$$$, where $$$d_v$$$ is the number of incoming edges of that vertex.
Each of the next $$$m$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1\le u,v\le n$$$), denoting an edge from $$$u$$$ to $$$v$$$. All ordered pairs $$$(u,v)$$$ are distinct.
Each of the next $$$q$$$ lines contains $$$n+1$$$ integers $$$t,a_1,a_2,\ldots,a_n$$$ ($$$0\le t\le10^{18}$$$; $$$1\le a_i\le10^9$$$) — the parameters of a query.
For each query, print $$$n$$$ integers — the values on vertices $$$1,2,\dots,n$$$ after $$$t$$$ seconds.
3 3 41 1 11 22 33 10 4 8 151 4 8 152 4 8 151000000000000000000 1 2 3
4 8 15 15 4 8 8 15 4 3 1 2
2 4 31 21 11 22 12 21 10 11000000000000000000 7 70 1000000000 1
1 10 7 7 1000000000 1
4 8 41 2 2 11 12 11 22 23 22 34 33 41 8 3 5 12 8 3 5 13 8 3 5 11000000000000000000 8 3 5 1
3 5 3 5 3 3 5 3 3 3 3 5 3 3 5 3
In the first example, each number moves to the next vertex of the cycle. After three seconds, all numbers return to their original positions.
In the second example, the first vertex takes the minimum of the two numbers, while the second takes the maximum.
In the third example, the array $$$[8,3,5,1]$$$ becomes $$$[3,5,3,5]$$$ after the first update and $$$[3,3,5,3]$$$ after the second update.