#include <bits/stdc++.h> // #pragma GCC optimize ("O3") // #pragma GCC target ("sse4") using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define pb push_back #define mp make_pair #define st first #define nd second const int A_DIGITS = 10; LL P10[A_DIGITS + 5]; struct BigInt { // a * 10^e + b LL a; int e; int b; BigInt(const BigInt& c): a(c.a), e(c.e), b(c.b) {} BigInt(LL _a, int _e = 0): a(_a), e(_e), b(0) { while (e && 10 * a < P10[A_DIGITS]) { a *= 10; --e; } while (a >= P10[A_DIGITS]) { assert (a % 10 == 0); ++e; a /= 10; } } BigInt inc() const { BigInt res = *this; ++res.b; if (res.e < 10 && res.b == P10[res.e]) { res.b = 0; ++res.a; } if (res.a == P10[A_DIGITS]) { ++res.e; res.a /= 10; } return res; } int digits() const { int result = 0; REP(i,A_DIGITS) result += a >= P10[i]; return result + e; } bool operator<(const BigInt& other) const { if (digits() != other.digits()) return digits() < other.digits(); if (a != other.a) return a < other.a; return b < other.b; } bool matches(const int& original) const { LL t = a; while (t > original) t /= 10; return t == original; } }; ostream& operator<<(ostream& out, const BigInt& x) { out << x.a; REP(i,x.e-BigInt(x.b).digits()) out << '0'; if (x.b) out << x.b; return out; } vector<BigInt> original, computed; int main(int argc, char* argv[]) { P10[0] = 1; FOR(i,1,A_DIGITS + 5) P10[i] = 10 * P10[i-1]; int N; scanf("%d", &N); REP(i,N) { int a; scanf("%d", &a); original.pb(a); if (i == 0) { computed.pb(a); } else if (computed[i-1] < a) { computed.pb(a); } else if (computed[i-1].inc().matches(a)) { computed.pb(computed[i-1].inc()); } else { int d = computed[i-1].digits() - BigInt(a).digits(); if (computed[i-1] < BigInt(a, d)) { computed.pb(BigInt(a, d)); } else { computed.pb(BigInt(a, d+1)); } } // // cout << original[i] << " " << computed[i] << endl; } LL result = 0; REP(i,N) result += computed[i].digits() - original[i].digits(); 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 | #include <bits/stdc++.h> // #pragma GCC optimize ("O3") // #pragma GCC target ("sse4") using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define pb push_back #define mp make_pair #define st first #define nd second const int A_DIGITS = 10; LL P10[A_DIGITS + 5]; struct BigInt { // a * 10^e + b LL a; int e; int b; BigInt(const BigInt& c): a(c.a), e(c.e), b(c.b) {} BigInt(LL _a, int _e = 0): a(_a), e(_e), b(0) { while (e && 10 * a < P10[A_DIGITS]) { a *= 10; --e; } while (a >= P10[A_DIGITS]) { assert (a % 10 == 0); ++e; a /= 10; } } BigInt inc() const { BigInt res = *this; ++res.b; if (res.e < 10 && res.b == P10[res.e]) { res.b = 0; ++res.a; } if (res.a == P10[A_DIGITS]) { ++res.e; res.a /= 10; } return res; } int digits() const { int result = 0; REP(i,A_DIGITS) result += a >= P10[i]; return result + e; } bool operator<(const BigInt& other) const { if (digits() != other.digits()) return digits() < other.digits(); if (a != other.a) return a < other.a; return b < other.b; } bool matches(const int& original) const { LL t = a; while (t > original) t /= 10; return t == original; } }; ostream& operator<<(ostream& out, const BigInt& x) { out << x.a; REP(i,x.e-BigInt(x.b).digits()) out << '0'; if (x.b) out << x.b; return out; } vector<BigInt> original, computed; int main(int argc, char* argv[]) { P10[0] = 1; FOR(i,1,A_DIGITS + 5) P10[i] = 10 * P10[i-1]; int N; scanf("%d", &N); REP(i,N) { int a; scanf("%d", &a); original.pb(a); if (i == 0) { computed.pb(a); } else if (computed[i-1] < a) { computed.pb(a); } else if (computed[i-1].inc().matches(a)) { computed.pb(computed[i-1].inc()); } else { int d = computed[i-1].digits() - BigInt(a).digits(); if (computed[i-1] < BigInt(a, d)) { computed.pb(BigInt(a, d)); } else { computed.pb(BigInt(a, d+1)); } } // // cout << original[i] << " " << computed[i] << endl; } LL result = 0; REP(i,N) result += computed[i].digits() - original[i].digits(); printf("%lld\n", result); } |