#include <bits/stdc++.h>
using namespace std;
const int MOD[] = {1'000'000'007, 1'000'000'009};
long long solve(int n, vector <string> &s) {
long long ans = 0;
vector <long long> hash(2, 0);
vector <int> basePow(2, 1);
map <long long, int> hashCnt;
hashCnt[0] = 1;
for (int i = n - 1; i >= 0; i--) {
int value = 0;
for (int j = 0; j < 3; j++) {
int d = s[j][i] - '0';
value += j == 2 ? d : -d;
}
for (int t = 0; t <= 1; t++) {
hash[t] = (hash[t] + (long long) value * basePow[t]) % MOD[t];
basePow[t] = ((long long) basePow[t] * 10) % MOD[t];
if (hash[t] < 0) {
hash[t] += MOD[t];
}
}
long long hashCombined = (hash[0] << 30) ^ hash[1];
ans += hashCnt[hashCombined];
hashCnt[hashCombined]++;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
vector <string> s(3);
for (auto &si : s) {
cin >> si;
}
cout << solve(s[0].size(), s);
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 | #include <bits/stdc++.h> using namespace std; const int MOD[] = {1'000'000'007, 1'000'000'009}; long long solve(int n, vector <string> &s) { long long ans = 0; vector <long long> hash(2, 0); vector <int> basePow(2, 1); map <long long, int> hashCnt; hashCnt[0] = 1; for (int i = n - 1; i >= 0; i--) { int value = 0; for (int j = 0; j < 3; j++) { int d = s[j][i] - '0'; value += j == 2 ? d : -d; } for (int t = 0; t <= 1; t++) { hash[t] = (hash[t] + (long long) value * basePow[t]) % MOD[t]; basePow[t] = ((long long) basePow[t] * 10) % MOD[t]; if (hash[t] < 0) { hash[t] += MOD[t]; } } long long hashCombined = (hash[0] << 30) ^ hash[1]; ans += hashCnt[hashCombined]; hashCnt[hashCombined]++; } return ans; } int main() { ios_base::sync_with_stdio(false); vector <string> s(3); for (auto &si : s) { cin >> si; } cout << solve(s[0].size(), s); return 0; } |
English