The United Galactic Federation has constructed the Quantum Corridor, a massive linear particle accelerator consisting of $$$n$$$ discrete energy chambers indexed from $$$1$$$ to $$$n$$$.
You are the lead physicist overseeing an experiment with a single Chronon particle currently stabilized in chamber $$$i$$$. Due to the corridor's unique resonance, the Chronon travels exclusively by tunneling, instantly teleporting from its current position to a new one. The tunneling drive operates in two modes:
The containment field is strictly bounded by the corridor's physical dimensions. A tunnel is only successful if the destination chamber $$$x$$$ satisfies $$$1 \le x \le n$$$. If a jump attempts to land the particle outside this range, the field suppresses the move, and the Chronon remains in its current chamber.
The Chronon possesses infinite energy and can perform any sequence of valid jumps. However, the specific geometry of the corridor and the jump lengths may render certain sections of the accelerator permanently inaccessible.
Your task is to calculate the number of Dark Chambers, defined as the number of indices in the range $$$[1, n]$$$ that the Chronon can never reach, regardless of the sequence of moves attempted.
The first line contains a single integer $$$Q$$$ ($$$1 \le Q \le 10^5$$$), the number of test cases.
Each of the next $$$Q$$$ lines contains four space-separated integers: $$$n$$$, $$$i$$$, $$$a$$$, and $$$b$$$. Here, $$$n$$$ ($$$1 \le n \le 10^9$$$) represents the total number of chambers, $$$i$$$ ($$$1 \le i \le n$$$) is the starting chamber, $$$a$$$ ($$$1 \le a \le 10^9$$$) is the length of the Alpha Shift, and $$$b$$$ ($$$1 \le b \le 10^9$$$) is the length of the Beta Shift.
For each test case, output a single integer on a new line representing the count of unreachable chambers.
29 3 3 615 10 1 17
60
Bob has a secret prime number $$$p$$$ ($$$10 \le p \le 10^8$$$). While he appreciates the properties of prime numbers, he would prefer it if his number were composite.
To achieve this, Bob can modify $$$p$$$ by prepending one or more decimal digits to the front of its decimal representation. He wants to prepend the minimum number of digits necessary to transform $$$p$$$ into a composite number.
For example, if $$$p = 11$$$:
Bob asks for your help.
The first line of the input contains an integer $$$T$$$ ($$$1 \le T \le 10^5$$$) — the number of test cases.
This is an interactive problem. Your program should perform the following steps for each test case:
1 NO
2 5
In the sample interaction, there is only one test case ($$$T = 1$$$). The hidden prime number is $$$p = 11$$$, which is known only to the judge. The value of $$$p$$$ remains constant throughout a single test case, though it may vary across different test cases. The interactor is not adaptive; that is, the hidden prime $$$p$$$ is fixed before the interaction begins and does not change based on your outputs.
The sample interaction proceeds as follows:
After outputting each line, you must flush the output. For example:
There are $$$n$$$ nests in a forest. These nests are connected by $$$n-1$$$ branches, forming a tree structure. From any nest, a crow can reach any other nest by flying along the branches, and there are no cycles.
A wise old crow wants to travel through all nests exactly once. It can fly in a straight path from one nest to the next if there exists a branch between those nests. The crow already decided the exact order in which it wants to visit the nests.
However, the current branch structure may not form a single straight path. To help the crow, you are allowed to carefully rearrange branches.
Your task is to transform the initial tree structure of nests into the desired chain structure using at most $$$n^2$$$ rearrangement operations.
In one rearrangement operation, you can choose three distinct nests $$$a,b,c$$$ such that the branches $$$(a,b)$$$ and $$$(b,c)$$$ exist in the current tree. Then perform the following change:
The first line contains an integer $$$t$$$ — the number of test cases.
For each test case:
It is guaranteed that the given branches form a tree structure.
The sum of $$$n^2$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case:
Each printed move must be valid at the moment it is applied. After all moves, the remaining branches must create the desired chain structure $$$p_1,p_2,\dots,p_n$$$.
1 4 1 2 3 4 2 1 2 3 2 4
1 4 2 3
Initial branches are $$$(2,1)$$$, $$$(2,3)$$$, $$$(2,4)$$$.
The move $$$[4\; 2\; 3]$$$ removes branch $$$(4,2)$$$ and adds branch $$$(4,3)$$$.
After the move, the branches become $$$(1,2)$$$, $$$(2,3)$$$, $$$(3,4)$$$, which forms the required chain $$$1-2-3-4$$$.
In the far-away galaxy of Phitron, a rare cosmic phenomenon known as a Dual Star System can be observed.
The system consists of two identical spherical planets, each with same radius, rotating on a circular orbit. The center of their rotation is the midpoint of the two planet centers. Throughout the motion, the two planets always remain on opposite sides of the orbit.
An astronaut living on a space station at the origin $$$(0,0,0)$$$ is fascinated by this phenomenon. He is feeling lazy today and he decided only to start his daily routine if he can draw a single straight line from his position that intersects or touches both planets at the same time.
Figure: Dual Star System in three-dimensional space. You are given the current coordinates of the centers of the two planets, $$$C_1$$$ and $$$C_2$$$, along with their common radius $$$r$$$ and angular speed $$$\omega$$$. The planets rotate with this speed, but the direction of rotation is unknown: it may be clockwise or counter-clockwise.
It is guaranteed that the space station lies on the same plane as the orbit of the planets. It is also guaranteed that the distance from the origin to the center of rotation is strictly greater than the radius of the circular orbit and planets are non-overlapping.
Since the direction of rotation is unknown, the astronaut wants to know the earliest possible time at which the alignment can occur, assuming the planets rotate in the more favorable direction.
Determine whether the alignment is possible. Print $$$-1$$$ if it is impossible, otherwise the minimum time at which it occurs.
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of the test cases follows.
Each test case consists of four lines.
The first line contains three integers $$$x_1, y_1, z_1$$$ ($$$-10^9 \le x_1, y_1, z_1 \le 10^9$$$) — the coordinates of the center of the first planet $$$C_1$$$.
The second line contains three integers $$$x_2, y_2, z_2$$$ ($$$-10^9 \le x_2, y_2, z_2 \le 10^9$$$) — the coordinates of the center of the second planet $$$C_2$$$.
The third line contains a single integer $$$r$$$ ($$$1 \le r \le 10^9$$$) — the radius of each planet.
The fourth line contains a real number $$$\omega$$$ ($$$0 \lt \omega \le 10$$$) — the angular speed of rotation in radians per second. It is guaranteed that $$$\omega$$$ contains at most $$$4$$$ digits after the decimal point.
For each test case, print $$$-1$$$, or the required minimum time in a single line.
Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-4}$$$.
33 2 59 6 1521.58 2 012 -2 010.60610 -12 2012 -2 1520.1265
00.6997215.000094
A radian is the SI unit for measuring angles, defined as the angle subtended at the center of a circle by an arc whose length is equal to the circle's radius. One radian is approximately equal to $$$57.295779513^\circ$$$. To be exact, $$$$$$ 1\ \mathrm{rad}=\frac{180}{\pi}^\circ. $$$$$$ You can use $$$\pi = \texttt{acos(-1.0)}$$$
You are given an array $$$A$$$ of length $$$n$$$ consisting only of numbers $$$0$$$ and $$$1$$$. You may obtain another array $$$A'$$$ by performing a cyclic rotation of $$$A$$$ by any number of positions (possibly zero).
A cyclic rotation shifts elements of an array circularly. For example, rotating $$$[1, 0, 1, 1, 0]$$$ gives: $$$$$$ [1, 0, 1, 1, 0], \; [0, 1, 1, 0, 1], \; [1, 1, 0, 1, 0], \; [1, 0, 1, 0, 1], \; [0, 1, 0, 1, 1] $$$$$$
Define an array $$$B$$$ as follows: $$$$$$B[i] = A[i] \oplus A'[i] \quad \text{for all } 0 \le i \lt n$$$$$$ where $$$\oplus$$$ denotes the bitwise XOR operation.
An array $$$X$$$ is said to be lexicographically larger than an array $$$Y$$$ if:
Your task is to determine the lexicographically maximum binary array $$$B$$$ that can be obtained among all possible cyclic rotations of $$$A$$$.
The first line of the input contains a single integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of test cases.
Each test case consists of two lines:
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output $$$n$$$ integers separated by spaces — the lexicographically maximum binary array $$$B$$$.
241 1 0 051 0 1 1 0
1 1 1 11 1 1 0 1
Consider the first test case where $$$A = [1, 1, 0, 0]$$$. The array has 4 cyclic rotations: $$$$$$ [1, 1, 0, 0], \; [1, 0, 0, 1], \; [0, 0, 1, 1], \; [0, 1, 1, 0]. $$$$$$
Calculating $$$B$$$ for each rotation:
Among all possible rotations $$$A'$$$, the lexicographically maximum $$$B$$$ is $$$[1, 1, 1, 1]$$$.
You are given two integers $$$N$$$ and $$$S$$$. Consider all possible arrays $$$[a_1, a_2, \dots, a_n]$$$ of $$$N$$$ non-negative integers such that $$$a_1 + a_2 + \dots + a_n = S$$$.
For each such array, compute the bitwise XOR of all its elements, i.e., $$$a_1 \oplus a_2 \oplus \dots \oplus a_n$$$. Find the sum of these values over all possible arrays.
Since the answer can be very large, output it modulo $$$998244353$$$.
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows.
Each test case consists of a single line containing two integers $$$N$$$ and $$$S$$$ ($$$1 \le N \le 5 \cdot 10^4$$$, $$$0 \le S \le 10^9$$$) — the length of the array and the required sum of elements.
It is guaranteed that the sum of $$$N$$$ over all test cases doesn't exceed $$$5 \cdot 10^4$$$.
For each test case, output a single integer — the sum of XOR values over all valid arrays, modulo $$$998244353$$$.
31 52 22 3
5412
In the first test case, the only valid array is $$$[5]$$$, and its XOR is $$$5$$$.
In the second test case, the valid arrays are $$$[0, 2]$$$, $$$[1, 1]$$$, and $$$[2, 0]$$$, with XOR values $$$2$$$, $$$0$$$, and $$$2$$$ respectively. The sum is $$$2 + 0 + 2 = 4$$$.
In the third test case, the valid arrays are $$$[0, 3]$$$, $$$[1, 2]$$$, $$$[2, 1]$$$, and $$$[3, 0]$$$, with XOR values $$$3$$$, $$$3$$$, $$$3$$$, and $$$3$$$ respectively. The sum is $$$3 + 3 + 3 + 3 = 12$$$.
You are a close friend of Dr. G, a bioinformatics scientist working at Phitron. Dr. G is studying the evolution of a synthetic genome in a controlled laboratory environment. Each genome is represented by a non-negative integer, where each bit in its binary representation corresponds to the presence or absence of a specific genetic marker.
The genome evolves over discrete time steps according to the following rules:
Dr. G has been manually checking whether the genome sequences evolve correctly, but this task has become extremely tedious and time-consuming. To help your friend, you decide to write a program that determines the genome value at a given time step $$$t$$$.
The first line contains an integer $$$T$$$ ($$$1 \le T \le 10^5$$$), the number of independent experiments.
Each of the next $$$T$$$ lines contains three integers $$$a$$$, $$$b$$$, and $$$t$$$ ($$$0 \le a, b \le 10^6$$$, $$$1 \le t \le 10^{8}$$$), where:
For each experiment, output a single integer — the genome value at time step $$$t$$$.
3 11 13 1 11 13 2 11 13 3
11 13 9
In this example, the initial genome values are $$$a = 11$$$ and $$$b = 13$$$.
The binary representations of the initial genomes are:
You are given a directed graph with $$$n$$$ nodes numbered from $$$1$$$ to $$$n$$$. Each node $$$i$$$ has an integer value $$$a_i$$$ associated with it. For any two distinct node $$$u$$$ and $$$v$$$, there is a directed edge from $$$u$$$ to $$$v$$$ if and only if:
$$$$$$(a_u \oplus a_v \gt a_u) \quad and \quad (a_u \oplus a_v \lt a_v)$$$$$$
where $$$\oplus$$$ denotes the bitwise XOR operation.
You may start from any node and traverse the graph by following directed edges. What is the maximum number of distinct nodes that can be visited in a single path?
Each test consists of several test cases. The first line contains a single integer $$$t$$$ $$$(1 \leq t \leq 10^4)$$$ — the number of test cases. The following describes the test cases:
The first line of each test case contains a single integer $$$n$$$ $$$(1 \le n \le 2 \times 10^5)$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,…,a_n$$$ $$$(0 \le a_i \le 2^{60})$$$
It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2 \times 10^5$$$
For each test case, output a single integer — the maximum number of distinct nodes that can be visited in a single path.
233 1 027 8
21
Bob visited the country of Bakdum not so long ago. The country has $$$n$$$ cities and they are all connected by two-way roads. Also, from one city, there is exactly one way to reach any other city.
Among the cities, there are several with high security because they have magical ley-lines running through them. Any city has at most 1 ley-line running through it. Bob went to a total of $$$m$$$ tours to see the ley-lines. During each tour, he was able to ride on a tracker bus, which could detect the presence of ley-lines. The $$$i$$$th tour can be represented as Bob going from city $$$u_i$$$ to city $$$v_i$$$, and during the whole tour the tracker was always on. But due to magical interference, Bob was only able to determine whether there was an even or odd number of ley-lines on that path, given as $$$p_i \in \{0, 1\}$$$.
Now after Bob returned, he was confused about the data he had collected. He has a suspicion that one of his tour results got flipped by a high energy cosmic ray bit flip incident. Now, for each tour report, assuming it was flipped individually, calculate the revised total number of ley-lines in Bakdum. If it is impossible to determine it uniquely or the data is inconsistent report them too.
The first line of the input contains $$$n$$$ ($$$1 \leq n \leq 500$$$) and $$$m$$$ ($$$1 \leq m \leq 50,000$$$).
Then follows $$$n - 1$$$ lines providing the city connections. Each line contains two integers $$$u$$$ and $$$v$$$, meaning there is a road between the cities $$$u$$$ and $$$v$$$. It is guaranteed that the layout satisfies the conditions in the statement.
Then there are $$$m$$$ lines, the $$$i$$$th line contains $$$u_i, v_i, p_i$$$ $$$(1 \leq u_i, v_i \leq n, p_i \in \{0, 1\})$$$. Here, $$$p_i = 0$$$ means the total number of ley-lines on that path was even, and $$$p_i = 1$$$ otherwise.
Output $$$m$$$ lines. The $$$i$$$th line should contain the total number of ley-lines in the country if the $$$i$$$th tour result was flipped. If the data is inconsistent output "impossible". If it is not possible to determine it uniquely, output "unknown". The output is case-sensitive.
5 6 1 2 2 3 2 4 4 5 1 1 0 1 3 1 3 4 1 1 4 1 1 5 0 1 2 0
5 2 3 1 impossible 2
4 4 1 2 1 3 3 4 1 1 1 2 2 1 1 2 0 3 3 1
impossible impossible impossible unknown
You are given an array $$$A$$$ of length $$$n$$$, where the array uses one-based indexing (i.e., the elements are $$$A[1], A[2], \dots, A[n]$$$).
Define the following function:
Function F(i, n, A):
Reverse(A[1..i]) // Reverse the prefix of length i
ret ← 0
For j ← 1 to n:
ret ← ret + (j × A[j]) // weighted sum
Return ret
That is, the function reverses the prefix of the array from index $$$1$$$ to $$$i$$$ and then computes the weighted sum.
The function operates on a temporary copy of the array $$$A$$$. That is,
For the given array $$$A$$$, consider all values $$$F(i, n, A)$$$ for $$$1 \le i \le n$$$. Your task is to output an ordering of indices $$$[p_1, p_2, \dots, p_n]$$$ which is a permutation of $$$\{1, 2, \dots, n\}$$$, such that:
The problem 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:
For each testcase, output the ordering in a single line separated by spaces.
250 -1 2 3 -451 0 1 0 -1
4 3 1 2 51 3 2 4 5
For the first array, the values of the function are:
Sorting the indices by non-decreasing values of $$$F(i, 5, A)$$$ gives the ordering: $$$[4 \; 3 \; 1 \; 2 \; 5]$$$
For the second array, there are two valid orderings that satisfy the constraints:
Among these, we output $$$[1 \; 3 \; 2 \; 4 \; 5]$$$ as it is the lexicographically smallest valid ordering.
There is a lottery that is going to happen and $$$n$$$ ($$$1 \le n \le 10^6$$$) people have already bought one or more lottery tickets: the $$$i$$$th person has bought $$$t_i$$$ ($$$1 \le t_i \le 5$$$) lottery tickets. There will be $$$m$$$ ($$$1 \le m \le \min{\left( 100, n \right)}$$$) draws. In the $$$j$$$-th ($$$1 \le j \le m$$$) draw:
Alice is the last (that is, the $$$n+1$$$-th) ticket buyer, and she wants to win the $$$k$$$-th prize ($$$1 \le k \le m$$$). Alice can buy between $$$1$$$ to $$$l$$$ ($$$1 \le l \le 2000$$$) lottery tickets (inclusive range). She wants to know, for each $$$x \in \{ 1, \dots, l \}$$$, what the probability for winning the $$$k$$$-th prize is if she buys exactly $$$x$$$ tickets.
Alice has figured out that the probability $$$f(x)$$$ of winning the first prize is given by $$$$$$ f(x) = \sum_{\substack{A \subseteq \{ 1, \dots, n\} \\ |A| \le m - 1}} \left( -1 \right)^{m - 1 - |A|} {{n - |A|}\choose{m - 1 - |A|}} \left( \frac{x}{x + \sum_{j \not\in A} t_j} \right) $$$$$$
Alice has asked you to figure out the rest.
The first line contains four space-separated integers $$$n, m, k, l$$$. The next line contains $$$n$$$ space-separated integers $$$t_1, \dots, t_n$$$.
For each $$$i \in \{1, \dots, l\}$$$, the probability that Alice wins the $$$k$$$-th prize if she buys $$$i$$$ tickets can be represented by $$$\frac{a_i}{b_i}$$$, where $$$a_i$$$ and $$$b_i$$$ are some integers.
The output will consist of $$$l$$$ lines. The $$$i$$$-th line will contain the unique integer $$$c_i$$$ satisfying $$$0 \le c_i \lt 998244353$$$ and $$$a_i \equiv b_i c_i \pmod{998244353}$$$.
3 1 1 41 2 3
855638017 748683265 332748118 199648871
You are simulating a classic Snake game on an $$$n \times n$$$ grid.
Grid
Cells are addressed by coordinates $$$(x, y)$$$, where:
The snake initially consists of a single cell:
Moves
You are given a string $$$s$$$ of length $$$k$$$. Each character represents a move:
Wrapping
The grid is toroidal. If the head moves outside the grid, it reappears on the opposite side:
Food
There are $$$m$$$ foods given in a fixed order at positions $$$(x_i, y_i)$$$.
Snake Movement
Each move is processed as follows:
Death Rule
The snake dies if after moving, its head occupies a cell that is currently part of its body. If the snake does not eat during this move, moving into the current tail cell is allowed, because the tail leaves at the same time.
Task
Simulate the game for at most $$$k$$$ moves.
3 10 3 LLDRRUURRD 1 3 2 3 3 3
ALIVE 4
3 5 3RDLLL1 22 22 1
DEAD
In the first sample, the grid is $$$3 \times 3$$$. The snake starts at $$$(1,1)$$$ with length $$$1$$$. Food $$$1$$$ is initially located at $$$(1,3)$$$.
The moves are: [ L, L, D, R, R, U, U, R, R, D ]
The snake survives all $$$10$$$ moves, so the answer is ALIVE 4.
In the second sample, the snake dies before all $$$k$$$ moves are performed.