BigNum
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 inline int Max(int x,int y) {return x>y?x:y;} 7 const int Maxn=10100; 8 const int WS=10000; 9 char Str1[10000],Str2[10000]; 10 struct BigNum 11 { 12 int Len,a[Maxn]; 13 inline void Clr() {Len=1;memset(a,0,sizeof(a));} 14 inline void Trans_Int(int p) 15 { 16 Len=0; 17 while (true) 18 { 19 a[++Len]=p%WS; 20 p/=WS; 21 if (p==0) break; 22 } 23 } 24 inline void Trans_Str(char * Str) 25 { 26 int b=strlen(Str+1),i; Len=0; 27 memset(a,0,sizeof(a)); //四位压缩 28 for (i=b;i>=4;i-=4) 29 { 30 int t=1; Len++; 31 for (int j=0;j<4;j++,t=t*10) 32 a[Len]+=(Str[i-j]-'0')*t; 33 } 34 if (i) 35 { 36 Len++; int t=1; 37 for (int j=0;j<i;j++,t=t*10) 38 a[Len]+=(Str[i-j]-'0')*t; 39 } 40 41 } 42 inline void Print() 43 { 44 printf("%d",a[Len]); 45 for (int i=Len-1;i>=1;i--) printf("%04d",a[i]); puts(""); 46 } 47 }; 48 inline bool operator < (BigNum A,BigNum B) 49 { 50 if (A.Len<B.Len) return true; 51 if (A.Len>B.Len) return false; 52 for (int i=A.Len;i>=1;i--) 53 { 54 if (A.a[i]<B.a[i]) return true; 55 if (A.a[i]>B.a[i]) return false; 56 } 57 return false; 58 } 59 inline bool operator > (BigNum A,BigNum B) 60 { 61 if (A.Len<B.Len) return false; 62 if (A.Len>B.Len) return true; 63 for (int i=A.Len;i>=1;i--) 64 { 65 if (A.a[i]<B.a[i]) return false; 66 if (A.a[i]>B.a[i]) return true; 67 } 68 return false; 69 } 70 inline bool operator == (BigNum A,BigNum B) 71 { 72 if (A.Len!=B.Len) return false; 73 for (int i=1;i<=A.Len;i++) if (A.a[i]!=B.a[i]) return false; 74 return true; 75 } 76 inline BigNum operator + (BigNum A,BigNum B) 77 { 78 BigNum C; C.Clr(); C.Len=Max(A.Len,B.Len); 79 for (int i=1;i<=C.Len;i++) 80 { 81 C.a[i]+=A.a[i]+B.a[i]; 82 C.a[i+1]=C.a[i]/WS; 83 C.a[i]%=WS; 84 } 85 if (C.a[C.Len+1]) C.Len++; 86 return C; 87 } 88 inline BigNum operator - (BigNum A,BigNum B) 89 { 90 BigNum C; C.Clr(); C.Len=Max(A.Len,B.Len); 91 for (int i=1;i<=C.Len;i++) 92 { 93 C.a[i]+=A.a[i]-B.a[i]; 94 if (C.a[i]<0) 95 { 96 C.a[i]+=WS; 97 C.a[i+1]--; 98 } 99 } 100 while (!C.a[C.Len]) C.Len--; 101 return C; 102 } 103 inline BigNum operator * (BigNum A,BigNum B) 104 { 105 BigNum C; C.Clr(); C.Len=A.Len+B.Len+1; 106 for (int i=1;i<=A.Len;i++) 107 for (int j=1;j<=B.Len;j++) 108 { 109 C.a[i+j-1]+=A.a[i]*B.a[j]; 110 C.a[i+j]+=C.a[i+j-1]/WS; 111 C.a[i+j-1]%=WS; 112 } 113 while (!C.a[C.Len]) C.Len--; 114 return C; 115 } 116 inline BigNum operator * (BigNum A,LL p) 117 { 118 BigNum B; B.Clr(); B.Trans_Int(p); 119 return A*B; 120 } 121 inline BigNum operator + (BigNum A,LL p) 122 { 123 BigNum B; B.Clr(); B.Trans_Int(p); 124 return A+B; 125 } 126 inline BigNum operator / (BigNum A,BigNum B) 127 { 128 BigNum C; C.Clr(); BigNum T; T.Clr(); 129 for (LL i=A.Len;i>=1;i--) 130 { 131 T=T*WS; T.a[1]=A.a[i]; 132 while ((T>B) || (T==B)) 133 { 134 T=T-B; 135 C.a[i]++; 136 } 137 } 138 C.Len=A.Len; 139 while (!C.a[C.Len] && C.Len>=2) C.Len--; 140 return C; 141 } 142 inline BigNum operator / (BigNum A,LL p) 143 { 144 BigNum B; B.Clr(); B.Trans_Int(p); 145 return A/B; 146 } 147 inline BigNum operator ^ (BigNum A,int p) 148 { 149 BigNum C; C.Clr(); C.Trans_Int(1); 150 while (true) 151 { 152 if (p&1) C=C*A; 153 A=A*A; p>>=1; if (!p) return C; 154 } 155 } 156 int main() 157 { 158 return 0; 159 }
Suffix Array
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 const int Maxn=100010; 7 int p=0,q=1,n,sa[2][Maxn],rk[2][Maxn],k,v[Maxn],a[Maxn],h[Maxn]; 8 char Str[Maxn]; 9 inline void Sort(int *sa,int *rk,int *Sa,int *Rk) 10 { 11 memset(v,0,sizeof(v)); 12 for (int i=1;i<=n;i++) v[rk[i]]++; 13 for (int i=1;i<=n;i++) v[i]+=v[i-1]; 14 for (int i=n;i;i--) if (sa[i]>k) Sa[v[rk[sa[i]-k]]--]=sa[i]-k; 15 for (int i=n-k+1;i<=n;i++) Sa[v[rk[i]]--]=i; 16 for (int i=1;i<=n;i++) Rk[Sa[i]]=Rk[Sa[i-1]]+(rk[Sa[i]]!=rk[Sa[i-1]] || rk[Sa[i]+k]!=rk[Sa[i-1]+k]); 17 } 18 void Make_Sa() 19 { 20 memset(v,0,sizeof(v)); 21 for (int i=1;i<=n;i++) v[a[i]]++; 22 for (int i=1;i<=30;i++) v[i]+=v[i-1]; 23 for (int i=1;i<=n;i++) sa[p][v[a[i]]--]=i; 24 for (int i=1;i<=n;i++) rk[p][sa[p][i]]=rk[p][sa[p][i-1]]+(a[sa[p][i]]!=a[sa[p][i-1]]); 25 for (k=1;k<=n;k<<=1,p^=1,q^=1) Sort(sa[p],rk[p],sa[q],rk[q]); 26 for (int i=1,k=0;i<=n;i++) 27 { 28 int j=sa[p][rk[p][i]-1]; 29 while (a[i+k]==a[j+k]) k++; 30 h[rk[p][i]]=k; if (k>0) k--; 31 } 32 } 33 int main() 34 { 35 scanf("%s",Str+1); n=strlen(Str+1); 36 for (int i=1;i<=n;i++) a[i]=Str[i]-'a'+1; 37 Make_Sa(); 38 for (int i=1;i<=n;i++) printf("%d ",sa[p][i]); puts(""); 39 for (int i=2;i<=n;i++) printf("%d ",h[i]); puts(""); 40 return 0; 41 }
读入输出优化
1 inline void Get_Int(int &x) 2 { 3 x=0; register char ch=getchar(); int f=1; 4 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 5 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} x*=f; 6 } 7 inline void Put_Int(int x) 8 { 9 char ch[20]; register int top=0; 10 if (x<0) putchar('-'),x=-x; 11 if (x==0) ch[++top]='0'; 12 while (x) ch[++top]=x%10+'0',x/=10; 13 while (top) putchar(ch[top--]); putchar('\n'); 14 }
平衡树(Splay)
1 2 #include <bits/stdc++.h> 3 using namespace std; 4 inline void Get_Int(int &x) 5 { 6 x=0; register char ch=getchar(); int f=1; 7 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 8 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} x*=f; 9 } 10 inline void Put_Int(int x) 11 { 12 char ch[20]; register int top=0; 13 if (x<0) putchar('-'),x=-x; 14 if (x==0) ch[++top]='0'; 15 while (x) ch[++top]=x%10+'0',x/=10; 16 while (top) putchar(ch[top--]); putchar('\n'); 17 } 18 //=================================== 19 const int Maxn=200100; 20 struct Node 21 { 22 Node * ch[2],* pre; int Val,Size,Cnt; 23 inline int d() {return this->pre->ch[1]==this;} 24 inline void Setc(Node * r,int d) {r->pre=this; this->ch[d]=r;} 25 }; 26 Node Memory[Maxn],* port=Memory,* null,* Root; 27 int Type,n,x; 28 inline void Init() {null=port++; null->pre=null->ch[0]=null->ch[1]=null; null->Val=null->Size=null->Cnt=0;} 29 inline Node * NewNode(int v) {Node * Ret=port++; Ret->Val=v; Ret->Size=Ret->Cnt=1; Ret->ch[0]=Ret->ch[1]=Ret->pre=null; return Ret;} 30 inline bool IsRoot(Node * x) {return x->pre==null;} 31 inline void Clr(Node * x) {x->pre=x->ch[0]=x->ch[1]=null; x->Val=x->Size=x->Cnt=0;} 32 inline void Push_Up(Node * x) 33 { 34 if (x==null) return; 35 x->Size=x->ch[0]->Size+x->ch[1]->Size+x->Cnt; 36 } 37 inline void Rotate(Node * x) 38 { 39 Node * y=x->pre; int d=x->d(); 40 if (y->pre!=null )y->pre->Setc(x,y->d()); else x->pre=null; 41 if (x->ch[!d]!=null) y->Setc(x->ch[!d],d); else y->ch[d]=null; 42 x->Setc(y,!d); 43 Push_Up(y); Push_Up(x); 44 } 45 void Splay(Node * x,Node * Goal) 46 { 47 while (x->pre!=Goal) 48 if (x->pre->pre==Goal) Rotate(x); else 49 (x->pre->d()==x->d())?(Rotate(x->pre),Rotate(x)):(Rotate(x),Rotate(x)); 50 Push_Up(x); 51 if (Goal==null) Root=x; 52 } 53 54 inline void Insert(int v) 55 { 56 if (Root==null) {Root=NewNode(v); return;} 57 Node * x=Root; 58 while (x!=null) 59 { 60 x->Size++; 61 if (x->Val==v) {x->Cnt++; break;} 62 if (x->Val<v) 63 { 64 if (x->ch[1]==null) 65 { 66 x->ch[1]=NewNode(v),x->ch[1]->pre=x; 67 x=x->ch[1]; 68 break; 69 } 70 x=x->ch[1]; 71 } 72 if (x->Val>v) 73 { 74 if (x->ch[0]==null) 75 { 76 x->ch[0]=NewNode(v),x->ch[0]->pre=x; 77 x=x->ch[0]; 78 break; 79 } 80 x=x->ch[0]; 81 } 82 } 83 Splay(x,null); 84 } 85 inline void Delete(int Val) 86 { 87 Node * x=Root; 88 while (x!=null) 89 { 90 x->Size--; 91 if (x->Val==Val) {x->Cnt--; break;} 92 x=x->ch[x->Val<Val]; 93 } 94 if (x->Cnt) return; 95 Splay(x,null); 96 if (x->ch[0]==null) {Root=x->ch[1]; Root->pre=null; return;} 97 if (x->ch[1]==null) {Root=x->ch[0]; Root->pre=null; return;} 98 x=x->ch[0]; 99 while (x->ch[1]!=null) x=x->ch[1]; 100 Splay(x,Root); 101 x->ch[1]=Root->ch[1]; x->ch[1]->pre=x; 102 x->pre=null; Push_Up(x); Root=x; 103 } 104 inline Node * Find(int Val) 105 { 106 Node * x=Root; 107 while (true) 108 { 109 if (x->Val==Val) return x; 110 if (x->Val<Val) x=x->ch[1]; 111 if (x->Val>Val) x=x->ch[0]; 112 } 113 } 114 inline int Get_Pre(int v) 115 { 116 Node * x=Root; int Tmp=0; 117 while (x!=null) 118 if (x->Val<v) Tmp=x->Val,x=x->ch[1]; else x=x->ch[0]; 119 return Tmp; 120 } 121 inline int Get_Suc(int v) 122 { 123 Node * x=Root; int Tmp=0; 124 while (x!=null) 125 if (x->Val>v) Tmp=x->Val,x=x->ch[0]; else x=x->ch[1]; 126 return Tmp; 127 } 128 inline int Rank(Node * x,int k) 129 { 130 while (x!=null) 131 { 132 int l=x->ch[0]->Size+1; 133 int r=x->ch[0]->Size+x->Cnt; 134 if (l<=k && k<=r) return x->Val; 135 if (k>r) k-=r,x=x->ch[1]; else x=x->ch[0]; 136 } 137 } 138 139 inline int Get_Rank(int v) 140 { 141 Node * x=Root; int Ret=0; 142 while (x!=null) 143 { 144 if (x->Val==v) return Ret+x->ch[0]->Size+1; 145 if (x->Val<v) Ret+=x->ch[0]->Size+x->Cnt,x=x->ch[1]; 146 if (x->Val>v) x=x->ch[0]; 147 } 148 return -1; 149 } 150 int main() 151 { 152 Get_Int(n); Init(); Root=null; 153 for (int i=1;i<=n;i++) 154 { 155 Get_Int(Type),Get_Int(x); 156 if (Type==1) Insert(x); 157 if (Type==2) Delete(x); 158 if (Type==3) Put_Int(Get_Rank(x)); 159 if (Type==4) Put_Int(Rank(Root,x)); 160 if (Type==5) Put_Int(Get_Pre(x)); 161 if (Type==6) Put_Int(Get_Suc(x)); 162 } 163 return 0; 164 }
Dinic
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #define Inf 0x7fffffff 6 using namespace std; 7 const int Maxn=400; 8 struct node 9 { 10 int to,next,w; 11 }edge[Maxn<<3]; //Warning:存边的数组要尽量开的大 12 int head[Maxn],cur[Maxn],Q[Maxn],Level[Maxn]; 13 int n,m,u,v,f,cnt,ans,temp,S,T; 14 inline int Min(int a,int b) {return a>b?b:a;} 15 inline void Add(int u,int v,int f) 16 { 17 edge[cnt].to=v; 18 edge[cnt].next=head[u]; 19 edge[cnt].w=f; 20 head[u]=cnt; 21 cnt++; 22 edge[cnt].to=u; 23 edge[cnt].next=head[v]; 24 edge[cnt].w=0; //Warning:反向边为零(经常打错) 25 head[v]=cnt; 26 cnt++; 27 } 28 29 int Find(int u,int low) 30 { 31 if (u==T || low==0) return low; 32 int tmp=0,cap=0;//cap记录当前流的流量 33 for (int i=cur[u];i!=-1;i=edge[i].next) //按照分层图Dfs 34 { 35 int v=edge[i].to; 36 if (edge[i].w>0 37 && Level[v]==Level[u]+1) 38 { 39 tmp=Find(v,Min(low,edge[i].w)); //这个点能流多少 40 if (tmp==0) continue; 41 low-=tmp; cap+=tmp; 42 edge[i].w-=tmp,edge[i^1].w+=tmp; //i和i^1互为反向边 2^1=3 3^1=2 43 if (edge[i].w>0) cur[u]=i;//当前弧优化 44 } 45 if (low==0) break; //已经不能再流了则退出! 46 } 47 if (cap) return cap; 48 Level[u]=-1; //优化:以后这个点就不用来了! 49 return 0; 50 } 51 52 53 bool Bfs() 54 { 55 int l=1,r=1; 56 memset(Level,-1,sizeof(Level)); 57 Q[1]=S;Level[S]=0; //找分层图 58 while (l<=r) 59 { 60 int u=Q[l++]; 61 for (int i=head[u];i!=-1;i=edge[i].next) 62 if (Level[edge[i].to]==-1 && edge[i].w>0) 63 { 64 Level[edge[i].to]=Level[u]+1; 65 Q[++r]=edge[i].to; 66 } 67 } 68 if (Level[T]>0) return true; 69 return false; 70 } 71 72 inline void Dinic() 73 { 74 ans=0; 75 int tmp; 76 while (Bfs()) 77 { 78 for (int i=1;i<=T;i++) cur[i]=head[i]; 79 ans+=Find(S,Inf); 80 } 81 } 82 83 84 int main() 85 { 86 while(scanf("%d%d",&m,&n)!=EOF) 87 { 88 S=1,T=n; //S为源点,T为汇点(多组数据时我读入也会用T读!!) 89 memset(head,-1,sizeof(head)); 90 cnt=0; 91 for (int i=1;i<=m;i++) 92 { 93 scanf("%d%d%d",&u,&v,&f); 94 Add(u,v,f); //存双向边的两条都要存 95 } 96 Dinic(); 97 98 printf("%d\n",ans); 99 } 100 return 0; 101 }
Dijkstra
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #define se second 8 #define fi first 9 #define mp make_pair 10 #define Pa pair<int,int> 11 using namespace std; 12 const int Maxn=1010; 13 const int Inf=0x3f3f3f3f; 14 struct EDGE{int to,next,w;}edge[Maxn<<3]; 15 int head[Maxn],Dis[Maxn],vis[Maxn],m,n,cnt,u,v,w; 16 priority_queue<Pa,vector<Pa>,greater<Pa> >Q; 17 inline void Add(int u,int v,int w) 18 {edge[cnt].to=v;edge[cnt].next=head[u];edge[cnt].w=w;head[u]=cnt++;} 19 void Dij() 20 { 21 memset(vis,false,sizeof(vis)); 22 for (int i=1;i<=n;i++) Dis[i]=Inf; 23 Dis[1]=0; Q.push(mp(0,1)); 24 while (!Q.empty()) 25 { 26 int u=Q.top().se; Q.pop(); 27 if (vis[u]) continue; vis[u]=true; 28 for (int i=head[u];i!=-1;i=edge[i].next) 29 if (Dis[edge[i].to]>Dis[u]+edge[i].w && !vis[edge[i].to]) 30 { 31 Dis[edge[i].to]=Dis[u]+edge[i].w; 32 Q.push(mp(Dis[edge[i].to],edge[i].to)); 33 } 34 } 35 } 36 int main() 37 { 38 while (scanf("%d%d",&m,&n)!=EOF) 39 { 40 cnt=0; memset(head,-1,sizeof(head)); 41 for (int i=1;i<=m;i++) 42 { 43 scanf("%d%d%d",&u,&v,&w); 44 Add(u,v,w),Add(v,u,w); 45 } 46 Dij(); 47 printf("%d\n",Dis[n]); 48 } 49 return 0; 50 }
Spfa
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 #include <vector> 6 #include <queue> 7 #define se second 8 #define fi first 9 #define mp make_pair 10 #define Pa pair<int,int> 11 using namespace std; 12 const int Maxn=1010; 13 const int Inf=0x3f3f3f3f; 14 struct EDGE{int to,next,w;}edge[Maxn<<3]; 15 int head[Maxn],Dis[Maxn],vis[Maxn],m,n,cnt,u,v,w; 16 inline void Add(int u,int v,int w) 17 {edge[cnt].to=v;edge[cnt].next=head[u];edge[cnt].w=w;head[u]=cnt++;} 18 queue<int> Q; 19 void Spfa() 20 { 21 memset(vis,false,sizeof(vis)); 22 for (int i=1;i<=n;i++) Dis[i]=Inf; 23 Q.push(1); Dis[1]=0; vis[1]=true; 24 while (!Q.empty()) 25 { 26 int u=Q.front(); Q.pop(); 27 for (int i=head[u];i!=-1;i=edge[i].next) 28 if (Dis[edge[i].to]>Dis[u]+edge[i].w) 29 { 30 Dis[edge[i].to]=Dis[u]+edge[i].w; 31 if (!vis[edge[i].to]) 32 { 33 vis[edge[i].to]=true; 34 Q.push(edge[i].to); 35 } 36 } 37 vis[u]=false; 38 } 39 } 40 int main() 41 { 42 while (scanf("%d%d",&m,&n)!=EOF) 43 { 44 cnt=0; memset(head,-1,sizeof(head)); 45 for (int i=1;i<=m;i++) 46 { 47 scanf("%d%d%d",&u,&v,&w); 48 Add(u,v,w),Add(v,u,w); 49 } 50 Spfa(); 51 printf("%d\n",Dis[n]); 52 } 53 return 0; 54 }