跳到正文

算法·理论

模板

Tyih约 13 分钟
原文
文章信息与版本

文章信息

发布
2025年4月9日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 13 分钟

版本

  1. v1当前

数学

exgcd

C++
void exgcd(int a,int b,int &x,int &y){
	if(!b) x=1,y=0;
	else exgcd(b,a%b,y,x),y-=a/b*x;
}

线性基

C++
#include<bits/stdc++.h>
#define IOS cin.tie(0),cout.tie(0),ios::sync_with_stdio(0)
#define ll long long
#define db double
#define pb push_back
#define eb emplace_back
#define MS(x,y) memset(x,y,sizeof x)
#define MC(x,y) memcpy(x,y,sizeif x)
#define PLL pair<ll,ll>
using namespace std;
const int N=5e3+5,M=1e5+5;
const ll INF=1ll<<60,mod=2008;
ll n,m,a[N],p[N];
ll qpow(ll a,ll b,ll p){
	ll ans=1;
	for(;b;b>>=1,a=a*a%p) if(b&1) ans=ans*a%p;
	return ans;
}
void add(ll x){
	for(int i=61;i>=0;i--){
		if(x&(1ll<<i)){
			if(!p[i]){
				p[i]=x;
				return ;
			}
			x^=p[i];			
		}
	}
	return ;
}
ll query_max(){
	ll ans=0;
	for(int i=61;i>=0;i--) if(p[i] && !(ans&(1ll<<i))) ans^=p[i];
	return ans;
}
ll query_num(){
	int cnt=0;
	for(int i=61;i>=0;i--) if(p[i]) ans++;
	return qpow(2,cnt,mod);
}
int main(){
	IOS;cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i],add(a[i]);
	cout<<query_max()<<"\n";
	return 0;
}

图论

网络流

建边

网络流
C++
int n,m;
struct edge{
	int to,nxt;
	ll w;
}e[M];
int tot=1,head[N];//tot=1!!!
void add(int u,int v,ll w){
	e[++tot]={v,head[u],w};
	head[u]=tot;
}
void con(int u,int v,ll w){
	add(u,v,w);
	add(v,u,0);
}
费用流
C++
int n,m;
struct edge{
	int to,nxt;
	ll w,c;
}e[M];
int tot=1,head[N];//tot=1!!!
void add(int u,int v,ll w,ll c){
	e[++tot]={v,head[u],w,c};
	head[u]=tot;
}
void con(int u,int v,ll w,ll c){
	add(u,v,w,c);
	add(v,u,0,-c);
}

EK

C++
namespace EK{
	int s,t;
	ll a[N],b[N];
	bool vis[N];
	bool bfs(){
		memset(vis,0,sizeof vis);
		memset(a,-1,sizeof a);
		memset(b,-1,sizeof b);
		queue<int> q;
		q.push(s);
		vis[s]=1;
		while(!q.empty()){
			int u=q.front();q.pop();
			for(int i=head[u];i;i=e[i].nxt){
				int v=e[i].to;ll w=e[i].w;
				if(vis[v]||!w) continue;
				a[v]=u;
				b[v]=i;
				vis[v]=1;
				q.push(v);
				if(v==t) return 1;
			}
		}
		return 0;
	}
	ll calc(){
		ll ans=0;
		while(bfs()){
			ll minn=INF;
			for(int i=t;i!=s;i=a[i]) minn=min(minn,e[b[i]].w);
			ans+=minn;
			for(int i=t;i!=s;i=a[i]) e[b[i]].w-=minn,e[b[i]^1].w+=minn;
		}
		return ans;
	}
}

Dicnic

C++
namespace Dicnic{
	int s,t;
	ll d[N],Ns[N];
	bool bfs(){
		memset(d,0x3f,sizeof d);
		queue<int> q;
		q.push(s); 
		d[s]=0;
		Ns[s]=head[s];
		while(!q.empty()){
			int u=q.front();q.pop();
			for(int i=head[u];i;i=e[i].nxt){
				int v=e[i].to;ll w=e[i].w;
				if(!w||d[v]<=d[u]+1) continue;
				d[v]=d[u]+1;
				Ns[v]=head[v];
				q.push(v);
				if(v==t) return 1;
			}
		}
		return 0;
	}
	ll dfs(int u,ll Sum){
		if(u==t) return Sum;
		ll pus=0,k;
		for(ll &i=Ns[u];i;i=e[i].nxt){
			int v=e[i].to;ll w=e[i].w;
			if(!w||d[v]!=d[u]+1) continue;
			k=dfs(v,min(Sum,w));
			if(!k) d[v]=1e9+7;
			e[i].w-=k;
			e[i^1].w+=k;
			Sum-=k;
			pus+=k;
			if(!Sum) break;//注意!!!
		}
		return pus;
	}
	ll calc(){
		ll ans=0;
		while(bfs()) ans+=dfs(s,INF);
		return ans;
	}
}

SSP

EK
C++
namespace SSP{
	int s,t;
	ll dis[N],a[N],b[N];
	bool vis[N];
	bool spfa(){
		MS(vis,0);MS(a,-1);MS(b,-1);MS(dis,0x3f);
		bool flag=0;
		queue<int> q;
		q.push(s);
		vis[s]=1;dis[s]=0;
		while(!q.empty()){
			int u=q.front();q.pop();
			vis[u]=0;
			for(int i=head[u];i;i=e[i].nxt){
				int v=e[i].to;
				ll w=e[i].w,c=e[i].c;
				if(!w || dis[v]<=dis[u]+c) continue;
				dis[v]=dis[u]+c;
				a[v]=u;b[v]=i;
				if(!vis[v]) q.push(v),vis[v]=1;
				if(v==t) flag=1;
			}
		}
		if(flag) return 1;
		return 0; 
	}
	PLL calc(){
		ll maxflow=0,maxc=0;
		while(spfa()){
			ll minn=INF;
			for(int i=t;i!=s;i=a[i]) minn=min(minn,e[b[i]].w);
			if(minn<=0) break;
			maxflow+=minn;
			for(int i=t;i!=s;i=a[i]){
				maxc+=e[b[i]].c*minn;
				e[b[i]].w-=minn;
				e[b[i]^1].w+=minn;
			}
		}
		return {maxflow,maxc};
	}
}
Dicnic
C++
struct SSP{
	struct edge{int to,nxt;ll w,c;}e[M];
	int cnt=1,head[N];
	void add(int u,int v,ll w,ll c){
		e[++cnt]={v,head[u],w,c};
		head[u]=cnt;
	}
	void con(int u,int v,ll w,ll c){
		add(u,v,w,c);
		add(v,u,0,-c);
	}
	int s,t,Ns[N];
	ll cost,d[N];
	bool vis[N];
	bool spfa(){
		MC(Ns,head);MS(d,0x3f);MS(vis,0);
		queue<int> q;
		q.push(s);d[s]=0;vis[s]=1;
		bool flag=0;
		while(!q.empty()){
			int u=q.front();q.pop();
			vis[u]=0;
			for(int i=head[u];i;i=e[i].nxt){
				int v=e[i].to;ll w=e[i].w;
				if(!w || d[v]<=d[u]+e[i].c) continue;
				d[v]=d[u]+e[i].c;
				if(!vis[v]) q.push(v),vis[v]=1;
				if(v==t) flag=1;
			}
		}
		return flag;
	}
	ll dfs(int u,ll Sum){
		if(u==t) return Sum;
		ll pus=0,k;
		vis[u]=1;
		for(int &i=Ns[u];i;i=e[i].nxt){
			int v=e[i].to;ll w=e[i].w;
			if(vis[v] || !w || d[v]!=d[u]+e[i].c) continue;
			k=dfs(v,min(Sum,w)); 
			if(!k) d[v]=INF;
			pus+=k;Sum-=k;
			e[i].w-=k;e[i^1].w+=k;
			cost+=k*e[i].c;
			if(!Sum) break;
		}
		vis[u]=0;
		return pus;
	}
	PLL calc(){
		ll flow=0,x=0;
		while(spfa()) while((x=dfs(s,INF))) flow+=x;
		return {flow,cost};
	}
}ssp;

kruskal

C++
#include<bits/stdc++.h>
using namespace std;
int a,b,cnt;
namespace kruskal{
	int ans,cut,f[N];
	struct node{
		int x,y,w;
	}k[M];
	int find(int x){
		if(f[x]==x) return x;
		return f[x]=find(f[x]);
	}
	bool cmp(node xx,node yy){
		return xx.w<yy.w;
	}
	ll calc(){
		for(int i=1;i<=b;i++) f[i]=i;
		cut=b;
		for(int i=1;i<=cnt;i++){
			if(k[i].w>a) break;
			int fx=find(k[i].x),fy=find(k[i].y);
			if(fx==fy) continue;
			cut--;
			f[fx]=fy; 
			ans+=k[i].w;
		}
		return ans+cut*a;
	}
} 
int main(){
	cin>>a>>b;
	for(int i=1;i<=b;i++){
		for(int j=1;j<=b;j++){
			k[++cnt].x=i,k[cnt].y=j;
			input(&k[cnt].w);
			if(!k[cnt].w) k[cnt].w=a;
		}	
	}
	sort(k+1,k+1+cnt,cmp);
	cout<<kruskal::calc();
	return 0;
}

字符串

Trie

C++
int getid(char ch){
	if(ch>='a' && ch<='z') return ch-'a'+1;
	if(ch>='A' && ch<='Z') return ch-'A'+27;
	if(ch>='0' && ch<='9') return ch-'0'+53;
}
struct Trie{
	int nxt[N][70],cnt,num[N];
	void clear(){
		for(int i=0;i<=cnt;i++){
			num[i]=0;
			for(int j=0;j<=68;j++) nxt[i][j]=0;
		}
		cnt=0;
	}
	void insert(string s){
		int l=s.size(),p=0;
		for(int i=0,ch;i<l;i++){
			ch=getid(s[i]);
			if(!nxt[p][ch]) nxt[p][ch]=++cnt;
			p=nxt[p][ch];
		}
		num[p]++;
	}
	int find(string s){
		int l=s.size(),p=0;
		for(int i=0,ch;i<l;i++){
			ch=getid(s[i]);
			if(!nxt[p][ch]) return 0;
			p=nxt[p][ch];
		}
		return num[p];	
	}
};
原文