TEMPLATES
1.快速排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 const int MAXN=1e5+1; 3 int a[MAXN]; 4 inline int read() 5 { 6 int f=1,x=0;char c=getchar(); 7 while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} 8 while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} 9 return f*x; 10 } 11 void qSort(int a[],int l,int r) 12 { 13 if(l==r)return; 14 int m=a[(l+r)>>1],i=l,j=r; 15 do{ 16 while(a[i]<m)i++; 17 while(a[j]>m)j--; 18 if(i<=j) 19 { 20 int t=a[i];a[i]=a[j];a[j]=t; 21 i++;j--; 22 } 23 }while(i<=j); 24 if(i<r)qSort(a,i,r); 25 if(j>l)qSort(a,l,j); 26 } 27 int main() 28 { 29 int n=read(); 30 for(int i=1;i<=n;i++)a[i]=read(); 31 qSort(a,1,n); 32 for(int i=1;i<=n;i++)printf("%d ",a[i]); 33 return 0; 34 }
2.归并排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 const int MAXN=1e5+1; 3 int a[MAXN],T[MAXN]; 4 inline int read() 5 { 6 int f=1,x=0;char c=getchar(); 7 while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} 8 while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} 9 return f*x; 10 } 11 void MergeSort(int a[],int l,int r) 12 { 13 if(l==r)return; 14 int m=(r-l)/2+l; 15 MergeSort(a,l,m); 16 MergeSort(a,m+1,r); 17 int p=l,q=m+1,it=l; 18 while(p<=m || q<=r) 19 { 20 if(q>r || (p<=m && a[p]<=a[q]) )T[it++]=a[p++]; 21 else T[it++]=a[q++]; 22 } 23 for(int i=l;i<=r;i++)a[i]=T[i]; 24 } 25 int main() 26 { 27 int n=read(); 28 for(int i=1;i<=n;i++)a[i]=read(); 29 MergeSort(a,1,n); 30 for(int i=1;i<=n;i++) 31 { 32 printf("%d",a[i]); 33 if(i!=n)printf(" "); 34 } 35 return 0; 36 }
3.堆排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 const int MAXN=1e5+1; 6 int a[MAXN]; 7 8 inline int read() 9 { 10 int f=1,x=0;char c=getchar(); 11 while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} 12 while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} 13 return f*x; 14 } 15 16 void insert(int a[],int size,int val) 17 { 18 int pos=size; 19 a[pos]=val; 20 while((pos>>1)>0) 21 { 22 if(a[pos]>a[pos>>1]) 23 { 24 swap(a[pos],a[pos>>1]); 25 pos >>= 1; 26 } 27 else break; 28 } 29 } 30 void down(int a[],int pos,int size) 31 { 32 if(pos>=size || (pos<<1) >size)return; 33 int lc=pos<<1,rc=pos<<1|1; 34 int d; 35 if(rc<=size)d=a[lc]>=a[rc]?lc:rc; 36 else d=lc; 37 if(a[d] > a[pos]) 38 { 39 swap(a[d],a[pos]); 40 down(a,d,size); 41 } 42 } 43 44 void heapSort(int a[],int size) 45 { 46 while(size>=2) 47 { 48 swap(a[1],a[size]); 49 size--; 50 down(a,1,size); 51 } 52 } 53 54 int main() 55 { 56 int n=read(); 57 for(int i=1;i<=n;i++) 58 { 59 int x=read(); 60 insert(a,i,x); 61 } 62 heapSort(a,n); 63 for(int i=1;i<=n;i++)printf("%d ",a[i]); 64 return 0; 65 }
4.计数排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 const int MAXN=1e5+1; 3 int A[MAXN],B[MAXN],C[MAXN]; 4 int main() 5 { 6 int n=0,Max=-(1<<20); 7 while(scanf("%d",&A[++n])==1){Max=(Max>A[n]?Max:A[n]);} 8 n--; 9 for(int i=1;i<=n;i++)B[A[i]]++; 10 for(int i=1;i<=Max;i++)B[i]+=B[i-1]; 11 for(int i=1;i<=n;i++) 12 { 13 int val=A[i]; 14 C[B[val]--]=val; 15 } 16 for(int i=1;i<=n;i++) 17 { 18 printf("%d",C[i]); 19 if(i!=n)printf(" "); 20 } 21 return 0; 22 }
5.Eratosthenes筛
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 const int MAXN=1e4+1; 5 bool notPrime[MAXN]; 6 inline int read() 7 { 8 int f=1,x=0;char c=getchar(); 9 while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} 10 while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} 11 return f*x; 12 } 13 int main() 14 { 15 memset(notPrime,0,sizeof(notPrime)); 16 int n=read(); 17 int m=sqrt(n+0.5); 18 for(int i=2;i<=m;i++) 19 if(!notPrime[i]) 20 { 21 for(int j=i*i;j<=n;j+=i)notPrime[j]=true; 22 } 23 for(int i=2;i<=n;i++) 24 if(!notPrime[i])printf("%d ",i); 25 return 0; 26 }
6.欧拉筛
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 const int MAXN=1e4+1; 3 int is_prime[MAXN],prime[MAXN]; 4 inline int read() 5 { 6 int f=1,x=0;char c=getchar(); 7 while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} 8 while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} 9 return f*x; 10 } 11 int main() 12 { 13 int n=read(),cnt=0; 14 for(int i=2;i<=n;i++)is_prime[i]=1; 15 for(int i=2;i<=n;i++) 16 { 17 if(is_prime[i])prime[++cnt]=i; 18 for(int j=1;j<=cnt;j++) 19 { 20 if(i*prime[j]>n)break; 21 is_prime[i*prime[j]]=0; 22 if(i % prime[j]==0)break; 23 } 24 } 25 for(int i=1;i<=cnt;i++) 26 { 27 printf("%d",prime[i]); 28 if(i!=cnt)printf("\n"); 29 } 30 return 0; 31 }
7.乘法逆元
线性求 1-n 中每个数对 p 的逆元.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 const int MAXN=1e5+1; 3 int inv[MAXN]; 4 int main() 5 { 6 int n,p; 7 scanf("%d %d",&n,&p); 8 inv[1]=1; 9 for(int i=2;i<=n;i++) 10 inv[i]=(long long)(p-p/i)*inv[p%i]%p; 11 for(int i=1;i<=n;i++) 12 { 13 printf("%d",inv[i]); 14 if(i!=n)printf("\n"); 15 } 16 return 0; 17 } 18 //实在不懂那就背过吧
8.扩展欧几里得算法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 void exgcd(int a,int b,int& gcd,int& x,int& y) 2 { 3 if(!b){gcd=a;x=1;y=0;} 4 else{ 5 exgcd(b,b%a,gcd,y,x); 6 y -= x*(a/b); 7 } 8 }
9.矩阵快速幂
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 typedef long long ll; 4 struct Matrix 5 { 6 static const int MAXN=100; 7 static const ll MOD=1e9+7; 8 ll m[MAXN][MAXN]; 9 int x,y; 10 Matrix(int a=0,int b=0):x(a),y(b){memset(m,0,sizeof(m));/*important!*/} 11 Matrix operator * (const Matrix &rhs) 12 const{ 13 if(y != rhs.x)return Matrix(0,0); 14 Matrix ans(x,rhs.y); 15 for(int k=0;k<y;k++) 16 for(int i=0;i<x;i++) 17 for(int j=0;j<rhs.y;j++) 18 ans.m[i][j]=(ans.m[i][j]+m[i][k]*rhs.m[k][j] %MOD)%MOD; 19 return ans; 20 } 21 Matrix operator ^ (long long index) 22 const{ 23 if(x!=y)return Matrix(0,0); 24 Matrix C(x,y),B=*this; 25 for(int i=0;i<x;i++)C.m[i][i]=1; 26 while(index) 27 { 28 if(index&1)C=C*B; 29 B=B*B; 30 index >>= 1; 31 } 32 return C; 33 } 34 }; 35 int main() 36 {//f[n]=f[n-1]+f[n-3],f[1]=f[2]=f[3]=1; 37 Matrix start(1,3),base(3,3); 38 ll (&S)[Matrix::MAXN][Matrix::MAXN]=start.m,(&B)[Matrix::MAXN][Matrix::MAXN]=base.m; 39 S[0][0]=0;S[0][1]=1;S[0][2]=1; 40 41 B[0][0]=0;B[0][1]=0;B[0][2]=1; 42 B[1][0]=1;B[1][1]=0;B[1][2]=0; 43 B[2][0]=0;B[2][1]=1;B[2][2]=1; 44 45 46 int T;scanf("%d",&T); 47 while(T--) 48 { 49 int n;scanf("%d",&n); 50 Matrix A=start; 51 A=A*(base^n); 52 printf("%d",A.m[0][0]); 53 if(T)printf("\n"); 54 } 55 56 return 0; 57 }
10.SPFA
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <queue> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 const int MAXN=1e4+1,MAXM=5e5+1,INF=2147483647; 7 8 queue<int>q; 9 10 struct Edge 11 { 12 int to,dist,link; 13 Edge(int t=0,int d=0,int l=-1):to(t),dist(d),link(l){} 14 }e[MAXM]; 15 16 int head[MAXN],cnt=0,MinDis[MAXN]; 17 bool inq[MAXN]; 18 19 void add(int from,int to,int dis) 20 { 21 e[++cnt].link=head[from]; 22 head[from]=cnt; 23 e[cnt].to=to;e[cnt].dist=dis; 24 } 25 26 inline int read() 27 { 28 int f=1,x=0;char c=getchar(); 29 while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} 30 while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} 31 return f*x; 32 } 33 34 void spfa(int begin,int n) 35 { 36 for(int i=1;i<=n;i++)MinDis[i]=INF; 37 MinDis[begin]=0; 38 inq[begin]=true; 39 q.push(begin); 40 while(!q.empty()) 41 { 42 int u=q.front();q.pop(); 43 inq[u]=false; 44 for(int it=head[u];it!=-1;it=e[it].link) 45 { 46 int v=e[it].to; 47 if((long long)MinDis[v]>(long long)(MinDis[u]+e[it].dist)) 48 { 49 MinDis[v]=MinDis[u]+e[it].dist; 50 if(!inq[v]) 51 { 52 inq[v]=true; 53 q.push(v); 54 } 55 } 56 } 57 } 58 } 59 60 int main() 61 { 62 memset(inq,0,sizeof(inq)); 63 memset(head,-1,sizeof(head)); 64 int n=read(),m=read(),s=read(); 65 for(int i=1;i<=m;i++) 66 { 67 int from=read(),to=read(),d=read(); 68 add(from,to,d); 69 } 70 spfa(s,n); 71 for(int i=1;i<=n;i++)printf("%d ",MinDis[i]); 72 return 0; 73 }
11.Dijkstra
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 const int MAXN=3000+1; 6 int w[MAXN][MAXN],d[MAXN],v[MAXN]; 7 int main() 8 { 9 int n,m,s; 10 cin>>n>>m>>s; 11 memset(w,0x7f,sizeof(w)); 12 memset(d,0x7f,sizeof(d)); 13 const int INF=d[0]; 14 for(int i=1,a,b,dis;i<=m;i++) 15 { 16 cin>>a>>b>>dis; 17 w[a][b]=min(w[a][b],dis);//防重边 18 } 19 memset(v,0,sizeof(v)); 20 d[s]=0; 21 for(int i=1;i<=n;i++) 22 { 23 int x,M=(1<<30)-1; 24 for(int y=1;y<=n;y++) 25 if(!v[y] && d[y]<=M)M=d[x=y]; 26 v[x]=1; 27 for(int y=1;y<=n;y++)d[y]=min(d[y],d[x]+w[x][y]); 28 } 29 for(int i=1;i<=n;i++) 30 if(d[i]<INF)cout<<d[i]<<" "; 31 else cout<<"2147483647"<<" "; 32 return 0; 33 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <queue> 5 using namespace std; 6 const int MAXN=1e4+1,MAXM=5e5+1,INF=2147483647; 7 int d[MAXN],head[MAXN],cnt=0; 8 struct Node 9 { 10 int u,d; 11 Node(int Num=0,int dis=0):u(Num),d(dis){} 12 bool operator < (const Node& rhs) 13 const{ 14 return d>rhs.d; 15 } 16 }; 17 priority_queue<Node>q; 18 struct Edge 19 { 20 int to,dist,link; 21 Edge(int t=0,int d=INF,int next=-1):to(t),dist(d),link(next){} 22 }e[MAXM]; 23 24 void insert(int from,int to,int dis) 25 { 26 e[++cnt].link=head[from]; 27 head[from]=cnt; 28 e[cnt].to=to; 29 e[cnt].dist=dis; 30 } 31 inline int read() 32 { 33 int f=1,x=0;char c=getchar(); 34 while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} 35 while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} 36 return f*x; 37 } 38 int main() 39 { 40 memset(head,-1,sizeof(head)); 41 int n=read(),m=read(),s=read(); 42 for(int i=1;i<=m;i++) 43 { 44 int x=read(),y=read(),dis=read(); 45 insert(x,y,dis); 46 } 47 for(int i=1;i<=n;i++)d[i]=INF; 48 d[s]=0; 49 q.push(Node(s,d[s])); 50 while(!q.empty()) 51 { 52 Node x=q.top();q.pop(); 53 if(d[x.u]!=x.d)continue; 54 for(int it=head[x.u];it!=-1;it=e[it].link) 55 { 56 int v=e[it].to; 57 if(d[v]>d[x.u]+e[it].dist) 58 { 59 d[v]=d[x.u]+e[it].dist; 60 q.push(Node(v,d[v])); 61 } 62 } 63 } 64 for(int i=1;i<=n;i++)printf("%d ",d[i]); 65 return 0; 66 }
12.Floyd
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /*平面上有n个点(n≤100),每个点的坐标均在-10000~10000之间。其中的一些点之间有连线。若有连线,则表示可从一个点到达另一个点,即两点间有通路,通路的距离为两点间的直线距离。现在的任务是找出从一点到另一点之间的最短路径。 2 */ 3 #include <cmath> 4 #include <cstdio> 5 #include <iostream> 6 using namespace std; 7 const int MAXN=100+1; 8 const double INF=1e100; 9 int x[MAXN],y[MAXN]; 10 double d[MAXN][MAXN]; 11 double dist(int u,int v) 12 { 13 return sqrt( (x[u]-x[v])*(x[u]-x[v])+(y[u]-y[v])*(y[u]-y[v]) ); 14 } 15 int main() 16 { 17 int n,m,s,t; 18 cin>>n; 19 for(int i=1;i<=n;i++)cin>>x[i]>>y[i]; 20 for(int i=1;i<=n;i++) 21 for(int j=1;j<=n;j++)d[i][j]=(i==j?0:INF); 22 cin>>m; 23 for(int i=1,u,v;i<=m;i++) 24 { 25 cin>>u>>v; 26 d[u][v]=d[v][u]=dist(u,v); 27 } 28 cin>>s>>t; 29 for(int k=1;k<=n;k++) 30 for(int i=1;i<=n;i++) 31 for(int j=1;j<=n;j++) 32 if(d[i][k]<INF && d[k][j]<INF)d[i][j]=min(d[i][j],d[i][k]+d[k][j]); 33 printf("%.2f",d[s][t]); 34 return 0; 35 }
13.最短路计数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 #include<iostream> 5 6 using namespace std; 7 8 const int N=1e6+1,M=2e6+1; 9 const int mod=100003,INF=1e7+1,dis=1; 10 int h[N],cnt[N],sp[N],Ect=0; 11 bool vis[N]; 12 struct Graph{ 13 int to,link; 14 Graph(){to=link=0;} 15 }; 16 Graph G[M]; 17 queue<int>q; 18 19 void AddEdge(int from,int to) 20 { 21 Ect++; 22 G[Ect].link=h[from]; 23 G[Ect].to=to; 24 h[from]=Ect; 25 } 26 inline int read() 27 { 28 char c=getchar();int f=1,s=0; 29 while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} 30 while(c>='0'&&c<='9'){s=s*10+c-'0';c=getchar();} 31 return s*f; 32 } 33 int main() 34 { 35 for(int i=0;i<N;i++)sp[i]=INF; 36 memset(cnt,0,sizeof(cnt)); 37 38 const int begin=1; 39 int n=read(),m=read(); 40 for(int i=1,x,y;i<=m;i++) 41 { 42 x=read();y=read(); 43 if(x!=y) 44 { 45 AddEdge(x,y); 46 AddEdge(y,x); 47 } 48 } 49 int now=0; 50 sp[begin]=0; 51 q.push(begin); 52 vis[begin]=true; 53 cnt[begin]=1; 54 while(!q.empty()) 55 { 56 now=q.front(); 57 q.pop(); 58 vis[now]=false; 59 for(int it=h[now];it!=0;it=G[it].link) 60 if(sp[now]+dis==sp[G[it].to]) 61 { 62 cnt[G[it].to]+=cnt[now]; 63 cnt[G[it].to]%=mod; 64 } 65 else if(sp[now]+dis<sp[G[it].to]) 66 { 67 cnt[G[it].to]=cnt[now]; 68 sp[G[it].to]=sp[now]+dis; 69 if(!vis[G[it].to]) 70 { 71 q.push(G[it].to); 72 vis[G[it].to]=true; 73 } 74 } 75 } 76 for(int i=1;i<=n;i++) 77 printf("%d\n",cnt[i]); 78 return 0; 79 }
14.拓扑排序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 由于无敌的凡凡在2005年世界英俊帅气男总决选中胜出,Yali Company总经理Mr.Z心情好,决定给每位员工发奖金。公司决定以每个人本年在公司的贡献为标准来计算他们得到奖金的多少。 3 于是Mr.Z下令召开m方会谈。每位参加会谈的代表提出了自己的意见:“我认为员工a的奖金应该比b高!”Mr.Z决定要找出一种奖金方案,满足各位代表的意见,且同时使得总奖金数最少。每位员工奖金最少为100元。 4 5 【数据范围】 6 数据满足n<=10000,m<=20000。 7 8 Input 9 第一行两个整数n,m,表示员工总数和代表数; 10 以下m行,每行2个整数a,b,表示某个代表认为第a号员工奖金应该比第b号员工高。 11 Output 12 若无法找到合法方案,则输出“Poor Xed”;否则输出一个数表示最少总奖金。 13 */ 14 #include <iostream> 15 #include <cstring> 16 #include <queue> 17 #include <vector> 18 using namespace std; 19 const int MAXN=10000+1,MAXM=20000+1; 20 struct Edge{ 21 int to,next; 22 Edge(int t=0,int link=-1):to(t),next(link){} 23 }e[MAXM]; 24 struct Node{ 25 int in,fee; 26 Node(){ 27 in=0;fee=100; 28 } 29 }node[MAXN]; 30 queue<int>q; 31 int head[MAXN],cnt=0; 32 void insert(int from,int to) 33 { 34 e[++cnt].next=head[from]; 35 head[from]=cnt; 36 e[cnt].to=to; 37 } 38 int topo_sort(int n) 39 { 40 int c=0; 41 for(int i=1;i<=n;i++) 42 if(node[i].in==0) 43 { 44 q.push(i); 45 c++; 46 } 47 while(!q.empty()) 48 { 49 int u=q.front();q.pop(); 50 for(int it=head[u];it!=-1;it=e[it].next){ 51 int v=e[it].to; 52 node[v].in--; 53 node[v].fee=(node[v].fee>node[u].fee?node[v].fee:node[u].fee+1); 54 if(node[v].in==0) 55 { 56 q.push(v); 57 c++; 58 } 59 else if(node[v].in<0)return -1; 60 } 61 } 62 if(c<n)return -1; 63 int sum=0; 64 for(int i=1;i<=n;i++)sum+=node[i].fee; 65 return sum; 66 } 67 int main() 68 { 69 memset(head,-1,sizeof(head)); 70 int n,m; 71 cin>>n>>m; 72 for(int i=0,a,b;i<m;i++) 73 { 74 cin>>a>>b; 75 insert(b,a); 76 node[a].in++; 77 } 78 int ans=topo_sort(n); 79 if(ans<=0)cout<<"Poor Xed"; 80 else cout<<ans; 81 return 0; 82 }
15.高精度算法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //#include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <vector> 5 #include <cstdio> 6 using namespace std; 7 struct Big{ 8 static const long long WIDTH=8; 9 static const long long BASE=100000000; 10 vector<long long>s; 11 Big(){s.clear();} 12 Big operator = (const string& str){ 13 s.clear(); 14 long long x=0,l=(str.length()-1)/WIDTH+1; 15 for(int i=0;i<l;i++) 16 { 17 long long end=str.length()-i*WIDTH; 18 long long start=max(0LL,end-WIDTH); 19 sscanf(str.substr(start,end-start).c_str(),"%lld",&x); 20 s.push_back(x); 21 } 22 return *this; 23 } 24 }; 25 Big operator + (const Big& x,const Big& y) 26 { 27 const long long Lx=x.s.size(),Ly=y.s.size(); 28 Big c;c.s.clear(); 29 long long g=0; 30 for(int i=0;;i++) 31 { 32 if(g==0 && i>=Lx && i>=Ly)break; 33 long long m=g; 34 if(i<Lx)m+=x.s[i]; 35 if(i<Ly)m+=y.s[i]; 36 c.s.push_back(m %Big::BASE); 37 g=m/Big::BASE; 38 } 39 return c; 40 } 41 42 istream& operator >> (istream& in,Big& num) 43 { 44 string s; 45 if(!(in>>s))return in; 46 num=s; 47 return in; 48 } 49 ostream& operator << (ostream& out,const Big& num) 50 { 51 out<<num.s.back(); 52 for(int i=num.s.size()-2;i>=0;i--) 53 { 54 char buf[20]; 55 sprintf(buf,"%08lld",num.s[i]); 56 for(int j=0;j<strlen(buf);j++)out<<buf[j]; 57 } 58 return out; 59 } 60 61 int main() 62 { 63 Big a,b; 64 cin>>a>>b; 65 cout<<a+b; 66 return 0; 67 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstring> 3 #include <vector> 4 #include <cstdio> 5 using namespace std; 6 struct Big{ 7 static const long long WIDTH=8; 8 static const long long BASE=100000000; 9 vector<long long>s; 10 Big(){s.clear();} 11 long long Length() 12 { 13 long long l=0,x=s.back(); 14 if(!x)l=1; 15 16 else while(x){l++;x/=10;} 17 return (s.size()-1)*WIDTH+l; 18 } 19 Big operator = (const string& str){ 20 s.clear(); 21 long long x=0,l=(str.length()-1)/WIDTH+1; 22 for(int i=0;i<l;i++) 23 { 24 long long end=str.length()-i*WIDTH; 25 long long start=max(0LL,end-WIDTH); 26 sscanf(str.substr(start,end-start).c_str(),"%lld",&x); 27 s.push_back(x); 28 } 29 return *this; 30 } 31 }; 32 33 bool operator < (Big x,Big y) 34 { 35 long long Lx=x.s.size(),Ly=y.s.size(); 36 if(Lx!=Ly)return Lx<Ly; 37 for(int i=Lx-1;i>=0;i--) 38 if(x.s[i]!=y.s[i])return x.s[i]<y.s[i]; 39 return false; 40 } 41 Big operator - (const Big& x,const Big& y) 42 { 43 const long long Lx=x.s.size(),Ly=y.s.size(); 44 Big c;c.s.clear(); 45 long long g=0; 46 for(int i=0;;i++) 47 { 48 if(i>=Lx && i>=Ly)break; 49 long long m=g; 50 if(i<Lx)m +=x.s[i]; 51 if(i<Ly)m -=y.s[i]; 52 c.s.push_back( (m + Big::BASE) %Big::BASE); 53 g=(m<0?-1:0); 54 } 55 while(!c.s.back() && c.s.size()>1)c.s.pop_back(); 56 return c; 57 } 58 istream& operator >> (istream& in,Big& num) 59 { 60 string s; 61 if(!(in>>s))return in; 62 num=s; 63 return in; 64 } 65 ostream& operator << (ostream& out,const Big& num) 66 { 67 out<<num.s.back(); 68 for(int i=num.s.size()-2;i>=0;i--) 69 { 70 char buf[20]; 71 sprintf(buf,"%08lld",num.s[i]); 72 for(int j=0;j<strlen(buf);j++)out<<buf[j]; 73 } 74 return out; 75 } 76 77 int main() 78 { 79 Big a,b; 80 cin>>a>>b; 81 cout<<(a-b); 82 return 0; 83 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <vector> 5 #include <cstdio> 6 using namespace std; 7 struct Big{ 8 static const long long WIDTH=8; 9 static const long long BASE=100000000; 10 vector<long long>s; 11 Big(){s.clear();} 12 Big operator = (const long long& num){ 13 s.clear(); 14 long long x=num; 15 if(!x)s.push_back(0); 16 else 17 while(x){ 18 s.push_back(x %Big::BASE); 19 x /=Big::BASE; 20 } 21 return *this; 22 } 23 Big operator = (const string& str){ 24 s.clear(); 25 long long x=0,l=(str.length()-1)/WIDTH+1; 26 for(int i=0;i<l;i++) 27 { 28 long long end=str.length()-i*WIDTH; 29 long long start=max(0LL,end-WIDTH); 30 sscanf(str.substr(start,end-start).c_str(),"%lld",&x); 31 s.push_back(x); 32 } 33 return *this; 34 } 35 }; 36 Big operator + (const Big& x,const long long& num) 37 { 38 Big c=x; 39 long long g=(c.s[0]+num)/Big::BASE; 40 c.s[0]=(c.s[0]+num) %Big::BASE; 41 int i=1; 42 while(g){ 43 if(i>=c.s.size())c.s.push_back(g %Big::BASE); 44 else{ 45 c.s[i]=c.s[i]+g; 46 g=c.s[i]/Big::BASE; 47 c.s[i] %=Big::BASE; 48 } 49 i++; 50 } 51 return c; 52 } 53 54 Big operator - (const Big& x,const long long& num) 55 { 56 Big c=x;const long long L=x.s.size(); 57 c.s[0]-=num; 58 int i=0; 59 while(c.s[i]<0 && i<c.s.size()){ 60 while(c.s[i]<0){ 61 c.s[i]+=Big::BASE; 62 c.s[i+1]--; 63 } 64 i++; 65 } 66 while(!c.s.back() && c.s.size()>1)c.s.pop_back();//至少有一位!!! 67 return c; 68 } 69 70 Big operator * (const Big& x,const long long& num) 71 { 72 Big c=x;const long long l=x.s.size(); 73 long long g=0; 74 for(int i=0;;i++) 75 { 76 if(g==0 && i>=l)break; 77 long long y=g; 78 y += num * x.s[i]; 79 c.s.push_back(y %Big::BASE); 80 g=y /Big::BASE; 81 } 82 return c; 83 } 84 85 Big operator / (const Big& x,const long long& num) 86 { 87 Big c=x; 88 const long long L=x.s.size(); 89 long long g=0; 90 for(int i=L-1;i>=0;i--) 91 { 92 g=g*Big::BASE+c.s[i]; 93 c.s[i]=g/num; 94 g %= num; 95 } 96 while(!c.s.back() && c.s.size()>1)c.s.pop_back(); 97 return c; 98 } 99 100 istream& operator >> (istream& in,Big& num) 101 { 102 string s; 103 if(!(in>>s))return in; 104 num=s; 105 return in; 106 } 107 ostream& operator << (ostream& out,const Big& num) 108 { 109 out<<num.s.back(); 110 for(int i=num.s.size()-2;i>=0;i--) 111 { 112 char buf[20]; 113 sprintf(buf,"%08lld",num.s[i]); 114 for(int j=0;j<strlen(buf);j++)out<<buf[j]; 115 } 116 return out; 117 } 118 int main() 119 { 120 return 0; 121 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <vector> 5 #include <cstdio> 6 using namespace std; 7 struct Big{ 8 static const long long WIDTH=8; 9 static const long long BASE=100000000; 10 vector<long long>s; 11 Big(){s.clear();} 12 bool mod2(){ 13 return !(s[0]%2); 14 } 15 Big operator = (const long long& num) 16 { 17 s.clear(); 18 long long x=num; 19 if(!x)s.push_back(0); 20 else 21 while(x){ 22 s.push_back(x%BASE); 23 x /=BASE; 24 } 25 return *this; 26 } 27 Big operator = (const string& str){ 28 s.clear(); 29 long long x=0,l=(str.length()-1)/WIDTH+1; 30 for(int i=0;i<l;i++) 31 { 32 long long end=str.length()-i*WIDTH; 33 long long start=max(0LL,end-WIDTH); 34 sscanf(str.substr(start,end-start).c_str(),"%lld",&x); 35 s.push_back(x); 36 } 37 return *this; 38 } 39 }; 40 bool operator < (const Big& x,const Big& y){ 41 const long long l1=x.s.size(),l2=y.s.size(); 42 if(l1 != l2)return l1<l2; 43 for(int i=l1-1;i>=0;i--) 44 if(x.s[i]!=y.s[i])return x.s[i]<y.s[i]; 45 return false; 46 } 47 bool operator == (const Big& x,const Big& y) 48 { 49 return !(x<y) && !(y<x); 50 } 51 bool operator != (const Big& x,const Big& y) 52 { 53 return x<y || y<x; 54 } 55 56 Big operator - (const Big& x,const Big& y) 57 { 58 const long long Lx=x.s.size(),Ly=y.s.size(); 59 Big c;c.s.clear(); 60 long long g=0; 61 for(int i=0;;i++) 62 { 63 if(i>=Lx && i>=Ly)break; 64 long long m=g; 65 if(i<Lx) m +=x.s[i]; 66 if(i<Ly) m -=y.s[i]; 67 c.s.push_back( (m +Big::BASE) %Big::BASE); 68 g=(m<0?-1:0); 69 } 70 while(!c.s.back() && c.s.size()>1)c.s.pop_back(); 71 return c; 72 } 73 Big operator * (const Big& x,const long long& num) 74 { 75 Big c; 76 const long long l=x.s.size(); 77 long long g=0; 78 for(int i=0;;i++) 79 { 80 if(g==0 && i>=l)break; 81 long long y=g; 82 if(i<l)y += num * x.s[i]; 83 c.s.push_back(y %Big::BASE); 84 g=y /Big::BASE; 85 } 86 return c; 87 } 88 Big operator / (const Big& x,const long long& num) 89 { 90 Big c=x; 91 const long long L=x.s.size(); 92 long long g=0; 93 for(int i=L-1;i>=0;i--) 94 { 95 g=g*Big::BASE+c.s[i]; 96 c.s[i]=g/num; 97 g %= num; 98 } 99 while(!c.s.back() && c.s.size()>1)c.s.pop_back(); 100 return c; 101 } 102 istream& operator >> (istream& in,Big& num) 103 { 104 string s; 105 if(!(in>>s))return in; 106 num=s; 107 return in; 108 } 109 ostream& operator << (ostream& out,const Big& num) 110 { 111 out<<num.s.back(); 112 for(int i=num.s.size()-2;i>=0;i--) 113 { 114 char buf[20]; 115 sprintf(buf,"%08lld",num.s[i]); 116 for(int j=0;j<strlen(buf);j++)out<<buf[j]; 117 } 118 return out; 119 } 120 121 Big gcd(Big a,Big b){ 122 if(a==b)return a; 123 if(a.mod2() && b.mod2())return gcd(a/2LL,b/2LL)*2LL; 124 Big c; 125 while(a!=b){ 126 c=a-b; 127 a=max(b,c); 128 b=min(b,c); 129 } 130 return a; 131 } 132 int main() 133 { 134 Big a,b,c; 135 cin>>a>>b; 136 if(a<b)swap(a,b); 137 cout<<gcd(a,b); 138 return 0; 139 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstring> 3 #include <vector> 4 #include <cstdio> 5 using namespace std; 6 struct Big{ 7 static const long long WIDTH=8; 8 static const long long BASE=100000000; 9 vector<long long>s; 10 Big(){s.clear();} 11 Big operator = (const string& str){ 12 s.clear(); 13 long long x=0,l=(str.length()-1)/WIDTH+1; 14 for(int i=0;i<l;i++) 15 { 16 long long end=str.length()-i*WIDTH; 17 long long start=max(0LL,end-WIDTH); 18 sscanf(str.substr(start,end-start).c_str(),"%lld",&x); 19 s.push_back(x); 20 } 21 return *this; 22 } 23 }; 24 Big operator * (const Big& x,const Big& y) 25 { 26 Big c;c.s.clear(); 27 const long long l1=x.s.size(),l2=y.s.size(); 28 long long g=0; 29 for(int i=0;i<l1;i++) 30 { 31 if(g==0 && i>=l1)break; 32 for(int j=0;;j++) 33 { 34 if(g==0 && j>=l2)break; 35 long long m=g; 36 if(j<l2)m+=x.s[i]*y.s[j]; 37 if(i+j>=c.s.size()) 38 { 39 c.s.push_back(m %Big::BASE); 40 g=m /Big::BASE; 41 } 42 else 43 { 44 g=(c.s[i+j]+m) /Big::BASE; 45 c.s[i+j]=(c.s[i+j]+m) %Big::BASE; 46 //这两句的顺序不能颠倒!!! 47 } 48 } 49 } 50 return c; 51 } 52 istream& operator >> (istream& in,Big& num) 53 { 54 string s; 55 if(!(in>>s))return in; 56 num=s; 57 return in; 58 } 59 ostream& operator << (ostream& out,const Big& num) 60 { 61 out<<num.s.back(); 62 for(int i=num.s.size()-2;i>=0;i--) 63 { 64 char buf[20]; 65 sprintf(buf,"%08lld",num.s[i]); 66 for(int j=0;j<strlen(buf);j++)out<<buf[j]; 67 } 68 return out; 69 } 70 71 int main() 72 { 73 Big a,b; 74 cin>>a>>b; 75 cout<<a*b; 76 return 0; 77 }
16.并查集
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //现在有一个并查集,你需要完成合并和查询操作。 2 //输入格式: 3 //第一行包含两个整数N、M,表示共有N个元素和M个操作。 4 //接下来M行,每行包含三个整数Zi、Xi、Yi 5 //当Zi=1时,将Xi与Yi所在的集合合并 6 //当Zi=2时,输出Xi与Yi是否在同一集合内,是的话输出Y;否则话输出N 7 //输出格式: 8 //如上,对于每一个Zi=2的操作,都有一行输出,每行包含一个大写字母,为Y或者N 9 #include <cstdio> 10 #include <cstring> 11 const int MAXN=10000+1; 12 int f[MAXN]; 13 inline int read() 14 { 15 int f=1,x=0;char c=getchar(); 16 while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} 17 while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} 18 return f*x; 19 } 20 int find(int u) 21 { 22 return f[u]==u?u:f[u]=find(f[u]); 23 } 24 void merge(int u,int v) 25 { 26 if(find(u)==find(v))return; 27 f[f[u]]=f[v]; 28 } 29 void ask(int u,int v) 30 { 31 find(u);find(v); 32 if(f[u] == find(v))printf("Y\n"); 33 else printf("N\n"); 34 } 35 int main(){ 36 int n=read(),m=read(); 37 for(int i=1;i<=n;i++)f[i]=i;//important!!! 38 for(int i=1;i<=m;i++){ 39 int z=read(),x=read(),y=read(); 40 if(z==1)merge(x,y); 41 else if(z==2)ask(x,y); 42 } 43 return 0; 44 }
17. 最小生成树
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //第一行包含两个整数N、M,表示该图共有N个结点和M条无向边。(N<=5000,M<=200000) 2 // 3 //接下来M行每行包含三个整数Xi、Yi、Zi,表示有一条长度为Zi的无向边连接结点Xi、Yi 4 // 5 //输出格式: 6 //输出包含一个数,即最小生成树的各边的长度之和;如果该图不连通则输出orz 7 #include <algorithm> 8 #include <cstdio> 9 #include <cstring> 10 using namespace std; 11 const int MAXN=5000+1,MAXM=200000+1; 12 int f[MAXN]; 13 struct Edge{ 14 int u,v,d; 15 bool operator < (const Edge& x)const{ 16 return d<x.d; 17 } 18 }e[MAXM]; 19 inline int read() 20 { 21 int f=1,x=0;char c=getchar(); 22 while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} 23 while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} 24 return f*x; 25 } 26 inline int find(int x) 27 { 28 if(f[x]==x)return x; 29 return f[x]=find(f[x]); 30 } 31 void merge(int u,int v) 32 { 33 if(find(u)==find(v))return; 34 f[f[u]]=f[v]; 35 } 36 int main() 37 { 38 memset(f,0,sizeof(f)); 39 int n=read(),m=read(); 40 for(int i=1;i<=n;i++)f[i]=i; 41 for(int i=1;i<=m;i++){e[i].u=read();e[i].v=read();e[i].d=read();} 42 sort(e+1,e+m+1); 43 int cnt=0,mst=0,p=0; 44 while(cnt<n-1 && p<=m){ 45 p++; 46 int x=e[p].u,y=e[p].v,z=e[p].d; 47 if(find(x) != find(y)) 48 { 49 merge(x,y); 50 cnt++; 51 mst+=z; 52 } 53 } 54 if(cnt==n-1)printf("%d",mst); 55 else printf("orz"); 56 return 0; 57 }
18.最长不上升子序列
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //经典问题导弹拦截 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 const int MAXN=1e5+1; 7 int a[MAXN],g[MAXN],ml=0,n=0; 8 int* lowerBound(int* begin,int* end,int val){ 9 //在不升序列中查找 10 int* mid,*l=begin,*r=end; 11 while(l<r){ 12 mid=(r-l)/2+l; 13 if(*mid<val)r=mid;//8 7 6 4 4 4 3 1 14 else l=mid+1; 15 } 16 return l; 17 } 18 int* lower_Bound(int* begin,int* end,int val){ 19 //在不降序列中查找 20 int *mid,*l=begin,*r=end; 21 while(l<r){ 22 mid=(r-l)/2+l; 23 if(*mid>=val)r=mid;//1 2 3 4 6 7 8 9 24 else l=mid+1; 25 } 26 return l; 27 } 28 int main() 29 { 30 while(scanf("%d",&a[0])==1)a[++n]=a[0]; 31 g[ml=1]=a[1]; 32 for(int i=2;i<=n;i++){ 33 if(a[i]<=g[ml])g[++ml]=a[i]; 34 else 35 { 36 int pos=lowerBound(g+1,g+ml+1,a[i])-g; 37 g[pos]=a[i]; 38 } 39 } 40 printf("%d\n",ml); 41 g[ml=1]=a[1]; 42 for(int i=2;i<=n;i++){ 43 if(a[i]>g[ml])g[++ml]=a[i]; 44 else{ 45 int pos=lower_Bound(g+1,g+ml+1,a[i])-g; 46 g[pos]=a[i]; 47 } 48 } 49 printf("%d",ml); 50 return 0; 51 }
19.最长公共子序列
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 const int MAXN=200+2; 4 char s1[MAXN],s2[MAXN]; 5 int f[MAXN][MAXN]; 6 inline int max(int a,int b) 7 { 8 return a>b?a:b; 9 } 10 int main() 11 { 12 #ifdef RFILE 13 freopen("lcs.in","r",stdin); 14 #endif 15 while(scanf("%s %s",s1+1,s2+1)==2) 16 { 17 memset(f,0,sizeof(f)); 18 const int l1=strlen(s1+1),l2=strlen(s2+1); 19 for(int i=1;i<=l1;i++) 20 for(int j=1;j<=l2;j++) 21 if(s1[i] == s2[j])f[i][j]=f[i-1][j-1]+1; 22 else f[i][j]=max(f[i-1][j],f[i][j-1]); 23 printf("%d\n",f[l1][l2]); 24 } 25 return 0; 26 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //[LuoguP1439](https://www.luogu.org/problemnew/show/P1439) 2 #include <algorithm> 3 #include <cstdio> 4 using namespace std; 5 const int MAXN=1e5+1; 6 int a[MAXN],b[MAXN],c[MAXN],g[MAXN]; 7 inline int read() 8 { 9 int f=1,x=0;char c=getchar(); 10 while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} 11 while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} 12 return f*x; 13 } 14 int main() 15 { 16 int n=read(); 17 for(int i=1;i<=n;i++)a[i]=read(); 18 for(int i=1;i<=n;i++)b[i]=read(); 19 for(int i=1;i<=n;i++)c[a[i]]=i; 20 for(int i=1;i<=n;i++)b[i]=c[b[i]]; 21 int ml; 22 g[ml=1]=b[1]; 23 for(int i=1;i<=n;i++) 24 { 25 if(g[ml]<b[i])g[++ml]=b[i]; 26 else{ 27 int pos=lower_bound(g+1,g+ml+1,b[i])-g; 28 g[pos]=b[i]; 29 } 30 } 31 printf("%d",ml); 32 return 0; 33 }
20.Hash
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //https://www.luogu.org/problemnew/show/P3370 2 #include <cstdio> 3 #include <string> 4 #include <cstring> 5 #include <iostream> 6 #include <algorithm> 7 using namespace std; 8 typedef unsigned long long ull; 9 const int MAXN=1e5+1; 10 struct Hash{ 11 static const ull MOD=1e8+1; 12 string s; 13 ull hashVal; 14 Hash(){ 15 s.clear(); 16 hashVal=1ULL; 17 } 18 ull hash(){ 19 const ull l=s.size(); 20 for(int i=0;i<l;i++){ 21 hashVal=(hashVal+hashVal*s[i]) %MOD; 22 } 23 return hashVal; 24 } 25 bool operator < (const Hash& rhs) 26 const{ 27 if(hashVal!=rhs.hashVal)return hashVal<rhs.hashVal; 28 else return s<rhs.s; 29 } 30 }h[MAXN]; 31 int main() 32 { 33 int n;cin>>n; 34 for(int i=1;i<=n;i++) 35 { 36 cin>>h[i].s; 37 h[i].hash(); 38 } 39 sort(h+1,h+n+1); 40 int cnt=1; 41 for(int i=2;i<=n;i++){ 42 if(h[i].hashVal != h[i-1].hashVal || h[i].s!=h[i-1].s)cnt++; 43 } 44 cout<<cnt; 45 return 0; 46 }