快排

procedure qsort(l,r:longint);
var i,j,t,m:longint;
begin i:=l; j:=r;
m:=a[(i+j) div ];
repeat
while a[i]<m do inc(i);
while m<a[j] do dec(j);
if i<=j then
begin t:=a[i]; a[i]:=a[j]; a[j]:=t;
inc(i); dec(j); end; until i>j;
if i<r then qsort(i,r); if j>l then qsort(l,j); end;

1.数组开两倍

procedure insert(x:longint);
var t,s,v:longint;
begin
len:=len+; f[len]:=x; s:=len;
while (s<>)and(f[s div ]>f[s]) do
begin
v:=f[s div ]; f[s div ]:=f[s]; f[s]:=v;
s:=s div ;
end;
end;
function get:longint;
var t,s,v:longint;
begin
get:=f[]; f[]:=f[len]; len:=len-; t:=;
while (t*<=len)or(t*+<=len) do
begin
if (t*+>len)or(f[t*]<f[t*+])
then s:=t* else s:=t*+;
if f[t]>f[s] then
begin
v:=f[t]; f[t]:=f[s]; f[s]:=v; t:=s;
end
else break;
end;
end;

欧拉函数(单个数值)

function phi(x:longint):longint;
var ans,i:longint;
begin
ans:=x;
for i:= to trunc(sqrt(x)) do
begin
if x mod i= then ans:=ans div i*(i-);
while x mod i= do x:=x div i;
end;
if x<> then ans:=ans div x*(x-);
exit(ans);
end;

欧拉函数(批量处理)

procedure work;
var i,j:longint;
begin
p:=;
for i:= to n do e[i]:=i;
for i:= to n do
if e[i]=i then
begin j:=i;
while j<=n do begin e[j]:=e[j] div i*(i-); inc(j,i); end;
end;
for i:= to n do
begin
if e[i]=i- then begin inc(p); prime[p]:=i; end;
end;
end;

质数筛法

procedure work(n:longint);
var i,j:longint;
begin
num:=;
for i:= to n do g[i]:=true;
for i:= to n do
if g[i]=true then
begin
inc(num); prime[num]:=i;
j:=i*;
while j<=n do begin g[j]:=false; j:=j+i; end;
end;
end;

质因数分解

procedure fenjie(x:longint);
var i,y:longint;
begin
t:=;y:=x;
for i:= to num do
begin
if prime[i]>y div then exit;
if x mod prime[i]= then
begin
inc(t); w[t]:=prime[i];
while x mod prime[i]= do x:=x div prime[i];
end;
if x= then break;
end;
end;

快速幂

function quick(x,n,p:qword):qword;
var ans:qword;
begin
ans:=;
while n> do
begin
if n and > then ans:=cheng(ans,x,p);
x:=cheng(x,x,p);
n:=n>>;
end;
exit(ans);
end;

快速乘

function cheng(x,n,p:qword):qword;
var ans:qword;
begin
ans:=;
while n> do
begin
if n and > then ans:=(ans+x) mod p;
x:=(x+x) mod p;
n:=n>>;
end;
exit(ans);
end;

拓扑排序

for i:= to n do if u[i]= then begin inc(t); f[t]:=i; end;
repeat
x:=f[t]; dec(t); inc(num);
new(p); p:=a[x];
while p<>nil do
begin
y:=p^.data;
dec(u[y]);
if u[y]= then
begin
inc(t); f[t]:=y;
end;
p:=p^.next;
end;
until num=n;

强连通分量tarjan

思路:dfn为遍历到各点的时间,low表示每个点能够追溯到的最早的栈中节点的次序号 instack表示结点是否在栈中。

对图遍历如果x的儿子y没访问过,则访问并用low[y]更行low[x],如果x的儿子y被访问过,则用dfn[y]更新low[x],这样就知道了x最早回溯到的点(在栈中),在栈中low[x]相等的值构成强连通分量。

procedure tarjan(x:longint);
var y:longint; p:point;
begin
inc(s); low[x]:=s; dfn[x]:=s; inc(t); q[t]:=x;
instack[x]:=true;
new(p); p:=a[x];
while p<>nil do
begin
y:=p^.x;
if dfn[y]= then begin tarjan(y); low[x]:=min(low[x],low[y]); end
else if instack[y]=true then low[x]:=min(low[x],dfn[y]);
p:=p^.next;
end;
if dfn[x]=low[x] then
begin
inc(num);
repeat
y:=q[t]; dec(t); f[y]:=x;instack[y]:=false;
until x=y;
end;
end;

主程序

 for i:= to n do begin dfn[i]:=; low[i]:=; w[i]:=; instack[i]:=false; end;
s:=; t:=; num:=;
for i:= to n do if dfn[i]= then tarjan(i);

树的直径

思路:求树上最长路,对于各个点,求出它儿子中最长和次长的链相加。

procedure dfs(x,e:longint);
var p:point; y:longint;
begin
s1[x]:=x; s2[x]:=x; new(p); p:=a[x];
while p<>nil do
begin
y:=p^.x;
if y=e then begin p:=p^.next; continue; end;
dfs(y,x);
if f1[y]+p^.v>f1[x] then
begin
f2[x]:=f1[x]; s2[x]:=s1[x];
f1[x]:=f1[y]+p^.v; s1[x]:=y;
end
else
if f1[y]+p^.v>f2[x] then
begin
f2[x]:=f1[y]+p^.v; s2[x]:=y;
end;
p:=p^.next;
end;
if f1[x]+f2[x]>f1[max]+f2[max] then
max:=x;
end;

kmp算法

  p[]:=; j:=;
for i:= to m do
begin
while (j>)and(b[j+]<>b[i]) do j:=p[j];
if b[j+]=b[i] then inc(j);
p[i]:=j;
end;
j:=; kmp:=;
for i:= to n do
begin
while (j>)and(b[j+]<>a[i]) do j:=p[j];
if b[j+]=a[i] then inc(j);
if j=m then begin inc(kmp); j:=p[j]; end;
end;

LCA

倍增

1.不要忘记dfs后调用work

2.注意枚举2^i时要包含0,注意顺序

procedure work;
var i,j:longint;
begin
for i:= to do
for j:= to n do
f[i+,j]:=f[i,f[i,j]];
end;
function lca(x,y:longint):longint;
var i,t:longint;
begin
if deep[x]<deep[y] then begin t:=x; x:=y; y:=t; end;
for i:= downto do
if ((deep[x]-deep[y])>>i)and = then x:=f[i,x];
if x=y then exit(x);
for i:= downto do
if f[i,x]<>f[i,y] then
begin x:=f[i,x]; y:=f[i,y]; end;
exit(f[,x]);
end;

二分图

染色

function dfs(x,color:longint):boolean;
var p:point; y:longint;
begin
new(p); p:=w[x]; g[x]:=color;
while p<>nil do
begin
y:=p^.x;
if g[y]=color then exit(false);
if (g[y]=)and(not dfs(y,-color)) then exit(false);
p:=p^.next;
end;
exit(true);
end;
function cheak(s:longint):boolean;
var i:longint;
begin
for i:= to n do g[i]:=;
for i:= to n do
if g[i]= then
if not dfs(i,) then exit(false);
exit(true);
end;

组合

费马小定理,快速求C(n,m)的值

procedure make;
var i:longint;
begin
f[]:=; g[]:=;
for i:= to n do
begin
f[i]:=(f[i-]*i) mod num;
g[i]:=g[i-]*quick(i,num-) mod num;
end;
end;
function get(x,y:int64):int64;
var i:longint; t:int64;
begin
if (x=)or(y=)or(x=y) then exit();
exit(((f[x]*g[x-y]) mod num)*g[y] mod num);
end;

同余

ax≡1(mod m)

program asdf;
var
a,m,x,y,t:longint;
function extgcd(a,b:longint;var x,y:longint):longint;
var d:longint;
begin
if b<> then begin d:=extgcd(b,a mod b,y,x); y:=y-(a div b)*x; end
else begin x:=;y:=; d:=a; end;
exit(d);
end;
begin
readln(a,m);
x:=; y:=;
t:=extgcd(a,m,x,y);
writeln((x+m) mod m);
end.

NOIP模板的更多相关文章

  1. NOIP模板总结

    NOIP模板总结 进考场先打一份缺省源: # include <cstdio> # include <iostream> # include <cstring> # ...

  2. NOIP模板整理计划

    先占个坑 [update]noip结束了,弃了 一.图论 1.单源最短路 洛谷P3371 (1)spfa 已加SLF优化 #include <iostream> #include < ...

  3. 【模板】NOIP模板汇总

    图论 数据结构 数学 其他: 洛谷模板:a,b两个字符串,求b串在a串中出现的位置 #include<iostream> #include<cstdio> #include&l ...

  4. noip模板复习

    自己敲模板还是有很多容易错的地方 写在注释里面了 LCA #include<bits/stdc++.h> #define REP(i, a, b) for(register int i = ...

  5. NOIP经典基础模板总结

    date: 20180820 spj: 距离NOIP还有81天 目录 STL模板: priority_queue 的用法:重载<,struct cmpqueue 的用法 stack 的用法vec ...

  6. NOIP常见模板集合

    Preface 这篇博客记录的是我联赛前虽然只有两天了的打板子记录. 只求真的能给我起到些作用吧,一般按照难度排序. 而且从这篇博客开始我会用H3的标题代替H4 为了节约篇幅,以下的代码一般均以cla ...

  7. NOIP前的模板复习和注意事项

    联赛除去今天刚好只有一个星期了,最后一个星期也很关键,要吃好睡好保持心情愉悦.当然也免不了最后的复习计划. 首先是模板,之前还有很多模板没有复习到,这些东西是一定要落实到位的. 每天往后面写一点... ...

  8. NOIP的模板--考前复习

    距离NOIP还有25天 可以去放弃一些巨难得题目去搞一些模板了 -------在校老师的原话 一·快排 虽然可以手打,最好用STL,里面有很多优化,会快很多 #include<iostream& ...

  9. 纪中10日T1 2300. 【noip普及组第一题】模板题

    2300. [noip普及组第一题]模板题 (File IO): input:template.in output:template.out 时间限制: 1000 ms  空间限制: 262144 K ...

随机推荐

  1. 关于Navicat连接MySQL 报 Authentication plugin 'caching_sha2_password' cannot be loaded

    报错原因: 报这个错是因为MySQL8使用了 caching_sha2_password 加密方式而之前MySQL使用的是 mysql_native_password 加密方式,而你的Navicat不 ...

  2. Mit6.824 Lab1-MapReduce

    前言 Mit6.824 是我在学习一些分布式系统方面的知识的时候偶然看到的,然后就开始尝试跟课.不得不说,国外的课程难度是真的大,一周的时间居然要学一门 Go 语言,然后还要读论文,进而做MapRed ...

  3. 13.6 模拟事件【JavaScript高级程序设计第三版】

    事件,就是网页中某个特别值得关注的瞬间.事件经常由用户操作或通过其他浏览器功能来触发. 但很少有人知道,也可以使用JavaScript 在任意时刻来触发特定的事件,而此时的事件就如同浏览器创建的事件一 ...

  4. keil5 mdk调用外部编辑器notepad++、sublime3、VSCode总结

    1.打开keil主界面,点击菜单栏Tools菜单,选择如下图所示的选项. 2.点击如下图所示的菜单上红笔标注的地方,给这个工具命名,如notepad++.sublime3.vscode等,如下图, 并 ...

  5. python内置模块[re]

    python内置模块[re] re模块: python的re模块(Regular Expression正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工 ...

  6. 【多线程】 Task ,async ,await

    [多线程]Task ,async ,await 一. WinForm 里经常会用到多线程, 多线程的好出就不多说了,来说说多线程比较麻烦的地方 1. UI 线程与其他线程的同步,主要是 Form 和 ...

  7. 还原T4模板执行前的警告对话框

    T4模板在保存的时候都会弹出个对话框,确认是否立即执行,大部分情况下我是不想立即执行的,所以一般都点Cancel,只有想执行的时候才点OK. 今天操作的时候不小心勾选了“Do not show thi ...

  8. 「个人训练」Copying Books(UVa714)

    好久不更新主要是怠惰了....还要加强训练. 题意分析与思路 注意到这样一句话: our goal is to minimize the maximum number of pages assigne ...

  9. Linux 下安装Python报错:zlib not available

    问题描述: 在Linux下安装Python时出现一个错误:zipimport.ZipImportError: can't decompress data; zlib not available 详细错 ...

  10. 第三十三篇 Python中关于OOP(面向对象)的常用术语

    面向对象的优点 从编程进化论可知,面向对象是一种更高等级的结构化编程方式,它的好处主要有两点: 1. 通过封装明确了内外,你做为类的缔造者,你就是女娲,女娲造物的逻辑别人无需知道,女娲想让你知道,你才 ...