#include<bits/stdc++.h> using namespace std; using i64 = long long; using i32 = int; template<typename T> T load() { T x; cin >> x; return x; } template<typename T> vector<T> loadN(int s) { vector<T> vt(s); for(auto& el : vt) el = load<T>(); return vt; } template<typename T> ostream& operator<<(ostream& os, const vector<T>& vt) { for(auto& el : vt) os << el << '\n'; return os; } i32 digits(i64 num) { auto c = 1; i64 val = 10; while(num >= val) ++c, val *= 10; return c; } i64 powTable[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000 }; i64 pow10(i64 num) { if(num > 18) return LLONG_MAX; return powTable[num]; } struct Scientific { static Scientific shifted(i64 core, i64 shift) { return Scientific{core, 0,shift}; } i64 left(i32 size) const { //assert(size <= length()); auto cL = coreLength(); if(size <= cL) return core / pow10(cL - size); auto left = size - cL; auto result = core * pow10(left); return result + addon / pow10(addonPadding - left); } i64 coreLength() const { return digits(core); } i64 length() const { return coreLength() + addonPadding; } friend ostream& operator<<(ostream& os, const Scientific& el) { os << el.core ; if(el.addonPadding > 0) os << setfill('0') << setw(el.addonPadding) << el.addon; return os; } friend istream& operator>>(istream& is, Scientific& el) { is >> el.core; el.addonPadding = 0; el.addon = 0; return is;} i64 core; i64 addon, addonPadding; }; Scientific evolve(const Scientific& before, const Scientific& original) { auto lengthDiff = before.length() - original.length(); if(lengthDiff < 0) return original; auto lhsLeft = before.left(original.coreLength()); auto rhsLeft = original.core; if(rhsLeft != lhsLeft) return Scientific::shifted(original.core, lengthDiff + ((rhsLeft < lhsLeft) ? 1 : 0)); //try extending auto clone = before; clone.addon += 1; if(digits(clone.addon) > clone.addonPadding) { clone.addon = 0; clone.core++; } if(clone.left(original.coreLength()) == original.core) { return clone; } return Scientific::shifted(original.core, lengthDiff + 1); } i32 main() { ios::sync_with_stdio(false); auto seqSize = load<i32>(); auto seq = loadN<Scientific>(seqSize); auto transformed = vector<Scientific>(); transformed.push_back(seq[0]); for(i32 i = 1 ; i < seqSize ; ++i) { auto& before = transformed[i - 1]; auto& now = seq[i]; transformed.push_back(evolve(before, now)); } i64 result = 0; for(i32 i = 0 ; i < seqSize ; ++i) result += transformed[i].length() - seq[i].length(); cout << result << '\n'; }
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 | #include<bits/stdc++.h> using namespace std; using i64 = long long; using i32 = int; template<typename T> T load() { T x; cin >> x; return x; } template<typename T> vector<T> loadN(int s) { vector<T> vt(s); for(auto& el : vt) el = load<T>(); return vt; } template<typename T> ostream& operator<<(ostream& os, const vector<T>& vt) { for(auto& el : vt) os << el << '\n'; return os; } i32 digits(i64 num) { auto c = 1; i64 val = 10; while(num >= val) ++c, val *= 10; return c; } i64 powTable[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000 }; i64 pow10(i64 num) { if(num > 18) return LLONG_MAX; return powTable[num]; } struct Scientific { static Scientific shifted(i64 core, i64 shift) { return Scientific{core, 0,shift}; } i64 left(i32 size) const { //assert(size <= length()); auto cL = coreLength(); if(size <= cL) return core / pow10(cL - size); auto left = size - cL; auto result = core * pow10(left); return result + addon / pow10(addonPadding - left); } i64 coreLength() const { return digits(core); } i64 length() const { return coreLength() + addonPadding; } friend ostream& operator<<(ostream& os, const Scientific& el) { os << el.core ; if(el.addonPadding > 0) os << setfill('0') << setw(el.addonPadding) << el.addon; return os; } friend istream& operator>>(istream& is, Scientific& el) { is >> el.core; el.addonPadding = 0; el.addon = 0; return is;} i64 core; i64 addon, addonPadding; }; Scientific evolve(const Scientific& before, const Scientific& original) { auto lengthDiff = before.length() - original.length(); if(lengthDiff < 0) return original; auto lhsLeft = before.left(original.coreLength()); auto rhsLeft = original.core; if(rhsLeft != lhsLeft) return Scientific::shifted(original.core, lengthDiff + ((rhsLeft < lhsLeft) ? 1 : 0)); //try extending auto clone = before; clone.addon += 1; if(digits(clone.addon) > clone.addonPadding) { clone.addon = 0; clone.core++; } if(clone.left(original.coreLength()) == original.core) { return clone; } return Scientific::shifted(original.core, lengthDiff + 1); } i32 main() { ios::sync_with_stdio(false); auto seqSize = load<i32>(); auto seq = loadN<Scientific>(seqSize); auto transformed = vector<Scientific>(); transformed.push_back(seq[0]); for(i32 i = 1 ; i < seqSize ; ++i) { auto& before = transformed[i - 1]; auto& now = seq[i]; transformed.push_back(evolve(before, now)); } i64 result = 0; for(i32 i = 0 ; i < seqSize ; ++i) result += transformed[i].length() - seq[i].length(); cout << result << '\n'; } |