#P14446. [ICPC 2025 Xi'an Practice] Zelda

    ID: 16204 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2025概率论期望ICPC拉格朗日插值法西安

[ICPC 2025 Xi'an Practice] Zelda

题目描述

Zelda is given integers l,rl, r (lrl \leq r) and she has two sequences aa and bb of length nn, indexed from 11 to nn. Zelda will use these two sequences to play a game, which proceeds as follows:

  • Uniformly and randomly select an integer xx from the interval [l,r][l, r].
  • For i=1,2,,ni = 1, 2, \cdots, n, execute the following operations in order: first let xmin(x,ai)x \gets \min(x, a_i), then let xmax(x,bi)x \gets \max(x, b_i).

Zelda wants to know the expected value of the final xx.

Unfortunately, Zelda has forgotten some of the integers in sequences aa and bb, so she wants you to calculate the expected value of the final xx when all forgotten integers are also uniformly and randomly selected from the interval [l,r][l, r], modulo 109+710^9 + 7. Since Zelda will play the game multiple times, you need to answer queries for multiple different values of rr.

输入格式

The first line of the input contains three integers n,q,ln, q, l (1n,l2001 \leq n, l \leq 200, 1q5×1041 \leq q \leq 5 \times 10^4), indicating the length of the two sequences, the number of times Zelda plays the game, and the lower bound for uniform random selections.

The next line of the input contains nn non-negative integers a1,a2,,ana_1, a_2, \cdots, a_n (ai=0a_i = 0 or lai200l \leq a_i \leq 200). ai=0a_i = 0 means the integer was forgotten by Zelda.

The next line of the input contains nn non-negative integers b1,b2,,bnb_1, b_2, \cdots, b_n (bi=0b_i = 0 or lbi200l \leq b_i \leq 200). bi=0b_i = 0 means the integer was forgotten by Zelda.

The next line of the input contains qq integers r1,r2,,rqr_1, r_2, \cdots, r_q (lri109l \leq r_i \leq 10^9), indicating the value of rr each time Zelda plays a game.

输出格式

The output contains qq lines, where the ii-th line contains a single integer representing the answer for the ii-th game, modulo 109+710^9 + 7.

Formally, let M=109+7M = 10^9 + 7. It can be shown that the answer can be expressed as an irreducible fraction pq\frac{p}{q}, where pp and qq are integers and q≢0(modM)q \not \equiv 0 \pmod M. Output the integer equal to pq1(modM)p \cdot q^{-1} \pmod M. In other words, output such an integer xx that 0x<M0 \leq x < M and xqp(modM)x \cdot q \equiv p \pmod M.

2 5 1
2 2
0 1
1 2 3 50 538
1
750000007
888888897
857200008
228862927
3 5 2
3 3 0
3 0 0
2 3 4 50 519
2
750000008
962962973
798646858
311741862

提示

In the first example, sequences a=[2,2]a = [2, 2] and b=[0,1]b = [0, 1] (where b1=0b_1 = 0 indicates a forgotten value), and there are q=5q = 5 queries.

In the first query, l=r=1l = r = 1, so x=b1=1x = b_1 = 1 always. The game process is as follows:

  • Initially, x=1x = 1.
  • Do operations with i=1i = 1, xmin(x,a1)=1x \gets \min(x, a_1) = 1 first, and then xmax(x,b1)=1x \gets \max(x, b_1) = 1.
  • Do operations with i=2i = 2, xmin(x,a2)=1x \gets \min(x, a_2) = 1 first, and then xmax(x,b2)=1x \gets \max(x, b_2) = 1.

So the answer for the first query is 11.

In the second query, l=1,r=2l = 1, r = 2, so x,b1x, b_1 should be selected from [1,2][1, 2].

  • x=1,b1=1x = 1, b_1 = 1, xx becomes 11 after all operations.
  • x=1,b1=2x = 1, b_1 = 2, xx becomes 22 after all operations.
  • x=2,b1=1x = 2, b_1 = 1, xx becomes 22 after all operations.
  • x=2,b1=2x = 2, b_1 = 2, xx becomes 22 after all operations.

Each case has probability 14\frac{1}{4}, so the answer for the second query is 14×(1+2+2+2)=74\frac{1}{4} \times (1 + 2 + 2 + 2) = \frac{7}{4}.