A string $$$s$$$ is beautiful if it can be expressed in the form:
$$$$$$s=t+t'+t$$$$$$
where:
You are given a string $$$s$$$. Find the number of beautiful substrings of $$$s$$$.
Each test contains multiple test cases. The first line of each test contains an integer $$$t$$$ ($$$1 \le t \le 10^5 $$$) — the number of test cases.
The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the length of $$$s$$$.
The second line contains the string $$$s$$$ ($$$|s|=n$$$). It is guaranteed that $$$s$$$ consists of only lowercase English characters.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.
For each test case, output the number of beautiful substrings of $$$s$$$.
26baabba5zzzzz
1 3
You are given a positive integer $$$N$$$ and a digit $$$x$$$ ($$$1 \le x \le 9$$$). Let $$$n$$$ be the number of digits of the decimal representation of $$$N$$$. For example, if $$$N=10000$$$, then $$$n=5$$$; if $$$N=114514$$$, then $$$n=6$$$.
Define $$$g(M)$$$ to be the number of occurrences of the digit $$$x$$$ in the decimal representation of $$$M$$$. For example, if $$$x=6$$$, then $$$g(1)=0$$$, $$$g(666)=3$$$, and $$$g(16161)=2$$$.
For each integer $$$k$$$ with $$$0 \le k \le n$$$, let $$$f(k)$$$ be the number of positive integers $$$M \le N$$$ for which $$$g(M)=k$$$.
Your task is to compute $$$f(k) \bmod {998244353}$$$ for every $$$0 \le k \le n$$$.
The first line of each test contains the integer $$$N$$$ ($$$1 \le N \le 10^{200000}$$$).
The second line contains the digit $$$x$$$ ($$$1 \le x \le 9$$$).
Output $$$n+1$$$ integers, the $$$i$$$-th one being $$$f(i-1) \bmod {998244353}$$$.
1145141
59048 39366 12726 2912 428 33 1
19198109
1062881 662661 169786 22744 1673 64 1 0
12
1 0
You are given a graph of $$$n$$$ vertices labeled from $$$1$$$ to $$$n$$$, initially with no edges. You need to process $$$q$$$ queries of the following types:
After each query, output the number of pairs of integers $$$(x,y)$$$ such that $$$1 \le x \lt y \le n$$$ and there exists a path from vertex $$$x$$$ to vertex $$$y$$$. A path from vertex $$$x$$$ to vertex $$$y$$$ is defined as a sequence of integers $$$p_1,p_2,\ldots,p_m$$$ such that $$$p_1=x$$$, $$$p_m=y$$$, and there exists an undirected edge between vertices $$$p_i$$$ and $$$p_{i+1}$$$ for all $$$1 \le i \lt m$$$.
Note that you must solve this problem in online mode. That is, you can only read the current query after outputting the answer for the previous one. Also, remember to flush the output after each query. To do this, use:
The first line of each test contains two integers $$$n$$$ and $$$q$$$ ($$$2 \le n \le 10^9$$$, $$$1 \le q \le 2 \cdot 10^5$$$) — the number of vertices and queries respectively.
Each of the next $$$q$$$ lines describes a query. If the line starts with the integer $$$1$$$, it is followed by two integers $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n$$$, $$$u \ne v$$$); if the line starts with the integer $$$2$$$, it is followed by two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \lt r \le n$$$).
After each query, output the number of pairs of integers $$$(x,y)$$$ such that $$$1 \le x \lt y \le n$$$ and there exists a path from vertex $$$x$$$ to vertex $$$y$$$.
6 31 1 52 2 41 1 6
1 4 6
You are given an array $$$a$$$ of length $$$n$$$. Your task is to process $$$q$$$ queries of three types:
Output the answer for each type $$$3$$$ query.
The first line of each test contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n \le 10^5$$$, $$$1 \le q \le 2 \cdot 10^5$$$) — the length of $$$a$$$ and the number of queries, respectively.
The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^7$$$) — the elements of $$$a$$$.
Each of the next $$$q$$$ lines contains a query in one of three formats:
It is guaranteed that there is at least one type $$$3$$$ query.
For each type $$$3$$$ query, output the answer — the sum of $$$a_l,a_{l+1},\ldots,a_r$$$.
5 101 2 3 4 53 1 51 1 103 1 52 1 4 23 1 41 3 152 3 5 53 1 52 1 5 13 2 4
15 24 7 15 3
Note: this problem is different from "WTF Another GCD?". The difference is highlighted in bold.
In this task, you need to maintain a multiset $$$S$$$ that stores pairs of integers $$$(v,w)$$$.
Your multiset should support $$$n$$$ operations of the following types:
$$$$$$\max(\{w~|~(v,w) \in S \land \gcd(v,k) \ne 1 \})$$$$$$
where $$$\gcd(x,y)$$$ represents the largest integer that divides both $$$x$$$ and $$$y$$$.
If no such pair exists, output $$$0$$$.
The first line of each test contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of operations.
Each of the next $$$n$$$ lines describes an operation:
For each query, output one integer representing the maximum value of $$$w$$$ you found, or $$$0$$$ if no such $$$w$$$ exists.
8+ 4 5+ 3 4? 2? 3- 3 4? 4+ 114514 8? 3
5 4 5 0
Note: this problem is different from "Another GCD". The difference is highlighted in bold.
In this task, you need to maintain a multiset $$$S$$$ that stores pairs of integers $$$(v,w)$$$.
Your multiset should support $$$n$$$ operations of the following types:
$$$$$$\max(\{w~|~(v,w) \in S \land \gcd(v,k) = 1 \})$$$$$$
where $$$\gcd(x,y)$$$ represents the largest integer that divides both $$$x$$$ and $$$y$$$.
If no such pair exists, output $$$0$$$.
The first line of each test contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of operations.
Each of the next $$$n$$$ lines describes an operation:
For each query, output one integer representing the maximum value of $$$w$$$ you found, or $$$0$$$ if no such $$$w$$$ exists.
8+ 4 5+ 3 4? 2? 3- 3 4? 4+ 114514 8? 3
4 5 0 8
This is an interactive problem.
There is a hidden non-negative integer $$$n$$$ ($$$0 \le n \lt 2^{64}$$$). Your task is to determine $$$n$$$ by asking queries of the following type:
You may ask no more than $$$63$$$ queries.
Each test contains multiple test cases. The first line of each test contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases.
To ask a query, output a line in the following format:
where $$$x$$$ ($$$0 \le x \lt 2^{64}$$$) is the integer you asked.
After each query, you should read one line containing one integer, denoting the number of set bits of $$$n \oplus x$$$.
When you are ready to output the answer, output a line in the following format:
where $$$n$$$ ($$$0 \le n \lt 2^{64}$$$) is the hidden integer.
Note that printing the answer is not counted within the total number of queries.
The interactor is NOT adaptive, meaning that the answer is known before the participant asks the queries and does not depend on the queries asked by the participant.
After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
1 2 13
? 0 ? 114514 ! 9
In the first test case, the hidden integer is $$$9$$$.
In the first query, $$$x=0$$$. The binary representation of $$$9 \oplus 0 = 9$$$ is $$$1001$$$, with $$$2$$$ set bits. The judge responds with $$$2$$$.
In the second query, $$$x=114514$$$. The binary representation of $$$9 \oplus 114514 = 114523$$$ is $$$11011111101011011$$$, with $$$13$$$ set bits. The judge responds with $$$13$$$.
You are given a positive integer $$$n$$$. Construct a permutation $$$p$$$ of $$$\{1,2,\ldots,n\}$$$ such that the length of the longest increasing subsequence of $$$p$$$ is equal to the length of the longest decreasing subsequence of $$$p$$$.
An increasing (decreasing) subsequence of an array $$$a$$$ of length $$$n$$$ is defined as an array $$$b$$$ such that:
Each test contains multiple test cases. The first line of each test contains an integer $$$t$$$ ($$$1 \le t \le 5 \cdot 10^4$$$) — the number of test cases.
The only line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of $$$p$$$ to be constructed.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, if no solution exists, output $$$-1$$$ on a single line.
Otherwise, output $$$n$$$ distinct integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$) on a single line — the permutation $$$p$$$ you constructed.
If multiple solutions exist, you may output any of them.
13
3 1 2
Yes, this problem is related to squares.
You are given a set $$$S$$$ of $$$n$$$ distinct points on the $$$xy$$$-plane, each with integer coordinates. A set of points $$$T$$$ is squarish if it contains exactly four points, and they can form the four vertices of a square.
Now, you have to remove exactly one point from $$$S$$$. Find the maximum number of squarish subsets of $$$S$$$ if you remove the point optimally.
In case you are not aware, three squared equals nine.
Each test contains multiple test cases. The first line of each test contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.
The first line of each test case contains an integer $$$n$$$ ($$$5 \le n \le 5000$$$) — the number of points in $$$S$$$.
The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^9 \le x_i,y_i \le 10^9$$$) — the coordinates of the $$$i$$$-th point. It is guaranteed that the $$$n$$$ points are distinct.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$.
For each test case, output the maximum number of squarish subsets of $$$S$$$ after removing exactly one point.
19-1 1-1 0-1 -10 10 00 -11 11 01 -1
4
In the sample test case, one of the optimal solutions is to remove $$$(1,1)$$$. There will be $$$4$$$ squarish subsets in $$$S$$$, which are:
The nuclear power plant (HDZ) on planet A has exploded, turning into ruins. The height of the ruins can be represented by a permutation $$$p$$$ of $$$\{1,2,\ldots,n\}$$$, where the height at position $$$i$$$ is $$$p_i$$$.
Scientists have designed a robot named Bronya with a detection range $$$d$$$ to clean up nuclear waste at the highest point (height $$$n$$$) in the ruins. During airdrop, since precise positioning is impossible, Bronya may land at any position in the ruins. After landing, Bronya begins moving: if Bronya is at position $$$i$$$, she will move to the highest position $$$j$$$ such that $$$\max(1,i-d) \le j \le \min(n,i+d)$$$, repeating this process $$$10^{100}$$$ times. As a scientist, you need to determine the minimum $$$d$$$ such that no matter where Bronya lands initially, she will reach the highest point after all movements.
Each test contains multiple test cases. The first line of each test contains an integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases.
The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 10^6$$$) — the length of $$$p$$$.
The second line contains $$$n$$$ distinct integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$) — the elements of $$$p$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.
For each test case, output the minimum $$$d$$$ that guarantees Bronya will reach the highest position.
31151 2 3 4 5107 3 1 9 10 2 5 6 4 8
0 1 5
Baozii is bad at coming up with interesting stories for the problem statement, so he decided to present the problem to you straightaway.
You are given a tree $$$T$$$, consisting of $$$n$$$ vertices labeled from $$$1$$$ to $$$n$$$. Recall that a tree is a connected acyclic graph. There are $$$k$$$ stones on the tree, where the $$$i$$$-th stone is located at vertex $$$a_i$$$. In each operation, you can move a stone to one of its neighbouring vertices. Note that having multiple stones on one vertex is allowed.
A path from vertex $$$u$$$ to vertex $$$v$$$ is defined as a sequence of distinct vertices $$$p_1,p_2,\ldots,p_m$$$, such that $$$p_1=u$$$, $$$p_m=v$$$, and there exists an edge between vertices $$$p_i$$$ and $$$p_{i+1}$$$ for all $$$1 \le i \lt m$$$.
The tree is good if there exists a path such that all stones are located on the path. Note that the stones may not necessarily cover the entire path. They just have to be located at vertices on the path.
Your task is to compute the minimum number of operations required to make $$$T$$$ good.
Each test contains multiple test cases. The first line of each test contains an 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 $$$k$$$ ($$$1 \le k \le n \le 2 \cdot 10^5$$$) — the number of vertices in $$$T$$$, and the number of stones, respectively.
Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n$$$, $$$u \ne v$$$), representing an edge between vertices $$$u$$$ and $$$v$$$. It is guaranteed that the input forms a valid tree.
The next line contains $$$k$$$ integers $$$a_1,a_2,\ldots,a_k$$$ ($$$1 \le a_i \le n$$$) — the initial locations of the stones.
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 minimum number of operations required to make $$$T$$$ good.
41 117 31 22 33 44 53 66 71 5 74 41 21 34 11 2 3 45 31 22 33 44 51 2 2
0 2 1 0
For an array $$$a$$$, define $$$f(a)$$$ as follows:
You are given an array $$$a$$$ of length $$$n$$$. Answer $$$q$$$ queries in the following form:
The first line of each test contains two integers $$$n$$$ and $$$q$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le q \le 10^6$$$) — the length of $$$a$$$ and the number of queries, respectively.
The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0 \le a_i \lt 2^{30}$$$) — the elements of $$$a$$$.
Each of the next $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$), describing a query.
For each query, output $$$f([a_l,a_{l+1},\ldots,a_r])$$$.
6 35 4 2 1 1 81 31 62 5
11 20 8
Baozii Cup problems have always been known for their succinct problem statements. This one is no exception.
You are given an array $$$a$$$ of length $$$n$$$. Count the number of quadruplets of integers $$$(i,j,k,l)$$$ that satisfy the following conditions:
The first line of each test contains an integer $$$n$$$ ($$$1 \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 n$$$) — the elements of $$$a$$$.
Output the number of quadruplets of integers satisfying the conditions on a single line.
61 1 4 5 1 4
2
71 1 2 1 2 2 1
6