• 高精度计算模板


    (我之前写了些什么玩意儿.)

    一个结构体封装的高精度

    
    #include<bits/stdc++.h>
    using namespace std;
    const int MOD = 10000;
    struct bint{
      int a[5000], len;
      int& operator [] (int x) { return a[x]; }
      bint():len(1){ memset(a, 0, sizeof(a));}
      bint(long long x) { memset(a, 0, sizeof(a)); len = 0; while(x) { a[++len] = x % MOD; x /= MOD; } }
      void print(){
        printf("%d", a[len]);
        for (int i = len - 1; i > 0; i--)
          printf("%04d", a[i]);
      }
    };
    bint operator +(bint& a,bint& b){
      bint c;
      c.len = max(a.len, b.len);
      int x = 0;
      for (int i = 1; i <= c.len; i++){
          c[i] = a[i] + b[i] + x;
          x = c[i] / MOD;
            c[i] %= MOD;
      }
      if(x > 0)
        c[++c.len] = x;
      return c;
    }
    bint operator - (bint& a,bint& b){
      for (int i = 1; i <= b.len; i++)
          if(a[i] < b[i]){
              a[i] += MOD,a[i + 1]--;
              a[i] -= b[i];
          }   
    }
    bint operator * (bint& a,int b){
      int x = 0;
      for (int i = 1; i <= a.len; i++) 
        x += a[i] * b, a[i] = x % MOD, x /= MOD;
      if(x) a[++a.len] = x;
      return a;
    }
    bint operator *(bint& a, bint& b) {
      bint c;
      for(int i = 1; i <= a.len; i++) {
          int x = 0;
          for(int j = 1; j <= b.len; j++) 
              x += c[i + j - 1] + a[i] * b[j], c[i + j - 1] = x % MOD, x /= MOD;
          c[i + b.len] = x;
      }
      c.len = a.len + b.len;
      while(c.len > 1 && c[c.len] == 0) c.len--;
      return c;
    }
    bint operator / (bint& a,int b){
      bint c; c.len = a.len; int x = 0;
      for (int i = a.len; i > 0; i--) {
        x = x * MOD + a[i];
        c[i] = x / b;
        x %= b;
      }
      while(c[c.len] == 0 && c.len > 1) c.len--;
      return c;
    }
    bool operator < (bint& a,bint & b){
      if(a.len > b.len) return false;
      if(a.len < b.len) return true;
      for(int i = a.len; i >= 0; i--){
    	if(a[i] < b[i]) return true;
    	if(a[i] > b[i]) return false;
      }
      return false;
    }
    
    
  • 相关阅读:
    git---如何解决The authenticity of host can't be established.
    前端模板引擎artTemplate.js
    微信小程序
    小程序的项目结构设计
    拖拽插件SortableJS
    iscroll.js的简单使用方法
    头疼的闭包
    关于setTimeout的妙用前端函数节流
    webpack 加载动态图片
    在React中实现条件渲染的7种方法
  • 原文地址:https://www.cnblogs.com/FoxC/p/11776364.html
Copyright © 2020-2023  润新知