#include <iostream> struct Result { std::string value; int digits; }; const long long PMIN = 1000000000ll; const long long PMAX = 9999999999ll; struct Num { long long prefix; int mult; long long extra; // num = prefix * 10^mult + extra // extra < 10^mult // mult > 0 <=> 9999999999 > prefix > 1000000000 }; long long digits(long long n) { if (n == 0) return 1; int d = 0; while (n > 0) { n /= 10; ++d; } return d; } long long digits(Num n) { return digits(n.prefix) + n.mult; } long long pow10(int x) { long long result = 1; for (int i=0;i<x;++i) result *= 10; return result; } long long nines(int n) { if (n == 0) return 0; long long result = 9; if (n > 9) n = 9; for (int i=1;i<n;++i) { result *= 10; result += 9; } return result; } Num inc(Num x) { if (x.mult == 0) { x.prefix++; if (x.prefix > PMAX) { x.mult = 1; x.extra = x.prefix % 10; x.prefix /= 10; return x; } return x; } x.extra++; if (digits(x.extra) > x.mult) { if (x.prefix+1 > PMAX) { x.prefix += 1; x.mult += 1; x.extra = x.prefix % 10; x.prefix /= 10; return x; } else { x.extra -= pow10(x.mult); x.prefix += 1; return x; } } return x; } Num roundUp(long long prefix, int d) { long long pp = 1; while (pp <= prefix) { d--; pp*=10; } while (d > 0 && prefix < PMIN) { d--; prefix*=10; } return {prefix, d, 0}; } Num roundUp2(long long prefix, int d) { long long pp = 1; while (pp <= prefix) { d--; pp*=10; } while (d > 0 && prefix < PMIN) { d--; prefix*=10; prefix+=9; } return {prefix, d, nines(d)}; } bool operator<(Num a, Num b) { int da = digits(a); int db = digits(b); if (da != db) return da < db; if (a.prefix != b.prefix) return a.prefix < b.prefix; else return a.extra < b.extra; } Num solve(long long current, Num prev) { long long dd = std::max(digits(prev), digits(current)); Num new1 = roundUp(current, dd); //std::clog << " :: new1 = " << new1.prefix << " + 10^" << new1.mult << " + " << new1.extra << std::endl; if (prev < new1) return new1; Num new2 = roundUp2(current, dd); //std::clog << " :: new2 = " << new2.prefix << " + 10^" << new2.mult << " + " << new2.extra << std::endl; if (prev < new2) return inc(prev); else return roundUp(current, digits(prev)+1); } int main() { int n; std::cin >> n; Num prev = {0,0,0}; long long result = 0; for (int i=0;i<n;++i) { long long curr; std::cin >> curr; prev = solve(curr, prev); //std::clog << " :: " << prev.prefix << " + 10^" << prev.mult << " + " << prev.extra << std::endl; result += digits(prev) - digits(curr); } std::cout << result << std::endl; 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 | #include <iostream> struct Result { std::string value; int digits; }; const long long PMIN = 1000000000ll; const long long PMAX = 9999999999ll; struct Num { long long prefix; int mult; long long extra; // num = prefix * 10^mult + extra // extra < 10^mult // mult > 0 <=> 9999999999 > prefix > 1000000000 }; long long digits(long long n) { if (n == 0) return 1; int d = 0; while (n > 0) { n /= 10; ++d; } return d; } long long digits(Num n) { return digits(n.prefix) + n.mult; } long long pow10(int x) { long long result = 1; for (int i=0;i<x;++i) result *= 10; return result; } long long nines(int n) { if (n == 0) return 0; long long result = 9; if (n > 9) n = 9; for (int i=1;i<n;++i) { result *= 10; result += 9; } return result; } Num inc(Num x) { if (x.mult == 0) { x.prefix++; if (x.prefix > PMAX) { x.mult = 1; x.extra = x.prefix % 10; x.prefix /= 10; return x; } return x; } x.extra++; if (digits(x.extra) > x.mult) { if (x.prefix+1 > PMAX) { x.prefix += 1; x.mult += 1; x.extra = x.prefix % 10; x.prefix /= 10; return x; } else { x.extra -= pow10(x.mult); x.prefix += 1; return x; } } return x; } Num roundUp(long long prefix, int d) { long long pp = 1; while (pp <= prefix) { d--; pp*=10; } while (d > 0 && prefix < PMIN) { d--; prefix*=10; } return {prefix, d, 0}; } Num roundUp2(long long prefix, int d) { long long pp = 1; while (pp <= prefix) { d--; pp*=10; } while (d > 0 && prefix < PMIN) { d--; prefix*=10; prefix+=9; } return {prefix, d, nines(d)}; } bool operator<(Num a, Num b) { int da = digits(a); int db = digits(b); if (da != db) return da < db; if (a.prefix != b.prefix) return a.prefix < b.prefix; else return a.extra < b.extra; } Num solve(long long current, Num prev) { long long dd = std::max(digits(prev), digits(current)); Num new1 = roundUp(current, dd); //std::clog << " :: new1 = " << new1.prefix << " + 10^" << new1.mult << " + " << new1.extra << std::endl; if (prev < new1) return new1; Num new2 = roundUp2(current, dd); //std::clog << " :: new2 = " << new2.prefix << " + 10^" << new2.mult << " + " << new2.extra << std::endl; if (prev < new2) return inc(prev); else return roundUp(current, digits(prev)+1); } int main() { int n; std::cin >> n; Num prev = {0,0,0}; long long result = 0; for (int i=0;i<n;++i) { long long curr; std::cin >> curr; prev = solve(curr, prev); //std::clog << " :: " << prev.prefix << " + 10^" << prev.mult << " + " << prev.extra << std::endl; result += digits(prev) - digits(curr); } std::cout << result << std::endl; return 0; } |