#include <cstdint>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string tmp;
cin >> tmp;
int n = tmp.length();
vector<uint8_t> A(n);
for(int i = 0;i<n;i++) A[i] = tmp[i] - '0';
cin >> tmp;
vector<uint8_t> B(n);
for(int i = 0;i<n;i++) B[i] = tmp[i] - '0';
cin >> tmp;
vector<uint8_t> C(n);
for(int i = 0;i<n;i++) C[i] = tmp[i] - '0';
vector<uint8_t> R(n);
int carry = 0;
for (int i = n-1; i >= 0; i--)
{
if (A[i] + B[i] == C[i])
{
R[i] = 1; // carry in=0, carry out=0
}
else if (A[i] + B[i] >= 10 && A[i] + B[i] - 10 == C[i])
{
R[i] = 2; // carry in=0, carry out=1
}
else if (A[i] + B[i] + 1 == C[i])
{
R[i] = 4; // carry in=1, carry out=0
}
else if (A[i] + B[i] + 1 >= 10 && A[i] + B[i] + 1 - 10 == C[i])
{
R[i] = 8; // carry in=1, carry out=1
}
else R[i] = 0; // nie pasuje
}
long long result = 0;
long long s0 = 0; // fragmenty z ci = 0
long long s1 = 0; // fragmenty z ci = 1
for (int p = n - 1; p >= 0; p--) {
s0++; // new fragment starts here (rightmost position j=p), carry_in=0
long long new_s0 = 0, new_s1 = 0;
if (R[p] == 1)
{
result += s0;
new_s0 = s0; // cin=0,cout=0 -> ok, s0
}
else if (R[p] == 2) {
new_s1 = s0; // cin=0,cout=1 -> zmiana w s1
}
else if (R[p] == 4) {
result += s1;
new_s0 = s1; // cin=1,cout=0 -> ok i zostaje 0
}
else if (R[p] == 8)
{
new_s1 = s1; // cin=1,cout=1 -> stays s1
}
s0 = new_s0;
s1 = new_s1;
}
cout << result << "\n";
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 | #include <cstdint> #include <iostream> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string tmp; cin >> tmp; int n = tmp.length(); vector<uint8_t> A(n); for(int i = 0;i<n;i++) A[i] = tmp[i] - '0'; cin >> tmp; vector<uint8_t> B(n); for(int i = 0;i<n;i++) B[i] = tmp[i] - '0'; cin >> tmp; vector<uint8_t> C(n); for(int i = 0;i<n;i++) C[i] = tmp[i] - '0'; vector<uint8_t> R(n); int carry = 0; for (int i = n-1; i >= 0; i--) { if (A[i] + B[i] == C[i]) { R[i] = 1; // carry in=0, carry out=0 } else if (A[i] + B[i] >= 10 && A[i] + B[i] - 10 == C[i]) { R[i] = 2; // carry in=0, carry out=1 } else if (A[i] + B[i] + 1 == C[i]) { R[i] = 4; // carry in=1, carry out=0 } else if (A[i] + B[i] + 1 >= 10 && A[i] + B[i] + 1 - 10 == C[i]) { R[i] = 8; // carry in=1, carry out=1 } else R[i] = 0; // nie pasuje } long long result = 0; long long s0 = 0; // fragmenty z ci = 0 long long s1 = 0; // fragmenty z ci = 1 for (int p = n - 1; p >= 0; p--) { s0++; // new fragment starts here (rightmost position j=p), carry_in=0 long long new_s0 = 0, new_s1 = 0; if (R[p] == 1) { result += s0; new_s0 = s0; // cin=0,cout=0 -> ok, s0 } else if (R[p] == 2) { new_s1 = s0; // cin=0,cout=1 -> zmiana w s1 } else if (R[p] == 4) { result += s1; new_s0 = s1; // cin=1,cout=0 -> ok i zostaje 0 } else if (R[p] == 8) { new_s1 = s1; // cin=1,cout=1 -> stays s1 } s0 = new_s0; s1 = new_s1; } cout << result << "\n"; return 0; } |
English