#include <iostream> #include <math.h> using ull = unsigned long long int; using namespace std; typedef struct big_num { int log10 = 0; int size = 1; ull numbers[12000]; } big_num; void print_big_num(big_num* a) { printf("%llu", a->numbers[0]); for (int i = 1; i < a->size; ++i) { printf("%018llu", a->numbers[i]); } printf("\n"); } bool is_bigger_or_equal(big_num* a, big_num* b) { if (a->size > b->size) return true; if (a->size == b->size) { for (int i = 0; i < a->size-1; ++i) { if (a->numbers[i] > b->numbers[i]) return true; } if (a->numbers[a->size - 1] >= b->numbers[b->size - 1]) return true; } return false; } bool is_diff_major_bigger_than_current(big_num* a, ull current, int current_log10) { int segment_magnitude = (a->log10) % 18; int offset_magnitude = current_log10 - segment_magnitude + 1; ull tmp = a->numbers[0] % (ull)pow(10, segment_magnitude + 1); if (current_log10 < segment_magnitude) { tmp /= (ull)pow(10, segment_magnitude - current_log10); } if (offset_magnitude > 0 && a->size > 1) { tmp *= (ull)pow(10, offset_magnitude); tmp += a->numbers[1] / (ull)pow(10, offset_magnitude); } // divide a by 10^(magnitude) and compare to the current // int segment = magnitude / 18; // int offset = magnitude % 18; // ull offset_pow = pow(10, offset); // if (a->size < segment + 1) { // return false; // } // if (a->size > segment + 2) { // return true; // } // ull tmp = a->numbers[segment] / offset_pow; // // cout << a->numbers[segment] << endl; // // cout << "tmp: " << tmp << endl; // if (a->size >= segment + 2) // tmp *= (a->numbers[segment - 1] % offset_pow); // cout << "segment : offset = " << segment_magnitude << " : " << offset_magnitude << endl; // cout << "current_log10: " << current_log10 << endl; // cout << "a.log10: " << a->log10 << endl; // cout << a->numbers[0] << endl; // // cout << "magnitude: " << magnitude << endl; // // cout << offset_pow << endl; // // print_big_num(a); // cout << "tmp: " << tmp << endl; // cout << "current: " << current << endl; // // } // if (tmp > current) // cout << "TAK" << endl; // else // cout << "NIE" << endl; return tmp > current; } void lift(big_num* a, int magnitude) { int blocks = magnitude / 18; int offset = magnitude % 18; ull offset_pow = pow(10, offset); a->size += blocks; if (offset > 0) { ull tmp = 0; ull tmp_prev = 0; // cout << "size: " << a->size << endl; for (int i = a->size - 1; i >= 0; --i) { tmp = 0; // tmp = a->numbers[i] / offset_pow; // a->numbers[i] = a->numbers[i] % offset_pow; for (int j = 0; j < offset; ++j) { a->numbers[i] *= 10; tmp *= 10; tmp += a->numbers[i] / (ull)1e18; a->numbers[i] = a->numbers[i] % (ull)1e18; } a->numbers[i] += tmp_prev; tmp_prev = tmp; } if (tmp > 0) { a->size++; for (int i = a->size - 1; i > 0; --i) { a->numbers[i] = a->numbers[i-1]; } a->numbers[0] = tmp; } } a->log10 += magnitude; } bool check_mod_part_is_almost_full(big_num* a, int magnitude) { int blocks = magnitude / 18; int offset = magnitude % 18; ull offset_pow = pow(10, offset); for (int i = a->size - 1; i >= a->size - blocks; --i) { if (a->numbers[i] != (ull)1e18-1) { return false; // cout << "NIE1" << endl; } } if ((a->numbers[a->size - blocks - 1] % offset_pow) + 1 == offset_pow) { // cout << (a->numbers[a->size - blocks - 1] % offset_pow) << endl; // cout << "TAK" << endl; return true; } // cout << "NIE2" << endl; return false; } void increase_by_mod_part_plus_one(big_num* a, big_num* b, int magnitude) { // cout << "INC" << endl; // print_big_num(a); // print_big_num(b); // cout << "/INC" << endl; int blocks = magnitude / 18; int offset = magnitude % 18; ull offset_pow = pow(10, offset); for (int i = a->size - 1; i > a->size - blocks - 1; --i) { a->numbers[i] = b->numbers[i]; // cout << "cos" << endl; } // cout << "b mod : " << b->numbers[a->size - blocks - 1] % offset_pow << endl; a->numbers[a->size - blocks - 1] += b->numbers[a->size - blocks - 1] % offset_pow; ull tmp = 0; a->numbers[a->size - 1] += 1; tmp = a->numbers[a->size - 1] / (ull)1e18; // cout << "tmp: " << tmp << endl; int i = a->size - 1; while(tmp) { a->numbers[i] = a->numbers[i] % (ull)1e18; --i; tmp = a->numbers[i] / (ull)1e18; } } int main() { int n; cin >> n; big_num *prev = new big_num; big_num *current = new big_num; ull counter = 0; cin >> prev->numbers[0]; prev->size = 1; prev->log10 = floor(log10(prev->numbers[0])); for (int i = 1; i < n; ++i) { cin >> current->numbers[0]; ull current_bak = current->numbers[0]; current->size = 1; current->log10 = floor(log10(current->numbers[0])); int current_bak_log10 = current->log10; if (is_bigger_or_equal(prev, current)) { int log_diff = prev->log10 - current->log10; lift(current, log_diff); // print_big_num(current); // ull increaser = pow(10, log_diff); // current->numbers[0] *= increaser; // current->log10 += log_diff; counter += log_diff; if (is_bigger_or_equal(prev, current)) { // ull diff_major = (prev->numbers[0] / increaser) - current_bak; // ull diff_mod = prev->numbers[0] % increaser; if (is_diff_major_bigger_than_current(prev, current_bak, current_bak_log10) || check_mod_part_is_almost_full(prev, log_diff)) { // if (diff_major > 0 || ((diff_mod + 1) == increaser)) { lift(current, 1); // current->numbers[0] *= 10; // current->log10++; counter++; } else { increase_by_mod_part_plus_one(current, prev, log_diff); // current->numbers[0] += diff_mod + 1; } } } big_num* tmp = prev; prev = current; current = tmp; for (int i = 0; i < current->size; ++i) current->numbers[i] = 0; current->size = 1; // printf("prev: "); // print_big_num(prev); // cout << "prev: " << prev->numbers[0] << " : " << prev->numbers[1] << endl; } cout << counter << 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 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 197 198 199 200 201 202 203 204 205 206 207 208 209 | #include <iostream> #include <math.h> using ull = unsigned long long int; using namespace std; typedef struct big_num { int log10 = 0; int size = 1; ull numbers[12000]; } big_num; void print_big_num(big_num* a) { printf("%llu", a->numbers[0]); for (int i = 1; i < a->size; ++i) { printf("%018llu", a->numbers[i]); } printf("\n"); } bool is_bigger_or_equal(big_num* a, big_num* b) { if (a->size > b->size) return true; if (a->size == b->size) { for (int i = 0; i < a->size-1; ++i) { if (a->numbers[i] > b->numbers[i]) return true; } if (a->numbers[a->size - 1] >= b->numbers[b->size - 1]) return true; } return false; } bool is_diff_major_bigger_than_current(big_num* a, ull current, int current_log10) { int segment_magnitude = (a->log10) % 18; int offset_magnitude = current_log10 - segment_magnitude + 1; ull tmp = a->numbers[0] % (ull)pow(10, segment_magnitude + 1); if (current_log10 < segment_magnitude) { tmp /= (ull)pow(10, segment_magnitude - current_log10); } if (offset_magnitude > 0 && a->size > 1) { tmp *= (ull)pow(10, offset_magnitude); tmp += a->numbers[1] / (ull)pow(10, offset_magnitude); } // divide a by 10^(magnitude) and compare to the current // int segment = magnitude / 18; // int offset = magnitude % 18; // ull offset_pow = pow(10, offset); // if (a->size < segment + 1) { // return false; // } // if (a->size > segment + 2) { // return true; // } // ull tmp = a->numbers[segment] / offset_pow; // // cout << a->numbers[segment] << endl; // // cout << "tmp: " << tmp << endl; // if (a->size >= segment + 2) // tmp *= (a->numbers[segment - 1] % offset_pow); // cout << "segment : offset = " << segment_magnitude << " : " << offset_magnitude << endl; // cout << "current_log10: " << current_log10 << endl; // cout << "a.log10: " << a->log10 << endl; // cout << a->numbers[0] << endl; // // cout << "magnitude: " << magnitude << endl; // // cout << offset_pow << endl; // // print_big_num(a); // cout << "tmp: " << tmp << endl; // cout << "current: " << current << endl; // // } // if (tmp > current) // cout << "TAK" << endl; // else // cout << "NIE" << endl; return tmp > current; } void lift(big_num* a, int magnitude) { int blocks = magnitude / 18; int offset = magnitude % 18; ull offset_pow = pow(10, offset); a->size += blocks; if (offset > 0) { ull tmp = 0; ull tmp_prev = 0; // cout << "size: " << a->size << endl; for (int i = a->size - 1; i >= 0; --i) { tmp = 0; // tmp = a->numbers[i] / offset_pow; // a->numbers[i] = a->numbers[i] % offset_pow; for (int j = 0; j < offset; ++j) { a->numbers[i] *= 10; tmp *= 10; tmp += a->numbers[i] / (ull)1e18; a->numbers[i] = a->numbers[i] % (ull)1e18; } a->numbers[i] += tmp_prev; tmp_prev = tmp; } if (tmp > 0) { a->size++; for (int i = a->size - 1; i > 0; --i) { a->numbers[i] = a->numbers[i-1]; } a->numbers[0] = tmp; } } a->log10 += magnitude; } bool check_mod_part_is_almost_full(big_num* a, int magnitude) { int blocks = magnitude / 18; int offset = magnitude % 18; ull offset_pow = pow(10, offset); for (int i = a->size - 1; i >= a->size - blocks; --i) { if (a->numbers[i] != (ull)1e18-1) { return false; // cout << "NIE1" << endl; } } if ((a->numbers[a->size - blocks - 1] % offset_pow) + 1 == offset_pow) { // cout << (a->numbers[a->size - blocks - 1] % offset_pow) << endl; // cout << "TAK" << endl; return true; } // cout << "NIE2" << endl; return false; } void increase_by_mod_part_plus_one(big_num* a, big_num* b, int magnitude) { // cout << "INC" << endl; // print_big_num(a); // print_big_num(b); // cout << "/INC" << endl; int blocks = magnitude / 18; int offset = magnitude % 18; ull offset_pow = pow(10, offset); for (int i = a->size - 1; i > a->size - blocks - 1; --i) { a->numbers[i] = b->numbers[i]; // cout << "cos" << endl; } // cout << "b mod : " << b->numbers[a->size - blocks - 1] % offset_pow << endl; a->numbers[a->size - blocks - 1] += b->numbers[a->size - blocks - 1] % offset_pow; ull tmp = 0; a->numbers[a->size - 1] += 1; tmp = a->numbers[a->size - 1] / (ull)1e18; // cout << "tmp: " << tmp << endl; int i = a->size - 1; while(tmp) { a->numbers[i] = a->numbers[i] % (ull)1e18; --i; tmp = a->numbers[i] / (ull)1e18; } } int main() { int n; cin >> n; big_num *prev = new big_num; big_num *current = new big_num; ull counter = 0; cin >> prev->numbers[0]; prev->size = 1; prev->log10 = floor(log10(prev->numbers[0])); for (int i = 1; i < n; ++i) { cin >> current->numbers[0]; ull current_bak = current->numbers[0]; current->size = 1; current->log10 = floor(log10(current->numbers[0])); int current_bak_log10 = current->log10; if (is_bigger_or_equal(prev, current)) { int log_diff = prev->log10 - current->log10; lift(current, log_diff); // print_big_num(current); // ull increaser = pow(10, log_diff); // current->numbers[0] *= increaser; // current->log10 += log_diff; counter += log_diff; if (is_bigger_or_equal(prev, current)) { // ull diff_major = (prev->numbers[0] / increaser) - current_bak; // ull diff_mod = prev->numbers[0] % increaser; if (is_diff_major_bigger_than_current(prev, current_bak, current_bak_log10) || check_mod_part_is_almost_full(prev, log_diff)) { // if (diff_major > 0 || ((diff_mod + 1) == increaser)) { lift(current, 1); // current->numbers[0] *= 10; // current->log10++; counter++; } else { increase_by_mod_part_plus_one(current, prev, log_diff); // current->numbers[0] += diff_mod + 1; } } } big_num* tmp = prev; prev = current; current = tmp; for (int i = 0; i < current->size; ++i) current->numbers[i] = 0; current->size = 1; // printf("prev: "); // print_big_num(prev); // cout << "prev: " << prev->numbers[0] << " : " << prev->numbers[1] << endl; } cout << counter << endl; return 0; } |