5#include <unordered_set>
10 if(this->
nexts[str[0] -
'a'] ==
nullptr) {
13 if(str.length() == 1) {
14 this->
nexts[str[0] -
'a']->end_of_word =
true;
15 this->
nexts[str[0] -
'a']->count++;
17 this->
nexts[str[0] -
'a']->insert(str.substr(1));
22 for(
const auto *next: this->
nexts) {
32 : positive(positive) {
33 int end_i =
vec.size() - 1;
34 while(end_i >= 0 &&
vec[end_i] == 0) {
37 this->vec = vector<unsigned short>();
38 for(
int i = 0; i <= end_i; i++) {
39 this->vec.push_back(
vec[i]);
41 if(this->vec.empty()) {
42 this->vec.push_back(0);
51 vec = vector<unsigned short>();
56 vec.push_back(ch -
'0');
65 vec = vector<unsigned short>();
70 vec.push_back(ch -
'0');
79 vec = vector<unsigned short>();
84 vec.push_back(ch -
'0');
93 vec = vector<unsigned short>();
95 vec.push_back(n % 10);
101 vec = vector<unsigned short>();
103 vec.push_back(n % 10);
109 vec = vector<unsigned short>();
111 vec.push_back(n % 10);
117 vec = vector<unsigned short>();
119 vec.push_back(n % 10);
125 vec = vector<unsigned short>();
127 vec.push_back(n % 10);
133 int start_i = str.length() - 1;
134 while(start_i >= 0 && isdigit(str[start_i]) != 0) {
135 vec.push_back(str[start_i] -
'0');
144 int start_i = strlen(str) - 1;
145 while(start_i >= 0 && isdigit(str[start_i]) != 0) {
146 vec.push_back(str[start_i] -
'0');
155 int end_i = bi.
vec.size() - 1;
156 while(end_i >= 0 && bi.
vec[end_i] == 0) {
159 this->
vec = vector<unsigned short>();
160 for(
int i = 0; i <= end_i; i++) {
161 this->
vec.push_back(bi.
vec[i]);
163 if(this->
vec.empty()) {
164 this->
vec.push_back(0);
176 vector<unsigned short> v;
177 unsigned short carry = 0;
178 for(
long long i = 0; i < max(this->
get_size(), bi.
get_size()) || carry != 0; i++) {
179 const unsigned short this_num = i < this->
get_size() ? (*this)[i] : 0;
180 const unsigned short bi_num = i < bi.
get_size() ? bi[i] : 0;
181 unsigned short sum = this_num + bi_num;
199 return -(bi - *
this);
201 vector<unsigned short> v;
202 unsigned short borrow = 0;
204 short this_num = i < this->
get_size() ? (*this)[i] : 0;
205 const short bi_num = i < bi.
get_size() ? bi[i] : 0;
207 short diff = this_num - bi_num;
215 auto ret =
BigInt(v,
true);
220 vector<unsigned short> res;
221 unsigned short c = 0;
222 for(
const auto v: this->
vec) {
223 unsigned short num = n * v + c;
226 res.emplace_back(num);
229 res.emplace_back(c % 10);
236 unsigned max_len = 0;
237 vector<vector<unsigned short>> vecs(bi.
vec.size());
238 for(
int i = 0; i < bi.
vec.size(); i++) {
239 vector<unsigned short> vi(i, 0);
240 vector<unsigned short> ai = *
this * bi.
vec[i];
241 vi.insert(vi.end(), ai.begin(), ai.end());
243 max_len = max(max_len,
static_cast<unsigned>(vi.size()));
245 vector<unsigned short> ans;
246 unsigned short c = 0;
247 for(
int i = 0; i < max_len; i++) {
249 for(
int j = 0; j < vecs.size(); j++) {
250 v += i < vecs[j].size() ? vecs[j][i] : 0;
258 ans.emplace_back(c % 10);
279 for(
int i =
get_size() - 1; i >= 0; --i) {
280 if((*
this)[i] > bi[i]) {
283 if((*
this)[i] < bi[i]) {
286 if((*
this)[i] < bi[i]) {
303 return !(*
this >= bi);
308 return this->
vec == bi.
vec;
315 return this->
vec != bi.
vec;
353 for(
auto it = bi.
vec.rbegin(); it != bi.
vec.rend(); ++it) {
385 vec = vector<unsigned short>();
418 numerator1 = -numerator1;
421 numerator2 = -numerator2;
430 numerator1 = -numerator1;
433 numerator2 = -numerator2;
442 numerator1 = -numerator1;
445 numerator2 = -numerator2;
479 this->
parent = vector<int>(n);
480 this->
rank = vector<int>(n);
481 this->
size = vector<int>(n);
482 for(
int i = 0; i < n; i++) {
519 unordered_set<int> us;
520 for(
int i = 0; i <
size.size(); i++) {
529 mat = vector<vector<int>>(m.
mat.size(), vector<int>(m.
mat.size(), 0));
530 for(
int i = 0; i < m.
mat.size(); i++) {
531 for(
int j = 0; j < m.
mat.size(); j++) {
539 for(
int i = 0; i <
mat.size(); i++) {
540 for(
int j = 0; j <
mat.size(); j++) {
541 for(
int k = 0; k <
mat.size(); k++) {
542 ret.
mat[i][j] +=
mat[i][k] * m.
mat[k][j];
559 for(
int i = 0; i < n; i++) {
ostream & operator<<(ostream &os, const BigInt &bi)
istream & operator>>(istream &is, const BigInt &)
array< TrieNode *, 26 > nexts
void insert(const string &str)
bool operator>(const BigInt &bi) const
BigInt operator+(const BigInt &bi) const
vector< unsigned short > operator*(unsigned short n) const
unsigned short operator[](unsigned long) const
unsigned long get_size() const
BigInt & operator*=(const BigInt &bi)
bool operator<(const BigInt &bi) const
BigInt & operator-=(const BigInt &bi)
bool operator!=(const BigInt &bi) const
BigInt operator/(const BigInt &bi) const
BigInt operator%(const BigInt &bi) const
bool operator<=(const BigInt &bi) const
bool operator>=(const BigInt &bi) const
vector< unsigned short > vec
BigInt & operator/=(const BigInt &bi)
BigInt & operator+=(const BigInt &bi)
bool operator==(const BigInt &bi) const
BigInt & operator%=(const BigInt &bi)
Fraction operator+(const Fraction &f) const
Fraction(bool positive, long long numerator, long long denominator)
Fraction operator-(const Fraction &f) const
unsigned long long get_denominator() const
unsigned long long get_numerator() const
unsigned long long denominator
分母
Fraction operator/(const Fraction &f) const
unsigned long long numerator
分子
Fraction operator*(const Fraction &f) const
vector< vector< int > > mat
Matrix operator*(const Matrix &m) const
vector< int > & operator[](int i)
static Matrix identity(int n)