板子 5.29--7.19

板子 5.29–7.19

目录

1. 树状数组
2. KMP
3. 矩阵快速幂
4. 数位DP
5. 状压枚举子集
6. 快速幂(新版
7. priority_queue
8. dijkstra
9. 单调栈
10. debug

内容

1. 树状数组
// 树状数组  快速求前缀和 / 前缀最大值
// 维护位置数量(离散化)...// (区间加 区间求和)维护差分数组 初始化add(i,a[i]-a[i-1])
// tr1:维护d[i]的区间和。tr2:维护i⋅d[i]的区间和。
// 则前缀和 pre[i]=(i + 1) * tr1.ask(i) - tr2.ask(i) 区间和 pre[r]-pre[l-1]
struct Tree
{int n;vector<int> tr;Tree(int n1){n = n1 + 10;tr.assign(n, 0);}void add(int x, int v) // 加点{assert(x > 0);for (int i = x; i <= n; i += i & -i)tr[i] += v;}int ask(int x) // 前缀查询{if (!x)return 0;int res = 0;for (int i = x; i; i -= i & -i)res += tr[i];return res;}int ask(int l, int r) // 区间查询{r = max(1ll, r);assert(l <= r);assert(l > 0);if (l > r)return 0ll;return ask(r) - ask(l - 1);}
}; // Tree tr(n);
2. KMP
 
pair<vector<int>, vector<int>> KMP(string &s, string &t)
{vector<int> idx; // t作为子串在s中出现的下标int n = t.size(), m = s.size();string a = " " + t + "#" + s;vector<int> kmp(n + m + 10); // t的前缀t[1~i]中 t[1~i]的前缀和后缀相同的最大长度for (int i = 2; i <= n + m + 1; i++) {int j = kmp[i - 1];while (j && a[i] != a[j + 1])j = kmp[j];if (a[i] == a[j + 1])j++;kmp[i] = j;if (i > n && kmp[i] == n)idx.push_back(i - 2 * n);}return {idx, kmp};
} // auto [idx, kmp] = KMP(s, t);
3. 矩阵快速幂
struct Matrix
{int n, m;vector<vector<int>> mat;Matrix(int _n, int _m) : n(_n), m(_m), mat(_n + 1, vector<int>(_m + 1, 0)) {} // 下标从1开始// 生成单位矩阵static Matrix identity(int size){Matrix res(size, size);for (int i = 1; i <= size; i++)res[i][i] = 1;return res;}// 只读访问const vector<int> &operator[](int i) const { return mat[i]; }// 可写访问vector<int> &operator[](int i) { return mat[i]; }// 矩阵乘法Matrix operator*(const Matrix &b) const{assert(m == b.n);Matrix res(n, b.m);for (int i = 1; i <= n; i++)for (int j = 1; j <= b.m; j++)for (int k = 1; k <= m; k++){res[i][j] = (res[i][j] + mat[i][k] * b[k][j]) % mod;}return res;}// 矩阵快速幂Matrix pow(int k) const{assert(n == m);Matrix res = Matrix::identity(n);Matrix a = *this;while (k){if (k & 1)res = res * a;a = a * a;k >>= 1;}return res;}
};
4. 数位DP
void _()
{string k;int d;cin >> k >> d;int n = k.size();k = ' ' + k;vector<int> num(n + 1);for (int i = 1; i <= n; i++)num[i] = k[n - i + 1] - '0';vector<vector<array<int, 2>>> f(n + 1, vector<array<int, 2>>(110, array<int, 2>{-1, -1}));auto dfs = [&](auto &&self, int u, int pre, int lim) -> int // 1~k数位和是d的倍数的个数{if (!u){return !pre;}if (~f[u][pre][lim])return f[u][pre][lim];int maxk = lim ? num[u] : 9;int res = 0;for (int i = 0; i <= maxk; i++){res += self(self, u - 1, (pre + i) % d, lim && i == maxk);res %= mod;}return f[u][pre][lim] = res;};cout << (dfs(dfs, n, 0, 1) - 1 + mod) % mod << '\n';
}
5. 状压枚举子集
void _() // 选择状态011001101 这些能够形成答案的最大值  然后再考虑划分为2个集合
{int n;cin >> n;int a[17][17] = {};for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++)cin >> a[i][j];}int mx = 1ll << n;vector<int> f(mx, -1);auto dp = [&](auto &&self, int u) -> int{if (~f[u])return f[u];f[u] = 0;for (int i = 1; i <= n; i++){for (int j = i + 1; j <= n; j++){f[u] += a[i][j] * (u >> i - 1 & 1 && u >> j - 1 & 1);}}// 子集for (int sub = u; sub = sub - 1 & u;)f[u] = max(f[u], self(self, sub) + self(self, sub ^ u));return f[u];};cout << dp(dp, mx - 1);
}
6. 快速幂(新版
constexpr int mod = 998244353;
int q_pow(int a, int b)
{if (!a)return 1ll;int res = 1;while (b){if (b & 1)res = res * a % mod; a = a * a % mod;b >>= 1;}return res;
}int fen(int u, int d)
{u %= mod, d %= mod;return u * q_pow(d, mod - 2) % mod;
}
7. priority_queue
struct Node
{int x, y;// priority_queue<Node> q;bool operator<(const Node &other) const // 大根堆{if (x - other.x)return x < other.x;elsereturn y < other.y;}// priority_queue<Node, vector<Node>, greater<>> q;bool operator>(const Node &other) const // greater<> 小根堆{if (x - other.x)return x > other.x;elsereturn y > other.y;}
};
8. dijkstra
struct Node
{int u, dis;// priority_queue<Node> q;bool operator<(const Node &other) const // 大根堆{return dis < other.dis;}// priority_queue<Node, vector<Node>, greater<>> q;bool operator>(const Node &other) const // greater<> 小根堆{return dis > other.dis;}
};
struct edge
{int v, w;
};auto dij = [&](vector<vector<edge>> &e, int s){vector<int> f(n + 1, 1e18);priority_queue<Node, vector<Node>, greater<>> q;q.push({s, 0});f[s] = 0;while (q.size()){auto [u, dis] = q.top();q.pop();if (dis > f[u])continue;for (auto [v, d] : e[u]){if (f[v] > f[u] + d){f[v] = f[u] + d;q.push({v, f[v]});}}}return f;};
9. 单调栈
// 下标    
int t = 0;vector<int> stk(n + 1);for (int i = 1; i <= n; i++){int x = a[i];for (int j = t; j >= 0; j--)if (!j)lmx[i] = -1, t = j;else if (a[stk[j]] >= x){lmx[i] = stk[j];t = j;break;}stk[++t] = i;}
10. debug
template <typename T>
void bug(initializer_list<T> list)
{cerr << "debug: ";for (const auto &elem : list)cerr << elem << ' ';cerr << endl;
}template <typename Container>
void bug(const Container &container)
{cerr << "debug: ";for (const auto &elem : container)cerr << elem << ' ';cerr << endl;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/89588.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/89588.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

min-max容斥学习笔记

最近报了航电的春季赛&#xff0c;在一道题目里面遇到了做法&#xff0c;感觉挺有意思。 考虑一个&#xff08;多重&#xff09;集合S{ai}S\{a_i\}S{ai​}&#xff0c;有如下的等式成立 min⁡ai∈S(ai)∑T⊆S,T≠∅(−1)∣T∣−1max⁡ai∈T(ai)\min_{a_i\in S}(a_i)\sum_{T\sub…

使用帆软制作项目

https://zhuanlan.zhihu.com/p/23429318335 项目背景 为加快大数据体系建设&#xff0c;稳步推进数字化转型战略&#xff0c;规范数据架构体系和数据治理体系&#xff0c;运用大数据推进全行数字化转型建设&#xff0c;为业务发展提供创新动力&#xff0c;目标是利用金融科技和…

论C/C++的条件编译#if、#ifdef、#ifndef、#undef

我们以实例来演示&#xff1a; ------------------------------------------实验①------------------------------------------ 子函数&#xff1a;主函数&#xff1a;当定义了COMMENT_FLAG该宏&#xff0c;且其为0&#xff0c;则运行结果如下&#xff1a;只执行了sub_func_1函…

21、鸿蒙Harmony Next开发:组件导航(Navigation)

目录 设置页面显示模式 设置标题栏模式 设置菜单栏 设置工具栏 路由操作 页面跳转 页面返回 页面替换 页面删除 移动页面 参数获取 路由拦截 单例跳转 子页面 页面显示类型 页面生命周期 页面监听和查询 页面转场 关闭转场 自定义转场 共享元素转场 跨包…

“外卖大战”正在改变国内“大零售”

出品 | 何玺排版 | 叶媛7月18日&#xff0c;市场监管总局约谈美团、饿了么、京东三家外卖平台&#xff0c;要求“理性竞争、规范促销”&#xff0c;剑指近期愈演愈烈的“0元购”“0.1秒杀”等外卖补贴乱象。但约谈之后&#xff0c;平台们是真整改&#xff0c;还是玩话术&#x…

当CAN握手EtherCAT:视觉检测系统的“双芯合璧”时代来了

在汽车制造的高速生产线上&#xff0c;设备间的“语言不通”曾是工程师们的头疼事&#xff1a;CAN总线像踏实的老司机&#xff0c;稳扎稳打传输传感器数据&#xff1b;而EtherCAT网关则是追求极致速度的“闪电侠”&#xff0c;主导着实时控制的重任。当视觉检测系统需要同时对接…

【C语言】动态内存管理全解析:malloc、calloc、realloc与free的正确使用

C语言学习 动态内存分配 友情链接&#xff1a;C语言专栏 文章目录C语言学习前言&#xff1a;一、为什么要有动态内存分配二、malloc和free2.1 malloc2.2 free三、calloc和realloc3.1 calloc3.2 realloc总结附录上文链接下文链接专栏前言&#xff1a; 在C语言编程中&#xff0…

基于Arduino智能家居环境监测系统—以光照强度检测修改

2 相关技术与理论 2.1 Arduino 技术 Arduino 是一款广受欢迎的开源电子原型平台&#xff0c;由硬件和软件组成&#xff0c;为开发者提供了便捷且低成本的解决方案&#xff0c;尤其适用于快速搭建交互式电子项目&#xff0c;在本智能家居环境监测系统中担当核心角色。​ 硬件方…

前端上传 pdf 文件 ,前端自己解析出来 生成界面 然后支持编辑

要在前端解析 PDF 文件并生成可编辑界面&#xff0c;我们可以使用 PDF.js 库来解析 PDF 内容&#xff0c;然后将其转换为可编辑的 HTML 元素。 主要特点和工作原理如下&#xff1a; PDF 解析&#xff1a; 使用 Mozilla 的 PDF.js 库解析 PDF 文件内容&#xff0c;提取文本信息。…

Linux“一切皆文件“设计哲学 与 Linux文件抽象层:struct file与file_operations的架构解析

在Linux系统中&#xff0c;“一切皆文件”&#xff08;Everything is a file&#xff09;是一个核心设计哲学&#xff0c;它抽象了系统资源的访问方式&#xff0c;使得几乎所有硬件设备、进程、网络连接等都可以通过统一的文件接口&#xff08;如open()、read()、write()、clos…

蓝桥杯零基础到获奖-第3章 C++ 变量和常量

蓝桥杯零基础到获奖-第3章 C 变量和常量 文章目录一、变量和常量1.变量的创建2.变量初始化3.变量的分类4.常量4.1 字⾯常量4.2 #define定义常量4.3 const 定义常量4.4 练习练习1&#xff1a;买票https://www.nowcoder.com/practice/0ad8f1c0d7b84c6d8c560298f91d5e66练习2&…

物理AI是什么技术?

当英伟达CEO黄仁勋在链博会上明确提出“物理AI将是AI的下一浪潮”时&#xff0c;这个看似陌生的概念瞬间引发了科技圈的广泛关注。究竟什么是物理AI&#xff1f;它与我们熟悉的人工智能有何不同&#xff1f;又将如何重塑我们与物理世界的交互方式&#xff1f; 物理AI&#xff1…

GRIB数据处理相关指令

GRIB 数据格式简介 GRIB(General Regularly distributed Information in Binary form)&#xff0c;是由世界气象组织&#xff08;WMO&#xff09;设计和维护的一种用于存储和传输网格数据的标准数据格式&#xff0c;它是一种自描述的二进制压缩格式&#xff0c;通常具有扩展名…

微服务学习(六)之分布式事务

微服务学习&#xff08;六&#xff09;之分布式事务一、认识Seata二、部署TC服务1、准备数据库表2、准备配置文件3、docker部署三、微服务集成seata1、引入依赖2、改造配置3、添加数据库表4、测试四、XA模式1、两阶段提交2、seata的XA模型3、优缺点4、实现步骤五、AT模式1、Sea…

Go实现用户登录小程序

写一个用户登录注册的小程序 运行程序&#xff0c;给出提示1. 注册输入用户名、密码、年龄、性别 {"用户名": "root", "passwd": "123456", "age": 18, "sex": "男"}注册前要判断是否存在此用户2. 登录…

鸿蒙蓝牙通信

https://developer.huawei.com/consumer/cn/doc/best-practices/bpta-bluetooth-low-energy 蓝牙权限 module.json5 {"module": {"requestPermissions": [{"name": "ohos.permission.ACCESS_BLUETOOTH","reason": "…

Java:Map

文章目录Map常用方法Map遍历的三种方法先获取Map集合的全部键&#xff0c;再通过遍历来找值Entry对象forEach结合lambda表达式Map 案例分析需求我的代码&#xff08;不好&#xff09;老师的代码&#xff08;好&#xff09;好在哪里另外集合分为Collection和MapMap常用方法 代码…

fastjson2 下划线字段转驼峰对象

在对接第三方或查询数据库时&#xff0c;返回的字段是下划线分隔的&#xff0c;而在业务中需要转成java对象&#xff0c;java对象的字段是驼峰的&#xff0c;使用fastjson2时&#xff0c;有两种方法可以实现&#xff1a; 比如数据格式是&#xff1a; {"item_id": &q…

【硬件】蓝牙音频协议

1. 无线音频传输的工作原理 在无线传输的过程中&#xff0c;音源设备首先将MP3、FLAC等音频文件还原为PCM格式。通过蓝牙音频编码转为蓝牙无线传输的文件&#xff0c;发送到音频设备段。将蓝牙无线传输的文件再次还原为PCM格式&#xff0c;之后转为模拟信号并放大&#xff0c;通…

【宇树科技:未来1-3年,机器人可流水线打螺丝】

在第三届中国国际供应链促进博览会上&#xff0c;宇树科技工作人员表示&#xff0c;未来1到3年内&#xff0c;机器人产品有望从单一工业化产品&#xff0c;发展至复合化工业场景&#xff0c;如机器人搬完箱子后&#xff0c;换个 “手” 就能在流水线上打螺丝。在3到10年内&…