#include <bits/stdc++.h> using namespace std; using i64 = long long; int getLength(i64 x){ if (!x) return 1; int res = 0; for (; x; x /= 10, ++res); return res; } i64 fpow(i64 a, int t){ i64 r = 1; for (; t; t >>= 1, a *= a) if (t & 1) r *= a; return r; } i64 result; static const int C = 17; static const int C1 = 11, C2 = 6; static int Error = fpow(10, C2) - 2; struct bignum { int len; // case 1: len <= C i64 number; // case 2: len > C i64 prefix, suffix; // prefix contains first 10 digits, suffix contains no more than 6 digits void extend(i64 value){ int g = getLength(value); if (++len <= C) { if (len < g) len = g; number = value * fpow(10, len - g); } else { prefix = value * fpow(10, C1 - g); suffix = 0; } result += len - g; } void append(i64 value){ int g = getLength(value); if (g > len) return extend(value); if (len <= C) { i64 imax = (value + 1) * fpow(10, len - g) - 1; if (imax <= number) return extend(value); i64 imin = value * fpow(10, len - g); if (imin > number) number = imin; else ++number; } else { assert(suffix <= Error); i64 imax = (value + 1) * fpow(10, C1 - g) - 1; if (imax < prefix) return extend(value); i64 imin = value * fpow(10, C1 - g); if (imin > prefix) { prefix = imin; suffix = 0; } else ++suffix; } result += len - g; } } answer; int n; int main(){ int i, a; scanf("%d", &n); answer.len = 1; for (i = 1; i <= n; ++i) scanf("%d", &a), answer.append(a); 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 | #include <bits/stdc++.h> using namespace std; using i64 = long long; int getLength(i64 x){ if (!x) return 1; int res = 0; for (; x; x /= 10, ++res); return res; } i64 fpow(i64 a, int t){ i64 r = 1; for (; t; t >>= 1, a *= a) if (t & 1) r *= a; return r; } i64 result; static const int C = 17; static const int C1 = 11, C2 = 6; static int Error = fpow(10, C2) - 2; struct bignum { int len; // case 1: len <= C i64 number; // case 2: len > C i64 prefix, suffix; // prefix contains first 10 digits, suffix contains no more than 6 digits void extend(i64 value){ int g = getLength(value); if (++len <= C) { if (len < g) len = g; number = value * fpow(10, len - g); } else { prefix = value * fpow(10, C1 - g); suffix = 0; } result += len - g; } void append(i64 value){ int g = getLength(value); if (g > len) return extend(value); if (len <= C) { i64 imax = (value + 1) * fpow(10, len - g) - 1; if (imax <= number) return extend(value); i64 imin = value * fpow(10, len - g); if (imin > number) number = imin; else ++number; } else { assert(suffix <= Error); i64 imax = (value + 1) * fpow(10, C1 - g) - 1; if (imax < prefix) return extend(value); i64 imin = value * fpow(10, C1 - g); if (imin > prefix) { prefix = imin; suffix = 0; } else ++suffix; } result += len - g; } } answer; int n; int main(){ int i, a; scanf("%d", &n); answer.len = 1; for (i = 1; i <= n; ++i) scanf("%d", &a), answer.append(a); printf("%lld\n", result); } |