大整数开根 codevs3119

版权声明:虽然是个蒟蒻但是转载还是要说一声的哟 https://blog.csdn.net/jpwang8/article/details/65449930

Description


给出一个正整数n,求n开根号后的整数部分的值。n的位数不超过1000位

Solution


我果然还是好弱啊

二分一个答案然后高精度乘法判断,然后我压位都打错一个晚上就过去了。。
这次的程序应该能当高精度的标了吧

Code


#include <stdio.h>
#include <string.h>
#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)
#define drp(i, st, ed) for (int i = st; i >= ed; i -= 1)
#define fill(x, t) memset(x, t, sizeof(x))
#define max(x, y) ((x)>(y)?(x):(y))
#define L 1001
#define MOD 1000
struct num{
    int s[L], len;
    inline bool operator ==(num b){
        num a = *this;
        if (a.len != b.len){
            return 0;
        }else{
            drp(i, a.len, 1){
                if (a.s[i] != b.s[i]){
                    return 0;
                }
            }
            return 1;
        }
    }
    inline bool operator <(num b){
        num a = *this;
        if (a.len < b.len){
            return 1;
        }else if (a.len > b.len){
            return 0;
        }else{
            drp(i, a.len, 1){
                if (a.s[i] < b.s[i]){
                    return 1;
                }else if (a.s[i] > b.s[i]){
                    return 0;
                }
            }
            return 0;
        }
    }
    inline bool operator <=(num b){
        num a = *this;
        if (a < b || a == b){
            return 1;
        }else{
            return 0;
        }
    }
    inline bool operator >(num b){
        num a = *this;
        if (a.len > b.len){
            return 1;
        }else if (a.len < b.len){
            return 0;
        }else{
            drp(i, a.len, 1){
                if (a.s[i] > b.s[i]){
                    return 1;
                }else if (a.s[i] < b.s[i]){
                    return 0;
                }
            }
            return 0;
        }
    }
    inline bool operator >=(num b){
        num a = *this;
        if (a > b || a == b){
            return 1;
        }else{
            return 0;
        }
    }
    inline num operator +(num b){
        num a = *this, c = (num){{0}, max(a.len, b.len)};
        int v = 0;
        rep(i, 1, c.len){
            c.s[i] = (a.s[i] + b.s[i] + v) % MOD;
            v = (a.s[i] + b.s[i] + v) / MOD;
        }
        if (v){
            c.len += 1;
            c.s[c.len] = v;
        }
        return c;
    }
    inline num operator -(num b){
        num a = *this, c = (num){{0}, max(a.len, b.len)};
        rep(i, 1, c.len){
            c.s[i] = a.s[i] - b.s[i];
            if (c.s[i] < 0){
                c.s[i] += MOD;
                a.s[i + 1] -= 1;
            }
        }
        while (!c.s[c.len] && c.len > 1){
            c.len -= 1;
        }
        return c;
    }
    inline num operator *(num b){
        num a = *this, c = (num){{0}, a.len + b.len};
        rep(i, 1, a.len){
            rep(j, 1, b.len){
                c.s[i + j - 1] += a.s[i] * b.s[j];
            }
        }
        rep(i, 1, a.len + b.len){
            c.s[i + 1] += c.s[i] / MOD;
            c.s[i] %= MOD;
        }
        while (!c.s[c.len] && c.len > 1){
            c.len -= 1;
        }
        return c;
    }
    inline num operator /(int b){
        num a = *this, c = (num){{0}, a.len};
        int v = 0;
        drp(i, len, 1){
            int t = v * MOD + a.s[i];
            c.s[i] = t / b;
            v = t % b;
        }
        while (!c.s[c.len] && c.len > 1){
            c.len -= 1;
        }
        return c;
    }
    inline void read(){
        fill(s, 0); len = 0;
        char st[L];
        scanf("%s", st);
        int v = 0, i;
        for (i = strlen(st) - 1; i >= 3; i -= 3){
            rep(j, i - 2, i){
                v = v * 10 + st[j] - '0';
            }
            s[++ len] = v;
            v = 0;
        }
        rep(j, 0, i){
            v = v * 10 + st[j] - '0';
        }
        s[++ len] = v;
    }
    inline void read1(int x){
        fill(s, 0);
        len = 0;
        do{s[++ len] = x % MOD;}while (x /= MOD);
    }
    inline void output(){
        num tmp = *this;
        int i = tmp.len;
        while (!tmp.s[i] && i > 1){
            i -= 1;
        }
        printf("%d", tmp.s[i]);
        drp(j, i - 1, 1){
            int v = tmp.s[j];
            int f[5] = {0};
            rep(k, 1, 3){
                f[k] = v % 10;
                v /= 10;
            }
            drp(k, 3, 1){
                printf("%d", f[k]);
            }
        }
        printf("\n");
    }
};
num one;
int main(void){
    one.s[1] = 1;
    one.len = 1;
    num p;
    p.read();
    num r = p;
    num l; l.s[1] = 0; l.len = 1;
    num mid, ans;
    while (l <= r){
        mid = l + r;
        mid = mid / 2;
        num qr = mid * mid;
        if (qr <= p){
            l = mid + one;
            ans = mid;
        }else if (qr > p){
            r = mid - one;
        }
    }
    ans.output();
    return 0;
}
阅读更多
换一批

没有更多推荐了,返回首页