当前位置: 首页 > news >正文

【数论 拆位法】P10308 「Cfz Round 2」Osmanthus|普及+

本文涉及知识点

数论:质数、最大公约数、菲蜀定理
位运算、状态压缩、枚举子集汇总

P10308 「Cfz Round 2」Osmanthus

题目描述

给定一个长度为 n n n 的序列 a a a

我们定义一次操作为,同时将序列 a a a 中的每个元素 a i a_i ai 替换为 ⨁ j = 1 i a j \bigoplus\limits_{j=1}^i a_j j=1iaj(即 a 1 a_1 a1 a i a_i ai 的异或和),其中 ⨁ \bigoplus 表示按位异或,即 C++ 中的 ^

现有 q q q 次有序的修改,每次修改会给定两个整数 x i , p i x_i,p_i xi,pi,表示将 a x i a_{x_i} axi 的值修改为 p i p_i pi修改之间并不独立,每次修改会对后续的修改产生影响

你需要在每次修改后,找到最小的正整数 t t t,满足进行 t t t 次操作后的序列 a a a 与操作前的序列 a a a 相同。可以证明一定存在满足要求的正整数 t t t

由于答案可能很大,所以你只需要输出答案对 ( 1 0 9 + 7 ) (10^9+7) (109+7) 取模的结果。

输入格式

第一行输入两个整数 n , q n,q n,q

第二行输入 n n n 个整数,表示给定的序列 a a a

接下来 q q q 行,每行输入两个整数 x i , p i x_i,p_i xi,pi,表示一次修改。

输出格式

q q q 行,每行输出一个整数,其中第 i i i 行的整数表示第 i i i 次修改后,最小的满足要求的正整数 t t t ( 1 0 9 + 7 ) (10^9+7) (109+7) 取模的结果。

输入输出样例 #1

输入 #1

3 3
3 1 0
2 2
1 0
2 0

输出 #1

4
2
1

说明/提示

「样例解释 #1」

1 1 1 次修改后的序列 a a a { 3 , 2 , 0 } \{3,2,0\} {3,2,0},此时进行 1 1 1 次操作后的序列 a a a { 3 , 1 , 1 } \{3,1,1\} {3,1,1},进行 2 2 2 次操作后的序列 a a a { 3 , 2 , 3 } \{3,2,3\} {3,2,3},进行 3 3 3 次操作后的序列 a a a { 3 , 1 , 2 } \{3,1,2\} {3,1,2},进行 4 4 4 次操作后的序列 a a a { 3 , 2 , 0 } \{3,2,0\} {3,2,0},所以最小的满足要求的正整数 t t t 4 4 4

「数据范围」

对于所有数据, 1 ≤ n , q ≤ 3 × 1 0 5 1 \le n,q \le 3\times10^5 1n,q3×105 0 ≤ a i , p i ≤ 1 0 9 0 \le a_i,p_i \le 10^9 0ai,pi109 1 ≤ x i ≤ n 1 \le x_i \le n 1xin

只有你通过本题的所有测试点,你才能获得本题的分数。

拆位法

各位分别讨论。
长度为1的序列,永远不变。
长度为2的序列a,如果全部是0,永远不变。如果a[0]是0,永远不变。
否则{ 10 , 11 , 10 , 11 ⋯ 10,11,10,11\cdots 10,11,10,11} 或{ 11 , 10 , 11 , 10 ⋯ 11,10,11,10 \cdots 11,10,11,10}循环。循环长度为2。
下表:x-y表示本单元格有x个a0,本行到当前单元个有y个a0。两个a0异或会抵消,故x和y大于2减去2,最后取值范围{0,1}。

次数1长度2长度3长度4长度5长度6长度7长度8
1次1-11-01-11-01-11-01-11-0
2次1-10-11-00-01-10-11-00-0
3次1-11-00-00-01-11-00-00-0
4次1-10-10-10-11-00-00-00-0
5次1-11-01-11-00-00-00-00-0
6次1-10-11-00-00-00-00-00-0
7次1-11-00-00-00-00-00-00-0
8次1-10-10-10-10-10-10-10-1

性质一:某行第一列,x是1,其它列x是0。可能和原始序列相等,否则一定不等,因为 a 0 ≠ 0 a_0 \neq 0 a0=0
长度1,可能周期1。且周期一定是1的而倍数。
长度2,可能周期2。且周期一定是2的而倍数。
长度3,4,可能周期4,且周期一定是4的而倍数。
长度5,可能周期8,且周期一定是8的而倍数。
猜想一:令删除前缀0后,长度len ,各数字的周期是 f ( l e n ) = 2 ⌈ l o g 2 l e n ⌉ f(len)=2^{\lceil log_2{len}\rceil} f(len)=2log2len
由于 n ≤ 3 × 1 0 5 n\le 3\times10^5 n3×105,我们直接用程序演算一下,就可以证明猜想。时间复杂度:O(nlogn)
注意:验证程序无需提交,所以可以超过1秒。

下面讨论a1

次数–长度1长度2长度3长度4长度5长度6
1次0-01-11-01-11-01-1
2次0-01-10-11-00-01-1
3次0-01-11-00-00-01-1
4次0-01-10-10-10-11-0
5次0-01-11-01-11-00-0
6次0-01-10-11-00-00-0
7次0-01-11-00-00-00-0
8次0-01-10-10-10-10-1

性质三 a i a_i ai周期符合f(len-i)。 删除前i列后,表格相同。
性质四:满足f(len),则一定满足f(len-1)。只有两种情况 f ( l e n ) = = f ( l e n − 1 ) , f ( l e n ) = 2 × f ( l e n − 1 ) f(len)==f(len-1),f(len)=2\times f(len-1) f(len)==f(len1)f(len)=2×f(len1) 都符合。
性质五:各位的len不同,根据性质四,取最大值。同一个二进制位,len也取最大,即 a 0 a_0 a0

猜想证明:拆分成4个行列相等的表格

请忽略各单元格的y,y方便验算x是否正确。
i > 0 i >0 i>0时,拆分成4个 行列都是 2 i − 1 的表格 行列都是2^{i-1}的表格 行列都是2i1的表格。数学归纳法来证明。

1 = = i 1==i 1==i时,下列性质成立。
在这里插入图片描述
性质一:3红色表格一样。
性质二:绿色表格(右下角)全部是0。
性质三:除最后一行外,每行1的数量时偶数。
下面来证明当 行列 2 i − 1 的表格成立,则行列 2 i 的表格也成立 行列2^{i-1}的表格成立,则行列2^{i}的表格也成立 行列2i1的表格成立,则行列2i的表格也成立
在这里插入图片描述
性质一:左上和左下相同时因为周期是 2 i − 1 2^{i-1} 2i1。左上和右上相等,第一行全部是1,其它行是性质三。
性质二:第一行全部为0,因为左上右上最后一行除第0列为1外,全为0。
其它行因为性质二。
性质三:左上右上相同,故1的数量是偶数。
右下全部是0,故下边1的数量就是左下1的数量。

实现

有序集合s记录所有 a [ i ] ≠ 0 a[i] \neq 0 a[i]=0的i。
如果如果s为空,答案的是f(N),否则f(N-*s.begin())。
注意:全部为0,也要操作一次,不能操作0次。

代码

核心代码

#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>
#include <chrono>
using namespace std::chrono;
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 T1, class T2, class T3, class T4, class T5 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) ;return in;
}template<class T1, class T2, class T3, class T4, class T5, class T6 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5, T6>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t) ;return in;
}template<class T1, class T2, class T3, class T4, class T5, class T6, class T7 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5, T6, T7>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t) >> get<6>(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 Solution {
public:vector<int> Ans(vector<int>& as, vector<pair<int, int>> xp) {const int N = as.size();multiset<int> s;for (int i = 0; i < N; i++) {if (as[i]) { s.emplace(i); }}auto f = [&](int len) {if (0 == len) { return 1; }int iMax = 0, cnt = 0;for (int i = 0; i < 31; i++) {if ((1 << i) & len) {iMax = i;cnt++;}}return 1 << (iMax + (cnt > 1));};vector<int> ans;for (auto [x, p] : xp) {x--;s.erase(x);if (p) { s.emplace(x); }const int len = s.empty() ? 0 : (N - *s.begin());ans.emplace_back(f(len));}return ans;};
};int main() {
#ifdef _DEBUGfreopen("a.in", "r", stdin);
#endif // DEBUG	ios::sync_with_stdio(0); cin.tie(nullptr);//CInBuff<> in; COutBuff<10'000'000> ob;int N, Q;cin >> N >> Q;auto as = Read<int>(N);auto xp = Read<pair<int, int>>(Q);#ifdef _DEBUG	//printf("N=%d,M=%d", N,M);//Out(v, ",v=");//Out(B, ",B=");//Out(que, ",que=");
#endif // DEBUG		Solution slu;auto res = Solution().Ans(as,xp);for (const auto& i : res) {cout << i << "\n";}return 0;
}

单元测试

vector<int> as;vector<pair<int, int>> xp;TEST_METHOD(TestMethod1){as = { 3,1,0 };xp = { {2,2},{1,0},{2,0} };auto res = Solution().Ans(as,xp);AssertEx({ 4,2,1 }, res);}

扩展阅读

我想对大家说的话
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
如果程序是一条龙,那算法就是他的是睛
失败+反思=成功 成功+反思=成功

视频课程

先学简单的课程,请移步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++**实现。

http://www.lqws.cn/news/578071.html

相关文章:

  • 车辆工程中的压力传感技术:MEMS与薄膜传感器的实战应用
  • 从设计到开发一个小程序页面
  • Java + 阿里云 Gmsse 实现 SSL 国密通信
  • 用基础模型构建应用(第四章)AI Engineering: Building Applications with Foundation Models学习笔记
  • Springboot + vue + uni-app小程序web端全套家具商场
  • MongoDB 安装使用教程
  • 第81题:搜索旋转排序数组Ⅱ
  • 【软考高项论文】论信息系统项目的干系人管理
  • 百度文库智能PPT月访问量超3400万,用户规模翻倍增长
  • 中钧科技亮相2025 亚欧商品贸易博览会,赋能数字经济新未来!
  • pyspark driver 上传pod本地文件到对象存储
  • AWS 开源 Strands Agents SDK,简化 AI 代理开发流程
  • Hive SQL 实战:电商销售数据分析全流程案例
  • Git远程仓库迁移与分支关联技术分享
  • 【Python使用】嘿马python运维开发全体系教程第2篇:日志管理,Linux概述【附代码文档】
  • 【硬核数学 · LLM篇】3.1 Transformer之心:自注意力机制的线性代数解构《从零构建机器学习、深度学习到LLM的数学认知》
  • Android Compose Modifier 详细解析
  • K8s-Pod深度解析
  • 鸿蒙进阶——Mindspore Lite AI框架源码解读之模型加载详解(五)
  • 阶段二开始-第一章—8天Python从入门到精通【itheima】-121节+122节(函数和方法的类型注解+Union联合类型注解)
  • Ruby 安装使用教程
  • 单例模式7种实现
  • Golang的多环境配置
  • Golang快速开发框架——项目立项与系统配置读取组件viper(一)
  • uni-app使用uview2自定义tabber
  • camera调试:安卓添加xml注册
  • 【软考高项论文】论信息系统项目的整体管理
  • Java 图书管理系统
  • 使用Verilog设计模块输出中位数,尽可能较少资源使用
  • 华为智选焕新鸿蒙智选,继续携手IAM赋能智慧家居健康生态协同演进