#include <bits/stdc++.h> using namespace std; typedef unsigned long long ll; const ll BASE = 1e9; string toStr(ll a) { if(a == 0) return "0"; string result = ""; while(a > 0) { result += (char)('0' + (a%10)); a /= 10; } reverse(result.begin(), result.end()); return result; } ll powr(ll a, ll b) { if(b == 0) return 1; if(b == 1) return a; ll result = 1; while(b) { if (b & 1) result *= a; a *= a; b >>= 1; } return result; } ll cyfry(ll x) { if(x == 0) return 1; ll result = 0; while(x > 0) { x /= 10; result++; } return result; } struct BigNum { ll a, b, c; // a*BASE*10^b + c BigNum(ll n = 0) { b = 0; a = n / BASE; c = n % BASE; reduce(); } void reduce() { ll pom = c; c = pom % BASE; ll r = pom / BASE; if(r > 0) { a *= pow(10, b); a += r; b = 0; } if(a == 0) b = 0; while(a > 0 && a % 10 == 0) { a /= 10; b++; } } void print() { ll zer = max(0ull, b + 9 - cyfry(c)); if(a > 0) { cout << a; for(int i = 0; i < zer; i++) cout << "0"; } cout << c; } string toString() { ll zer = max(0ull, b + 9 - cyfry(c)); string result = ""; if(a > 0) { result += toStr(a); for(int i = 0; i < zer; i++) result += "0"; } result += toStr(c); return result; } string firstN(ll n) { ll zer = max(0ull, b + 9 - cyfry(c)); string result = ""; if(a > 0) { result += toStr(a); if(result.size() == n) return result; for(int i = 0; i < zer; i++) { result += '0'; if(result.size() == n) return result; } } result += toStr(c); return string(result.begin(), result.begin() + n); } }; ll cyfry(BigNum x) { return (x.a > 0 ? cyfry(x.a) + x.b + 9: cyfry(x.c)); } bool eq(BigNum a, BigNum b) { return (a.a == b.a && a.b == b.b && a.c == b.c); } bool greaterThan(BigNum a, BigNum b) { if (cyfry(a) > cyfry(b)) return true; else if (cyfry(a) < cyfry(b)) return false; ll zer = min(a.b, b.b); ll X1 = a.a * powr(10, a.b - zer); ll X2 = a.c; ll Y1 = b.a * powr(10, b.b - zer); ll Y2 = b.c; return (X1 > Y1 || (X1 == Y1 && X2 > Y2)); } BigNum pow10(BigNum a, ll x) { if(x > 9) { a.a = a.c; a.b = x - 9; a.c = 0; a.reduce(); } else { while(x > 0) { a.c *= 10; a.b++; a.reduce(); x--; } } return a; } ll lastN(BigNum a, ll x) { ll mod = min(9ull, x); if(x > 9 + a.b) { ll result = a.a % powr(10, x-a.b-9) + a.c % powr(10, mod); return result; } return a.c % powr(10, mod); } BigNum sum(BigNum a, ll b) { a.c += b; a.reduce(); return a; } int main() { ios_base::sync_with_stdio(0); int n; // BigNum T(98), T2(9); cin >> n; ll v; BigNum x, xLast; ll odp = 0; for (int i = 0; i < n; i++) { cin >> v; x = BigNum(v); if (i == 0) { xLast = x; continue; } if ((greaterThan(xLast, x) || eq(xLast, x)) && cyfry(x) == cyfry(xLast)) { xLast = pow10(x, 1); odp++; } else if (greaterThan(xLast, x)) { // cout << "= " << xLast.a << " " << xLast.b << " " << xLast.c << endl; //cout << cyfry(xLast) << " " << cyfry(x) << endl; BigNum pom0 = pow10(x, cyfry(xLast) - cyfry(x)); BigNum pom1 = sum(pom0, lastN(xLast, cyfry(xLast) - cyfry(x))); BigNum pom2 = sum(pom1, 1); BigNum pom3 = pow10(x, cyfry(xLast) - cyfry(x) + 1); // cout << ">>"; // pom0.print(); cout << " "; pom1.print(); cout << " "; pom2.print(); cout << " "; pom3.print(); cout << endl; //cout << ">> " << pom2Str << " " << string(pom2Str.begin(), pom2Str.begin() + cyfry(x)) << " " << x.toString() << endl; // cout << "@" << pom2.firstN(cyfry(x)) << " " << x.toString() << endl; if (greaterThan(pom0, xLast)) { odp += cyfry(xLast) - cyfry(x); xLast = pom0; } else if (greaterThan(pom1, xLast)) { odp += cyfry(xLast) - cyfry(x); xLast = pom1; } else if (greaterThan(pom2, xLast) && pom2.firstN(cyfry(x)) == x.toString()) { odp += cyfry(xLast) - cyfry(x); xLast = pom2; } else { odp += cyfry(xLast) - cyfry(x) + 1; xLast = pom3; } } else { xLast = x; } //cout << "!"; //xLast.print(); // cout << endl; } cout << odp; }
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 | #include <bits/stdc++.h> using namespace std; typedef unsigned long long ll; const ll BASE = 1e9; string toStr(ll a) { if(a == 0) return "0"; string result = ""; while(a > 0) { result += (char)('0' + (a%10)); a /= 10; } reverse(result.begin(), result.end()); return result; } ll powr(ll a, ll b) { if(b == 0) return 1; if(b == 1) return a; ll result = 1; while(b) { if (b & 1) result *= a; a *= a; b >>= 1; } return result; } ll cyfry(ll x) { if(x == 0) return 1; ll result = 0; while(x > 0) { x /= 10; result++; } return result; } struct BigNum { ll a, b, c; // a*BASE*10^b + c BigNum(ll n = 0) { b = 0; a = n / BASE; c = n % BASE; reduce(); } void reduce() { ll pom = c; c = pom % BASE; ll r = pom / BASE; if(r > 0) { a *= pow(10, b); a += r; b = 0; } if(a == 0) b = 0; while(a > 0 && a % 10 == 0) { a /= 10; b++; } } void print() { ll zer = max(0ull, b + 9 - cyfry(c)); if(a > 0) { cout << a; for(int i = 0; i < zer; i++) cout << "0"; } cout << c; } string toString() { ll zer = max(0ull, b + 9 - cyfry(c)); string result = ""; if(a > 0) { result += toStr(a); for(int i = 0; i < zer; i++) result += "0"; } result += toStr(c); return result; } string firstN(ll n) { ll zer = max(0ull, b + 9 - cyfry(c)); string result = ""; if(a > 0) { result += toStr(a); if(result.size() == n) return result; for(int i = 0; i < zer; i++) { result += '0'; if(result.size() == n) return result; } } result += toStr(c); return string(result.begin(), result.begin() + n); } }; ll cyfry(BigNum x) { return (x.a > 0 ? cyfry(x.a) + x.b + 9: cyfry(x.c)); } bool eq(BigNum a, BigNum b) { return (a.a == b.a && a.b == b.b && a.c == b.c); } bool greaterThan(BigNum a, BigNum b) { if (cyfry(a) > cyfry(b)) return true; else if (cyfry(a) < cyfry(b)) return false; ll zer = min(a.b, b.b); ll X1 = a.a * powr(10, a.b - zer); ll X2 = a.c; ll Y1 = b.a * powr(10, b.b - zer); ll Y2 = b.c; return (X1 > Y1 || (X1 == Y1 && X2 > Y2)); } BigNum pow10(BigNum a, ll x) { if(x > 9) { a.a = a.c; a.b = x - 9; a.c = 0; a.reduce(); } else { while(x > 0) { a.c *= 10; a.b++; a.reduce(); x--; } } return a; } ll lastN(BigNum a, ll x) { ll mod = min(9ull, x); if(x > 9 + a.b) { ll result = a.a % powr(10, x-a.b-9) + a.c % powr(10, mod); return result; } return a.c % powr(10, mod); } BigNum sum(BigNum a, ll b) { a.c += b; a.reduce(); return a; } int main() { ios_base::sync_with_stdio(0); int n; // BigNum T(98), T2(9); cin >> n; ll v; BigNum x, xLast; ll odp = 0; for (int i = 0; i < n; i++) { cin >> v; x = BigNum(v); if (i == 0) { xLast = x; continue; } if ((greaterThan(xLast, x) || eq(xLast, x)) && cyfry(x) == cyfry(xLast)) { xLast = pow10(x, 1); odp++; } else if (greaterThan(xLast, x)) { // cout << "= " << xLast.a << " " << xLast.b << " " << xLast.c << endl; //cout << cyfry(xLast) << " " << cyfry(x) << endl; BigNum pom0 = pow10(x, cyfry(xLast) - cyfry(x)); BigNum pom1 = sum(pom0, lastN(xLast, cyfry(xLast) - cyfry(x))); BigNum pom2 = sum(pom1, 1); BigNum pom3 = pow10(x, cyfry(xLast) - cyfry(x) + 1); // cout << ">>"; // pom0.print(); cout << " "; pom1.print(); cout << " "; pom2.print(); cout << " "; pom3.print(); cout << endl; //cout << ">> " << pom2Str << " " << string(pom2Str.begin(), pom2Str.begin() + cyfry(x)) << " " << x.toString() << endl; // cout << "@" << pom2.firstN(cyfry(x)) << " " << x.toString() << endl; if (greaterThan(pom0, xLast)) { odp += cyfry(xLast) - cyfry(x); xLast = pom0; } else if (greaterThan(pom1, xLast)) { odp += cyfry(xLast) - cyfry(x); xLast = pom1; } else if (greaterThan(pom2, xLast) && pom2.firstN(cyfry(x)) == x.toString()) { odp += cyfry(xLast) - cyfry(x); xLast = pom2; } else { odp += cyfry(xLast) - cyfry(x) + 1; xLast = pom3; } } else { xLast = x; } //cout << "!"; //xLast.print(); // cout << endl; } cout << odp; } |