#include <cstdio> #include <algorithm> #include <cstring> #include <cmath> long long POW10[19]; short LENGTHS[200001] = {0}; inline short read(long long *n) { register char c=0; short count = 0; while (c<33) c=getc_unlocked(stdin); (*n)=0; while(c>32){ (*n)=(*n)*10+(c-'0'); c=getc_unlocked(stdin); count++; } return count; } long long get_number_length(long long a) { // if (a < 200001) { // return LENGTHS[a]; // } long long count = 0; while (a) { count++; a /= 10; } return count; } void build_powers() { long long temp = 1; for (long long i = 0; i < 19; i++) { POW10[i] = temp; temp *=10; } } struct BigNum { // prefix * 10^zeroes + suffix long long prefix; long long prefix_length; long long zeroes; long long suffix; long long suffix_length; BigNum(long long value) { this->prefix_length = 0; this->zeroes = 0; this->suffix = 0; this->suffix_length = 0; this->set_prefix(value); } void set_prefix(long long value) { this->prefix = value; this->prefix_length = get_number_length(this->prefix); if (!value) { this->prefix_length = 0; } } void change_prefix(long long value) { long long value_length = get_number_length(value); this->zeroes -= value_length - this->prefix_length; this->set_prefix(value); } long long get_length() const { if (!this->prefix_length && !this->zeroes && !this->suffix) { return 1; } if (this->zeroes < 9 && this->suffix_length >= this->zeroes) { return get_number_length(this->prefix * POW10[this->zeroes] + this->suffix); } return this->prefix_length + this->zeroes; } bool operator < (const BigNum &y) { // x < y BigNum result = (*this - y); if (result.zeroes < 9) { return result.prefix * POW10[result.zeroes] + result.suffix < 0; } else if (result.prefix < 0 || (result.prefix == 0 && result.suffix < 0)) { // printf("%d\n", result.prefix); return true; } else { return false; } } bool operator == (const BigNum &y) { // x == y BigNum result = (*this - y); if (result.zeroes < 9) { return result.prefix * POW10[result.zeroes] + result.suffix == 0; } else if (result.prefix == 0 && result.suffix == 0) { return true; } else { return false; } } BigNum operator + (const BigNum &y) { // zeroes = x.zeroes = y.zeroes // (x.prefix * 10^x.zeroes + x.suffix) + (y.prefix * 10^y.zeroes + y.suffix) = // (x.prefix + y.prefix) * 10^zeroes + x.suffix + y.suffix BigNum result = BigNum(0); result.zeroes = this->zeroes; result.set_prefix(this->prefix + y.prefix); result.set_suffix(this->suffix + y.suffix); return result; } BigNum operator - (const BigNum &y) { // zeroes = x.zeroes = y.zeroes // (x.prefix * 10^x.zeroes + x.suffix) - (y.prefix * 10^y.zeroes + y.suffix) = // (x.prefix - y.prefix) * 10^zeroes + x.suffix - y.suffix BigNum result = BigNum(0); result.zeroes = this->zeroes; result.set_prefix(this->prefix - y.prefix); result.set_suffix(this->suffix - y.suffix); return result; } long long get_normalized_prefix(long long characters) const { if (characters <= this->prefix_length) { return this->prefix; } return this->prefix * POW10[characters - this->prefix_length]; } void set_suffix(long long new_suffix) { if (new_suffix) { long long new_suffix_length = get_number_length(new_suffix); this->suffix = new_suffix; this->suffix_length = new_suffix_length; } } void print() { printf(">pref: %d ", prefix); for (long long i = 0; i < this->zeroes - this->suffix_length; i++) { printf("%d", 0); } if (this->suffix_length) { printf("%d", suffix); } puts("<"); } void set_zeroes(long long zeroes) { this->set_prefix(this->prefix * POW10[this->zeroes - zeroes]); this->zeroes = zeroes; this->set_suffix(this->suffix); } }; int main() { long long n; long long result = 0; long long _last_number; // LENGTHS[0] = 1; // LENGTHS[10] = 2; // LENGTHS[100] = 3; // LENGTHS[1000] = 4; // LENGTHS[10000] = 6; // LENGTHS[100000] = 7; // for (long long i = 1; i < 200001; i++) { // if (!LENGTHS[i]) { // LENGTHS[i] = LENGTHS[i - 1]; // } // } build_powers(); read(&n); read(&_last_number); BigNum last_number = BigNum(_last_number); // last_number.print(); for (long long i = 1; i < n; i++) { long long _a; long long original_length = 0; original_length = read(&_a); BigNum a = BigNum(_a); // printf("%d %d\n", a.get_length(), last_number.get_length()); if (a.get_length() <= last_number.get_length()) { a.zeroes = last_number.get_length() - a.get_length(); long long min_zeroes = std::min(a.zeroes, last_number.zeroes); a.set_zeroes(min_zeroes); last_number.set_zeroes(min_zeroes); // a.print(); // last_number.print(); if (a < last_number || a == last_number) { BigNum difference = last_number - a; difference.set_suffix(difference.suffix + 1); // difference.print(); // printf("%d %d\n", difference.get_length(), a.get_length() - original_length); if (difference.get_length() <= a.get_length() - original_length) { a = a + difference; } else { a.zeroes++; } } } last_number = a; // last_number.print(); // puts("--------------"); result += a.get_length() - original_length; } printf("%lld\n", result); }
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 250 251 252 253 | #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> long long POW10[19]; short LENGTHS[200001] = {0}; inline short read(long long *n) { register char c=0; short count = 0; while (c<33) c=getc_unlocked(stdin); (*n)=0; while(c>32){ (*n)=(*n)*10+(c-'0'); c=getc_unlocked(stdin); count++; } return count; } long long get_number_length(long long a) { // if (a < 200001) { // return LENGTHS[a]; // } long long count = 0; while (a) { count++; a /= 10; } return count; } void build_powers() { long long temp = 1; for (long long i = 0; i < 19; i++) { POW10[i] = temp; temp *=10; } } struct BigNum { // prefix * 10^zeroes + suffix long long prefix; long long prefix_length; long long zeroes; long long suffix; long long suffix_length; BigNum(long long value) { this->prefix_length = 0; this->zeroes = 0; this->suffix = 0; this->suffix_length = 0; this->set_prefix(value); } void set_prefix(long long value) { this->prefix = value; this->prefix_length = get_number_length(this->prefix); if (!value) { this->prefix_length = 0; } } void change_prefix(long long value) { long long value_length = get_number_length(value); this->zeroes -= value_length - this->prefix_length; this->set_prefix(value); } long long get_length() const { if (!this->prefix_length && !this->zeroes && !this->suffix) { return 1; } if (this->zeroes < 9 && this->suffix_length >= this->zeroes) { return get_number_length(this->prefix * POW10[this->zeroes] + this->suffix); } return this->prefix_length + this->zeroes; } bool operator < (const BigNum &y) { // x < y BigNum result = (*this - y); if (result.zeroes < 9) { return result.prefix * POW10[result.zeroes] + result.suffix < 0; } else if (result.prefix < 0 || (result.prefix == 0 && result.suffix < 0)) { // printf("%d\n", result.prefix); return true; } else { return false; } } bool operator == (const BigNum &y) { // x == y BigNum result = (*this - y); if (result.zeroes < 9) { return result.prefix * POW10[result.zeroes] + result.suffix == 0; } else if (result.prefix == 0 && result.suffix == 0) { return true; } else { return false; } } BigNum operator + (const BigNum &y) { // zeroes = x.zeroes = y.zeroes // (x.prefix * 10^x.zeroes + x.suffix) + (y.prefix * 10^y.zeroes + y.suffix) = // (x.prefix + y.prefix) * 10^zeroes + x.suffix + y.suffix BigNum result = BigNum(0); result.zeroes = this->zeroes; result.set_prefix(this->prefix + y.prefix); result.set_suffix(this->suffix + y.suffix); return result; } BigNum operator - (const BigNum &y) { // zeroes = x.zeroes = y.zeroes // (x.prefix * 10^x.zeroes + x.suffix) - (y.prefix * 10^y.zeroes + y.suffix) = // (x.prefix - y.prefix) * 10^zeroes + x.suffix - y.suffix BigNum result = BigNum(0); result.zeroes = this->zeroes; result.set_prefix(this->prefix - y.prefix); result.set_suffix(this->suffix - y.suffix); return result; } long long get_normalized_prefix(long long characters) const { if (characters <= this->prefix_length) { return this->prefix; } return this->prefix * POW10[characters - this->prefix_length]; } void set_suffix(long long new_suffix) { if (new_suffix) { long long new_suffix_length = get_number_length(new_suffix); this->suffix = new_suffix; this->suffix_length = new_suffix_length; } } void print() { printf(">pref: %d ", prefix); for (long long i = 0; i < this->zeroes - this->suffix_length; i++) { printf("%d", 0); } if (this->suffix_length) { printf("%d", suffix); } puts("<"); } void set_zeroes(long long zeroes) { this->set_prefix(this->prefix * POW10[this->zeroes - zeroes]); this->zeroes = zeroes; this->set_suffix(this->suffix); } }; int main() { long long n; long long result = 0; long long _last_number; // LENGTHS[0] = 1; // LENGTHS[10] = 2; // LENGTHS[100] = 3; // LENGTHS[1000] = 4; // LENGTHS[10000] = 6; // LENGTHS[100000] = 7; // for (long long i = 1; i < 200001; i++) { // if (!LENGTHS[i]) { // LENGTHS[i] = LENGTHS[i - 1]; // } // } build_powers(); read(&n); read(&_last_number); BigNum last_number = BigNum(_last_number); // last_number.print(); for (long long i = 1; i < n; i++) { long long _a; long long original_length = 0; original_length = read(&_a); BigNum a = BigNum(_a); // printf("%d %d\n", a.get_length(), last_number.get_length()); if (a.get_length() <= last_number.get_length()) { a.zeroes = last_number.get_length() - a.get_length(); long long min_zeroes = std::min(a.zeroes, last_number.zeroes); a.set_zeroes(min_zeroes); last_number.set_zeroes(min_zeroes); // a.print(); // last_number.print(); if (a < last_number || a == last_number) { BigNum difference = last_number - a; difference.set_suffix(difference.suffix + 1); // difference.print(); // printf("%d %d\n", difference.get_length(), a.get_length() - original_length); if (difference.get_length() <= a.get_length() - original_length) { a = a + difference; } else { a.zeroes++; } } } last_number = a; // last_number.print(); // puts("--------------"); result += a.get_length() - original_length; } printf("%lld\n", result); } |