#include <iostream>
#include <string>
#include <vector>
#include <assert.h>
using int64 = long long int;
int64 solve(const std::string& a, const std::string& b, const std::string& c)
{
int64 result = 0;
bool need_1 = false;
int num_exact_sums = 0;
for (int i = 0; i < a.length(); ++i)
{
const int a_digit = a[i] - '0';
const int b_digit = b[i] - '0';
const int c_digit = c[i] - '0';
const int ab_sum = a_digit + b_digit;
if (ab_sum == c_digit)
{
if (need_1)
{
need_1 = false;
num_exact_sums = 0;
}
++num_exact_sums;
result += num_exact_sums;
}
else if (ab_sum + 1 == c_digit)
{
if (need_1)
num_exact_sums = 0;
need_1 = true;
}
else if ((ab_sum + 1) % 10 == c_digit)
{
if (!need_1)
{
need_1 = false;
num_exact_sums = 0;
}
}
else if (ab_sum % 10 == c_digit)
{
if (!need_1)
{
num_exact_sums = 0;
}
else
{
++num_exact_sums;
result += num_exact_sums;
}
need_1 = false;
}
else
{
need_1 = false;
num_exact_sums = 0;
}
}
return result;
}
int64 num(const std::string& a, const int start, const int end)
{
int64 value = 0;
for (int i = start; i <= end; i++)
value = value * 10 + (a[i] - '0');
return value;
}
void check(const std::string& a, const std::string& b, const std::string& c, const int64 expected_result)
{
int64 correct_result = 0;
for (int start = 0; start < a.length(); ++start)
for (int end = start; end < a.length(); ++end)
{
const int64 a_num = num(a, start, end);
const int64 b_num = num(b, start, end);
const int64 c_num = num(c, start, end);
if (a_num + b_num == c_num)
++correct_result;
}
if (expected_result != correct_result)
{
std::cout << "ERROR: " << std::endl << a << std::endl << b << std::endl << c << std::endl;
std::cout << "GOT: " << expected_result << std::endl;
std::cout << "EXPECTED: " << correct_result << std::endl;
assert(false);
}
}
void test()
{
std::string a, b, c;
#if 0
a = b = c = "xxx";
for (int a0 = 0; a0 <= 9; a0++)
for (int a1 = 0; a1 <= 9; a1++)
for (int a2 = 0; a2 <= 9; a2++)
for (int b0 = 0; b0 <= 9; b0++)
for (int b1 = 0; b1 <= 9; b1++)
for (int b2 = 0; b2 <= 9; b2++)
for (int c0 = 0; c0 <= 9; c0++)
for (int c1 = 0; c1 <= 9; c1++)
for (int c2 = 0; c2 <= 9; c2++)
{
a[0] = a0 + '0';
a[1] = a1 + '0';
a[2] = a2 + '0';
b[0] = b0 + '0';
b[1] = b1 + '0';
b[2] = b2 + '0';
c[0] = c0 + '0';
c[1] = c1 + '0';
c[2] = c2 + '0';
int64 result = solve(a, b, c);
check(a, b, c, result);
}
#else
const int tests = 1000 * 1000;
const int n = 10;
a.resize(n);
b.resize(n);
c.resize(n);
for (int i = 0; i < tests; ++i)
{
for (int j = 0; j < n; ++j)
{
a[j] = '0' + (rand() % 10);
b[j] = '0' + (rand() % 10);
c[j] = '0' + (rand() % 10);
}
const int64 result = solve(a, b, c);
check(a, b, c, result);
}
#endif
}
int main()
{
//test();
std::string a, b, c;
std::cin >> a >> b >> c;
std::cout << solve(a, b, c);
return 0;
}
/*
000
000
020
037523
040834
978367
11111
22222
33433
15015
25025
40040
0555
0445
1000
09
09
18
099
091
190
0111
0999
1000
*/
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | #include <iostream> #include <string> #include <vector> #include <assert.h> using int64 = long long int; int64 solve(const std::string& a, const std::string& b, const std::string& c) { int64 result = 0; bool need_1 = false; int num_exact_sums = 0; for (int i = 0; i < a.length(); ++i) { const int a_digit = a[i] - '0'; const int b_digit = b[i] - '0'; const int c_digit = c[i] - '0'; const int ab_sum = a_digit + b_digit; if (ab_sum == c_digit) { if (need_1) { need_1 = false; num_exact_sums = 0; } ++num_exact_sums; result += num_exact_sums; } else if (ab_sum + 1 == c_digit) { if (need_1) num_exact_sums = 0; need_1 = true; } else if ((ab_sum + 1) % 10 == c_digit) { if (!need_1) { need_1 = false; num_exact_sums = 0; } } else if (ab_sum % 10 == c_digit) { if (!need_1) { num_exact_sums = 0; } else { ++num_exact_sums; result += num_exact_sums; } need_1 = false; } else { need_1 = false; num_exact_sums = 0; } } return result; } int64 num(const std::string& a, const int start, const int end) { int64 value = 0; for (int i = start; i <= end; i++) value = value * 10 + (a[i] - '0'); return value; } void check(const std::string& a, const std::string& b, const std::string& c, const int64 expected_result) { int64 correct_result = 0; for (int start = 0; start < a.length(); ++start) for (int end = start; end < a.length(); ++end) { const int64 a_num = num(a, start, end); const int64 b_num = num(b, start, end); const int64 c_num = num(c, start, end); if (a_num + b_num == c_num) ++correct_result; } if (expected_result != correct_result) { std::cout << "ERROR: " << std::endl << a << std::endl << b << std::endl << c << std::endl; std::cout << "GOT: " << expected_result << std::endl; std::cout << "EXPECTED: " << correct_result << std::endl; assert(false); } } void test() { std::string a, b, c; #if 0 a = b = c = "xxx"; for (int a0 = 0; a0 <= 9; a0++) for (int a1 = 0; a1 <= 9; a1++) for (int a2 = 0; a2 <= 9; a2++) for (int b0 = 0; b0 <= 9; b0++) for (int b1 = 0; b1 <= 9; b1++) for (int b2 = 0; b2 <= 9; b2++) for (int c0 = 0; c0 <= 9; c0++) for (int c1 = 0; c1 <= 9; c1++) for (int c2 = 0; c2 <= 9; c2++) { a[0] = a0 + '0'; a[1] = a1 + '0'; a[2] = a2 + '0'; b[0] = b0 + '0'; b[1] = b1 + '0'; b[2] = b2 + '0'; c[0] = c0 + '0'; c[1] = c1 + '0'; c[2] = c2 + '0'; int64 result = solve(a, b, c); check(a, b, c, result); } #else const int tests = 1000 * 1000; const int n = 10; a.resize(n); b.resize(n); c.resize(n); for (int i = 0; i < tests; ++i) { for (int j = 0; j < n; ++j) { a[j] = '0' + (rand() % 10); b[j] = '0' + (rand() % 10); c[j] = '0' + (rand() % 10); } const int64 result = solve(a, b, c); check(a, b, c, result); } #endif } int main() { //test(); std::string a, b, c; std::cin >> a >> b >> c; std::cout << solve(a, b, c); return 0; } /* 000 000 020 037523 040834 978367 11111 22222 33433 15015 25025 40040 0555 0445 1000 09 09 18 099 091 190 0111 0999 1000 */ |
English