什么辣鸡高精度开方?
闲的无 (dan) 聊 (teng)
OI渣渣参不了GDKOI,唉
写写高精度开方,都是码农(不码农怎么消磨时间呢…)
以下的是开整数方,想看开小数的绕道
方法应该不少,这是其中的两种
二分
二分,不必多说
时间复杂度 O(log2n)
二分法改改能算小数嘚,不过很恶心
code
var
n,l,r,mid:ansistring;
function add(s1,s2:ansistring):ansistring;
var
a,b,c:array[0..10000]of longint;
n,i,l1,l2,l3,x:longint;
s:string;
begin
fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
fillchar(c,sizeof(c),0);
x:=0;
l1:=length(s1);
for i:=1 to l1 do a[l1-i+1]:=ord(s1[i])-ord('0');
l2:=length(s2);
for i:=1 to l2 do b[l2-i+1]:=ord(s2[i])-ord('0');
i:=1;
while (i<=l1)or(i<=l2) do
begin
c[i]:=a[i]+b[i]+x;
x:=c[i] div 10;
c[i]:=c[i] mod 10;
inc(i);
end;
if x>0 then
begin
l3:=i;
c[i]:=x;
end
else l3:=i-1;
add:='';
for i:=l3 downto 1 do
add:=add+chr(c[i]+48);
end;
function time(n1,n2:ansistring):ansistring;
var
a,b,c:array[0..10000]of longint;
lena,lenb,lenc,i,j,x:longint;
begin
fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
fillchar(c,sizeof(c),0);
lena:=length(n1);
lenb:=length(n2);
for i:=1 to lena do a[lena-i+1]:=ord(n1[i])-ord('0');
for i:=1 to lenb do b[lenb-i+1]:=ord(n2[i])-ord('0');
for i:=1 to lena do
begin
x:=0;
for j:=1 to lenb do
begin
c[i+j-1]:=a[i]*b[j]+x+c[i+j-1];
x:=c[i+j-1]div 10;
c[i+j-1]:=c[i+j-1]mod 10;
end;
c[i+j]:=x;
end;
lenc:=lena+lenb;
while (c[lenc]=0)and(lenc>1)do dec(lenc);
time:='';
for i:=lenc downto 1 do
time:=time+chr(c[i]+48);
end;
function division(s:ansistring;y:int64):ansistring;
var
a,b,c:array[0..10000]of longint;
lena,lenb,lenc,i,j,x:longint;
begin
fillchar(a,sizeof(a),0);
fillchar(b,sizeof(b),0);
fillchar(c,sizeof(c),0);
lena:=length(s);
for i:=1 to lena do a[lena-i+1]:=ord(s[i])-ord('0');
x:=0;
for i:=lena downto 1 do
begin
x:=x*10+a[i];
c[i]:=x div y;
x:=x mod y;
end;
lenc:=lena;
division:='';
while (c[lenc]=0)and(lenc>1)do dec(lenc);
for i:=lenc downto 1 do
division:=division+chr(c[i]+48);
end;
function bigger(s1,s2:ansistring):boolean;
begin
if (length(s1)>length(s2))or(length(s1)=length(s2))and((s1>s2))then
exit(true)
else exit(false);
end;
function smaller(s1,s2:ansistring):boolean;
begin
if (length(s1)<length(s2))or(length(s1)=length(s2))and((s1<=s2))then
exit(true)
else exit(false);
end;
function judge(s:ansistring):boolean;
begin
if smaller(time(s,s),n) and bigger(time(add(s,'1'),add(s,'1')),n)then
exit(true)
else exit(false);
end;
begin
readln(n);
l:='1';
r:=n;
mid:=division(