Repository navigation
Expand file tree
/
Copy pathBigInteger.cpp
More file actions
161 lines (161 loc) · 3.24 KB
/
Copy pathBigInteger.cpp
File metadata and controls
161 lines (161 loc) · 3.24 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
// from: www.github.com/kzvd4729
/************* Big Integer ***************/
string NumtoString(long x)
{
string ret;
while(x)
{
ret.push_back((char)(x%10+'0'));
x/=10;
}
reverse(ret.begin(),ret.end());
if(ret.size()==0)ret.push_back('0');
return ret;
}
long StringtoNum(string s)
{
long ret=0;
for(auto x:s)
ret=ret*10+(x-'0');
return ret;
}
int Compare(string a,string b)
{
int x=a.size(),y=b.size();
if(x<y)return -1;if(x>y)return 1;
for(int i=0;i<x;i++)
{
if(a[i]<b[i])return -1;
if(a[i]>b[i])return 1;
}
return 0;
}
string Abs(string s)
{
if(s[0]=='-')
s.erase(s.begin());
return s;
}
string Add(string a,string b)
{
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
int x=a.size(),y=b.size();
while(x<y)a.push_back('0'),x++;
while(y<x)b.push_back('0'),y++;
int c=0;string r;
for(int i=0;i<x;i++)
{
int x=c+a[i]-'0'+b[i]-'0';
c=x/10;x%=10;
r.push_back((char)(x+'0'));
}
if(c)r.push_back((char)(c+'0'));
reverse(r.begin(),r.end());return r;
}
string Sub(string a,string b)
{
//assuming a>=b;
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
int x=a.size(),y=b.size();
while(y<x)b.push_back('0'),y++;
int c=0;string r;
for(int i=0;i<x;i++)
{
int x=(a[i]-'0')-(b[i]-'0'+c);
if(x<0)x+=10,c=1;
else c=0;
r.push_back((char)(x+'0'));
}
while(r.size()>1)
{
if(r.back()=='0')r.pop_back();
else break;
}
reverse(r.begin(),r.end());return r;
}
string Mul(string a,string b)
{
int x=a.size(),y=b.size();string ret="0";
for(int i=x-1;i>=0;i--)
{
int c=0;string tmp;
for(int j=i;j<x-1;j++)tmp.push_back('0');
for(int j=y-1;j>=0;j--)
{
int p=a[i]-'0',q=b[j]-'0';
int xx=p*q+c;c=xx/10;
tmp.push_back((char)(xx%10+'0'));
}
if(c)tmp.push_back((char)(c+'0'));
reverse(tmp.begin(),tmp.end());
ret=Add(ret,tmp);
}
reverse(ret.begin(),ret.end());
while(ret.size()>1)
{
if(ret.back()=='0')ret.pop_back();
else break;
}
reverse(ret.begin(),ret.end());
return ret;
}
string Mul(string a,long b)
{
//b should be <= 1e17
int x=a.size();string ret;long c=0;
for(int i=x-1;i>=0;i--)
{
int z=a[i]-'0';c+=z*b;
ret.push_back((char)(c%10)+'0');
c/=10;
}
while(c)
{
ret.push_back((char)(c%10)+'0');
c/=10;
}
reverse(ret.begin(),ret.end());
return ret;
}
pair<string,string>Div(string a,string b)
{
//if(b=="0")assert(0);
if(Compare(a,b)<0)return {"0",a};
int x=a.size(),y=b.size();
string rm="0",rt;int i=0;
while(i<x)
{
if(rm=="0")rm.clear();int cnt=0;
while(true)
{
if(i>=x)break;
if(a[i]!='0')rm.push_back(a[i]);
else if(rm.size())rm.push_back(a[i]);
cnt++;i++;
if(rm.size()>=b.size())
if(Compare(rm,b)>=0)break;
if(rt.size())rt.push_back('0');
}
if(Compare(rm,b)>=0)
{
for(int k=9;k>=1;k--)
{
string tmp=Mul(b,NumtoString(k));
if(Compare(rm,tmp)>=0)
{
rt.push_back((char)(k+'0'));
rm=Sub(rm,tmp);
break;
}
}
}
}
if(rm.size()==0)rm.push_back('0');
return {rt,rm};
}
//Assuming all numbers are positive
//Careful for negative numbers
//Also not much optimized
///////////////////////////////////////////