[code3119] High-precision practice of large integer Open root

Source: Internet
Author: User

Question Description
Given a positive integer n, the value of the integer part after the opening of the square root of N. The number of bits in n is no more than 1000 bits.
Input
Read in a positive integer n not exceeding 1000 bits.
Output
Output the answers you ask
Input example
17
Output example
4

High-precision open root: Need to use the hand to open the square root of the method, I actually this method will not, is temporarily to the online learning

Online said the method is quite detailed, I am not detailed here, the following direct code:

High-precision templates require high-reduction, high-multiplication, and high-and-low.

1#include <iostream>2#include <algorithm>3#include <cmath>4#include <cstring>5#include <queue>6#include <cstdio>7 using namespacestd;8typedefLong LongLL;9InlineintRead ()Ten { One     intx=0, f=1;CharC=GetChar (); A      while(!isdigit (c)) {if(c=='-') f=-1; c=GetChar ();} -      while(IsDigit (c)) {x=x*Ten+c-'0'; c=GetChar ();} -     returnx*F; the } - Const intmaxn=1010; - structData - { +     intL,V[MAXN]; -Data () {l=1; memset (V,0,sizeof(v));} +Dataoperator= (Constdata&s) A     { atL=s.l;memset (V,0,sizeof(v)); -          for(intI=1; i<=l;i++) v[i]=S.v[i]; -         return* This; -     } -Dataoperator= (Const int&s) -     { inL=1; v[1]=s; -          while(v[l]>9) v[l+1]+=v[l]/Ten, v[l]%=Ten, l++; to         return* This; +     } -Dataoperator- (Constdata&s) the     { *Data c;c.l=l; $          for(intI=1; i<=c.l;i++) c.v[i]=V[i];Panax Notoginseng          for(intI=1; i<=c.l;i++) c.v[i]-=S.v[i]; -          for(intI=1; i<c.l;i++)if(c.v[i]<0) c.v[i]+=Ten, c.v[i+1]--; the          while(!C.V[C.L]) c.l--; +         returnC; A     } theDataoperator* (Const int&s) +     { -Data c;c.l=l; $          for(intI=1; i<=c.l;i++) c.v[i]=v[i]*s; $          for(intI=1; i<c.l;i++)if(c.v[i]>9) c.v[i+1]+= (C.v[i])/Ten, c.v[i]%=Ten; -          while(c.v[c.l]>9) c.v[c.l+1]+= (C.V[C.L])/Ten, c.v[c.l]%=Ten, c.l++; -         returnC; the     } -Dataoperator+ (Const int&s)Wuyi     { theData c=* This; -c.v[1]+=s;intI=1; Wu          while(c.v[i]>9) c.v[i+1]+=c.v[i]/Ten, c.v[i]%=Ten, i++; -C.l=Max (c.l,i); About         returnC; $     } -     BOOL operator<= (Constdata& t)Const  -     { -         if(L!=T.L)returnl<T.L; A          for(inti=l;i;i--)if(V[i]!=t.v[i])returnv[i]<T.v[i]; +         return 1; the     } - }a; $ intnum[510]; the voidScan (Data &s) the { the     CharCH[MAXN]; thescanf"%s", ch+1); S.l=strlen (ch+1); -      for(intI=1; i<=s.l;i++) s.v[i]=ch[s.l-i+1]-'0';  in     return; the }  the voidPrint (data s) { for(inti=s.l;i;i--) printf ("%d", S.v[i]);cout<<Endl;} About voidInit (data s)//set this number from one digit to the left every two bits (for example, 65536 into 6,55,36) the { the     if(s.l%2) the     { +num[0]=S.V[S.L]; -          for(intI=2; i<=s.l;i+=2) num[i/2]=s.v[s.l-i+1]*Ten+s.v[s.l-i];  the     }Bayi     Else  for(intI=1; i<=s.l;i+=2) num[i/2]=s.v[s.l-i+1]*Ten+s.v[s.l-i]; the     return; the }  - data Sqrt (data s) - { the data ans,t,q,p; theans=0; t=0; the     intlen= s.l%2? s.l/2+1: s.l/2;//dividing the number of blocks the      for(intI=0; i<len;i++) -     { theT= (t* -)+Num[i]; thep=ans* -; ans=ans*Ten;//the ANS will be multiplied by 10 in advance, and ans will not be updated if the following loop does not go in again . the          for(intj=9; j>=0; j--)94         { theq= (P+J) *J; the             if(q<=t) the             {98ans=ans+J; Aboutt=t-Q; -                  Break;101             }102         }103     }    104     returnans; the }106 intMain ()107 {108 Scan (a);109 Init (a); the Print (Sqrt (a));111     return 0; the}

[code3119] High-precision practice of large integer Open root

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.