problems
problems
You are given two integer sequences A1 , A2 , … , AN and B1 , B2 , … , BM . For any two sequences
U 1 , U 2 , … , U p and V1 , V2 , … , Vq , we define
p q
Score(U , V ) = ∑ ∑ U i ⋅ Vj .
i=1 j=1
Input
The first line of the input contains a single integer T denoting the number of test cases. The
description of T test cases follows.
The first line of each test case contains two integers, N and M , denoting the length of A and B
respectively.
The second line contains N integers, elements of A .
The third line contains M integers, elements of B .
The next line will contain an integer, Q, number of queries.
Each of the next Q lines will contain one of 3 kinds of updates as mentioned in the statement
It’s guaranteed that each update is a valid update operation.
Output
For each query of the third type, print a single line containing one integer - the answer to that query.
Constraints
1 ≤ T ≤ 10
5
2 ≤ N , M , Q ≤ 10
5
0 ≤ |A i |, |Bi |, |X| ≤ 10
Example Input
1
3 4
2 -1 5
3 3 2 4
6
3
1 2 3 -2
3
1 1 3 1
2 2 4 2
3
Example Output
72
24
90
Explanation
Before the first operation, A = [2, −1, 5], B = [3, 3, 2, 4]
+(−3) ∗ 2 +(−3) ∗ 4 +3 ∗ 3 +3 ∗ 3 +3 ∗ 2 +3 ∗ 4 = 24 .
Beauty and The Array (BEAUAR)
Let’s define a function score:
m
i=1
Here p[i. . . j] means sub-array of p starting from i-th position and ending at j-th position.
You are given two arrays A and B of n integers. You can do the following operation any number of
times, possibly zero.
Perform a left circular rotation on both of the arrays simultaneously i.e move the first element of each
array to the last position and shift all remaining elements one position left. For example, If an array is
{1, 5, 3, 2} and you perform left circular rotation once, then the array will become {5, 3, 2, 1} . If you
Input
The first line of the input contains a single integer T denoting the number of test cases. The
description of T test cases follows.
Each test case consists of 3 lines:
First line contains n, number of elements of the array.
Next line contains n space separated integers, denoting elements of the array A .
Next line contains n space separated integers, denoting elements of the array B .
Output
For each testcase, output in a single line answer for that test case, which is the maximum
Beauty(A, B) that can be achieved.
Constraints
1 ≤ T ≤ 10
17
1 ≤ n ≤ 2
17
0 ≤ |A i |, |Bi | ≤ 2
Example Input
1
5
-1 3 -8 -1 4
3 1 -2 3 -10
Example Output
7
Explanation
In the sample test, to get the maximum beauty value (7), you have to perform the left circular operation 2
times. Then the arrays will be :
A = {−8, −1, 4, −1, 3}
B = {−2, 3, −10, 3, 1} .
If we now pick i = 3, j = 5 , we get Score(A[3...5], B[3...5]) = 7.
This is the maximum that we can obtain. So, Beauty(A, B) = 7
IPL Begins (FCIPL)
This year p footballers and q cricketers have been invited to participate in IPL (Indian Programming
League) as guests. You have to accommodate them in r rooms such that-
No room may remain empty.
A room may contain either only footballers or only cricketers, not both.
No cricketers are allowed to stay alone in a room.
Find the number of ways to place the players. Note though, that all the rooms are identical. But each of
the cricketers and footballers are unique.
Since the number of ways can be very large, print the answer modulo 998, 244, 353 .
Input
The first line of the input contains a single integer T denoting the number of test cases. The
description of T test cases follows.
The first and only line of each test case contains three space-separated integers p , q and r denoting
the number of footballers, cricketers and rooms.
Output
For each test case, output the number of ways to place the players modulo 998, 244, 353 .
Constraints
1 ≤ T ≤ 100
1 ≤ p, q, r ≤ 100
Example Input
4
2 1 4
2 4 4
2 5 4
2 8 4
Example Output
0
3
10
609
Explanation
Example case 2: Three possible ways are:
{Footballer 1}, {Footballer 2}, {Cricketer 1, Cricketer 2}, {Cricketer 3, Cricketer 4}
{Footballer 1}, {Footballer 2}, {Cricketer 1, Cricketer 3}, {Cricketer 2, Cricketer 4}
{Footballer 1}, {Footballer 2}, {Cricketer 1, Cricketer 4}, {Cricketer 2, Cricketer 3}
Please note that the rooms are identical.
Is This a Give Away (GVAWAY)
You are given two integers l and r .
You have to choose k real numbers in the interval [l, r] uniform randomly.
What is the expected count of distinct numbers chosen by you?
Input
The first line of the input contains a single integer T denoting the number of test cases. The
description of T test cases follows.
The first and only line of each test case contains three space-separated integers l, r and k.
Output
For each test case, print a single line containing one integer - the expected count of distinct numbers
chosen. It can be proved that the expected count is always an integer.
Constraints
5
1 ≤ T ≤ 10
1 ≤ k ≤ 100
1 ≤ l ≤ r ≤ 100
Example Input
3
3 6 4
1 3 1
6 7 2
Example Output
4
1
2
Explanation
Example case 2: You choose only 1 real number, so it doesn't matter which real number you chose,
number of distinct number is always 1 . So expected count of distinct number is 1 .
K-Prob (KPRB)
You are given a string S of length n and an integer K .
We will define F (L) here. Let's choose K random sub-strings of length L from S .
Then F (L) equals the probability such that all of them are pairwise identical.
See the Explanation section for more clarity.
You have to find F (L) for each 1 ≤ L ≤ n .
P
It can be shown that any F (L) can be expressed as a fraction , where P and Q are coprime integers,
Q
Input:
First line will contain two integers, n (the length of string S ) and K .
Next line will contain the string S .
Output:
Print n space separated integers, F (L), for each 1 ≤ L ≤ n , in a single line,
Constraints
6
1 ≤ n ≤ 10
6
2 ≤ K ≤ 10
Sample Input:
5 3
ababa
Sample Output:
239578645 748683265 332748118 748683265 1
Explanation:
In our sample, for L = 3 , we have 3 sub-strings :
aba (Starting from 1st position)
bab (Starting from 2nd position)
aba (Starting from 3rd position)
Among them we can chose in the following ways, to get all 3 of them pairwise identical:
1 1 1 1
(1, 1, 1) : P robability = × × =
3 3 3 27
1 1 1 1
(2, 2, 2) : P robability = × × =
3 3 3 27
1 1 1 1
(3, 3, 3) : P robability = × × =
3 3 3 27
1 1 1 1
(1, 1, 3) : P robability = × × =
3 3 3 27
1 1 1 1
(1, 3, 1) : P robability = × × =
3 3 3 27
1 1 1 1
(3, 1, 1) : P robability = × × =
3 3 3 27
1 1 1 1
(3, 3, 1) : P robability = × × =
3 3 3 27
1 1 1 1
(3, 1, 3) : P robability = × × =
3 3 3 27
1 1 1 1
(1, 3, 3) : P robability = × × =
3 3 3 27
(1, 3, 3) means that the three random substrings chosen are the substring starting at index 1, at index 3
and at index 3.
1 1
So F (3) = 9 × 27
=
3
Nicks Landing (LNDNCK)
In a city called Nick's Landing, there are N people (numbered 1 through N ). For each valid i, the i-th
person has beauty Bi and politeness Pi ; no two people are equally beautiful.
You should answer Q queries. In each query you are given two integers L and R , and you should
calculate Score(L, R) as defined below:
Score (L, R) :
Initialize empty Lists Q, OddQ, EvenQ
For each person with id between [L, R] :
add his id to List Q
Sort Q in increasing order of the beauty value of the persons
Flag = 0;
For each person's id in Q :
Flag = 1 - Flag
If (Flag == 1) append his id to List OddQ
Else append his id to List EvenQ
Return DiffSum (OddQ) + DiffSum (EvenQ)
DiffSum (Z[1...n]) :
Sum = 0
For i = 2 to n :
u = Z[i]
v = Z[i - 1]
Sum = Sum + |P[u] - P[v]|
Return Sum
Input
The first line of the input contains a single integer N .
Each of the next N lines contains two space-separated integers Bi and Pi .
The next line contains a single integer Q.
Each of the next Q lines contains two space-separated integers L and R describing a query.
Output
For each query, print a single integer(answer to the query) in a single line.
Constraints
5
1 ≤ N , Q ≤ 2 ⋅ 10
5
1 ≤ Bi , Pi ≤ 2 ⋅ 10
1 ≤ L ≤ R ≤ N
Example Output
2
15
10
8
0
Explanation
For the 2 -nd query, Q = {1, 2, 3, 4, 5, 6} .
If we sort them according to the persons's beauty value, then Q = {3, 1, 4, 2, 6, 5}.
Now, OddQ = {3, 4, 6} and EvenQ = {1, 2, 5}
So, Dif f Sum (OddQ) = |P3 - P4 | + |P4 - P6 | = 6 + 2 = 8.
So Score = 7 + 8 = 15.
Minimize the Distance (MINDIST)
You are given two line segments AB and CD in N -dimensional space. Find the minimum distance
between them, i.e. the minimum distance |PQ| over all points P ∈ AB and Q ∈ CD .
Input
The first line of the input contains a single integer T denoting the number of test cases. The
description of T test cases follows.
The first line of each test case contains a single integer N .
The second line contains N space-separated integers a1 , a2 , … , aN denoting the coordinates of the
point A.
The third line contains N space-separated integers b1 , b2 , … , bN denoting the coordinates of the
point B.
The fourth line contains N space-separated integers c1 , c2 , … , cN denoting the coordinates of the
point C.
The fifth line contains N space-separated integers d1 , d2 , … , dN denoting the coordinates of the
point D .
Output
For each query, print a single line containing one real number - the minimum distance between the line
segments. Your output will be considered correct if the absolute or relative error does not exceed 10 −12 .
Constraints
5
1 ≤ T ≤ 10
5
3 ≤ N ≤ 10
Example Input
2
3
879 130 645
214 801 973
487 518 681
186 74 684
3
810 702 731
598 325 737
158 871 316
336 999 983
Example Output
146.899739355990
587.980716360221
Prime-partite Graph (PRMPRT)
We have a graph with N nodes (numbered 1 through N ). For each valid u and v , there is an undirected
edge between nodes u and v if v is a prime divisor of u or u is a prime divisor of v .
You should answer Q queries. In each query, you should find the length of the shortest path between two
given nodes a and b or determine that there is no path between them.
Input
The first line of the input contains a single integer N .
The second line contains a single integer Q.
Each of the next Q lines contains two space-separated integers a and b describing a query.
Output
For each query, print a single line containing one integer - the distance between the nodes or −1 if the
nodes are not connected.
Constraints
6
1 ≤ N , Q ≤ 10
2 ≤ a, b ≤ N
Example Input
20
3
2 3
2 6
2 9
Example Output
2
1
3
Explanation
For a = 2 and b = 9 , one possible shortest path is 2 → 6 → 3 → 9 .
Walk 4 steps (WALK4)
You are given a weighted undirected graph consisting of n nodes and m edges. The nodes are
numbered from 1 to n. The graph does not contain any multiple edges or self loops.
A walk W on the graph is a sequence of vertices (with repetitions of vertices and edges allowed) such
that every adjacent pair of vertices in the sequence is an edge of the graph. We define the cost of a walk
W , C ost(W ) , as the maximum over the weights of the edges along the walk.
You will be given q queries. In each query, you will be given an integer X .
You have to count the number of different walks W of length 4 such that C ost(W ) = X .
Two walks are considered different if they do not represent the same edge sequence.
Input:
First line contains 2 integers : the number of nodes n and number of edges m .
Next m lines each describe u, v and w , describing an edge between u and v with weight w .
Next line contains q , the number of queries.
Next q lines each describe an integer X - the cost of the walk in the query.
Output:
For each query, output in a single line the number of different possible walks.
Constraints
1 ≤ n ≤ 100
n(n−1)
1 ≤ m ≤
2
1 ≤ u, v ≤ n
1 ≤ w ≤ 100
1 ≤ q ≤ 100
1 ≤ X ≤ 100
Sample Input:
3 3
1 2 1
2 3 2
3 1 3
3
1
2
3
Sample Output:
2
10
36
EXPLANATION:
For X = 2 , all possible 10 walks are listed below :
1 -> 2 -> 1 -> 2 -> 3
1 -> 2 -> 3 -> 2 -> 1
1 -> 2 -> 3 -> 2 -> 3
2 -> 1 -> 2 -> 3 -> 2
2 -> 3 -> 2 -> 1 -> 2
2 -> 3 -> 2 -> 3 -> 2
3 -> 2 -> 1 -> 2 -> 1
3 -> 2 -> 1 -> 2 -> 3
3 -> 2 -> 3 -> 2 -> 1
3 -> 2 -> 3 -> 2 -> 3
Weighted Necklace (WGTNKLC)
You are given K different colors of beads. A bead of the i-th color has a weight wi . You have infinitely
many beads of each color. All the beads of the same color have the same weight.
You have to answer q independent queries. For each query, you will be given S . You have to form a
necklace consisting of n beads such that the total weight of the necklace is S . You can use any number
of beads of a single color (possibly zero). Count the number of distinct possible necklaces you can make.
Two necklaces will be considered the same if one can be rotated to another.
Input
First line contains n, denoting the size of the necklace.
Second line contains K , denoting the number of different colors.
Third line contains K space separated integers, where the i-th integer wi denotes the weight of a
bead of color i.
Fourth line contains q , denoting the number of queries.
Each of the next q lines will contain a single integer S , denoting the total weight of the necklace for this
query.
Output
For each query, output in a new line, the number of valid necklaces mod 998244353.
Constraints
5
1 ≤ n, q, K , wi , S ≤ 10
Example Input
4
4
1 2 3 4
4
5
6
7
8
Example Output
1
3
5
9
Explanation
For S = 6, three ways to form the necklace are:
1 -> 1 -> 1 -> 3
1 -> 1 -> 2 -> 2
1 -> 2 -> 1 -> 2
Please note, we do not count 2 -> 1 -> 1 -> 2 because it is just a rotation of 1 -> 1 -> 2 -> 2.