You are given a tree $$$T$$$ consisting of $$$n$$$ vertices labeled from $$$1$$$ to $$$n$$$, rooted at $$$1$$$. A pair of vertices $$$(u,v)$$$ is called valid if and only if $$$u \ne v$$$, $$$u \ne 1$$$, and $$$u$$$ is not an ancestor$$$^{\text{∗}}$$$ of $$$v$$$.
For a valid pair $$$(u,v)$$$, define $$$f(u,v)$$$ as follows:
Find the sum of $$$f(u,v)$$$ over all valid pairs $$$(u,v)$$$.
$$$^{\text{∗}}$$$An ancestor of vertex $$$v$$$ is any vertex on the simple path from $$$v$$$ to the root, including the root, but not including $$$v$$$. The root has no ancestors.
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 an integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$), representing the number of vertices in $$$T$$$.
The $$$i$$$-th of the next $$$n-1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i,v_i \le n$$$, $$$u_i \ne v_i$$$), representing an edge in $$$T$$$. It is guaranteed that the edges form a valid tree.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
For each test case, output an integer representing the sum of $$$f(u,v)$$$ over all valid pairs $$$(u,v)$$$.
521 231 22 331 21 361 22 33 42 55 681 22 32 44 55 61 77 8
15665163
You are given a directed graph $$$G$$$ consisting of $$$n$$$ vertices labeled from $$$1$$$ to $$$n$$$. An odd cycle is defined as a sequence of vertices (not necessarily distinct) $$$p_0,p_1,\ldots,p_m$$$, such that $$$m$$$ is odd, and there exists a directed edge from vertex $$$p_i$$$ to $$$p_{i+1}$$$ for every $$$0 \le i \lt m$$$.
For each vertex $$$u$$$, determine whether $$$u$$$ is contained in at least one odd cycle.
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 two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le m \le 2 \cdot 10^5$$$), representing the number of vertices and the number of edges in $$$G$$$, respectively.
Each of the next $$$m$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n$$$), representing a directed edge from $$$u$$$ to $$$v$$$. $$$G$$$ may contain self loops and multiple edges.
It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases each do not exceed $$$2 \cdot 10^5$$$.
For each test case, output a line containing a binary string $$$s$$$ of length $$$n$$$. For every $$$1 \le i \le n$$$, if vertex $$$i$$$ is contained in at least one odd cycle, then $$$s_i=$$$ 1; otherwise, $$$s_i=$$$ 0.
71 03 31 22 33 12 21 12 24 41 22 33 44 12 31 22 22 18 81 22 33 14 55 45 66 57 79 141 44 22 55 33 66 11 27 88 78 99 82 44 66 2
01111100001111100010111111000
Xue Ba Ti! Shu Zheng Fang Ti!
There are some cubes in the $$$3$$$-dimensional space. Each cube has a side length of $$$1$$$. The sides of the cubes are parallel to the $$$x$$$, $$$y$$$, and $$$z$$$ axes. A cube is said to be located at coordinates $$$(x,y,z)$$$ if and only if its $$$8$$$ corners are at:
respectively.
All cubes are located at non-negative integer coordinates in space and obey the laws of gravity. Formally, if there is a cube located at $$$(x,y,z)$$$, then either $$$z=0$$$ or there is a cube located at $$$(x,y,z-1)$$$.
An $$$x$$$-view of a $$$3$$$-dimensional structure is the $$$2$$$-dimensional shape observed if you align your sight in the positive $$$x$$$ direction. The $$$y$$$-view is defined similarly.
You don't know the exact locations of the cubes, but fortunately, you have the $$$x$$$-view and $$$y$$$ view of the structure. You also know that the locations of all cubes satisfy: $$$x \le a, y \le b, z \le c$$$. You want to find: whats the minimum and maximum number of cubes possibly present in the structure for these views to be valid? It is also possible that you have poor eyesight and there doesn't exist any valid structure, or somehow the laws of gravity are disobeyed. In any of these cases, output $$$-1$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2\cdot 10^4$$$). The description of the test cases follows.
The first line of each test case contains three integers $$$a$$$, $$$b$$$, and $$$c$$$ ($$$1 \le a,b,c \le 10^3$$$). The size of the $$$x$$$-view will be $$$c \times b$$$, and the size of the $$$y$$$-view will be $$$c \times a$$$.
The $$$i$$$-th of the next $$$c$$$ lines contains a binary string $$$s_i$$$ of length $$$b$$$. If $$$s_j=1$$$, then the $$$x$$$-view contains a cell at $$$y=j-1$$$ and $$$z=c-i$$$.
The $$$i$$$-th of the next $$$c$$$ lines contains a binary string $$$s_i$$$ of length $$$a$$$. If $$$s_j=1$$$, then the $$$y$$$-view contains a cell at $$$x=j-1$$$ and $$$z=c-i$$$.
It is guaranteed that the sum of $$$(a+b)\cdot c$$$ over all test cases does not exceed $$$2 \cdot 10^6$$$.
For each test case, if there doesn't exist any valid structure, output $$$-1$$$. Otherwise, output two integers $$$l$$$ and $$$r$$$, corresponding to the minimum and maximum number of cubes, respectively.
43 3 30100101111001101112 2 2110000115 4 31111111111111111111111111112 2 200000000
7 12-115 600 0
The structures of the first sample test case are given below (Source: HDZ's MineCraft):
Given two non-negative integers $$$n$$$ and $$$m$$$, find the number of pairs of integers $$$(x,y)$$$ such that:
where $$$\&$$$ represents the bitwise AND operation, and $$$\oplus$$$ represents the bitwise XOR operation.
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 only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$0 \le n,m \le 10^9$$$).
For each test case, output an integer representing the number of pairs of integers.
50 01 13 769 96114514 191981
1311166306496
In this problem, you need to write a program to solve a problem in a C-like language called MiniC.
You are given an implementation of an interpreter of MiniC, written in C++, in the contest materials section. You are also given a PDF file containing technical details about this language. You may explore freely using the provided source code.
Write a program in MiniC to solve the following problem:
Your MiniC program can use at most $$$256$$$ megabytes of memory and should take at most $$$1$$$ second to execute. Note that if your MiniC program gets a Compile Error, Runtime Error, Time Limit Exceeded, or Memory Limit Exceeded, your submission will get the Wrong Answer verdict.
Output the program you wrote. Your program size should not exceed $$$5000$$$ characters.
50 3 1 4 2
5 2 5 4 5
Note that the sample input is what your MiniC program will receive, and the sample output is what your MiniC program should output. Your submitted code, in whatever language, should print the source code of your MiniC program.
There is an infinitely large $$$2$$$-dimensional grid. Initially, you are at $$$(0,0)$$$, and there is a set $$$S=\{(0,0)\}$$$. You will walk exactly $$$n$$$ steps on the grid. In each step:
Compute the expected size of $$$S$$$ after $$$n$$$ steps.
Formally, let $$$M = 998\,244\,353$$$. It can be shown that the exact answer can be expressed 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}$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 2 \cdot 10^5$$$). The description of the test cases follows.
The only line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$).
Note that there is no restriction on the sum of $$$n$$$ over all test cases.
For each test case, output the expected size of $$$S$$$.
4123114514
2249561091499122180340469626
This is a classical game of hero versus monsters.
A monster's attributes consist of base attributes and enhancement points.
The base attributes are:
The monster has $$$n$$$ enhancement points that can be freely distributed among the four attributes $$$a,d,h,s$$$. Each enhancement point increases exactly one attribute by $$$1$$$, and the total number of enhancement points assigned must be exactly $$$n$$$.
The hero has fixed attributes:
The battle proceeds in rounds. In each round, both sides attack each other once simultaneously.
The damage dealt from one side to the other is given by $$$$$$ \text{damage} = \max\left( \left\lceil \frac{\text{own speed}}{\text{opponent speed}} \right\rceil \times \text{own attack} - \text{opponent defense}, \; 1 \right). $$$$$$
The battle continues until the health of at least one side becomes less than or equal to $$$0$$$.
You are given $$$q$$$ independent queries. For each query, the hero's attributes $$$A,D,H,S$$$ are given.
For each query, consider all possible monsters, i.e. all possible ways to distribute the $$$n$$$ enhancement points among the monster's base attributes $$$a,d,h,s$$$. The hero will fight each of these monsters. Each of the battles is independent.
For each query, let the number of battles the hero wins and loses be $$$w$$$ and $$$l$$$, respectively. Compute $$$w-l$$$.
The first line of each test contains $$$5$$$ integers $$$a$$$, $$$d$$$, $$$h$$$, $$$s$$$, and $$$n$$$ ($$$1 \le a,h,s \le 200$$$, $$$0 \le d,n \le 200$$$), representing the base attributes of the monster and the number of enhancement points.
The following line contains an integer $$$q$$$ ($$$1 \le q \le 10^5$$$), representing the number of queries.
Each of the next $$$q$$$ lines contains $$$4$$$ integers $$$A$$$, $$$D$$$, $$$H$$$, and $$$S$$$ ($$$1 \le A,H,S \le 200$$$, $$$0 \le D \le 200$$$), representing the attributes of the hero.
For each query, output $$$w-l$$$.
1 1 1 1 232 2 2 21 1 1 13 3 3 3
9 -4 10
10 2 9 5 1051 1 1 110 12 8 1019 34 78 110 10 10 10100 100 100 100
-286 215 219 214 286
In the first sample test, the monsters that the hero is going to fight are $$$(3,1,1,1)$$$, $$$(1,3,1,1)$$$, $$$(1,1,3,1)$$$, $$$(1,1,1,3)$$$, $$$(2,2,1,1)$$$, $$$(2,1,2,1)$$$, $$$(2,1,1,2)$$$, $$$(1,2,2,1)$$$, $$$(1,2,1,2)$$$, $$$(1,1,2,2)$$$.
Let $$$\Sigma$$$ be an alphabet, which in this problem is the set of lowercase English letters. Let $$$\Sigma^+$$$ denote the set of all non-empty strings over $$$\Sigma$$$. For a string $$$t$$$, let $$$|t|$$$ denote its length, and let $$$t[i..j]$$$ denote the substring of $$$t$$$ starting at position $$$i$$$ and ending at position $$$j$$$.
You are given a string $$$s \in \Sigma^+$$$. For a string $$$p$$$, define $$$\operatorname{occ}_s(p)=\{i \mid s[i..i+|p|-1]=p\}$$$.
For all $$$1 \le i \le |s|$$$, compute:
$$$$$$f_s(i)=\lvert\left\{w \in \Sigma^+ \mid \exists x \in \operatorname{occ}_s(w), x \le i \le x + |w|-1\right\}\rvert$$$$$$
In other words, $$$f_s(i)$$$ is the number of distinct substrings of $$$s$$$ that cover position $$$i$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.
The only line of each test case contains the string $$$s$$$ ($$$1 \le |s| \le 10^6$$$).
It is guaranteed that the sum of $$$|s|$$$ over all test cases does not exceed $$$10^6$$$.
For each test case, output $$$|s|$$$ integers, where the $$$i$$$-th integer represents $$$f_s(i)$$$.
4aaaaaaabaaaabcbcba
15 5 5 5 55 8 9 7 57 12 15 15 15 12 7
For an array $$$a$$$ of length $$$n$$$ and an integer $$$k$$$, define $$$f_k(a)$$$ as follows:
You are given two integers $$$m$$$ and $$$k$$$. Construct an array $$$a$$$, where $$$1 \le a_i \le m$$$ for all $$$1 \le i \le |a|$$$, such that $$$f_k(a) \lt f_{k+1}(a)$$$.
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 only line of each test case contains two integers $$$m$$$ and $$$k$$$ ($$$1 \le m,k \le 10^5$$$).
It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, if no such $$$a$$$ exists, output $$$-1$$$ on a single line.
Otherwise, first output an integer $$$n$$$ ($$$1 \le n \le 5 \cdot m$$$) on a single line, representing the length of $$$a$$$. It can be proven that if there exists a valid $$$a$$$, there exists one with length no larger than $$$5 \cdot m$$$.
Then, output $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le m$$$) on a single line.
If there are multiple valid $$$a$$$'s, you may output any of them.
21 15 3
-1 12 4 3 2 1 4 3 5 4 3 2 1 5
This is an interactive problem.
There is a hidden directed graph $$$G$$$ consisting of $$$n$$$ vertices. It is guaranteed that $$$G$$$ does not contain self loops or multiple edges. Your task is to determine whether $$$G$$$ contains a vertex with in-degree $$$n-1$$$ and out-degree $$$0$$$.
You may ask queries of the following format:
Please find any vertex that satisfies the above conditions, or determine that such a vertex does not exist, using no more than $$$3n$$$ queries.
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 an integer $$$n$$$ ($$$2 \le n \le 10^3$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$.
To ask a query, print a line in the following format:
The judge will respond with an integer $$$x$$$. If $$$x=1$$$, there is an edge from vertex $$$u$$$ to $$$v$$$; if $$$x=0$$$, there is no edge from vertex $$$u$$$ to $$$v$$$.
After printing each query do not forget to output the end of line and flush$$$^{\text{∗}}$$$ the output. Otherwise, you will get Idleness limit exceeded verdict.
If, at any interaction step, you read $$$-1$$$ instead of valid data, your solution must exit immediately. This means that your solution will receive Wrong answer because of an invalid query or any other mistake. Failing to exit can result in an arbitrary verdict because your solution will continue to read from a closed stream.
To report the answer, print a line in the following format:
After that, proceed to process the next test case or terminate the program if it was the last test case.
Note that reporting the answer does not count towards the number of queries.
Note that the interactor is adaptive. This means that $$$G$$$ may change during the interaction, but it is guaranteed that there is always some graph that satisfies the answers to all previous queries.
$$$^{\text{∗}}$$$To flush, use:
2 4 1 0 4 0 1
? 1 2 ? 3 1 ! 2 ? 1 4 ? 4 3 ! -1
The two graphs in the sample test are given below:

You are given an integer $$$n$$$. Construct a set of at least $$$m=\left\lfloor \sqrt{2}n \right\rfloor$$$ points, where the coordinates of the $$$i$$$-th point are $$$(x_i,y_i)$$$, such that:
It can be shown that this is always possible.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 138$$$). The description of the test cases follows.
The only line of each test case contains an integer $$$n$$$ ($$$3 \le n \le 5000$$$). It is guaranteed that within the same test, the values of $$$n$$$ in all test cases are pairwise distinct.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$.
For each test case, first output an integer $$$m$$$ ($$$\left\lfloor \sqrt{2}n \right\rfloor \le m \le n^2$$$), representing the number of points you constructed. Then output $$$m$$$ lines, where the $$$i$$$-th line contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i,y_i \le n$$$), representing the coordinates of the $$$i$$$-th point.
13
4 1 1 2 1 2 2 3 2
There is a $$$n \times m$$$ grid of white cells. The following operation will be performed exactly $$$k$$$ times:
Find the expected sum of the perimeters of all black connected components after all operations.
Formally, let $$$M = 10^9+7$$$. It can be shown that the exact answer can be expressed 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}$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.
The only line in each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le n,m \le 10^9, 1 \le k \le 10^{18}$$$).
For each test case, output the expected sum of the perimeters of all black connected components after all operations.
51 2 22 2 24 4 1011 45 14114 514 1919810
563724697842233769187309680
You are given an array $$$a$$$ of length $$$n$$$. Find the longest even-length palindromic subsequence of $$$a$$$.
The first line of each test contains an integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the length of $$$a$$$.
The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^5$$$) — the elements of $$$a$$$.
It is guaranteed that, except for the sample test, the elements of $$$a$$$ are generated uniformly at random in the range $$$[1,10^5]$$$, and each element occurs at least twice in $$$a$$$. Specifically, $$$a$$$ is generated by the following C++ code:
Firstly, output an even integer $$$m$$$ ($$$2 \le m \le n$$$) on a single line, representing the length of the longest even-length palindromic subsequence of $$$a$$$.
Then, output $$$m$$$ integers $$$b_1,b_2,\ldots,b_m$$$ ($$$1 \le b_i \le 10^5$$$) on a single line, representing the subsequence you found. If there are multiple longest even-length palindromic subsequences, you may output any of them.
71 1 4 5 1 4 5
2 5 5
92 10 2 10 1 9 2 1 9
4 2 10 10 2