#include <iostream> #include <algorithm> using namespace std; #define I long long #define MAX_DIG 17 #define D(x) int dig(I x, int r=0) { if(!x) return r; return dig(x/10,r+1); } I m10(int v, I r=1) { if(v<=0) return r; return m10(v-1, 10*r); } int find_near(I x,I l, I &y) { int ld = dig(l),xd = dig(x); int rd = max(ld-xd, 0); I m = m10(rd); I r = l-x*m; D(cout << "rd:" << rd << " m:" << m << " r:" << r << "\n"); if(r<0) { y=x*m; return rd; } if((r+1)/m>0) { y=x*m*10; return rd+1; } y = x*m+r+1; return rd; } int main() { I n,x,y,l=0,w=0,z=0; cin >> n; while(n--) { cin >> x; int d = find_near(x,l,y); D(cout << l << " " << x << " :" << y << " d:" << d << "\n"); w+=d+z; if (dig(y) > MAX_DIG) { y/=10; z++; } l=y; } cout << w << "\n"; }
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 | #include <iostream> #include <algorithm> using namespace std; #define I long long #define MAX_DIG 17 #define D(x) int dig(I x, int r=0) { if(!x) return r; return dig(x/10,r+1); } I m10(int v, I r=1) { if(v<=0) return r; return m10(v-1, 10*r); } int find_near(I x,I l, I &y) { int ld = dig(l),xd = dig(x); int rd = max(ld-xd, 0); I m = m10(rd); I r = l-x*m; D(cout << "rd:" << rd << " m:" << m << " r:" << r << "\n"); if(r<0) { y=x*m; return rd; } if((r+1)/m>0) { y=x*m*10; return rd+1; } y = x*m+r+1; return rd; } int main() { I n,x,y,l=0,w=0,z=0; cin >> n; while(n--) { cin >> x; int d = find_near(x,l,y); D(cout << l << " " << x << " :" << y << " d:" << d << "\n"); w+=d+z; if (dig(y) > MAX_DIG) { y/=10; z++; } l=y; } cout << w << "\n"; } |