zoukankan      html  css  js  c++  java
  • 优秀大整数

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3   
      4 struct BigInteger
      5 {
      6     static const int BASE = 100000000; //和WIDTH保持一致
      7     static const int WIDTH = 8;        //八位一存储,如修改记得修改输出中的%08d
      8     bool sign;                         //符号, 0表示负数
      9     size_t length;                     //位数
     10     vector<int> num;                   //反序存
     11     //构造函数
     12     BigInteger(long long x = 0) { *this = x; }
     13     BigInteger(const string &x) { *this = x; }
     14     BigInteger(const BigInteger &x) { *this = x; }
     15     //剪掉前导0,并且求一下数的位数
     16     void cutLeadingZero()
     17     {
     18         while (num.back() == 0 && num.size() != 1)
     19         {
     20             num.pop_back();
     21         }
     22         int tmp = num.back();
     23         if (tmp == 0)
     24         {
     25             length = 1;
     26         }
     27         else
     28         {
     29             length = (num.size() - 1) * WIDTH;
     30             while (tmp > 0)
     31             {
     32                 length++;
     33                 tmp /= 10;
     34             }
     35         }
     36     }
     37     //赋值运算符
     38     BigInteger &operator=(long long x)
     39     {
     40         num.clear();
     41         if (x >= 0)
     42         {
     43             sign = true;
     44         }
     45         else
     46         {
     47             sign = false;
     48             x = -x;
     49         }
     50         do
     51         {
     52             num.push_back(x % BASE);
     53             x /= BASE;
     54         } while (x > 0);
     55         cutLeadingZero();
     56         return *this;
     57     }
     58     BigInteger &operator=(const string &str)
     59     {
     60         num.clear();
     61         sign = (str[0] != '-'); //设置符号
     62         int x, len = (str.size() - 1 - (!sign)) / WIDTH + 1;
     63         for (int i = 0; i < len; i++)
     64         {
     65             int End = str.size() - i * WIDTH;
     66             int start = max((int)(!sign), End - WIDTH); //防止越界
     67             sscanf(str.substr(start, End - start).c_str(), "%d", &x);
     68             num.push_back(x);
     69         }
     70         cutLeadingZero();
     71         return *this;
     72     }
     73     BigInteger &operator=(const BigInteger &tmp)
     74     {
     75         num = tmp.num;
     76         sign = tmp.sign;
     77         length = tmp.length;
     78         return *this;
     79     }
     80     //绝对值
     81     BigInteger abs() const
     82     {
     83         BigInteger ans(*this);
     84         ans.sign = true;
     85         return ans;
     86     }
     87     //正号
     88     const BigInteger &operator+() const { return *this; }
     89     //负号
     90     BigInteger operator-() const
     91     {
     92         BigInteger ans(*this);
     93         if (ans != 0)
     94             ans.sign = !ans.sign;
     95         return ans;
     96     }
     97     // + 运算符
     98     BigInteger operator+(const BigInteger &b) const
     99     {
    100         if (!b.sign)
    101         {
    102             return *this - (-b);
    103         }
    104         if (!sign)
    105         {
    106             return b - (-*this);
    107         }
    108         BigInteger ans;
    109         ans.num.clear();
    110         for (int i = 0, g = 0;; i++)
    111         {
    112             if (g == 0 && i >= num.size() && i >= b.num.size())
    113                 break;
    114             int x = g;
    115             if (i < num.size())
    116                 x += num[i];
    117             if (i < b.num.size())
    118                 x += b.num[i];
    119             ans.num.push_back(x % BASE);
    120             g = x / BASE;
    121         }
    122         ans.cutLeadingZero();
    123         return ans;
    124     }
    125     // - 运算符
    126     BigInteger operator-(const BigInteger &b) const
    127     {
    128         if (!b.sign)
    129         {
    130             return *this + (-b);
    131         }
    132         if (!sign)
    133         {
    134             return -((-*this) + b);
    135         }
    136         if (*this < b)
    137         {
    138             return -(b - *this);
    139         }
    140         BigInteger ans;
    141         ans.num.clear();
    142         for (int i = 0, g = 0;; i++)
    143         {
    144             if (g == 0 && i >= num.size() && i >= b.num.size())
    145                 break;
    146             int x = g;
    147             g = 0;
    148             if (i < num.size())
    149                 x += num[i];
    150             if (i < b.num.size())
    151                 x -= b.num[i];
    152             if (x < 0)
    153             {
    154                 x += BASE;
    155                 g = -1;
    156             }
    157             ans.num.push_back(x);
    158         }
    159         ans.cutLeadingZero();
    160         return ans;
    161     }
    162     // * 运算符
    163     BigInteger operator*(const BigInteger &b) const
    164     {
    165         int lena = num.size(), lenb = b.num.size();
    166         BigInteger ans;
    167         for (int i = 0; i < lena + lenb; i++)
    168             ans.num.push_back(0);
    169         for (int i = 0, g = 0; i < lena; i++)
    170         {
    171             g = 0;
    172             for (int j = 0; j < lenb; j++)
    173             {
    174                 long long x = ans.num[i + j];
    175                 x += (long long)num[i] * (long long)b.num[j];
    176                 ans.num[i + j] = x % BASE;
    177                 g = x / BASE;
    178                 ans.num[i + j + 1] += g;
    179             }
    180         }
    181         ans.cutLeadingZero();
    182         ans.sign = (ans.length == 1 && ans.num[0] == 0) || (sign == b.sign);
    183         return ans;
    184     }
    185     //*10^n 大数除大数中用到
    186     BigInteger e(size_t n) const
    187     {
    188         int tmp = n % WIDTH;
    189         BigInteger ans;
    190         ans.length = n + 1;
    191         n /= WIDTH;
    192         while (ans.num.size() <= n)
    193             ans.num.push_back(0);
    194         ans.num[n] = 1;
    195         while (tmp--)
    196             ans.num[n] *= 10;
    197         return ans * (*this);
    198     }
    199     // /运算符 (大数除大数)
    200     BigInteger operator/(const BigInteger &b) const
    201     {
    202         BigInteger aa((*this).abs());
    203         BigInteger bb(b.abs());
    204         if (aa < bb)
    205             return 0;
    206         char *str = new char[aa.length + 1];
    207         memset(str, 0, sizeof(char) * (aa.length + 1));
    208         BigInteger tmp;
    209         int lena = aa.length, lenb = bb.length;
    210         for (int i = 0; i <= lena - lenb; i++)
    211         {
    212             tmp = bb.e(lena - lenb - i);
    213             while (aa >= tmp)
    214             {
    215                 str[i]++;
    216                 aa = aa - tmp;
    217             }
    218             str[i] += '0';
    219         }
    220         BigInteger ans(str);
    221         delete[] str;
    222         ans.sign = (ans == 0 || sign == b.sign);
    223         return ans;
    224     }
    225     // %运算符
    226     BigInteger operator%(const BigInteger &b) const
    227     {
    228         return *this - *this / b * b;
    229     }
    230     // ++ 运算符
    231     BigInteger &operator++()
    232     {
    233         *this = *this + 1;
    234         return *this;
    235     }
    236     // -- 运算符
    237     BigInteger &operator--()
    238     {
    239         *this = *this - 1;
    240         return *this;
    241     }
    242     // += 运算符
    243     BigInteger &operator+=(const BigInteger &b)
    244     {
    245         *this = *this + b;
    246         return *this;
    247     }
    248     // -= 运算符
    249     BigInteger &operator-=(const BigInteger &b)
    250     {
    251         *this = *this - b;
    252         return *this;
    253     }
    254     // *=运算符
    255     BigInteger &operator*=(const BigInteger &b)
    256     {
    257         *this = *this * b;
    258         return *this;
    259     }
    260     // /= 运算符
    261     BigInteger &operator/=(const BigInteger &b)
    262     {
    263         *this = *this / b;
    264         return *this;
    265     }
    266     // %=运算符
    267     BigInteger &operator%=(const BigInteger &b)
    268     {
    269         *this = *this % b;
    270         return *this;
    271     }
    272     // < 运算符
    273     bool operator<(const BigInteger &b) const
    274     {
    275         if (sign != b.sign) //正负,负正
    276         {
    277             return !sign;
    278         }
    279         else if (!sign && !b.sign) //负负
    280         {
    281             return -b < -*this;
    282         }
    283         //正正
    284         if (num.size() != b.num.size())
    285             return num.size() < b.num.size();
    286         for (int i = num.size() - 1; i >= 0; i--)
    287             if (num[i] != b.num[i])
    288                 return num[i] < b.num[i];
    289         return false;
    290     }
    291     bool operator>(const BigInteger &b) const { return b < *this; }                     // >  运算符
    292     bool operator<=(const BigInteger &b) const { return !(b < *this); }                 // <= 运算符
    293     bool operator>=(const BigInteger &b) const { return !(*this < b); }                 // >= 运算符
    294     bool operator!=(const BigInteger &b) const { return b < *this || *this < b; }       // != 运算符
    295     bool operator==(const BigInteger &b) const { return !(b < *this) && !(*this < b); } //==运算符
    296     // 逻辑运算符
    297     bool operator||(const BigInteger &b) const { return *this != 0 || b != 0; } // || 运算符
    298     bool operator&&(const BigInteger &b) const { return *this != 0 && b != 0; } // && 运算符
    299     bool operator!() { return (bool)(*this == 0); }                             // ! 运算符
    300    
    301     //重载<<使得可以直接输出大数
    302     friend ostream &operator<<(ostream &out, const BigInteger &x)
    303     {
    304         if (!x.sign)
    305             out << '-';
    306         out << x.num.back();
    307         for (int i = x.num.size() - 2; i >= 0; i--)
    308         {
    309             char buf[10];
    310             //如WIDTH和BASR有变化,此处要修改为%0(WIDTH)d
    311             sprintf(buf, "%08d", x.num[i]);
    312             for (int j = 0; j < strlen(buf); j++)
    313                 out << buf[j];
    314         }
    315         return out;
    316     }
    317     //重载>>使得可以直接输入大数
    318     friend istream &operator>>(istream &in, BigInteger &x)
    319     {
    320         string str;
    321         in >> str;
    322         size_t len = str.size();
    323         int start = 0;
    324         if (str[0] == '-')
    325             start = 1;
    326         if (str[start] == '')
    327             return in;
    328         for (int i = start; i < len; i++)
    329         {
    330             if (str[i] < '0' || str[i] > '9')
    331                 return in;
    332         }
    333         x.sign = !start;
    334         x = str.c_str();
    335         return in;
    336     }
    337 };
    338   
    339 typedef BigInteger Int;
    340   
    341 int main()
    342 {
    343     Int ans, a = 2, b;
    344     int t; scanf("%d", &t);
    345     for(; t--; ) {
    346         cin >> b;
    347         a = 2; ans = 1;
    348         for(; b != 0; b /= 2, a *= a)
    349              if(b %2 == 1) ans *= a;
    350         cout << ans << '
    ';
    351     }
    352     return 0;
    353 }
  • 相关阅读:
    day10 Python 形参顺序
    为oracle中的表格增加列和删除列
    为mapcontrol中的图层设置透明度
    最大的愿望 2007-05-10
    动心 2004年后半年
    写在十年 2007-09-15 (写给L之三)
    致vi老大 2011.1
    如潮 2011.2
    自然人——女孩思绪 (2006-09-14 08:21:51)
    朋友(2003年)
  • 原文地址:https://www.cnblogs.com/Shy-key/p/11961781.html
Copyright © 2011-2022 走看看