Problem Solution Dytechlab Cup 2022

                                             Dytechlab Cup 2022

A. Ela Sorting Books
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Ela loves reading a lot, just like her new co-workers in DTL! On her first day after becoming an engineer in DTL, she is challenged by a co-worker to sort a heap of books into different compartments on the shelf.

n books must be split into k compartments on the bookshelf (n is divisible by k). Each book is represented by a lowercase Latin letter from 'a' to 'y' inclusively, which is the beginning letter in the title of the book.

Ela must stack exactly nk books in each compartment. After the books are stacked, for each compartment indexed from 1 to k, she takes the minimum excluded (MEX) letter of the multiset of letters formed by letters representing all books in that compartment, then combines the resulting letters into a string. The first letter of the resulting string is the MEX letter of the multiset of letters formed by the first compartment, the second letter of the resulting string is the MEX letter of the multiset of letters formed by the second compartment, ... and so on. Please note, under the constraint of this problem, MEX letter can always be determined for any multiset found in this problem because 'z' is not used.

What is the lexicographically greatest resulting string possible that Ela can create?

A string a is lexicographically greater than a string b if and only if one of the following holds:

  • b is a prefix of a, but ba;
  • in the first position where a and b differ, the string a has a letter that appears later in the alphabet than the corresponding letter in b.

The minimum excluded (MEX) letter of a multiset of letters is the letter that appears earliest in the alphabet and is not contained in the multiset. For example, if a multiset of letters contains 7 letters 'b''a''b''c''e''c''f' respectively, then the MEX letter of this compartment is 'd', because 'd' is not included in the multiset, and all letters comes before 'd' in the alphabet, namely 'a''b' and 'c', are included in the multiset.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t100). Description of the test cases follows.

The first line of each test case contains two integers n and k (1n2001kn). It is guaranteed that n is divisible by k.

The second line of each test case contains a string of n lowercase Latin letters from 'a' to 'y' inclusively. Each letter represents the starting letter of the title of a book in the initial heap.

It is guaranteed that the sum of n over all test cases does not exceed 1000.

Output

For each test case, output a string of k letters which is the most optimal string that Ela can find.

Example
input
Copy
5
12 3
cabccadabaac
12 6
cabccadabaac
12 12
cabccadabaac
25 1
abcdefghijklmnopqrstuvwxy
10 5
bcdxedbcfg
output
Copy
edb
ccbbba
bbbbbaaaaaaa
z
aaaaa
Note

In the first test case, the books can be divided into 3 compartments as below:

  • the first compartment contains the books with indices 1,2,3,7multiset1={'c''a''b''d'}  MEX(multiset1)= 'e'
  • the second compartment contains the books with indices 4,5,6,9 : multiset2={'c''c''a''b'}  MEX(multiset2)= 'd'
  • the third compartment contains the remaining books 8,10,11,12 : multiset3={ 'a''a''a''c'}  MEX(multiset3)= 'b'

Therefore, the answer is 'edb'. It can be proven that there is no way that Ela can arrange the books so that it results in a lexicographically greater string.

B. Ela's Fitness and the Luxury Number
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

While working at DTL, Ela is very aware of her physical and mental health. She started to practice various sports, such as Archery, Yoga, and Football.

Since she started engaging in sports activities, Ela switches to trying a new sport on days she considers being "Luxury" days. She counts the days since she started these activities, in which the day she starts is numbered as day 1. A "Luxury" day is the day in which the number of this day is a luxurious number.

An integer x is called a luxurious number if it is divisible by x.

Here r denotes the "floor" of a real number r. In other words, it's the largest integer not greater than r.

For example: 856100 are luxurious numbers, since 8 is divisible by 8=2.8284=256 is divisible 56=7.4833=7, and 100 is divisible by 100=10=10, respectively. On the other hand 540 are not, since 5 are not divisible by 5=2.2361=2, and 40 are not divisible by 40=6.3246=6.

Being a friend of Ela, you want to engage in these fitness activities with her to keep her and yourself accompanied (and have fun together, of course). Between day l and day r, you want to know how many times she changes the activities.

Input

Each test contains multiple test cases. The first line has the number of test cases t (1t10 000). The description of the test cases follows.

The only line of each test case contains two integers l and r (1lr1018) — the intervals at which you want to know how many times Ela changes her sports.

Output

For each test case, output an integer that denotes the answer.

Example
input
Copy
5
8 19
8 20
119 121
1 100000000000000000
1234567891011 1000000000000000000
output
Copy
5
6
2
948683296
2996666667
Note

In the first test case, 5 luxury numbers in range [8,19] are: 8,9,12,15,16.

C. Ela and Crickets
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Ela likes Chess a lot. During breaks, she usually challenges her co-worker in DTL to some chess games. She's not an expert at classic chess, but she's very interested in Chess variants, where she has to adapt to new rules and test her tactical mindset to win the game.

The problem, which involves a non-standard chess pieces type that is described below, reads: given 3 white crickets on a nn board, arranged in an "L" shape next to each other, there are no other pieces on the board. Ela wants to know with a finite number of moves, can she put any white cricket on the square on row x, column y?

An "L"-shape piece arrangement can only be one of the below:

For simplicity, we describe the rules for crickets on the board where only three white crickets are. It can move horizontally, vertically, or diagonally, but only to a square in some direction that is immediately after another cricket piece (so that it must jump over it). If the square immediately behind the piece is unoccupied, the cricket will occupy the square. Otherwise (when the square is occupied by another cricket, or does not exist), the cricket isn't allowed to make such a move.

See an example of valid crickets' moves on the pictures in the Note section.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t104). The description of the test cases follows.

The first line of each test case contains n (4n105) — denotes the size of the chessboard.

The second line of each test case contains 6 numbers: r1c1r2c2r3c3 (1r1,c1,r2,c2,r3,c3n) — coordinates of the crickets. The input ensures that the three crickets are arranged in an "L" shape that the legend stated.

The third line of each test case contains 2 numbers: xy (1x,yn) — coordinates of the target square.

Output

For each test case, print "YES" or "NO" to denotes whether Ela can put a cricket on the target square.

Example
input
Copy
6
8
7 2 8 2 7 1
5 1
8
2 2 1 2 2 1
5 5
8
2 2 1 2 2 1
6 6
8
1 1 1 2 2 1
5 5
8
2 2 1 2 2 1
8 8
8
8 8 8 7 7 8
4 8
output
Copy
YES
NO
YES
NO
YES
YES
Note

Here's the solution for the first test case. The red square denotes where the crickets need to reach. Note that in chess horizontals are counted from bottom to top, as well as on this picture.

D. Ela and the Wiring Wizard
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ela needs to send a large package from machine 1 to machine n through a network of machines. Currently, with the network condition, she complains that the network is too slow and the package can't arrive in time. Luckily, a Wiring Wizard offered her a helping hand.

The network can be represented as an undirected connected graph with n nodes, each node representing a machine. m wires are used to connect them. Wire i is used to connect machines ui and vi, and has a weight wi. The aforementioned large package, if going through wire i, will move from machine ui to machine vi (or vice versa) in exactly wi microseconds. The Wiring Wizard can use his spell an arbitrary number of times. For each spell, he will choose the wire of index i, connecting machine ui and vi, and rewire it following these steps:

  • Choose one machine that is connected by this wire. Without loss of generality, let's choose vi.
  • Choose a machine that is currently connecting to vi (including ui), call it ti. Disconnect the wire indexed i from vi, then using it to connect ui and ti.

The rewiring of wire i will takes wi microseconds, and the weight of the wire will not change after this operation. After a rewiring, a machine might have some wire connect it with itself. Also, the Wiring Wizard has warned Ela that rewiring might cause temporary disconnections between some machines, but Ela just ignores it anyway. Her mission is to send the large package from machine 1 to machine n as fast as possible. Note that the Wizard can use his spell on a wire zero, one, or many times. To make sure the network works seamlessly while transferring the large package, once the package starts transferring from machine 1, the Wiring Wizard cannot use his spell to move wires around anymore.

Ela wonders, with the help of the Wiring Wizard, what is the least amount of time needed to transfer the large package from machine 1 to n.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t100). The description of the test cases follows.

The first line contains n and m (2n500n1m250000), the number of nodes and number of wires, respectively.

For the next m lines, i-th line will contains uivi and wi (1ui,vin1wi109) - the indices 2 machines that are connected by the i-th edge and the weight of it.

It is guaranteed that the sum of n over all test cases does not exceed 500 and the sum of m over all test cases does not exceed 250000. The graph in each test case is guaranteed to be connected, no self-loops, but it can contain multiple edges.

Output

For each test case, output one integer denotes the least amount of time needed to transfer the large package from machine 1 to n.

Example
input
Copy
3
8 9
1 2 3
6 4 5
3 5 6
6 1 3
7 4 4
3 8 4
2 3 3
7 8 5
4 5 2
4 5
1 2 1
2 4 1
3 4 1
3 1 1
1 3 2
8 8
4 6 92
7 1 65
6 5 43
6 7 96
4 3 74
4 8 54
7 4 99
2 5 22
output
Copy
9
2
154
Note

Here is the graph in the first test case in the sample input:

Ela can ask the Wiring Wizard to use his spell on wire with the index of 7, which is connecting machines 2 and 3. Then, since the machine 8 is connected to machine 3, the Wiring Wizard can disconnect wire 7 from machine 3 and connect it to machine 8 in 3 microseconds (weight of wire 3).

After that, the package can be sent from machine 1 to machine 8 in 6 microseconds. Therefore, the answer is 3+6=9 microseconds.

Here is the graph in the third test case in the sample input:

E. Ela Goes Hiking
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ela likes to go hiking a lot. She loves nature and exploring the various creatures it offers. One day, she saw a strange type of ant, with a cannibalistic feature. More specifically, an ant would eat any ants that it sees which is smaller than it.

Curious about this feature from a new creature, Ela ain't furious. She conducts a long, non-dubious, sentimental experiment.

She puts n cannibalistic ants in a line on a long wooden stick. Initially, the ants have the same weight of 1. The distance between any two consecutive ants is the same. The distance between the first ant in the line to the left end and the last ant in the line to the right end is also the same as the distance between the ants. Each ant starts moving towards the left-end or the right-end randomly and equiprobably, at the same constant pace throughout the experiment. Two ants will crash if they are standing next to each other in the line and moving in opposite directions, and ants will change direction immediately when they reach the end of the stick. Ela can't determine the moving direction of each ant, but she understands very well their behavior when crashes happen.

  • If a crash happens between two ants of different weights, the heavier one will eat the lighter one, and gain the weight of the lighter one. After that, the heavier and will continue walking in the same direction. In other words, if the heavier one has weight x and walking to the right, the lighter one has weight y and walking to the left (x>y), then after the crash, the lighter one will diminish, and the heavier one will have weight x+y and continue walking to the right.
  • If a crash happens between two ants with the same weight, the one walking to the left end of the stick will eat the one walking to the right, and then continue walking in the same direction. In other words, if one ant of weight x walking to the left, crashes with another ant of weight x walking to the right, the one walking to the right will disappear, and the one walking to the left will have to weight 2x and continue walking to the left.

Please, check the example in the "Note" section, which will demonstrate the ants' behavior as above.

We can prove that after a definite amount of time, there will be only one last ant standing. Initially, each ant can randomly and equiprobably move to the left or the right, which generates 2n different cases of initial movements for the whole pack. For each position in the line, calculate the probability that the ant begins in that position and survives. Output it modulo 109+7.

Formally, let M=109+7. It can be shown that the answer can be expressed as an irreducible fraction pq, where p and q are integers and q0(modM). Output the integer equal to pq1modM. In other words, output such an integer x that 0x<M and xqp(modM).

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t103). The description of the test cases follows.

The only line of each test contains an integer n (1n106) — the number of ants in the experiment.

It is guaranteed that the sum of n in all tests will not exceed 106.

Output

For each test, print n lines. i-th line contains a single number that denotes the survival probability of the i-th ant in the line modulo 109+7.

Example
input
Copy
3
4
5
2
output
Copy
0
250000002
250000002
500000004
0
250000002
250000002
250000002
250000002
0
1
Note

Here is the example of 6 ants moving on the branch. An ant's movement will be denoted by either a character L or R. Initially, the pack of ants on the branch will move as RLRRLR. Here's how the behavior of the pack demonstrated:

Initially, the ants are positioned as above.

After a while, the ant with index 2 (walking to the left) will crash with the ant with index 1 (walking to the right). The two ants have the same weight, therefore, ant 2 will eat ant 1 and gain its weight to 2. The same thing happens with ant 5 and ant 4.

The ant 6 will walk to the end of the stick, therefore changing its direction.

After that, the ant with index 5 will crash with the ant with index 3. Since ant 5 is more heavy (weight=2) than ant 3 (weight=1), ant 5 will eat ant 3 and gain its weight to 3.

Ant 2 will walk to the end of the stick, therefore changing its direction.

After that, the ant with index 5 will crash with the ant with index 2. Since ant 5 is more heavy (weight=3) than ant 2 (weight=2), ant 5 will eat ant 2 and gain its weight to 5.

Lastly, after ant 5 walk to the end of the branch and changes its direction, ant 5 will eat ant 6 and be the last ant standing.

F. Ela and Prime GCD
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

After a long, tough, but fruitful day at DTL, Ela goes home happily. She entertains herself by solving Competitive Programming problems. She prefers short statements, because she already read too many long papers and documentation at work. The problem of the day reads:

You are given an integer c. Suppose that c has n divisors. You have to find a sequence with n1 integers [a1,a2,...an1], which satisfies the following conditions:

  • Each element is strictly greater than 1.
  • Each element is a divisor of c.
  • All elements are distinct.
  • For all 1i<n1gcd(ai,ai+1) is a prime number.

In this problem, because c can be too big, the result of prime factorization of c is given instead. Note that gcd(x,y) denotes the greatest common divisor (GCD) of integers x and y and a prime number is a positive integer which has exactly 2 divisors.

Input

The first line contains one integer t (1t104) - the number of test cases.

The first line of each test case contains one integer m (1m16) - the number of prime factor of c.

The second line of each test case contains m integers b1,b2,,bm (1bi<220) — exponents of corresponding prime factors of c, so that c=p1b1p2b2pmbm and n=(b1+1)(b2+1)(bm+1) hold. pi is the i-th smallest prime number.

It is guaranteed that the sum of nm over all test cases does not exceed 220.

Output

Print the answer for each test case, one per line. If there is no sequence for the given c, print 1.

Otherwise, print n1 lines. In i-th line, print m space-separated integers. The j-th integer of i-th line is equal to the exponent of j-th prime number from ai.

If there are multiple answers, print any of them.

Example
input
Copy
5
2
1 1
1
1
3
1 1 1
1
4
2
2 1
output
Copy
0 1 
1 1 
1 0 
1 
0 1 1 
0 0 1 
1 0 1 
1 1 0 
0 1 0 
1 1 1 
1 0 0 
-1
2 0 
1 1 
0 1 
2 1 
1 0 
Note

In each test case, the values of c are 623016, and 12 in that order.

In the first test case, 1236 are divisors of 6. Here, sequences [2,6,3] and [3,6,2] can be answer. Permutation [3,2,6] is invalid because gcd(a1,a2)=1 is not a prime number.

In the forth test case, 124816 are divisors of 16. Among the permutation of sequence [2,4,8,16], no valid answer exist.

G. Ela Takes Dancing Class
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

DTL engineers love partying in the weekend. Ela does, too! Unfortunately, she didn't know how to dance yet. Therefore, she decided to take a dancing class.

There are n students in the dancing class, including Ela. In the final project, n students will participate in a choreography described below.

n students are positioned on the positive side of the Ox-axis. The i-th dancer is located at ai>0. Some dancers will change positions during the dance (we'll call them movable dancers), and others will stay in the same place during a choreography (we'll call them immovable dancers). We distinguish the dancers using a binary string s of length n: if si equals '1', then the i-th dancer is movable, otherwise the i-th dancer is immovable.

Let's call the "positive energy value" of the choreography d>0. The dancers will perform "movements" based on this value.

Each minute after the dance begins, the movable dancer with the smallest x-coordinate will start moving to the right and initiate a "movement". At the beginning of the movement, the dancer's energy level will be initiated equally to the positive energy value of the choreography, which is d. Each time they move from some y to y+1, the energy level will be decreased by 1. At some point, the dancer might meet other fellow dancers in the same coordinates. If it happens, then the energy level of the dancer will be increased by 1. A dancer will stop moving to the right when his energy level reaches 0, and he doesn't share a position with another dancer.

The dancers are very well-trained, and each "movement" will end before the next minute begins.

To show her understanding of this choreography, Ela has to answer q queries, each consisting of two integers k and m. The answer to this query is the coordinate of the m-th dancer of both types from the left at k-th minute after the choreography begins. In other words, denote xk,1,xk,2,,xk,n as the sorted coordinates of the dancers at k-th minute from the beginning, you need to print xk,m.

Input

The first line contains three integers nd and q (1n1051d1091q105) — the number of dancers, the positive energy value of the choreography, and the number of queries.

The second line contains n integers a1,a2,,an (1a1<a2<<an109) — the coordinates of each dancer.

The third line contains a binary string s of length n — the movability of each dancer. Each character is either '0' or '1'. It is guaranteed that s contains at least one character '1'.

Then q lines follow, the i-th of them contains two integers ki and mi (1ki1091min) — the content of each query.

Output

Output q lines, each contains a single integer — the answer for the corresponding query.

Examples
input
Copy
4 3 8
1 3 6 7
1011
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
output
Copy
3
5
6
7
3
6
7
10
input
Copy
1 1 5
2
1
1 1
2 1
3 1
4 1
5 1
output
Copy
3
4
5
6
7
Note

Let's consider the first example test case.

In the first minute, 1 is the lowest coordinate between movable dancers. The energy level is initiated with 3. Then the following happens:

  • The dancer moves from 1 to 2. The energy level decreased to 2.
  • The dancer moves from 2 to 3. The energy level decreased to 1, then increased to 2 when she met another dancer at 3.
  • The dancer moves from 3 to 4. The energy level decreased to 1.
  • The dancer moves from 4 to 5. The energy level decreased to 0.

At the end of the first minute, the sorted coordinates of the dancers become [3,5,6,7], and their respective movability is '0111'.

In the second minute, 5 is the lowest coordinate between movable dancers. The energy level is initiated with 3. Then the following happens:

  • The dancer moves from 5 to 6. The energy level decreased to 2, then increased to 3 when she met another dancer at 6.
  • The dancer moves from 6 to 7. The energy level decreased to 2, then increased to 3 when she met another dancer at 7.
  • The dancer moves from 7 to 8. The energy level decreased to 2.
  • The dancer moves from 8 to 9. The energy level decreased to 1.
  • The dancer moves from 9 to 10. The energy level decreased to 0.

At the end of the second minute, the sorted coordinates of the dancers become [3,6,7,10], and their respective movability is '0111'.

Comments

Popular posts from this blog

Whiteboarding Interviews

Version Control with Git: A Beginner’s Guide

Callback function in JavaScript