#include <iostream> using namespace std; bool isless(string a, string b) { if(a.size() < b.size()) return 1; if(a.size() > b.size()) return 0; for(long long i = 0; i < a.size(); i++) { if(a[i] < b[i]) { return 1; } if(a[i] > b[i]) { return 0; } } return 1; } string add(string a, string b) { long long al = a.size()-1; long long bl = b.size()-1; long long carry = 0, temp, i; string result = ""; while((al >= 0) && (bl >= 0)) { temp = (long long)(a[al] - '0') + (long long)(b[bl] - '0') + carry; carry = 0; if(temp > 9) { carry = 1; temp = temp - 10; } result += char(temp + '0'); al--; bl--; } while(al >= 0) { temp = (long long)(a[al] - '0') + carry; carry = 0; if(temp > 9) { carry = 1; temp = temp % 10; } result += char(temp + '0'); al--; } while(bl >= 0) { temp = (long long)(b[bl] - '0') + carry; carry = 0; if(temp > 9) { carry = 1; temp = temp % 10; } result += char(temp + '0'); bl--; } if(carry) { result += "1"; } string addition = ""; for(i = result.size()-1; i >= 0; i--) { addition += result[i]; } return addition; } int main() { ios::sync_with_stdio(false); long long n, i, x = 0; string pa, a, p, ao; cin >> n; cin >> p; for(i = 1; i < n; i++) { cin >> a; ao = a; pa = p.substr(0, a.size()); if((pa == a) && (p.size() != a.size())) { a = add(p, "1"); if(a.substr(0, pa.size()) != pa) { a = ao; } } while(isless(a, p)) { a += "0"; } p = a; x += (a.size() - ao.size()); } cout << x; 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 | #include <iostream> using namespace std; bool isless(string a, string b) { if(a.size() < b.size()) return 1; if(a.size() > b.size()) return 0; for(long long i = 0; i < a.size(); i++) { if(a[i] < b[i]) { return 1; } if(a[i] > b[i]) { return 0; } } return 1; } string add(string a, string b) { long long al = a.size()-1; long long bl = b.size()-1; long long carry = 0, temp, i; string result = ""; while((al >= 0) && (bl >= 0)) { temp = (long long)(a[al] - '0') + (long long)(b[bl] - '0') + carry; carry = 0; if(temp > 9) { carry = 1; temp = temp - 10; } result += char(temp + '0'); al--; bl--; } while(al >= 0) { temp = (long long)(a[al] - '0') + carry; carry = 0; if(temp > 9) { carry = 1; temp = temp % 10; } result += char(temp + '0'); al--; } while(bl >= 0) { temp = (long long)(b[bl] - '0') + carry; carry = 0; if(temp > 9) { carry = 1; temp = temp % 10; } result += char(temp + '0'); bl--; } if(carry) { result += "1"; } string addition = ""; for(i = result.size()-1; i >= 0; i--) { addition += result[i]; } return addition; } int main() { ios::sync_with_stdio(false); long long n, i, x = 0; string pa, a, p, ao; cin >> n; cin >> p; for(i = 1; i < n; i++) { cin >> a; ao = a; pa = p.substr(0, a.size()); if((pa == a) && (p.size() != a.size())) { a = add(p, "1"); if(a.substr(0, pa.size()) != pa) { a = ao; } } while(isless(a, p)) { a += "0"; } p = a; x += (a.size() - ao.size()); } cout << x; return 0; } |