快排

  1. procedure qsort(l,r:longint);
  2. var i,j,t,m:longint;
  3. begin i:=l; j:=r;
  4. m:=a[(i+j) div ];
  5. repeat
  6. while a[i]<m do inc(i);
  7. while m<a[j] do dec(j);
  8. if i<=j then
  9. begin t:=a[i]; a[i]:=a[j]; a[j]:=t;
  10. inc(i); dec(j); end; until i>j;
  11. if i<r then qsort(i,r); if j>l then qsort(l,j); end;

1.数组开两倍

  1. procedure insert(x:longint);
  2. var t,s,v:longint;
  3. begin
  4. len:=len+; f[len]:=x; s:=len;
  5. while (s<>)and(f[s div ]>f[s]) do
  6. begin
  7. v:=f[s div ]; f[s div ]:=f[s]; f[s]:=v;
  8. s:=s div ;
  9. end;
  10. end;
  11. function get:longint;
  12. var t,s,v:longint;
  13. begin
  14. get:=f[]; f[]:=f[len]; len:=len-; t:=;
  15. while (t*<=len)or(t*+<=len) do
  16. begin
  17. if (t*+>len)or(f[t*]<f[t*+])
  18. then s:=t* else s:=t*+;
  19. if f[t]>f[s] then
  20. begin
  21. v:=f[t]; f[t]:=f[s]; f[s]:=v; t:=s;
  22. end
  23. else break;
  24. end;
  25. end;

欧拉函数(单个数值)

  1. function phi(x:longint):longint;
  2. var ans,i:longint;
  3. begin
  4. ans:=x;
  5. for i:= to trunc(sqrt(x)) do
  6. begin
  7. if x mod i= then ans:=ans div i*(i-);
  8. while x mod i= do x:=x div i;
  9. end;
  10. if x<> then ans:=ans div x*(x-);
  11. exit(ans);
  12. end;

欧拉函数(批量处理)

  1. procedure work;
  2. var i,j:longint;
  3. begin
  4. p:=;
  5. for i:= to n do e[i]:=i;
  6. for i:= to n do
  7. if e[i]=i then
  8. begin j:=i;
  9. while j<=n do begin e[j]:=e[j] div i*(i-); inc(j,i); end;
  10. end;
  11. for i:= to n do
  12. begin
  13. if e[i]=i- then begin inc(p); prime[p]:=i; end;
  14. end;
  15. end;

质数筛法

  1. procedure work(n:longint);
  2. var i,j:longint;
  3. begin
  4. num:=;
  5. for i:= to n do g[i]:=true;
  6. for i:= to n do
  7. if g[i]=true then
  8. begin
  9. inc(num); prime[num]:=i;
  10. j:=i*;
  11. while j<=n do begin g[j]:=false; j:=j+i; end;
  12. end;
  13. end;

质因数分解

  1. procedure fenjie(x:longint);
  2. var i,y:longint;
  3. begin
  4. t:=;y:=x;
  5. for i:= to num do
  6. begin
  7. if prime[i]>y div then exit;
  8. if x mod prime[i]= then
  9. begin
  10. inc(t); w[t]:=prime[i];
  11. while x mod prime[i]= do x:=x div prime[i];
  12. end;
  13. if x= then break;
  14. end;
  15. end;

快速幂

  1. function quick(x,n,p:qword):qword;
  2. var ans:qword;
  3. begin
  4. ans:=;
  5. while n> do
  6. begin
  7. if n and > then ans:=cheng(ans,x,p);
  8. x:=cheng(x,x,p);
  9. n:=n>>;
  10. end;
  11. exit(ans);
  12. end;

快速乘

  1. function cheng(x,n,p:qword):qword;
  2. var ans:qword;
  3. begin
  4. ans:=;
  5. while n> do
  6. begin
  7. if n and > then ans:=(ans+x) mod p;
  8. x:=(x+x) mod p;
  9. n:=n>>;
  10. end;
  11. exit(ans);
  12. end;

拓扑排序

  1. for i:= to n do if u[i]= then begin inc(t); f[t]:=i; end;
  2. repeat
  3. x:=f[t]; dec(t); inc(num);
  4. new(p); p:=a[x];
  5. while p<>nil do
  6. begin
  7. y:=p^.data;
  8. dec(u[y]);
  9. if u[y]= then
  10. begin
  11. inc(t); f[t]:=y;
  12. end;
  13. p:=p^.next;
  14. end;
  15. until num=n;

强连通分量tarjan

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

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

  1. procedure tarjan(x:longint);
  2. var y:longint; p:point;
  3. begin
  4. inc(s); low[x]:=s; dfn[x]:=s; inc(t); q[t]:=x;
  5. instack[x]:=true;
  6. new(p); p:=a[x];
  7. while p<>nil do
  8. begin
  9. y:=p^.x;
  10. if dfn[y]= then begin tarjan(y); low[x]:=min(low[x],low[y]); end
  11. else if instack[y]=true then low[x]:=min(low[x],dfn[y]);
  12. p:=p^.next;
  13. end;
  14. if dfn[x]=low[x] then
  15. begin
  16. inc(num);
  17. repeat
  18. y:=q[t]; dec(t); f[y]:=x;instack[y]:=false;
  19. until x=y;
  20. end;
  21. end;

主程序

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

树的直径

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

  1. procedure dfs(x,e:longint);
  2. var p:point; y:longint;
  3. begin
  4. s1[x]:=x; s2[x]:=x; new(p); p:=a[x];
  5. while p<>nil do
  6. begin
  7. y:=p^.x;
  8. if y=e then begin p:=p^.next; continue; end;
  9. dfs(y,x);
  10. if f1[y]+p^.v>f1[x] then
  11. begin
  12. f2[x]:=f1[x]; s2[x]:=s1[x];
  13. f1[x]:=f1[y]+p^.v; s1[x]:=y;
  14. end
  15. else
  16. if f1[y]+p^.v>f2[x] then
  17. begin
  18. f2[x]:=f1[y]+p^.v; s2[x]:=y;
  19. end;
  20. p:=p^.next;
  21. end;
  22. if f1[x]+f2[x]>f1[max]+f2[max] then
  23. max:=x;
  24. end;

kmp算法

  1. p[]:=; j:=;
  2. for i:= to m do
  3. begin
  4. while (j>)and(b[j+]<>b[i]) do j:=p[j];
  5. if b[j+]=b[i] then inc(j);
  6. p[i]:=j;
  7. end;
  8. j:=; kmp:=;
  9. for i:= to n do
  10. begin
  11. while (j>)and(b[j+]<>a[i]) do j:=p[j];
  12. if b[j+]=a[i] then inc(j);
  13. if j=m then begin inc(kmp); j:=p[j]; end;
  14. end;

LCA

倍增

1.不要忘记dfs后调用work

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

  1. procedure work;
  2. var i,j:longint;
  3. begin
  4. for i:= to do
  5. for j:= to n do
  6. f[i+,j]:=f[i,f[i,j]];
  7. end;
  8. function lca(x,y:longint):longint;
  9. var i,t:longint;
  10. begin
  11. if deep[x]<deep[y] then begin t:=x; x:=y; y:=t; end;
  12. for i:= downto do
  13. if ((deep[x]-deep[y])>>i)and = then x:=f[i,x];
  14. if x=y then exit(x);
  15. for i:= downto do
  16. if f[i,x]<>f[i,y] then
  17. begin x:=f[i,x]; y:=f[i,y]; end;
  18. exit(f[,x]);
  19. end;

二分图

染色

  1. function dfs(x,color:longint):boolean;
  2. var p:point; y:longint;
  3. begin
  4. new(p); p:=w[x]; g[x]:=color;
  5. while p<>nil do
  6. begin
  7. y:=p^.x;
  8. if g[y]=color then exit(false);
  9. if (g[y]=)and(not dfs(y,-color)) then exit(false);
  10. p:=p^.next;
  11. end;
  12. exit(true);
  13. end;
  14. function cheak(s:longint):boolean;
  15. var i:longint;
  16. begin
  17. for i:= to n do g[i]:=;
  18. for i:= to n do
  19. if g[i]= then
  20. if not dfs(i,) then exit(false);
  21. exit(true);
  22. end;

组合

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

  1. procedure make;
  2. var i:longint;
  3. begin
  4. f[]:=; g[]:=;
  5. for i:= to n do
  6. begin
  7. f[i]:=(f[i-]*i) mod num;
  8. g[i]:=g[i-]*quick(i,num-) mod num;
  9. end;
  10. end;
  11. function get(x,y:int64):int64;
  12. var i:longint; t:int64;
  13. begin
  14. if (x=)or(y=)or(x=y) then exit();
  15. exit(((f[x]*g[x-y]) mod num)*g[y] mod num);
  16. end;

同余

ax≡1(mod m)

  1. program asdf;
  2. var
  3. a,m,x,y,t:longint;
  4. function extgcd(a,b:longint;var x,y:longint):longint;
  5. var d:longint;
  6. begin
  7. if b<> then begin d:=extgcd(b,a mod b,y,x); y:=y-(a div b)*x; end
  8. else begin x:=;y:=; d:=a; end;
  9. exit(d);
  10. end;
  11. begin
  12. readln(a,m);
  13. x:=; y:=;
  14. t:=extgcd(a,m,x,y);
  15. writeln((x+m) mod m);
  16. 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. 剑指offer—从头到尾打印链表

    输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 递归添加...不为空就加 import java.util.ArrayList; public class Solution { pu ...

  2. jquery点击按钮复制内容

    做移动端的项目遇到一个需求要点击按钮复制dom里的内容,看了很多资料显示必须textarea或者input里的内容才能简单复制,还有就是用插件的了,最终都因为遇到各种问题放弃,最终选择了最简单的点击复 ...

  3. java 时间转换去杠

    public static String minusHyphen(String dateParam){ if(dateParam ==null) return null; if(dateParam.i ...

  4. SpringBoot学习(1)

    springboot的自动配置功能,主要流程如下: 1 启动的时候加载我们的主配置类,也就是我们的入口类:从而开启我们的自动配置配置功能,这个是通过@EnableAutoConfiguration注解 ...

  5. py函数初识

    一. 什么是函数 1. 我们到目前为止, 已经可以完成一些软件的基础功能了. 那么我们来完成这样一个功能: 约x print("拿出手机") print("打开陌&quo ...

  6. 利用nodejs实现商品管理系统(一)

    一.界面分类:用户登录界面,商品管理界面(包含商品编辑,创建,删除,列表界面) 功能实现:1.用户输入用户名与密码,通过加密,与数据库校验,如果正确,则跳转到商品管理界面,否则一直停留在用户界面. 2 ...

  7. LOOP AT SCREEN

    用法 主に.画面の属性を変更させるために使用する. 照会モードでは入力不可とするが入力可能モードでは入力可能とする.ラジオボタンAが選択された場合はラジオボタンBに関連する項目は非表示とするなど.   ...

  8. java 第五章 方法定义及调用

    1.方法的定义 什么是方法 方法是完成某个功能的一组语句,通常将常用的功能写成一个方法 方法的定义 [访问控制符] [修饰符] 返回值类型 方法名( (参数类型 形式参数, ,参数类型 形式参数, , ...

  9. oracle 数据被修改怎么修复?(闪回)

    数据被删除 或者 update 的时候忘记勾选where 限制条件,数据全部更新了?  怎么办? 要跑路了? NO !!! 看下面,迅速帮你闪回数据! demo sql: 1. SELECT * FR ...

  10. java字符流实现文件间的内容复制

    package com.io.demo1; import java.io.FileReader; import java.io.FileWriter; public class TestFileSTr ...