#include <iostream> #include <algorithm> using namespace std; const int N = 200005; unsigned long long int ile( unsigned long long int n ) { unsigned long long int r = 0; if( n == 0 ) return 1; else { while( n > 0 ) { r ++; n /= 10; } return r; } } unsigned long long int wszystkie[N]; unsigned long long int a[N]; unsigned long long int CE( unsigned long long int a, unsigned long long int b ) { unsigned long long int d = ile(a) - ile(b); unsigned long long int pow = 1; unsigned long long int v = a; for( int i = 0; i < d; i ++ ) { v /= 10; //pow *= 10; } //unsigned long long int v = a/pow; return v; } struct para { unsigned long long int d; bool flag; }; bool f( unsigned long long int k ) { bool flag = false; k ++; int r = 10; while( true ) { if( k == r ) { flag = true; break; } if( r > k ) break; r *= 10; } return flag; } para DE( unsigned long long int a, unsigned long long int b ) { unsigned long long int c = CE(a,b); int e = ile(a) - ile(b); unsigned long long int pow = 1; for( int i = 0; i < e; i ++ ) { pow *= 10; } unsigned long long int v = a - pow*c; para X; X.d = v; bool w; //cout << ile(v) << " " << e << "\n"; if( ile(v) == e && f(v) == true ) w = true; else w = false; X.flag = w; return X; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; //para Y = DE( 599999900000, 59 ); //cout << Y.d << "\n"; //if( Y.flag == false ) cout << "NIE"; //else cout << "TAK"; //cout << CE(599999900000, 59); unsigned long long int tab[n]; for( int i = 0; i < n; i ++ ) { cin >> tab[i]; wszystkie[i] = ile(tab[i]); a[i] = tab[i]; } wszystkie[0] = ile(tab[0]); a[0] = tab[0]; for( int i = 1; i < n; i ++ ) { unsigned long long int b = tab[i]; if( wszystkie[i - 1] == ile(b) ) { if( a[i-1] >= b ) { a[i] = b*10; if( a[i] > 100000000000000000 ) a[i] /= 10; wszystkie[i] = ile(b) + 1; } } else if( wszystkie[i-1] > ile(b) ) { unsigned long long int c = CE(a[i-1],b); //cout << b << " CE =" << c <<" "; if( b > c ) { a[i] = b; int r = 0; while( r < wszystkie[i-1] - ile(b) ) { a[i] *= 10; if( a[i] > 100000000000000000 ) { a[i] /= 10; break; } r ++; } wszystkie[i] = wszystkie[i-1]; } else if( b == c ) { if( wszystkie[i-1] - ile(b) < 18 ) { unsigned long long int d = DE(a[i-1],b).d; //cout << "panek to:" << d << "\n"; if( DE(a[i-1],b).flag == false ) { a[i] = a[i - 1] + 1; wszystkie[i] = wszystkie[i-1]; } else { a[i] = b; int r = 0; while( r < wszystkie[i-1] - ile(b) + 1 ) { a[i] *= 10; r ++; if( a[i] > 100000000000000000 ) { a[i] /= 10; break; } } wszystkie[i] = wszystkie[i-1] + 1; } } else { a[i] = a[i-1]; wszystkie[i] = wszystkie[i-1]; } } else if( b < c ) { a[i] = b; int r = 0; while( r < wszystkie[i-1] - ile(b) + 1 ) { a[i] *= 10; r ++; if( a[i] > 100000000000000000 ) { a[i] /= 10; break; } } wszystkie[i] = wszystkie[i-1] + 1; } } //cout << a[i] << "\n"; //cout << wszystkie[i] << " " << a[i] << "\n"; } unsigned long long int wynik = 0; for( int i = 0; i < n; i ++ ) { wynik += ( wszystkie[i] - ile(tab[i]) ); } cout << wynik; 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 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | #include <iostream> #include <algorithm> using namespace std; const int N = 200005; unsigned long long int ile( unsigned long long int n ) { unsigned long long int r = 0; if( n == 0 ) return 1; else { while( n > 0 ) { r ++; n /= 10; } return r; } } unsigned long long int wszystkie[N]; unsigned long long int a[N]; unsigned long long int CE( unsigned long long int a, unsigned long long int b ) { unsigned long long int d = ile(a) - ile(b); unsigned long long int pow = 1; unsigned long long int v = a; for( int i = 0; i < d; i ++ ) { v /= 10; //pow *= 10; } //unsigned long long int v = a/pow; return v; } struct para { unsigned long long int d; bool flag; }; bool f( unsigned long long int k ) { bool flag = false; k ++; int r = 10; while( true ) { if( k == r ) { flag = true; break; } if( r > k ) break; r *= 10; } return flag; } para DE( unsigned long long int a, unsigned long long int b ) { unsigned long long int c = CE(a,b); int e = ile(a) - ile(b); unsigned long long int pow = 1; for( int i = 0; i < e; i ++ ) { pow *= 10; } unsigned long long int v = a - pow*c; para X; X.d = v; bool w; //cout << ile(v) << " " << e << "\n"; if( ile(v) == e && f(v) == true ) w = true; else w = false; X.flag = w; return X; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; //para Y = DE( 599999900000, 59 ); //cout << Y.d << "\n"; //if( Y.flag == false ) cout << "NIE"; //else cout << "TAK"; //cout << CE(599999900000, 59); unsigned long long int tab[n]; for( int i = 0; i < n; i ++ ) { cin >> tab[i]; wszystkie[i] = ile(tab[i]); a[i] = tab[i]; } wszystkie[0] = ile(tab[0]); a[0] = tab[0]; for( int i = 1; i < n; i ++ ) { unsigned long long int b = tab[i]; if( wszystkie[i - 1] == ile(b) ) { if( a[i-1] >= b ) { a[i] = b*10; if( a[i] > 100000000000000000 ) a[i] /= 10; wszystkie[i] = ile(b) + 1; } } else if( wszystkie[i-1] > ile(b) ) { unsigned long long int c = CE(a[i-1],b); //cout << b << " CE =" << c <<" "; if( b > c ) { a[i] = b; int r = 0; while( r < wszystkie[i-1] - ile(b) ) { a[i] *= 10; if( a[i] > 100000000000000000 ) { a[i] /= 10; break; } r ++; } wszystkie[i] = wszystkie[i-1]; } else if( b == c ) { if( wszystkie[i-1] - ile(b) < 18 ) { unsigned long long int d = DE(a[i-1],b).d; //cout << "panek to:" << d << "\n"; if( DE(a[i-1],b).flag == false ) { a[i] = a[i - 1] + 1; wszystkie[i] = wszystkie[i-1]; } else { a[i] = b; int r = 0; while( r < wszystkie[i-1] - ile(b) + 1 ) { a[i] *= 10; r ++; if( a[i] > 100000000000000000 ) { a[i] /= 10; break; } } wszystkie[i] = wszystkie[i-1] + 1; } } else { a[i] = a[i-1]; wszystkie[i] = wszystkie[i-1]; } } else if( b < c ) { a[i] = b; int r = 0; while( r < wszystkie[i-1] - ile(b) + 1 ) { a[i] *= 10; r ++; if( a[i] > 100000000000000000 ) { a[i] /= 10; break; } } wszystkie[i] = wszystkie[i-1] + 1; } } //cout << a[i] << "\n"; //cout << wszystkie[i] << " " << a[i] << "\n"; } unsigned long long int wynik = 0; for( int i = 0; i < n; i ++ ) { wynik += ( wszystkie[i] - ile(tab[i]) ); } cout << wynik; return 0; } |