Michelangelo has adopted $$$N$$$ mutant cats and arranged them in a circle ($$$N$$$ is a multiple of 4).
He needs to assign each cat a letter from the set [$$$m$$$, $$$e$$$, $$$o$$$, $$$w$$$, $$$M$$$, $$$E$$$, $$$O$$$, $$$W$$$] to form a continuous circular string.
The string must follow Master Splinter's strict training rules:
You are also given $$$M$$$ constraints. Each constraint is a pair of numbers $$$(u, v)$$$, meaning the cat at index $$$u$$$ and the cat at index $$$v$$$ must be assigned the exact same character (both the letter and the case must match perfectly).
Calculate the total number of valid circular strings you can create that satisfy all the rules and all $$$M$$$ constraints.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — denotes the number of test cases.
for each test case:
The first line is Two space-separated integers $$$N$$$ ($$$4 \le N \le 10^5$$$) and $$$M$$$ ($$$0 \le M \le 10^5$$$) — denotes the number of cats and it is guaranteed to be a multiple of 4, and the number of constraints respectively.
The next $$$M$$$ lines contains Two space-separated integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le N$$$) — denotes a constraint where the character at position $$$u$$$ must equal the character at position $$$v$$$.
It is guaranteed that the sum of $$$N$$$ over all test cases does not exceed $$$10^5$$$.
It is guaranteed that the sum of $$$M$$$ over all test cases does not exceed $$$10^5$$$.
For each test case print the total number of valid circular strings.
516 11 1316 12 1032 316 2018 624 2420 21 52 648 34 2012 2816 32
882016
Raphael and Rayo are trying to fix a corrupted genetic sequence for the team!
They have an initial DNA string $$$a$$$ and a target DNA string $$$b$$$.
To transform string $$$a$$$ into string $$$b$$$, they must use the following techniques:
Trying to figure it out Find the minimum number of replacement operations needed to make string $$$a$$$ match string $$$b$$$. If the transformation is impossible, print -1.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — denotes the number of test cases.
for each test case:
The first line is two strings $$$a$$$ and $$$b$$$ ($$$1 \le |a| , |b| \le 10^5$$$) — denotes the starting DNA and the target DNA, they are guaranteed to only have lowercase letters.
The second line is an integer $$$M$$$ — denotes the number of pairs that can be swapped for free.
The next $$$M$$$ lines contains two integers $$$i$$$ and $$$j$$$ ($$$1 \le i, j \le |a|$$$) — denotes the positions in string $$$a$$$ that can be swapped for free.
It is guaranteed that the sum of $$$|a|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
It is guaranteed that the sum of $$$|b|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
for each test case, print a single integer, the minimum number of replacement operations needed.
If it's impossible, print -1.
3turtle purtel21 64 5abc def0hello world11 2
334
Michelangelo has adopted $$$N$$$ mutant cats and arranged them in a circle ($$$N$$$ is a multiple of 8).
He needs to assign each cat a letter from the set [$$$m$$$, $$$e$$$, $$$o$$$, $$$w$$$, $$$M$$$, $$$E$$$, $$$O$$$, $$$W$$$] to form a continuous circular string.
The string must follow Master Splinter's strict training rules:
You are also given $$$M$$$ constraints. Each constraint is a pair of numbers $$$(u, v)$$$, meaning the cat at index $$$u$$$ and the cat at index $$$v$$$ must be assigned the exact same character (both the letter and the case must match perfectly).
Calculate the total number of valid circular strings you can create that satisfy all the rules and all $$$M$$$ constraints.
Since the answer can be very large, output it modulo $$$10^9 + 7$$$.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — denotes the number of test cases.
for each test case:
The first line is Two space-separated integers $$$N$$$ ($$$8 \le N \le 10^5$$$) and $$$M$$$ ($$$0 \le M \le 10^5$$$) — denotes the number of cats and it is guaranteed to be a multiple of 8, and the number of constraints respectively.
The next $$$M$$$ lines: Two space-separated integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le N$$$) — denotes a constraint where the character at position $$$u$$$ must equal the character at position $$$v$$$.
It is guaranteed that the sum of $$$N$$$ over all test cases does not exceed $$$10^5$$$.
It is guaranteed that the sum of $$$M$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, print the total number of valid circular strings modulo $$$10^9 + 7$$$.
3 8 0 8 1 2 6 8 1 1 6
8 4 0
Leonardo and Raphael are chasing Shredder through a long underground sewer.
The sewer consists of $$$n$$$ chambers arranged in a straight line, numbered from $$$1$$$ to $$$n$$$ from left to right.
At the beginning, Shredder may be anywhere in the sewer. Leonardo blocks the left side at chamber $$$L = 1$$$, and Raphael blocks the right side at chamber $$$R = n$$$. Therefore, the possible area where Shredder can still be hiding is the segment $$$[L, R]$$$.
no comment. The turtles want to shrink this segment as much as possible.
You are given four arrays $$$a$$$, $$$b$$$, $$$c$$$, and $$$d$$$.
For every chamber $$$i$$$, the following conditions hold:
$$$$$$c_i \ge a_i \ge i$$$$$$
and
$$$$$$d_i \le b_i \le i$$$$$$
The turtles can perform the following moves any number of times and in any order:
However, this move is only safe if Raphael is far enough to cover him. Formally, the move is allowed only if:
$$$$$$R \ge c_L$$$$$$
After the move, the left boundary becomes $$$L = a_L$$$.
However, this move is only safe if Leonardo is far enough to cover him. Formally, the move is allowed only if:
$$$$$$L \le d_R$$$$$$
After the move, the right boundary becomes $$$R = b_R$$$.
The turtles may stop after any number of moves. Their goal is to minimize the size of the final possible hiding segment.
Find the minimum possible value of:
$$$$$$R - L + 1$$$$$$
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — denotes the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — denotes the number of chambers.
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$i \le a_i \le n$$$) — denotes the target chamber Leo can move to from chamber $$$i$$$.
The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le i$$$) — denotes the target chamber Raph can move to from chamber $$$i$$$.
The fourth line contains $$$n$$$ integers $$$c_1, c_2, \dots, c_n$$$ ($$$a_i \le c_i \le n$$$) — denotes the minimum chamber position Raph must be at ($$$R \ge c_L$$$) for Leo to safely execute his move from chamber $$$L$$$.
The fifth line contains $$$n$$$ integers $$$d_1, d_2, \dots, d_n$$$ ($$$1 \le d_i \le b_i$$$) — denotes the maximum chamber position Leo can be at ($$$L \le d_R$$$) for Raph to safely execute his move from chamber $$$R$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print a single integer — the minimum possible size of the final segment where Shredder may still be hiding.
2 5 2 2 4 5 5 1 1 2 4 4 4 5 5 5 5 1 1 2 3 4 5 2 4 5 5 5 1 1 2 4 5 3 4 5 5 5 1 1 2 4 5
3 1
Donatello and Moon are working in their lab down in the sewers to synthesize a new chemical agent called Mel Prime! It is Moon's 2nd favorite.
The Mel starts with an initial power level of $$$1$$$ and then Moon performs the following operation on Mel exactly $$$K$$$ times: $$$$$$X = X + \gcd(X, Y)$$$$$$
Donatello trying to feed Moon. Given the integers $$$Y$$$ and $$$K$$$, Calculate the final Mel Prime Power (MPP)!
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^4$$$) — denotes the number of test cases.
Each of the next $$$T$$$ lines contains two space-separated integers $$$Y$$$ and $$$K$$$ ($$$1 \le Y \le 10^9$$$, $$$1 \le K \le 10^9$$$).
For each test case, print a single integer representing the final value of $$$X$$$ after $$$K$$$ operations are complete.
12 2
4
Donatello and Moon are working in their lab down in the sewers to synthesize a new chemical agent called Mel! It is Moon's favorite.
The Mel starts with an initial power level of $$$X$$$.
Donatello and Moon after realising the power of Mel. In a single reaction step, Donatello's and Moon machine takes the smallest prime factor of the current power level and adds it to the total. This reaction process is repeated exactly $$$K$$$ times.
Note: If the power level is prime, its smallest prime factor is itself
Calculate the final Mel power level!
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — denotes the number of test cases.
For each test case, the only line of the test case with two space-separated numbers $$$X$$$ and $$$K$$$ ($$$2 \le X \le 10^9$$$ and $$$1 \le K \le 10^9$$$) — denotes the initial power level of Mel and the number of times the reaction process is repeated, respectively.
For each test case print the final power level of Mel after exactly $$$K$$$ steps.
410 315 417 221 5
16 24 36 32
After seeing the newest ACM reel, Barakat was inspired so he went to sleep, and started dreaming about playing football with Kylian Mbappe, after the game,
Kylian Mbappé has brought two special boxes of pepperoni pizza down for some Ninja Turtles and Barakat. Each box contains exactly 5 slices, and every slice provides a specific amount of energy.
Barakat and Mbappé in his dream. The ninja turtle and Barakt is hungry, but can only eat exactly 4 slices, and they must all be chosen from the same box. Calculate the maximum possible energy a turtle can gain!
The first line is $$$5$$$ integers $$$a_1, b_1, c_1, d_1, e_1$$$ ($$$1 \le a_1, b_1, c_1, d_1, e_1 \le 100$$$) — denotes the energy values of the slices in the first box
The second line is $$$5$$$ integers $$$a_2, b_2, c_2, d_2, e_2$$$ ($$$1 \le a_2, b_2, c_2, d_2, e_2 \le 100$$$) — denotes the energy values of the slices in the second box.
Print a single integer: the maximum possible total energy from eating 4 slices from a single box.
7 15 3 22 1018 4 9 12 6
54
If they choose Box 1, the best 4 slices give: 7 + 15 + 22 + 10 = 54 energy.
If they choose Box 2, the best 4 slices give: 18 + 9 + 12 + 6 = 45 energy.
so the maximum possible total energy from eating 4 slices from a single box is 54
Shaaker recently has built a high-tech database containing the profiles of known mutants in New York City.
Each mutant's profile is uniquely defined by a sequence of $$$q$$$ yes/no traits (represented as 1s and 0s).
The Ninja Turtles have just encountered $$$n$$$ mysterious figures hiding in the sewers! Donatello uses his scanner to read the $$$q$$$ traits of each figure from Shaaker's database.
However, the Foot Clan is using signal jammers. Because of the interference, the data is corrupted: for every figure scanned, exactly one trait is guaranteed to be a lie (a 1 flipped to 0, or a 0 flipped to 1).
Given Shaaker's database and the corrupted scanner readings for the $$$n$$$ encounters, identify the correct original mutant from the database for each encounter!
It is guaranteed that each broken code can be uniquely connected to exactly one original secret code from the database.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — denotes the number of test cases.
In each test case:
The first line contains three space-separated numbers, $$$d$$$ ($$$1 \le d \le 10^5$$$), $$$q$$$ ($$$1 \le q \le 20$$$), and $$$n$$$ ($$$1 \le n \le 10^5$$$) — denotes how many original secret codes, how many digits are in each code, and the number of broken codes, respectively.
Next $$$d$$$ lines: The original secret codes.
Next $$$n$$$ lines: The broken codes.
The sum of $$$d$$$ over all test cases does not exceed 100,000.
The sum of $$$n$$$ over all test cases does not exceed 100,000.
For each test case, output $$$n$$$ lines. Each line should contain a single integer representing the 1-based index (the line number from $$$1$$$ to $$$d$$$) of the database entry that matches the corrected scan.
24 5 3000001010101110110000010010111010105 6 4000000111000100111010101001011111100100011010001001001
1232345
Raph and Mocha have traded naps for competitive programming prep! Since they're tackling the ACPC (Arab Collegiate Programming Contest), they've been spending their afternoon grinding through complex algorithms. They were just in the middle of a deep dive into FFT (Fast Fourier Transform), trying to master polynomial multiplication, when a mysterious digital puzzle appeared on Raph's shell-phone—a parting gift from a defeated Kraang droid.
Mocha and Raphael. You are given an array $$$a$$$ consisting of $$$n$$$ non-negative integers. In one round, every element in the array is updated simultaneously. Specifically, for each index $$$i$$$ ($$$1 \le i \le n$$$), the new value $$$a'_i$$$ is calculated as the bitwise XOR ($$$\oplus$$$) of the prefix minimum up to $$$i$$$ and the suffix maximum starting from $$$i$$$:
$$$$$$a'_i = \min(a_1, a_2, \dots, a_i) \oplus \max(a_i, a_{i+1}, \dots, a_n)$$$$$$
You will repeatedly apply this operation to the array. Your task is to determine the exact number of rounds required until all elements in the array become equal.
Since the number of rounds can potentially be very large, output the answer modulo $$$10^9 + 7$$$.
If the array will never reach a state where all elements are equal, output $$$-1$$$.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — denotes the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — denotes the length of the array $$$a$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) — denotes the elements of the array.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output a single integer — the minimum number of rounds required modulo $$$10^9 + 7$$$. If the elements will never become equal, output $$$-1$$$.
231 1 140 7 2 7
01
In the first test case, the array is initially $$$[1, 1, 1]$$$. All elements are already equal, so the number of rounds required is $$$0$$$.
In the second test case, the array is initially $$$[0, 7, 2, 7]$$$. After the first round, the array updates simultaneously:
After $$$1$$$ round, the array becomes $$$[7, 7, 7, 7]$$$. All elements are equal, so the answer is $$$1$$$.
Moon had the worst breakup ever, again. His ex was way smarter than him, which devastated him.
So, he needed advice from the one and only Apple since Apple is also now in Germany, again. Apple decided to help Moon, he gave Moon a permutation of length $$$n$$$ and told him to go to Master Splinter.
Moon said, "4 JAYYED."
Master Splinter is testing Moon with the Shoree Puzzle
Moon is given a number $$$N$$$. Consider an array of length $$$N$$$ containing the integers from $$$1$$$ to $$$N$$$ exactly once (this is called a permutation).
There are $$$N!$$$ (N-factorial) possible ways to arrange this array. For every possible arrangement, Moon counts the number of inversions. An inversion is a pair of numbers where a larger number is placed before a smaller number.
Let $$$S$$$ be the set of all possible inversion counts you can get from every single arrangement. Your task is to find the $$$\operatorname{MEX}$$$ of the $$$S$$$.
Note: The $$$\operatorname{MEX}$$$ (Minimum EXcluded value) of an array is the smallest non-negative integer that is not in the array. For example:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
For each test case:
The first line and only line contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of the permutation.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case find the $$$\operatorname{MEX}$$$ of the $$$S$$$.
53581220
4112967191
Explanation for the first sample case ($$$n=3$$$): For $$$n=3$$$, the possible permutations and their inversion counts are:
Leonardo is planning the nightly patrols to keep the city safe from the Foot Clan!
Tuffaha gave him a list of $$$n$$$ possible patrol zones. Each zone has a starting street number and an ending street number.
He needs to select exactly $$$k$$$ different patrol zones for his ninja squads. Then, from each chosen zone he must pick exactly one specific street number to set up a stealth ambush.
Leo mad at Tuffaha after Tuffaha chose the wrong streets to buy shawarma. To make their team coordination absolutely perfect, Leonardo wants to maximize the bitwise AND of all $$$k$$$ chosen street numbers.
Calculate the highest possible bitwise AND he can achieve!
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — denotes the number of test cases.
The first line of each test case contains two integers $$$n$$$ ($$$1 \le n \le 10^5$$$) and $$$k$$$ ($$$1 \le k \le n$$$) — denotes the total number of patrol zones, and the number of zones Leonardo must choose respectively.
The second line of each test case contains two space-separated integers $$$l_i$$$ and $$$r_i$$$ ($$$0 \le l_i \le r_i \le 10^9$$$) — denotes the starting and ending street numbers of each zone.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
for each test case, print the maximum possible bitwise AND of the $$$k$$$ chosen street numbers.
33 15 102 415 202 23 75 103 21 24 58 9
2071
Splinter and his students Splinter gave his students an array $$$a$$$ of $$$n$$$ integers. He would like the bitwise XOR of all elements to equal $$$0$$$.
To do so, the students may perform the following operation any number of times:
Find the minimum number of operations needed, and the number of distinct index sequences of that length that achieve it.
Two sequences are considered distinct if they differ in length or at any position.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — denotes the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — denotes the length of the array $$$a$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) — denotes the elements of the array.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output two integers: the minimum number of operations required, and the number of distinct sequences of that length that achieve the goal.
242 2 3 331 2 2
0 11 1
In the first test case, the array is initially $$$[2, 2, 3, 3]$$$. The XOR of all elements is $$$2 \oplus 2 \oplus 3 \oplus 3 = 0$$$. The minimum number of operations is $$$0$$$, and there is $$$1$$$ sequence of length $$$0$$$ (the empty sequence).
In the second test case, the array is initially $$$[1, 2, 2]$$$. The XOR of all elements is $$$1 \oplus 2 \oplus 2 = 1$$$. — If we choose $$$i=1$$$, $$$S$$$ is the XOR of elements not equal to $$$a_1=1$$$, which are $$$\{2, 2\}$$$. $$$S = 2 \oplus 2 = 0$$$. Replacing $$$a_1$$$ with $$$0$$$ gives $$$[0, 2, 2]$$$. The total XOR is $$$0 \oplus 2 \oplus 2 = 0$$$. This is one sequence of length $$$1$$$ starting with index $$$1$$$.
Leo stood in the damp shadows of the sewer lair, sliding a heavy briefcase toward Jebreel. Inside lay a crisp, royal blue Chelsea jersey and a stack of VIP passes to Stamford Bridge. Jebreel's hand shook as he took it; he was wearing his favorite Liverpool kit, and the thought of covering the Liver bird with Chelsea blue felt like a personal betrayal. To seal the deal, Jebreel pulled the blue shirt on, the Chelsea crest resting directly over his heart.
Jebreel With Chelsea jersey You are given an array $$$a$$$ of $$$n$$$ integers. In one operation, you can choose an index $$$i$$$ ($$$1 \le i \le n$$$) and replace $$$a_i$$$ with the number of times its current value appears in the suffix $$$a_i, a_{i+1}, \dots, a_n$$$.
Find the minimum number of operations required to make all elements in the array equal. If it is impossible to make all elements equal, output $$$-1$$$.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — denotes the number of test cases.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — denotes the length of the array $$$a$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) — denotes the elements of the array.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output a single integer — the minimum number of operations required to make all elements equal. If it is impossible, output $$$-1$$$.
237 7 721 2
01
In the first test case, the array is initially $$$[7, 7, 7]$$$. All elements are already equal, so the number of operations required is $$$0$$$.
In the second test case, the array is initially $$$[1, 2]$$$. If we choose index $$$i=2$$$, $$$a_2$$$ is replaced by the count of $$$2$$$ in the suffix $$$[2]$$$, which is $$$1$$$. The array becomes $$$[1, 1]$$$. The minimum number of operations is $$$1$$$.