zoukankan      html  css  js  c++  java
  • 【大整数模板】

      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 typedef long long ll;
      4 
      5 struct BigInteger
      6 {
      7     int len;
      8     int arg[2005];
      9     BigInteger(int x = 0){
     10         IntToBigInteger(x);
     11     }
     12     BigInteger(char *str){
     13         CharToBigInteger(str);
     14     }
     15     void print()
     16     {
     17         for (int i = len - 1; i >= 0; i--){
     18             cout<<arg[i];
     19         }
     20         cout<<endl;
     21     }
     22     void CharToBigInteger(char *str)
     23     {
     24         len = strlen(str);
     25         memset(arg, 0, sizeof(arg));
     26         for (int i = 0; i < len; i++){
     27             arg[i] = str[len - i - 1] - 48;
     28         }
     29         while(len > 0 && arg[len-1] == 0){
     30             len--;
     31         }
     32     }
     33     void IntToBigInteger(int x)
     34     {
     35         len = 0;
     36         memset(arg, 0, sizeof(arg));
     37         do{
     38             arg[len++] = x % 10;
     39             x = x / 10;
     40         }while (x);
     41     }
     42     friend istream &operator>>(istream &in, BigInteger &w)
     43     {
     44         char str[1005];
     45         in>>str;
     46         w.CharToBigInteger(str);
     47         return in;
     48     }
     49     friend ostream& operator<<(ostream &out, BigInteger w)
     50     {
     51         for (int i = w.len - 1; i >= 0; i--){
     52             out<<w.arg[i];
     53         }
     54         return out;
     55     }
     56     friend bool operator==(BigInteger r, BigInteger w)
     57     {
     58         if (r.len != w.len) return false;
     59         int i = r.len - 1;
     60         while (i >= 0){
     61             if (r.arg[i] != w.arg[i])
     62                 return false;
     63             i--;
     64         }
     65         return true;
     66     }
     67     friend bool operator<=(BigInteger r, BigInteger w)
     68     {
     69         if (r.len < w.len) return true;
     70         else if (w.len < r.len) return false;
     71         int i = r.len - 1;
     72         while (i >= 0){
     73             if (r.arg[i]<w.arg[i])
     74                 return true;
     75             else if (r.arg[i]>w.arg[i])
     76                 return false;
     77             i--;
     78         }
     79         return true;
     80     }
     81     friend bool operator<=(int x, BigInteger w)
     82     {
     83         BigInteger r;
     84         r.IntToBigInteger(x);
     85         if (r.len < w.len) return true;
     86         else if (w.len < r.len) return false;
     87         int i = r.len - 1;
     88         while (i >= 0){
     89             if (r.arg[i]<w.arg[i])
     90                 return true;
     91             else if (r.arg[i]>w.arg[i])
     92                 return false;
     93             i--;
     94         }
     95         return true;
     96     }
     97     friend bool operator<(BigInteger r, BigInteger w)
     98     {
     99         if (r.len < w.len) return true;
    100         else if (w.len < r.len) return false;
    101         int i = r.len - 1;
    102         while (i >= 0){
    103             if (r.arg[i]<w.arg[i])
    104                 return true;
    105             else if (r.arg[i]>w.arg[i])
    106                 return false;
    107             i--;
    108         }
    109         return false;
    110     }
    111     friend bool operator<(int x, BigInteger w)
    112     {
    113         BigInteger r;
    114         r.IntToBigInteger(x);
    115         if (r.len < w.len) return true;
    116         else if (w.len < r.len)    return false;
    117         int i = r.len - 1;
    118         while (i >= 0){
    119             if (r.arg[i]<w.arg[i])
    120                 return true;
    121             else if (r.arg[i]>w.arg[i])
    122                 return false;
    123             i--;
    124         }
    125         return false;
    126     }
    127     friend BigInteger operator+(int r, BigInteger w){
    128         w.arg[0] += r;
    129         int i = 0;
    130         while (w.arg[i] >= 10){
    131             w.arg[i + 1] += w.arg[i] / 10;
    132             w.arg[i] = w.arg[i] % 10;
    133             i++;
    134         }
    135         if(w.arg[i]) i++;
    136         w.len = i > w.len ? i : w.len;
    137         return w;
    138     }
    139     friend BigInteger operator+(BigInteger w, int r)
    140     {
    141         w.arg[0] += r;
    142         int i = 0;
    143         while (w.arg[i] >= 10){
    144             w.arg[i + 1] += w.arg[i] / 10;
    145             w.arg[i] = w.arg[i] % 10;
    146             i++;
    147         }
    148         if(w.arg[i]) i++;
    149         w.len = i > w.len ? i : w.len;
    150         return w;
    151     }
    152     friend BigInteger operator+(BigInteger r, BigInteger w)
    153     {
    154         int len = r.len > w.len ? r.len : w.len;
    155         for (int i = 0; i < len; i++){
    156             if(i < w.len)
    157                 r.arg[i] = r.arg[i] + w.arg[i];
    158             r.arg[i + 1] += r.arg[i] / 10;
    159             r.arg[i] = r.arg[i] % 10;
    160         }
    161         while (r.arg[len] >= 10){
    162             r.arg[len + 1] += r.arg[len] / 10;
    163             r.arg[len] = r.arg[len] % 10;
    164             len++;
    165         }
    166         if(r.arg[len]) len++;
    167         r.len  = len > r.len ? len : r.len;
    168         return r;
    169     }
    170     friend BigInteger operator-(BigInteger r, BigInteger w)
    171     {
    172         for (int i = 0; i < r.len; i++){
    173             if (r.arg[i] >= w.arg[i])
    174                 r.arg[i] = r.arg[i] - w.arg[i];
    175             else{
    176                 r.arg[i] = r.arg[i] + 10;
    177                 r.arg[i + 1] = r.arg[i + 1] - 1;
    178                 r.arg[i] = r.arg[i] - w.arg[i];
    179             }
    180         }
    181         while (r.arg[r.len - 1] == 0 && r.len > 1)
    182             r.len--;
    183         return r;
    184     }
    185     friend BigInteger operator-(BigInteger r, int w)
    186     {
    187         for (int i = 0; i < r.len; i++, w = w / 10){
    188             if (r.arg[i] >= w % 10)
    189                 r.arg[i] = r.arg[i] - w % 10;
    190             else{
    191                 r.arg[i] = r.arg[i] + 10;
    192                 r.arg[i + 1] = r.arg[i + 1] - 1;
    193                 r.arg[i] = r.arg[i] - w % 10;
    194             }
    195         }
    196         while (r.arg[r.len - 1] == 0 && r.len > 1)
    197             r.len--;
    198         return r;
    199     }
    200     friend BigInteger operator*(int x, BigInteger w)
    201     {
    202         BigInteger r;
    203         if(x == 0 || (w.len == 1 && w.arg[0] == 0)){
    204             return r;
    205         }
    206         for (int i = 0; i < w.len; i++){
    207             r.arg[i] += w.arg[i] * x;
    208             r.arg[i + 1] += r.arg[i] / 10;
    209             r.arg[i] = r.arg[i] % 10;
    210         }
    211         int i = r.arg[w.len] == 0 ? w.len-1 : w.len;
    212         while (r.arg[i] >= 10){
    213             r.arg[i + 1] = r.arg[i] / 10;
    214             r.arg[i] = r.arg[i] % 10;
    215             i++;
    216         }
    217         r.len = (i >= 0) ? i + 1 : 1;
    218         return r;
    219     }
    220     friend BigInteger operator*(BigInteger w, int x)
    221     {
    222         BigInteger r;
    223         if(x == 0 || (w.len == 1 && w.arg[0] == 0)){
    224             return r;
    225         }
    226         for (int i = 0; i < w.len; i++){
    227             r.arg[i] += w.arg[i] * x;
    228             r.arg[i + 1] += r.arg[i] / 10;
    229             r.arg[i] = r.arg[i] % 10;
    230         }
    231         int i = r.arg[w.len] == 0 ? w.len-1 : w.len;
    232         while (r.arg[i] >= 10){
    233             r.arg[i + 1] = r.arg[i] / 10;
    234             r.arg[i] = r.arg[i] % 10;
    235             i++;
    236         }
    237         r.len = (i >= 0) ? i + 1 : 1;
    238         return r;
    239     }
    240     friend BigInteger operator*(BigInteger r, BigInteger w)
    241     {
    242         BigInteger v;
    243         if((r.len == 1 && r.arg[0] == 0)|| (w.len == 1 && w.arg[0] == 0)){
    244             return v;
    245         }
    246         for (int i = 0; i < r.len; i++){
    247             for (int k = 0; k < w.len; k++){
    248                 v.arg[i + k] += w.arg[k] * r.arg[i];
    249                 v.arg[i + k + 1] += v.arg[i + k] / 10;
    250                 v.arg[i + k] = v.arg[i + k] % 10;
    251             }
    252         }
    253         int i = w.len + r.len - 1;
    254         i = v.arg[i] == 0 ? i-1 : i;
    255         while (v.arg[i] >= 10){
    256             v.arg[i + 1] = v.arg[i] / 10;
    257             v.arg[i] = v.arg[i] % 10;
    258             i++;
    259         }
    260         v.len = (i >= 0) ? i + 1 : 1;
    261         return v;
    262     }
    263 
    264     friend BigInteger operator/(BigInteger r, int w)
    265     {
    266         BigInteger  h, resl;
    267         if(w == 0) return h;
    268         for (int i = r.len - 1; i >= 0; i--){
    269             resl = 10 * resl;
    270             h = r.arg[i] + (10 * h);
    271             while (w <= h){
    272                 resl = 1 + resl;
    273                 h = h - w;
    274             }
    275         }
    276         return resl;
    277     }
    278     friend BigInteger operator/(BigInteger r, BigInteger w)
    279     {
    280         BigInteger h, resl;
    281         if(w.len == 1 && w.arg[0] == 0) return h;
    282         for (int i = r.len - 1; i >= 0; i--){
    283             resl = 10 * resl;
    284             h = r.arg[i] + (10 * h);
    285             while (w <= h){
    286                 resl = 1 + resl;
    287                 h = h - w;
    288             }
    289         }
    290         return resl;
    291     }
    292 
    293     friend BigInteger operator%(BigInteger r, BigInteger w)
    294     {
    295         BigInteger h;
    296         if(w.len == 1 && w.arg[0] == 0) return h;
    297         for (int i = r.len - 1; i >= 0; i--){
    298             h = r.arg[i] + (10 * h);
    299             while (w <= h){
    300                 h = h - w;
    301             }
    302         }
    303         return h;
    304     }
    305     void sqrt()
    306     {
    307         BigInteger w, r;
    308         w.len = r.len = 0;
    309         int lens = len - 1;
    310         if(len == 1 && arg[0] == 1)
    311             return ;
    312         r.arg[r.len++] = arg[lens--];
    313         if (len % 2 == 0)
    314             r = arg[lens--] + 10 * r;
    315         while (lens >= -1){
    316             int i = 0;
    317             while ((i*(i + 20 * w)) <= r){
    318                 i++;
    319             }
    320             i--;
    321             if (i == -1 || (r.len == 1 && r.arg[0] == 1))
    322                 i = 0;
    323             r = r - (i*(i + 20 * w));
    324             w = i + 10 * w;
    325             if(lens >= 0){
    326                 r = arg[lens--] + 10 * r;
    327                 r = arg[lens--] + 10 * r;
    328             }
    329             else
    330                 lens -= 2;
    331         }
    332         *this = w;
    333     }
    334 };
    335 
    336 
    337 int main()
    338 {
    339 //    BigInteger x=BigInteger(2);
    340 //    BigInteger y=BigInteger(3);
    341 //    BigInteger z=x*y;
    342 //    z.print();
    343 //    print(); 
    344     int cnt=0;
    345     BigInteger x=BigInteger(1);
    346     BigInteger y=BigInteger(2);
    347     while(cnt<=9)
    348     {
    349         x=x*y;
    350         cnt++;
    351     //    x.print();
    352     }
    353     x.print();
    354 //    cout<<x.len<<endl;
    355 //    x.print();
    356 //    int T;
    357 //    scanf("%d",&T);
    358 //    while(T--)
    359 //    {
    360 //        int q;
    361 //        scanf("%d",&q);
    362 //        x.print();
    363 //    }
    364     return 0;
    365 }
    View Code
  • 相关阅读:
    MySql从一窍不通到入门(五)Sharding:分表、分库、分片和分区
    rac 关库 启库
    rac 配置dg完成版
    CRS添加、删除节点
    分区表性能窥测
    传入时间按月分区并创建每月表空间
    python to be linux daemon
    python to be Windows Daemon
    Shell数组相关操作
    Python操作PDF与Tiff文件
  • 原文地址:https://www.cnblogs.com/itcsl/p/7536826.html
Copyright © 2011-2022 走看看