【封装工程】OI/ACM常用封装
前言
笔者有的时候无聊,就将一些奇怪的东西封装起来。
范围主要是在\(OI\)或者\(ACM\)中的常见数据结构等。
随着笔者的能力的提升,可能会对原来的封装程序进行修改,并且保留原来的版本。
【ST表(静态RMQ)】
// program at 2019-11-12
template <class T, int N, int M>
struct ST {
T f[N + 5][M + 1];
int log2[N];
T compare(T x, T y) { // can change, depend on the priority of problem
return x < y ? x : y;
}
void init(int* a) {
log2[0] = -1;
for (int i = 1; i <= N; i++)
f[i][0] = a[i], log2[i] = log2[i >> 1] + 1;
for (int j = 1; j <= M; j++)
for (int i = 1; i + (1 << j) - 1 <= N; i++)
f[i][j] = compare(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
}
T query(int l, int r) {
int k = log2[r - l + 1];
return compare(f[l][k], f[r - (1 << k) + 1][k]);
}
};
高精度
四位压位高精,支持高精加高精、高精减高精(\(a>b\))、高精乘高精、高精乘低精。
// program at 2019-11-14
struct bigint {
static const int BASE = 10000;
static const int SIZE = 105
int len, a[SIZE];
bigint() {
memset(a, 0, sizeof a);
len = 1;
}
bigint(long long x) { *this = x; }
void operator=(int x) {
len = 0;
while (x) {
a[++len] = x % BASE;
x /= BASE;
}
}
friend bigint operator+(bigint a, bigint b) {
bigint c;
c.len = max(a.len, b.len) + 1;
for (int i = 1, x = 0; i <= c.len; i++)
c.a[i] = a.a[i] + b.a[i] + x, x = c.a[i] / BASE, c.a[i] %= BASE;
while (!c.a[c.len]) c.len--;
return c;
}
friend bigint operator-(bigint a, bigint b) {
a.len = max(a.len, b.len);
for (int i = 1; i <= a.len; i++) {
if (a.a[i] < b.a[i])
a.a[i] += BASE, a.a[i + 1]--;
a.a[i] -= b.a[i];
}
while (!a.a[a.len]) a.len--;
return a;
}
friend bigint operator*(bigint a, bigint b) {
bigint c;
c.len = a.len + b.len;
for (int i = 1; i <= a.len; i++) {
int x = 0;
for (int j = 1; j <= b.len; j++)
c.a[i + j - 1] += a.a[i] * b.a[j] + x, x = c.a[i + j - 1] / BASE, c.a[i + j - 1] %= BASE;
c.a[b.len + i] = x;
}
while (!c.a[c.len]) c.len--;
return c;
}
friend bigint operator*(bigint a, int b) {
a.len += 3;
for (int i = 1; i <= a.len; i++) a.a[i] *= b;
for (int i = 1; i <= a.len; i++) a.a[i + 1] += a.a[i] / BASE, a.a[i] %= BASE;
while (!a.a[a.len]) a.len--;
return a;
}
void print() {
printf("%d", a[len]);
for (int i = len - 1; i >= 1; i--) printf("%04d", a[i]);
puts("");
}
bigint& operator+=(bigint b) {
*this = *this + b;
return *this;
}
bigint& operator-=(bigint b) {
*this = *this - b;
return *this;
}
bigint& operator*=(bigint b) {
*this = *this * b;
return *this;
}
};
【封装工程】OI/ACM常用封装的更多相关文章
- React Native之TextInput的介绍与使用(富文本封装与使用实例,常用输入框封装与使用实例)
React Native之TextInput的介绍与使用(富文本封装与使用实例,常用输入框封装与使用实例) TextInput组件介绍 TextInput是一个允许用户在应用中通过键盘输入文本的基本组 ...
- Lua常用封装方法
Lua 获取随机值 --获取随机值,指定上限和下限 function getRandom(min,max) -- 接收一个整数n作为随即序列的种子 math.randomseed(os.time()) ...
- Java 字节的常用封装
一. Java 的字节 byte (字节) 是 Java 中的基本数据类型,一个 byte 包含8个 bit(位),byte 的取值范围是-128到+127. byte 跟 Java 其他基本类型的关 ...
- 多态、Object类和 JDK常用封装类型
多态 定义:某一类事物的多种存在形态. 多态的体现:父类的引用指向了自己的子类对象.父类的引用也可以接收自己的子类对象. 多态的前提:必须是类与类之间有关系,要么继承,要么实现.通常还有一个前提,存在 ...
- 第一百六十一节,封装库--JavaScript,完整封装库文件
封装库--JavaScript,完整封装库文件 /** *feng_zhuang_ku_1.0版本,js封装库,2016/12/29日:林贵秀 **/ /** 前台调用 * 每次调用$()创建库对象, ...
- ACM常用算法及练习(2)
ACM常用算法及练习 知识类型 重要度 容易度 应掌握度 典型题 其他 数据结构(5) 链表 ★★☆ ★★★ ★★☆ 栈 stack ★★★ ★★★ ★★★ HLoj120 ...
- ACM常用算法及练习(1)
ACM常用算法及练习 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码,因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打出来. 1.最短 ...
- Redis操作Hash工具类封装,Redis工具类封装
Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...
- Redis操作字符串工具类封装,Redis工具类封装
Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...
随机推荐
- servlet_filterj简介
Filter总结: 1):Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, ...
- typescript中新增的基本数据类型
javascript中有7种数据类型,分别是:boolean,number,string,null,undefined和object,以及在es6中新增的一种类型 symbol.而typescript ...
- [BZOJ4180] 字符串计数
膜一发KsCla巨佬 #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=2e5 ...
- MySQL_入手<二>之删--改--查
接上 上篇文章继续 查询 # 比较运算 # 根据WHERE条件查找数据: = > < >= <= != select * from t_hero where age < ...
- certutil 命令配合PS反弹后门
Certutil.exe是一个命令行程序,作为证书服务的一部分安装.您可以使用Certutil.exe转储和显示证书颁发机构(CA)配置信息,配置证书服务,备份和还原CA组件以及验证证书,密钥对和证书 ...
- redis 学习(二)-- 通用命令
redis 学习(二)-- 通用命令 1. keys pattern 含义:查找所有符合给定模式(pattern)的key 命令 含义 keys * 遍历所有 key keys he[h-l]* 遍历 ...
- node工具之http-proxy-middleware
简介 一个轻松的配置代理服务器的中间件,让Node.js代理变得简单 url路径 foo://example.com:8042/over/there?name=ferret#nose \_/ \___ ...
- vue路由公用
大体思路,一个页面,多个按钮,点击按钮后都跳转到一个路由:通过父亲传的值是什么,来决定跳那个路由:ajax数据也是通过判断来决定拉那个数据 路由: export default { routes: [ ...
- NLP采用Bert进行简单文本情感分类
参照当Bert遇上Kerashttps://spaces.ac.cn/archives/6736此示例准确率达到95.5%+ https://github.com/CyberZHG/keras-ber ...
- PPT中准确插入公式
Mathtype直接输入到PPT中的公式,漂移且太小.一般的文本公式不美观. 将Mathtype的公式作为文本形式出现,既解决了漂移和大小的问题,也兼顾的美观. 具体操作如下: 1 打开Mathtyp ...