#include <bits/stdc++.h>
using namespace std;
#define e1 first
#define e2 second
#define pb push_back
#define mp make_pair
#define boost ios_base::sync_with_stdio(false)
#define eb emplace_back
#define OUT(x) {cout << x; exit(0); }
#define FOR(i, a, b) for (int i=(a); i<=(b); ++i)
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> PII;
typedef pair <ll, ll> PLL;
typedef pair <PLL, PLL> PP;
typedef unsigned int ui;
const int mod = 1e9+7;
const int inf = 1e9+9;
const ll MOD = 1e9+696969;
const ll INF = 1e18;
const int maxn = 250000;
char tab[maxn];
string s;
int TRUELEN, LEN, lastnonzero, n, m;
ll result;
void put(int number_of_zeros) {
FOR(i, 0, 11) tab[i] = '0';
FOR(i, lastnonzero, LEN) tab[i] = '0';
FOR(i, 1, m) tab[i] = s[i];
LEN = m + number_of_zeros;
lastnonzero = LEN + 1;
}
int compare(int d) {
FOR(i, 1, d)
if (tab[i] > s[i]) return 1;
else if (tab[i] < s[i]) return -1;
return 0;
}
void increase(int pos) {
if (pos == m) {
put(LEN - m + 1);
}
else {
if (tab[pos] != '9') tab[pos]++;
else tab[pos] = '0', increase(pos - 1);
}
}
void update() {
if (m > LEN) {
put(0);
return;
}
int c = compare(m);
if (c == -1) put(LEN - m);
else if (c == 1) put(LEN - m + 1);
else increase(LEN);
}
int main()
{
boost;
cin >> n;
LEN = 1; lastnonzero = 2;
FOR(i, 0, maxn-1) tab[i] = '0';
TRUELEN = 1;
result = 0;
FOR(step, 1, n) {
cin >> s;
s = "#" + s;
m = s.length() - 1;
update();
result += LEN - (int)s.length() + 1;
TRUELEN = (int)s.length() - 1;
}
cout << 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 | #include <bits/stdc++.h> using namespace std; #define e1 first #define e2 second #define pb push_back #define mp make_pair #define boost ios_base::sync_with_stdio(false) #define eb emplace_back #define OUT(x) {cout << x; exit(0); } #define FOR(i, a, b) for (int i=(a); i<=(b); ++i) typedef long long ll; typedef unsigned long long ull; typedef pair <int, int> PII; typedef pair <ll, ll> PLL; typedef pair <PLL, PLL> PP; typedef unsigned int ui; const int mod = 1e9+7; const int inf = 1e9+9; const ll MOD = 1e9+696969; const ll INF = 1e18; const int maxn = 250000; char tab[maxn]; string s; int TRUELEN, LEN, lastnonzero, n, m; ll result; void put(int number_of_zeros) { FOR(i, 0, 11) tab[i] = '0'; FOR(i, lastnonzero, LEN) tab[i] = '0'; FOR(i, 1, m) tab[i] = s[i]; LEN = m + number_of_zeros; lastnonzero = LEN + 1; } int compare(int d) { FOR(i, 1, d) if (tab[i] > s[i]) return 1; else if (tab[i] < s[i]) return -1; return 0; } void increase(int pos) { if (pos == m) { put(LEN - m + 1); } else { if (tab[pos] != '9') tab[pos]++; else tab[pos] = '0', increase(pos - 1); } } void update() { if (m > LEN) { put(0); return; } int c = compare(m); if (c == -1) put(LEN - m); else if (c == 1) put(LEN - m + 1); else increase(LEN); } int main() { boost; cin >> n; LEN = 1; lastnonzero = 2; FOR(i, 0, maxn-1) tab[i] = '0'; TRUELEN = 1; result = 0; FOR(step, 1, n) { cin >> s; s = "#" + s; m = s.length() - 1; update(); result += LEN - (int)s.length() + 1; TRUELEN = (int)s.length() - 1; } cout << result; } |
English