#include <iostream> typedef long long int llint; llint pow10(llint p) { llint ret=1; while(p){ret*=10;--p;} return ret; } llint digs(llint p) { llint ret=0; while(p){++ret;p/=10;} return ret; } llint mmax(llint a,llint b) { return a>b?a:b; } struct num { llint original; llint start; llint offset=0; llint end=0; llint added() const { return digs(start)+offset-digs(original); } void inc() { ++end; if(end==pow10(offset)) { ++offset; end=0; } } #if 1 static void make_larger_than(num& a,const num& b) { llint a_dec=pow10(9); llint a_digs=10; while(!(a.start/a_dec)) {a_dec/=10;--a_digs;} llint b_dec=pow10(9); llint b_digs=10; while(!(b.start/b_dec)) {b_dec/=10;--b_digs;} llint digs_diff=a_digs-(b_digs+b.offset); if(digs_diff>0)return; llint a_start=a.start; llint b_start=b.start; while(a_start&&b_start) { llint a_dig=a_start/a_dec; llint b_dig=b_start/b_dec; if(a_dig>b_dig) { a.offset=mmax(0,-digs_diff); return; } else if(a_dig<b_dig) { a.offset=mmax(0,-digs_diff+1); return; } a_start%=a_dec;a_dec/=10; b_start%=b_dec;b_dec/=10; } if(a_start) { a.offset=mmax(0,-digs_diff); } else if(b_start) { if(b.end+1!=pow10(b.offset)) { a.offset=mmax(0,-digs_diff); a.end=b.end; a.inc(); } else { a.start*=pow10(b_digs-a_digs); ++b_start; if(b_start==pow10(b_digs-a_digs)) a.start*=10; else a.start+=b_start; a.offset=b.offset; a.end=0; } } else { a.offset=mmax(0,-digs_diff); a.end=b.end; a.inc(); } } #else #endif }; int main() { llint N; std::cin>>N; num prev; std::cin>>prev.original; prev.start=prev.original; llint res = 0; for(llint i=1;i<N;++i) { num curr; std::cin>>curr.original; curr.start=curr.original; num::make_larger_than(curr,prev); while(!(curr.start%10)){curr.start/=10;++curr.offset;} llint p=pow10(9); llint p_index=10; while(p&&0) { if(digs(curr.start)+curr.offset>16) break; if(curr.end/p) { llint extra_offset=curr.offset-p_index; curr.start*=pow10(extra_offset); curr.start+=curr.end/p; curr.offset-=extra_offset; curr.end=curr.end%p; } p/=10; --p_index; } //printf("i:%i start=%d offset=%d end=%d\n",(int)i,(int)curr.start,(int)curr.offset,(int)curr.end); res+=curr.added(); prev=curr; } std::cout<<res; return 0; } /* 2 1234 1 2 1234 2 4 39 40 40 41 */
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 | #include <iostream> typedef long long int llint; llint pow10(llint p) { llint ret=1; while(p){ret*=10;--p;} return ret; } llint digs(llint p) { llint ret=0; while(p){++ret;p/=10;} return ret; } llint mmax(llint a,llint b) { return a>b?a:b; } struct num { llint original; llint start; llint offset=0; llint end=0; llint added() const { return digs(start)+offset-digs(original); } void inc() { ++end; if(end==pow10(offset)) { ++offset; end=0; } } #if 1 static void make_larger_than(num& a,const num& b) { llint a_dec=pow10(9); llint a_digs=10; while(!(a.start/a_dec)) {a_dec/=10;--a_digs;} llint b_dec=pow10(9); llint b_digs=10; while(!(b.start/b_dec)) {b_dec/=10;--b_digs;} llint digs_diff=a_digs-(b_digs+b.offset); if(digs_diff>0)return; llint a_start=a.start; llint b_start=b.start; while(a_start&&b_start) { llint a_dig=a_start/a_dec; llint b_dig=b_start/b_dec; if(a_dig>b_dig) { a.offset=mmax(0,-digs_diff); return; } else if(a_dig<b_dig) { a.offset=mmax(0,-digs_diff+1); return; } a_start%=a_dec;a_dec/=10; b_start%=b_dec;b_dec/=10; } if(a_start) { a.offset=mmax(0,-digs_diff); } else if(b_start) { if(b.end+1!=pow10(b.offset)) { a.offset=mmax(0,-digs_diff); a.end=b.end; a.inc(); } else { a.start*=pow10(b_digs-a_digs); ++b_start; if(b_start==pow10(b_digs-a_digs)) a.start*=10; else a.start+=b_start; a.offset=b.offset; a.end=0; } } else { a.offset=mmax(0,-digs_diff); a.end=b.end; a.inc(); } } #else #endif }; int main() { llint N; std::cin>>N; num prev; std::cin>>prev.original; prev.start=prev.original; llint res = 0; for(llint i=1;i<N;++i) { num curr; std::cin>>curr.original; curr.start=curr.original; num::make_larger_than(curr,prev); while(!(curr.start%10)){curr.start/=10;++curr.offset;} llint p=pow10(9); llint p_index=10; while(p&&0) { if(digs(curr.start)+curr.offset>16) break; if(curr.end/p) { llint extra_offset=curr.offset-p_index; curr.start*=pow10(extra_offset); curr.start+=curr.end/p; curr.offset-=extra_offset; curr.end=curr.end%p; } p/=10; --p_index; } //printf("i:%i start=%d offset=%d end=%d\n",(int)i,(int)curr.start,(int)curr.offset,(int)curr.end); res+=curr.added(); prev=curr; } std::cout<<res; return 0; } /* 2 1234 1 2 1234 2 4 39 40 40 41 */ |