【期望 位运算 拆位法】P10500 Rainbow的信号|普及+
本文摘要: 题目要求计算给定数字序列的三种位运算期望值:异或(XOR)、与(AND)、或(OR)。对于长度为N的序列,需要考虑所有可能的子区间[l,r],其中1≤l≤r≤N。关键解法是采用位分解法,将32位整数逐位处理。对于异或期望,使用动态规划记录前缀异或和;对于与/或期望,利用其单调性性质,维护有效区间进行计算。最终结果需要处理l=r的特殊情况,并考虑所有可能的子区间组合。时间复杂度为O(N
本文涉及知识点
P10500 Rainbow的信号
题目描述
Freda 发明了传呼机之后,rainbow 进一步改进了传呼机发送信息所使用的信号。
由于现在是数字、信息时代,rainbow 发明的信号用 N N N 个自然数表示。
为了避免两个人的对话被大坏蛋 VariantF 偷听,rainbow 把对话分成 A 、 B 、 C A、B、C A、B、C 三部分,分别用 a 、 b 、 c a、b、c a、b、c 三个密码加密。
现在 Freda 接到了 rainbow 的信息,她的首要工作就是解密。
Freda 了解到,这三部分的密码计算方式如下:
在 1 ∼ N 1 \sim N 1∼N 这 N N N 个数中,等概率地选取两个数 l 、 r l、r l、r,如果 l > r l>r l>r,则交换 l 、 r l、r l、r。
把信号中的第 l l l 个数到第 r r r 个数取出来,构成一个数列 P P P。
A A A 部分对话的密码是数列 P P P 的 x o r xor xor 和的数学期望值, x o r xor xor 和就是数列 P P P 中各个数异或之后得到的数; x o r xor xor 和的期望就是对于所有可能选取的 l 、 r l、r l、r,所得到的数列的 x o r xor xor 和的平均数。
B B B 部分对话的密码是数列 P P P 的 a n d and and 和的期望,定义类似于 x o r xor xor 和。
C C C 部分对话的密码是数列 P P P 的 o r or or 和的期望,定义类似于 x o r xor xor 和。
请你帮忙计算这三个密码。
输入格式
第一行一个正整数 N N N。
第二行 N N N 个自然数,表示 Freda 接到的信号。
输出格式
一行三个实数,分别表示 x o r xor xor 和、 a n d and and 和、 o r or or 和的期望,四舍五入保留 3 3 3 位小数,相邻两个实数之间用一个空格隔开。
输入输出样例 #1
输入 #1
2
4 5
输出 #1
2.750 4.250 4.750
说明/提示
样例解释
样例 1 共包含四种可能的 l , r l,r l,r:
| c | c | c | c |
|---|---|---|---|
| l , r l, r l,r | xor 和 | and 和 | or 和 |
| 1,1 | 4 | 4 | 4 |
| 1,2 | 1 | 4 | 5 |
| 2,1 | 1 | 4 | 5 |
| 2,2 | 5 | 5 | 5 |
以上每一对 l , r l,r l,r 出现的概率均相同, 因此分别对 xor 和、and 和、or 和取平均数就是数学期望值。
数据范围与约定
对于 20 % 20 \% 20% 的数据, 1 ≤ N ≤ 100 1 \le N \le 100 1≤N≤100 。
对于 40 % 40 \% 40% 的数据, 1 ≤ N ≤ 1000 1 \le N \le 1000 1≤N≤1000 。
对于另外 30 % 30 \% 30% 的数据, N N N 个数为 0 0 0 或 1 1 1 。
对于 100 % 100 \% 100% 的数据, 1 ≤ N ≤ 100000 1 \le N \le 100000 1≤N≤100000, N N N 个自然数均不超过 10 9 10^9 109 。
期望 位运算 拆位法:
方案数: n × n n \times n n×n,将(l,r)和(r,l)合并处理后。 l ≠ r 的方案数是 2 ; 1 = = r 的方案数是 1 。 l \neq r的方案数是2; 1 == r 的方案数是1。 l=r的方案数是2;1==r的方案数是1。
异或期望: a [ i ] ⊕ a [ i ] 是 0 a[i] \oplus a[i]是0 a[i]⊕a[i]是0,故无需考虑 l = = r l == r l==r。各位分别处理,b是bool向量。b[i]=(1<<bit)&a[i]。dp[i] 记录 ∑ x : 0 i − 1 ( b [ x ⋯ i ] 异或和 ) \sum_{x:0}{i-1}(b[x\cdots i]异或和 ) ∑x:0i−1(b[x⋯i]异或和)。
如果a[i+1]是0,则dp[i+1]=dp[i] + b[i]。
如果a[i+1]是1,则dp[i+1]=(i-dp[i])+(0==b[i])
cnt[bit] = ∑ d p \sum dp ∑dp
答案是: ( 2 z + ∑ a ) / n / n (2z+\sum a)/n/n (2z+∑a)/n/n,z= ∑ b i t : 0 29 ( 1 < < b i t ) × c n t [ b i t ] \sum_{bit:0}^{29}(1<<bit) \times cnt[bit] ∑bit:029(1<<bit)×cnt[bit]
时间负重度:O(nlogn)
位与、位或期望
x ∈ [ 0 , i ] x\in[0,i] x∈[0,i], a[x…i]的位与(或)的结果最多只有31个,且单调。x变小时,位与:结果发生变化,至少一位永久变成0;位或:结果发生变化,至少一位永久变成1。直接使用模板。
z = ∑ i : 0 N − 1 ∑ x : 0 i a [ x . . i ] 位与 z=\sum_{i:0}^{N-1}\sum_{x:0}^i a[x..i]位与 z=∑i:0N−1∑x:0ia[x..i]位与,结果是: ( z + z − ∑ a ) / n n (z + z - \sum a )/nn (z+z−∑a)/nn
$l == r $被重复计算了,需要扣掉。
代码
核心代码
#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>
#include<array>
#include <bitset>
using namespace std;
template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {
in >> pr.first >> pr.second;
return in;
}
template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t);
return in;
}
template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {
in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);
return in;
}
template<class T = int>
vector<T> Read() {
int n;
cin >> n;
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> ReadNotNum() {
vector<T> ret;
T tmp;
while (cin >> tmp) {
ret.emplace_back(tmp);
if ('\n' == cin.get()) { break; }
}
return ret;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
cin >> ret[i];
}
return ret;
}
template<int N = 1'000'000>
class COutBuff
{
public:
COutBuff() {
m_p = puffer;
}
template<class T>
void write(T x) {
int num[28], sp = 0;
if (x < 0)
*m_p++ = '-', x = -x;
if (!x)
*m_p++ = 48;
while (x)
num[++sp] = x % 10, x /= 10;
while (sp)
*m_p++ = num[sp--] + 48;
AuotToFile();
}
void writestr(const char* sz) {
strcpy(m_p, sz);
m_p += strlen(sz);
AuotToFile();
}
inline void write(char ch)
{
*m_p++ = ch;
AuotToFile();
}
inline void ToFile() {
fwrite(puffer, 1, m_p - puffer, stdout);
m_p = puffer;
}
~COutBuff() {
ToFile();
}
private:
inline void AuotToFile() {
if (m_p - puffer > N - 100) {
ToFile();
}
}
char puffer[N], * m_p;
};
template<int N = 1'000'000>
class CInBuff
{
public:
inline CInBuff() {}
inline CInBuff<N>& operator>>(char& ch) {
FileToBuf();
while (('\r' == *S) || ('\n' == *S) || (' ' == *S)) { S++; }//忽略空格和回车
ch = *S++;
return *this;
}
inline CInBuff<N>& operator>>(int& val) {
FileToBuf();
int x(0), f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
inline CInBuff& operator>>(long long& val) {
FileToBuf();
long long x(0); int f(0);
while (!isdigit(*S))
f |= (*S++ == '-');
while (isdigit(*S))
x = (x << 1) + (x << 3) + (*S++ ^ 48);
val = f ? -x : x; S++;//忽略空格换行
return *this;
}
template<class T1, class T2>
inline CInBuff& operator>>(pair<T1, T2>& val) {
*this >> val.first >> val.second;
return *this;
}
template<class T1, class T2, class T3>
inline CInBuff& operator>>(tuple<T1, T2, T3>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val);
return *this;
}
template<class T1, class T2, class T3, class T4>
inline CInBuff& operator>>(tuple<T1, T2, T3, T4>& val) {
*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);
return *this;
}
template<class T = int>
inline CInBuff& operator>>(vector<T>& val) {
int n;
*this >> n;
val.resize(n);
for (int i = 0; i < n; i++) {
*this >> val[i];
}
return *this;
}
template<class T = int>
vector<T> Read(int n) {
vector<T> ret(n);
for (int i = 0; i < n; i++) {
*this >> ret[i];
}
return ret;
}
template<class T = int>
vector<T> Read() {
vector<T> ret;
*this >> ret;
return ret;
}
private:
inline void FileToBuf() {
const int canRead = m_iWritePos - (S - buffer);
if (canRead >= 100) { return; }
if (m_bFinish) { return; }
for (int i = 0; i < canRead; i++)
{
buffer[i] = S[i];//memcpy出错
}
m_iWritePos = canRead;
buffer[m_iWritePos] = 0;
S = buffer;
int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);
if (readCnt <= 0) { m_bFinish = true; return; }
m_iWritePos += readCnt;
buffer[m_iWritePos] = 0;
S = buffer;
}
int m_iWritePos = 0; bool m_bFinish = false;
char buffer[N + 10], * S = buffer;
};
class CRangCal {
public:
template<class _Pr, class T = int>
static vector<vector<pair<T, int>>> RangCal(const vector<T>& nums, _Pr pr) {
vector<vector<pair<int, int>>> vNumIndex(nums.size());
for (int i = 0; i < nums.size(); i++) {
if (i) {
for (const auto& [preNum, preIndex] : vNumIndex[i - 1]) {
auto iNew = pr(preNum, nums[i]);
if (vNumIndex[i].empty() || (vNumIndex[i].back().first != iNew)) {
vNumIndex[i].emplace_back(make_pair(iNew, preIndex));
}
else {
vNumIndex[i].back().second = preIndex;
}
}
}
if (vNumIndex[i].empty() || (vNumIndex[i].back().first != nums[i])) {
vNumIndex[i].emplace_back(make_pair(nums[i], i));
}
else {
vNumIndex[i].back().second = i;
}
}
return vNumIndex;//vNumIndex[i]记录 nums[x…i]的异或值(不重复)及索引,索引升序。重复值保留索引大的。x∈ \in∈[0,i]。
}
};
class Solution {
public:
tuple<double,double,double> Ans(const int N, vector<int>& a) {
return make_tuple(Ans1(N,a), Ans2(N,a), Ans3(N, a));
}
double Ans1(const int N, vector<int>& a) {
double ans = 0;
for (int bit = 0; bit < 31; bit++) {
vector<int> dp(N);
vector<bool> b(N);
for (int i = 0; i < N; i++) {
b[i] = (1 << bit) & a[i];
}
for (int i = 0; i + 1 < N; i++) {
if (b[i+1]) {
dp[i + 1] = i - dp[i] ;
}
else {
dp[i + 1] = dp[i];
}
dp[i + 1] += (b[i] ^ b[i + 1]);
}
long long sum = accumulate(dp.begin(), dp.end(), 0LL);
ans += (double)(1 << bit) * sum;
}
ans *= 2;
ans += accumulate(a.begin(), a.end(), 0.0);
return ans/N/N;
}
double Ans22(const int N, vector<int>& a) {
//auto v = CRangCal::RangCal(a, [&](const int i1, const int i2) {return i1 & i2; });
double ans = 0;
vector<int> tmp;
for (const auto& i : a) {
for (auto& j : tmp) {
j = j & i;
}
tmp.emplace_back(i);
ans += accumulate(tmp.begin(), tmp.end(), 0.0)/N/N;
}
ans *= 2;
ans -= accumulate(a.begin(), a.end(), 0.0) / N / N;
return ans;
}
double Ans33(const int N, vector<int>& a) {
//auto v = CRangCal::RangCal(a, [&](const int i1, const int i2) {return i1 & i2; });
double ans = 0;
vector<int> tmp;
for (const auto& i : a) {
for (auto& j : tmp) {
j = j | i;
}
tmp.emplace_back(i);
ans += accumulate(tmp.begin(), tmp.end(), 0.0) / N / N;
}
ans *= 2;
ans -= accumulate(a.begin(), a.end(), 0.0) / N / N;
return ans;
}
double Ans2(const int N, vector<int>& a) {
auto v = CRangCal::RangCal(a, [&](const int i1, const int i2) {return i1 & i2; });
double ans = 0;
for (const auto v1 : v) {
ans += (double)v1.front().first * (v1.front().second + 1);
for (int i = 1; i < v1.size(); i++) {
ans += (double)v1[i].first * (v1[i].second-v1[i-1].second) ;
}
}
ans *= 2;
ans -= accumulate(a.begin(), a.end(), 0.0);
return ans / N / N;
}
double Ans3(const int N, vector<int>& a) {
auto v = CRangCal::RangCal(a, [&](const int i1, const int i2) {return i1 | i2; });
double ans = 0;
for (const auto v1 : v) {
ans += (double)v1.front().first * (v1.front().second + 1);
for (int i = 1; i < v1.size(); i++) {
ans += (double)v1[i].first * (v1[i].second - v1[i - 1].second);
}
}
ans *= 2;
ans -= accumulate(a.begin(), a.end(), 0.0);
return ans / N / N;
}
};
int main() {
#ifdef _DEBUG
freopen("a.in", "r", stdin);
#endif // DEBUG
ios::sync_with_stdio(0); cin.tie(nullptr);
//CInBuff<> in; COutBuff<10'000'000> ob;
auto a = Read<int>();
#ifdef _DEBUG
//printf("N=%d,L=%d,K=%d", N,L,K);
//Out(vp, ",vp=");
Out(a, ",a=");
//Out(ab, ",ab=");
//Out(par, "par=");
//Out(que, "que=");
//Out(B, "B=");
#endif // DEBUG
auto res = Solution().Ans(a.size(),a);
printf("%.3lf ", get<0>(res));
printf("%.3lf ", get<1>(res));
printf("%.3lf\n", get<2>(res));
return 0;
};
单元测试
vector<int> a;
TEST_METHOD(TestMethod01)
{
a = { 1,1 };
auto res = Solution().Ans(a.size(), a);
AssertEx(0.5, get<0>(res), 0.001);
AssertEx(1, get<1>(res), 0.001);
AssertEx(1, get<2>(res), 0.001);
}
TEST_METHOD(TestMethod02)
{
a = { 1,1,1 };
auto res = Solution().Ans(a.size(), a);
AssertEx(0.555555, get<0>(res), 0.001);
AssertEx(1, get<1>(res), 0.001);
AssertEx(1, get<2>(res), 0.001);
}
TEST_METHOD(TestMethod03)
{
a = { 4,5 };
auto res = Solution().Ans(a.size(),a);
AssertEx(2.750, get<0>(res), 0.001);
AssertEx(4.250, get<1>(res), 0.001);
AssertEx(4.750, get<2>(res), 0.001);
}
TEST_METHOD(TestMethod11)
{
a = { 2,4 };
Solution slu;
AssertEx(slu.Ans22(a.size(), a), slu.Ans2(a.size(), a), 0.001);
AssertEx(slu.Ans33(a.size(), a), slu.Ans3(a.size(), a), 0.001);
}
TEST_METHOD(TestMethod12)
{
a = { 2,4,7,8 };
Solution slu;
AssertEx(slu.Ans22(a.size(), a), slu.Ans2(a.size(), a), 0.001);
AssertEx(slu.Ans33(a.size(), a), slu.Ans3(a.size(), a), 0.001);
}
TEST_METHOD(TestMethod13)
{
a = { 2,4,7,8,3 };
Solution slu;
AssertEx(slu.Ans22(a.size(), a), slu.Ans2(a.size(), a), 0.001);
AssertEx(slu.Ans33(a.size(), a), slu.Ans3(a.size(), a), 0.001);
}
TEST_METHOD(TestMethod14)
{
a = { 2,4,7,8,3,2,1 };
Solution slu;
AssertEx(slu.Ans22(a.size(),a), slu.Ans2(a.size(), a),0.001);
AssertEx(slu.Ans33(a.size(), a), slu.Ans3(a.size(), a), 0.001);
}
TEST_METHOD(TestMethod15)
{
a = { 2,4,7,8,3,2,1,3,4,5,8,11,12,3,4 };
Solution slu;
AssertEx(slu.Ans22(a.size(), a), slu.Ans2(a.size(), a), 0.001);
AssertEx(slu.Ans33(a.size(), a), slu.Ans3(a.size(), a), 0.001);
}
TEST_METHOD(TestMethod16)
{
a = { 1,2,4,7,9,3,2,4,11,13,8,3,2,1,3,4,5,8,11,12,3,4,12,2,2,45,7,100,102,23,23,15 };
Solution slu;
AssertEx(slu.Ans22(a.size(), a), slu.Ans2(a.size(), a), 0.001);
AssertEx(slu.Ans33(a.size(), a), slu.Ans3(a.size(), a), 0.001);
}
扩展阅读
| 我想对大家说的话 |
|---|
| 工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。 |
| 学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作 |
| 有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注 |
| 闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。 |
| 子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。 |
| 如果程序是一条龙,那算法就是他的是睛 |
| 失败+反思=成功 成功+反思=成功 |
视频课程
先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176
测试环境
操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。
更多推荐



所有评论(0)