• 高精度计算模板


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

    一个结构体封装的高精度

    
    #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;
    }
    
    
  • 相关阅读:
    Ext的组件结构分析(转)
    分析模式 责任模式
    Nhibernate学习起步之manytoone篇(转 明了篇)
    企业开发框架NHibernate和Spring.Net简介3
    企业开发框架NHibernate和Spring.Net简介4
    NHibernate Cascades: the different between all, alldeleteorphans and saveupdate
    XML与数据库
    企业开发框架NHibernate和Spring.Net简介1
    python对函数的理解
    seleniumwebdriver(python) (十五) 鼠标事件
  • 原文地址:https://www.cnblogs.com/FoxC/p/11776364.html
Copyright © 2020-2023  润新知