Repository navigation
Expand file tree
/
Copy pathBigInt Alpha0.4.cpp
More file actions
284 lines (253 loc) · 7.41 KB
/
Copy pathBigInt Alpha0.4.cpp
File metadata and controls
284 lines (253 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdio>
#include <string>
#include <algorithm>
//#pragma warning(disable:4996)
using namespace std;
#define LL long long
#define ULL unsigned long long
#define LD long double
#define Rep(x,y,i) for (int i=x;i<y;i++) //[x,y)
#define RepD(x,y,i) for (int i=x;i>y;i--) //(y,x]
#define Mem(X) memset(X,0,sizeof(X));
#define Pr(X) cout<<" "<<#X<<"="<<X<<" ";
#define PrL(X) cout<<#X<<" = "<<X<<endl;
#define PrLL cout<<endl;
using namespace std;
const double EPS = 1e-10;
#define IntMod 10000
struct BigInt
{
private:
vector<int> A;
bool Positive;
int VecNum;
public:
//Constructors
inline int GetLength(LL a) {int t=0; while (a>0) {a/=IntMod; t++;} return t;}
BigInt() { Positive = 1; VecNum = 0; }
BigInt(const BigInt &a) { A = a.A; VecNum = a.VecNum; Positive = a.Positive; }
BigInt(string s)
{
A.reserve(100);
if (s == "-0") { A.push_back(0); Positive = 1; VecNum = 1; return; }
int kk = 3; int kl = 0;
int l = s.length();
int j = l - 1;
if (s[0] == '-') { Positive = 0; kk++; kl++; }
else Positive = 1;
while (j>kk)
{
int t = 0;
Rep(0, 4, i) { t *= 10; t += s[j - (3 - i)] - '0'; }
j -= 4;
A.push_back(t);
}
int t = 0;
int k = j + 1;
Rep(kl, k, i) { t *= 10; t += s[i] - '0'; }
A.push_back(t);
VecNum = A.size();
}
BigInt(const LL &b)
{
LL a=b;
Positive=(a>=0);
VecNum=GetLength(abs(a));
A.resize(VecNum);
Rep(0,VecNum,i)
{
A[i]=a%IntMod;
a/=IntMod;
}
}
//BigInt& operator = (BigInt& a) { VecNum=a.VecNum; Positive=a.Positive; A=a.A; }
BigInt& operator = (const string s) { BigInt x(s); *this=x; }
BigInt& operator = (LL s) { BigInt x(s); *this = x; }
//Basic Math Functions
friend BigInt abs(BigInt a) { a.Positive=1; return a; }
friend bool isnegative(BigInt &a) { return a.Positive; }
BigInt& operator - () { Positive = !(Positive); return *this; }
// Ostream and Instream
friend ostream& operator << (ostream &out, BigInt &a)
{
if (a.VecNum==0)
{
out<<"0";
return out;
} //Bug Fixed if there is a BigInt constructed by the default constructor
if (!a.Positive) out << "-";
out << a.A[a.VecNum - 1];
RepD(a.VecNum - 2, -1, i)
{
if (a.A[i] == 0) { out << "0000"; continue; }
Rep(0, (int)(4 - log(a.A[i]) / log(10) - EPS), j) out << '0';
out << a.A[i];
}
return out;
}
friend istream& operator >> (istream &in, BigInt &a)
{
string s;
in >> s;
int L = s.length() - 1;int i = 0;bool flag = 0;
if (s[i] == '-') { i++; flag = 1; }
while (s[i] == '0' && i < L) i++;
string b(s.begin() + i, s.end());
if (flag) b.insert(0, "-");
a = BigInt(b);
return in;
}
//Bool Operators
bool operator < (const BigInt &b) const
{
if (Positive && b.Positive)
{
if (VecNum != b.VecNum) return (bool)(VecNum<b.VecNum);
RepD(VecNum-1, -1, i)
if (A[i] != b.A[i])
return (bool)(A[i]<b.A[i]);
//Bug Fixed that there should be a RepD rather that Rep
return 0;
}
if (!Positive && b.Positive) return 1;
if (Positive && !b.Positive) return 0;
BigInt a = b;
BigInt c = (*this);
if (!c.Positive && !a.Positive) return !((-c) < (-a));
return 1;
}
bool operator > (const BigInt &b) const { return !((*this)<b); }
bool operator == (const BigInt &b) const { return (Positive == b.Positive && A == b.A && VecNum == b.VecNum); }
bool operator <= (const BigInt &b) const { return (*this == b) || (*this < b); }
bool operator >= (const BigInt &b) const { return (*this == b) || !(*this < b);}
bool operator != (const BigInt &b) const { return !(*this == b); }
bool operator < (const string b) const { BigInt x(b); return (*this) < x; }
bool operator == (const string b) const { BigInt x(b); return x==(*this) < x; }
bool operator > (const string b) const
bool operator <= (const string b) const
bool operator >= (const string b) const
bool operator != (const string b) const
bool operator < (const LL& b) const { BigInt x(b); return *this < x; }
bool operator == (const LL& b) const { BigInt x(b); return *this == x; }
bool operator > (const LL& b) const
bool operator <= (const LL& b) const
bool operator >= (const LL& b) const
bool operator != (const LL& b) const
//Function of Plus and Minus
BigInt operator - (const BigInt &b) const
{
BigInt x=*this;
BigInt y=b;
if (!x.Positive && y.Positive) return -(-x + y);
if ( x.Positive && !y.Positive) return x + y;
if (!x.Positive && !y.Positive) return (-y) - (-x);
if (x<y) return -(y - x);
int L = max(x.VecNum, y.VecNum);
y.A.resize(L);
x.A.resize(L);
Rep(0, L, i)
{
x.A[i] -= y.A[i];
if (x.A[i]<0) { x.A[i] += IntMod; x.A[i + 1]--; }
}
while (x.A[L - 1] == 0 && ((L-1)!=0) ) { x.VecNum--; x.A.pop_back(); L--; }
return x;
}
BigInt operator + (const BigInt &b) const
{
BigInt x=*this;
BigInt y=b;
if (!x.Positive && y.Positive) return y - (-x);
if (!x.Positive && !y.Positive) return -(-x + (-y));
if ( x.Positive && !y.Positive) return x - (-y);
int L = max(x.VecNum, y.VecNum);
x.A.resize(L + 1);
y.A.resize(L + 1);
for (int i = 0; i<L; i++) x.A[i] += y.A[i];
for (int i = 0; i<L; i++)
{ x.A[i + 1] += x.A[i] / IntMod; x.A[i] %= IntMod; }
x.VecNum = L;
if (x.A[L]) x.VecNum++;
else x.A.erase(x.A.begin() + L);
return x;
}
BigInt operator - (const LL &b) const { BigInt y(b); return *this - y; }
BigInt operator + (const LL &b) const { BigInt y(b); return *this + y; }
BigInt operator - (const string b) const { BigInt y(b); return *this - y; }
BigInt operator + (const string b) const { BigInt y(b); return *this + y; }
BigInt operator += (const BigInt& b) { *this=*this+b; return *this; }
BigInt operator += (const LL& b) { *this=*this+b; return *this; }
BigInt operator -= (const BigInt& b) { *this=*this-b; return *this; }
BigInt operator -= (const LL& b) { *this=*this-b; return *this; }
//Function of Multiply and Division
BigInt operator * (const BigInt& b) const
{
BigInt x=*this;
BigInt y=b;
BigInt ans;
if ((x.Positive && y.Positive) || (!x.Positive && !y.Positive)) ans.Positive=1;
else ans.Positive=0;
int m=x.VecNum,n=y.VecNum;
int L=m+n+1;
ans.VecNum=L;
ans.A.resize(L+3);
Rep(0,m,i)
Rep(0,n,j)
{
int pos=i+j;
LL t=x.A[i]*y.A[j];
int post=0;
while (t>0)
{
ans.A[pos+post]+=t%IntMod;
t/=IntMod;
post++;
}
}
Rep(0,L,i) { ans.A[i + 1] += ans.A[i] / IntMod; ans.A[i] %= IntMod; }
while (ans.A[L - 1] == 0 && ((L-1)!=0) ) { ans.VecNum--; ans.A.pop_back(); L--; }
return ans;
}
BigInt operator * (const LL &b) const { BigInt x(b); return (*this) * x; }
BigInt operator * (const string b) const { BigInt x(b); return (*this) * x; }
BigInt operator *= (const BigInt& b){ *this = *this * b; return *this; }
BigInt operator *= (const LL &b) { *this = *this * b; return *this; }
BigInt operator *= (const string b) { *this = *this * b; return *this; }
BigInt operator / (const BigInt& b) const
{
}
//Function of Remainder
BigInt operator % (const BigInt& b) const
{
}
//Function of BITS
};
int main()
{
//freopen("D:\\YA.in","r",stdin);
//freopen("mul.in","r",stdin);
//freopen("mul.out","w",stdout);
BigInt a(123456);
BigInt b=(string)"123456";
BigInt c=a*"123456";
cout<<c<<endl;
//BigInt c=a*b;
//cout<<c<<endl;
}
//Equals A/B
struct BigDec:public BigInt
{
private:
BigInt A;
BigInt B;
bool Positive;
public:
BigDec() { Positive=1; A="0"; B="1"; }
BigDec(string s) { }
BigDec(double t) { }
BigDec(LL t) { }
};