Archaeologist Monocarp is exploring some ancient ruins when the heavy stone exit door suddenly slams shut, trapping him inside.
In the center of the ruins, he discovers an ancient artifact shaped like a tree consisting of $$$n$$$ vertices connected by $$$n - 1$$$ magical branches (undirected edges). Each branch contains a glowing magical stone inscribed with an integer weight $$$w$$$.
Monocarp studies the artifact and deduces the ancient rules for traversing its magical pathways. The cost of a simple path between any vertex $$$i$$$ and vertex $$$j$$$, denoted as $$$c(i, j)$$$, is equal to the bitwise XOR of the weights of all edges on the simple path between $$$i$$$ and $$$j$$$. If $$$i = j$$$, the cost is $$$0$$$.
Furthermore, one can travel between two vertices $$$u$$$ and $$$v$$$ using a sequence of intermediate jumps. The total distance $$$d(u, v)$$$ is defined as the minimum possible value of $$$\sum_{i=1}^{k-1} c(a_i, a_{i+1})$$$ over all possible sequences of vertices $$$a_1, a_2, \ldots, a_k$$$ of any length $$$k \ge 1$$$ such that $$$a_1 = u$$$ and $$$a_k = v$$$.
As Monocarp observes the artifact, he realizes the ruins are highly unstable. Occasionally, the magical stones fluctuate and the entire tree changes: a magical surge of value $$$x$$$ occurs, updating the weight of every edge $$$j$$$ to $$$w_j \oplus x$$$ (where $$$\oplus$$$ denotes the bitwise XOR operation).
Desperate to leave, Monocarp finds another ruin near the locked door bearing a cryptic scripture. The scripture indicates that the door will only open if he can correctly answer a series of questions based on the artifact's current state. For a given source vertex $$$s$$$, he must compute the sum of the minimum distances from $$$s$$$ to all vertices $$$i$$$ in the tree, i.e., $$$\sum_{i=1}^n d(s, i)$$$.
You are given $$$q$$$ events (either a magical surge or a query from the scripture). Help Monocarp compute the answers for the scripture to open the door and escape!
The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n \le 10^5$$$, $$$1 \le q \le 2 \cdot 10^5$$$) — the number of vertices in the tree artifact and the number of events, respectively.
Each of the next $$$n - 1$$$ lines contains three integers $$$u$$$, $$$v$$$, and $$$w$$$ ($$$1 \le u, v \le n$$$; $$$u \neq v$$$; $$$0 \le w \lt 2^{30}$$$), denoting an edge between vertices $$$u$$$ and $$$v$$$ with an initial weight of $$$w$$$. It is guaranteed that the given edges form a valid tree.
Each of the next $$$q$$$ lines contains a query of one of the following two types:
For each query of the second type, print a single integer on a new line — the sum of the minimum distances $$$d(s, i)$$$ for all $$$1 \le i \le n$$$.
4 31 2 31 3 42 4 12 11 22 2
9 11
Explanation for the sample test case:
Initially, the artifact has the following magical branches (edges):
For the first query (2 1), the scripture asks for the sum of distances from source vertex $$$s = 1$$$:
In the second query (1 2), a magical surge of $$$x = 2$$$ occurs. All edge weights are updated to $$$w_j \oplus 2$$$:
For the third query (2 2), the scripture asks for the sum of distances from source vertex $$$s = 2$$$ on the updated tree:
Bog the Frog lives in a peaceful pond containing $$$n$$$ lily pads, uniquely numbered from $$$1$$$ to $$$n$$$. He wants to plan a journey to visit every single lily pad exactly once.
Bog can begin his journey from any lily pad. However, Bog has strict jumping rules: he can only leap from his current lily pad to the next one if the absolute difference between their numbers is a prime number.
Because Bog likes to keep his travel logs neatly organized, he wants his path to be the lexicographically smallest$$$^{\text{∗}}$$$ sequence possible. Can you help Bog find the perfect sequence of lily pads to jump to, or tell him if such a journey is impossible?
$$$^{\text{∗}}$$$An array $$$a$$$ is lexicographically smaller than array $$$b$$$ if there exists an index $$$i$$$ such that $$$a_j=b_j$$$ for all indices $$$1≤j \lt i$$$ and $$$a_i \lt b_i$$$.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases.
Each test case consists of a single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the number of lily pads in the pond.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 \cdot 10^5$$$.
For each test case, print $$$n$$$ space-separated integers — the lexicographically smallest valid jumping sequence. If no valid sequence exists, output $$$-1$$$.
3 1 2 4
1 -1 2 4 1 3
In the third test case, the jump sequence $$$(2,4,1,3)$$$ follows prime differences:
Alice and Bob are given an array $$$a$$$ consisting of $$$n$$$ integers. They decided to play a game on it.
The players take turns making moves, with Alice moving first. In each move, the player must choose two indices $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$) such that all of the following conditions are satisfied:
After that, the segment $$$[a_l, a_{l+1}, \ldots, a_r]$$$ is deleted from the array $$$a$$$. The player who cannot make a move loses.
Furthermore, Alice has the ability to arbitrarily rearrange the array before starting the game. Determine who wins, if Alice can rearrange the array before making the first move, and assuming that both players play optimally.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$).
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$).
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 line containing the winner of the game assuming both players play optimally.
Output Alice if Alice wins the game, otherwise output Bob.
351 2 1 2 321 299 9 8 2 4 4 3 5 3
AliceBobAlice
For the first testcase, Alice can rearrange the array to $$$[1, 1, 2, 2, 3]$$$.
Kusuo had been trapped inside a cage by Kusuke. To free himself, he needs to solve Kusuke's problem and to do it, he requires your help—
…wait, never mind. He already freed himself. Looks like he didn't need your help after all. Anyway, here's the problem if you still want to give it a try.
Let $$$n$$$ be an even integer. You are given a permutation $$$ p = [p_1, p_2, \dots, p_n] $$$ of the integers from $$$0$$$ to $$$n-1$$$.
Define an $$$n \times n$$$ matrix $$$a = [a_{i,j}]$$$ as $$$$$$ a_{i,j} = \begin{cases} \mathrm{mex}(p[i..j]), & i \le j, \\ a_{j,i}, & i \gt j, \end{cases} $$$$$$ — where $$$\mathrm{mex}(p[i..j])$$$$$$^{\text{∗}}$$$ is the minimum excluded element of the subarray $$$[p_i, p_{i+1}, \dots, p_j]$$$.
The score of the permutation $$$p$$$ is defined as $$$ f(p) = \det(a) $$$ (the determinant of $$$a$$$)$$$^{\text{†}}$$$.
To make the problem harder, Kusuke added $$$q$$$ queries. In each query, two indices $$$x$$$ and $$$y$$$ are given, and you swap $$$p_x$$$ and $$$p_y$$$.
The queries are persistent, meaning each swap permanently modifies the permutation and affects all subsequent queries.
After each query, output the score of the current permutation.
$$$^{\text{∗}}$$$$$$\mathrm{mex}$$$ (minimum excluded value) of a set of integers is the smallest non-negative integer that does not appear in the set.
$$$^{\text{†}}$$$The determinant is a value associated with a square matrix.
The first line contains a single integer $$$t$$$ — the number of test cases $$$(1 \le t \le 10^4)$$$.
The first line of each test case contains two integers $$$n$$$ and $$$q$$$ $$$(4 \le n \le 10^6,\; 1 \le q \le 10^6,\; n \text{ is even})$$$ — the size of the permutation and the number of queries.
The second line contains $$$n$$$ integers $$$ p_1, p_2, \dots, p_n $$$ — a permutation of the integers from $$$1$$$ to $$$n$$$.
Each of the next $$$q$$$ lines contains two integers $$$x$$$ and $$$y$$$ $$$(1 \le x, y \le n)$$$ — the indices whose values should be swapped.
It is guaranteed that the sum of $$$n$$$ and $$$q$$$ over all test cases does not exceed $$$10^6$$$.
For each test case, output $$$q$$$ lines.
After each query, print a single integer — the score of the current permutation.
1 4 2 1 0 2 3 1 4 1 2
1 0
Initially, $$$ p = [1,0,2,3]. $$$
Query 1: swap $$$p_1$$$ and $$$p_4$$$.
The permutation becomes $$$ p = [3,0,2,1]. $$$
The matrix $$$a$$$ becomes $$$$$$ a = \begin{bmatrix} 0 & 1 & 1 & 4 \\ 1 & 1 & 1 & 3 \\ 1 & 1 & 0 & 0 \\ 4 & 3 & 0 & 0 \end{bmatrix}. $$$$$$
The determinant of $$$a$$$ is $$$ \det(a)=1. $$$
Query 2: swap $$$p_1$$$ and $$$p_2$$$.
The permutation becomes $$$ p = [0,3,2,1]. $$$
The matrix $$$a$$$ becomes $$$$$$ a = \begin{bmatrix} 1 & 1 & 1 & 4 \\ 1 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 \\ 4 & 0 & 0 & 0 \end{bmatrix}. $$$$$$
The determinant of $$$a$$$ is $$$ \det(a)=0. $$$
In the ancient kingdom of Insomnia, the Royal Mathematician has discovered a strange and powerful sequence known as the Resonating Array. It is said that such an array holds the key to unlocking the kingdom's greatest secrets — but only if it is constructed correctly. The rule is simple yet mysterious.
For every pair of consecutive elements in the array, the remainder when the first element is divided by the second must be exactly $$$1$$$.
Formally, if the array is $$$a_1, a_2, \dots, a_n$$$, then for every $$$1 \le i \lt n$$$, we must have $$$a_i \bmod a_{i+1} = 1$$$.
The king has requested the lexicographically smallest$$$^{\text{∗}}$$$ such array of length $$$n$$$, using positive integers.
Can you help the Royal Mathematician find it before the kingdom falls into chaos?
$$$^{\text{∗}}$$$An array $$$a$$$ is lexicographically smaller than array $$$b$$$ if there exists an index $$$i$$$ such that $$$a_j=b_j$$$ for all indices $$$1≤j \lt i$$$ and $$$a_i \lt b_i$$$.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases.
Each test case consists of a single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the length of the array.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 \cdot 10^5$$$.
For each test case, print $$$n$$$ positive integers $$$a_1, a_2, \dots, a_n$$$ — the lexicographically smallest array such that for every $$$1 \le i \lt n$$$: $$$a_i \bmod a_{i+1} = 1$$$.
2 1 2
1 1 2
There are $$$n$$$ players in the academy, numbered from $$$1$$$ to $$$n$$$. To practice quick decision-making, they introduce a drill with the following rules:
Your task is to determine the number of possible passing arrangements such that exactly one player is forgotten.
Since the answer may be large, print it modulo $$$998\,244\,353$$$.
The first line contains one positive integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
The first line of each testcase contains a single integer $$$n$$$ ($$$2 \le n \le 10^6$$$).
For each test case, output the number of valid passing arrangements modulo $$$998\,244\,353$$$.
3234
0648
For $$$n = 2$$$, no valid passing arrangements exist.
For $$$n = 3$$$, all valid passing arrangements where exactly one player receives no ball are as follows:
Thus, we have a total of $$$6$$$ possible ways.
Alice and Bob are playing a game on an array of integers.
They are given an array $$$a$$$ of length $$$n$$$. The players take turns making moves, with Alice moving first.
In a move, the player must select a non-empty subsequence$$$^{\text{∗}}$$$ of the array such that there exists at least one element in the subsequence which is not equal to the $$$\gcd$$$ of all elements in the subsequence.
Let $$$\text{g}$$$ denote the gcd of the selected subsequence. After selecting the subsequence, the player must replace all the elements in the chosen subsequence with the value $$$\text{g}$$$.
More formally, suppose the chosen subsequence corresponds to the indices $$$i_1, i_2, \ldots, i_k$$$. Let $$$$$$ \text{g} = \gcd(a_{i_1}, a_{i_2}, \ldots, a_{i_k}) $$$$$$ The subsequence is valid only if and only if $$$$$$ \exists j \in \{1,2,\ldots,k\} \text{ such that } a_{i_j} \ne \text{g} $$$$$$ After choosing a valid subsequence, set $$$a_{i_j} := \text{g}$$$ for all $$$1 \le j \le k$$$.
The players continue alternating moves on the updated array.
The first player who cannot make a move $$$\color{red}{\text{wins}}$$$.
Assuming both players play optimally, determine who will win the game.
$$$^{\text{∗}}$$$A subsequence is a sequence obtained by deleting zero or more elements from the array without changing the relative order of the remaining elements.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$).
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^{18}$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.
For each test case, output a single line containing the winner of the game assuming both players play optimally.
Output Alice if Alice wins the game, otherwise output Bob.
3 4 1 1 1 1 2 3 4 3 35 42 30
Alice Bob Bob
In the first test case, all elements of the array are equal. For any chosen subsequence, the $$$\gcd$$$ of the subsequence is equal to every element in it, so the condition for a valid move is not satisfied. Hence, no legal move exists for the starting array, and Alice wins the game.
In the second test case, the only valid move for Alice is to select both elements of the array. The $$$\gcd$$$ of the selected subsequence is $$$\gcd(3,4)=1$$$, and since neither of the elements is equal to $$$1$$$, the move is valid. After the move, both elements become $$$1$$$, so the array becomes $$$[1,1]$$$. Now, Bob has no valid move, so Bob wins the game.
In the third test case, it can be shown that Bob wins the game assuming both players play optimally.
You are given a weighted undirected graph with $$$n$$$ nodes and $$$m$$$ edges. Each edge has a weight $$$w$$$, representing the time required to travel through that edge.
You start at node $$$a$$$ and want to reach node $$$b$$$.
Some nodes contain shelters. If you arrive at a shelter, you may stop and rest there. Resting resets your continuous travel time counter to zero, and resting does not add to the travel time.
It is raining, and you cannot travel continuously under the rain for more than $$$k$$$ units of time.
Formally, along any path from $$$a$$$ to $$$b$$$, the sum of edge weights between two consecutive shelter nodes must not exceed $$$k$$$. The same restriction also applies between the start node $$$a$$$ and the first shelter visited, and between the last shelter visited and the destination node $$$b$$$.
Determine whether it is possible to reach node $$$b$$$ from node $$$a$$$ while respecting this constraint.
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
For each test case:
The first line contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le n \le 2\cdot10^5$$$, $$$0 \le m \le 2\cdot10^5$$$, $$$0 \le k \le 10^9$$$) — the number of nodes, the number of edges, and the maximum allowed continuous travel time under the rain.
The second line contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a,b \le n$$$) — the starting node and the destination node.
The third line contains an integer $$$s$$$ ($$$0 \le s \le n$$$) — the number of nodes that contain shelters.
The fourth line contains $$$s$$$ integers $$$s_1, s_2, \ldots, s_s$$$ ($$$1 \le s_i \le n$$$) — the nodes that contain shelters.
Each of the next $$$m$$$ lines contains three integers $$$u_i$$$, $$$v_i$$$, and $$$w_i$$$ ($$$1 \le u_i,v_i \le n$$$, $$$1 \le w_i \le 10^9$$$) describing an undirected edge between nodes $$$u_i$$$ and $$$v_i$$$ with travel time $$$w_i$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot10^5$$$, and the sum of $$$m$$$ over all test cases does not exceed $$$2\cdot10^5$$$.
For each test case, print YES if it is possible to reach node $$$b$$$ from node $$$a$$$ while respecting the rain constraint. Otherwise, print NO.
53 2 61 3121 2 32 3 33 2 41 3121 2 32 3 32 1 101 201 2 74 3 41 4131 2 32 3 33 4 36 5 61 623 51 2 32 3 33 4 34 5 35 6 3
YES YES YES NO YES
In the second test case, the path $$$1 \to 2 \to 3$$$ is valid. Although the total length of the path is $$$6$$$, the traveler stops at node $$$2$$$, which is a shelter. This resets the rain timer before continuing the journey.
In the fourth test case, although node $$$3$$$ contains a shelter, it cannot be reached from node $$$1$$$ without exceeding the limit $$$k$$$. Therefore, it is impossible to reach the destination.
In the fifth test case, the traveler first goes $$$1 \to 2 \to 3$$$ (total time $$$6$$$) and rests at shelter $$$3$$$. Then they continue $$$3 \to 4 \to 5$$$ (total time $$$6$$$) and rest again at shelter $$$5$$$. Finally, they travel $$$5 \to 6$$$ (time $$$3$$$), successfully reaching the destination.
You are given two integer sequences: $$$[a_1, a_2, \ldots , a_{n-1}]$$$ and $$$[c_1, c_2, \ldots , c_n]$$$.
You build a random rooted tree $$$T_n$$$ with vertex set $$${1, 2, \ldots , n}$$$ as follows:
Let $$$$$$F = \sum_{i \in S}{c_i}$$$$$$
where $$$S$$$ is the set of leaf vertices$$$^{\text{∗}}$$$ in $$$T_n$$$.
Find the expected value of $$$F$$$.
$$$^{\text{∗}}$$$A vertex is a leaf if it is not the root and its degree in $$$T_n$$$ equals to $$$1$$$.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.
Each test case is described as follows.
The first line contains an integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the number of vertices in the tree $$$T_n$$$.
The second line contains $$$n-1$$$ integers $$$a_1, a_2, \ldots, a_{n-1}$$$ ($$$1 \le a_i \lt 998\,244\,353$$$).
For every test case, and for every $$$k$$$ with $$$1 \le k \le n-1$$$, the prefix sum $$$$$$ a_1 + a_2 + \dots + a_k \not\equiv 0 \pmod{998\,244\,353}. $$$$$$
The third line contains $$$n$$$ integers $$$c_1, c_2, \ldots, c_n$$$ ($$$1 \le c_i \le 10^9$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, print a single integer — the expected value of $$$$$$ F = \sum_{i \in S} c_i, $$$$$$ where $$$S$$$ is the set of leaf vertices.
Since the expected value can be a fraction, output it modulo $$$998\,244\,353$$$.
4 3 5 1 2 1 6 2 5 1 8 8 10 7 4 1 10 1 2 9 6 4 6 5 8 3 5 2 5 9 5
831870301 8 334205461 5
In the first test case, vertex $$$2$$$ will have vertex $$$1$$$ as its parent. There is a $$$\frac{a_2}{a_1+a_2}$$$ = $$$\frac{1}{5+1}$$$ probability that the parent of vertex $$$3$$$ is vertex $$$2$$$, and a $$$\frac{a_1}{a_1+a_2}$$$ = $$$\frac{5}{5+1}$$$ probability that the parent is vertex $$$1$$$.
Hence, the expected value of $$$F$$$ is $$$$$$ \frac{1}{6} \cdot c_3 + \frac{5}{6} \cdot (c_2 + c_3) = \frac{1}{6} \cdot 6 + \frac{5}{6} \cdot (1 + 6) = \frac{41}{6}. $$$$$$
Jasper is a world-renowned jeweler who has just acquired $$$n$$$ rare gemstones. Each gemstone has a certain luster value $$$a_i$$$. To showcase them in his gallery, Jasper needs to arrange these stones into exactly $$$k$$$ non-empty display cases.
Jasper is a perfectionist. For any display case $$$s$$$ containing $$$m$$$ stones, he defines the Visual Strain $$$f(s)$$$ as the sum of the differences between the maximum and minimum luster seen so far as a visitor walks past the case from left to right. Formally, if the stones are placed in the order $$$s_1, s_2, \dots, s_m$$$, the strain is:
$$$$$$ f(s) = \sum_{i=1}^{m} \left( \max_{1 \le j \le i} \{s_j\} - \min_{1 \le j \le i} \{s_j\} \right) $$$$$$
Jasper knows the order within a case matters. For each case, he will expertly rearrange the stones he is given to ensure the Visual Strain is as small as possible. Let $$$g(s)$$$ be the minimum possible value of $$$f(s)$$$ across all $$$m!$$$ permutations of the stones in $$$s$$$.
Your task is to partition the $$$n$$$ gemstones into exactly $$$k$$$ disjoint non-empty subsequences $$$s_1, s_2, \dots, s_k$$$ such that every gemstone belongs to exactly one subsequence, and the total Visual Strain $$$\sum_{i=1}^{k} g(s_i)$$$ is minimized.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases.
Each test case consists of two lines: The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 5000$$$) — the number of gemstones and the number of display cases. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the luster values of the gemstones.
It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$2.5 \cdot 10^7$$$.
For each test case, print a single integer representing the minimum possible total Visual Strain.
35 21 10 5 8 33 11 5 1011 14 4 4 4 4 6 6 3 3 3 3
81310
In the first test case, an optimal partition is $$$s_1 = \{1, 3, 5\}$$$ and $$$s_2 = \{8, 10\}$$$.
In the second test case, all stones must be in one case $$$\{1, 5, 10\}$$$. The optimal permutation is $$$(5, 1, 10)$$$, yielding $$$(5-5) + (5-1) + (10-1) = 0 + 4 + 9 = 13$$$.
In the third test case, there is only one case ($$$k=1$$$) containing five $$$4$$$s, two $$$6$$$s, and four $$$3$$$s. An optimal permutation is $$$(4, 4, 4, 4, 4, 3, 3, 3, 3, 6, 6)$$$.
Alice is tracking her mood swings for the next $$$n$$$ days. Each day her mood will randomly be either bad ($$$0$$$) or good ($$$1$$$), independently with equal probability.
After $$$n$$$ days, Alice looks back at the sequence of moods and wants to remember a period where her mood only improves or stays the same. To do this, she selects a subsequence$$$^{\text{∗}}$$$ of days such that her mood never gets worse as time goes forward. In other words, once she chooses a day with mood $$$1$$$, she cannot later choose a day with mood $$$0$$$.
Among all such subsequences, Alice always chooses one with the maximum possible length. Let $$$f(S)$$$ denote this length for a mood sequence $$$S$$$.
Since the moods are generated randomly, Alice wonders what value to expect.
Formally, consider all binary strings $$$S$$$ of length $$$n$$$, each occurring with probability $$$\frac{1}{2^n}$$$. Compute the expected value of $$$f(S)$$$.
$$$^{\text{∗}}$$$A subsequence of a sequence is obtained by deleting zero or more elements without changing the order of the remaining elements.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.
The first and only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^6$$$, $$$10^8 \le m \le 10^9$$$).
It is guaranteed that $$$m$$$ is prime.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.
For each test case, output the expected value of $$$f(S)$$$ modulo $$$m$$$.
Formally, let the expected value be written 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 such an integer $$$x$$$ that $$$0 \le x \lt m$$$ and $$$x \cdot q \equiv p \pmod m$$$.
3 1 100000007 2 100000037 4 100000039
1 25000011 68750030
In the first test case, $$$n = 1$$$. The possible mood sequences are:
In the second test case, $$$n = 2$$$. The possible mood sequences are:
In the third test case, $$$n = 4$$$. The expected value of $$$f(S)$$$ over all $$$2^4$$$ binary strings equals $$$\frac{51}{16}$$$.
The ancient Kingdom of Lumina consists of $$$n$$$ cities connected by $$$m$$$ bidirectional magical leylines. Each leyline possesses a base resonance, represented by an integer $$$w$$$. This resonance can be positive (harmonious), negative (dissonant), or even zero (inert).
The High Mage wishes to establish a grand unified network—a subset of exactly $$$n - 1$$$ leylines that connects all $$$n$$$ cities together so that magic can flow freely between any two cities.
The overall instability of a chosen network is defined as the product of the resonances of its constituent leylines. To prevent a catastrophic magical surge, the High Mage must select a network that minimizes this instability.
Because the leylines are powerful, the minimum instability can be an exceptionally large positive or negative number. You are tasked with finding the minimum possible instability and reporting it modulo $$$10^9 + 7$$$.
Note on Modulo Arithmetic: The answer must be the mathematically correct modulo. If the true minimum product is $$$P$$$, you should output $$$(P \pmod{10^9 + 7} + 10^9 + 7) \pmod{10^9 + 7}$$$. You are minimizing the actual integer product first, and then applying the modulo to that minimum value.
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 two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 2 \cdot 10^5$$$, $$$n - 1 \le m \le 2 \cdot 10^5$$$) — the number of cities and the number of leylines, respectively.
Each of the following $$$m$$$ lines contains three integers $$$u$$$, $$$v$$$, and $$$w$$$ ($$$1 \le u, v \le n$$$, $$$u \neq v$$$, $$$-10^9 \le w \le 10^9$$$) — indicating a bidirectional leyline connecting city $$$u$$$ and city $$$v$$$ with a magical resonance of $$$w$$$.
It is guaranteed that for each test case, the given graph is simple (contains no self-loops and no multiple edges between the same pair of cities) and that it is always possible to connect all cities (the graph is connected).
It is also guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$2 \cdot 10^5$$$.
For each test case, print a single integer on a new line — the minimum possible product of the edge weights of a spanning tree, modulo $$$10^9 + 7$$$.
64 51 2 22 3 -33 4 -44 1 51 3 13 31 2 -22 3 53 1 -14 41 2 22 3 33 4 41 4 54 41 2 -22 3 -33 4 -41 4 -55 51 2 -12 3 -23 4 -34 5 -45 1 -53 31 2 10000000002 3 10000000001 3 0
999999967 999999997 24 999999947 24 0
You are given two integers $$$n$$$ and $$$m$$$.
Find the number of ordered pairs $$$(a, b)$$$ that satisfy:
Note that $$$|$$$ denotes the bitwise OR operator.
The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. Description of each testcase follows.
Each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^6 $$$).
It is guaranteed that the sum of $$$n$$$ over all testcases does not exceed $$$10^6$$$ and the sum of $$$m$$$ over all testcases does not exceed $$$10^6$$$.
For each testcase, print the answer on a separate line.
56 720 20998 244353 4201000 1000
9625475933850200
Anup was working tirelessly to finalize the problem set for the upcoming Insomnia contest. Meanwhile, Darsh, having absolutely nothing better to do, wandered over to demand a treat(chapo in local lingo). Hoping to get back to work, Anup struck a deal: Darsh would get his chapo, but only if he could first solve a seemingly innocent, yet "not-so-simple" problem.
Anup describes a process to build a tree:
Given an integer $$$n$$$, construct an array $$$a[a_1,a_2,\ldots,a_n]$$$ of size $$$n$$$ consisting of zeroes and ones where $$$a_1= 0 $$$. Consider a tree with $$$n+1$$$ vertices labeled $$$0, 1, 2, \ldots, n$$$, where vertex $$$0$$$ is designated as the root.
For each $$$i = 1, 2, 3, \ldots, n$$$, if:
Let $$$h(a)$$$ be the height of the tree for one possible valid array $$$a$$$. Anup asks Darsh to find the sum of $$$h(a)$$$ over all possible valid arrays of $$$0$$$s and $$$1$$$s where $$$a_1 = 0$$$. Since this sum can be incredibly large, output the answer modulo $$$10^9 + 7$$$.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
The only line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array.
For each test case, output a single integer — the sum of the heights of the trees formed by all valid arrays, modulo $$$10^9 + 7$$$.
2 1 2
1 3
You are given an array $$$a$$$ of length $$$n$$$ consisting of positive integers.
An index $$$i$$$ $$$(1 \le i \lt n)$$$ splits the array into two non empty subarrays: $$$[a_1, a_2, \dots, a_i]$$$ and $$$[a_{i+1}, a_{i+2}, \dots, a_n]$$$.
We call an index $$$i$$$ good if it is possible to make
$$$$$$ \gcd(a_1, a_2, \dots, a_i) = \gcd(a_{i+1}, a_{i+2}, \dots, a_n) $$$$$$
by changing the value of at most one element in the array to any positive integer.
Note that the modification (if any) can be applied to any position in the array and is considered independently for each index.
Your task is to determine the number of good indices.
The first line contains a single integer $$$t$$$ $$$(1 \le t \le 10^3)$$$ — the number of test cases.
Each test case consists of two lines.
The first line contains a single integer $$$n$$$ $$$(2 \le n \le 2 \cdot 10^5)$$$ — the length of the array.
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ $$$(1 \le a_i \le 10^9)$$$.
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 number of good indices.
2612 6 18 3 9 1584 8 16 2 10 5 20 25
5 5
For the second test case,
| $$$i$$$ | Left Array | Right Array | GCD (Left, Right) | Good? |
| 1 | [1] | [8,16,2,10,5,20,25] | (1, 1) | Yes |
| 2 | [1,8] | [16,2,10,5,20,25] | (1, 1) | Yes |
| 3 | [1,8,16] | [2,10,5,20,25] | (1, 1) | Yes |
| 4 | [4,8,16,2] | [10,5,20,25] | (2, 5) | No |
| 5 | [4,8,16,2,10] | [5,20,25] | (2, 5) | No |
| 6 | [4,8,16,2,10,5] | [1,25] | (1, 1) | Yes |
| 7 | [4,8,16,2,10,5,20] | [1] | (1, 1) | Yes |
The bold entries in the table represent the numbers that were modified from their original values.
Thus, indices $$${1, 2, 3, 6, 7}$$$ are good.