留做备用。
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 | struct bigInt { //For Non-negative Numbers // You must include header **iostream**,**algorithm** and **string** before using this class! private: string v; static string & bIsort(string & v) { //each char has reduced '0' for (int i = 0; i < v.length(); i++) { if (v[i] > 9) { if (i == v.length() - 1)v.append(1, v[i] / 10); else v[i + 1] += v[i] / 10; v[i] %= 10; } } return v; } static string lng2str(long long v) { string s = ""; while (v) { s.append(1, v % 10 + '0'); v /= 10; } reverse(s.begin(), s.end()); return s; } static string& trimPre0(string& v3) { while (v3.length() != 0 && (*v3.begin()) == '0')v3.erase(v3.begin()); if (v3.length() == 0)v3 = "0"; return v3; } public: bigInt() { v = "0"; } bigInt(long long n) { v = lng2str(n); } bigInt(string s) { v = s; } bigInt& operator = (const bigInt & bI) { this->v = bI.v; return *this; } bigInt operator + (bigInt bI2) const { string s1 = v, s2 = bI2.v; if (s1.length() > s2.length())swap(s1, s2); reverse(s1.begin(), s1.end()); reverse(s2.begin(), s2.end()); int s1len = s1.length(), s2len = s2.length(); for (int i = 0; i < s1len; i++)s2[i] += s1[i] - '0'; for (int i = 0; i < s2len - 1; i++)if (s2[i] > '9') { s2[i + 1] += (s2[i] - '0') / 10; s2[i] = '0' + (s2[i] - '0') % 10; } if (s2[s2len - 1] > '9') { s2.append(1, '0' + (s2[s2len - 1] - '0') / 10); s2[s2len - 1] = '0' + (s2[s2len - 1] - '0') % 10; } reverse(s2.begin(),s2.end()); return bigInt(s2); } bigInt operator - (bigInt bI2) const { string s1, s2; if ((*this) < bI2) { s1 = v; s2 = bI2.v; } else { s1 = bI2.v; s2 = v; } reverse(s1.begin(), s1.end()); reverse(s2.begin(), s2.end()); int s1len = s1.length(), s2len = s2.length(); for (int i = 0; i<s1len; i++)s2[i] -= s1[i] - '0'; for (int i = 0; i<s2len - 1; i++)if (s2[i]<'0') { s2[i + 1] -= 1; s2[i] += 10; } if (s2[s2len - 1] == '0')s2.erase(s2.end() - 1); reverse(s2.begin(), s2.end()); return bigInt(trimPre0(s2)); } bigInt operator * (bigInt bI2) const { string v1 = v, v2 = bI2.v; if (v2.length() > v1.length())swap(v1, v2); reverse(v1.begin(), v1.end()); reverse(v2.begin(), v2.end()); string v3 = ""; for (int i = 0; i < v1.length(); i++)v1[i] -= '0'; for (int i = 0; i < v2.length(); i++)v2[i] -= '0'; v3.resize(v1.length() + v2.length(), 0); for (int i = 0; i < v2.length(); i++) { for (int j = 0; j < v1.length(); j++) { v3[i + j] += v2[i] * v1[j]; } bIsort(v3); } for (int i = 0; i < v3.length(); i++)v3[i] += '0'; reverse(v3.begin(), v3.end()); trimPre0(v3); return bigInt(v3); } bigInt operator / (long long bI2) const { string v1 = v; for (int i = 0; i < v1.length(); i++)v1[i] -= '0'; long long v2 = bI2; string v3 = ""; long long div = 0; for (int i = 0; i < v1.length(); i++) { div *= 10; div += v1[i]; if (div < v2) { v3.append(1, '0'); continue; } v3.append(lng2str(div / v2)); div %= v2; } return bigInt(trimPre0(v3)); } bool operator < (bigInt bI2) const { if (v.length() != bI2.v.length())return v.length() < bI2.v.length(); for (int i = 0; i < v.length(); i++) { if (v[i] != bI2.v[i])return v[i] < bI2.v[i]; } return false; } friend istream& operator >> (istream& in, bigInt& bI) { in >> bI.v; return in; } friend ostream& operator << (ostream& out, bigInt& bI) { out << bI.v; return out; } }; |
Update 20180924:
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 | class bigInt { /* This class is capable of calculating big non-negative numbers. You must include header files **iostream**,**algorithm** and **string** before using this class! */ private: string v; static string & bIsort(string & v) { //each char has reduced '0' for (int i = 0; i < v.length(); i++) { if (v[i] > 9) { if (i == v.length() - 1)v.append(1, v[i] / 10); else v[i + 1] += v[i] / 10; v[i] %= 10; } } return v; } static string lng2str(unsigned long long v) { string s = ""; while (v) { s.append(1, v % 10 + '0'); v /= 10; } reverse(s.begin(), s.end()); return s; } static string & trimPre0(string & v3) { while (v3.length() != 0 && (*v3.begin()) == '0')v3.erase(v3.begin()); if (v3.length() == 0)v3 = "0"; return v3; } public: bigInt() { v = "0"; } bigInt(long long n) { v = lng2str(n); } bigInt(string s) { v = s; } bigInt & operator = (const bigInt & bI) { this->v = bI.v; return *this; } bigInt operator + (const bigInt & bI2) const { string s1 = v, s2 = bI2.v; if (s1.length() > s2.length())swap(s1, s2); reverse(s1.begin(), s1.end()); reverse(s2.begin(), s2.end()); int s1len = s1.length(), s2len = s2.length(); for (int i = 0; i < s1len; i++)s2[i] += s1[i] - '0'; for (int i = 0; i < s2len - 1; i++)if (s2[i] > '9') { s2[i + 1] += (s2[i] - '0') / 10; s2[i] = '0' + (s2[i] - '0') % 10; } if (s2[s2len - 1] > '9') { s2.append(1, '0' + (s2[s2len - 1] - '0') / 10); s2[s2len - 1] = '0' + (s2[s2len - 1] - '0') % 10; } reverse(s2.begin(), s2.end()); return bigInt(s2); } bigInt operator - (const bigInt & bI2) const { string s1, s2; if ((*this) < bI2) { s1 = v; s2 = bI2.v; } else { s1 = bI2.v; s2 = v; } reverse(s1.begin(), s1.end()); reverse(s2.begin(), s2.end()); int s1len = s1.length(), s2len = s2.length(); for (int i = 0; i<s1len; i++)s2[i] -= s1[i] - '0'; for (int i = 0; i<s2len - 1; i++)if (s2[i]<'0') { s2[i + 1] -= 1; s2[i] += 10; } if (s2[s2len - 1] == '0')s2.erase(s2.end() - 1); reverse(s2.begin(), s2.end()); return bigInt(trimPre0(s2)); } bigInt operator * (const bigInt & bI2) const { string v1 = v, v2 = bI2.v; if (v2.length() > v1.length())swap(v1, v2); reverse(v1.begin(), v1.end()); reverse(v2.begin(), v2.end()); string v3 = ""; for (int i = 0; i < v1.length(); i++)v1[i] -= '0'; for (int i = 0; i < v2.length(); i++)v2[i] -= '0'; v3.resize(v1.length() + v2.length(), 0); for (int i = 0; i < v2.length(); i++) { for (int j = 0; j < v1.length(); j++) { v3[i + j] += v2[i] * v1[j]; } bIsort(v3); } for (int i = 0; i < v3.length(); i++)v3[i] += '0'; reverse(v3.begin(), v3.end()); trimPre0(v3); return bigInt(v3); } bigInt operator / (const unsigned long long & bI2) const { string v1 = v; for (int i = 0; i < v1.length(); i++)v1[i] -= '0'; long long v2 = bI2; string v3 = ""; long long div = 0; for (int i = 0; i < v1.length(); i++) { div *= 10; div += v1[i]; if (div < v2) { v3.append(1, '0'); continue; } v3.append(lng2str(div / v2)); div %= v2; } return bigInt(trimPre0(v3)); } unsigned long long operator % (const unsigned long long & bI2) const { string v1 = v; for (int i = 0; i < v1.length(); i++)v1[i] -= '0'; unsigned long long v2 = bI2; unsigned long long div = 0; for (int i = 0; i < v1.length(); i++) { div *= 10; div += v1[i]; if (div < v2)continue; div %= v2; } return div % v2; } bigInt & operator += (const bigInt & bI2) { *this = *this + bI2; return *this; } bigInt & operator -= (const bigInt & bI2) { *this = *this - bI2; return *this; } bigInt & operator *= (const bigInt & bI2) { *this = *this * bI2; return *this; } bool operator < (const bigInt & bI2) const { if (v.length() != bI2.v.length())return v.length() < bI2.v.length(); for (int i = 0; i < v.length(); i++) { if (v[i] != bI2.v[i])return v[i] < bI2.v[i]; } return false; } bool operator == (const bigInt & bI2) const { return v == bI2.v; } friend istream& operator >> (istream& in, bigInt & bI) { in >> bI.v; return in; } friend ostream& operator << (ostream& out, bigInt & bI) { out << bI.v; return out; } }; |
20181121更新:(高精加法、高精减法、高精乘法、高精除单精(商、模)、高精除高精(商、模)全部测试通过,Final Version)
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 | #include<iostream> #include<algorithm> #include<cstdio> #include<vector> class bigInt { /* This class is capable of calculating big non-negative numbers of any length. You must include header files **iostream**,**algorithm**,**cstdio** and **vector** before using this class! */ typedef vector<int> Container; private: Container v; //Every Digit is stored in this vector in the reverse order static Container & bIsort(Container & v) { for (int i = 0; i < (int)v.size(); i++) { if (v[i] > 9) { if (i == (int)v.size() - 1)v.push_back(v[i] / 10); else v[i + 1] += v[i] / 10; v[i] %= 10; } } return v; } static Container lng2vec(unsigned long long v) { Container c; while (v) { c.push_back(v % 10); v /= 10; } return c; } static Container & trimPre0(Container & v) { int cnt = 0; int i = v.size() - 1; while (i >= 0 && v[i] == 0)i--, cnt++; v.resize(v.size() - cnt); if (v.size() == 0)v.push_back(0); return v; } static Container easyDiv(bigInt & bI1, const bigInt & bI2) { unsigned long long cnt = 0; while (bI1 >= bI2) { bI1 -= bI2; cnt++; } return lng2vec(cnt); } public: bigInt() { v.clear(); } bigInt(long long n) { v = lng2vec(n); trimPre0(v); } bigInt(Container c) { v = c; trimPre0(v); } bigInt(string s) { for (int i = s.size() - 1; i >= 0; i--)v.push_back(s[i] - '0'); trimPre0(v); } bigInt & operator = (const bigInt & bI) { this->v = bI.v; return *this; } bool operator < (const bigInt & bI2) const { if (v.size() != bI2.v.size())return v.size() < bI2.v.size(); for (int i = v.size() - 1; i >= 0; i--) { if (v[i] != bI2.v[i])return v[i] < bI2.v[i]; } return false; } bool operator > (const bigInt & bI2) const { if (v.size() != bI2.v.size())return v.size() > bI2.v.size(); for (int i = v.size() - 1; i >= 0; i--) { if (v[i] != bI2.v[i])return v[i] > bI2.v[i]; } return false; } bool operator <= (const bigInt & bI2) const { return (*this)<bI2 || (*this)==bI2; } bool operator >= (const bigInt & bI2) const { return (*this)>bI2 || (*this)==bI2; } bool operator == (const bigInt & bI2) const { return v == bI2.v; } bigInt & operator += (const bigInt & bI2) { Container & s1 = v; const Container & s2 = bI2.v; if (s1.size() < s2.size())s1.resize(s2.size()); int s1Len = s1.size(), s2Len = s2.size(); for (int i = 0; i < s2Len; i++)s1[i] += s2[i]; for (int i = 0; i < s1Len - 1; i++)if (s1[i] > 9) { s1[i + 1] += s1[i] / 10; s1[i] %= 10; } if (s1[s1Len - 1] > 9) { s1.push_back(s1[s1Len - 1] / 10); s1[s1Len - 1] %= 10; } return *this; } bigInt operator + (const bigInt & bI2) const { bigInt bI1 = *this; bI1 += bI2; return bI1; } bigInt & operator -= (const bigInt & bI2) { Container & s1 = v; const Container & s2 = bI2.v; bool flag = false; if ((*this) < bI2)flag = true, s1.resize(s2.size()); int s1Len = s1.size(), s2Len = s2.size(); for (int i = 0; i < s2Len; i++)s1[i] -= s2[i]; if (flag)for (int i = 0; i < s2Len; i++)s1[i] = -s1[i]; for (int i = 0; i < s1Len - 1; i++)if (s1[i] < 0) { s1[i + 1] -= 1; s1[i] += 10; } trimPre0(s1); return *this; } bigInt operator - (const bigInt & bI2) const { bigInt bI1 = *this; bI1 -= bI2; return bI1; } bigInt operator * (const bigInt & bI2) const { const Container & v1 = v; const Container & v2 = bI2.v; Container v3; v3.resize(v1.size() + v2.size(), 0); for (int i = 0; i < (int)v2.size(); i++) { for (int j = 0; j < (int)v1.size(); j++) { v3[i + j] += v2[i] * v1[j]; } } bIsort(v3); return bigInt(trimPre0(v3)); } bigInt & operator *= (const bigInt & bI2) { *this = (*this)*bI2; return *this; } bigInt operator / (const unsigned long long & bI2) const { const Container & v1 = v; long long v2 = bI2; Container v3; long long div = 0; for (int i = v1.size() - 1; i >= 0; i--) { div *= 10; div += v1[i]; if (div < v2) { v3.push_back(0); continue; } Container tmp = lng2vec(div / v2); v3.insert(v3.end(), tmp.begin(), tmp.end()); div %= v2; } reverse(v3.begin(), v3.end()); return bigInt(trimPre0(v3)); } unsigned long long operator % (const unsigned long long & bI2) const { const Container & v1 = v; unsigned long long v2 = bI2; unsigned long long div = 0; for (int i = v1.size() - 1; i >= 0; i--) { div *= 10; div += v1[i]; if (div < v2)continue; div %= v2; } return div % v2; } bigInt operator / (const bigInt & bI2) const { const Container & v1 = v; const Container & v2 = bI2.v; Container v3; bigInt div = 0; for (int i = v1.size() - 1; i >= 0; i--) { div *= 10; div += v1[i]; if (div < v2) { v3.push_back(0); continue; } Container tmp = easyDiv(div, v2); v3.insert(v3.end(), tmp.begin(), tmp.end()); } reverse(v3.begin(), v3.end()); return bigInt(trimPre0(v3)); } bigInt & operator /= (const bigInt & bI2) { *this = (*this)/bI2; return *this; } bigInt operator % (const bigInt & bI2) const { const Container & v1 = v; const Container & v2 = bI2.v; bigInt div = 0; for (int i = v1.size() - 1; i >= 0; i--) { div *= 10; div += v1[i]; if (div < v2)continue; easyDiv(div, v2); } return div; } bigInt & operator %= (const bigInt & bI2) { *this = (*this) % bI2; return *this; } friend istream& operator >> (istream& in, bigInt & bI) { string s; in >> s; bI.v.clear(); for (int i = s.size() - 1; i >= 0; i--) bI.v.push_back(s[i] - '0'); trimPre0(bI.v); return in; } friend ostream& operator << (ostream& out, bigInt & bI) { for (int i = bI.v.size() - 1; i >= 0; i--) out << (char)(bI.v[i] + '0'); return out; } void scan() { v.clear(); scanf(" "); char c; do { c = getchar(); if (c <= '9'&&c >= '0')v.push_back(c - '0'); else { ungetc(c, stdin); break; } } while (1); reverse(v.begin(), v.end()); trimPre0(v); } void print() { for (int i = v.size() - 1; i >= 0; i--) { printf("%d", v[i]); } } }; |