随笔分类 - ACM——各种模板
摘要://#pragma comment(linker, "/STACK:1024000000,1024000000")#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define CL(arr, val) memset(arr, val, sizeof(arr))#define lc l,m,rt> 1#define Min(x, y) (x) b[2*N];int m
阅读全文
摘要:首先是参考胡浩大神的数组模拟指针的模板,超简洁。View Code #include <cstdio>#include <cstdlib>#include <string>#include <climits>#include <iostream> #include <vector>#include <set>#include <cmath>#include <cctype>#include <algorithm>#include <sstream>#include
阅读全文
摘要://#pragma comment(linker,"/STACK:327680000,327680000")#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <cstring>#include <algorithm>#include <string>#include <set>#include <functional>#include <numeric>
阅读全文
摘要:#define CL(a,num) memset((a),(num),sizeof(a))#define inf 0x7f7f7f7f#define M 1007#define N 1000007const int head = 0;int u[N],d[N],l[N],r[N],c[N],row[N];int s[M],o[M];int ak,n,m;void init(int m){ int i; for (i = 1; i <= m; ++i){ l[i] = i - 1; r[i] = i + 1; u[i] = d[i] = i;...
阅读全文
摘要:http://www.notonlysuccess.com/index.php/divide-tree/http://blog.sina.com.cn/s/blog_5f5353cc0100ki2e.html以上为两个不错的讲解:struct node{ int l,r; int mid(){ return (l + r)>>1; }}tt[N<<2];int toLeft[20][N];int val[20][N],sorted[N];int n,q;void build(int l,int r,int rt,int d){ int i; tt[r...
阅读全文
摘要:http://poj.org/problem?id=1330题意:给n个点,n-1条边,一棵树,求每个询问的LCA;思路:rmq求LCA。。。。http://dongxicheng.org/structure/lca-rmq/View Code #include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>#include <stack>#include <
阅读全文
摘要:数据结构struct Mat{ ll mat[3][3]; void init(){ for (int i = 0; i < 2; ++i){ for (int j = 0; j < 2; ++j){ mat[i][j] = ; } } mat[1][1] = 0; }}; 乘法Mat operator * (Mat a,Mat b){ Mat c; int i,j,k; CL(c.mat,0); for (i = 0; i < 2; ...
阅读全文
摘要:字符串处理:unix的ELF哈希函数unsigned int ELFHash(char* str){ unsigned int hash = 0; unsigned int x = 0; while (*str){ hash = (hash << 4) + (*str++); if ((x = hash & 0xF0000000L) != 0){ hash ^= (x >> 24); hash &= ~x; } } return (hash & 0x7FFFFFFF);} BK...
阅读全文
摘要:高斯消元法,是线性代数中的一个算法,可用来求解线性方程组,并可以求出矩阵的秩,以及求出可逆方阵的逆矩阵。高斯消元法的原理是:若用初等行变换将增广矩阵 化为 ,则AX = B与CX = D是同解方程组。所以我们可以用初等行变换把增广矩阵转换为行阶梯阵,然后回代求出方程的解。以上是线性代数课的回顾,下面来说说高斯消元法在编程中的应用。首先,先介绍程序中高斯消元法的步骤:(我们设方程组中方程的个数为equ,变元的个数为var,注意:一般情况下是n个方程,n个变元,但是有些题目就故意让方程数与变元数不同)1. 把方程组转换成增广矩阵。2. 利用初等行变换来把增广矩阵转换成行阶梯阵。枚举k从0到equ
阅读全文
摘要:链表:struct node{ int v; //边的结束顶点 int w; //边的长度 node* next; //指向以同一起点的下一条边的指针}*head[N],H[M]; //head[u]指向以u为起始点的第一条边void init(){ memset(head,NULL,sizeof(head));}void add(int u, int v, int w)//添加边{ node* p = &H[tt++]//new node; p->v = v; p->w = w; p->next = head[u]; head[u] = p;}//使用的时...
阅读全文
摘要:http://www.matrix67.com/blog/archives/115求next函数模板:void GetNext(char *s){ int len = strlen(s); int i,j; i = 0; j = -1;//j从0开始,i从1开始 next[0] = -1; for (i = 1; i < len; ++i) { while (j > -1 && s[j + 1] != s[i])//不相同的话j就跳跃知道相同或者成为-1 j = next[j]; if (s[j + 1] == s[...
阅读全文
摘要:欧拉函数:定义:用于计算 p(n),比n小的所有与n互质的数。计算公式:p(n)=n*(1-1/p1)*(1-1/p2)....*(1-1/pk)【p1,p2,pk都是n的素因子】另:若n=p1^q1*p2^q2*.....*pk^qk则,p(n)=(p1-1)*p1^(q1-1)*(p1-1)*p2^(q2-1)......*(pk-1)*pk^(qk-1)性质:若m,n互质,φ(mn)=φ(m)φ(n)。当n为奇数时,φ(2n)=φ(n)欧拉定理:a,m互质,a^φ(m)≡1(mod m)例:2,3互质,那么,2^2%3=1推论:对于互质的数a、n,满足a^(φ(n)+1) ≡ a (mo
阅读全文
摘要:首先要分清a[] c[] sum[] 他们各自所代表的意思;a[]就是输入的数组;c[]就是建立的树状数组;c[i] = a[i - 2^k +1] + ...... + a[i];a有多少个c就有多少个,而且c[i]肯定包含相应的a[i];lowbit(i) = 2^k 表示i的二进制数表示形式留下左右边的1其余为取0得到的数sum[k] = c[N1] + c[N2] + c[N3].......+ c[Nm];Ni-1 = Ni - lowbit(i);求和的话,就是有c[Nm] c[Nm-1] c[Nm-2] .... c[N1]的过程 Ni - lowbit(i)的过程是将Ni的二进
阅读全文
摘要:问题描述:对于任何正整数x,起约数的个数记做g(x).例如g(1)=1,g(6)=4.如果某个正整数x满足:对于任意i(0<i<x),都有g(i)<g(x),则称x为反素数.现在给一个N,求出不超过N的最大的反素数.比如:输入1000 输出 840思维过程:求[1..N]中约数在大的反素数-->求约数最多的数如果求约数的个数 756=2^2*3^3*7^1(2+1)*(3+1)*(1+1)=24基于上述结论,给出算法:按照质因数大小递增顺序搜索每一个质因子,枚举每一个质因子为了剪枝:性质一:一个反素数的质因子必然是从2开始连续的质数.因为最多只需要10个素数构造:2,3
阅读全文
摘要:并查集结构: for (i = 0; i <= n; ++i) f[i] = i;查找祖先:int find(int x){ if (x != f[x]) { f[x] = find(f[x]); } return f[x];}合并操作:经常使用的:void Union(int x,int y){ x = find(x); y = find(y); if (x != y) { f[y] = x; num[x] += num[y];//根记录子系的个数 }}还有一种写法:void Union(int...
阅读全文
摘要:结构:struct node{ int flag; node *next[27];}*head;生成节点:/*动态分配内存*/node * newnode(){ int i; node * p = new node; // c语言用(node * )malloc(sizeof(node), 这里是动态分配内存,时间上可能消耗的多一些 p->flag = 0; for(i = 0; i < 26; i++) p->next[i] = NULL; return p;}/*静态分配内存*/node T[1000000];int t = 0...
阅读全文
摘要:两个不错的介绍KM算法的文章:1:http://www.cppblog.com/MatoNo1/archive/2011/07/23/151724.aspx2: KM算法是通过给每个顶点一个标号(叫做顶标)来把求最大权匹配的问题转化为求完备匹配的问题的。设顶点Xi的顶标为A[i],顶点Yi的顶标为B[i],顶点Xi与Yj之间的边权为w[i,j]。在算法执行过程中的任一时刻,对于任一条边(i,j),A[i]+B[j]>=w[i,j]始终成立。KM算法的正确性基于以下定理: 若由二分图中所有满足A[i]+B[j]=w[i,j]的边(i,j)构成的子图(称做相等子图)有完备匹配,那么这个完备匹
阅读全文
摘要:spfa:void add(int u,int v,int w){ g[cnt].v = v; g[cnt].w = w; g[cnt].next = pre[u]; pre[u] = cnt++;}bool spfa(int s){ int i; queue<int>q; for (i = 0; i < maxn; ++i) dis[i] = -inf; dis[s] = 0; q.push(s); inq[s] = true; while (!q.empty()) { int u = q.front(); q....
阅读全文
摘要:点结构:struct point{ double x,y; point(double a = 0,double b = 0): x(a),y(b){}}; 浮点误差处理:int dblcmp(double x){ if(fabs(x) < eps) return 0; return x > 0 ? 1:-1;}或者int dblcmp(double x){ if (x > eps) return 1; else if (x < -eps) return -1; else return 0;}判断线段是否相交并求交点(规范相交)double det(double...
阅读全文
摘要:1:利用a^b%n = (((a%c)*a)%c......)运算计算时间复杂度认为得到优化,O(b),但b很大是还是不行。int modexp_simple(int a,int b,int n){ int ret = 1; while (b--) { ret = a * ret % n; } return ret;}2:算法2:另一种算法利用了二分的思想,可以达到O(logn)。可以把b按二进制展开为:b = p(n)*2^n + p(n-1)*2^(n-1) +…+ p(1)*2 + p(0)其中p(i) (0<=i<=n)为 0 或 1这样 ...
阅读全文