noip模板复习
QAQ明天就noip了???这么快!!!!
所有人都开始疯狂敲键盘了,虽然其中不乏还有在最后几个小时还在刷bzoj的大佬,但是基本都在打模板QAQ
dijkstra
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<cmath>
5 #include<cstdlib>
6 #include<iostream>
7 #include<queue>
8 #define maxn 10005
9 using namespace std;
10
11 struct edge{
12 int u,v,w,nxt;
13 }e[maxn*4];
14
15 int dis[maxn],head[maxn],vis[maxn];
16 int n,m,k,ans;
17
18 int tot;
19 void adde(int u,int v,int w){
20 e[tot]=(edge){u,v,w,head[u]};
21 head[u]=tot++;
22 }
23
24 struct node{
25 int id,dis;
26 bool operator<(const node&a)const{
27 return dis>a.dis;}
28 };
29
30 priority_queue<node>q;
31
32 void dijkstra(int s,int t){
33 for(int i=1;i<=n;i++){
34 dis[i]=(i==s)?0:0x3f3f3f;
35 vis[i]=0;
36 }
37 q.push((node){s,0});
38 while(!q.empty()){
39 node x=q.top();
40 int u=x.id;q.pop();
41 if(vis[u])continue;
42 vis[u]=1;
43 for(int i=head[u];i!=-1;i=e[i].nxt ){
44 int v=e[i].v;
45 if(dis[u]+e[i].w<dis[v]&&!vis[v]){
46 dis[v]=dis[u]+e[i].w;
47 q.push((node){v,dis[v]});
48 }
49 }
50 }
51 printf("%d",dis[t]);
52 }
53
54 int main(){
55 memset(head,-1,sizeof(head));
56 scanf("%d%d",&n,&m);
57 for(int i=1;i<=m;i++){
58 int u,v,w;
59 scanf("%d%d%d",&u,&v,&w);
60 adde(u,v,w);adde(v,u,w);
61 }
62 int s,t;
63 scanf("%d%d",&s,&t);
64 dijkstra(s,t);
65 return 0;
66 }
spfa
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<cmath>
5 #include<cstdlib>
6 #include<iostream>
7 #include<queue>
8 #define maxn 10005
9 using namespace std;
10
11 struct edge{
12 int u,v,w,nxt;
13 }e[maxn*4];
14
15 int dis[maxn],head[maxn],inq[maxn];
16 int n,m,k,ans;
17
18 int tot;
19 void adde(int u,int v,int w){
20 e[tot]=(edge){u,v,w,head[u]};
21 head[u]=tot++;
22 }
23
24 queue<int>q;
25
26 void spfa(int s,int t){
27 for(int i=1;i<=n;i++){
28 dis[i]=(i==s)?0:0x3f3f3f;
29 }
30 q.push(s);inq[s]=1;
31 while(!q.empty()){
32 int u=q.front();
33 q.pop();inq[u]=0;
34 for(int i=head[u];i!=-1;i=e[i].nxt ){
35 int v=e[i].v;
36 if(dis[u]+e[i].w<dis[v]){
37 dis[v]=dis[u]+e[i].w;
38 if(!inq[v]){
39 q.push(v);inq[v]=1;
40 }
41 }
42 }
43 }
44 printf("%d",dis[t]);
45 }
46
47 int main(){
48 memset(head,-1,sizeof(head));
49 scanf("%d%d",&n,&m);
50 for(int i=1;i<=m;i++){
51 int u,v,w;
52 scanf("%d%d%d",&u,&v,&w);
53 adde(u,v,w);adde(v,u,w);
54 }
55 int s,t;
56 scanf("%d%d",&s,&t);
57 spfa(s,t);
58 return 0;
59 }
topsort
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<iostream>
4 #include<algorithm>
5 #include<cstdlib>
6 #include<stack>
7 #define maxn 10005
8 using namespace std;
9
10 struct node{
11 int u,v,nxt;
12 }e[maxn*4];
13
14 int n,m,in[maxn],head[maxn],root;
15 int ans[maxn],sum;
16
17 int tot;
18 void adde(int u,int v){
19 e[tot]=(node){u,v,head[u]};
20 head[u]=tot++;in[v]++;
21 }
22
23 stack<int >s;
24 void topsort(int x){
25 s.push(x);
26 while(!s.empty()){
27 int u=s.top();s.pop();
28 ans[++sum]=u;
29 for(int i=head[u];i!=-1;i=e[i].nxt ){
30 int v=e[i].v;
31 in[v]--;
32 if(!in[v]){
33 s.push(v);
34 }
35 }
36 }
37 if(sum==n){
38 for(int i=1;i<=n;i++){
39 printf("%d ",ans[i]);
40 }
41 }else printf("ERROR");
42 }
43
44 int main(){
45 scanf("%d%d",&n,&m);
46 memset(head,-1,sizeof(head));
47 for(int i=1;i<=m;i++){
48 int u,v;
49 scanf("%d%d",&u,&v);
50 adde(u,v);
51 }
52 for(int i=1;i<=n;i++){
53 if(!in[i]){root=i;break;}
54 }
55 topsort(root);
56 return 0;
57 }
kruskal
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<algorithm>
3 #include<iostream>
4 #include<cmath>
5 #include<cstring>
6 #include<cstdlib>
7 #include<queue>
8 #define maxn 10005
9 using namespace std;
10
11 struct node{
12 int u,v,w,nxt;
13 }e[maxn*4];
14
15 int n,m,fa[maxn],head[maxn],sum,ans;
16
17 int tot;
18 void adde(int u,int v,int w){
19 e[tot]=(node){u,v,w,head[u]};
20 head[u]=tot++;
21 }
22
23 int comp(const void *a,const void *b){
24 return (*(struct node*)a).w>(*(struct node*)b).w?1:-1;
25 }
26
27 int find(int x){
28 if(fa[x]==x)return x;
29 return fa[x]=find(fa[x]);
30 }
31
32 int main(){
33 scanf("%d%d",&n,&m);
34 memset(head,-1,sizeof(head));
35 for(int i=1;i<=m;i++){
36 int u,v,w;
37 scanf("%d%d%d",&u,&v,&w);
38 adde(u,v,w);adde(v,u,w);
39 }
40 for(int i=1;i<=n;i++)fa[i]=i;
41 qsort(e,tot,sizeof(e[0]),comp);
42 for(int i=0;i<tot;i++){
43 int u=e[i].u,v=e[i].v;
44 int fu=find(u),fv=find(v);
45 if(fu!=fv){
46 fa[fv]=fu;
47 sum++;ans+=e[i].w;
48 if(sum==n-1){
49 printf("%d",ans);break;
50 }
51 }
52 }
53 return 0;
54 }
55
prim
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<algorithm>
3 #include<iostream>
4 #include<cmath>
5 #include<cstring>
6 #include<cstdlib>
7 #include<queue>
8 #define maxn 10005
9 using namespace std;
10
11 struct node{
12 int u,v,w,nxt;
13 }e[maxn*4];
14
15 int n,m,head[maxn],sum,ans;
16 int low[maxn],mst[maxn],vis[maxn];
17
18 int tot;
19 void adde(int u,int v,int w){
20 e[tot]=(node){u,v,w,head[u]};
21 head[u]=tot++;
22 }
23
24 void prim(){
25 for(int i=head[1];i!=-1;i=e[i].nxt ){
26 int v=e[i].v;
27 low[v]=e[i].w;mst[v]=1;
28 }sum=1;vis[1]=1;
29 int minn=0x3f3f3f,minid;
30 while(sum<n){
31 minn=0x3f3f3f;
32 for(int i=1;i<=n;i++){
33 if(!vis[i]&&low[i]&&minn>low[i]){
34 minn=low[i];minid=i;
35 }
36 }
37 ans+=low[minid];
38 vis[minid]=1;low[minid]=0;mst[minid]=0;sum++;
39 if(sum==n){
40 printf("%d",ans);
41 }
42 for(int i=head[minid];i!=-1;i=e[i].nxt ){
43 int v=e[i].v;
44 if(!vis[v]&&e[i].w<low[v]){
45 low[v]=e[i].w;mst[v]=minid;
46 }
47 }
48
49 }
50
51 }
52
53 int main(){
54 scanf("%d%d",&n,&m);
55 memset(head,-1,sizeof(head));
56 for(int i=1;i<=m;i++){
57 int u,v,w;
58 scanf("%d%d%d",&u,&v,&w);
59 adde(u,v,w);adde(v,u,w);
60 }
61 prim();
62 return 0;
63 }
RMQ
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<cmath>
5 #include<cstdlib>
6 #include<iostream>
7 #include<queue>
8 #define maxn 100005
9 using namespace std;
10 //max
11 int f[maxn][32];
12 int a[maxn],n,m;
13
14 void first(){
15 for(int j=1;j<=16;j++){
16 for(int i=1;i<=n;i++){
17 f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
18 }
19 }
20 }
21
22 int main(){
23 scanf("%d",&n);
24 for(int i=1;i<=n;i++){
25 scanf("%d",&a[i]);
26 f[i][0]=a[i];
27 }
28 first();
29 scanf("%d",&m);
30 for(int i=1;i<=m;i++){
31 int l,r;
32 scanf("%d%d",&l,&r);
33 int ans=f[l][0],k=0;
34 while((1<<(k+1))<=r-l+1)k++;
35 ans=max(f[l][k],f[r-(1<<k)+1][k]);
36 printf("%d\n",ans);
37 }
38 return 0;
39 }
LCA倍增
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<iostream>
5 #include<cstdlib>
6 #include<queue>
7 #include<cmath>
8 #define maxn 10005
9 using namespace std;
10
11 struct node{
12 int u,v,w,nxt;
13 }e[maxn*4];
14
15 int dis[maxn][17],f[maxn][17],vis[maxn];
16 int n,m,head[maxn],dep[maxn],ans;
17
18 int tot;
19 void adde(int u,int v,int w){
20 e[tot]=(node){u,v,w,head[u]};
21 head[u]=tot++;
22 }
23
24 void build(int u){
25 vis[u]=1;
26 for(int i=head[u];i!=-1;i=e[i].nxt ){
27 int v=e[i].v;
28 if(!vis[v]){
29 dep[v]=dep[u]+1;
30 f[v][0]=u;
31 dis[v][0]=e[i].w;
32 build(v);
33 }
34 }
35 }
36
37 void first(){
38 for(int j=1;j<=16;j++){
39 for(int i=1;i<=n;i++){
40 f[i][j]=f[f[i][j-1]][j-1];
41 }
42 }
43 }
44
45 int up(int x,int d){
46 int i=0;
47 while(d){
48 if(d&1){
49 x=f[x][i];
50 }d>>=1;i++;
51 }
52 return x;
53 }//类似快速幂的做法
54
55 int lca(int u,int v){
56 if(dep[u]<dep[v])swap(u,v);
57 if(dep[u]!=dep[v]){
58 int d=dep[u]-dep[v];
59 u=up(u,d);
60 }
61 if(u==v)return u;
62 for(int k=16;k>=0;k--){
63 if(f[u][k]!=f[v][k]){
64 u=f[u][k];
65 v=f[v][k];
66 }
67 }
68 return f[u][0];
69
70 }
71
72 int main(){
73 memset(head,-1,sizeof(head));
74 scanf("%d%d",&n,&m);
75 for(int i=1;i<n;i++){
76 int u,v,w;
77 scanf("%d%d%d",&u,&v,&w);
78 adde(u,v,w);adde(v,u,w);
79 }
80 build(1);
81 first();
82 for(int i=1;i<=m;i++){
83 int u,v;cin>>u>>v;
84 int lcaid=lca(u,v);
85 cout<<lcaid<<endl;
86 }
87 return 0;
88 }
LCA:tarjan
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<iostream>
5 #include<cstdlib>
6 #include<queue>
7 #include<cmath>
8 #define maxn 40005
9 using namespace std;
10
11 struct node{
12 int u,v,nxt;
13 }e[maxn*4],p[maxn*4];
14
15 int n,m,dfn[maxn],fa[maxn],belong[maxn],root,maxid;
16 int head[maxn],heap[maxn],vis[maxn*4],visid[maxn];
17 int ord[maxn*4];
18
19 int tot;
20 void adde(int u,int v){
21 e[tot]=(node){u,v,head[u]};
22 head[u]=tot++;
23 }
24
25 int tpt;
26 void addp(int u,int v,int pos){
27 p[tpt]=(node){u,v,heap[u]};
28 ord[tpt]=pos;heap[u]=tpt++;
29 }
30
31 int find(int x){
32 if(fa[x]==x)return x;
33 return fa[x]=find(fa[x]);
34 }
35
36 int num;
37 void tarjan(int u,int from){
38 dfn[u]=++num;
39 for(int i=head[u];i!=-1;i=e[i].nxt ){
40 int v=e[i].v;
41 if(!dfn[v]){
42 tarjan(v,u);fa[v]=u;visid[v]=1;
43 }
44 }
45 for(int i=heap[u];i!=-1;i=p[i].nxt){
46 int vv=p[i].v;
47 if(visid[vv]&&!vis[ord[i]]){
48 vis[ord[i]]=find(vv);
49 }
50 }fa[u]=from;visid[u]=1;
51 }
52
53 int main(){
54 freopen("tarjanlca.txt","r",stdin);
55 memset(head,-1,sizeof(head));
56 memset(heap,-1,sizeof(heap));
57 scanf("%d",&n);
58 for(int i=1;i<=n;i++){
59 int u,v;scanf("%d%d",&u,&v);
60 if(v==-1){root=u;continue;}
61 adde(u,v);adde(v,u);maxid=max(maxid,max(u,v));
62 }
63 for(int i=1;i<=maxid;i++)fa[i]=i;
64 scanf("%d",&m);
65 for(int i=1;i<=m;i++){
66 int u,v;scanf("%d%d",&u,&v);
67 addp(u,v,i);addp(v,u,i);
68 }visid[root]=1;
69 tarjan(root,0);
70 for(int i=1;i<=m;i++){
71 int lca=vis[i],pos=(i-1)*2;
72 if(lca==p[pos].u )
73 puts("1");
74 else if(lca==p[pos].v )
75 puts("2");
76 else puts("0");
77 }
78 return 0;
79 }
强连通分量tarjan
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<iostream>
5 #include<cstdlib>
6 #include<queue>
7 #include<cmath>
8 #include<stack>
9 #define maxn 10005
10 using namespace std;
11
12 struct node{
13 int u,v,nxt;
14 }e[maxn*4];
15
16 int n,m,dfn[maxn],low[maxn],belong[maxn],head[maxn],num,ans;
17
18 int tot;
19 void adde(int u,int v){
20 e[tot]=(node){u,v,head[u]};
21 head[u]=tot++;
22 }
23
24 stack<int>s;
25
26 void tarjan(int u){
27 dfn[u]=low[u]=++num;s.push(u);
28 for(int i=head[u];i!=-1;i=e[i].nxt ){
29 int v=e[i].v;
30 if(!dfn[v]){
31 tarjan(v);
32 low[u]=min(low[u],low[v]);
33 }else{
34 if(!belong[v]){
35 low[u]=min(low[u],dfn[v]);
36 }
37 }
38 }
39 if(dfn[u]==low[u]){
40 ans++;belong[u]=ans;
41 while(s.top()!=u){
42 belong[s.top()]=ans;
43 s.pop();
44 }s.pop();
45 }
46 }
47
48 int main(){
49 memset(head,-1,sizeof(head));
50 scanf("%d%d",&n,&m);
51 for(int i=1;i<=m;i++){
52 int u,v;
53 scanf("%d%d",&u,&v);
54 adde(u,v);
55 }
56 for(int i=1;i<=n;i++){
57 if(!dfn[i])tarjan(i);
58 }cout<<ans;
59 return 0;
60 }
匈牙利算法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<cstdlib>
4 #include<iostream>
5 #include<algorithm>
6 #include<cmath>
7 #define maxn 10005
8 using namespace std;
9
10 struct edge{
11 int u,v,nxt;
12 }e[maxn*4];
13
14 int n,m,att[maxn],head[maxn],ans,vis[maxn];
15
16 int read(){
17 int xx=0,ff=1;char ch=getchar();
18 while(ch<'0'||ch>'9'){if(ch=='-')ff=-1;ch=getchar();}
19 while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
20 return xx*ff;
21 }
22
23 int tot;
24 void adde(int u,int v){
25 e[tot]=(edge){u,v,head[u]};
26 head[u]=tot++;
27 }
28
29 bool check(int u){
30 for(int i=head[u];i!=-1;i=e[i].nxt){
31 int v=e[i].v;
32 if(vis[v])continue;
33 vis[v]=1;
34 if(!att[v]||check(att[v])){
35 att[v]=u;return 1;
36 }
37 }return 0;
38 }
39
40 int main(){
41 memset(head,-1,sizeof(head));
42 n=read();m=read();
43 for(int i=1;i<=m;i++){
44 int u,v;
45 u=read();v=read();
46 adde(u,v);
47 }
48 for(int i=1;i<=n;i++){
49 memset(vis,0,sizeof(vis));
50 if(check(i))ans++;
51 }
52 return 0;
53 }
KMP(看毛片算法)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<iostream>
4 using namespace std;
5 char A[1000005],B[1000005];
6 int nxt[1000005];
7 void get_nxt(char *s){
8 nxt[0]=-1; int k=-1,j=0;
9 while(s[j]){
10 if(k==-1||s[j]==s[k]){
11 k++; j++; nxt[j]=k;
12 }
13 else k=nxt[k];
14 }
15 }
16 void matching(char *T,char *S){
17 int i=0,j=0;
18 while(T[i]){
19 if(j==-1||T[i]==S[j]) i++,j++;
20 else j=nxt[j];
21 if(~j&&!S[j]) printf("%d\n",i-j+1);
22 }
23 for(int i=1;S[i-1];i++) printf("%d ",nxt[i]);
24 }
25 int main()
26 {
27 scanf("%s",A); scanf("%s",B);
28 get_nxt(B);
29 matching(A,B);
30 return 0;
31 }
这是我剽窃ZJ大佬的(毕竟我目前还不会这个东东)
dinic
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<cmath>
5 #include<cstdlib>
6 #include<iostream>
7 #include<queue>
8 #define maxn 1005
9 using namespace std;
10
11 struct edge {
12 int u,v,c,nxt,flow;
13 } e[maxn*maxn];
14
15 int n,m,head[maxn],dep[maxn],fl[maxn],cl[maxn],ans;
16
17 int tot;
18 void adde(int u,int v,int c) {
19 e[tot].u=u;
20 e[tot].v=v;
21 e[tot].c=c;
22 e[tot].nxt=head[u];
23 head[u]=tot;
24 tot++;
25 }
26
27 queue<int>q;
28 bool bfs(){
29 q.push(1);
30 memset(dep,-1,sizeof(dep));
31 dep[1]=0;
32 while(!q.empty()){
33 int u=q.front();q.pop();
34 for(int i=head[u];i!=-1;i=e[i].nxt ){
35 int v=e[i].v;
36 if(dep[v]==-1&&e[i].flow<e[i].c ){
37 dep[v]=dep[u]+1;q.push(v);
38 }
39 }
40 }
41 return dep[n]!=-1;
42 }
43
44 int dfs(int u,int lim){
45 if(u==n){return lim;}
46 int t;
47 for(int i=head[u];i!=-1;i=e[i].nxt ){
48 int v=e[i].v;
49 if(dep[v]!=dep[u]+1)continue;
50 if(e[i].c>e[i].flow && (t=dfs(v,min(lim,e[i].c-e[i].flow)))){
51 e[i].flow+=t;e[i^1].flow-=t;return t;
52 }
53 }return 0;
54 }
55
56 int main() {
57 memset(head,-1,sizeof(head));
58 scanf("%d%d",&m,&n);
59 for(int i=1; i<=m; i++) {
60 int u,v,c;
61 scanf("%d%d%d",&u,&v,&c);
62 adde(u,v,c);
63 adde(v,u,0);
64 }
65 while(bfs()){
66 ans+=dfs(1,0x3f3f3f);
67 }printf("%d",ans);
68 return 0;
69 }
线性筛和欧拉筛
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 int vis1[100005],prime1[10005]; 3 int vis2[100005],prime2[10005]; 4 int tot; 5 int main(){ 6 int n; 7 scanf("%d",&n);vis1[1]=vis2[1]=1; 8 for(int i=2;i<=n;i++){ 9 if(!vis1[i])prime1[++tot]=i; 10 for(int j=1;j<=tot&&i*prime1[j]<=n;j++){ 11 vis1[i*prime1[j]]=1; 12 if(i%prime1[j]==0)break; 13 } 14 } 15 for(int i=2,j;(j=i*i)<=n;i++){ 16 if(!vis2[i])prime2[++tot]=i; 17 for(;j<=n;j+=i){ 18 vis2[j]=1; 19 } 20 } 21 22 for(int i=1;i<=n;i++){ 23 if(!vis1[i])printf("%d ",i); 24 }printf("\n"); 25 for(int i=1;i<=n;i++){ 26 if(!vis2[i])printf("%d ",i); 27 } 28 }
高精度加法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<iostream>
5 #include<cstdlib>
6 #include<cmath>
7 #define maxn 505
8 using namespace std;
9
10 int a[maxn],b[maxn],la,lb,ans[maxn];
11 char s1[maxn],s2[maxn];
12
13 int main(){
14 cin>>s1>>s2;
15 la=strlen(s1);lb=strlen(s2);
16 for(int i=1;i<=la;i++){a[la-i+1]=s1[i-1]-'0';}
17 for(int i=1;i<=lb;i++){b[lb-i+1]=s2[i-1]-'0';}
18 for(int i=1;i<=max(la,lb);i++)a[i]+=b[i];
19 int l=max(la,lb);
20 for(int i=1;i<=l;i++)
21 if(a[i]>=10)a[i+1]+=a[i]/10,a[i]%=10;
22 while(a[l+1]){
23 l++;
24 if(a[l]>=10)a[l+1]+=a[l]/10;a[l]%=10;
25 }
26 for(int i=l;i>=1;i--)
27 printf("%d",a[i]);
28 return 0;
29 }
高精度减法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<iostream>
5 #include<cstdlib>
6 #include<cmath>
7 #define maxn 505
8 using namespace std;
9
10 int a[maxn],b[maxn],la,lb,ans[maxn];
11 char s1[maxn],s2[maxn];
12 int ff=1;
13
14 void change(){
15 for(int i=1;i<=la;i++)ans[i]=a[i];
16 for(int i=1;i<=lb;i++)a[i]=b[i];
17 for(int i=1;i<=la;i++)b[i]=ans[i];
18 }
19
20 int main(){
21 cin>>s1>>s2;
22 la=strlen(s1);lb=strlen(s2);//a>b
23 for(int i=1;i<=la;i++){a[la-i+1]=s1[i-1]-'0';}
24 for(int i=1;i<=lb;i++){b[lb-i+1]=s2[i-1]-'0';}
25 if(lb>la){
26 change();ff=-1;
27 swap(la,lb);
28 for(int i=lb+1;i<=la;i++)b[i]=0;
29 }else if(la==lb){
30 for(int i=la;i>=1;i--){
31 if(b[i]>a[i]){change();ff=-1;break;}
32 if(b[i]<a[i])break;
33 }
34 }
35 for(int i=1;i<=max(la,lb);i++)a[i]-=b[i];
36
37 int l=max(la,lb);
38 for(int i=1;i<=l;i++)
39 if(a[i]<0)a[i+1]-=1,a[i]+=10;
40 while(!a[l]){l--;}
41 if(ff==-1)printf("-");
42 for(int i=l;i>=1;i--)
43 printf("%d",a[i]);
44 if(l==-1)puts("0");
45 return 0;
46 /*
47 283756
48 1371298
49 */
50 }
高精度乘法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cmath>
#define maxn 505
using namespace std;
int a[maxn],b[maxn],la,lb,ans[maxn];
char s1[maxn],s2[maxn];
void mul(){
for(int i=1;i<=lb;i++){
for(int j=1;j<=la;j++){
ans[i+j-1]+=b[i]*a[j];
}
}
int l=la+lb-1;
while(ans[l+1]){l++;}
for(int i=1;i<=l;i++){
if(ans[i]>=10){
ans[i+1]+=ans[i]/10;
ans[i]%=10;
}
}while(ans[l+1]){
l++;
if(ans[l]>=10){
ans[l+1]+=ans[l]/10;
ans[l]%=10;
}
}
for(int i=l;i>=1;i--){
printf("%d",ans[i]);
}
}
int main(){
cin>>s1>>s2;
la=strlen(s1);lb=strlen(s2);
for(int i=1;i<=la;i++){a[la-i+1]=s1[i-1]-'0';}
for(int i=1;i<=lb;i++){b[lb-i+1]=s2[i-1]-'0';}
mul();
return 0;
}
高精度除法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 #include<iostream>
5 #include<queue>
6 #include<cstdlib>
7 #include<cmath>
8 #define maxn 1005
9 using namespace std;
10
11 int a1[maxn],a2[maxn],a3[maxn],a4[maxn];
12 int len1,len2;
13 char s1[maxn],s2[maxn];
14
15 int check(int a[],int b[]){
16 if(a[0]<b[0])return 0;
17 if(a[0]>b[0])return 1;
18 for(int i=a[0];i>=1;i--){
19 if(a[i]>b[i])return 1;
20 if(a[i]<b[i])return 0;
21 }
22 return 1;
23 }
24
25 void _minus(int a[],int b[]){
26 for(int i=1;i<=a[0];i++){
27 if(a[i]<b[i]){
28 a[i]+=10;a[i+1]--;
29 }
30 a[i]-=b[i];
31 }
32 while(a[a[0]]==0&&a[0]>1)//余数的位数
33 a[0]--;
34 }
35
36 int main(){
37 scanf("%s%s",s1+1,s2+1);
38 len1=strlen(s1+1);
39 len2=strlen(s2+1);
40 for(int i=1;i<=len1;i++)a1[i]=s1[len1-i+1]-'0';
41 for(int i=1;i<=len2;i++)a2[i]=s2[len2-i+1]-'0';
42 a1[0]=len1;a2[0]=len2;
43 a4[0]=len1-len2+1;
44 for(int i=a4[0];i>0;i--){
45 memset(a3,0,sizeof(a3));
46 for(int j=1;j<=a2[0];j++){
47 a3[j+i-1]=a2[j];//移i位
48 }
49 a3[0]=a2[0]+i-1;
50 while(check(a1,a3)){
51 a4[i]++;_minus(a1,a3);
52 }
53 }
54 while(a4[a4[0]]==0&&a4[0]>1)//商的位数
55 a4[0]--;
56 for(int i=a4[0];i>=1;i--){
57 printf("%d",a4[i]);
58 }
59 /*
60 3886218
61 56322
62 out:69
63 */
64 }
离散化操作
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<iostream>
4 #include<algorithm>
5 #include<cmath>
6 #include<queue>
7 #include<cstdlib>
8 #define maxn 300005
9 using namespace std;
10
11 struct node{
12 int l,r;
13 }e[maxn];
14
15 int ord[maxn*2];
16 int n,m,k;
17
18 int cmp(int a,int b){
19 return a<b;
20 }
21
22 int main(){
23 //freopen("离散化.txt","r",stdin);
24 scanf("%d",&n);int y=0;
25 for(int i=1;i<=n;i++){
26 int l,r;
27 scanf("%d%d",&l,&r);
28 e[i].l=l;e[i].r=r;
29 ord[++y]=e[i].l;ord[++y]=e[i].r;
30 }
31 sort(ord+1,ord+y+1,cmp);
32 y=unique(ord+1,ord+y+1)-ord-1;
33 for(int i=1;i<=n;i++){
34 int l=lower_bound(ord+1,ord+y+1,e[i].l)-ord;
35 int r=lower_bound(ord+1,ord+y+1,e[i].r)-ord;
36 printf("%d: %d %d\n",i,l,r);
37 }
38 return 0;
39 }
数字hash表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<iostream>
4 #include<algorithm>
5 #include<cmath>
6 #include<queue>
7 #include<cstdlib>
8 #define maxn 300005
9 #define mod 9317
10 #define ll long long
11 using namespace std;
12
13 ll n,a,ans;
14 ll ha[mod+2][1000];
15
16 bool hash(ll x){
17 ll b=x%mod;
18 if(!ha[b][0]){
19 ll l=ha[b][0];l++;
20 ha[b][0]++;ha[b][l]=x;
21 return true;
22 }else{
23 ll l=ha[b][0];
24 for(ll i=1;i<=l;i++){
25 if(x==ha[b][i])return false;
26 }
27 l=++ha[b][0];
28 ha[b][l]=x;
29 return true;
30 }
31 }
32
33 int main(){
34 // freopen("hash.txt","r",stdin);
35 scanf("%lld",&n);
36 for(ll i=1;i<=n;i++){
37 scanf("%lld",&a);
38 bool k=hash(a);
39 if(k)ans++;
40 }cout<<ans;
41 /*
42 10
43 9317 0 9171 9317 123 2234 9717 42342 80924 42342
44 */
45 }
字符双hash
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstring>
3 #include<iostream>
4 #include<algorithm>
5 #include<cmath>
6 #include<queue>
7 #include<cstdlib>
8 #define maxn 30005
9 #define ll long long
10 using namespace std;
11
12 struct node{
13 ll a,b;
14 bool operator==(node&x)const{
15 return a==x.a&&b==x.b;}
16 }e[maxn];
17
18 int cmp(node x,node y){
19 if(x.a==y.a )
20 return x.b>y.b; return x.a>y.a;
21 }
22
23 ll n,a,ans;
24 char s[1005];
25
26 ll hash1(ll pos){
27 ll _bit=2317,mod=12373127;
28 ll now=0;
29 for(ll i=1;s[i];i++){
30 now=(1ll*now*_bit+s[i]-0)%mod;
31 }return now;
32 }
33
34 ll hash2(ll pos){
35 ll _bit=539,mod=1927345;
36 ll now=0;
37 for(ll i=1;s[i];i++){
38 now=(1ll*now*_bit+s[i]-0)%mod;
39 }return now;
40 }
41
42 int main(){
43 scanf("%lld",&n);
44 for(ll i=1;i<=n;i++){
45 scanf("%s",s+1);
46 e[i]=(node){hash1(i),hash2(i)};
47 }
48 sort(e+1,e+n+1,cmp);
49 n=unique(e+1,e+n+1)-e-1;
50 cout<<n;
51 }
模板题:https://www.luogu.org/problemnew/show/P3370