高精度进阶

Posted rorschach-xr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了高精度进阶相关的知识,希望对你有一定的参考价值。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#define SF scanf
#define PF printf
#define MAXN 100010
using namespace std;
typedef long long ll;
struct BigInteger 
    typedef unsigned long long LL;
 
    static const int BASE = 100000000;
    static const int WIDTH = 8;
    vector<int> s;
 
    BigInteger& clean()while(!s.back()&&s.size()>1)s.pop_back(); return *this;
    BigInteger(LL num = 0) *this = num;
    BigInteger(string s) *this = s;
    BigInteger& operator = (long long num) 
        s.clear();
        do 
            s.push_back(num % BASE);
            num /= BASE;
         while (num > 0);
        return *this;
    
    BigInteger& operator = (const string& str) 
        s.clear();
        int x, len = (str.length() - 1) / WIDTH + 1;
        for (int i = 0; i < len; i++) 
            int end = str.length() - i*WIDTH;
            int start = max(0, end - WIDTH);
            sscanf(str.substr(start,end-start).c_str(), "%d", &x);
            s.push_back(x);
        
        return (*this).clean();
    
 
    BigInteger operator + (const BigInteger& b) const 
        BigInteger c; c.s.clear();
        for (int i = 0, g = 0; ; i++) 
            if (g == 0 && i >= int(s.size()) && i >= int(b.s.size())) break;
            int x = g;
            if (i < int(s.size())) x += s[i];
            if (i < int(b.s.size())) x += b.s[i];
            c.s.push_back(x % BASE);
            g = x / BASE;
        
        return c;
    
    BigInteger operator - (const BigInteger& b) const 
        BigInteger c; c.s.clear();
        for (int i = 0, g = 0; ; i++) 
            if (g == 0 && i >= int(s.size()) && i >= int(b.s.size())) break;
            int x = s[i] + g;
            if (i < int(b.s.size())) x -= b.s[i];
            if (x < 0) g = -1; x += BASE; else g = 0;
            c.s.push_back(x);
        
        return c.clean();
    
    BigInteger operator * (const BigInteger& b) const 
        int i, j; LL g;
        vector<LL> v(s.size()+b.s.size(), 0);
        BigInteger c; c.s.clear();
        for(i=0;i<int(s.size());i++) for(j=0;j<int(b.s.size());j++) v[i+j]+=LL(s[i])*b.s[j];
        for (i = 0, g = 0; ; i++) 
            if (g ==0 && i >= int(v.size())) break;
            LL x = v[i] + g;
            c.s.push_back(x % BASE);
            g = x / BASE;
        
        return c.clean();
    
    BigInteger operator / (const ll b) const 
        BigInteger c;
		c.s.resize(20); 
	    ll x=0;
		for(int i=int(s.size())-1;i>=0;i--)
			x=x*BASE+s[i];
			c.s[i]=x/b;
			x=x%b;
		    
	    return c.clean();;  
    
    ll operator % (const ll b) const  //???????
        BigInteger c = *this;
        ll m=0;
        for (int i = s.size()-1; i >= 0; i--) 
            m = (m*BASE + s[i])%b;
        
        return m;
    
    int bsearch(const BigInteger& b, const BigInteger& m) const
        int L = 0, R = BASE-1, x;
        while (1) 
            x = (L+R)>>1;
            if (b*x<=m) if (b*(x+1)>m) return x; else L = x;
            else R = x;
        
    
    BigInteger& operator += (const BigInteger& b) *this = *this + b; return *this;
    BigInteger& operator -= (const BigInteger& b) *this = *this - b; return *this;
    BigInteger& operator *= (const BigInteger& b) *this = *this * b; return *this;
 
    bool operator < (const BigInteger& b) const 
        if (s.size() != b.s.size()) return s.size() < b.s.size();
        for (int i = s.size()-1; i >= 0; i--)
            if (s[i] != b.s[i]) return s[i] < b.s[i];
        return false;
    
    bool operator >(const BigInteger& b) constreturn b < *this;
    bool operator<=(const BigInteger& b) constreturn !(b < *this);
    bool operator>=(const BigInteger& b) constreturn !(*this < b);
    bool operator!=(const BigInteger& b) constreturn b < *this || *this < b;
    bool operator==(const BigInteger& b) constreturn !(b < *this) && !(b > *this);
;
 
istream& operator >> (istream& in, BigInteger& x) 
    string s;
    if (!(in >> s)) return in;
    x = s;
    return in;

 

以上是关于高精度进阶的主要内容,如果未能解决你的问题,请参考以下文章

Vue进阶(幺玖肆):JavaScript 浮点数精度计算

Java高级进阶学习资料!java中数据类型的精度排序

C语言进阶—数据的存储

随笔分类

如何快速打造高性能对话式AI?这个NLP公开课带你从入门到进阶,快来报名

从初识到进阶,硬核解说C语言< 进阶篇 1 > 深度剖析数据在内存中的存储