上海交通大学ACM算法模板gai文档格式.docx

上传人:b****4 文档编号:16609967 上传时间:2022-11-24 格式:DOCX 页数:125 大小:255.87KB
下载 相关 举报
上海交通大学ACM算法模板gai文档格式.docx_第1页
第1页 / 共125页
上海交通大学ACM算法模板gai文档格式.docx_第2页
第2页 / 共125页
上海交通大学ACM算法模板gai文档格式.docx_第3页
第3页 / 共125页
上海交通大学ACM算法模板gai文档格式.docx_第4页
第4页 / 共125页
上海交通大学ACM算法模板gai文档格式.docx_第5页
第5页 / 共125页
点击查看更多>>
下载资源
资源描述

上海交通大学ACM算法模板gai文档格式.docx

《上海交通大学ACM算法模板gai文档格式.docx》由会员分享,可在线阅读,更多相关《上海交通大学ACM算法模板gai文档格式.docx(125页珍藏版)》请在冰豆网上搜索。

上海交通大学ACM算法模板gai文档格式.docx

2.字典树

3.后缀树

4.线段树

5.并查集

6.二叉堆

7.逆序数(归并排序)

8.树状DP

9.欧拉路

10.八数码

11.高斯消元法

12.字符串匹配(KMP算法)

13.全排列,全组合

14.二维线段树

15.稳定婚姻匹配

16.后缀数组

17.左偏树

18.标准RMQ-ST

19.度限制最小生成树

20.最优比率生成树(0/1分数规划)

21.最小花费置换

22.区间K大数

23.LCA-RMQ-ST

24.LCA–Tarjan

25.指数型母函数

26.指数型母函数(大数据)

27.单词前缀树(字典树+KMP)

28.FFT(大数乘法)

29.二分图网络最大流最小割

30.混合图欧拉回路

31.无源汇上下界网络流

32.二分图最小点权覆盖

33.带约束的轨道计数(Burnside引理)

34.三分法求函数波峰

35.单词计数,矩阵乘法

36.字符串和数值hash

37.滚动队列,前向星表示法

38.最小点基,最小权点基

第一章常用函数和STL

一.常用函数

#include 

<

stdio.h>

int 

getchar( 

void 

);

 

//读取一个字符, 

一般用来去掉无用字符

char 

*gets( 

*str 

//读取一行字符串

stdlib.h>

malloc( 

size_t 

size 

//动态内存分配, 

开辟大小为 

的空间

qsort( 

*buf, 

num, 

size, 

(*compare)(const 

*, 

const 

*) 

//快速排序

Sample:

compare_ints( 

void* 

a, 

) 

{int* 

arg1 

(int*) 

a;

int* 

arg2 

b;

if( 

*arg1 

*arg2 

return 

-1;

else 

== 

0;

1;

}

array[] 

-2, 

99, 

0, 

-743, 

2, 

3, 

};

array_size 

7;

array, 

array_size, 

sizeof(int), 

compare_ints 

math.h>

//求反正弦, 

arg∈[-1, 

1], 

返回值∈[-pi/2, 

+pi/2]

double 

asin( 

arg 

//求正弦, 

arg为弧度, 

弧度=角度*Pi/180.0, 

返回值∈[-1, 

1]

sin( 

//求e的arg次方

exp( 

//求num的对数, 

基数为e

log( 

num 

//求num的根

sqrt( 

//求base的exp次方

pow( 

base, 

exp 

string.h>

//初始化内存, 

常用来初始化数组

memset( 

buffer, 

ch, 

count 

the_array, 

sizeof(the_array) 

//printf是它的变形, 

常用来将数据格式化为字符串

sprintf( 

*buffer, 

*format, 

... 

sprintf(s, 

"

%d%d"

 

123, 

4567);

//s="

1234567"

//scanf是它的变形, 

常用来从字符串中提取数据

sscanf( 

result[100]="

24 

hello"

str[100];

num;

result, 

%d 

%s"

num,str 

//num=24;

str="

;

//字符串比较, 

返回值<

0代表str1<

str2, 

=0代表str1=str2, 

>

0代表str1>

str2

strcmp( 

*str1, 

*str2 

二.常用STL

[标准container概要]

vector<

T>

大小可变的向量,类似数组的用法,容易实现删除

list<

双向链表

queue<

队列,empty(),front(),pop(),push()

stack<

栈,empty(),top(),pop(),push()

priority_queue<

优先队列,empty(),top(),pop(),push()

set<

集合

map<

key,val>

关联数组,常用来作hash映射

[标准algorithm摘录]

for_each()对每一个元素都唤起(调用)一个函数

find()查找第一个能与引数匹配的元素

replace()用新的值替换元素,O(N)

copy()复制(拷贝)元素,O(N)

remove()移除元素

reverse()倒置元素

sort()排序,O(Nlog(N))

partial_sort()部分排序

binary_search()二分查找

merge()合并有序的序列,O(N)

[C++String摘录]

copy()从别的字符串拷贝

empty()判断字符串是否为空

erase()从字符串移除元素

find()查找元素

insert()插入元素

length()字符串长度

replace()替换元素

substr()取子字符串

swap()交换字符串

第二章重要公式与定理

1.FibonacciNumber

0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610…

Formula:

2.LucasNumber

1,3,4,7,11,18,29,47,76,123...

3.CatalanNumber

1,2,5,14,42,132,429,1430,4862,16796,58786,208012…

Application:

1)将n+2边形沿弦切割成n个三角形的不同切割数

n=2;

n=3;

2)n+1个数相乘,给每两个元素加上括号的不同方法数

(1(23)),((12)3)

(1(2(34))),(1((23)4)),((12)(34)),((1(23))4),(((12)3)4)

3)n个节点的不同形状的二叉树数(严《数据结构》P.155)

4)从n*n方格的左上角移动到右下角不升路径数

4.StirlingNumber(SecondKind)

S(n,m)表示含n个元素的集合划分为m个集合的情况数

或者是n个有标号的球放到m个无标号的盒子中,要求无一为空,其不同的方案数

SpecialCases:

5.BellNumber

n个元素集合所有的划分数

6.Stirling'

7.SumofReciprocalApproximation

EulerGamma=0.57721566490153286060651209;

8.YoungTableau

YoungTableau(杨式图表)是一个矩阵,它满足条件:

如果格子[i,j]没有元素,则[i+1,j]也一定没有元素

如果格子[i,j]有元素a[i,j],则[i+1,j]要么没有元素,要么a[i+1,j]>

a[i,j]

Y[n]代表n个数所组成的杨式图表的个数

9.整数划分

将整数n分成k份,且每份不能为空,任意两种分法不能相同

1)不考虑顺序

for(int 

p=1;

p<

=n 

p++)

i=p;

i<

i++)

j=k;

j>

=1 

j--)

dp[i][j] 

+= 

dp[i-p][j-1];

cout<

dp[n][k] 

endl;

2)考虑顺序

dp[i][j]=dp[i-k][j-1];

(k=1..i)

3)若分解出来的每个数均有一个上限m

dp[i][j]=dp[i-k][j-1];

(k=1..m)

10.错排公式

11.三角形内切圆半径公式

12.三角形外接圆半径公式

13.圆內接四边形面积公式

14.基础数论公式

1)模取幂

2)n的约数的个数

若n满足

则n的约数的个数为

第三章大数模板

typedefinthugeint;

//应不大于,以防乘法时溢出

constintBase=1000;

constintCapacity=1000;

structxnum

{intLen;

intData[Capacity];

xnum():

Len(0){}

xnum(constxnum&

V):

Len(V.Len){memcpy(Data,V.Data,Len*sizeof*Data);

xnum(intV):

Len(0){

for(;

V>

0;

V/=Base)Data[Len++]=V%Base;

xnum(charS[]);

xnum&

operator=(constxnum&

V){

Len=V.Len;

memcpy(Data,V.Data,Len*sizeof*Data);

return*this;

}

int&

operator[](intIndex){returnData[Index];

intoperator[](intIndex)const{returnData[Index];

voidprint(){

printf("

%d"

Len==0?

0:

Data[Len-1]);

for(inti=Len-2;

i>

=0;

i--)

for(intj=Base/10;

j/=10)printf("

Data[i]/j%10);

}};

xnum:

:

xnum(charS[])

{intI,J;

Data[Len=0]=0;

J=1;

for(I=strlen(S)-1;

I>

I--){

Data[Len]+=(S[I]-'

0'

)*J;

J*=10;

if(J>

=Base)J=1,Data[++Len]=0;

if(Data[Len]>

0)Len++;

intcompare(constxnum&

A,constxnum&

B)

{intI;

if(A.Len!

=B.Len)returnA.Len>

B.Len?

1:

-1;

for(I=A.Len-1;

I>

=0&

&

A[I]==B[I];

I--);

if(I<

0)return0;

returnA[I]>

B[I]?

xnumoperator+(constxnum&

{xnumR;

intI;

intCarry=0;

for(I=0;

I<

A.Len||I<

B.Len||Carry>

I++)

{if(I<

A.Len)Carry+=A[I];

B.Len)Carry+=B[I];

R[I]=Carry%Base;

Carry/=Base;

}R.Len=I;

returnR;

xnumoperator-(constxnum&

R.Len=A.Len;

intI;

R.Len;

{R[I]=A[I]-Carry;

B.Len)R[I]-=B[I];

if(R[I]<

0)Carry=1,R[I]+=Base;

elseCarry=0;

while(R.Len>

0&

R[R.Len-1]==0)R.Len--;

xnumoperator*(constxnum&

A,constintB)

if(B==0)return0;

xnumR;

hugeintCarry=0;

A.Len||Carry>

A.Len)Carry+=hugeint(A[I])*B;

R[I]=Carry%Base;

Carry/=Base;

if(B.Len==0)return0;

A.Len;

{hugeintCarry=0;

for(intJ=0;

J<

J++)

{if(J<

B.Len)Carry+=hugeint(A[I])*B[J];

if(I+J<

R.Len)Carry+=R[I+J];

if(I+J>

=R.Len)R[R.Len++]=Carry%Base;

elseR[I+J]=Carry%Base;

}}returnR;

xnumoperator/(constxnum&

hugeintC=0;

=0;

I--){C=C*Base+A[I];

R[I]=C/B;

C%=B;

R.Len=A.Len;

returnR;

}//div

xnumR,Carry=0;

intLeft,Right,Mid;

I--)

{Carry=Carry*Base+A[I];

Left=0;

Right=Base-1;

while(Left<

Right)

{Mid=(Left+Right+1)/2;

if(compare(B*Mid,Carry)<

=0)Left=Mid;

elseRight=Mid-1;

R[I]=Left;

Carry=Carry-B*Left;

}R.Len=A.Len;

}//mod

xnumoperator%(constxnum&

{intI;

{Carry=Carry*Base+A[I];

{Mid=(Left+Right+1)/2;

returnCarry;

istream&

operator>

(istream&

In,xnum&

V)

{charCh;

for(V=0;

In>

Ch;

{V=V*10+(Ch-'

if(cin.peek()<

='

'

)break;

returnIn;

ostream&

operator<

(ostream&

Out,constxnum&

Out<

(V.Len==0?

0:

V[V.Len-1]);

for(I=V.Len-2;

I--)

for(intJ=Base/10;

J>

J/=10)Out<

V[I]/J%10;

returnOut;

xnumgcd(xnuma,xnumb)

{if(compare(b,0)==0)returna;

elsereturngcd(b,a%b);

intdiv(char*A,intB)

intC=0;

intAlen=strlen(A);

Alen;

I++){C=C*Base+A[I]-'

returnC;

xnumC(intn,intm)

{inti;

xnumsum=1;

for(i=n;

i>

=n-m+1;

i--)sum=sum*i;

for(i=1;

i<

=m;

i++)sum=sum/i;

returnsum;

#defineMAXN9999

#defineDLEN4

classBigNum{

private:

inta[1000];

//可以控制大数的位数intlen;

//大数长度

public:

BigNum(){len=1;

memset(a,0,sizeof(a));

BigNum(constint);

BigNum(constchar*);

BigNum(constBigNum&

BigNum&

operator=(constBigNum&

BigNumoperator+(constBigNum&

)const;

BigNumoperator-(constBigNum&

BigNumoperator*(constBigNum&

BigNumoperator/(constint&

BigNumoperator^(constint&

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 电子电路

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1