我的算法小笔记
saplingyang
这个作者很懒,什么都没留下…
展开
-
扩展欧几里得
扩展欧几里得:已知a, b,求解一组x,y,使它们满足贝祖等式: ax+by =gcd(a, b)ax1+by1=gcd(a,b)bx2+(a%b)y2=gcd(b,a%b)gcd(a,b)=gcd(b,a%b)ax1+by1 = bx2+(a%b)y2ax1+by1=bx2+(a-a/b*b)y2ax1+by1=ay2+b(x2-a/b*y2)x1=y2; y1=x2...原创 2018-11-21 14:22:30 · 148 阅读 · 0 评论 -
欧拉函数
求某个数的欧拉函数值#include<iostream>#include<cmath>#define ll long longint main(){ ll n; std::cin>>n; ll res=n; for(ll i=2;i<=sqrt(n);i++) { if(n%i==0) res=res/i*(i-1...原创 2018-11-20 16:26:09 · 158 阅读 · 0 评论 -
js加减乘除混合运算
function getResult(str){ // var _str=str.repalce('*','×'); // var __str=_str.replace('/','÷'); var str1=str.split(''); var str2=[]; for(var i=0;i<str1.length;i++) { if(!Number(str1[i])&am...原创 2018-10-27 09:27:55 · 3293 阅读 · 0 评论 -
最近点对(转)
#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>#include <math.h>using namespace std;const int MAXN = 100010;const double INF = 1e20;...转载 2018-10-14 11:12:07 · 134 阅读 · 0 评论 -
并查集
int p[maxn];void init(){ for(int i=0;i<maxn;i++) p[i]=i; }int findset(int x){ return p[x]!=x?p[x]=findset(p[x]):x;}void join(int x,int y){ int a=findset(x); int b=findset(y); if(a!=b...原创 2018-06-08 18:20:06 · 127 阅读 · 0 评论 -
已知二叉树的中序和后序遍历排列,求前序遍历
#include<iostream>#include<string>using namespace std;void Preorder(string inorder,string postorder){ if(inorder.size()>0) { char ch=postorder[postorder.size()-1]; cout<&l...原创 2018-03-20 21:45:45 · 2923 阅读 · 0 评论 -
最短路SPFA
#include<iostream>#include<vector>#include<queue>#include<cstring>#define inf 0x3f3f3f3f#define maxn 20005using namespace std;int d[maxn],inq[maxn];vector<pair<in...原创 2018-03-11 16:29:16 · 155 阅读 · 0 评论 -
最小费用最大流
#include<iostream>#include<cstring>#include<vector>#include<queue>#define maxn 1005#define inf 0x3f3f3f3fusing namespace std;struct Edge{ int from,to,cap,flow,cost; Ed...原创 2018-03-16 22:01:51 · 178 阅读 · 0 评论 -
最短路径dijkstra(从s到t)
(1)邻接矩阵写法#include<stdio.h>#include<string.h>#define inf 0x3f3f3f3fint g[2500][2500];int d[2500];int v[2500];int n,m,s,t;int dijkstra(int s,int t){ memset(v,0,sizeof(v)); fo...原创 2017-12-10 17:26:26 · 707 阅读 · 0 评论 -
网络流最大流模板dicnic
#include<iostream>#include<vector>#include<queue>#include<cstring>#define inf 0x3f3f3f3f#define MAXN 100using namespace std;struct Edge{ int from,to; int cap; int flo...原创 2018-02-14 11:51:40 · 479 阅读 · 0 评论 -
快排
#include<stdio.h>int QKpass(int a[],int low,int high){ int x=a[low]; while(low<high) { while(a[high]>x&&low<high) high--; if(low<high) { a[low]=a[high]; lo...原创 2018-02-26 19:40:43 · 157 阅读 · 0 评论 -
最大子段和
#include<iostream>#include<cmath>#include<cstring>using namespace std;int a[50005];long long dp[50005];int main(){ memset(a,0,sizeof(a)); memset(dp,0,sizeof(dp)); int n; s...原创 2018-02-09 12:23:18 · 131 阅读 · 0 评论 -
KMP
两篇讲的比较好的博客,结合起来看http://www.cnblogs.com/tangzhengyue/p/4315393.htmlhttp://www.cnblogs.com/yjiyjige/p/3263858.html#include#includeint next[20];char textstr[20],keystr[20];void getnext(){转载 2017-12-09 18:00:19 · 157 阅读 · 0 评论 -
乘法逆元
#include<iostream>#define ll long longll exgcd(ll a,ll b,ll &x,ll &y){ if(b==0){ x=1; y=0; return a; } ll gcd=exgcd(b,a%b,y,x); y-=(a/b)*x; return gcd;}ll inv(ll a,ll p){...原创 2018-11-21 18:01:31 · 164 阅读 · 0 评论