-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint128wrapper.cpp
More file actions
158 lines (150 loc) · 2.83 KB
/
int128wrapper.cpp
File metadata and controls
158 lines (150 loc) · 2.83 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
class Bint
{
public:
using value_type = __int128;
private:
value_type value;
public:
constexpr Bint():value(0){}
constexpr Bint(const value_type &val):value(val) {}
constexpr Bint(const long long &val):value(val) {}
constexpr Bint(const int &val):value(val) {}
Bint(const string &s)
{
value = 0;
for (size_t i = 0; i < s.length(); i++)
if ('0' <= s[i] && s[i] <= '9')
value = value * 10 + s[i] - '0';
}
template<typename T>
explicit operator T () const
{
return (T)value;
}
constexpr const Bint operator -() const
{
return Bint(-value);
}
constexpr const Bint operator +(const Bint &rhs) const
{
return Bint(value + rhs.value);
}
constexpr const Bint operator -(const Bint &rhs) const
{
return Bint(value + (-rhs).value);
}
constexpr const Bint operator *(const Bint &rhs) const
{
return Bint(value * rhs.value);
}
constexpr const Bint operator /(const Bint &rhs) const
{
return Bint(value / rhs.value);
}
constexpr const Bint operator %(const Bint &rhs) const
{
return Bint(value % rhs.value);
}
Bint &operator +=(const Bint &rhs)
{
return *this = *this + rhs;
}
Bint &operator -=(const Bint &rhs)
{
return *this = *this - rhs;
}
Bint &operator *=(const Bint &rhs)
{
return *this = *this * rhs;
}
Bint &operator /=(const Bint &rhs)
{
return *this = *this / rhs;
}
Bint &operator %=(const Bint &rhs)
{
return *this = *this % rhs;
}
Bint &operator ++()
{
return *this += 1;
}
Bint operator ++(int)
{
auto tmp = *this;
*this += 1;
return tmp;
}
Bint &operator --()
{
return *this -= 1;
}
Bint operator --(int)
{
auto tmp = *this;
*this -= 1;
return tmp;
}
Bint pow(long long p) const
{
Bint tmp=1, mult=*this;
while(p>0)
{
if((p&1)>0) tmp*=mult;
p>>=1;
mult*=mult;
}
return tmp;
}
constexpr bool operator ==(const Bint &rhs) const
{
return value == rhs.value;
}
constexpr bool operator !=(const Bint &rhs) const
{
return value != rhs.value;
}
constexpr bool operator >(const Bint &rhs) const
{
return value > rhs.value;
}
constexpr bool operator <(const Bint &rhs) const
{
return value < rhs.value;
}
constexpr bool operator >=(const Bint &rhs) const
{
return value >= rhs.value;
}
constexpr bool operator <=(const Bint &rhs) const
{
return value <= rhs.value;
}
string to_s() const
{
if(value==0)
{
return "0";
}
bool minus=value<0;
char str[128];
int ind=0;
Bint tmp(*this);
while(tmp!=0)
{
str[ind++] = abs((int)(tmp%10)) + '0';
tmp/=10;
}
if(minus)
str[ind++]='-';
str[ind]='\0';
for(int i=0;i<ind/2;i++)
swap(str[i], str[ind-1-i]);
return string(str);
}
};
ostream& operator<<(ostream &os, Bint &value)
{
os<<value.to_s();
return os;
}