You are given a positive integer $$$n$$$. Your task is to construct an array $$$a$$$ of size $$$|a|$$$ such that $$$|a| \le n$$$.
Let $$$b$$$ be an empty array. For every pair of indices $$$i$$$ and $$$j$$$ such that $$$1 \le i \lt j \le |a|$$$, we add the greatest common divisor (GCD) of $$$a_i$$$ and $$$a_j$$$ to array $$$b$$$.
Your constructed array $$$a$$$ must satisfy the condition that the MEX of the array $$$b$$$ (containing all pairwise GCDs $$$gcd(a_i, a_j)$$$ for $$$i \neq j$$$) is at least $$$n$$$.
The MEX (Minimum Excluded value) of a set of positive integers is the smallest positive integer that is not present in the set. The MEX of an empty set of positive integers is $$$1$$$.
The input consists of $$$t$$$ test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 30$$$) – the number of test cases. Each of the following $$$t$$$ lines contains a single integer $$$n$$$ ($$$1 \le n \le 30$$$).
For each test case, output two lines. The first line should contain a single integer, the size of the constructed array $$$a$$$. The second line should contain $$$|a|$$$ space-separated integers, the elements of the array $$$a$$$. The elements $$$a_i$$$ must satisfy $$$1 \le a_i \le 10^{18}$$$.
2 2 3
2 1 2 3 1 2 4
We have an array $$$a$$$ of $$$n$$$ integers, and an initially empty sheet of paper.
We will do the following operations in-order $$$m$$$ times:
You are asked to calculate the probability that the integers written on the sheet are non-decreasing. Output the calculated value modulo $$$998244353$$$.
The first and only line contains two positive integers $$$n$$$ and $$$m$$$ $$${(1 \le n, \space m \le 10^6)}$$$ — the length of the array and the number of times we repeat the operations.
The following line contains $$$n$$$ integers $$$a_1, \space a_2, \space \dots, \space a_n$$$ $$$(1 \le a_i \le n)$$$ — the elements of the array $$$a$$$.
Output one integer, the probability that the integers written on the sheet are non-decreasing modulo $$$998244353$$$.
3 21 1 2
110916040
The possible final sequences on the paper are :
There is a Pizza Man, who sells pizza, called Hosen.Hosen can store no more than $$$m$$$ pieces of pizza and initially he has $$$m$$$ pieces of pizza stored in his store.
There are $$$n$$$ customers; the $$$i_{th}$$$ of them will order $$$a_i$$$ pieces of pizza.
And Hosen has a positive integer $$$d$$$, that is when the number of pieces in the store is less than $$$d$$$, he refills it to $$$m$$$.
Here is a more detailed explanation.
When a customer orders $$$x$$$ pieces of pizza, the following happens:
You are given $$$m$$$,$$$n$$$ and the array of customers $$$a$$$; find the minimum value of $$$d$$$ for which all the customers will be happy.
The first line contains a single integer $$$t$$$ $$$(1 \le t \le 10^5)$$$, the number of test cases.
The first line of each test case contains two integers $$$n$$$ and $$$m$$$ $$$(1 \le n,m \le 2 \times 10^5)$$$.
The second line of each test case contains $$$n$$$ integers $$${a_1,a_2,\dots,a_n}$$$ $$$(1 \le a_i \le m)$$$, the elements of the array $$$a$$$.
Let $$$s$$$ be the sum of the array elements. It is guaranteed that the sum of $$$s$$$ overall test cases doesn't exceed $$$2 \times 10^5$$$.
For each test case output a single positive integer $$$d$$$, the minimum value of $$$d$$$ for which all the customers will be happy.
43 21 1 12 21 25 51 3 3 3 56 52 5 1 1 1 5
1 2 3 5
You are the master of an arena with $$$n$$$ fighters under your control, numbered from $$$1$$$ to $$$n$$$. Fighter $$$1$$$ is your favourite—you want them to emerge as the sole champion.
You are given an $$$n\times n$$$ matrix $$$M$$$. For $$$i\neq j$$$:
It is guaranteed that $$$M_{i,i} = \texttt{0}$$$ for all $$$i$$$, and for all $$$i \ne j$$$, the pair $$$(M_{i,j}, M_{j,i})$$$ is one of: $$$(\texttt{1}, \texttt{0})$$$, $$$(\texttt{0}, \texttt{1})$$$, or $$$(\texttt{?}, \texttt{?})$$$.
You must schedule exactly $$$n-1$$$ one-on-one matches so that in the end exactly one fighter remains undefeated, and that fighter must be number $$$1$$$. A fighter may appear in multiple matches (until eliminated).
Decide any choices for the '?' entries and output a sequence of $$$n-1$$$ matches $$$(a,b)$$$ meaning "$$$a$$$ defeats $$$b$$$". If it's impossible to make fighter 1 the unique champion, print $$$\textbf{No}$$$.
The first line contains a single integer $$$t$$$ $$$(1 \le t \le 500)$$$ — the number of test cases.
The first line of each test case contains integer $$$n$$$ ($$$2\le n\le 1000$$$) — the number of fighters.
Then in each test case follow $$$n$$$ lines each with a string of length $$$n$$$, the $$$i$$$-th line being $$$M_{i,1}M_{i,2}\dots M_{i,n}$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$1000$$$.
In each test case, if it is impossible to guarantee fighter $$$1$$$ wins, print $$$\textbf{No}$$$. Otherwise, print $$$\textbf{Yes}$$$ followed by $$$n - 1$$$ lines. Each line should contain two integers $$$a$$$ and $$$b$$$ indicating that fighter $$$a$$$ defeats fighter $$$b$$$ in a scheduled match. You may print each character in either case, for example $$$\textbf{YES}$$$ and $$$\textbf{yEs}$$$ will also be accepted.
3301000110050100000100100?011?011110050100000100100001110111100
Yes 2 3 1 2 Yes 4 5 3 4 2 3 1 2 No
Given a tree of $$$n$$$ vertices, each node is initially either black or white.
A path from node $$$u$$$ to node $$$v$$$ is:
A white path is clean if and only if there is no black path that contains this white path as a subpath of it.
A path is a subpath from another path if the set of vertices of this path forms a subset of the vertices of the other path.
The score of the tree is the number of Clean White Paths.
Note that path $$$(u,v)$$$ is the same as path $$$(v,u)$$$ and has to be counted once, and you have to count paths of the form $$$(u,u)$$$.
Given $$$q$$$ update queries, in each query you are given an integer $$$u$$$, make the node $$$u$$$ black.
After each query, print the score of the tree.
The first line contains a single integer $$$t$$$ $$$(1 \le t \le 10^5)$$$, the number of test cases.
The first line of each test case contains two integers $$$n$$$ and $$$q$$$ $$$(1 \le n \le 3 \times 10^5)$$$ $$$(1 \le q \le 3 \times 10^5)$$$ , the size of the tree and the number of queries respectively.
The second line of each test case contains a binary string $$$s$$$ of size $$$n$$$ $$$(s_i \in (0,1))$$$ , if $$$s_i$$$ is $$$1$$$ then the $$$i_{th}$$$ node is white, otherwise the node is black.
The next $$$n-1$$$ lines describe the edges of the tree, each line contains two integers $$$u$$$,$$$v$$$ $$$(1 \le u,v \le n)$$$, which means there is an edge between node $$$u$$$ and node $$$v$$$.
Each of the next $$$q$$$ lines contains a single integer $$$u$$$ $$$(1 \le u \le n)$$$, the node to make black.
It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases doesn't exceed $$$3 \times 10^5$$$.
In each test case, after each query print the score of the tree.
15 5111115 21 35 43 443241
10 6 2 2 0
After the first update, node $$$4$$$ is black; all white paths are clean, so the answer is $$$10$$$.
After the third update, there are two clean white paths: $$$(1,5)$$$ and $$$(1,1)$$$.
In many parts of the world, when someone absolutely dominates a field—be it sports, coding, cooking, or making memes—we say they're your uncle.
It's not a family thing. It's a respect thing. Like, "Tourist is your uncle" means he owns Competitive Programming, and you just watch and clap.
So today, we're not solving graphs or DP. We're solving respect.
Given a simple question, answer it from your heart.
Who is your uncle?
Print the name of your uncle, or just print "Tourist".
Who is your uncle?
Tourist
There are $$$n$$$ cities, numbered from $$$1$$$ to $$$n$$$. City $$$1$$$ is where Alice lives, and city $$$n$$$ is where her rival Bob resides.
The cities are connected by directed roads. Each city has at most two outgoing roads.
Alice wants to move from city $$$1$$$ to city $$$n$$$. She can visit the same city multiple times. In each city she visits, she can choose to buy the pile in that city, but she can buy at most one pile per city during her journey.
When Alice reaches city $$$n$$$, she will play a Nim game against Bob using the piles she has collected. Bob plays first.
Your task is to determine whether Alice can select piles along her journey such that she guarantees a victory over Bob.
Note: Alice must select at least one pile on the path from $$$1$$$ to $$$n$$$, and it's guaranteed there's a path from $$$1$$$ to $$$n$$$.
In Nim, players take turns removing any number of stones from one pile. The player who removes the last stone wins.
The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of cities.
Each of the next $$$n$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$0 \leq u_i, v_i \leq n$$$) — the outgoing roads from city $$$i$$$.
The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 10^6$$$) — the number of stones in the pile at each city.
output YES if Alice can guarantee a win, and NO otherwise.
4 2 0 3 4 1 0 0 0 2 4 1 1
YES
4 2 0 3 4 1 0 0 0 2 4 8 1
NO
Given an array $$$a$$$ of length $$$n$$$ $$$(1 \le n \le 10^5)$$$ $$$(1 \le a_i \le 10^9)$$$. Initially, you are at index $$$1$$$ and you want to go to index $$$n$$$ and visit every index exactly once.
You can jump from your current index $$$i$$$ to another index $$$j$$$ if and only if the absolute difference doesn't exceed 2, that is $$$|i - j| \le 2$$$.
You also have an array $$$b$$$, that initially contains one element $$$a_1$$$. When you jump to a new index $$$j$$$, you mark it as visited and put $$$a_j$$$ at the end of the array $$$b$$$.
Your task is to print the lexicographically minimum array $$$b$$$ that can be obtained after a valid walk, that is after visiting all indices and ending at index $$$n$$$.
The first line contains a single integer $$$t$$$ $$$(1 \le t \le 10^5)$$$, the number of test cases.
The first line of each test case contains a single integer $$$n$$$ $$$(1 \le n \le 10^5)$$$, the length of the array $$$a$$$.
The second line of each test case contains $$$n$$$ integers $$${a_1,\space a_2,\dots,\space a_n}$$$ $$$(1 \le a_i \le 10^9)$$$, the elements of the array $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.
For each test case, print $$$n$$$ space separated integers $$${b_1, \space b_2, \dots ,\space b_n}$$$, the elements of array $$$b$$$, that is the lexicographically minimum array that can be obtained after a valid walk.
333 2 144 3 1 273 1 1 2 3 2 1
3 2 1 4 1 3 2 3 1 1 2 2 3 1
You are given an array $$$a$$$ of length $$$n$$$ and a positive integer $$$k$$$.
You will perform the following operation on the array exactly $$$k$$$ times:
After performing the operation exactly $$$k$$$ times, print the resulting array.
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \lt n \leq 10^5$$$) — the size of the array and the number of operations.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the elements of the array.
Print $$$n - k$$$ integers — the elements of the resulting array after performing the operations.
5 2 1 2 3 4 5
2 3 4
For an array $$$a$$$ of length $$$n$$$, we define the function $$$f(a)$$$ as following: $$$$$${f(a) = gcd(a_1, \space a_1) + gcd(a_1, \space a_2) + ..gcd(a_1, \space a_2, \space ..a_n)}$$$$$$ You are given two positive integers $$$n$$$ and $$$m$$$. You need to output the sum of $$$f(a)$$$ over all arrays $$$a$$$ satisfying the following:
For two positive integers $$$x$$$ and $$$y$$$, $$$gcd(x, \space y)$$$ equals the greatest integer that divides both $$$x$$$ and $$$y$$$.
The first line contains a single positive integer number $$$t$$$ $$${(1 \le t \le 10^3)}$$$ — the number of testcases.
The first line of each testcase contains two positive integers $$$n$$$ and $$$m$$$ $$${(1 \le n, \space m \le 10^6)}$$$.
For each testcase, output in a separate line the answer described in the statement. Since the output number can be too large, output it modulo $$${10^9 + 7}$$$.
21 52 9
15 550
You are given an array $$$a$$$ of $$$n$$$ elements and an integer $$$x$$$. You also have a variable $$$v$$$ which is initially equal to $$$0$$$.
You will perform exactly one operation for each element of the array, and the operations must be applied in the order of the array (i.e., from $$$a_1$$$ to $$$a_n$$$). For each $$$i$$$ from $$$1$$$ to $$$n$$$, you can choose one of the following operations:
You must use the second operation (AND) at least $$$x$$$ times throughout the process.
Determine the maximum possible value of $$$v$$$ at the end.
The first line of input contains one integer $$$t$$$ $$$(1$$$ $$$\le$$$ $$$t$$$ $$$\le$$$ $$$1000)$$$, the number of test cases.
The first line of each test case contains two integers $$$n$$$ and $$$x$$$ $$$(1$$$ $$$\le$$$ $$$x$$$ $$$\le$$$ $$$n$$$ $$$\le$$$ $$$10^5)$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,..,a_n$$$ $$$(0 \le a_i \le 10^5)$$$, the elements of the array.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, print in a separate line the maximum value possible for $$$v$$$ after the $$$n$$$ operations.
32 12 23 20 3 24 11 0 2 3
2 2 3
You are given an array $$$a$$$ of $$$n$$$ non-negative integers, and a positive integer $$$m$$$.
You need to answer $$$q$$$ queries. In each query, you are given a non-negative integer $$$x$$$.
You can do the following operation:
For each query, output the minimum number of operations needed to make the elements of the array equal, or output $$$-1$$$ if they cannot be made equal.
The first line contains a single positive integer $$${1 \le t \le 3 \times 10^5}$$$ — the number of testcases.
The first line of each testcase contains two positive integers $$$n$$$ and $$$m$$$ $$${(1 \le m \le n \le 10^6)}$$$.
The next line contains $$$n$$$ non-negative integers $$${a_1, \space a_2, \space ..a_n}$$$ — the elements of array $$$a$$$, where $$$({0 \le a_i \lt 2^{30}})$$$.
The next line contains a single positive integer $$$q$$$ $$${(1 \le q \le 10^6)}$$$ — the number of queries.
Each of the following $$$q$$$ lines contains a single non-negative integer $$$({0 \le x \lt 2^{30}})$$$.
It is guaranteed that the sum of $$$n$$$ and $$$q$$$ over all testcases does not exceed $$${10^6}$$$.
For each query of each testcase, output the answer to it as described in the problem statement.
22 11 32246 222 4 14 30 28 12426273031
1 -1 3 3 3 3
You are given an integer $$$n$$$. Your task is to construct a permutation $$$p$$$ of the set $$$\{1, 2, \dots, n\}$$$ such that when the permutation is considered as circular (i.e. $$$p_1$$$ and $$$p_n$$$ are adjacent), the maximum bitwise OR over all adjacent pairs is minimized.
Formally, for a permutation $$$p = [p_1, p_2, \dots, p_n]$$$, define
$$$f(p) = \max_{1 \le i \le n} \Bigl( p_i \; | \; p_{i+1} \Bigr)$$$,
where we define $$$p_{n+1} = p_1$$$ and $$$|$$$ denotes the bitwise OR operation. Your goal is to construct a permutation $$$p$$$ such that $$$f(p)$$$ is as small as possible.
If there are multiple answers, you may output any one of them.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases.
Each of the following $$$t$$$ lines contains a single integer $$$n$$$ ($$$2 \le n \le 5 \cdot 10^5$$$) — the size of the permutation.
It is guaranteed that the sum of all $$$n$$$ over the test cases does not exceed $$$5 \cdot 10^5$$$.
For each test case, output a permutation of $$$\{1,2,\dots,n\}$$$ (i.e. a sequence of $$$n$$$ distinct integers) such that the maximum bitwise OR of any two adjacent elements (considering the permutation circularly) is minimized.
3 3 4 7
1 3 2 1 4 2 3 1 7 2 3 4 5 6