CF2180G.Balance
NOI/NOI+/CTSC
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You have an array a which is initially empty. You have to process q queries of the following types on your array.
- Type 1: Erase the middle element of a. Formally, if a has n elements, remove the ⌈2n⌉-th∗ element of a and concatenate the remaining parts. It's guaranteed that a is non-empty whenever you are given a query of this type.
- Type 2: Insert x at the beginning, the end, and between every two consecutive elements of a. Formally, if a=[a1,a2,…,an] before this query, a will become [x,a1,x,a2,x,…,x,an,x] after the query.
- Type 3: Output the sum of the balances of all 2L−1 non-empty subsequences† of a, where L is the length of a at this moment. It is guaranteed that L will not be a multiple of 109+7 whenever this query is processed.
The balance of an array b=[b1,b2,…,bm] is defined as $$ \text{balance}(b) = \frac{1 \cdot b_1 + 2 \cdot b_2 + \ldots + m \cdot b_m}{m}. $$
∗⌈k⌉ is the k rounded up, i. e. the smallest integer greater than or equal to k.
†A sequence b is a subsequence of a sequence a if b can be obtained from a by the deletion of several (possibly, zero or all) element from arbitrary positions. Two subsequences are considered different if the sets of positions of the deleted elements are different.
你有一个初始为空的数组 a。你需要对这个数组执行 q 个查询,查询类型如下:
- 类型 1:删除 a 的中间元素。形式化地,若 a 当前有 n 个元素,则删除其第 ⌈2n⌉ 个∗元素,并将剩余两部分拼接起来。保证每次给出此类查询时,a 非空。
- 类型 2:在 a 的开头、结尾以及每两个相邻元素之间插入 x。形式化地,若查询前 a=[a1,a2,…,an],则查询后 a 变为 [x,a1,x,a2,x,…,x,an,x]。
- 类型 3:输出 a 的所有 2L−1 个非空子序列† 的“平衡值”之和,其中 L 是当前 a 的长度。保证在执行该查询时,L 不是 109+7 的倍数。
数组 b=[b1,b2,…,bm] 的平衡值定义为
balance(b)=m1⋅b1+2⋅b2+…+m⋅bm.
∗ ⌈k⌉ 表示对 k 向上取整,即不小于 k 的最小整数。
† 若序列 b 可通过从序列 a 中任意位置删除若干(可能为零个或全部)元素得到,则称 b 是 a 的一个子序列。若两个子序列所删除元素的位置集合不同,则认为它们是不同的子序列。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains a single integer q (2≤q≤106), denoting the number of queries. The next q lines describe the queries:
- Each query of type 1 is in the form 1, which means to erase the middle element of a. It's guaranteed that a is non-empty whenever you are given a query of this type.
- Each query of type 2 is in the form 2 x, where x is an integer to be inserted as described and 1≤x≤109.
- Each query of type 3 is in the form 3, which means to output the sum of the balances of all non-empty subsequences of a. It is guaranteed that the length of a will not be a multiple of 109+7 whenever this query is processed.
It is also guaranteed that each test case contains at least one query of the third type, and that the total sum of q over all test cases does not exceed 106.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 q(2≤q≤106),表示查询次数。接下来的 q 行描述这些查询:
- 类型 1 的查询格式为
1,表示删除数组 a 的中间元素。保证在执行此类查询时 a 非空。 - 类型 2 的查询格式为
2 x,其中 x 是待插入的整数,且满足 1≤x≤109。 - 类型 3 的查询格式为
3,表示输出数组 a 的所有非空子序列的“平衡值”之和。保证在执行此类查询时,a 的长度不为 109+7 的倍数。
此外,保证每个测试用例中至少包含一个类型 3 的查询,且所有测试用例的 q 总和不超过 106。
输出格式
For each query of type 3, output the desired sum of balances.
Formally, let M=109+7. It can be shown that the exact answer can be expressed as an irreducible fraction QP, where P and Q are integers and Q≡0(modM). Output the integer equal to P⋅Q−1modM. In other words, output such an integer x that 0≤x<M and x⋅Q≡P(modM).
对于每个类型为 3 的查询,输出所要求的余额总和。
形式化地,令 M=109+7。可以证明,精确答案可表示为既约分数 QP,其中 P 和 Q 为整数,且 Q≡0(modM)。请输出整数 P⋅Q−1modM。换言之,请输出满足 0≤x<M 且 x⋅Q≡P(modM) 的整数 x。
输入输出样例
输入#1
2 5 2 1 2 2 3 1 3 10 2 33532 1 2 938556 2 559503 2 353115 3 1 3 1 3
输出#1
833333355 7 856804480 553793656 724179701
说明/提示
In the first test case, the array a is initially empty.
After the first query, the array becomes [1].
After the second query, the array becomes [2,1,2].
In the third query, the subsequences of a and their balances are as follows:
- Subsequence [1] with balance $$\frac{1 \cdot 1}{1} = 1;$$
- Two subsequences [2], each having balance $$\frac{1 \cdot 2}{1} = 2;$$
- Subsequence [1,2] with balance $$\frac{1 \cdot 1 + 2 \cdot 2}{2} = \frac{5}{2};$$
- Subsequence [2,1] with balance $$\frac{1 \cdot 2 + 2 \cdot 1}{2} = 2;$$
- Subsequence [2,2] with balance $$\frac{1 \cdot 2 + 2 \cdot 2}{2} = 3;$$
- Subsequence [2,1,2] with balance $$\frac{1 \cdot 2 + 2 \cdot 1 + 3 \cdot 2}{3} = \frac{10}{3}.$$
Thus, the total sum of balances is: $$\frac{95}{6}.$$
After the fourth query, the array a becomes [2,2].
在第一个测试用例中,数组 a 初始为空。
执行第一个查询后,数组变为 [1]。
执行第二个查询后,数组变为 [2,1,2]。
在第三个查询中,数组 a 的所有子序列及其对应的平衡值如下:
- 子序列 [1] 的平衡值为 $$\frac{1 \cdot 1}{1} = 1;$$
- 两个子序列 [2],每个的平衡值均为 $$\frac{1 \cdot 2}{1} = 2;$$
- 子序列 [1,2] 的平衡值为 $$\frac{1 \cdot 1 + 2 \cdot 2}{2} = \frac{5}{2};$$
- 子序列 [2,1] 的平衡值为 $$\frac{1 \cdot 2 + 2 \cdot 1}{2} = 2;$$
- 子序列 [2,2] 的平衡值为 $$\frac{1 \cdot 2 + 2 \cdot 2}{2} = 3;$$
- 子序列 [2,1,2] 的平衡值为 $$\frac{1 \cdot 2 + 2 \cdot 1 + 3 \cdot 2}{3} = \frac{10}{3}.$$
因此,所有子序列平衡值的总和为: $$\frac{95}{6}.$$
执行第四个查询后,数组 a 变为 [2,2]。
输入解题思路,AI测评打分。不知道怎么写?