knightnemo commited on
Commit
c730cb2
·
verified ·
1 Parent(s): 14c7991

Upload code_segments/segment_221.txt with huggingface_hub

Browse files
Files changed (1) hide show
  1. code_segments/segment_221.txt +23 -0
code_segments/segment_221.txt ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ This is the hard version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on $n$ and $t$. You can make hacks only if both versions of the problem are solved.
2
+
3
+ Consider a perfect binary tree with size $2^n - 1$, with nodes numbered from $1$ to $2^n-1$ and rooted at $1$. For each vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), vertex $2v$ is its left child and vertex $2v + 1$ is its right child. Each node $v$ also has a value $a_v$ assigned to it.
4
+
5
+ Define the operation $\mathrm{pop}$ as follows:
6
+
7
+ 1. initialize variable $v$ as $1$; 2. repeat the following process until vertex $v$ is a leaf (i.e. until $2^{n - 1} \le v \le 2^n - 1$); 1. among the children of $v$, choose the one with the larger value on it and denote such vertex as $x$; if the values on them are equal (i.e. $a_{2v} = a_{2v + 1}$), you can choose any of them; 2. assign $a_x$ to $a_v$ (i.e. $a_v := a_x$); 3. assign $x$ to $v$ (i.e. $v := x$); 3. assign $-1$ to $a_v$ (i.e. $a_v := -1$).
8
+
9
+ Then we say the $\mathrm{pop}$ operation is deterministic if there is a unique way to do such operation. In other words, $a_{2v} \neq a_{2v + 1}$ would hold whenever choosing between them.
10
+
11
+ A binary tree is called a max-heap if for every vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), both $a_v \ge a_{2v}$ and $a_v \ge a_{2v + 1}$ hold.
12
+
13
+ A max-heap is deterministic if the $\mathrm{pop}$ operation is deterministic to the heap when we do it for the first and the second time.
14
+
15
+ Initially, $a_v := 0$ for every vertex $v$ ($1 \le v \le 2^n - 1$), and your goal is to count the number of different deterministic max- heaps produced by applying the following operation $\mathrm{add}$ exactly $k$ times:
16
+
17
+ * Choose an integer $v$ ($1 \le v \le 2^n - 1$) and, for every vertex $x$ on the path between $1$ and $v$, add $1$ to $a_x$.
18
+
19
+ Two heaps are considered different if there is a node which has different values in the heaps.
20
+
21
+ Since the answer might be large, print it modulo $p$.
22
+
23
+ Each test con