#include <bits/stdc++.h> #define F first #define S second #define ll long long #define ld long double #define vi vector < int > #define vll vector < ll > #define pb push_back #define sz(x) int((x).size()) #define pii pair < int, int > #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define vpii vector < pii > #define sqr(x) ((x)*(x)) #define fenwick(x) ((x) & -(x)) #define debug(x) cout << "[ " << #x << " ]: " << x << "\n"; using namespace std; //CZY DANE POSORTOWANE, N = 0, N = 1, CZYTANIE TRESCI, < rosnaco, > malejaco const int MAX = 2e5 + 12; const ll MOD = 1e10; const ll EEE = 1e16; ll tab[MAX]; int oryg[MAX]; ll dodane[MAX]; int log10(ll a) { int w = 0; while (a) { ++w; a /= 10; } return w; } ll ppow(int ile) { if (ile == 0)return 1; if (ile == 1)return 10; if (ile == 2)return 100; if (ile == 3)return 1000; if (ile == 4)return 10000; if (ile == 5)return 100000; if (ile == 6)return 1000000; if (ile == 7)return 10000000; if (ile == 8)return 100000000; if (ile == 9)return 1000000000; if (ile == 10)return 10000000000; if (ile == 11)return 1e11; if (ile == 12)return 1e12; if (ile == 13)return 1e13; if (ile == 14)return 1e14; if (ile == 15)return 1e15; if (ile == 16)return 1e16; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N; ll ans = 0; int czy_dodalem = 0; cin >> N; for (int i = 0; i < N; ++i) { cin >> tab[i]; oryg[i] = log10(tab[i]); } int curr = 0; for (int i = 1; i < N; ++i) { vi taba, tabb; ll aa = tab[curr], bb = tab[i]; if (aa < bb) { curr = i; continue; } ll cpbb = bb; while (aa){ taba.pb(aa % 10); aa /= 10;} while (cpbb){tabb.pb(cpbb % 10);cpbb /= 10; } reverse(all(tabb)); reverse(all(taba)); while (sz(tabb) != sz(taba)) { tabb.pb(0); bb *= 10; ++dodane[i]; } //cout << dodane[curr] << "+\t" << oryg[curr] << "-\t" << log10(aa) << "-\t" << oryg[i] << "\n"; //cout << dodane[curr] + oryg[curr] - log10(aa) - oryg[i] << "PE\n"; dodane[i] += dodane[curr] + oryg[curr] - log10(tab[curr]); bool czy = 0; for (int x = 0; x < oryg[i]; ++x) { if (taba[x] > tabb[x]) { czy = 1; bb *= 10; while (bb >= 1e16)bb /= 10; curr = i; tab[i] = bb; dodane[i]++; break; } if (taba[x] < tabb[x]) { czy = 1; bb %= EEE; curr = i; tab[i] = bb; break; } } if (czy)continue; int czy2 = 1; for (int x = sz(taba) - 1; x >= oryg[i]; --x) { if (taba[x] == 10 && x == oryg[i]) { czy2 = 1; break; } if (taba[x] == 9) { if (x - 1 == oryg[i] - 1) { czy2 = 1; break; } else { czy2 = 0; tabb[x] = 0; taba[x - 1]++; if (taba[x - 1] < 10 && x - 1 != oryg[i] - 1) { for (int y = x - 1; y >= oryg[i]; --y) { tabb[y] = taba[y]; } break; } } } else { czy2 = 0; tabb[x] = taba[x] + 1; for (int y = x - 1; y >= oryg[i]; --y) { tabb[y] = taba[y]; } break; } } if (czy2) { bb *= 10; while (bb >= 1e16)bb /= 10; curr = i; tab[i] = bb; dodane[i]++; continue; } else { ll xd = 0; for (int x = 0; x < sz(tabb); ++x) { xd *= 10; xd += tabb[x]; } tab[i] = xd; curr = i; } } for (int i = 0; i < N; ++i) { //cout << dodane[i] << "\t" << i << "\n"; //if(dodane[i] < 0)break; ans += dodane[i]; } cout << ans << "\n"; 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 | #include <bits/stdc++.h> #define F first #define S second #define ll long long #define ld long double #define vi vector < int > #define vll vector < ll > #define pb push_back #define sz(x) int((x).size()) #define pii pair < int, int > #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define vpii vector < pii > #define sqr(x) ((x)*(x)) #define fenwick(x) ((x) & -(x)) #define debug(x) cout << "[ " << #x << " ]: " << x << "\n"; using namespace std; //CZY DANE POSORTOWANE, N = 0, N = 1, CZYTANIE TRESCI, < rosnaco, > malejaco const int MAX = 2e5 + 12; const ll MOD = 1e10; const ll EEE = 1e16; ll tab[MAX]; int oryg[MAX]; ll dodane[MAX]; int log10(ll a) { int w = 0; while (a) { ++w; a /= 10; } return w; } ll ppow(int ile) { if (ile == 0)return 1; if (ile == 1)return 10; if (ile == 2)return 100; if (ile == 3)return 1000; if (ile == 4)return 10000; if (ile == 5)return 100000; if (ile == 6)return 1000000; if (ile == 7)return 10000000; if (ile == 8)return 100000000; if (ile == 9)return 1000000000; if (ile == 10)return 10000000000; if (ile == 11)return 1e11; if (ile == 12)return 1e12; if (ile == 13)return 1e13; if (ile == 14)return 1e14; if (ile == 15)return 1e15; if (ile == 16)return 1e16; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N; ll ans = 0; int czy_dodalem = 0; cin >> N; for (int i = 0; i < N; ++i) { cin >> tab[i]; oryg[i] = log10(tab[i]); } int curr = 0; for (int i = 1; i < N; ++i) { vi taba, tabb; ll aa = tab[curr], bb = tab[i]; if (aa < bb) { curr = i; continue; } ll cpbb = bb; while (aa){ taba.pb(aa % 10); aa /= 10;} while (cpbb){tabb.pb(cpbb % 10);cpbb /= 10; } reverse(all(tabb)); reverse(all(taba)); while (sz(tabb) != sz(taba)) { tabb.pb(0); bb *= 10; ++dodane[i]; } //cout << dodane[curr] << "+\t" << oryg[curr] << "-\t" << log10(aa) << "-\t" << oryg[i] << "\n"; //cout << dodane[curr] + oryg[curr] - log10(aa) - oryg[i] << "PE\n"; dodane[i] += dodane[curr] + oryg[curr] - log10(tab[curr]); bool czy = 0; for (int x = 0; x < oryg[i]; ++x) { if (taba[x] > tabb[x]) { czy = 1; bb *= 10; while (bb >= 1e16)bb /= 10; curr = i; tab[i] = bb; dodane[i]++; break; } if (taba[x] < tabb[x]) { czy = 1; bb %= EEE; curr = i; tab[i] = bb; break; } } if (czy)continue; int czy2 = 1; for (int x = sz(taba) - 1; x >= oryg[i]; --x) { if (taba[x] == 10 && x == oryg[i]) { czy2 = 1; break; } if (taba[x] == 9) { if (x - 1 == oryg[i] - 1) { czy2 = 1; break; } else { czy2 = 0; tabb[x] = 0; taba[x - 1]++; if (taba[x - 1] < 10 && x - 1 != oryg[i] - 1) { for (int y = x - 1; y >= oryg[i]; --y) { tabb[y] = taba[y]; } break; } } } else { czy2 = 0; tabb[x] = taba[x] + 1; for (int y = x - 1; y >= oryg[i]; --y) { tabb[y] = taba[y]; } break; } } if (czy2) { bb *= 10; while (bb >= 1e16)bb /= 10; curr = i; tab[i] = bb; dodane[i]++; continue; } else { ll xd = 0; for (int x = 0; x < sz(tabb); ++x) { xd *= 10; xd += tabb[x]; } tab[i] = xd; curr = i; } } for (int i = 0; i < N; ++i) { //cout << dodane[i] << "\t" << i << "\n"; //if(dodane[i] < 0)break; ans += dodane[i]; } cout << ans << "\n"; return 0; } |