#P3284. [SCOI2014] 方伯伯打扑克

[SCOI2014] 方伯伯打扑克

Description

Uncle Fang has some blank playing cards. He wants to use them to play a math game.

He first decides to form mm piles from these blank cards, with the size of each pile being an integer power of 22. Specifically, the ii-th pile (note: indexed from 00) will have 2ni2^{n_i} cards. He first decides that pile 00 has 2n02^{n_0} cards, and then labels the cards in this pile from top to bottom with the decimal numbers 12n01 \sim 2^{n_0} in order.

Before starting the game, Uncle Fang decides to shuffle, and he decides to shuffle x0x_0 times. He has a fixed shuffling pattern: each shuffle is equivalent to the following two steps:

  1. Take out all cards in odd positions (counted from the top) in order to form a new pile.
  2. Place this new pile on top of the remaining old pile.

For example, when n0=3n_0 = 3, pile 00 starts from top to bottom as 12345678'', after one shuffle it becomes 13572468'', and after two shuffles it becomes ``15263748''.

After shuffling, Uncle Fang decides to add a number t0t_0 to the labels of the cards from the l0l_0-th to the r0r_0-th from top to bottom. He then converts these labels to binary and XORs them in order to obtain a value; he records the value modulo 2n012^{n_0-1} as ans0\mathrm{ans}_0.

Similarly, Uncle Fang will play with the remaining m1m-1 piles in the same way. Specifically, he decides to operate on each pile according to the following formulas:

  1. For the ii-th pile, the pile will have 2ni2^{n_i} cards, labeled from top to bottom with the decimal integers 12ni1 \sim 2^{n_i}. Here, $n_i = (\mathrm{ans}_{i-1} + i - 1) \bmod 5 \mathrel{+} \mathrm{Base}$, where Base\mathrm{Base} is a positive integer decided in advance.
  2. He first decides the position of the first card he will look at, denoted by lil_i, where $l_i = (2\mathrm{ans}_{i-1} + l_{i-1} + i - 1) \bmod 2^{n_i} \mathrel{+} 1$.
  3. He then decides the position of the last card he will look at, denoted by rir_i, where $r_i = \bigl(\mathrm{ans}_{i-1} + 1 + 2^{\lfloor n_i/2 \rfloor}(l_i \bmod 2^{\lfloor n_i/2 \rfloor})\bigr) \bmod 2^{n_i} \mathrel{+} 1$.
  4. Since the above two formulas are not simple, it is possible that li>ril_i > r_i. In that case, swap their values.
  5. After deciding which cards to look at, he decides to shuffle xix_i times, where xi=(rili+ti1+i1)mod2nix_i = (r_i - l_i + t_{i-1} + i - 1) \bmod 2^{n_i}.
  6. He also decides the number tit_i to add to each selected card, where ti=(li+ri)mod2nit_i = (l_i + r_i) \bmod 2^{n_i}.
  7. After shuffling, he adds tit_i to each card from the lil_i-th to the rir_i-th from top to bottom, converts them to binary and XORs them in order to obtain a value; he records the value modulo 2ni12^{n_i-1} as ansi\mathrm{ans}_i, and then returns to Step 1 to play with the next pile.

Uncle Fang has heard about your outstanding informatics skills. He wants to know whether you can compute in advance the game result ansm1\mathrm{ans}_{m-1} for the last pile, i.e., pile m1m-1. Can you do it?

Input Format

The first line contains an integer mm, the number of piles.

The next line contains six integers: n0,x0,l0,r0,t0,Basen_0, x_0, l_0, r_0, t_0, \mathrm{Base}.

Output Format

Output a single integer, the final answer.

2
5 1 4 27 3 15
2700

Hint

Constraints

  • For 10%10\% of the testdata, m100m \le 100.
  • For 20%20\% of the testdata, m5×105m \le 5\times 10^5, ni20n_i \le 20, Base16\mathrm{Base} \le 16.
  • For 60%60\% of the testdata, m5×105m \le 5\times 10^5, ni60n_i \le 60, Base55\mathrm{Base} \le 55.
  • For all testdata, $m \le 5\times 10^6,\ n_i \le 60,\ 0 < l_i \le r_i \le 2^{n_i},\ 0 < x_i, t_i < 10^9,\ \mathrm{Base} \le 55$.

Sample Explanation ans0=1\mathrm{ans}_0 = 1, n1=16n_1 = 16, l1=7l_1 = 7, r1=1795r_1 = 1795, x1=1791x_1 = 1791, t1=1802t_1 = 1802.

Translated by ChatGPT 5