高精度开方

本文介绍了两种高精度开方的方法:二分法和竖式开方。二分法具有O(log2n)的时间复杂度,而竖式开方算法在处理大数据时更优,时间复杂度约为O(5log10n)。文章还讨论了如何通过改进竖式开方,将时间复杂度降低到O(log10n2)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么辣鸡高精度开方?

闲的无 (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(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值