You are given an integer interval $$$[l,r]$$$. At first, your score $$$sco$$$ is equal to $$$0$$$.
Repeat the following process:
Your task is to find the expected value of $$$sco$$$. Output the answer modulo $$$998\,244\,353$$$.
Formally, let $$$M = 998\,244\,353$$$. It can be shown that the 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 of each testcase contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 2 \cdot 10^6$$$).
For each test case, print a single integer — the answer modulo $$$998\,244\,353$$$.
42 31 15 81 2000000
51454755778881266578
In the first test case, the initial interval is $$$[2,3]$$$. The following events occur with equal probability:
Thus, the expected value of $$$sco$$$ is $$$\frac{4+5+5+6}{4}=5$$$.
You are given a tree with $$$n$$$ nodes, rooted at node $$$1$$$. Each node $$$i$$$ contains $$$k_i$$$ items, all having the same price $$$p_i$$$.
You need to process several queries. Each query is described by four integers: $$$u$$$, $$$d$$$, $$$p_{\text{lower}}$$$, and $$$p_{\text{upper}}$$$.
For each query, you must determine the number of items that satisfy all of the following conditions:
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of nodes in the tree.
Each of the next $$$n - 1$$$ lines contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le n$$$; $$$a \ne b$$$), denoting an edge between nodes $$$a$$$ and $$$b$$$.
Then $$$n$$$ lines follow. The $$$i$$$-th of these lines contains two integers $$$k_i$$$ and $$$p_i$$$ ($$$0 \le k_i \le 10^5$$$, $$$1 \le p_i \le 10^9$$$) — the number of items and their common price at node $$$i$$$.
Then an integer $$$q$$$ ($$$1 \le q \le 2 \cdot 10^5$$$) is given — the number of queries.
In the $$$i$$$'th line of the next $$$q$$$ lines contains four integers $$$U_i ~(1 \le U_i \le n)$$$, $$$D_i~(0 \le D_i \le n - 1)$$$, $$$PL_i$$$ and $$$PU_i~(1 \le PL_i, ~PU_i \le 10^9)$$$.
Let $$$ans_0 = 0$$$. For each query $$$i$$$, the actual parameters are decoded as follows: $$$$$$u = ((U_i + ans_{i-1}) \mod n) + 1,$$$$$$ $$$$$$d = (D_i + ans_{i-1}) \mod n,$$$$$$ $$$$$$p_{\text{lower}} = ((PL_i + ans_{i-1}) \mod 10^9) + 1,$$$$$$ $$$$$$p_{\text{upper}} = ((PU_i + ans_{i-1}) \mod 10^9) + 1.$$$$$$
If $$$p_{\text{lower}} \gt p_{\text{upper}}$$$, then these two values are swapped.
After decoding $$$1 \le u \le n$$$, $$$0 \le d \le n - 1$$$, $$$1 \le p_{\text{lower}} \le p_{\text{upper}} \le 10^9$$$.
After printing the answer to query $$$i$$$, it becomes $$$ans_i$$$, which is then used in decoding query $$$i+1$$$.
For each query, print a single integer — the number of items satisfying the conditions above.
101 22 33 44 51 61 73 82 93 103 48 58 83 88 70 80 11 94 54 1057 5 1 1010 5 7 102 1 10 108 7 5 102 5 7 10
20 0 0 0 8
You have an infinite-storey building. Each floor has exactly $$$n$$$ rooms, and each room can accommodate one person.
Placing one person on floor $$$x$$$ (where floors are $$$1$$$-indexed: $$$x=1,2,3,\dots$$$) costs $$$2^{x}$$$ units of budget. You are given a total budget of $$$m$$$ and you must spend all of it.
Your task is to determine the maximum possible number of people you can accommodate while spending exactly $$$m$$$. If it is impossible to spend the budget exactly, output $$$-1$$$.
The first line contains an integer $$$T$$$ $$$(1 \le T \le 10^5)$$$ — the number of test cases.
Each of the next $$$T$$$ lines contains two integers $$$n$$$ $$$(1 \le n \le 10^{18})$$$ and $$$m$$$ $$$(1 \le m \le 10^{18})$$$.
For each test case, print a single integer on its own line — the maximum number of people that can be accommodated while spending exactly $$$m$$$, or $$$-1$$$ if it is impossible.
25 101 30
54
For the first case: $$$n = 5$$$, $$$m = 10$$$. It is optimal to place 5 people on floor 1. Cost is $$$5\cdot2^1 = 10$$$ — exact, so the answer is $$$5$$$.
For the second case 2: $$$n = 1, m = 30$$$. At most one person can be accommodated per floor. If we place one person on floors $$$1, 2, 3, 4$$$ (cost $$$2 + 4 + 8 + 16 = 30$$$). Total people = $$$4$$$, which is maximal since each floor can host only one person.
Fahim had only been at CloseAI for two weeks, but he already knew two things with absolute certainty:
One morning, Saltman burst into the engineering floor holding a pile of printed stack traces like a kindergarten art project gone wrong.
"Tokenization is broken again!" he shouted. "If we don't fix this, our shareholders will replace me with a bag of potatoes!"
Fahim stared at him. A thought crossed his mind: If I fix this, maybe they'll replace him with me.
The challenge? Every model at CloseAI was panicking because they had forgotten how to tokenize words. They needed a clean implementation of tokenization, or as Saltman called it, "that spicy token cruncher thing".
So Fahim sat down, cracked his knuckles, and dove into the problem:
You're given $$$n$$$ lowercase English words, and you must apply tokenization to them, like a hero saving a collapsing startup. Each unique character from 'a' to 'z' starts as its own token — basic, simple, unlike Saltman's understanding of machine learning.
Then comes the tokenization loop:
Fahim has to count all adjacent token pairs across all words. He must find the most frequent adjacent token— the one that appears more than anything else. If there's a tie, he chooses the lexicographically smallest adjacent token. Then he merges that pair into a new token, like smashing two puzzle pieces together with pure engineering rage. Fahim also has to handle overlapping cases properly. For example, in a word like 'aaa', the pair 'aa' appears twice but overlaps — he only merges the leftmost occurrence first, so 'aaa' becomes ['aa', 'a'] in one merge step.
He repeats this: count, pick, merge, replace everywhere. He keeps going until there are no adjacent pairs of tokens. That's when the algorithm ends, and all the final tokens must be printed.
As he coded, something incredible happened: the errors stopped. The server logs went silent. The models started tokenizing again without screaming across the datacenter. Before anyone realized, the entire company was running smoothly.
Saltman strutted over and said, "Nice work, chat-bro. You've saved CloseAI."
Fahim smiled. One step closer to taking your job, Saltman.
A single line containing a string $$$s$$$ — one or more words separated by single spaces.
Each word is a valid lowercase English dictionary word consisting only of characters from a to z.
It is guaranteed that each word has length at most $$$10$$$ and the total number of words does not exceed $$$10^5$$$.
Print each unique token on a separate line in lexicographically sorted order.
cat bat rat
a at b bat c cat r rat t
In the first example:
Initial state:
Each character is a token: c, a, t, b, r.
Words: cat $$$\to$$$ [c, a, t], bat $$$\to$$$ [b, a, t], rat $$$\to$$$ [r, a, t]
Step 1: Count adjacent pairs
Frequencies: "at" appears 3 times, "ca", "ba", "ra" each appear 1 time.
Most frequent pair: "at" $$$\to$$$ merge (a, t) into token at.
After merge:
cat $$$\to$$$ [c, at], bat $$$\to$$$ [b, at], rat $$$\to$$$ [r, at]
Step 2: Count adjacent pairs
Frequencies: "cat", "bat", "rat" each appear 1 time (tie).
Lexicographically smallest: "bat" $$$ \lt $$$ "cat" $$$ \lt $$$ "rat" $$$\to$$$ merge (b, at) into token bat.
After merge:
cat $$$\to$$$ [c, at], bat $$$\to$$$ [bat], rat $$$\to$$$ [r, at]
Step 3: Count adjacent pairs
Frequencies: "cat", "rat" each appear 1 time (tie).
Lexicographically smallest: "cat" $$$ \lt $$$ "rat" $$$\to$$$ merge (c, at) into token cat.
After merge:
cat $$$\to$$$ [cat], bat $$$\to$$$ [bat], rat $$$\to$$$ [r, at]
Step 4: Count adjacent pairs
Only one pair: "rat" $$$\to$$$ merge (r, at) into token rat.
Final state:
cat $$$\to$$$ [cat], bat $$$\to$$$ [bat], rat $$$\to$$$ [rat]
All unique tokens: a, at, b, bat, c, cat, r, rat, t
Output in lexicographically sorted order.
The Metropolitan University campus has a long street with $$$n$$$ streetlights arranged in a row, numbered from $$$1$$$ to $$$n$$$. Each streetlight is either on (represented by $$$1$$$) or off (represented by $$$0$$$).
The streetlights have a peculiar automatic control system. Every minute, the system updates all streetlights simultaneously according to the following rule:
For a streetlight at position $$$i$$$ (where $$$1 \le i \le n$$$), we define its neighbors as all positions $$$j$$$ such that $$$|i - j| = 1$$$ and $$$1 \le j \le n$$$.
A streetlight at position $$$i$$$ (where $$$1 \le i \le n$$$) toggles its state (on $$$\to$$$ off or off $$$\to$$$ on) if and only if it has exactly two neighbors and both of them were on in the previous minute.
You are given the initial state of all streetlights. Your task is to determine the state of all streetlights after exactly $$$k$$$ minutes.
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 10^5$$$) — 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 $$$10^6$$$.
For each test case, print a single line containing a binary string of length $$$n$$$ — the state of the streetlights after $$$k$$$ minutes.
15 101010
01110
Alice and Bob have been close ever since high school. Now they both study at Martian University (MU), but in different departments located far apart. Their schedules are packed, and their classes take place in buildings scattered across the campus. Even though they want to spend time together, they can only meet during the short breaks between their classes.
The campus has $$$N$$$ buildings connected by $$$M$$$ undirected roads. Each road requires a certain amount of time to walk. Both Alice and Bob have several classes throughout the day, each class occurs at a specific building and lasts between a given start and end time. Class intervals do not overlap for each student. Since days on Mars do not follow Earth's fixed length, MU measures time on its own continuous scale, and a single day may be arbitrarily long. As a result, class start and end times can be large values.
Between two consecutive classes of their own, a student may roam the campus freely, traveling along any roads and visiting any buildings, as long as they arrive at their next class on time. Alice and Bob can meet only if they are in the same building at the same moment, during free periods for both. They are dedicated to their studies and will not be late to any class. They also work part time before and after their class schedules, so they will only meet between classes, never before the first class or after the last one.
Alice and Bob may choose different buildings to meet during different free intervals. Your task is to determine the maximum total time they can spend together during the day.
The first line contains one integer $$$t$$$ $$$(1 \le t \le 10)$$$, the number of test cases.
Each test case begins with a line containing two integers $$$N$$$ and $$$M$$$ $$$(1 \le N \le 200,\ 1 \le M \le \frac{N(N-1)}{2})$$$, where $$$N$$$ is the number of buildings and $$$M$$$ is the number of roads. Each of the next $$$M$$$ lines contains three integers $$$u, v, w$$$ $$$(1 \le u, v \le N,\ 1 \le w \le 10^5)$$$, describing a bidirectional road between buildings $$$u$$$ and $$$v$$$ with travel time $$$w$$$ minutes.
Then a line follows containing two integers $$$A$$$ and $$$B$$$, the number of classes Alice and Bob have, respectively. The next $$$A$$$ lines describe Alice's classes; each line contains three integers $$$start, end, loc$$$ $$$(1 \le start \lt end \le 10^{15},\ 1 \le loc \le N)$$$. After that, the next $$$B$$$ lines describe Bob's classes in the same format.
The sum of $$$A + B$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print a single integer — the maximum total time Alice and Bob can spend together.
14 51 2 31 3 42 3 23 4 12 4 12 21 10 120 30 21 10 120 30 3
6
You are given a tree consisting of $$$N$$$ nodes numbered from $$$1$$$ to $$$N$$$, connected by $$$N-1$$$ edges.
You are also given a list of $$$M$$$ distinct "special" nodes. The remaining $$$N-M$$$ nodes are "normal."
You can move between nodes along the tree edges.
For each node $$$i$$$ $$$(1 \le i \le N)$$$, determine the maximum number of distinct nodes that can be visited in a single continuous journey starting from node $$$i$$$.
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 2 \cdot 10^5$$$), the number of test cases. The description of each test case follows:
It is guaranteed that the sum of $$$N$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print $$$N$$$ space-separated integers, where the $$$i$$$-th integer is the answer for a starting journey from node $$$i$$$.
23 121 22 33 111 21 3
3 2 32 3 3
Test Case 1:
This is an interactive problem. You must flush the output after printing each line. For example, in C++ use fflush(stdout), in Java — System.out.flush(), in Pascal — flush(output), and in Python — sys.stdout.flush().
Two players, A and B, are playing on a pile of $$$N$$$ stones. Player A moves first, and then they alternate turns. On each move, a player may remove any number of stones from $$$1$$$ to $$$k$$$, but not more than the number currently in the pile. A player who cannot move loses.
The value of $$$k$$$ is an unknown integer satisfying $$$1 \le k \le 1000$$$. Your task is to determine this unknown value.
You may ask queries. A query is a single integer $$$N$$$ ($$$1 \le N \le 10^{18}$$$). For this $$$N$$$, the system simulates optimal play and replies with "A" if player A wins, or "B" if player B wins.
You may ask at most $$$70$$$ queries (not counting the final answer).
The first line contains a single integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of test cases.
Following lines will contain responses to your queries — "A" or "B". The $$$i$$$-th line is a response to your $$$i$$$-th query.
You can read a response only after printing a query and flushing the output.
To ask a query, output "? n" (without quotes), where $$$1 \le n \le 10^{18}$$$, followed by a flush operation.
When you determine the unknown value $$$k$$$, output "! k" (without quotes), where $$$1 \le k \le 1000$$$, flush the output, and continue to the next test case.
1 A B A
? 1 ? 765 ? 1000 ! 50
You are playing a 3D version of Fruit Ninja. A strange fruit appears — it is shaped like a tetrahedron, defined by $$$4$$$ points in 3d-space that do not lie on the same plane.
To get a perfect score, you must slice the fruit with a single plane so that the two pieces have exactly equal volume.
The fruit's vertices are:
$$$$$$P_1(x_1,y_1,z_1), P_2(x_2,y_2,z_2), P_3(x_3,y_3,z_3), P_4(x_4,y_4,z_4)$$$$$$
Your task is to output any plane that cuts the tetrahedron into two equal-volume parts.
A plane is written as: $$$ax + by + cz + d = 0$$$
A correct answer is guaranteed to exist, and if there are multiple such slicing planes, you may output any.
The first line contains a single integer $$$T~(T \le 10^4)$$$ — the number of test cases.
For each test case, the input contains four lines. Each line has three integers $$$x_i, y_i, z_i ~(|x_i|, |y_i|, |z_i| \le 10^4)$$$ — the coordinates of the tetrahedron's vertices $$$(P_1, P_2, P_3, P_4)$$$.
For each test case, print four real numbers $$$a, b, c, d$$$ — defining a plane $$$(ax + by + cz + d = 0)$$$ that splits the tetrahedron into two equal volumes. Any of $$$|a|,|b|,|c|,|d|$$$ are bounded by $$$10^{9}$$$.
Your answer is accepted if the absolute or relative error is less than or equal to $$$10^{-6}$$$.
2 0 0 0 0 0 2 0 2 0 2 1 1 1 2 6 2 4 3 2 5 9 1 7 9
0.47778 -3.00000 -2.40000 4.00000 -11.01667 -2.88333 -0.19444 31.45000
You are given an array $$$a$$$ of size $$$n$$$ consisting of non-negative integers. At first, your score is $$$0$$$.
You can perform the following operation:
You need to answer $$$q$$$ independent queries. For the $$$i$$$-th query, given a number $$$x_i$$$, you should find the maximum score you can obtain after performing exactly $$$x_i$$$ operations. Since the answer may be large, output the answer modulo $$$998\,244\,353$$$.
The first line contains an integer $$$t$$$ $$$(1 \le t \le 10^5)$$$, the number of test cases.
For each test case,the input consists of four lines:
It is guaranteed that the sum of $$$n$$$ and $$$q$$$ over all test cases will not exceed $$$2 \cdot 10^5$$$.
For each test case, output $$$q$$$ space-separated integers in a single line, representing the maximum score after performing $$$x_i$$$ operations, modulo $$$998\,244\,353$$$.
248 7 2 1221 26100000 0 99882 191 99699 487415 4 12 200000
15 40258200160 1100098 60800104 385867226
In the first test case,