#include <cassert> #include <cstring> #include <iostream> #include <string> #define TS_DEBUG 0 #if !TS_DEBUG #define NDEBUG #endif const int MAX_DIGITS = 256*1024; class Huge { private: int n; char digits[MAX_DIGITS]; public: Huge() : n(1) { digits[0] = 0; } Huge & operator=(const Huge& src); int length() { return n; } int CompareTo(const Huge& other); int CompareToPrefixOf(const Huge& other); int IsPrefixOf(const Huge& other); void AppendZeros(int zeroCount); void SetToZeroExtended(const Huge& src, int zeroCount); void SetToIncremented(const Huge& src); friend std::istream& operator>>(std::istream& is, Huge& value); friend std::ostream& operator<<(std::ostream& os, const Huge& value); }; Huge & Huge::operator=(const Huge& src) { if (this != &src) { n = src.n; memcpy(digits, src.digits, src.n); } return *this; } int Huge::CompareTo(const Huge& other) { if (n < other.n) { return -1; } else if (n == other.n) { return memcmp(digits, other.digits, n); } else { return 1; } } int Huge::CompareToPrefixOf(const Huge& other) { assert(n <= other.n); return memcmp(digits, other.digits, n); } int Huge::IsPrefixOf(const Huge& other) { return n <= other.n && CompareToPrefixOf(other) == 0; } void Huge::AppendZeros(int zeroCount) { assert(0 <= zeroCount); for (int i = 0; i < zeroCount; i++) { digits[n++] = 0; } } void Huge::SetToZeroExtended(const Huge& src, int zeroCount) { n = src.n + zeroCount; memcpy(digits, src.digits, src.n); memset(&digits[src.n], 0, zeroCount); } void Huge::SetToIncremented(const Huge& src) { n = src.n; int c = 1; for (int i = src.n - 1; i >= 0; i--) { int s = src.digits[i] + c; int d = s < 10 ? s : s - 10; c = s < 10 ? 0 : 1; digits[i] = d; } if (c > 0) { memmove(&digits[1], &digits[0], n); digits[0] = 1; n++; } } std::istream& operator>>(std::istream& is, Huge& value) { std::string input; is >> input; int m = input.length(); int i = 0; while (i < m && (isspace(input[i]) || input[i] == '0')) { i++; } int n = 0; while (i < m && isdigit(input[i])) { value.digits[n] = input[i] - '0'; n++; i++; } value.n = n; return is; } std::ostream& operator<<(std::ostream& os, const Huge& value) { for (int i = 0; i < value.n; i++) { char c = '0' + value.digits[i]; os << c; } return os; } void debug1(const Huge& x, const Huge& x1) { #if TS_DEBUG std::cerr << "[" << x << "] -> [" << x1 << "]\n"; #endif } void debug2(const char* name, const Huge& x) { #if TS_DEBUG std::cerr << name << " = [" << x << "]\n"; #endif } int main() { long long result = 0; int n; std::cin >> n; Huge a; Huge b; Huge b1; Huge d; std::cin >> a; debug1(a, a); for (int i = 1; i < n; i++) { std::cin >> b; int cmp = b.CompareTo(a); if (cmp > 0) { b1 = b; } else if (cmp == 0) { b1.SetToZeroExtended(b, 1); } else { assert(cmp < 0); int lenDiff = a.length() - b.length(); if (lenDiff == 0) { b1.SetToZeroExtended(b, 1); } else { int cmpPre = b.CompareToPrefixOf(a); if (cmpPre > 0) { b1.SetToZeroExtended(b, lenDiff); } else if (cmpPre == 0) { d.SetToIncremented(a); debug2("d", d); if (b.IsPrefixOf(d)) { b1 = d; } else { b1.SetToZeroExtended(b, lenDiff + 1); } } else { b1.SetToZeroExtended(b, lenDiff + 1); } } } debug1(b, b1); assert(b1.CompareTo(a) > 0); assert(b.IsPrefixOf(b1)); result += b1.length() - b.length(); a = b1; } std::cout << result << '\n'; return 0; }
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 | #include <cassert> #include <cstring> #include <iostream> #include <string> #define TS_DEBUG 0 #if !TS_DEBUG #define NDEBUG #endif const int MAX_DIGITS = 256*1024; class Huge { private: int n; char digits[MAX_DIGITS]; public: Huge() : n(1) { digits[0] = 0; } Huge & operator=(const Huge& src); int length() { return n; } int CompareTo(const Huge& other); int CompareToPrefixOf(const Huge& other); int IsPrefixOf(const Huge& other); void AppendZeros(int zeroCount); void SetToZeroExtended(const Huge& src, int zeroCount); void SetToIncremented(const Huge& src); friend std::istream& operator>>(std::istream& is, Huge& value); friend std::ostream& operator<<(std::ostream& os, const Huge& value); }; Huge & Huge::operator=(const Huge& src) { if (this != &src) { n = src.n; memcpy(digits, src.digits, src.n); } return *this; } int Huge::CompareTo(const Huge& other) { if (n < other.n) { return -1; } else if (n == other.n) { return memcmp(digits, other.digits, n); } else { return 1; } } int Huge::CompareToPrefixOf(const Huge& other) { assert(n <= other.n); return memcmp(digits, other.digits, n); } int Huge::IsPrefixOf(const Huge& other) { return n <= other.n && CompareToPrefixOf(other) == 0; } void Huge::AppendZeros(int zeroCount) { assert(0 <= zeroCount); for (int i = 0; i < zeroCount; i++) { digits[n++] = 0; } } void Huge::SetToZeroExtended(const Huge& src, int zeroCount) { n = src.n + zeroCount; memcpy(digits, src.digits, src.n); memset(&digits[src.n], 0, zeroCount); } void Huge::SetToIncremented(const Huge& src) { n = src.n; int c = 1; for (int i = src.n - 1; i >= 0; i--) { int s = src.digits[i] + c; int d = s < 10 ? s : s - 10; c = s < 10 ? 0 : 1; digits[i] = d; } if (c > 0) { memmove(&digits[1], &digits[0], n); digits[0] = 1; n++; } } std::istream& operator>>(std::istream& is, Huge& value) { std::string input; is >> input; int m = input.length(); int i = 0; while (i < m && (isspace(input[i]) || input[i] == '0')) { i++; } int n = 0; while (i < m && isdigit(input[i])) { value.digits[n] = input[i] - '0'; n++; i++; } value.n = n; return is; } std::ostream& operator<<(std::ostream& os, const Huge& value) { for (int i = 0; i < value.n; i++) { char c = '0' + value.digits[i]; os << c; } return os; } void debug1(const Huge& x, const Huge& x1) { #if TS_DEBUG std::cerr << "[" << x << "] -> [" << x1 << "]\n"; #endif } void debug2(const char* name, const Huge& x) { #if TS_DEBUG std::cerr << name << " = [" << x << "]\n"; #endif } int main() { long long result = 0; int n; std::cin >> n; Huge a; Huge b; Huge b1; Huge d; std::cin >> a; debug1(a, a); for (int i = 1; i < n; i++) { std::cin >> b; int cmp = b.CompareTo(a); if (cmp > 0) { b1 = b; } else if (cmp == 0) { b1.SetToZeroExtended(b, 1); } else { assert(cmp < 0); int lenDiff = a.length() - b.length(); if (lenDiff == 0) { b1.SetToZeroExtended(b, 1); } else { int cmpPre = b.CompareToPrefixOf(a); if (cmpPre > 0) { b1.SetToZeroExtended(b, lenDiff); } else if (cmpPre == 0) { d.SetToIncremented(a); debug2("d", d); if (b.IsPrefixOf(d)) { b1 = d; } else { b1.SetToZeroExtended(b, lenDiff + 1); } } else { b1.SetToZeroExtended(b, lenDiff + 1); } } } debug1(b, b1); assert(b1.CompareTo(a) > 0); assert(b.IsPrefixOf(b1)); result += b1.length() - b.length(); a = b1; } std::cout << result << '\n'; return 0; } |