CF1810G.The Maximum Prefix

NOI/NOI+/CTSC

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You're going to generate an array aa with a length of at most nn, where each aia_{i} equals either 11 or −1-1.

You generate this array in the following way.

  • First, you choose some integer kk (1≤k≤n1\le k \le n), which decides the length of aa.
  • Then, for each ii (1≤i≤k1\le i \le k), you set ai=1a_{i} = 1 with probability pip_{i}, otherwise set ai=−1a_{i} = -1 (with probability 1−pi1 - p_{i}).

After the array is generated, you calculate si=a1+a2+a3+…+ais_{i} = a_{1} + a_{2} + a_{3}+ \ldots + a_{i}. Specially, s0=0s_{0} = 0. Then you let SS equal to max⁡i=0ksi\displaystyle \max_{i=0}^{k}{s_{i}}. That is, SS is the maximum prefix sum of the array aa.

You are given n+1n+1 integers h0,h1,…,hnh_{0} , h_{1}, \ldots ,h_{n}. The score of an array aa with maximum prefix sum SS is hSh_{S}. Now, for each kk, you want to know the expected score for an array of length kk modulo 109+710^9+7.

你将生成一个长度至多为 nn 的数组 aa,其中每个元素 aia_{i} 的值为 11 或 −1-1。

该数组按如下方式生成:

  • 首先,你选择某个整数 kk(满足 1≤k≤n1\le k \le n),它决定数组 aa 的长度;
  • 然后,对每个 ii(满足 1≤i≤k1\le i \le k),以概率 pip_{i} 令 ai=1a_{i} = 1,否则以概率 1−pi1 - p_{i} 令 ai=−1a_{i} = -1。

数组生成完毕后,你计算前缀和 si=a1+a2+a3+…+ais_{i} = a_{1} + a_{2} + a_{3}+ \ldots + a_{i}。特别地,定义 s0=0s_{0} = 0。再令 S=max⁡i=0ksiS = \displaystyle \max_{i=0}^{k}{s_{i}},即 SS 是数组 aa 的最大前缀和。

现给定 n+1n+1 个整数 h0,h1,…,hnh_{0} , h_{1}, \ldots ,h_{n}。若数组 aa 的最大前缀和为 SS,则其得分为 hSh_{S}。现在,对每个 kk,你需要计算长度为 kk 的数组的期望得分(对 109+710^9+7 取模)。

输入格式

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤50001 \le t \le 5000) — the number of test cases. Their description follows.

The first line contains an integer nn (1≤n≤50001\le n \le 5000).

Then for the following nn lines, each line contains two integers xix_{i} and yiy_{i} (0≤xi<109+70 \le x_{i} \lt 10^9 + 7, 1≤yi<109+71\le y_{i} \lt 10^9 + 7, xi≤yix_{i} \le y_{i}), indicating pi=xiyip_{i} = \frac{x_{i}}{y_{i}}.

The next line contains n+1n+1 integers h0,h1,…,hnh_{0},h_{1}, \ldots, h_{n} (0≤hi<109+70 \le h_{i} \lt 10^9 + 7).

It is guaranteed that the sum of nn over all test cases does not exceed 50005000.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤50001 \le t \le 5000),表示测试用例的数量。随后是这些测试用例的描述。

第一行包含一个整数 nn(1≤n≤50001\le n \le 5000)。

接下来的 nn 行中,每行包含两个整数 xix_{i} 和 yiy_{i}(0≤xi<109+70 \le x_{i} \lt 10^9 + 7,1≤yi<109+71\le y_{i} \lt 10^9 + 7,且 xi≤yix_{i} \le y_{i}),表示 pi=xiyip_{i} = \frac{x_{i}}{y_{i}}。

下一行包含 n+1n+1 个整数 h0,h1,…,hnh_{0},h_{1}, \ldots, h_{n}(0≤hi<109+70 \le h_{i} \lt 10^9 + 7)。

保证所有测试用例中 nn 的总和不超过 50005000。

输出格式

For each test case, output nn integers in one single line, the ii-th of which denotes the expected score for an array of length ii, 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 p⋅q−1 mod Mp \cdot q^{-1} \bmod M. In other words, output such an integer xx that 0≤x<M0 \le x \lt M and x⋅q≡p(modM)x \cdot q \equiv p \pmod{M}.

对每个测试用例,在一行中输出 nn 个整数,其中第 ii 个整数表示长度为 ii 的数组的期望得分,结果对 109+710^9 + 7 取模。

形式化地,令 M=109+7M = 10^9 + 7。可以证明答案可表示为一个既约分数 pq\frac{p}{q},其中 pp 和 qq 为整数,且 q≢0(modM)q \not \equiv 0 \pmod{M}。请输出满足 p⋅q−1 mod Mp \cdot q^{-1} \bmod M 的整数。换言之,输出满足 0≤x<M0 \le x \lt M 且 x⋅q≡p(modM)x \cdot q \equiv p \pmod{M} 的整数 xx。

输入输出样例

  • 输入#1

    4
    2
    1 2
    1 2
    1 2 3
    3
    1 3
    1 4
    5 5
    1 1 1 1
    3
    2 5
    4 6
    0 2
    4 3 2 1
    5
    5 6
    5 7
    1 6
    1 3
    4 7
    9 0 4 5 2 4

    输出#1

    500000005 750000007 
    1 1 1 
    200000005 333333339 333333339 
    500000005 880952391 801587311 781746041 789304620

说明/提示

In the first test case, if we choose k=1k=1, there are 22 possible arrays with equal probabilities: [1][1] and [−1][-1]. The SS values for them are 11 and 00. So the expected score is 12h0+12h1=32\frac{1}{2}h_{0} + \frac{1}{2}h_{1} = \frac{3}{2}. If we choose k=2k=2, there are 44 possible arrays with equal probabilities: [1,1][1,1], [1,−1][1,-1], [−1,1][-1,1], [−1,−1][-1,-1], and the SS values for them are 2,1,0,02,1,0,0. So the expected score is 12h0+14h1+14h2=74\frac{1}{2}h_{0} + \frac{1}{4}h_{1} + \frac{1}{4}h_{2} = \frac{7}{4}.

In the second test case, no matter what the SS value is, the score is always 11, so the expected score is always 11.

在第一个测试用例中,若选择 k=1k=1,则存在 22 种等概率的可能数组:[1][1] 和 [−1][-1],它们对应的 SS 值分别为 11 和 00。因此期望得分为 12h0+12h1=32\frac{1}{2}h_{0} + \frac{1}{2}h_{1} = \frac{3}{2}。若选择 k=2k=2,则存在 44 种等概率的可能数组:[1,1][1,1]、[1,−1][1,-1]、[−1,1][-1,1]、[−1,−1][-1,-1],它们对应的 SS 值分别为 2,1,0,02,1,0,0。因此期望得分为 12h0+14h1+14h2=74\frac{1}{2}h_{0} + \frac{1}{4}h_{1} + \frac{1}{4}h_{2} = \frac{7}{4}。

在第二个测试用例中,无论 SS 取何值,得分恒为 11,因此期望得分恒为 11。

输入解题思路,AI测评打分。不知道怎么写?

首页