UVa 10023 square root: High precision and open square formula

Source: Internet
Author: User
Tags mul square root strlen time limit

10023-square Root

Time limit:3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=964

The Problem

You are are to determinate X by given Y, from expression

The Input

The the number of test cases, followed by a blank line.

Each test case is the input contains a positive integer Y (1<=y<=101000), with no blanks or leading zeroes in it.

It is guaranteed, this for given Y, X would be always an integer.

Each test case is separated by a.

The Output

For each test case, your program should print X in the same format as Y is given in input.

Print a blank line between the outputs for two consecutive test cases.

Sample Input

1

7206604678144

Sample Output

2684512

Using the open square formula can be broken, Newton's method incredibly will tle. (Java almost tle)

Complete code:

/*0.562s*/#include &lt;cstdio&gt; #include &lt;cstring&gt; const int MAXN = 1010;  
    
Char S[MAXN], RE[MAXN];  
    int big (char s1[], char s2[]) {int len1, len2, I, q;  
    q = 0;  
    while (s1[q] = = ' 0 ') q++;  
    strcpy (S1, S1 + q);  
        if (strlen (s1) = = 0) {s1[0] = ' 0 ';  
    S1[1] = 0;  
    } q = 0;  
    while (s2[q] = = ' 0 ') q++;  
    strcpy (s2, s2 + q);  
        if (strlen (s2) = = 0) {s2[0] = ' 0 ';  
    S2[1] = 0;  
    } len1 = strlen (S1);  
    Len2 = strlen (s2);  
    if (Len1 &gt; Len2) return 1;  
    else if (Len1 &lt; len2) return 0; else {for (i = 0; i &lt; len1 i++) {if (s1[i) &gt; S2[i]) retur  
            n 1;  
        else if (S1[i] &lt; s2[i]) return 0;  
} return 0;  
    } void Mul (char s[], int t, char re[])//Multiply {int left, I, J, K, Len;  
    char c;left = 0;  
    j = 0;  
        for (i = strlen (s)-1; I &gt;= 0; i--) {k = T * (s[i]-"0") + left;  
        Re[j++] = (k% 10) + ' 0 ';  
    left = K/10;  
        while (Left &gt; 0) {re[j++] = (left% 10) + ' 0 ';  
    Left/= 10;  
    } Re[j] = 0;  
    Len = strlen (re);  
        for (i = 0; i &lt; LEN/2 i++) {c = re[i];  
        Re[i] = re[len-1-i];  
    Re[len-1-I] = C;  
} return;  
    } void Sub (char a[], char b[])//minus {int left, len1, Len2, temp, J;  
    Len1 = strlen (a)-1;  
    Len2 = strlen (b)-1;  
    left = 0;  
        while (len2 &gt;= 0) {temp = A[len1]-b[len2] + left;  
            if (Temp &lt; 0) {temp = 10;  
        left =-1;  
        else left = 0;  
        A[LEN1] = temp + ' 0 ';  
        len1--;  
    len2--; while (len1 &gt;= 0) {temp = a[len1]-' 0' + left;  
            if (Temp &lt; 0) {temp = 10;  
        left =-1;  
        else left = 0;  
        A[LEN1] = temp + ' 0 ';  
    len1--;  
    } j = 0;  
    while (a[j] = = ' 0 ') j + +;  
    strcpy (A, A + j);  
        if (strlen (a) = = 0) {a[0] = ' 0 ';  
    A[1] = 0;  
} return;  
    } void Sqr (char s[], char re[])//root {char TEMP[MAXN];  
    Char LEFT[MAXN];  
    Char P[MAXN];  
    int I, J, Len1, Len2, q;  
    Len1 = strlen (s);  
        if (len1% 2 = 0) {Left[0] = s[0];  
        LEFT[1] = s[1];  
        LEFT[2] = 0;  
    j = 2;  
        else {left[0] = s[0];  
        LEFT[1] = 0;  
    j = 1;  
    } re[0] = ' 0 ';  
    RE[1] = 0;  
    q = 0;  
        while (J &lt;= len1) {mul (Re, temp);  
        Len2 = strlen (temp); for (i = 9; I &gt;= 0; i--) {temp[len2-1] = i + ' 0';  
            Mul (temp, I, p);  
        if (!big (p, left));  
        } re[q++] = i + ' 0 ';  
        RE[Q] = 0;  
        Sub (left, p);  
        Len2 = strlen (left);  
        LEFT[LEN2] = S[j];  
        Left[len2 + 1] = s[j + 1];  
        Left[len2 + 2] = 0;  
    J + + 2;  
    int main (void) {int t;  
    scanf ("%d", &amp;t);  
        while (t--) {scanf ("%s", s);  
        SQR (S, re);  
        int i = 0;  
        while (re[i] = = ' 0 ') i++;  
        strcpy (Re, re + i);  
        printf ("%s\n", re);  
    if (t) putchar (' \ n ');  
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

/*2.818s*/
    
import java.io.*;  
Import java.util.*;  
Import java.math.*;  
    
public class Main {  
    static final BigInteger two = new BigInteger ("2");  
    static Scanner cin = new Scanner (new Bufferedinputstream (system.in));  
    
    public static void Main (string[] args) {  
        int n = cin.nextint ();  
        while (n--> 0) {  
            BigInteger y = Cin.nextbiginteger ();  
            BigInteger C = y, temp;  
            do {  
                temp = y;  
                y = Temp.add (c.divide (y)). Divide (two);  
            }  
            while (Y.compareto (temp) = = 1);  
            System.out.println (y);  
            if (n > 0)  
                System.out.println ();  
        }  
    }  
}

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.