首页 > 其他分享 >E. Expected Power

E. Expected Power

时间:2024-10-07 10:33:26浏览次数:1  
标签:case 10 frac Power cdot int test Expected

E. Expected Power

You are given an array of $n$ integers $a_1,a_2,\ldots,a_n$. You are also given an array $p_1, p_2, \ldots, p_n$.

Let $S$ denote the random multiset (i. e., it may contain equal elements) constructed as follows:

  • Initially, $S$ is empty.
  • For each $i$ from $1$ to $n$, insert $a_i$ into $S$ with probability $\frac{p_i}{10^4}$. Note that each element is inserted independently.

Denote $f(S)$ as the bitwise XOR of all elements of $S$. Please calculate the expected value of $(f(S))^2$. Output the answer modulo $10^9 + 7$.

Formally, let $M = 10^9 + 7$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$).

The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le 1023$).

The third line of each test case contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1 \le p_i \le 10^4$).

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

Output

For each test case, output the expected value of $(f(S))^2$, modulo $10^9 + 7$.

Example

Input

4
2
1 2
5000 5000
2
1 1
1000 2000
6
343 624 675 451 902 820
6536 5326 7648 2165 9430 5428
1
1
10000

Output

500000007
820000006
280120536
1

Note

In the first test case, $a = [1, 2]$ and each element is inserted into $S$ with probability $\frac{1}{2}$, since $p_1 = p_2 = 5000$ and $\frac{p_i}{10^4} = \frac{1}{2}$. Thus, there are $4$ outcomes for $S$, each happening with the same probability of $\frac{1}{4}$:

  • $S = \varnothing$. In this case, $f(S) = 0$, $(f(S))^2 = 0$.
  • $S = \{1\}$. In this case, $f(S) = 1$, $(f(S))^2 = 1$.
  • $S = \{2\}$. In this case, $f(S) = 2$, $(f(S))^2 = 4$.
  • $S = \{1,2\}$. In this case, $f(S) = 1 \oplus 2 = 3$, $(f(S))^2 = 9$.

Hence, the answer is $0 \cdot \frac{1}{4} + 1 \cdot \frac{1}{4} + 4\cdot \frac{1}{4} + 9 \cdot \frac{1}{4} = \frac{14}{4} = \frac{7}{2} \equiv 500\,000\,007 \pmod{10^9 + 7}$.

In the second test case, $a = [1, 1]$, $a_1$ is inserted into $S$ with probability $0.1$, while $a_2$ is inserted into $S$ with probability $0.2$. There are $3$ outcomes for $S$:

  • $S = \varnothing$. In this case, $f(S) = 0$, $(f(S))^2 = 0$. This happens with probability $(1-0.1) \cdot (1-0.2) = 0.72$.
  • $S = \{1\}$. In this case, $f(S) = 1$, $(f(S))^2 = 1$. This happens with probability $(1-0.1) \cdot 0.2 + 0.1 \cdot (1-0.2) = 0.26$.
  • $S = \{1, 1\}$. In this case, $f(S) = 0$, $(f(S))^2 = 0$. This happens with probability $0.1 \cdot 0.2 = 0.02$.

Hence, the answer is $0 \cdot 0.72 + 1 \cdot 0.26 + 0 \cdot 0.02 = 0.26 = \frac{26}{100} \equiv 820\,000\,006 \pmod{10^9 + 7}$.

 

解题思路

  由于 $1 \leq a_i \leq 1023$,因此有 $S \in [0, 1023]$(这里的 $S$ 表示集合内元素的异或值)。一种求期望的做法是分别求出每个 $S$ 对应的 $S^2$ 的期望再求和,也就是 $\sum\limits_{S=0}^{1023}{P_S \cdot S^2}$,其中 $P_S$ 表示异或结果等于 $S$ 的概率。

  $P_S$ 可以通过简单的 dp 求得。定义 $f(i,j)$ 表示从前 $i$ 个数中选出异或结果为 $j$ 的集合的概率,根据是否选择第 $i$ 个数进行状态转移,有状态转移方程 $f(i,j) = f(i-1, j) \cdot \left(1 - \frac{p_i}{10^4} \right) + f(i-1, j \oplus a_i) \cdot \frac{p_i}{10^4}$。

  最后要求的期望就是 $\sum\limits_{S=0}^{1023}{f(n, S) \cdot S^2}$。

  AC 代码如下,时间复杂度为 $O\left(n \cdot 2^{\left\lfloor\log{A}\right\rfloor + 1} \right)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 2e5 + 5, M = 1 << 10, mod = 1e9 + 7;

int a[N], p[N];
int f[2][M];

void solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
    }
    memset(f[0], 0, sizeof(f[0]));
    f[0][0] = 1;
    for (int i = 1; i <= n; i++) {
        memset(f[i & 1], 0, sizeof(f[0]));
        for (int j = 0; j < M; j++) {
            f[i & 1][j] = ((10000 - p[i]) * 285700002ll % mod * f[i - 1 & 1][j] + p[i] * 285700002ll % mod * f[i - 1 & 1][j ^ a[i]]) % mod;
        }
    }
    int ret = 0;
    for (int i = 0; i < M; i++) {
        ret = (ret + 1ll * f[n & 1][i] * i % mod * i) % mod;
    }
    cout << ret << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}

  实际上是因为 $a_i$ 的值域太小所以才放过上面的暴力做法,下面给出正解。

  如果单纯求的是 $S$ 的期望,我们容易想到拆位然后分别考虑每一位对期望的贡献。$S^2$ 也是可以这样做的,有$$\begin{align*} S^2 &= \left( b_9 \cdot 2^9 + b_8 \cdot 2^8 + \cdots +b_0 \cdot 2^0 \right)^2 \\ &= \sum\limits_{i=0}^{9}{\sum\limits_{j=0}^{9}}{b_i b_j \cdot 2^{i+j}} \end{align*}$$

  所以现在我们需要求出每个 $b_i b_j = 1$ 的概率是多少,记为 $P_{i,j}$,那么所求期望就是 $\sum\limits_{i=0}^{9}{\sum\limits_{j=0}^{9}}{P_{i,j} \cdot 2^{i+j}}$。而 $P_{i,j}$ 同样可以用 dp 求得。

  定义 $f(i,j,k,u,v)$ $(u,v \in \{0,1\})$ 表示从前 $i$ 个数中选出异或结果的二进制的第 $j$ 位为 $u$ 且第 $k$ 位为 $v$ 的集合的概率,一样根据是否选择第 $i$ 个数进行状态转移,状态转移方程就是 $f(i,j,k,u,v) = f(i-1,j,k,u,v) \cdot \left(1 - \frac{p_i}{10^4} \right) + f(i-1,j,k,u \oplus a_{i,u} ,v \oplus a_{i,v}) \cdot \frac{p_i}{10^4}$,其中 $a_{i,j}$ 表示 $a_i$ 的二进制第 $j$ 位的值。

  最后要求的期望就是 $\sum\limits_{i=0}^{9}{\sum\limits_{j=0}^{9}}{f(n,i,j,1,1) \cdot 2^{i+j}}$。

  AC 代码如下,时间复杂度为 $O(n \log^2{A})$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 2e5 + 5, M = 10, mod = 1e9 + 7;

int a[N], p[N];
int f[2][M][M][2][2];

void solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
    }
    memset(f[0], 0, sizeof(f[0]));
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < M; j++) {
            f[0][i][j][0][0] = 1;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < M; j++) {
            for (int k = 0; k < M; k++) {
                for (int u = 0; u <= 1; u++) {
                    for (int v = 0; v <= 1; v++) {
                        f[i & 1][j][k][u][v] = ((10000 - p[i]) * 285700002ll % mod * f[i - 1 & 1][j][k][u][v] + p[i] * 285700002ll % mod * f[i - 1 & 1][j][k][u ^ (a[i] >> j & 1)][v ^ (a[i] >> k & 1)]) % mod;
                    }
                }
            }
        }
    }
    int ret = 0;
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < M; j++) {
            ret = (ret + (1ll << i + j) * f[n & 1][i][j][1][1]) % mod;
        }
    }
    cout << ret << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Tutorial for Codeforces Round 976 (Div. 2) and Divide By Zero 9.0:https://codeforces.com/blog/entry/134516

标签:case,10,frac,Power,cdot,int,test,Expected
From: https://www.cnblogs.com/onlyblues/p/18449808

相关文章

  • Cisco Firepower 9300 Series FTD Software 7.6.0 & ASA Software 9.22.1
    CiscoFirepower9300SeriesFTDSoftware7.6.0&ASASoftware9.22.1FirepowerThreatDefense(FTD)Software-思科防火墙系统软件请访问原文链接:https://sysin.org/blog/cisco-firepower-9300/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.orgCiscoSecure防......
  • Cisco Firepower 4100 Series FTD Software 7.6.0 & ASA Software 9.22.1
    CiscoFirepower4100SeriesFTDSoftware7.6.0&ASASoftware9.22.1FirepowerThreatDefense(FTD)Software-思科防火墙系统软件请访问原文链接:https://sysin.org/blog/cisco-firepower-4100/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.orgCiscoSecure防......
  • Cisco Firepower 1000 Series FTD Software 7.6.0 & ASA Software 9.22.1
    CiscoFirepower1000SeriesFTDSoftware7.6.0&ASASoftware9.22.1FirepowerThreatDefense(FTD)Software-思科防火墙系统软件请访问原文链接:https://sysin.org/blog/cisco-firepower-1000/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org面向小型办公室......
  • 【VMware VCF】使用 PowerVCF 连接和管理 VMware Cloud Foundation 环境。
    VMware有一个非常强大的命令行工具叫PowerCLI,该工具是基于PowerShell开发的模块,主要用于在Windows环境中连接和管理传统虚拟化解决方案,比如vSphere、vSAN以及NSX等。之所以PowerCLI非常强大,是因为它几乎可以实现这些解决方案WEBUI中的所有管理操作,甚至更多。如果你......
  • Windows Powershell and WSL terminal 路径
    在windowspowershell中访问C,D盘cdC:,cdD:,...:PSC:\Users\phil>cdC:PSC:\Users\phil>pwdPath----C:\Users\philPSC:\Users\phil>在windowspowershell中访问WSL:PSC:\Users\phil>cd\\wsl.localhost\Ubuntu\home\phil\在W......
  • Rustup-init.exe安装后执行cargo run 报错:`link.exe` returned an unexpected error的
    版本:rustc1.81.0(eeb90cda12024-09-04)报错情况如下图:摸索了后,总结一下关键解决方法:从微软件官网:https://visualstudio.microsoft.com/zh-hans/downloads/找到选项“用于VisualStudio的工具”,在其子项中下载“VisualStudio2022生成工具”下载后安装时,在Visualstu......
  • 集合论(ZFC)之 幂集公理(Axiom of Power Set)注解
            集合论(ZFC)之幂集公理(AxiomofPowerSet)定义了给定一个集合X,存在一个集合Y为该集合X的幂集,记Y=P(X),其包含了集合X的所有子集(Subset)。    子集关系的定义为,如果集合U的所有元素,都是集合X的元素,那么集合U就是集合X的子集,记U ⊂X,有∀z(z∈U→......
  • PowerShell 脚本禁用 Realtek Audio Console 中的前面板插孔检测,通常需要修改注册表项
     PowerShell脚本禁用RealtekAudioConsole中的前面板插孔检测,通常需要修改注册表项。以下是一个示例脚本,用于执行此操作:powershellCopyCode#设置注册表路径$registryPath="HKLM:\SOFTWARE\Realtek\Audio\RtkNGUI\Settings"#检查注册表路径是否存在if(-not(Test-......
  • 山海鲸可视化 VS PowerBI,中外免费报表软件对比
    在数据分析与可视化的时代,选择合适的报表工具显得尤为重要。山海鲸可视化和PowerBI是市场上颇受欢迎的两款免费报表软件,各有特色。接下来,我们将从功能、优缺点等方面进行对比,帮助你找到最适合的工具。山海鲸可视化山海鲸可视化是一款国内自主研发的报表工具,专注于用户体验和简易......
  • 【GiraKoo】PowerShell美化笔记
    【GiraKoo】PowerShell美化笔记oh-my-poshinitpwsh--config"$env:POSH_THEMES_PATH/powerlevel10k_lean.omp.json"|Invoke-Expression#-------------------------------ps-read-line-------------------------------#引入ps-read-lineImport-ModulePSReadLi......