#include <cstdio> const unsigned long unit = 1UL<<31; // = 1000000000000000000ULL const int maxdigits = 25002; // const unsigned long long unit = 10; using namespace std; class bn { public: bn(int data) { for (int i = 0; i < maxdigits; i++) digits[i] = 0; maxnonzero = 0; do { digits[maxnonzero++] = data & (unit - 1); data = data >> 31; } while(data != 0); while(digits[maxnonzero] == 0 && maxnonzero >0) maxnonzero--; } bool operator<(const bn &other) const { if (maxnonzero != other.maxnonzero) return maxnonzero < other.maxnonzero; for (int i = maxnonzero; i >= 0; i--) if (digits[i] != other.digits[i]) return digits[i] < other.digits[i]; return false; } void normalize() { int rest = digits[0] >> 31; digits[0] = digits[0] & (unit - 1); for (int i = 1; i < maxdigits && (i <= maxnonzero || rest); i++) { digits[i] += rest; rest = digits[i] / unit; digits[i] %= unit; if (digits[i] && i > maxnonzero) maxnonzero = i; } } void mul10() { for (int i = 0; i <= maxnonzero; i++) digits[i] *= 10; normalize(); } void add(int num) { digits[0] += num; int rest = digits[0] >> 31; digits[0] = digits[0] & (unit - 1); int i; for (i = 1; i < maxdigits && rest; i++) { digits[i] += rest; rest = digits[i] >> 31; digits[i] = digits[i] & (unit - 1);; } if (i - 1 > maxnonzero && i -1 < maxdigits && digits[i-1]) maxnonzero = i - 1; } void print() { for(int i = maxnonzero; i >=0 ; i--) printf("%lld", digits[i]); printf("\n"); } private: unsigned long digits[maxdigits]; int maxnonzero; }; int main() { int n; int tmp; long long cnt = 0; scanf("%d", &n); scanf("%d", &tmp); bn old(tmp); for(int i = 1; i < n; i++) { scanf("%d", &tmp); bn mi(tmp), ma(tmp); //ma.mul10(); /* printf("ii\n"); mi.print();*/ while(! (old < ma) ) { mi.mul10(); ma.mul10(); ma.add(9); cnt++; } /* printf("mia\n"); mi.print(); ma.print();*/ if (old < mi) old = mi; else old.add(1); /* printf("old: "); old.print();*/ } printf("%lld\n", cnt++); }
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 | #include <cstdio> const unsigned long unit = 1UL<<31; // = 1000000000000000000ULL const int maxdigits = 25002; // const unsigned long long unit = 10; using namespace std; class bn { public: bn(int data) { for (int i = 0; i < maxdigits; i++) digits[i] = 0; maxnonzero = 0; do { digits[maxnonzero++] = data & (unit - 1); data = data >> 31; } while(data != 0); while(digits[maxnonzero] == 0 && maxnonzero >0) maxnonzero--; } bool operator<(const bn &other) const { if (maxnonzero != other.maxnonzero) return maxnonzero < other.maxnonzero; for (int i = maxnonzero; i >= 0; i--) if (digits[i] != other.digits[i]) return digits[i] < other.digits[i]; return false; } void normalize() { int rest = digits[0] >> 31; digits[0] = digits[0] & (unit - 1); for (int i = 1; i < maxdigits && (i <= maxnonzero || rest); i++) { digits[i] += rest; rest = digits[i] / unit; digits[i] %= unit; if (digits[i] && i > maxnonzero) maxnonzero = i; } } void mul10() { for (int i = 0; i <= maxnonzero; i++) digits[i] *= 10; normalize(); } void add(int num) { digits[0] += num; int rest = digits[0] >> 31; digits[0] = digits[0] & (unit - 1); int i; for (i = 1; i < maxdigits && rest; i++) { digits[i] += rest; rest = digits[i] >> 31; digits[i] = digits[i] & (unit - 1);; } if (i - 1 > maxnonzero && i -1 < maxdigits && digits[i-1]) maxnonzero = i - 1; } void print() { for(int i = maxnonzero; i >=0 ; i--) printf("%lld", digits[i]); printf("\n"); } private: unsigned long digits[maxdigits]; int maxnonzero; }; int main() { int n; int tmp; long long cnt = 0; scanf("%d", &n); scanf("%d", &tmp); bn old(tmp); for(int i = 1; i < n; i++) { scanf("%d", &tmp); bn mi(tmp), ma(tmp); //ma.mul10(); /* printf("ii\n"); mi.print();*/ while(! (old < ma) ) { mi.mul10(); ma.mul10(); ma.add(9); cnt++; } /* printf("mia\n"); mi.print(); ma.print();*/ if (old < mi) old = mi; else old.add(1); /* printf("old: "); old.print();*/ } printf("%lld\n", cnt++); } |