#include <iostream> #include <cmath> using namespace std; int len(int n) { int w = 0; while (n) {w++; n/=10;} return w; //return log10(n)+1; } int p10[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000}; struct Number { int a; int k,b; Number(istream & cin) : k(0), b(0) { cin >> a;} Number(int a, int k, int b) :a(a),k(k),b(b){} void show() {cout << a<< "e"<<k<<" +"<<b<<endl;} int dif(int n) { return len(a)+k-len(n); } Number next(int n) { int da = len(a); int dn = len(n); if (da>dn) { int a1=a,r=0,p=1; //for (int i=0;i<da-dn;i++) {r=r+a1%10*p; a1/=10; p*=10;} r = a1%p10[da-dn]; a1 /= p10[da-dn]; //cout << a1 << " "<<r<<endl; if (n==a1) { int b1 = b + 1; if (len(b1)>k) { int r1=r+1; if (len(r1)>len(r)) return Number(n,da-dn+k+1,0); else return Number(a+1,k,0); } else return Number(a,k,b+1); } else if (n>a1) { return Number(n,da-dn+k,0); } else { // n<a1 return Number(n,da-dn+k+1,0); } } else if (da==dn) { if (n==a) { int b1 = b + 1; if (len(b1)>k) return Number(a,k+1,0); else return Number(a,k,b+1); } else if (n>a) { return Number(n,k,0); } else { // n<a return Number(n,k+1,0); } } else { //da<dn if (dn>da+k) return Number(n,0,0); else { long long a1=a; int b1=b, b2=b,k1=k; //for (int i=0;i<dn-da;i++) {a1*=10;k1--;} a1*=p10[dn-da]; k1-=dn-da; //cout << a1 << endl; if (k-len(b1)<dn-da) { int p=1; b2=0; for (int i=0;i<k-(dn-da);i++){b2=b2+b1%10*p; b1/=10; p*=10;} a1 += b1; } //cout << a1 <<" "<<k1<<" "<<b2<< endl; if (n==a1) { int b1 = b2 + 1; if (len(b1)>k1) return Number(n,k1+1,0); else return Number(a1,k1,b1); } else if (n>a1) { return Number(n,k1,0); } else { // n<a1 return Number(n,k1+1,0); } } } } }; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; Number a(cin); //a.show(); n--; long long answer= 0; while (n--) { int a1; cin >> a1; a = a.next(a1); answer += a.dif(a1); //a.show(); } cout << answer <<endl; 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 | #include <iostream> #include <cmath> using namespace std; int len(int n) { int w = 0; while (n) {w++; n/=10;} return w; //return log10(n)+1; } int p10[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000}; struct Number { int a; int k,b; Number(istream & cin) : k(0), b(0) { cin >> a;} Number(int a, int k, int b) :a(a),k(k),b(b){} void show() {cout << a<< "e"<<k<<" +"<<b<<endl;} int dif(int n) { return len(a)+k-len(n); } Number next(int n) { int da = len(a); int dn = len(n); if (da>dn) { int a1=a,r=0,p=1; //for (int i=0;i<da-dn;i++) {r=r+a1%10*p; a1/=10; p*=10;} r = a1%p10[da-dn]; a1 /= p10[da-dn]; //cout << a1 << " "<<r<<endl; if (n==a1) { int b1 = b + 1; if (len(b1)>k) { int r1=r+1; if (len(r1)>len(r)) return Number(n,da-dn+k+1,0); else return Number(a+1,k,0); } else return Number(a,k,b+1); } else if (n>a1) { return Number(n,da-dn+k,0); } else { // n<a1 return Number(n,da-dn+k+1,0); } } else if (da==dn) { if (n==a) { int b1 = b + 1; if (len(b1)>k) return Number(a,k+1,0); else return Number(a,k,b+1); } else if (n>a) { return Number(n,k,0); } else { // n<a return Number(n,k+1,0); } } else { //da<dn if (dn>da+k) return Number(n,0,0); else { long long a1=a; int b1=b, b2=b,k1=k; //for (int i=0;i<dn-da;i++) {a1*=10;k1--;} a1*=p10[dn-da]; k1-=dn-da; //cout << a1 << endl; if (k-len(b1)<dn-da) { int p=1; b2=0; for (int i=0;i<k-(dn-da);i++){b2=b2+b1%10*p; b1/=10; p*=10;} a1 += b1; } //cout << a1 <<" "<<k1<<" "<<b2<< endl; if (n==a1) { int b1 = b2 + 1; if (len(b1)>k1) return Number(n,k1+1,0); else return Number(a1,k1,b1); } else if (n>a1) { return Number(n,k1,0); } else { // n<a1 return Number(n,k1+1,0); } } } } }; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; Number a(cin); //a.show(); n--; long long answer= 0; while (n--) { int a1; cin >> a1; a = a.next(a1); answer += a.dif(a1); //a.show(); } cout << answer <<endl; return 0; } |