// Karol Kosinski 2018 #include <cstdio> #define FOR(i,a,b) for(int i=(a);i<(b);++i) //#define DEBUG(x...) printf(x) #define DEBUG(x...) using namespace std; typedef long long LL; const int NX = 200001; LL A[NX]; LL Log10[NX]; const LL P10[] = {1LL, 10LL, 100LL, 1000LL, 10000LL, 100000LL, 1000000LL, 10000000LL, 100000000LL, 1000000000LL, 10000000000LL, 100000000000LL, 1000000000000LL, 10000000000000LL, 100000000000000LL}; void prepare_log10() { Log10[0] = -1; FOR(i,1,NX) Log10[i] = Log10[i / 10] + 1; } struct Num { LL base, real_base, tens, real_tens, counter; Num(LL b): base(b), real_base(b), tens(0), real_tens(0), counter(0) {} // base == real_base / 10 ^ (tens - real_tens) // tens >= real_tens // counter < 10 ^ real_tens void norm() { if(Log10[counter] < real_tens) return; LL base_candidate = (real_base + counter / P10[real_tens]) / P10[tens - real_tens]; if(base_candidate == base) { real_base += counter / P10[real_tens]; counter -= (counter / P10[real_tens]) * P10[real_tens]; } else { real_base = base; tens += 1; real_tens = tens; counter = 0; } } void expand() { real_base *= 10; real_tens -= 1; norm(); } void produce_bigger_than(Num& other) { if(other.real_base <= real_base) { while(other.real_base < real_base and 0 < other.real_tens) { other.expand(); } if(other.real_base == real_base) { tens = real_tens = other.real_tens; counter = other.counter + 1; DEBUG("* (%10lld, %10lld, %10lld, %10lld, %10lld)\n", base, real_base, tens, real_tens, counter); norm(); } else if(other.real_base > real_base) { tens = real_tens = other.real_tens + 1; } } else // other.real_base > real_base { LL aux = other.real_base, x = 0; while(aux > real_base) { aux /= 10; ++x; } if(aux == real_base) { real_base = other.real_base; tens = other.real_tens + x; real_tens = other.real_tens; counter = other.counter + 1; DEBUG("+ (%10lld, %10lld, %10lld, %10lld, %10lld)\n", base, real_base, tens, real_tens, counter); norm(); } else // aux < real_base { tens = real_tens = other.real_tens + x; } } } }; int main() { int n; scanf("%d", &n); FOR(i,0,n) scanf("%lld", A + i); prepare_log10(); LL res = 0; Num cur = A[0]; FOR(i,1,n) { Num next = A[i]; DEBUG(" (%10lld, %10lld, %10lld, %10lld, %10lld)\n", cur.base, cur.real_base, cur.tens, cur.real_tens, cur.counter); next.produce_bigger_than(cur); res += next.tens; cur = next; } DEBUG(" (%10lld, %10lld, %10lld, %10lld, %10lld)\n", cur.base, cur.real_base, cur.tens, cur.real_tens, cur.counter); printf("%lld\n", res); 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 | // Karol Kosinski 2018 #include <cstdio> #define FOR(i,a,b) for(int i=(a);i<(b);++i) //#define DEBUG(x...) printf(x) #define DEBUG(x...) using namespace std; typedef long long LL; const int NX = 200001; LL A[NX]; LL Log10[NX]; const LL P10[] = {1LL, 10LL, 100LL, 1000LL, 10000LL, 100000LL, 1000000LL, 10000000LL, 100000000LL, 1000000000LL, 10000000000LL, 100000000000LL, 1000000000000LL, 10000000000000LL, 100000000000000LL}; void prepare_log10() { Log10[0] = -1; FOR(i,1,NX) Log10[i] = Log10[i / 10] + 1; } struct Num { LL base, real_base, tens, real_tens, counter; Num(LL b): base(b), real_base(b), tens(0), real_tens(0), counter(0) {} // base == real_base / 10 ^ (tens - real_tens) // tens >= real_tens // counter < 10 ^ real_tens void norm() { if(Log10[counter] < real_tens) return; LL base_candidate = (real_base + counter / P10[real_tens]) / P10[tens - real_tens]; if(base_candidate == base) { real_base += counter / P10[real_tens]; counter -= (counter / P10[real_tens]) * P10[real_tens]; } else { real_base = base; tens += 1; real_tens = tens; counter = 0; } } void expand() { real_base *= 10; real_tens -= 1; norm(); } void produce_bigger_than(Num& other) { if(other.real_base <= real_base) { while(other.real_base < real_base and 0 < other.real_tens) { other.expand(); } if(other.real_base == real_base) { tens = real_tens = other.real_tens; counter = other.counter + 1; DEBUG("* (%10lld, %10lld, %10lld, %10lld, %10lld)\n", base, real_base, tens, real_tens, counter); norm(); } else if(other.real_base > real_base) { tens = real_tens = other.real_tens + 1; } } else // other.real_base > real_base { LL aux = other.real_base, x = 0; while(aux > real_base) { aux /= 10; ++x; } if(aux == real_base) { real_base = other.real_base; tens = other.real_tens + x; real_tens = other.real_tens; counter = other.counter + 1; DEBUG("+ (%10lld, %10lld, %10lld, %10lld, %10lld)\n", base, real_base, tens, real_tens, counter); norm(); } else // aux < real_base { tens = real_tens = other.real_tens + x; } } } }; int main() { int n; scanf("%d", &n); FOR(i,0,n) scanf("%lld", A + i); prepare_log10(); LL res = 0; Num cur = A[0]; FOR(i,1,n) { Num next = A[i]; DEBUG(" (%10lld, %10lld, %10lld, %10lld, %10lld)\n", cur.base, cur.real_base, cur.tens, cur.real_tens, cur.counter); next.produce_bigger_than(cur); res += next.tens; cur = next; } DEBUG(" (%10lld, %10lld, %10lld, %10lld, %10lld)\n", cur.base, cur.real_base, cur.tens, cur.real_tens, cur.counter); printf("%lld\n", res); return 0; } |