CF2200G.Operation Permutation

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

AksLolCoding has an integer xx and a list of nn operations. Each operation is a string starting with one of the symbols +,-,x, or / (representing addition, subtraction, multiplication, and real number division respectively), followed immediately by a positive integer yy (1≤y≤1091 \leq y \leq 10^9). For example, the operation x3 represents multiplying xx by 33.

AksLolCoding will randomly permute the operations and then apply all operations sequentially to xx in the permuted order. Help AksLolCoding compute the expected∗^{\text{∗}} final value of xx 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(modM)p \cdot q^{-1} \pmod M. In other words, output such an integer aa that 0≤a<M0 \le a \lt M and a⋅q≡p(modM)a \cdot q \equiv p \pmod M.

∗^{\text{∗}}The expected final value of xx is the average of the final value of xx over all n!n! permutations.

AksLolCoding 有一个整数 xx 和一个包含 nn 个操作的列表。每个操作是一个字符串,以符号 +、-、x 或 /(分别表示加法、减法、乘法和实数除法)之一开头,其后紧跟着一个正整数 yy(1≤y≤1091 \leq y \leq 10^9)。例如,操作 x3 表示将 xx 乘以 33。

AksLolCoding 将随机打乱这些操作的顺序,然后按打乱后的顺序依次对 xx 执行所有操作。请帮助 AksLolCoding 计算 xx 的最终值的期望值(对所有 n!n! 种排列取平均)模 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(modM)p \cdot q^{-1} \pmod M 的整数。换言之,输出满足 0≤a<M0 \le a < M 且 a⋅q≡p(modM)a \cdot q \equiv p \pmod M 的整数 aa。

∗^{\text{∗}} xx 的最终值的期望值,即对全部 n!n! 种排列所得最终值取算术平均。

输入格式

The first line contains a single integer tt (1≤t≤10001 \leq t \leq 1000), the number of test cases.

For each test case, the first line contains two integers nn and xx (1≤n≤30001 \leq n \leq 3000, 1≤x≤1091 \leq x \leq 10^9).

The second line of each test case contains nn strings, each representing an operation in the format described above.

The sum of n2n^2 over all test cases does not exceed 300023000^2.

Note: x is used to represent multiplication, not *

第一行包含一个整数 tt(1≤t≤10001 \leq t \leq 1000),表示测试用例的数量。

对于每个测试用例,第一行包含两个整数 nn 和 xx(1≤n≤30001 \leq n \leq 3000,1≤x≤1091 \leq x \leq 10^9)。

每个测试用例的第二行包含 nn 个字符串,每个字符串表示一个如上所述格式的操作。

所有测试用例中 n2n^2 的总和不超过 300023000^2。

注意:此处的 xx 表示乘法运算,而非 \*。

输出格式

For each test case, output a single integer: the expected final value of xx modulo 109+710^9+7.

对于每个测试用例,输出一个整数:xx 的最终期望值对 109+710^9+7 取模的结果。

输入输出样例

  • 输入#1

    4
    2 10
    x2 -10
    4 2
    +6 +7 /1 -13
    8 1
    +1 x2 x3 +4 +5 +6 -7 -8
    9 864209753
    -918273645 x564738291 /365107362 x734582911 -654321789 x998244353 +172519103 /482193765 /482091376

    输出#1

    5
    2
    166666677
    601980218

说明/提示

In the first test case, xx can either be (10⋅2)−10=10(10\cdot 2)-10=10 or (10−10)⋅2=0(10-10)\cdot 2=0, resulting in an expected value of 55.

In the second test case, all possible permutations result in x=2x=2.

In the third test case, the expected value of xx is 556\frac{55}{6}.

在第一个测试用例中,xx 的值可以是 (10⋅2)−10=10(10\cdot 2)-10=10 或 (10−10)⋅2=0(10-10)\cdot 2=0,因此期望值为 55。

在第二个测试用例中,所有可能的排列均得到 x=2x=2。

在第三个测试用例中,xx 的期望值为 556\frac{55}{6}。

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

首页