noip基本算法网络版.docx
- 文档编号:20141663
- 上传时间:2023-04-25
- 格式:DOCX
- 页数:24
- 大小:17.96KB
noip基本算法网络版.docx
《noip基本算法网络版.docx》由会员分享,可在线阅读,更多相关《noip基本算法网络版.docx(24页珍藏版)》请在冰豆网上搜索。
noip基本算法网络版
生成序列
01序列
Procedurepoducer1(n:
integer);
VarI,j:
integer;
B:
array[0..100]ofinteger;
Begin
B[0]:
=0;
Whileb[0]=0do
Begin
I:
=n;
Whileb[i]=1dodec(i);
B[i]:
=1;
Forj:
=I+1tondo
B[j]:
=0;
End;
End;
全排列
Procedureproducer2(n:
integer);
Varx,I,j,p,min:
integer;
B:
array[0..100]ofinteger;
Begin
Fori:
=1tondo
B[i]:
=I;
B[0]:
=0;
Whileb[0]=0do
Begin
I:
=n;
Whileb[i]
Ifi=0thenbreak;
Min:
=n+1;p:
=0;
Forj:
=itondo
If(min>b[j])and(b[j]>b[i-1])then
Begin
P:
=j;min:
=b[j];
End;
X:
=b[p];b[p]:
=b[i-1];b[i-1]:
=x;
Forj:
=Ito(n+i)div2do
Begin
X:
=b[j];b[j]:
=b[n+i-j];b[n+i-j]:
=x;
End;
End;
End;
组合
Procedureproducer3(n,m:
integer);
VarI,j:
integer;
B:
array[0..100]ofinteger;
Begin
Fori:
=1tomdo
B[i]:
=I;
B[0]:
=0;
Whileb[0]=0do
Begin
I:
=m;
Whileb[i]=n+i-mdodec(i);
Ifi=0thenbreak;
Inc(b[i]);
Forj:
=I+1tomdo
B[j]:
=b[j-1]+1;
End;
End;
数论
素数的判断
朴素算法
Functionisprime(n:
longint):
boolean;
Vari:
longint;
Begin
Isprime:
=true;
Fori:
=2totrunc(sqrt(n))do
Ifnmodi=0then
Begin
Isprime:
=false;
Exit;
End;
Ifi=1thenisprime:
=false;
End;
Miller_Rabbin
Functionmiller_rabbin(p:
longint):
boolean;;
VarI,x:
longint;
Begin
Miller_rabbin:
=true;
Randomize;
Fori:
=1to30do
Begin
X:
=trunc(random*100+1);
Ifmodular(x,p-1,p)<>1then//**a^(p-1)modp
Begin
Miller_Rabbin:
=false;
Exit;
End;
End;
Ifp=1thenmiller_rabbin:
=false;
End;
分治求a^bmodc
Functionmodular(a,b,c:
longint):
longint;
VarI,j,len:
longint;
Bin:
array[1..50]ofinteger;
Begin
Ifb<=1then
Begin
Ifb=1thenmodular:
=amodc;
Ifb=0thenmodular:
=1;
Exit;
End;
Len:
=0;
J:
=b;
Whilej<>0do
Begin
Inc(i);
Bin[i]:
=jand1;
Bin[i]:
=jshr1;
End;
J:
=a;
Fori:
=len-1downto1do
Begin
J:
=jmodc*jmodc;
Ifbin[i]=1thenj:
=amodc*jmodc;
End;
Modular:
=j;
End;
欧几里德辗转相除法
Functiongcd(va,vb:
longint):
longint;
Varr:
longint;
Begin
Whilevamodvb<>0do
Begin
R:
=vamodvb;
Va:
=vb;vb:
=r;
End;
Gcd:
=vb;
End;
进制转换
Functionchange(a:
string;dx,dy:
longint):
string;
Vartemp:
string;
Len,I,j,x,t:
longint;
Begin
T:
=0;temp:
=’’;
Len:
=length(a);j:
=1;
Fori:
=lendownto1do
Begin
If(a[i]>=’A’)and(a[i]<=’F’)thenx:
=ord(a[i])-55elsex:
=ord(a[i])-48;
T:
=t+x*j;
T:
=t*dx;
End;
Whilet<>0do
Begin
X:
=tmoddy;
If(x>=10)and(x<=15)thentemp:
=temp+chr(x+55)elsetemp:
=temp+chr(x+48);
T:
=tdivdy;
End;
Iftemp=’’thenchange:
=’0’elsechange:
=temp;
End;
高精度计算
Procedureplus_part;
Begin
Ifla>lbthenlc:
=laelselc:
=lb;
Fori:
=1tolcdo
Begin
C[i]:
=a[i]+b[i];
C[i+1]:
=c[i+1]+c[i]div1000;
C[i]:
=c[i]mod1000;
End;
End;
Procedureminus_part;
Begin
Ifla>lbthenlc:
=laelselc:
=lb;
Fori:
=1tolcdo
Begin
C[i]:
=a[i]-b[i];
Ifc[i]<0then
Begin
C[i+1]:
=c[i+1]-1;
C[i]:
=c[i]+1000;
End;
End;
End;
Proceduremult_part;
Begin
Fori:
=1tolado
Forj:
=1tolbdo
Begin
C[la+lb-1]:
=c[la+lb-1]+a[i]*b[j];
C[la+lb]:
=c[la+lb]+c[la+lb-1]div1000;
C[la+lb-1]:
=c[la+lb-1]mod1000;
End;
End;
二分法BinarySearch
Procedurebinary_search;
begin
left:
=1;right:
=n+1;c:
=0;
whileleft<=rightdo
begin
mid:
=(left+right)div2;
iff[mid]=1then
begin
right:
=mid-1;
c:
=mid;
end
else
begin
left:
=mid+1;
c:
=mid+1;
end;
end;
end;
排序
直接选择排序insertionsort
Procedureinsertion_sort;
Begin
Fori:
=1tondo
Begin
J:
=1;
While(ja[j])doinc(j);
X:
=a[i];
Fork:
=jtoI-1do
A[k+1]:
=a[k];
A[j]:
=x;
End;
End;
快速排序Quicksort
Procedureqsort(be,en:
longint);
VarI,j,x,mid,temp:
longint;
Begin
I:
=be;j:
=en;mid:
=(i+j)div2;
X:
=a[mid];
Repeat
Whilex>a[i]doinc(i);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- noip 基本 算法 网络版
![提示](https://static.bdocx.com/images/bang_tan.gif)