You are given $$$2$$$ arrays $$$A$$$ and $$$B$$$ of length $$$n$$$. You need to determine if there exists a permutation $$$P$$$ such that the following constraints are satisfied for all $$$1 \leq i \leq n$$$:
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2\times 10^5$$$), the number of test cases.
For each of the test case,
The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 5\times 10^5$$$).
The second line contains $$$n$$$ space separated integers $$$A_1, A_2, \dots, A_n$$$ denoting the elements of $$$A$$$ ($$$1 \leq A_i \leq i)$$$.
The third line contains $$$n$$$ space separated integers $$$B_1,B_2,\dots, B_n$$$ denoting the elements of $$$B$$$ ($$$1 \leq B_i \leq n-i+1$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5\times 10^5$$$.
For each of the test case, if there doesn't exist any permutation print $$$-1$$$. If there exists, print any valid permutation.
511131 1 21 2 131 2 31 1 151 1 2 3 23 3 2 1 151 2 2 2 22 2 1 2 1
1 3 1 2 -1 3 1 4 5 2 -1
For test case $$$4$$$,
Similarly
Bob is a student enrolled in a university program with $$$n$$$ single credit courses this semester.
Each course has a course maximum of $$$2047$$$ and has a cutoff for grades, where a grade $$$10$$$ requires a minimum of $$$1024$$$ marks, $$$9$$$ requires $$$512$$$ marks, and so on. Formally, one must score greater than or equal to $$$2^i$$$ for grade $$$i$$$.
Bob, unfortunately, forgot the marks he scored in each course. All he remembers is the XOR of all the marks he got. Now, Bob wants to maximise his Semester Grade Point Average (SGPA), but he needs your help to calculate it. If Bob's marks in the courses are $$$a_1, \dots, a_n$$$, then their SGPA is $$$\frac1n \sum_{i = 1}^n a_i$$$.
Given the XOR of all the marks he got in his courses your task is to write a program to find the maximium SGPA Bob can achieve this semester.
Here XOR denotes the bitwise XOR operation.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ ($$$2 \le n \le 10^3$$$) and $$$x$$$ ($$$0 \le x \le 2047$$$), representing the number of courses Bob is enrolled in, and the XOR of Bob's marks in the $$$n$$$ courses, respectively.
Output a single integer which represents $$$n$$$ multiplied by the maximum possible SGPA achievable by Bob.
32 02 10243 45
20 19 29
Here's an explanation of the samples,
You are a student enrolled in $$$n$$$ courses, and each course has a certain number of assignments, represented by an array $$$a$$$. The $$$i^{th}$$$ element of the array $$$a$$$ represents the number of assignments in the $$$i^{th}$$$ course. Your objective is to minimize the total number of assignments you need to complete across all courses. To reduce total number of assignments, you can apply the following operation any number of times (possibly zero) on the array,
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t\leq 5\times 10^4$$$), the number of test cases. The description of the test cases follows.
The first line contains an integer $$$n$$$ $$$(1 \leq n \leq 10^5)$$$, the number of courses.
The second line contains $$$n$$$ space separated integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$), where $$$a_i$$$ represents the number of assignments in the $$$i$$$-th course.
The sum of $$$n$$$ over all test cases do not exceed $$$10^5$$$.
For each test case output a single integer representing the minimum possible sum of the array.
41124 236 10 241000000 4 10 5638383
1 6 14 147578
This is an interactive problem!
There are $$$n$$$ ($$$n$$$ is odd) students in your class indexed from $$$1$$$ to $$$n$$$, each with marks between $$$-10^9$$$ and $$$10^9$$$ (excluding $$$0$$$). You can ask the marks of the $$$j^{th}$$$ student to the $$$i^{th}$$$ the student($$$i$$$ $$$\ne$$$ $$$j$$$), if the marks of the $$$i^{th}$$$ student is positive, then he will always truthfully reply the correct marks of the $$$j^{th}$$$ student else he may flip the sign of the marks. You know that the median of the class is positive and you want to find the student with the maximum marks in no more than $$$\dfrac{3(n+1)}{2}$$$ queries.
The query should be of the form: $$$?$$$ $$$i$$$ $$$j$$$ ($$$i$$$ $$$\ne$$$ $$$j$$$). The answer will be:
It is given that there exists a unique student with maximum marks.
The first line contains a single odd integer $$$n$$$ ($$$3 \le n \le 10^3$$$) — the number of students.
The interaction between your program and the jury's program begins after reading the number of students $$$n$$$.
To make a query, output a line in the format "? i j", where $$$i \ne j$$$.
As an answer, you will receive a non-zero integer $$$x$$$ ($$$-10^9 \le x \le 10^9$$$), the marks of the $$$j^{th}$$$ student given by $$$i^{th}$$$ student.
You can make no more than $$$\dfrac{3(n+1)}{2}$$$ queries. To output the answer you need to print "! i", where $$$i$$$ is the student with maximum marks. Outputting the answer does not count as a query.
The interactor in this problem is not adaptive.
You have to use a flush operation right after printing each line. For example, in C++ you should use the function fflush(stdout), in Java — System.out.flush(), in Pascal — flush(output) and in Python — sys.stdout.flush().
5 2 -4 4
? 1 2 ? 5 4 ? 5 4 ! 4
Marks of the $$$5$$$ people are $$$1, 2, 3, 4, -5$$$.
They answer $$$2$$$, since 1 has to tell the truth.
They answer $$$-4$$$, since 5 chose to lie.
They answer $$$4$$$, since 5 chose not to lie.
You are given a tree $$$T$$$ with $$$n$$$ vertices rooted at vertex $$$1$$$. The vertex $$$i$$$ initially has color $$$c_i$$$. Additionally, there are $$$q$$$ queries, each of which is either of the following 2 types:
The first line contains a single integer t $$$(1≤t≤2⋅10^5)$$$ — the number of test cases. The description of the test cases follows.
The first line of each test case contains two space separated integers $$$n$$$ $$$(1≤n≤2⋅10^5)$$$ and $$$q$$$ $$$(1≤q≤2⋅10^5)$$$.
The second line contains $$$n$$$ integers $$$c_1, c_2, ..., c_n$$$ $$$(1≤ c_i ≤n)$$$ — the initial colors of vertices.
The $$$i$$$-th of the next $$$n−1$$$ lines contains two integers $$$v_i$$$ and $$$u_i$$$ $$$(1≤ v_i,\,u_i ≤n;\, v_i≠u_i)$$$ — the $$$i$$$-th edge of the tree.
The $$$j$$$-th of the next $$$q$$$ lines contains three integers $$$type$$$, $$$v$$$ and $$$x$$$ $$$(1≤ type ≤2,\, 1≤ v ≤n,\, 1≤ x ≤n)$$$ — the $$$j$$$-th query.
It is guaranteed that the given edges form a valid tree. The sum of $$$n$$$ and sum of $$$q$$$ over all testcases do not exceed $$$2⋅10^5$$$. Also, there is atleast one $$$type\ 2$$$ query in each testcase.
For each query of $$$type\ 2$$$, print a single integer — the number of vertices which have color $$$x$$$ in the subtree of $$$v$$$.
28 83 4 2 3 4 1 7 21 21 34 22 53 83 76 82 1 31 2 31 6 32 1 32 2 32 3 21 7 22 3 24 61 1 1 11 22 33 42 1 11 1 12 1 11 3 22 1 32 2 2
2 4 2 2 3 4 4 0 1
There are two independent parts of this problem. The common part is highlighted in italics.
There are $$$n$$$ soldiers positioned at locations $$$a_i$$$, each armed with a gun of power $$$p_i$$$.
Whenever they spot a monster with a shield with $$$s$$$ standing at location $$$d$$$, each soldier fires a bullet towards it. The bullet starts with an initial effective power of $$$p_i$$$, but due to air resistance, this power decreases by 1 for every unit the bullet travels.
The Bullet will hit the monster if the effective power is greater than or equal to the shield width. Then let the number of bullets that hit the monster be $$$c(d,s)$$$
Formally, let $$$c(d,s)$$$ be the number of $$$1 \leq i\leq n$$$ such that $$$|a_i - d| \leq p_i - s$$$.
You are given $$$q$$$ queries for each query compute $$$\sum_{d=l}^r c(d, s)$$$
The first line contains two integers $$$n \:(1\leq n \leq 10^6)$$$ and $$$q \:(1\leq q \leq 10^6)$$$.
The second line contains $$$n$$$ integers $$$a_i \:(1\leq a_i \leq 2\times 10^3)$$$ denoting the location of the $$$i^{\mathrm{th}}$$$ soldier.
The third line contains $$$n$$$ integers $$$p_i \:(0\leq p_i \leq 10^3)$$$ denoting the power of the $$$i^{\mathrm{th}}$$$ soldier.
For the next $$$q$$$ lines, each line contains three integers $$$l\:(1\leq l \leq 2\times 10^3)$$$, $$$r\:(l\leq r \leq 2\times 10^3)$$$ and $$$s \:(1\leq s \leq 10^3)$$$ denoting the location of the monster and the shield width for that day.
Clarification: The shield width must be a non-negative integer, the position of the monster must be an integer.
For each query, print a single integer, the sum of the number of bullets that hit the monsters.
5 33 4 3 6 50 1 1 2 32 5 12 4 22 6 1
6 1 8
$$$c(3, 1) = 2$$$, $$$\because (i = 3, 5)$$$ satisfy the above inequality
Similarly, $$$c(2, 1) = 0$$$, $$$c(4, 1) = 2$$$, $$$c(5,1) = 2$$$
$$$\sum_{d=2}^5 c(d, 1) = 6$$$
There are two independent parts of this problem. The common part is highlighted in italics.
There are $$$n$$$ soldiers positioned at locations $$$a_i$$$, each armed with a gun of power $$$p_i$$$.
Whenever they spot a monster with a shield with $$$s$$$ standing at location $$$d$$$, each soldier fires a bullet towards it. The bullet starts with an initial effective power of $$$p_i$$$, but due to air resistance, this power decreases by 1 for every unit the bullet travels.
The Bullet will hit the monster if the effective power is greater than or equal to the shield width. Then let the number of bullets that hit the monster be $$$c(d,s)$$$
Formally, let $$$c(d,s)$$$ be the number of $$$1 \leq i\leq n$$$ such that $$$|a_i - d| \leq p_i - s$$$.
You are a monster and can survive no more than $$$k$$$ bullet hits on you. What minimum shield width would you choose so that you can stand anywhere and survive?
Clarification: The shield width must be a non-negative integer, the position of the monster must be an integer.
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1\leq t\leq 10^4)$$$ — the number of test cases. This is followed by the description of the test cases.
The first line contains two integers $$$n \:(1\leq n \leq 10^5)$$$ and $$$k \:(0\leq k \leq n)$$$.
The second line contains $$$n$$$ integers $$$a_i \:(1\leq a_i \leq 10^9)$$$ denoting the location of the $$$i^{\mathrm{th}}$$$ soldier.
The third line contains $$$n$$$ integers $$$p_i \:(0\leq p_i \leq 10^9)$$$ denoting the power of the $$$i^{\mathrm{th}}$$$ soldier.
It is guaranteed that the sum of the values of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output a single integer $$$s$$$ the minimum shield width so that you can stand anywhere and survive.
24 12 3 2 41 0 1 010 717 5 7 17 17 5 6 9 2 824 1 21 29 28 19 23 9 25 11
2 12
For the first testcase
If $$$s \leq 1$$$, You will die at $$$d = 2$$$ ($$$\because$$$ Bullet of Soldier 1 and Soldier 3 will hit you)
It can be shown that you will survive with $$$s = 2$$$ no matter where you stand.
Given a rooted tree on $$$n$$$ vertices with values $$$a_1, \dots, a_n$$$ written on the vertices, we define the score of a vertex as the product of $$$a_u$$$ for all vertices $$$u$$$ in the subtree of $$$v$$$.
However, you've lost the tree and just the values remain. All you remember is the following,
Even though there are $$$(n - 1)!$$$ such trees possible, you try to compute the score of each vertex using just this information.
Output the expected score, modulo $$$998\,244\,353$$$, of each vertex on uniformly randomly sampling a tree satisfying the above constraints.
Formally, let $$$M = 998\,244\,353$$$. It can be shown that the expected score can be expressed as an irreducible fraction $$$\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \not \equiv 0 \pmod{M}$$$. Output the integer equal to $$$p \cdot q^{-1} \bmod M$$$. In other words, output an integer $$$x$$$ such that $$$0 \le x \lt M$$$ and $$$x \cdot q \equiv p \pmod{M}$$$.
The first line contains an integer $$$n$$$ ($$$2 \le n \le 2 \times 10^5$$$), the size of the lost tree.
The second line contains $$$n$$$ space-separated integers $$$a_1, \dots, a_n$$$ ($$$0 \le a_i \lt 998\,244\,353$$$), $$$a_i$$$ being the value written on the $$$i$$$-th vertex.
Output $$$n$$$ integers, the expected score of the vertices $$$1, 2, \dots, n$$$ in that order.
24 2
8 2
41 2 3 4
24 11 6 4
In sample 2, the following 6 trees are possible
For an array $$$a$$$ of integers, let's denote $$$m_0$$$ as the maximum possible sum of a subarray $$$a_l, a_{l+1}, ..., a_r$$$ for some $$$1 \leq l \leq r \leq n$$$ such that $$$a_i \geq 0$$$ for all $$$l \leq i \leq r$$$, and $$$m_1$$$ as the minimum possible sum of a subarray $$$a_l, a_{l+1}, ..., a_r$$$ for some $$$1 \leq l \leq r \leq n$$$ such that $$$a_i \leq 0$$$ for all $$$l \leq i \leq r$$$. An empty subarray should also be considered, it has sum $$$0$$$.
Define k-goodness of an array over a pair $$$x, y$$$ as the maximum value of $$$xm_0 - ym_1$$$ if you are allowed to $$$change$$$ $$$the$$$ $$$sign^\dagger$$$ of at most k elements of the array.
You are given an array $$$a$$$ of $$$n$$$ integers and an integer $$$k$$$ along with $$$m$$$ queries. Each query is described by a pair of integers $$$x_i, y_i$$$ and you have to determine the k-goodness of the array for each pair $$$x_i, y_i$$$.
$$$^\dagger$$$Changing the sign of a number means multiplying the number with $$$-1$$$.
The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 1000)$$$ $$$-$$$ the number of test cases.
The first line of each test case contains two integers $$$n,$$$ $$$k$$$ and $$$m$$$ $$$(1 \leq n \leq 5000, 0 \leq k \leq n, 1 \leq m \leq 5000)$$$ $$$-$$$ the length of the array, the goodness number and the number of queries.
The second line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ $$$(-n \leq a_i \leq n)$$$ $$$-$$$ the elements of the array.
The next $$$m$$$ lines of the input contain the queries, the query number $$$i$$$ is written as two integers: $$$x_i, y_i$$$ $$$(0 \leq x_i \leq 10^4, 0 \leq y_i \leq 10^4)$$$.
Additional constraint on the input:
For each test case, print $$$m$$$ integers, the $$$i$$$-th $$$(1 \leq i \leq m)$$$ of which is the answer to the $$$i$$$-th query.
35 1 32 -1 0 2 02 92 29 21 1 3-12 11 11 21 0 3-12 11 11 2
31 10 45 2 1 2 1 1 2
In first test case: $$$k = 1$$$ is given, so we are allowed to change the sign of at most one element.
Consider the 1st query, its optimal to change the sign of $$$a_1$$$, then the array becomes $$$[-2, -1, 0, 2, 0]$$$, then $$$m_0 = 2$$$ (sum of subarray $$$a_3, a_4$$$) and $$$m_1 = -3$$$ (sum of subarray $$$a_1, a_2$$$). Then k-goodness $$$=$$$ $$$2(2) - 9(-3)$$$ $$$=$$$ $$$31$$$.
Consider the 2nd query, its optimal to change the sign of $$$a_2$$$, then the array becomes $$$[2, 1, 0, 2, 0]$$$, then $$$m_0 = 5$$$ (sum of subarray $$$a_1, ..., a_4$$$) and $$$m_1 = 0$$$ (sum of subarray $$$a_5$$$). Then k-goodness $$$=$$$ $$$2(5) - 2(0)$$$ $$$=$$$ $$$10$$$.
Payas is bored and is lost drawing figures in his notebook. To have fun he writes the numbers from $$$1$$$ to $$$n$$$ in that order, takes a permutation $$$p$$$ of length $$$n$$$ and goes on drawing arcs from $$$p_i$$$ to $$$p_{i+1}$$$. Interestingly he finds that no $$$2$$$ arcs intersect except maybe at the endpoints! Being curious he sets out to find the number of permutations starting and ending with the same elements as $$$p$$$ for which this holds...
Formally you need to find the number of permutations $$$p$$$ of length $$$n$$$ for which the following condition is satisfied:
The first line of the input is a single integer $$$t$$$ ($$$1 \leq t \leq 2 \times 10^5$$$), the number of test cases.
Each of the next $$$t$$$ lines consist of $$$3$$$ space separated integers $$$n$$$ ($$$2 \leq n \leq 10^6$$$), the length of the permutation, $$$x$$$, the first element of the permutation and $$$y-$$$ the last element of the permutation ($$$1 \leq x, y \leq n, x \neq y$$$).
It is guaranteed that the sum of $$$n$$$ over all test-cases does not exceed $$$10^6$$$.
For each test case, print a single integer denoting the number of permutations satisfying the problem condition modulo $$$10^9+7$$$.
33 1 24 3 45 1 4
1 1 3
In the first test case, the desired permutations are $$$[1,3,2]$$$.
In the second test case, the desired permutations are $$$[3,2,1,4]$$$.
In the third test case, the desired permutations are $$$[1,5,2,3,4]$$$, $$$[1,2,3,5,4]$$$ and $$$[1,2,5,3,4]$$$.
The permutation $$$[1,3,2,5,4]$$$ does not satisfy the problem constraints, because the arc that goes from $$$1 \rightarrow 3$$$ intersects the arc going from $$$2 \rightarrow 5$$$. Or in formal notation, for $$$i=1$$$ and $$$j=3$$$, $$$\min(p_1,p_2) \lt \min(p_3,p_4) \lt \max(p_1,p_2) \lt \max(p_3,p_4)$$$, thus violating the problem condition.
You are given $$$m$$$ pairs of alphabets, $$$(a_1, b_1), \dots, (a_m, b_m)$$$.
Given a string $$$s$$$ of length $$$n$$$, we can do the following operation on it,
We say that two strings $$$s$$$ and $$$t$$$ are equivalent if we can transform $$$s$$$ into $$$t$$$ by repeatedly applying the operation on it.
Find the size of largest set consisting of strings of length $$$n$$$ over the first $$$k$$$ lowercase English alphabets such that no two distinct strings in the set are equivalent.
Since the size of this set can be very large, output it modulo $$$998\ 244\ 353$$$.
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\leq t \leq 100$$$), the number of test cases. The description of the test cases follows.
The first line of each test case contains three integers $$$n, m$$$ and $$$k$$$ ($$$1 \leq n \leq 15000$$$, $$$0 \leq m \leq k(k - 1) / 2$$$, $$$1 \leq k \leq 7$$$), the length of the strings, the pairs of characters, and the number of characters.
$$$m$$$ lines follow, the $$$t$$$-th line has 2 characters $$$a_t$$$ and $$$b_t$$$ separated by a space, denoting that we can swap those 2 characters in a string. ($$$a_t \neq b_t$$$, $$$a_t$$$ and $$$b_t$$$ are from the first $$$k$$$ lowercase English alphabets)
The summation of $$$n$$$ over all test cases does not exceed $$$15000$$$.
For each test case, output the size of this largest set modulo $$$998244353$$$.
3 1 0 7 2 1 7 a b 3 1 2 a b
7 48 4
In the first test, all 7 possible strings are non-equivalent.
In the second case, out of the 49 possible strings, only the strings "$$$ab$$$" and "$$$ba$$$" are equivalent. Therefore, answer is 48.