#include <iostream> #include <string> #include <array> #include <vector> #include <cmath> #include <cassert> #ifndef dbgprint #define dbgprint //printf #endif using namespace std; int N; using ull = unsigned long long; int get_nr_of_digits(ull nr) { int digits = 0; while (nr > 0) { nr /= 10; digits++; } return digits; } ull power_of_ten(int x) { if (x == 0) { return 1; } if (x % 2 == 0) { auto tmp = power_of_ten(x/2); return tmp * tmp; } else { return 10*power_of_ten(x-1); } } bool IsPrefix( std::string const& lhs, std::string const& rhs ) { return std::equal( lhs.begin(), lhs.begin() + std::min( lhs.size(), rhs.size() ), rhs.begin() ); } using ull = unsigned long long; ull result = 0; ull now; ull next_small(ull next) { if (next > now) { dbgprint("1-\n"); return next; } else if (next == now) { dbgprint("2-\n"); result += 1; return next * 10; } else { // next_small is smaller std::string now_s = std::to_string(now); std::string next_s = std::to_string(next); if (IsPrefix(now_s, next_s)) { dbgprint("IS PREFIX\n"); auto diff = now_s.length() - next_s.length(); now_s = std::to_string(now+1); if (IsPrefix(now_s, next_s)) { dbgprint("IS +1 \n"); result += diff; return now+1; } else { dbgprint("NOPE, *10\n"); while (next < now) { next *= 10; result++; } return next; } } else { dbgprint("NOT A PREFIX\n"); while (next < now) { next *= 10; result++; } return next; } } } struct Digit { ull base; ull nr_of_digits; ull rest; Digit(ull nr) { base = nr; nr_of_digits = base_nr_of_digits(); rest = 0; } int base_nr_of_digits() const { return get_nr_of_digits(base); } ull get_base_of_same_digits(int nr_of_digits_to_cmp) const { int diff = base_nr_of_digits() - nr_of_digits_to_cmp; if (diff > 0) { return base/power_of_ten(diff); } else { return base * power_of_ten(-diff); } } ull get() const { if (nr_of_digits > 9) { return -1; } ull now = (ull)base * (ull)power_of_ten(nr_of_digits - base_nr_of_digits()) + (ull)rest; if (now > 1e9) { return -1; } return now; } void print() { dbgprint("(%llu %llu %llu)", base, nr_of_digits, rest); } inline bool operator< (const Digit& rhs) const { if (nr_of_digits == rhs.nr_of_digits) { if (base == rhs.base) { return (rest < rhs.rest); } string a = to_string(this->base); string b = to_string(rhs.base); return (a < b); } return (nr_of_digits < rhs.nr_of_digits); } inline bool operator> (const Digit& rhs) const { return rhs < *this; } inline bool operator<=(const Digit& rhs) const { return !(*this > rhs); } inline bool operator>=(const Digit& rhs) const { return !(*this < rhs); } inline bool operator==(const Digit& rhs) const { return (nr_of_digits == rhs.nr_of_digits && base == rhs.base && rest == rhs.rest); } Digit next(ull next_b) { Digit out(*this); Digit next(next_b); this->print(); next.print(); dbgprint("\n"); if (next > *this) { dbgprint("1-\n"); out = next; } else if (next == *this) { dbgprint("2-\n"); out = next; out.nr_of_digits++; } else { // next is smaller // determine what is the suffix to add ull base = get_base_of_same_digits(get_nr_of_digits(next.base)); if (base < next.base) { dbgprint("3-\n"); out.base = next.base; out.rest = 0; } else if (base > next.base) { dbgprint("4-\n"); out.base = next.base; out.rest = 0; out.nr_of_digits++; } else { // base == next.base dbgprint("7-\n"); out.base = max(next.base, this->base); out.nr_of_digits = this->nr_of_digits; out.rest = this->rest+1; } } result += out.nr_of_digits - next.nr_of_digits; return out; } }; int main() { // N=100; scanf("%d", &N); now = 0; auto now_large = Digit(0); for (int i = 0; i < N; ++i) { int a; scanf("%d", &a); if (now <= 1e17) { now = next_small(a); now_large = Digit(now); } else { now_large = now_large.next(a); } dbgprint("|| %d --> %llu ++ %llu || ", a, now, result); now_large.print(); dbgprint("\n\n"); } printf("%llu\n", result); 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 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 | #include <iostream> #include <string> #include <array> #include <vector> #include <cmath> #include <cassert> #ifndef dbgprint #define dbgprint //printf #endif using namespace std; int N; using ull = unsigned long long; int get_nr_of_digits(ull nr) { int digits = 0; while (nr > 0) { nr /= 10; digits++; } return digits; } ull power_of_ten(int x) { if (x == 0) { return 1; } if (x % 2 == 0) { auto tmp = power_of_ten(x/2); return tmp * tmp; } else { return 10*power_of_ten(x-1); } } bool IsPrefix( std::string const& lhs, std::string const& rhs ) { return std::equal( lhs.begin(), lhs.begin() + std::min( lhs.size(), rhs.size() ), rhs.begin() ); } using ull = unsigned long long; ull result = 0; ull now; ull next_small(ull next) { if (next > now) { dbgprint("1-\n"); return next; } else if (next == now) { dbgprint("2-\n"); result += 1; return next * 10; } else { // next_small is smaller std::string now_s = std::to_string(now); std::string next_s = std::to_string(next); if (IsPrefix(now_s, next_s)) { dbgprint("IS PREFIX\n"); auto diff = now_s.length() - next_s.length(); now_s = std::to_string(now+1); if (IsPrefix(now_s, next_s)) { dbgprint("IS +1 \n"); result += diff; return now+1; } else { dbgprint("NOPE, *10\n"); while (next < now) { next *= 10; result++; } return next; } } else { dbgprint("NOT A PREFIX\n"); while (next < now) { next *= 10; result++; } return next; } } } struct Digit { ull base; ull nr_of_digits; ull rest; Digit(ull nr) { base = nr; nr_of_digits = base_nr_of_digits(); rest = 0; } int base_nr_of_digits() const { return get_nr_of_digits(base); } ull get_base_of_same_digits(int nr_of_digits_to_cmp) const { int diff = base_nr_of_digits() - nr_of_digits_to_cmp; if (diff > 0) { return base/power_of_ten(diff); } else { return base * power_of_ten(-diff); } } ull get() const { if (nr_of_digits > 9) { return -1; } ull now = (ull)base * (ull)power_of_ten(nr_of_digits - base_nr_of_digits()) + (ull)rest; if (now > 1e9) { return -1; } return now; } void print() { dbgprint("(%llu %llu %llu)", base, nr_of_digits, rest); } inline bool operator< (const Digit& rhs) const { if (nr_of_digits == rhs.nr_of_digits) { if (base == rhs.base) { return (rest < rhs.rest); } string a = to_string(this->base); string b = to_string(rhs.base); return (a < b); } return (nr_of_digits < rhs.nr_of_digits); } inline bool operator> (const Digit& rhs) const { return rhs < *this; } inline bool operator<=(const Digit& rhs) const { return !(*this > rhs); } inline bool operator>=(const Digit& rhs) const { return !(*this < rhs); } inline bool operator==(const Digit& rhs) const { return (nr_of_digits == rhs.nr_of_digits && base == rhs.base && rest == rhs.rest); } Digit next(ull next_b) { Digit out(*this); Digit next(next_b); this->print(); next.print(); dbgprint("\n"); if (next > *this) { dbgprint("1-\n"); out = next; } else if (next == *this) { dbgprint("2-\n"); out = next; out.nr_of_digits++; } else { // next is smaller // determine what is the suffix to add ull base = get_base_of_same_digits(get_nr_of_digits(next.base)); if (base < next.base) { dbgprint("3-\n"); out.base = next.base; out.rest = 0; } else if (base > next.base) { dbgprint("4-\n"); out.base = next.base; out.rest = 0; out.nr_of_digits++; } else { // base == next.base dbgprint("7-\n"); out.base = max(next.base, this->base); out.nr_of_digits = this->nr_of_digits; out.rest = this->rest+1; } } result += out.nr_of_digits - next.nr_of_digits; return out; } }; int main() { // N=100; scanf("%d", &N); now = 0; auto now_large = Digit(0); for (int i = 0; i < N; ++i) { int a; scanf("%d", &a); if (now <= 1e17) { now = next_small(a); now_large = Digit(now); } else { now_large = now_large.next(a); } dbgprint("|| %d --> %llu ++ %llu || ", a, now, result); now_large.print(); dbgprint("\n\n"); } printf("%llu\n", result); return 0; } |