Last night, you had a strange dream. In it, you discovered a mysterious function $$$f(n)$$$ defined over integers. Upon waking up, the definition was still clear in your mind:
$$$$$$ f(n) = \begin{cases} n + 20 & \text{if } n \lt 48, \\ f\big(f(n - 21)\big) & \text{if } n \geq 48. \end{cases} $$$$$$
Curious about what this function actually computes, you decide to evaluate it for various values of $$$n$$$.
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases. Each of the next $$$t$$$ lines contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^{18}$$$).
For each test case, output a single integer — the value of $$$f(n)$$$.
Each answer should be printed on its own line.
4 1 2 25 48
21 22 45 67
This is an interactive problem.
Alice and Bob are playing a game on a $$$k$$$-dimensional Boolean cube. Its vertices are all binary strings of length $$$k$$$. There is an edge between two vertices if their strings differ at exactly one position.
Initially, the token is at the vertex $$$00\ldots0$$$. Alice and Bob make moves alternately. In one move, a player must choose one bit and flip it. In other words, from the current vertex, the token can be moved to any vertex connected by an edge.
It is forbidden to move the token to a vertex that has already been visited(the initial vertex $$$00\ldots0$$$ counts as visited). The player who cannot make a move loses.
You may choose whether you want to play as Alice or as Bob. After that, you have to play the game and win.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows.
The only line of each test case contains a single integer $$$k$$$ ($$$3 \le k \le 10$$$) — the dimension of the Boolean cube.
Then interaction follows.
First, you need to output one of the following lines:
After that, the game starts at the vertex $$$00\ldots 0$$$.
If you chose Alice, you should make the first move. If you chose Bob, the jury will make the first move.
To make a move, output a single integer $$$i$$$ ($$$1 \le i \le k$$$) — the position of the bit you want to flip.
After each move of the jury, you should read a single integer $$$i$$$ ($$$1 \le i \le k$$$) — the position of the bit flipped by the jury.
If you cannot make a move, you should print $$$0$$$. The interactor will print $$$-1$$$ and finish the interaction.
If the jury cannot make a move and loses, you should read $$$0$$$ and proceed to the next test case.
After printing each move, do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded verdict.
If, at any interaction step, you read $$$-1$$$, your solution must terminate immediately. This means that your solution has made an invalid move, you lost, or some other error happened. Failing to terminate can result in an arbitrary verdict.
The jury program is adaptive, meaning it's behavior might be different on the same test based on the participant's choices.
1 3 3 1 2 0
Bob 2 3 3
Please note that the sample test does not have to provide the optimal strategy and just shows an example of the complete interaction where the program wins the game and passes the test.
You wrote a contest where you competed against $$$n$$$ other participants, and you were given $$$p$$$ problems to solve.
You solved exactly $$$x$$$ problems. For each problem $$$i$$$, you know that it was solved by exactly $$$k_i$$$ of your competitors.
You do not know which participants solved which problems. Find the minimum possible number of participants who solved strictly more problems than you.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows.
The first line of each test case contains three integers $$$n$$$, $$$p$$$, and $$$x$$$ ($$$1 \le n, p \le 10^6$$$, $$$0 \le x \le p$$$) — the number of your competitors, the number of problems, and the number of problems you solved.
The second line of each test case contains $$$p$$$ integers $$$k_1, k_2, \ldots, k_p$$$ ($$$0 \le k_i \le n$$$), where $$$k_i$$$ is the number of other competitors who solved problem $$$i$$$.
It is guaranteed that the sum of $$$n$$$ and the sum of $$$p$$$ over all test cases do not exceed $$$10^6$$$.
For each test case, output a single integer — the minimum possible number of participants who could have solved strictly more problems than you.
42 4 12 2 2 25 2 10 14 5 34 4 3 3 06 6 34 4 4 3 3 4
2022
In the first test, both of your competitors solved all $$$4$$$ problems, but you solved just one, so you are definitely behind both of them.
In the fourth test case, you could have lost to $$$2$$$ competitors if they solved the following sets of problems:
It can be shown that there is no more optimal solution.
As a waterpark director, you have to plan the rides. You already know that your rides will look like a directed graph with $$$n$$$ vertices and $$$m$$$ edges. However, you don't know yet how long each of the edges will be, which is determined by the edge's weight. Based on the materials you have, each weight should be one of the digits $$$d_1, d_2, \ldots, d_k$$$.
The weight of a directed path is the sum of the digits assigned to the edges of the path (edges in a path are allowed to repeat).
You know that if there is a directed path of weight $$$w$$$ divisible by $$$67$$$, it will become the only ride used, and you want to avoid that. Provide the assignment, where there is no path with weight divisible by 67, or determine there is no such assignment.
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 200000$$$).
The second line contains a non-empty string $$$s$$$ consisting of distinct characters from 1 to 9. These are the allowed digits $$$d_i$$$ ($$$1 \le d_i \le 9$$$).
Each of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$). This means that there is a directed edge from $$$u_i$$$ to $$$v_i$$$. Multiple edges and self-loops are allowed.
If there is no valid assignment, print NO.
Otherwise, print YES on the first line.
On the second line print $$$m$$$ integers $$$a_1, a_2, \ldots, a_m$$$, where $$$a_i$$$ is the digit assigned to the $$$i$$$-th edge. Each $$$a_i$$$ must be one of the allowed digits.
If there are several valid assignments, print any of them.
2 131 2
YES 3
4 36791 21 33 4
YES 6 7 7
3 31231 22 33 1
NO
In an orienteering competition, a course is described by a sequence of controls. A competitor must visit the controls in the given order. Each control is denoted by a unique positive integer. The pair of adjacent controls in a sequence is called a leg.
For a mass-start race, course setters often make the race pass in two laps. Different competitors may receive different combinations of parts of these laps, so that they can't simply follow each other. Such variations are made in such way that after both laps every runner completed the same set of legs, but did it in a different order.
You are given two sequences of controls, which describe two laps of one of the runners. It is guaranteed that:
Given two laps for one of the competitors, determine the total number of variations possible for the race. Each variation should respect the given constraints, and union of the laps should have the same set of legs.
Compute the number of possible variations modulo $$$998244353$$$.
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n, m \le 200000$$$) — the lengths of the two laps.
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the control points of the first lap.
The third line contains $$$m$$$ integers $$$b_1, b_2, \dots, b_m$$$ ($$$1 \le b_i \le 10^9$$$) — the control points of the second lap.
It is guaranteed that:
Output a single integer — the number of valid variations modulo $$$998244353$$$.
5 61 2 4 5 81 3 4 6 7 8
4
2 36 76 1 7
2
Possible variations for the sample test are:
Maximilien is a student officer at Polytechnique and is preparing for the traditional $$$\textit{défilé du 14 juillet}$$$ on the Champs-Élysées. However, Maximilien prefers chaos over order, and thus plans to organize a prank that will definitely make an impression on the big day!
For the prank, Maximilien settles on the following idea: for each row of students parading, he will choose some number $$$a_i$$$ of students in that row to wear their hats backward.
Not satisfied with this alone, Maximilien adds an extra constraint: for every student wearing their hat backward, the immediate neighbors in the same row must wear their hats straight. More precisely, if such a student has a neighbor directly to their left in the row, that neighbor must wear their hat straight; similarly, if they have a neighbor directly to their right in the row, that neighbor must also wear their hat straight.
As a loyal comrade of Maximilien and a zealous mathematician, you decide to help him determine how many different valid configurations of hats for each row satisfy this rule. As this number can be large, output it modulo $$$10^9+7$$$.
The input consists of the following lines: $$$\cdot$$$ The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 10^{5}$$$), the dimensions of the parade block (there are $$$n$$$ rows, each containing $$$m$$$ students). $$$\cdot$$$ Each of the next $$$n$$$ lines contains a single integer $$$a_i$$$ ($$$0 \leq a_i \leq m$$$), the number of students in row $$$i$$$ who must wear their hats backward.
Output $$$n$$$ integers in $$$n$$$ different lines where the $$$i$$$-th integer represents the number of valid hat configurations for the $$$i$$$-th row that satisfy all the given constraints. As this number can be large, output it modulo $$$10^9+7$$$.
3 3 0 1 2
1 3 1
A certain species of space invaders were found on the streets of Paris, taking a mosaic form. Each invader has a fixed shape represented by the following $$$8 \times 13$$$ pattern:
| #..#..#..#..# |
| #.#########.# |
| ###.#####.### |
| ###.#####.### |
| #####...##### |
| ..#########.. |
| ..###...###.. |
| .##....##.... |
Each # denotes an occupied cell, and each . denotes empty cell.
For a positive integer $$$k$$$, a scaled invader is obtained by replacing every cell of the pattern with a $$$k \times k$$$ block. Occupied cells become filled $$$k \times k$$$ blocks, and empty cells become empty $$$k \times k$$$ blocks.
You want to code an «invader detector». Your program will be given a photo of a grid that may or may not contain space invaders. You need to determine if there exists such configuration of space invaders that:
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 4000$$$) — dimensions of the grid.
Each of the next $$$n$$$ lines contains a row of a grid, that has exactly $$$m$$$ characters, each being "#" or ".", that represent occupied or empty cell correspondingly.
Print YES if the photo is valid, otherwise print NO.
8 13#..#..#..#..##.#########.####.#####.######.#####.########...#####..#########....###...###...##....##....
YES
4 5.#.#.#.#.#.#.#.#.#.#
NO
8 30..#..#..#..#..#..#..#..#..#..#..#.#########.#..#.#########.#..###.#####.###..###.#####.###..###.#####.###..###.#####.###..#####...#####..#####...#####....#########......#########......###...###......###...###.....##....##.......##....##....
YES
9 16#######..#########.......#########...........##.##..........##..######.....##...#######...##....##...##..##.....##...##.##......#########.......
NO
The first sample matches the space invader pattern with scaling parameter $$$k=1$$$.
Please note the constraints of the problems are quite IO heavy. Jury's Python solution uses
sys.stdin.buffer.read().split()and stores the grid in one dimensional arrays.
You are given a clock where a day consists of $$$h$$$ hours and every hour consists of $$$m$$$ minutes. The time is written in the format $$$a:b$$$ ($$$0 \le a \lt h$$$, $$$0 \le b \lt m$$$). Note that the leading zeros are preserved. So for $$$h = 240$$$, $$$m=100$$$, $$$a=9$$$, $$$b=41$$$ the time would look like 009:41.
We call a time a:b nice if both of the following values are perfect squares$$$^{\text{∗}}$$$:
More formally, let $$$d$$$ be the number of digits in $$$m-1$$$. Then define: $$$\text{concat}(a, b) = a \cdot 10^d + b$$$
For example, for $$$h=24$$$, $$$m=60$$$, the time $$$1:21$$$ is nice, because:
Your task is to determine how many nice times there are in a day.
$$$^{\text{∗}}$$$A perfect square is an integer that is the square of another integer.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows.
The only line of each test case contains two integers $$$h$$$ and $$$m$$$ ($$$2 \le h, m \le 5 \cdot 10^{5}$$$).
It is guaranteed that the sum of $$$h$$$ and the sum of $$$m$$$ across all test cases do not exceed $$$5 \cdot 10^{5}$$$.
For each test case, output a single integer — the number of nice times in the corresponding day.
724 6024 9124 9924 10090000 9000010 400000400000 10
101110493276402000
$$$10$$$ nice times for the first test case: 00:00, 00:01, 00:04, 00:09, 00:16, 00:25, 00:36, 00:49, 01:21, 20:25
High above the valleys, the legendary Mount Thalassa feeds a vast river system. From a single source at the summit, the river splits into smaller and smaller streams, forming a tree-like structure flowing down the mountain.
Along these waterways lie isolated fishing spots. The fishing spots correspond exactly to the nodes of this tree. Each spot produces a fixed amount of fish every year. Because of the rugged terrain, these locations are difficult to access, and the transport infrastructure is limited to what was built long ago: downstream rails that allow fish to be transported downriver, and ziplines that allow fish to be transported upriver.
Each transport connection has a maximum yearly capacity.
Two rival companies, AquaDyn Fisheries and BlueCurrent Co., have been instructed to split the mountain's fishing resources evenly.
They agree on the following:
Fishing spots produce a fixed amount of fish, but they can store and handle any amount of incoming fish.
After choosing a valid repartition of the fishing spots, the two companies compare the total yearly fish production of their respective regions.
Your task is to determine a valid repartition of the fishing spots so that the two companies can end up with equal total amounts of fish, while minimizing the amount of fish that needs to be transferred between them. The transfer can only be done on integer quantities.
If multiple choices are possible, any one minimizing the required transfer is acceptable.
The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$), the number of fishing spots.
The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i \le 10^9$$$ is the yearly fish production of spot $$$i$$$.
The following $$$n-1$$$ lines describe the transport network. Each line contains four integers: $$$u \ v \ c_{uv} \ c_{vu}$$$
It is guaranteed that the graph formed by the fishing spots and connections is a tree.
Output a single integer — the minimum amount of fish that must be transferred between the two companies after choosing an optimal repartition.
If it is impossible to balance the total amount of fish for any valid repartition, output $$$-1$$$.
23 31 2 0 0
0
21 91 2 5 0
-1
21 91 2 0 5
4
In an abandoned industrial complex, represented by a grid of $$$M$$$ rows and $$$N$$$ columns, each cell $$$(i, j)$$$ has an altitude $$$h_{i,j}$$$. A toxic gas begins to fill the complex. If the gas reaches a height $$$H$$$, all cells whose altitude is strictly less than $$$H$$$ become impassable ($$$h_{i,j} \lt H$$$). Cells where $$$h_{i,j} \ge H$$$ remain safe.
You are initially at cell $$$(1, 1)$$$ (top left) and must reach the exit located at cell $$$(M, N)$$$ (bottom right). You can move horizontally or vertically between two adjacent cells, provided that both cells are passable.
Your goal is to determine the maximum value of $$$H$$$ such that there still exists a path between the entrance and the exit. Note that the entrance and the exit themselves must also be passable.
The first line contains two integers $$$M$$$ and $$$N$$$ ($$$1 \le M, N \le 500$$$). The next $$$M$$$ lines each contain $$$N$$$ integers $$$h_{i,j}$$$ ($$$0 \le h_{i,j} \le 10^9$$$).
A single integer: the maximum gas height $$$H$$$.
3 310 5 82 3 1215 10 10
5
You are moving from one apartment to another.
There are $$$n$$$ caps and $$$n$$$ pairs of sneakers in the old apartment. You also have a suitcase of size $$$W$$$ that you can use to transport caps and sneakers.
For every integer $$$i$$$ from $$$1$$$ to $$$W-1$$$, consider the following moving scenario:
In each scenario, you may make several trips from the old apartment to the new one. After every such trip except the last one, you return back to the old apartment.
During every trip from the old apartment to the new one:
During every return trip from the new apartment to the old one you must wear exactly one pair of sneakers and you carry the empty suitcase back. You don't have to wear the cap, so if you had one on during the forward trip, you can leave it at the new apartment.
Let $$$f(i)$$$ be the minimum possible number of trips from the old apartment to the new one required to move all $$$n$$$ caps and all $$$n$$$ pairs of sneakers to the new apartment in the scenario where cap has size $$$i$$$.
Your task is to compute $$$\sum_{i=1}^{W-1} f(i)$$$ modulo $$$998244353$$$.
The only line contains two integers $$$n$$$ and $$$W$$$ ($$$2 \le n,\ W \le 5 \times 10^{17}, n \times W \le 10^{18}$$$)
Print one integer: the value of $$$\sum_{i=1}^{W-1} f(i)$$$ modulo $$$998244353$$$.
3 5
8
5 5
14
67 69
3602
For the first sample test, 2 forward trips for each of 2 scenarios is achievable: transferring 1 cap + 1 sneakers pair per trip in a suitcase.
For the second sample test, $$$f(1) = 4,\ f(2) = 4,\ f(3) = 3,\ f(4) = 3$$$. For scenarios with answer 4, you can transfer 1 cap + 1 sneakers pair per trip in a suitcase. For $$$i = 3$$$ and $$$i = 4$$$ you can transfer one suitcase with 2 sneakers pairs and wear 1 cap that you keep at the new apartment. In the next trips you transfer 1 cap + 1 sneakers pair twice.
For both of described tests, 1 sneakers pair and 1 cap is worn during the last trip, and so is considered delivered.
Alice and Bob play a game on an undirected tree. The players start at two different nodes of the tree. The players take turns and Alice plays first. At their turn, the player must move to an empty adjacent node and can optionally decide to destroy the node they just left.
Destroyed nodes can no longer be used by the players. Note also that the tree can become a forest when nodes are destroyed.
If the two players end up in two different connected components, the game immediately ends and the player that is in the biggest component wins (if the two components are of the same size, this is considered a draw). If a player cannot move, he or she loses. Assuming both players play optimally, given the tree and the starting nodes of each player, who will win?
The first three lines contain the following integers:
A single line with either:
4141 22 33 4
BOB WINS
6161 42 42 34 55 6
DRAW
Explanation of sample 1. After Alice's first move, she will be at node 2. Then Bob moves to node 3. Then Alice can only move to node 1 (unless she already destroyed it in which case she loses), and can either leave node 2 intact (in which case, Bob moves to 2 and wins by blocking Alice), or destroy node 2 (in which case, Alices ends up in the connected component {1} which is smaller than Bob's connected component {3, 4}, so she loses).
Explanation of sample 2. Alice moves from 1 to 4, then Bob moves from 6 to 5, then Alice move from 4 to 2 and destroys node 4. This disconnects the two players that are both in a connected component of size 2.