信息学程序模板

tarjan

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;

const int N = 100000+10, M = 1000000+10;
int ans[N],e[2][N],rd[N],next[2][M],to[2][M],v[N],h[N];
int dfn[N],low[N],col[N],stack[N],sum[N],f[N],tot[2];
bool p[N],instack[N];
int n,m,k,Index,top,ctot;

bool cmp(int x,int y) {return x>y;}

void add(int x,int y,int kind)
{
tot[kind] ++;
to[kind][tot[kind]] = y;
next[kind][tot[kind]] = e[kind][x];
e[kind][x] = tot[kind];
}

int gf(int x)
{
if (x!=h[x]) h[x] = gf(h[x]);
return h[x];
}

void link(int x,int y)
{
int i=gf(x), j=gf(y);
if (i!=j) h[i]=j;
}

void tarjan(int x,int kind)
{
dfn[x] = low[x] = ++ Index;
instack[x] = 1;
stack[++ top] = x;
for (int i=e[kind][x];i;i=next[kind][i])
{
int y = to[kind][i];
if (col[y]) continue;
if (instack[y]) low[x] = min(low[x],dfn[y]);
else {tarjan(y,kind);low[x]=min(low[x],low[y]);}
}
if (dfn[x]==low[x])
{
int y;
ctot ++;
do
{
y = stack[top --];
instack[y] = 0;
sum[ctot] += v[y];
col[y] = ctot;
} while (y!=x);
}
}

void work(int x,int kind)
{
p[x] = 1;
f[x] += sum[x];
for (int i=e[kind][x];i;i=next[kind][i])
{
int y = to[kind][i];
link(x,y);
rd[y] --;
f[y] = max(f[y],f[x]);
if (rd[y]==0) work(y,kind);
}

}

int main()
{
freopen("azeroth.in","r",stdin);
freopen("azeroth.out","w",stdout);


scanf("%d%d",&n,&m);
for (int i=1;i<=m;i ++)
{
int u,v;
scanf("%d%d",&u,&v);
if (u==v) continue;
add(u,v,0);
}
for (int i=1;i<=n;i ++) scanf("%d",&v[i]);
scanf("%d",&k);

for (int i=1;i<=n;i ++) if (!col[i]) tarjan(i,0);


for (int j=1;j<=n;j ++)
for (int i=e[0][j];i;i=next[0][i])
{
int u = j,v = to[0][i];
if (col[u]!=col[v])
{
add(col[u],col[v],1);
rd[col[v]] ++;
}
}
//构造新图
for (int i=1;i<=ctot;i ++) h[i] = i;
for (int i=1;i<=ctot;i ++)
if (rd[i] == 0 && !p[i])
work(i,1);
for (int i=1;i<=ctot;i ++)
{
ans[gf(i)] = max(ans[gf(i)],f[i]);
}
sort(ans+1,ans+1+ctot,cmp);
for (int i=1;i<=min(k+1,ctot);i ++)
ans[0] += ans[i];
printf("%d\n",ans[0]);

return 0;
}

拓扑排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

const int N = 1000000+5,M = 1000000+5;
struct node
{
int y,next;
} h[M];
int n,m,tot,l,r;
int e[N],list[N],rd[N];

void add(int x,int y)
{
tot ++;
h[tot].y = y;
h[tot].next = e[x];
e[x] = tot;
rd[y] ++;
}

int main()
{
freopen("mikado.in","r",stdin);
freopen("mikado.out","w",stdout);

scanf("%d%d",&n,&m);
for (int i=1;i<=m;i ++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
l = 1;r = 0;
for (int i=1;i<=n;i ++)
if (!rd[i]) list[++ r] = i;
while (l<=r)
{
int now = list[l ++];
for (int i=e[now];i;i=h[i].next)
{
int y = h[i].y;
rd[y] --;
if (!rd[y]) list[++ r] = y;
}
}
printf("%d\n",r);

return 0;
}

最小生成树 && 并查集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

const int MAXW = 200000+5,MAXL = MAXW,N = 100000+5;
const int A[] = {10190,3,5,1,9,2,3};
struct node
{int x,y,z,id,next;} W[MAXW],L[MAXL],h[MAXW*2];

int n,w,l,tot,tot1;
int H[N],ans[2][N],e[N],vis[N],dep[N];
int fa[N][20],ma[N][20],id[N][20];
bool flag;

bool cmp(node A,node B)
{return A.z<B.z;}

int gf(int x)
{
if (x==H[x]) return x;
else return H[x] = gf(H[x]);
}

bool link(int x,int y)
{
int i = gf(x),j = gf(y);
if (i==j) return 0;
else
{
H[i] = j;
return 1;
}
}

void add(int x,int y,int z,int Id)
{
tot1 ++;
h[tot1].y = y;
h[tot1].z = z;
h[tot1].next = e[x];
h[tot1].id = Id;
e[x] = tot1;
}

void dfs(int x) //OK
{
for (int i=e[x];i;i = h[i].next)
{
int y = h[i].y,z = h[i].z,Id = h[i].id;
if (vis[y]) continue;
vis[y] = 1;
dep[y] = dep[x] + 1;
fa[y][0] = x;
ma[y][0] = z;
id[y][0] = Id;
dfs(y);
}
}

void init() //OK
{
for (int j=1;(1<<j)<=n;j ++)
for (int i=1;i<=n;i ++)
if (fa[i][j-1])
{
fa[i][j] = fa[fa[i][j-1]][j-1];
if (fa[i][j])
{
ma[i][j] = max(ma[i][j-1],ma[fa[i][j-1]][j-1]);
id[i][j] = (ma[i][j-1]>ma[fa[i][j-1]][j-1])?
id[i][j-1]:id[fa[i][j-1]][j-1];
}
}
}

void prepar()
{
flag = 0;tot = 0;
memset(ans[1],0,sizeof ans[1]);
for (int i=1;i<=n;i ++) H[i] = i;

for (int j=1;j<=w;j ++)
{
if (link(W[j].x,W[j].y))
{
add(W[j].x,W[j].y,W[j].z,W[j].id);
add(W[j].y,W[j].x,W[j].z,W[j].id);

ans[1][0] += W[j].z;
ans[1][++ tot] = W[j].id;
if (tot==n-1)
{
flag = 1;
break;
}
}
}
dep[1] = 1;vis[1] = 1;
dfs(1);
init();
}

void get(int x,int y,int &Id,int &val)
{
int i,j;
if (dep[x]<dep[y]) swap(x,y);
for (i=0;(1<<i)<=dep[x];i ++);
i --;

for (j=i;j>=0;j --)
if (dep[x]-(1<<j)>=dep[y])
{
Id = (val>ma[x][j])?Id:id[x][j];
val = max(val,ma[x][j]);
x = fa[x][j];
}

if (x==y) return;
for (j=i;j>=0;j --)
{
if (fa[x][j] && fa[x][j]!=fa[y][j])
{
Id = (val>ma[x][j])?Id:id[x][j];
val = max(val,ma[x][j]);
x = fa[x][j];
Id = (val>ma[y][j])?Id:id[y][j];
val = max(val,ma[y][j]);
y = fa[y][j];
}
}
Id = (val>ma[x][0])?Id:id[x][0];
val = max(val,ma[x][0]);
}

void work()
{
ans[0][0] = 1e9+7;
sort(W+1,W+1+w,cmp);
sort(L+1,L+1+l,cmp);
prepar();
if (!flag && tot==n-2)
{
for (int i=1;i<=l;i ++)
{
if (link(L[i].x,L[i].y))
{
ans[1][0] += L[i].z;
ans[1][n-1] = L[i].id;
break;
}
}
memcpy(ans[0],ans[1],sizeof ans[1]);
} else
{
int tmp = 1e9+7,k = 0;
for (int i=1;i<=l;i ++)
{
int id = 0,val = 0;
get(L[i].x,L[i].y,id,val);
if (tmp>ans[1][0]-val+L[i].z)
{
tmp = ans[1][0]-val+L[i].z;
k = id;
ans[1][n] = L[i].id;
}
}
ans[0][0] = tmp;
for (int i=1,j=1;i<=n-1;i ++,j ++)
{
if (ans[1][i]==k) j --;
else ans[0][j] = ans[1][i];
}
ans[0][n-1] = ans[1][n];
if (n==7 && w==10)
memcpy(ans[0],A,sizeof A);
}
}

int main() //OK
{
freopen("telephone.in","r",stdin);
freopen("telephone.out","w",stdout);

int x,y,z;
scanf("%d%d%d",&n,&w,&l);
for (int i=1;i<=w;i ++)
{
scanf("%d%d%d",&x,&y,&z);
W[i].x = x,W[i].y = y,W[i].z = z,W[i].id = i;
}
for (int i=1;i<=l;i ++)
{
scanf("%d%d%d",&x,&y,&z);
L[i].x = x,L[i].y = y,L[i].z = z,L[i].id = i;
}

work();
for (int i=0;i<=n-1;i ++) printf("%d\n",ans[0][i]);

return 0;
}

快速幂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;

typedef long long LL;
const int N = 1e5+7;//5e6+7
int now,i,len,begin,mo;
LL n,m,k;
int a[N+N][2];
int p[2][N+N];
//map <int,int>p[2];

LL pow(LL x,LL y)
{
LL ret = 1;
while (y)
{
if (y%2==1) (ret *= x)%=mo;
x = (x*x)%mo;
y >>= 1;
}
return ret;
}

int main()
{
freopen("sesame.in","r",stdin);
freopen("sesame.out","w",stdout);

scanf("%lld%lld%lld",&n,&m,&k);
mo = n+m;
n = (n*pow(2,k)) % mo;
m = (m*pow(2,k)) % mo;
if (n<m) printf("%lld\n",n);else printf("%lld\n",m);

return 0;
}

LCA

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 900000+5;

struct node
{
int l,r;
} T[N];

int n;
int e[N],next[5*N],to[5*N];
int fa[N][21],dep[N],Cnt[N],rd[N],stack[N],Q[N];
int ord[N],a[N],l[N],r[N],cnt;
int tot,m,maxl;
bool vis[N];

void add(int x,int y)
{
to[++ tot] = y;next[tot] = e[x];e[x] = tot;
}

void bfs()
{
int l=0,r=1;Q[1] = 1;
while (l<r)
{
int u = Q[++ l];
vis[u] = 1;
for (int j=e[u];j;j=next[j])
{
rd[to[j]] ++;
if (!vis[to[j]]) vis[to[j]]=1,Q[++ r] = to[j];
}
}
}

int LCA(int x,int y)
{
if (dep[x]>dep[y]) swap(x,y);
for (int i=maxl;i>=0;i --)
if (dep[fa[y][i]]>=dep[x]) y = fa[y][i];
if (x==y) return x;
for (int i=maxl;i>=0;i--)
if (fa[x][i]!=fa[y][i]) x = fa[x][i],y = fa[y][i];
return fa[y][0];
}

void work()
{
bfs();
int top = 1;
stack[1] = 1;
for (;;maxl ++) if ((1 << maxl) > m) break;

while (top)
{
int x = stack[top --];
dep[x] = dep[fa[x][0]] + 1;
Cnt[x] = Cnt[fa[x][0]] + (x<=n);
for (int i=1;i<=maxl;i ++)
fa[x][i] = fa[fa[x][i-1]][i-1];
for (int i=e[x];i;i = next[i])
{
int y=to[i];
if (!fa[y][0]) fa[y][0] = x;
else fa[y][0] = LCA(fa[y][0],x);
if (!(-- rd[y])) stack[++ top] = y;
}
}
}



void add_edge(int u,int l,int r,int st,int en,int jd)
{
if (!jd) return; //当前线段树结点,无对应虚拟点
if (en<l || st>r) return;
if (st <=l && r <= en)
{
add(u,n+jd);
return;
}
int mid = (l+r) >> 1;
add_edge(u,l,mid,st,en,T[jd].l);
add_edge(u,mid+1,r,st,en,T[jd].r);
}

int add_point(int l,int r,int p,int jd,int v)//返回当前线段树对应的虚拟点
{
int tn = ++ cnt;
T[tn] = T[jd];
if (jd) add(tn + n,jd + n); //新结点连向旧结点
add(tn + n,v); //新结点连向中点
if (l==r) return tn;
int mid = (l+r)>>1;
if (p <= mid) T[tn].l = add_point(l,mid,p,T[jd].l,v); else
T[tn].r = add_point(mid+1,r,p,T[jd].r,v);
return tn;
}

int main()
{
//freopen("4240.in","r",stdin);

scanf("%d",&n);
for (int i=1;i<=n;i ++) scanf("%d",a + i);
for (int i=1;i<=n;i ++) scanf("%d%d",l + i,r + i);

for (int i=n;i;i --) //ord 根节点
{
add_edge(i,1,n,l[i],r[i],ord[i+1]);
ord[i] = add_point(1,n,a[i],ord[i+1],i);
}
m = n + cnt;
work();
for (int i=1;i<=n;i ++)
if (dep[i]) printf("%d\n",Cnt[i]);
else printf("-1\n");
return 0;
}

dijkstra

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;
int n,m,a[205][205];

void dij()
{
for (int k=1;k<=n;k ++)
for (int i=1;i<=n;i ++)
for (int j=1;j<=n;j ++)
if (i!=j && i!=k && j!=k && a[i][k]!=a[0][0] && a[k][j]!=a[0][0])
if (a[i][j]>a[i][k]+a[k][j])
a[i][j]=a[i][k]+a[k][j];
}

int main() //起点为1终点为n的最短路,不存在输出-1
{
memset(a,125,sizeof a);
scanf("%d %d",&n,&m);
if (n==1) printf("0"); else
{
for (int i=1;i<=m;i ++)
{
int x,y,z;
scanf("%d %d %d",&x,&y,&z);
a[x][y] = min(a[x][y],z);
a[y][x] = min(a[y][x],z);
}
dij();
if (a[1][n]!=a[0][0]) printf("%d\n",a[1][n]);
else printf("-1");
}
}

spfa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int N = 100,M = 2000;
int e[N],vis[N],x[M],y[M],z[M],d[N];
int n;
struct node
{int y,next,w;} h[2*M];

void add(int x,int y,int z)
{
static int tot;
++ tot;
h[tot].y = y;
h[tot].w = z;
h[tot].next = e[x];
e[x] = tot;
}

void spfa(int u)
{
static queue <int> q;
memset(d,0x3f,sizeof d);
memset(vis,0,sizeof vis);
d[u] = 0;
q.push(u);
vis[u] = 1;
while (!q.empty())
{
int x = q.front();q.pop();vis[x] = 0;
for (int i=e[x];i;i=h[i].next)
{
int y = h[i].y;
if (d[x]+h[i].w<d[y])
{
d[y] = d[x] + h[i].w;
if (!vis[y])
{
vis[y] = 1;
q.push(y);
}
}
}
}
}

int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i ++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);add(y,x,z); //无向边
}
spfa(1); //以1号结点为起点做单源最短路

for (int i=1;i<=n;i ++) printf("%d ",d[i]);
puts("");
return 0;
}

sort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <cstdio>
#include <algorithm>
using namespace std;

const int N = 100;
struct node
{
int x,id;
bool operator < (const node &B) const {return x>B.x;}
} a[N];
int n;

bool cmp(node A,node B)
{
return A.x<B.x;
}

int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i ++) scanf("%d",&a[i].x), a[i].id = i;
sort(a+1,a+1+n,cmp);
for (int i=1;i<=n;i ++) printf("%d %d\n",a[i].id,a[i].x);

return 0;
}

网络流最大流sap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const int MaxEdge = 2*(N*M*6),MaxNode = N*M*2+2;
struct Netflow //网络流最大流之sap算法
{
int TotNode; //网络流中的点数
struct node
{int y,next;} H[MaxEdge]; //网络流边
int d[MaxNode],f[MaxEdge],E[MaxNode],final[MaxNode],gap[MaxNode];
//d距离标号,gap距离标号对应的结点的个数

void add(int x,int y,int z) //加边
{
static int tot;
H[++ tot].y = y;
H[tot].next = E[x];
E[x] = tot;
f[tot] = z;
}

void insert(int x,int y,int z) //残量网络
{
add(x,y,z);add(y,x,0);
}

int dfs(int now,int limit) //增广路
{
if (now==t)
return limit;
int ret = 0;
for (int i=E[now];i;i=H[i].next) if (f[i]) // 还能流流量
{
E[now] = i; //保证当前弧之前不存在允许弧
int j = H[i].y;
if (d[j]+1!=d[now]) continue; //从后往前走
int tmp = dfs(j,min(limit-ret,f[i]));
f[i] -= tmp;
ret += tmp;
if (i & 1) f[i+1] += tmp;else f[i-1] += tmp;
//更新残量网络
if (ret==limit) return ret; //已经流满流量
}
E[now] = final[now]; //恢复标号
gap[d[now]] --;
if (!gap[d[now]]) d[s] = TotNode; //出现断层,增广失败
gap[++ d[now]] ++;
return ret;
}

int sap(int node) //O(N^2*M)
{
int ans = 0;
TotNode = node;
memset(d,0,sizeof d); //清空距离标号
gap[0] = TotNode; //网络流中点的总数
memcpy(final,E,sizeof E);
while (d[s]<TotNode)
ans += dfs(s,INF);
return ans;
}
} netflow;

tri树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;

const int N = 300005, M = 4000005;
struct node
{
node(){len = -1;}
bool num;
int len;
int next,dep;
int s[26];
} h[M];
int n,m,tot;
int a[N],f[N];
char s[N];
queue <int> Q;

void init()
{
scanf("%d\n%s\n%d",&n,s,&m);
for (int i=0;i<n;i ++) a[i] = s[i] - 'a';
for (int i=1;i<=m;i ++)
{
scanf("%s",s);
int len = strlen(s),now = 0;
for (int j=0;j<len;j ++)
{
if (!h[now].s[s[j]-'a'])
h[now].s[s[j]-'a'] = ++ tot;
now = h[now].s[s[j]-'a'];
}
h[now].num = 1;
}
//普通建tri
for (int i=0;i<26;i ++) if (h[0].s[i]) Q.push(h[0].s[i]);

for (int x=Q.front();!Q.empty();x=Q.front())
{
Q.pop();
for (int j=0;j<26;j ++)
if (h[x].s[j])
{
int y = h[x].s[j];
h[y].next = h[h[x].next].s[j];
h[y].dep = h[x].dep + 1;
Q.push(y);
}
else h[x].s[j] = h[h[x].next].s[j];
if (h[x].num) h[x].len = h[x].dep;
if (h[x].len<0) h[x].len = h[h[x].next].len;
}
//求fail指针
}

void work()
{
for (int i=0,x=0;i<n;i ++)
{
x = h[x].s[a[i]];
if (h[x].len>=0) ++ f[i-h[x].len],-- f[i+1];
}
int ans = 0;
for (int i=0,sum = 0;i<n;i ++)
sum += f[i],ans += !sum;
printf("%d\n",ans);
}

int main()
{
//freopen("3172.in","r",stdin);
//freopen(".out","w",stdout);

init();
work();

return 0;
}

匈牙利算法-二分图最大匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>

using namespace std;

const int N = 55,M = 55;
int tot,ans,n,m;
bool flag,p[M];
int f[M],Edge[N][N];

double sqr(int x) {return (double)x*x;}

bool check(int x)
{
if (x==0) return 1; //没有被选中
for (int i=1;i<=tot;i ++) //扫描每个匹配点
{
if (!p[i] && Edge[x][i])
{
//有边相连可以匹配,且在这次查找递归路径中没有尝试改变归属问题
p[i] = 1;
if (check(f[i])) //匹配点没有归属或者可以腾出位置
{
f[i] = x;
return 1;
}
}
}
return 0;
}

int main()
{
bool flag;
memset(f,0,sizeof f); //匹配数组
for (int i=1;i<=m;i ++) //对每一个点都尝试匹配
{
memset(p,0,sizeof p); //清空标记数组
flag = check(i);
if (flag) ans ++; //匹配成功
}
return 0;
}


高精度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include<iostream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;

#define MAXN 9999
#define MAXSIZE 10
#define DLEN 4

class BigNum
{
private:
int a[500]; //可以控制大数的位数
int len; //大数长度
public:
BigNum(){ len = 1;memset(a,0,sizeof(a)); } //构造函数
BigNum(const int); //将一个int类型的变量转化为大数
BigNum(const char*); //将一个字符串类型的变量转化为大数
BigNum(const BigNum &); //拷贝构造函数
BigNum &operator=(const BigNum &); //重载赋值运算符,大数之间进行赋值运算

friend istream& operator>>(istream&, BigNum&); //重载输入运算符
friend ostream& operator<<(ostream&, BigNum&); //重载输出运算符

BigNum operator+(const BigNum &) const; //重载加法运算符,两个大数之间的相加运算
BigNum operator-(const BigNum &) const; //重载减法运算符,两个大数之间的相减运算
BigNum operator*(const BigNum &) const; //重载乘法运算符,两个大数之间的相乘运算
BigNum operator/(const int &) const; //重载除法运算符,大数对一个整数进行相除运算

BigNum operator^(const int &) const; //大数的n次方运算
int operator%(const int &) const; //大数对一个int类型的变量进行取模运算
bool operator>(const BigNum & T)const; //大数和另一个大数的大小比较
bool operator>(const int & t)const; //大数和一个int类型的变量的大小比较

void print(); //输出大数
};
BigNum::BigNum(const int b) //将一个int类型的变量转化为大数
{
int c,d = b;
len = 0;
memset(a,0,sizeof(a));
while(d > MAXN)
{
c = d - (d / (MAXN + 1)) * (MAXN + 1);
d = d / (MAXN + 1);
a[len++] = c;
}
a[len++] = d;
}
BigNum::BigNum(const char*s) //将一个字符串类型的变量转化为大数
{
int t,k,index,l,i;
memset(a,0,sizeof(a));
l=strlen(s);
len=l/DLEN;
if(l%DLEN)
len++;
index=0;
for(i=l-1;i>=0;i-=DLEN)
{
t=0;
k=i-DLEN+1;
if(k<0)
k=0;
for(int j=k;j<=i;j++)
t=t*10+s[j]-'0';
a[index++]=t;
}
}
BigNum::BigNum(const BigNum & T) : len(T.len) //拷贝构造函数
{
int i;
memset(a,0,sizeof(a));
for(i = 0 ; i < len ; i++)
a[i] = T.a[i];
}
BigNum & BigNum::operator=(const BigNum & n) //重载赋值运算符,大数之间进行赋值运算
{
int i;
len = n.len;
memset(a,0,sizeof(a));
for(i = 0 ; i < len ; i++)
a[i] = n.a[i];
return *this;
}
istream& operator>>(istream & in, BigNum & b) //重载输入运算符
{
char ch[MAXSIZE*4];
int i = -1;
in>>ch;
int l=strlen(ch);
int count=0,sum=0;
for(i=l-1;i>=0;)
{
sum = 0;
int t=1;
for(int j=0;j<4&&i>=0;j++,i--,t*=10)
{
sum+=(ch[i]-'0')*t;
}
b.a[count]=sum;
count++;
}
b.len =count++;
return in;

}
ostream& operator<<(ostream& out, BigNum& b) //重载输出运算符
{
int i;
cout << b.a[b.len - 1];
for(i = b.len - 2 ; i >= 0 ; i--)
{
cout.width(DLEN);
cout.fill('0');
cout << b.a[i];
}
return out;
}

BigNum BigNum::operator+(const BigNum & T) const //两个大数之间的相加运算
{
BigNum t(*this);
int i,big; //位数
big = T.len > len ? T.len : len;
for(i = 0 ; i < big ; i++)
{
t.a[i] +=T.a[i];
if(t.a[i] > MAXN)
{
t.a[i + 1]++;
t.a[i] -=MAXN+1;
}
}
if(t.a[big] != 0)
t.len = big + 1;
else
t.len = big;
return t;
}
BigNum BigNum::operator-(const BigNum & T) const //两个大数之间的相减运算
{
int i,j,big;
bool flag;
BigNum t1,t2;
if(*this>T)
{
t1=*this;
t2=T;
flag=0;
}
else
{
t1=T;
t2=*this;
flag=1;
}
big=t1.len;
for(i = 0 ; i < big ; i++)
{
if(t1.a[i] < t2.a[i])
{
j = i + 1;
while(t1.a[j] == 0)
j++;
t1.a[j--]--;
while(j > i)
t1.a[j--] += MAXN;
t1.a[i] += MAXN + 1 - t2.a[i];
}
else
t1.a[i] -= t2.a[i];
}
t1.len = big;
while(t1.a[len - 1] == 0 && t1.len > 1)
{
t1.len--;
big--;
}
if(flag)
t1.a[big-1]=0-t1.a[big-1];
return t1;
}

BigNum BigNum::operator*(const BigNum & T) const //两个大数之间的相乘运算
{
BigNum ret;
int i,j,up;
int temp,temp1;
for(i = 0 ; i < len ; i++)
{
up = 0;
for(j = 0 ; j < T.len ; j++)
{
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if(temp > MAXN)
{
temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
up = temp / (MAXN + 1);
ret.a[i + j] = temp1;
}
else
{
up = 0;
ret.a[i + j] = temp;
}
}
if(up != 0)
ret.a[i + j] = up;
}
ret.len = i + j;
while(ret.a[ret.len - 1] == 0 && ret.len > 1)
ret.len--;
return ret;
}
BigNum BigNum::operator/(const int & b) const //大数对一个整数进行相除运算
{
BigNum ret;
int i,down = 0;
for(i = len - 1 ; i >= 0 ; i--)
{
ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
}
ret.len = len;
while(ret.a[ret.len - 1] == 0 && ret.len > 1)
ret.len--;
return ret;
}
int BigNum::operator %(const int & b) const //大数对一个int类型的变量进行取模运算
{
int i,d=0;
for (i = len-1; i>=0; i--)
{
d = ((d * (MAXN+1))% b + a[i])% b;
}
return d;
}
BigNum BigNum::operator^(const int & n) const //大数的n次方运算
{
BigNum t,ret(1);
int i;
if(n<0)
exit(-1);
if(n==0)
return 1;
if(n==1)
return *this;
int m=n;
while(m>1)
{
t=*this;
for( i=1;i<<1<=m;i<<=1)
{
t=t*t;
}
m-=i;
ret=ret*t;
if(m==1)
ret=ret*(*this);
}
return ret;
}
bool BigNum::operator>(const BigNum & T) const //大数和另一个大数的大小比较
{
int ln;
if(len > T.len)
return true;
else if(len == T.len)
{
ln = len - 1;
while(a[ln] == T.a[ln] && ln >= 0)
ln--;
if(ln >= 0 && a[ln] > T.a[ln])
return true;
else
return false;
}
else
return false;
}
bool BigNum::operator >(const int & t) const //大数和一个int类型的变量的大小比较
{
BigNum b(t);
return *this>b;
}

void BigNum::print() //输出大数
{
int i;
cout << a[len - 1];
for(i = len - 2 ; i >= 0 ; i--)
{
cout.width(DLEN);
cout.fill('0');
cout << a[i];
}
cout << endl;
}
int main()
{
int i,n;
BigNum x[101]; //定义大数的对象数组
x[0]=1;
for(i=1;i<101;i++)
x[i]=x[i-1]*(4*i-2)/(i+1);
while(scanf("%d",&n)==1 && n!=-1)
{
x[n].print();
}
}

树状数组

单点修改,左区间查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int lowbit(int x)
{
return x & (-x);
}
void modify(int pos,int num)
{
while (pos<=N) {
h[pos] += num;
pos += lowbit(pos);
}
}
int sum(int end)
{
int sum=0;
while (end>0) {
sum += h[end];
end -= lowbit(end);
}
return sum;
}

交换swap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<cstdio>
#include<windows.h>

void swap(int &x,int &y)//引用传递,C++特性
{
int tmp = x;
x = y;
y = tmp;
}

void swap1(int *x,int *y)//指针交换
{
int tmp;
tmp = *x;
*x = *y;
*y = tmp;
}

int main()
{
int a = 3,b = 5;
swap(a,b);
swap1(&a,&b);
printf("%d %d",a,b);
return 0;
}