退役选手ZlycerQan的强大的的高精度

  1 #include <cstdio>
  2 #include <iostream>
  3 #include <vector>
  4 #include <iomanip>
  5 #include <cassert>
  6 #include <algorithm>
  7 #include <cstring>
  8 
  9 const int Big_B = 1000000000;
 10 const int Big_L = 9;
 11 inline int intcmp_ (int a, int b) {
 12     if (a > b) return 1;
 13     return a < b ? -1 : 0;
 14 }
 15 struct Int {
 16 #define rg register
 17     inline int max (int a, int b) {
 18         return a > b ? a : b;
 19     }
 20     inline int min (int a, int b) {
 21         return a < b ? a : b;
 22     }
 23     std :: vector <int> c;
 24     Int () {} typedef long long LL;
 25     Int (int x) {
 26         for (; x > 0; c.push_back (x % Big_B), x /= Big_B);
 27     }
 28     Int (LL x) {
 29         for (; x > 0; c.push_back (x % Big_B), x /= Big_B);
 30     }
 31     inline void CrZ () {
 32         for (; !c.empty () && c.back () == 0; c.pop_back ());
 33     }
 34     inline Int &operator += (const Int &rhs) {
 35         c.resize (max (c.size (), rhs.c.size ()));
 36         rg int i, t = 0, S;
 37         for (i = 0, S = rhs.c.size (); i < S; ++ i)
 38             c[i] += rhs.c[i] + t, t = c[i] >= Big_B, c[i] -= Big_B & (-t);
 39         for (i = rhs.c.size (), S = c.size (); t && i < S; ++ i)
 40             c[i] += t, t = c[i] >= Big_B, c[i] -= Big_B & (-t);
 41         if (t) c.push_back (t);
 42         return *this;
 43     }
 44     inline Int &operator -= (const Int &rhs) {
 45         c.resize (max (c.size (), rhs.c.size ()));
 46         rg int i, t = 0, S;
 47         for (i = 0, S = rhs.c.size (); i < S; ++ i)
 48             c[i] -= rhs.c[i] + t, t = c[i] < 0, c[i] += Big_B & (-t);
 49         for (i = rhs.c.size (), S = c.size (); t && i < S; ++ i)
 50             c[i] -= t, t = c[i] < 0, c[i] += Big_B & (-t);
 51         CrZ ();
 52         return *this;
 53     }
 54     inline Int &operator *= (const Int &rhs) {
 55         rg int na = c.size (), i, j, S, ai;
 56         c.resize (na + rhs.c.size ());
 57         LL t;
 58         for (i = na - 1; i >= 0; -- i) {
 59             ai = c[i], t = 0, c[i] = 0;
 60             for (j = 0, S = rhs.c.size (); j < S; ++ j) {
 61                 t += c[i + j] + (LL) ai * rhs.c[j];
 62                 c[i + j] = t % Big_B, t /= Big_B;
 63             }
 64             for (j = rhs.c.size (), S = c.size (); t != 0 && i + j < S; ++ j)
 65                 t += c[i + j], c[i + j] = t % Big_B, t /= Big_B;
 66             assert (t == 0);
 67         }
 68         CrZ ();
 69         return *this;
 70     }
 71     inline Int &operator /= (const Int &rhs) {
 72         return *this = div (rhs);
 73     }
 74     inline Int &operator %= (const Int &rhs) {
 75         return div (rhs), *this;
 76     }
 77     inline Int &shlb (int l = 1) {
 78         if (c.empty ()) return *this;
 79         c.resize (c.size () + l);
 80         rg int i;
 81         for (i = c.size () - 1; i >= l; -- i) c[i] = c[i - l];
 82         for (i = 0; i < l; ++ i) c[i] = 0;
 83         return *this;
 84     }
 85     inline Int &shrb (int l = 1) {
 86         for (rg int i = 0; i < c.size () - l; ++ i) c[i] = c[i + l];
 87         c.resize (max (c.size () - l, 0));
 88         return *this;
 89     }
 90     inline int Comp (const Int &rhs) const {
 91         if (c.size () != rhs.c.size ()) return intcmp_ (c.size (), rhs.c.size ());
 92         for (rg int i = c.size () - 1; i >= 0; -- i)
 93             if (c[i] != rhs.c[i]) return intcmp_ (c[i], rhs.c[i]);
 94         return 0;
 95     }
 96     inline Int div (const Int &rhs) {
 97         assert (!rhs.c.empty ());
 98         Int q, r;
 99         rg int i;
100         if (rhs > *this) return 0;
101         q.c.resize (c.size () - rhs.c.size () + 1);
102         rg int _l, _r, mid;
103         for (i = c.size () - 1; i > c.size () - rhs.c.size (); -- i) r.shlb (), r += c[i];
104         for (i = c.size () - rhs.c.size (); i >= 0; -- i) {
105             r.shlb ();
106             r += c[i];
107             if (r.Comp (rhs) < 0) q.c[i] = 0;
108             else {
109                 _l = 0, _r = Big_B;
110                 for (; _l != _r; ) {
111                     mid = _l + _r >> 1;
112                     if ((rhs * mid).Comp (r) <= 0) _l = mid + 1;
113                     else _r = mid;
114                 }
115                 q.c[i] = _l - 1, r -= rhs * q.c[i];
116             }
117         }
118         q.CrZ (), *this = r;
119         return q;
120     }
121     friend inline Int operator + (const Int &lhs, const Int &rhs) {
122         Int res = lhs;
123         return res += rhs;
124     }
125     friend inline Int operator - (const Int &lhs, const Int &rhs) {
126         Int res = lhs;
127         return res -= rhs;
128     }
129     friend inline Int operator * (const Int &lhs, const Int &rhs) {
130         Int res = lhs;
131         return res *= rhs;
132     }
133     friend inline Int operator / (const Int &lhs, const Int &rhs) {
134         Int res = lhs;
135         return res.div (rhs);
136     }
137     friend inline Int operator % (const Int &lhs, const Int &rhs) {
138         Int res = lhs;
139         return res.div (rhs), res;
140     }
141     friend inline std :: ostream &operator << (std :: ostream &out, const Int &rhs) {
142         if (rhs.c.size () == 0) out << "0";
143         else {
144             out << rhs.c.back ();
145             for (rg int i = rhs.c.size () - 2; i >= 0; -- i)
146                 out << std :: setfill ('0') << std :: setw (Big_L) << rhs.c[i];
147         }
148         return out;
149     }
150     friend inline std :: istream &operator >> (std :: istream &in, Int &rhs) {
151         static char s[10000];
152         in >> s + 1;
153         int Len = strlen (s + 1);
154         int v = 0;
155         LL r = 0, p = 1;
156         for (rg int i = Len; i >= 1; -- i) {
157             ++ v;
158             r = r + (s[i] - '0') * p, p *= 10;
159             if (v == Big_L) rhs.c.push_back (r), r = 0, v = 0, p = 1;
160         }
161         if (v != 0) rhs.c.push_back (r);
162         return in;
163     }
164     friend inline bool operator < (const Int &lhs, const Int &rhs) {
165         return lhs.Comp (rhs) < 0;
166     }
167     friend inline bool operator <= (const Int &lhs, const Int &rhs) {
168         return lhs.Comp (rhs) <= 0;
169     }
170     friend inline bool operator > (const Int &lhs, const Int &rhs) {
171         return lhs.Comp (rhs) > 0;
172     }
173     friend inline bool operator >= (const Int &lhs, const Int &rhs) {
174         return lhs.Comp (rhs) >= 0;
175     }
176     friend inline bool operator == (const Int &lhs, const Int &rhs) {
177         return lhs.Comp (rhs) == 0;
178     }
179     friend inline bool operator != (const Int &lhs, const Int &rhs) {
180         return lhs.Comp (rhs) != 0;
181     }
182 #undef rg
183 };
184 Int a;
185 int Main () {
186     return 0;
187 }
188 int ZlycerQan = Main ();
189 int main (int argc, char *argv[]) {
190     ;
191 }

 

posted @ 2018-05-20 21:14  Niver  阅读(523)  评论(3编辑  收藏  举报