#include<iostream> #include<algorithm> using namespace std; const long long P[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000}; long long S = 0; long long next(long long x, long long y){ int e = 0; for(long long z = y;z <= x;z = 10 * z + 9) e++; S += e; if(P[e] * y > x) return P[e] * y; return x + 1; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; long long a[n]; for(int i = 0;i < n;i++) cin >> a[i]; int I, J = 0; for(I = 1;I < n && a[I - 1] < P[16];I++) a[I] = next(a[I - 1], a[I]); for(int i = I;i < n;i++){ while(a[i] < P[16]){ a[i] *= 10; S++; } if(a[i] >= a[i - 1]) S += J; else S += ++J; } cout << S << endl; }
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 | #include<iostream> #include<algorithm> using namespace std; const long long P[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000}; long long S = 0; long long next(long long x, long long y){ int e = 0; for(long long z = y;z <= x;z = 10 * z + 9) e++; S += e; if(P[e] * y > x) return P[e] * y; return x + 1; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; long long a[n]; for(int i = 0;i < n;i++) cin >> a[i]; int I, J = 0; for(I = 1;I < n && a[I - 1] < P[16];I++) a[I] = next(a[I - 1], a[I]); for(int i = I;i < n;i++){ while(a[i] < P[16]){ a[i] *= 10; S++; } if(a[i] >= a[i - 1]) S += J; else S += ++J; } cout << S << endl; } |