text
stringlengths
0
801
The first line contains an integer $t$ ($1\leq t \leq 10^3$) — the number of test cases.
The only line of each test case contains four integers $n$, $m$, $l$, $f$ ($2\leq n\leq m \leq 2\cdot 10^7$, $1\leq l,f \leq 10^9$, $\bf{n=m}$): $n$, $m$ gives the upper bound on the number of Lelle and Flamm's wins, $l$ and $f$ determine the final score of the performance.
Unusual additional constraint: it is guaranteed that, for each test, there are no pairs of test cases with the same pair of $n$, $m$.
For each test case, output a single integer — the maximum total score of a successful performance.
In the first test case, a possible performance is as follows:
* Flamm wins, $\gcd(0,1)=1$. * Lelle wins, $\gcd(1,1)=1$. * Flamm wins, $\gcd(1,2)=1$. * Flamm wins, $\gcd(1,3)=1$. * Lelle wins, $\gcd(2,3)=1$. * Lelle and Flamm agree to stop the match.
The final score is $2\cdot2+3\cdot5=19$.
In the third test case, a possible performance is as follows:
* Flamm wins, $\gcd(0,1)=1$. * Lelle wins, $\gcd(1,1)=1$. * Lelle wins, $\gcd(2,1)=1$. * Lelle wins, $\gcd(3,1)=1$. * Lell
This is the hard version of the problem. In this version, it is not guaranteed that $n=m$, and the time limit is higher. You can make hacks only if both versions of the problem are solved.
In the court of the Blue King, Lelle and Flamm are having a performance match. The match consists of several rounds. In each round, either Lelle or Flamm wins.
Let $W_L$ and $W_F$ denote the number of wins of Lelle and Flamm, respectively. The Blue King considers a match to be successful if and only if:
* after every round, $\gcd(W_L,W_F)\le 1$; * at the end of the match, $W_L\le n, W_F\le m$.
Note that $\gcd(0,x)=\gcd(x,0)=x$ for every non-negative integer $x$.
Lelle and Flamm can decide to stop the match whenever they want, and the final score of the performance is $l \cdot W_L + f \cdot W_F$.
Please help Lelle and Flamm coordinate their wins and losses such that the performance is successful, and the total score of the performance is maximized.
The first line contains an integer $t$ ($1\leq t \leq 10^3$) — the number of test cases.
The only line of each test case contains four integers $n$, $m$, $l$, $f$ ($2\leq n\leq m \leq 2\cdot 10^7$, $1\leq l,f \leq 10^9$): $n$, $m$ give the upper bound on the number of Lelle and Flamm's wins, $l$ and $f$ determine the final score of the performance.
Unusual additional constraint: it is guaranteed that, for each test, there are no pairs of test cases with the same pair of $n$, $m$.
For each test case, output a single integer — the maximum total score of a successful performance.
In the first test case, a possible performance is as follows:
* Flamm wins, $\gcd(0,1)=1$. * Lelle wins, $\gcd(1,1)=1$. * Flamm wins, $\gcd(1,2)=1$. * Flamm wins, $\gcd(1,3)=1$. * Flamm wins, $\gcd(1,4)=1$. * Lelle and Flamm agree to stop the match.
The final score is $1\cdot2+4\cdot5=22$.
Consider a grid graph with $n$ rows and $n$ columns. Let the cell in row $x$ and column $y$ be $(x,y)$. There exists a directed edge from $(x,y)$ to $(x+1,y)$, with non-negative integer value $d_{x,y}$, for all $1\le x < n, 1\le y \le n$, and there also exists a directed edge from $(x,y)$ to $(x,y+1)$, with non-negative integer value $r_{x,y}$, for all $1\le x \le n, 1\le y < n$.
Initially, you are at $(1,1)$, with an empty set $S$. You need to walk along the edges and eventually reach $(n,n)$. Whenever you pass an edge, its value will be inserted into $S$. Please maximize the MEX$^{\text{∗}}$ of $S$ when you reach $(n,n)$.
$^{\text{∗}}$The MEX (minimum excluded) of an array is the smallest non- negative integer that does not belong to the array. For instance:
* The MEX of $[2,2,1]$ is $0$, because $0$ does not belong to the array. * The MEX of $[3,1,0,1]$ is $2$, because $0$ and $1$ belong to the array, but $2$ does not. * The MEX of $[0,3,1,2]$ is $4$, because $0, 1, 2$, and $3$ belong to the array, but $4$ does not.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le100$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le20$) — the number of rows and columns.
Each of the next $n-1$ lines contains $n$ integers separated by single spaces — the matrix $d$ ($0\le d_{x,y}\le 2n-2$).
Each of the next $n$ lines contains $n-1$ integers separated by single spaces — the matrix $r$ ($0\le r_{x,y}\le 2n-2$).
It is guaranteed that the sum of all $n^3$ does not exceed $8000$.
For each test case, print a single integer — the maximum MEX of $S$ when you reach $(n,n)$.
In the first test case, the grid graph and one of the optimal paths are as follows:
![](CDN_BASE_URL/70956fd41a5289db10b3b0bb41d0efae)
In the second test case, the grid graph and one of the optimal paths are as follows:
![](CDN_BASE_URL/c6a0ac2a80551ddd517e35658fa66438)
Turtle gives you a string $s$, consisting of lowercase Latin letters.
Turtle considers a pair of integers $(i, j)$ ($1 \le i < j \le n$) to be a pleasant pair if and only if there exists an integer $k$ such that $i \le k < j$ and both of the following two conditions hold:
* $s_k \ne s_{k + 1}$; * $s_k \ne s_i$ or $s_{k + 1} \ne s_j$.
Besides, Turtle considers a pair of integers $(i, j)$ ($1 \le i < j \le n$) to be a good pair if and only if $s_i = s_j$ or $(i, j)$ is a pleasant pair.
Turtle wants to reorder the string $s$ so that the number of good pairs is maximized. Please help him!
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 length of the string.
The second line of each test case contains a string $s$ of length $n$, consisting of lowercase Latin letters.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output the string $s$ after reordering so that the number of good pairs is maximized. If there are multiple answers, print any of them.
In the first test case, $(1, 3)$ is a good pair in the reordered string. It can be seen that we can't reorder the string so that the number of good pairs is greater than $1$. bac and cab can also be the answer.
In the second test case, $(1, 2)$, $(1, 4)$, $(1, 5)$, $(2, 4)$, $(2, 5)$, $(3, 5)$ are good pairs in the reordered string. efddd can also be the answer.
This is an easy version of this problem. The differences between the versions are the constraint on $m$ and $r_i < l_{i + 1}$ holds for each $i$ from $1$ to $m - 1$ in the easy version. You can make hacks only if both versions of the problem are solved.
Turtle gives you $m$ intervals $[l_1, r_1], [l_2, r_2], \ldots, [l_m, r_m]$. He thinks that a permutation $p$ is interesting if there exists an integer $k_i$ for every interval ($l_i \le k_i < r_i$), and if he lets $a_i = \max\limits_{j = l_i}^{k_i} p_j, b_i = \min\limits_{j = k_i + 1}^{r_i} p_j$ for every integer $i$ from $1$ to $m$, the following condition holds:
$$\max\limits_{i = 1}^m a_i < \min\limits_{i = 1}^m b_i$$