#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (auto i=(a); i<(b); i++)
#define FORD(i, a, b) for (auto i=(a); i>(b); i--)
#define SZ(x) ((int)(x).size())
#define ITH_BIT(m, i) ((m)>>(i) & 1)
#define PPC(x) __builtin_popcount(x)
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = std::min(a, (T1)b); }
template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = std::max(a, (T1)b); }
const int maxN = 1 << 20;
char T[3][maxN];
void solve()
{
FOR(s, 0, 3)
scanf ("%s", T[s]);
int n = strlen(T[0]);
long long res = 0;
int lastFail = -1, carrys = 0;
bool carryNeeded = false;
auto markFail = [&](int i) {
lastFail = i;
carrys = 0;
carryNeeded = false;
};
auto harvest = [&](int i) {
res += i - lastFail - carrys;
};
FOR(i, 0, n)
{
int a = T[0][i] - '0';
int b = T[1][i] - '0';
int c = T[2][i] - '0';
bool hitWithoutCarry = (a + b) % 10 == c;
bool hitWithCarry = (a + b + 1) % 10 == c;
if (not hitWithoutCarry and not hitWithCarry)
{
markFail(i);
continue;
}
bool carryGenerated = a + b + hitWithCarry >= 10;
if (carryGenerated != carryNeeded)
markFail(i - carryNeeded);
else
carrys += carryGenerated;
if (hitWithoutCarry)
harvest(i);
carryNeeded = lastFail != i and hitWithCarry;
}
printf("%lld\n", res);
}
int main()
{
int tc = 1;
// scanf ("%d", &tc);
FOR(cid, 1, tc+1)
{
// printf("Case #%d: ", cid);
solve();
}
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 | #pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (auto i=(a); i<(b); i++) #define FORD(i, a, b) for (auto i=(a); i>(b); i--) #define SZ(x) ((int)(x).size()) #define ITH_BIT(m, i) ((m)>>(i) & 1) #define PPC(x) __builtin_popcount(x) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = std::min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = std::max(a, (T1)b); } const int maxN = 1 << 20; char T[3][maxN]; void solve() { FOR(s, 0, 3) scanf ("%s", T[s]); int n = strlen(T[0]); long long res = 0; int lastFail = -1, carrys = 0; bool carryNeeded = false; auto markFail = [&](int i) { lastFail = i; carrys = 0; carryNeeded = false; }; auto harvest = [&](int i) { res += i - lastFail - carrys; }; FOR(i, 0, n) { int a = T[0][i] - '0'; int b = T[1][i] - '0'; int c = T[2][i] - '0'; bool hitWithoutCarry = (a + b) % 10 == c; bool hitWithCarry = (a + b + 1) % 10 == c; if (not hitWithoutCarry and not hitWithCarry) { markFail(i); continue; } bool carryGenerated = a + b + hitWithCarry >= 10; if (carryGenerated != carryNeeded) markFail(i - carryNeeded); else carrys += carryGenerated; if (hitWithoutCarry) harvest(i); carryNeeded = lastFail != i and hitWithCarry; } printf("%lld\n", res); } int main() { int tc = 1; // scanf ("%d", &tc); FOR(cid, 1, tc+1) { // printf("Case #%d: ", cid); solve(); } return 0; } |
English