some 模板
注:此博客部分模板(也有可能是全部)来源于其它大佬的bolg
1.高精度
#include<bits/stdc++.h>
#define MAXN 501
const int Big_B = ; const int Big_L = ;
inline int intcmp_ (int a, int b) { if (a > b) return ; return a < b ? - : ; }
struct Int
{
#define rg register
inline int max (int a, int b) { return a > b ? a : b; }
inline int min (int a, int b) { return a < b ? a : b; }
std :: vector <int> c; Int () {} typedef long long LL;
Int (int x) { for (; x > ; c.push_back (x % Big_B), x /= Big_B); }
Int (LL x) { for (; x > ; c.push_back (x % Big_B), x /= Big_B); }
inline void CrZ () { for (; !c.empty () && c.back () == ; c.pop_back ()); }
inline Int &operator += (const Int &rhs){
c.resize (max (c.size (), rhs.c.size ())); rg int i, t = , S;
for (i = , S = rhs.c.size (); i < S; ++ i)
c[i] += rhs.c[i] + t, t = c[i] >= Big_B, c[i] -= Big_B & (-t);
for (i = rhs.c.size (), S = c.size (); t && i < S; ++ i)
c[i] += t, t = c[i] >= Big_B, c[i] -= Big_B & (-t);
if (t) c.push_back (t); return *this;
}
inline Int &operator -= (const Int &rhs){
c.resize (max (c.size (), rhs.c.size ())); rg int i, t = , S;
for (i = , S = rhs.c.size (); i < S; ++ i)
c[i] -= rhs.c[i] + t, t = c[i] < , c[i] += Big_B & (-t);
for (i = rhs.c.size (), S = c.size (); t && i < S; ++ i)
c[i] -= t, t = c[i] < , c[i] += Big_B & (-t);
CrZ (); return *this;
}
inline Int &operator *= (const Int &rhs){
rg int na = c.size (), i, j, S, ai;
c.resize (na + rhs.c.size ()); LL t;
for (i = na - ; i >= ; -- i){
ai = c[i], t = , c[i] = ;
for (j = , S = rhs.c.size (); j < S; ++ j){
t += c[i + j] + (LL) ai * rhs.c[j];
c[i + j] = t % Big_B, t /= Big_B;
}
for (j = rhs.c.size (), S = c.size (); t != && i + j < S; ++ j)
t += c[i + j], c[i + j] = t % Big_B, t /= Big_B;
assert (t == );
}
CrZ (); return *this;
}
inline Int &operator /= (const Int &rhs) { return *this = div (rhs); }
inline Int &operator %= (const Int &rhs) { return div (rhs), *this; }
inline Int &shlb (int l = ){
if (c.empty ()) return *this; c.resize (c.size () + l);rg int i;
for (i = c.size () - ; i >= l; -- i) c[i] = c[i - l];
for (i = ; i < l; ++ i) c[i] = ;
return *this;
}
inline Int &shrb (int l = ){
for (rg int i = ; i < c.size () - l; ++ i) c[i] = c[i + l];
c.resize (max (c.size () - l, )); return *this;
}
inline Int div (const Int &rhs){
assert (!rhs.c.empty ()); Int q, r; rg int i; if (rhs > *this) return ;
q.c.resize (c.size () - rhs.c.size () + ); rg int _l, _r, mid;
for (i = c.size () - ; i > c.size () - rhs.c.size (); -- i) r.shlb (), r += c[i];
for (i = c.size () - rhs.c.size (); i >= ; -- i){
r.shlb (); r += c[i];
if (r.Comp (rhs) < ) q.c[i] = ;
else {
_l = , _r = Big_B;
for (; _l != _r; ){
mid = _l + _r >> ;
if ((rhs * mid).Comp (r) <= ) _l = mid + ; else _r = mid;
}
q.c[i] = _l - , r -= rhs * q.c[i];
}
}
q.CrZ (), *this = r; return q;
}
inline int Comp (const Int &rhs) const {
if (c.size () != rhs.c.size ()) return intcmp_ (c.size (), rhs.c.size ());
for (rg int i = c.size () - ; i >= ; -- i)
if (c[i] != rhs.c[i]) return intcmp_ (c[i], rhs.c[i]);
return ;
}
friend inline Int operator + (const Int &lhs, const Int &rhs)
{ Int res = lhs; return res += rhs; }
inline friend Int operator - (const Int &lhs, const Int &rhs){
if (lhs < rhs){
putchar ('-');
Int res = rhs; return res -= lhs;
}
else { Int res = lhs; return res -= rhs; }
}
friend inline Int operator * (const Int &lhs, const Int &rhs)
{ Int res = lhs; return res *= rhs; }
friend inline Int operator / (const Int &lhs, const Int &rhs)
{ Int res = lhs; return res.div (rhs); }
friend inline Int operator % (const Int &lhs, const Int &rhs)
{ Int res = lhs; return res.div (rhs), res; }
friend inline std :: ostream &operator << (std :: ostream &out, const Int &rhs){
if (rhs.c.size () == ) out << "";
else {
out << rhs.c.back ();
for (rg int i = rhs.c.size () - ; i >= ; -- i)
out << std :: setfill ('') << std :: setw (Big_L) << rhs.c[i];
}
return out;
}
friend inline std :: istream &operator >> (std :: istream &in, Int &rhs){
static char s[];
in >> s + ; int Len = strlen (s + );
int v = ; LL r = , p = ;
for (rg int i = Len; i >= ; -- i){
++ v; r = r + (s[i] - '') * p, p *= ;
if (v == Big_L) rhs.c.push_back (r), r = , v = , p = ;
}
if (v != ) rhs.c.push_back (r); return in;
}
friend inline bool operator < (const Int &lhs, const Int &rhs)
{ return lhs.Comp (rhs) < ; }
friend inline bool operator <= (const Int &lhs, const Int &rhs)
{ return lhs.Comp (rhs) <= ; }
friend inline bool operator > (const Int &lhs, const Int &rhs)
{ return lhs.Comp (rhs) > ; }
friend inline bool operator >= (const Int &lhs, const Int &rhs)
{ return lhs.Comp (rhs) >= ; }
friend inline bool operator == (const Int &lhs, const Int &rhs)
{ return lhs.Comp (rhs) == ; }
friend inline bool operator != (const Int &lhs, const Int &rhs)
{ return lhs.Comp (rhs) != ; }
#undef rg
};
int Main (){
return ;
}
int ZlycerQan = Main ();
int main (int argc, char *argv[]) {;}
高精度
2.KMP算法
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int next[];
int la, lb, j;
char a[], b[];
int main () {
cin >> a + ;
cin >> b + ;
la = strlen (a + );
lb = strlen (b + );
for (int i = ; i <= lb; i++) {
while (j && b[i] != b[j + ])
j = next[j];
if (b[j + ] == b[i])j++;
next[i] = j;
}
j = ;
for (int i = ; i <= la; i++) {
while (j && b[j + ] != a[i])
j = next[j];
if (b[j + ] == a[i])j++;
if (j == lb) {
printf ("%d\n", i - lb + );
j = next[j];
}
}
for (int i = ; i <= lb; i++)
printf ("%d ", next[i]);
return ;
}
KMP
求nxt数组是自己匹配自己从2开始
求最大匹配是从1开始遍历被匹配的串
3.加入堆优化的Dijkstra
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = ;
int n, m, s, cnt, head[N], dis[N];
bool vis[N];
struct node{
int next, to, w;
}e[N];
int read() {
int s = , w = ;
char ch = getchar();
while(!isdigit(ch)){if(ch == '-') w = -;ch = getchar();}
while(isdigit(ch)){s = s * + ch - '';ch = getchar();}
return s * w;
}
void add(int x, int y, int z) {
e[++cnt].next = head[x];
e[cnt].to = y;
e[cnt].w = z;
head[x] = cnt;
}
struct Node {
int u, v;
bool operator<(const Node &b) const {
return u > b.u;
}
};
void dijikstra(int s) {
priority_queue <Node> q;
memset(dis, 0x3f3f3f3f, sizeof(dis));
dis[s] = ;
Node o;
o.u = ;
o.v = s;
q.push(o);
while(!q.empty()) {
int u = q.top().v;
int d = q.top().u;
q.pop();
if(d != dis[u])continue;
for(int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
int w = e[i].w;
if(dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
Node p;
p.u = dis[v], p.v = v;
q.push(p);
}
}
}
}
int main () {
n = read();
m = read();
s = read();
while(m--) {
int x, y, z;
x = read();
y = read();
z = read();
add (x, y, z);
}
dijikstra(s);
for(int i = ; i <= n; i++)
printf("%d ", dis[i]);
return ;
} 堆优化 迪杰
Dijkstra
4.未加入堆优化的Dijkstra
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = ;
int head[N], cnt, n, m, s, pre[N];
long long dis[N];
bool vis[N];
struct node {
int next, to;
long long w;
}e[N];
void add(int x, int y, long long z) {
e[++cnt].next = head[x];
e[cnt].to = y;
e[cnt].w = z;
head[x] = cnt;
}
void dijkstra(int s) {
for(int i = ; i <= n; i++) dis[i] = ;
dis[s] = ;
for(int i = ; i <= n; i++) {
int k = , maxn = ;
for(int j = ; j <= n; j++)
if(!vis[j] && dis[j] <= maxn)
k = j, maxn = dis[j];
vis[k] = ;
for(int j = head[k]; j; j = e[j].next)
if(dis[e[j].to] > dis[k] + e[j].w)
dis[e[j].to] = dis[k] + e[j].w/*, pre[e[j].to] = k*/;
}
}
int main () {
scanf("%d%d%d", &n, &m, &s);
for(int i = ; i <= m; i++) {
int x, y;
long long z;
scanf("%d%d%lld", &x, &y, &z);
add(x, y, z);
}
dijkstra(s);
for(int i = ; i <= n; i++)
printf("%lld ", dis[i]);
/*while(pre[n]) {
cout << pre[n] << " ";
n = pre[n];
}*/
return ;
}
朴素 dijkstra
注释掉的是记录路径的方法
5.SPFA
#include <iostream>
#include <cstdio>
#include <queue>
#define N 500005
#define inf 2147483647
using namespace std;
int n, m, s, cnt;
int dis[N], vis[N], head[N];
struct node {
int next, to, w;
}tr[N];
void add (int x, int y, int z) {
tr[++cnt].to = y;
tr[cnt].next = head[x];
tr[cnt].w = z;
head[x] = cnt;
}
void spfa () {
queue<int> q;
for (int i = ; i <= n; i++)
dis[i] = inf;
vis[s] = ;
q.push(s);
dis[s] = ;
while (!q.empty()) {
int he = q.front();
q.pop();
vis[he] = ;
for (int i = head[he]; i ;i = tr[i].next) {
if (dis[tr[i].to] > dis[he] + tr[i].w) {
dis[tr[i].to] = dis[he] + tr[i].w;
if (!vis[tr[i].to]) {
vis[tr[i].to] = ;
q.push(tr[i].to);
}
}
}
}
}
int main () {
scanf ("%d%d%d", &n, &m, &s);
for (int i = ; i <= m; i++) {
int a, b, c;
scanf ("%d%d%d", &a, &b, &c);
add (a, b, c);
}
spfa ();
for (int i = ; i <= n; i++)
if (s == i) printf ("0 ");
else printf ("%d ", dis[i]);
return ;
} spfa
SPFA
6.Tire树
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char ch[];
int n, m, tr[][], s, root, tot, f[];
int main () {
scanf ("%d", &n);
for (int i = ; i <= n; i++) {
root = ;
cin >> ch;
int len = strlen (ch);
for (int j = ; j < len; j++) {
int nu = ch[j] - 'a';
if (!tr[root][nu]) tr[root][nu] = ++tot;
root = tr[root][nu];
}
f[root] = ;
}
scanf ("%d", &m);
for (int i = ; i <= m; i++) {
root = ;
cin >> ch;
int len = strlen (ch);
for (int j = ; j < len; j++) {
int nu = ch[j] - 'a';
if (!tr[root][nu]) {
printf ("WRONG\n");
break;
}
root = tr[root][nu];
if (j == len - && f[root] == ) printf ("REPEAT\n");
if (j == len - && f[root] == ) f[root] = ,printf ("OK\n");
}
}
return ;
}
Tire树
7.树状数组
#include <iostream>
#include <cstdio>
using namespace std;
int n, m, tr[];
int lowbit (int x) {
return x & (-x);
}
void add (int x, int k) {
while (x <= n) {
tr[x] += k;
x += lowbit (x);
}
}
int sum (int x) {
int ans = ;
while (x !=) {
ans += tr[x];
x -= lowbit (x);
}
return ans;
}
int main () {
scanf ("%d%d", &n, &m);
for (int i = ; i <= n; i++) {
int a;
scanf ("%d", &a);
add (i, a);
}
for (int i = ; i <= m; i++) {
int a, b, c;
scanf ("%d%d%d", &a, &b, &c);
if (a == ) add (b, c);
if (a == ) cout << sum(c) - sum (b - ) << endl;
}
return ;
}
单点修改+区间查询
#include <iostream>
#include <cstdio>
using namespace std;
int n, m, now, x, c[];
int lowbit (int x) {
return x & -x;
}
void build (int x, int y) {
while (x <= n) {
c[x] += y;
x += lowbit (x);
}
}
int sum (int x) {
int ans = ;
while (x) {
ans += c[x];
x -= lowbit (x);
}
return ans;
}
int main () {
scanf ("%d%d", &n, &m);
for (int i = ; i <= n; i++) {
scanf ("%d", &x);
build (i, x - now);
now = x;
}
while (m--) {
scanf ("%d", &x);
if (x == ) {
int l, r, w;
scanf ("%d%d%d", &l, &r, &w);
build (l, w);
build (r + , -w);
}
else if (x == ) {
scanf ("%d", &x);
printf ("%d\n", sum (x));
}
}
return ;
}
区间修改+单点查询
8.裴蜀定理
#include <cstdio>
#include <iostream>
using namespace std;
int n, ans, x, y;
int read() {
int s = , w = ;
char ch = getchar();
while(!isdigit(ch)) {if(ch == '-') w = -; ch = getchar();}
while(isdigit(ch)) {s = s * + ch - ''; ch = getchar();}
return s * w;
}
int gcd(int x, int y) {
// if(x < 0) x = -x;
// if(y < 0) y = -y;
return y == ? x : gcd(y, x % y);
}
int main() {
n = read();
x = read(), y = read();
if(x < ) x = -x;
if(y < ) y = -y;
ans = gcd(x, y);
for(int i = ; i <= n - ; i++) {
y = read();
if(y < ) y = -y;
ans = gcd(ans, y);
// x = ans;
}
cout << ans << endl;
return ;
}
裴蜀定理
9.gcd
int gcd(int x, int y) {
return y == ? x : gcd(y, x % y);
}
gcd
10.lcm(最小公倍数)
众所周知lcm(x, y) = x * y / gcd(x, y)
11.exgcd
void exgcd(int a, int b, int &x, int &y) {
if(!b) {x = , y = ;return;}
int d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
exgcd
12.欧拉筛
#include<iostream>
#include<cstdio>
using namespace std;
int n, p[], vis[],num;
int oulashai(int x) {
for(int i = ; i <= n; i++) {
if(!vis[i])p[++num] = i;
for(int j = ; j <= num; j++) {
if(i * p[j] > n) break;
vis[i * p[j]] = ;
if(i % p[j] == ) break;
}
}
}
int main() {
scanf("%d",&n);
oulashai(n);
for(int i = ; i <= num; i++)
printf("%d ",p[i]);
}
欧拉筛
就先整理到这里趴你们的博主太菜了
some 模板的更多相关文章
- Jade模板引擎让你飞
写在前面:现在jade改名成pug了 一.安装 npm install jade 二.基本使用 1.简单使用 p hello jade! 渲染后: <p>hello jade!</p ...
- ABP入门系列(2)——通过模板创建MAP版本项目
一.从官网创建模板项目 进入官网下载模板项目 依次按下图选择: 输入验证码开始下载 下载提示: 二.启动项目 使用VS2015打开项目,还原Nuget包: 设置以Web结尾的项目,设置为启动项目: 打 ...
- CMS模板应用调研问卷
截止目前,已经有数十家网站与我们合作,进行了MIP化改造,在搜索结果页也能看到"闪电标"的出现.除了改造方面的问题,MIP项目组被问到最多的就是:我用了wordpress,我用了织 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- 【原创分享·微信支付】C# MVC 微信支付之微信模板消息推送
微信支付之微信模板消息推送 今天我要跟大家分享的是“模板消息”的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信生存的呀,所以他能不 ...
- OpenCV模板匹配算法详解
1 理论介绍 模板匹配是在一幅图像中寻找一个特定目标的方法之一,这种方法的原理非常简单,遍历图像中的每一个可能的位置,比较各处与模板是否“相似”,当相似度足够高时,就认为找到了我们的目标.OpenCV ...
- 前端MVC学习总结(一)——MVC概要与angular概要、模板与数据绑定
一.前端MVC概要 1.1.库与框架的区别 框架是一个软件的半成品,在全局范围内给了大的约束.库是工具,在单点上给我们提供功能.框架是依赖库的.AngularJS是框架而jQuery则是库. 1.2. ...
- ThinkPHP+Smarty模板中截取包含中英文混合的字符串乱码的解决方案
好几天没写博客了,其实有好多需要总结的,因为最近一直在忙着做项目,但是困惑了几天的Smarty模板中截取包含中英文混合的字符串乱码的问题,终于解决了,所以记录下来,需要的朋友看一下: 出现乱码的原因: ...
- ThinkPHP 模板substr的截取字符串函数
ThinkPHP 模板substr的截取字符串函数在Common/function.php加上以下代码 /** ** 截取中文字符串 **/ function msubstr($str, $start ...
- DDD领域驱动设计 - 设计文档模板
设计文档模板: 系统背景和定位 业务需求描述 系统用例图 关键业务流程图 领域语言整理,主要是整理领域中的各种术语的定义,名词解释 领域划分(分析出子域.核心域.支撑域) 每个子域的领域模型设计(实体 ...
随机推荐
- addpath(),genpath()
clear all:clear clc: addpath(): 打开不在同一目录下的文件 addpath('sparse-coding');%sparse-coding,SIFT均表示路径,此目录下的 ...
- SWIG 3 中文手册——3. Windows 上使用 SWIG
目录 3 Windows 上使用 SWIG 后续章节 3 Windows 上使用 SWIG 暂时略过. 后续章节 <4. 脚本语言>
- 34,Leetcode 组合总和I,II -C++ 回溯法
I 题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的数字可以无 ...
- js 宏任务和微任务
.宏任务(macrotask )和微任务(microtask ) macrotask 和 microtask 表示异步任务的两种分类. 在挂起任务时,JS 引擎会将所有任务按照类别分到这两个队列中,首 ...
- HDU 5047 Sawtooth 找规律+拆分乘
Sawtooth Think about a plane: ● One straight line can divide a plane into two regions. ● Two lines ...
- WPF 通过名称查找属性(DependencyProperty)
使用名称来查找DependencyProperty. 如果有这样的需求,则是需要通过DependencyPropertyDescriptor来查找. 通常是使用附加属性或者依赖属性的方法. 下面给出附 ...
- [转] Performance_js中计算网站性能监控利器
1.Performance方法 Performance提供的方法可以灵活使用,获取到页面加载等标记的耗时情况. performance.now() //返回当前到页面打开时刻的耗时,精确到千分之一毫秒 ...
- HTML5 下拉控件绑定数据
<select id="CommunityList" class="form-control" > <option>請選擇社團</ ...
- linux系统shell基础知识入门二
条件判断语句 test或[],这两是等价的.但用[]这种可能看起来更简洁 必须在[符号和检查条件之间留出空格,而test命令之后也总是应该有一个空格 如果要把test 和then 放一行上,那么必须在 ...
- 开发技术--浅谈python基础知识
开发|浅谈python基础知识 最近复习一些基础内容,故将Python的基础进行了总结.注意:这篇文章只列出来我觉得重点,并且需要记忆的知识. 前言 目前所有的文章思想格式都是:知识+情感. 知识:对 ...