#include <bits/stdc++.h>
using namespace std;
#define fors(u, n, s) for(int u = (s); u<(n); u++)
#define foru(u, n) fors(u, n, 0)
#define f first
#define s second
#define vec vector
#define pb push_back
#define sz(x) ((int)x.size())
typedef long long ll;
#define debug(x) cout << __LINE__ << " | " << x << endl
// #define debug(x) {}
const int P = 4;
const ll primes[P] = {
10000019,
10000169,
10000223,
10000379,
};
struct num{
ll a[P];
num(ll x) {
foru(i, P) a[i] = (x+primes[i])%primes[i];
}
friend num operator*(const num &lhs, const num &rhs){
num out(0);
foru(i, P) out.a[i] = (lhs.a[i]*rhs.a[i])%primes[i];
return out;
}
friend void operator*=(num &lhs, const num &rhs){
lhs = (lhs*rhs);
}
friend num operator+(const num &lhs, const num &rhs){
num out(0);
foru(i, P) out.a[i] = (lhs.a[i]+rhs.a[i])%primes[i];
return out;
}
friend void operator+=(num &lhs, const num &rhs){
lhs = (lhs+rhs);
}
friend bool operator<(const num &lhs, const num &rhs){
foru(i, P){
if(lhs.a[i] < rhs.a[i]) return true;
if(lhs.a[i] > rhs.a[i]) return false;
}
return false;
}
friend bool operator==(const num &lhs, const num &rhs){
return !(lhs < rhs || rhs < lhs);
}
};
void solve() {
string a, b, c; cin >> a >> b >> c;
int n = a.length();
num p(1);
vec<num> v;
for(int i = n-1; i>=0; i--){
ll va = a[i]-'0';
ll vb = b[i]-'0';
ll vc = c[i]-'0';
ll val = va+vb-vc;
v.pb(num(val)*p);
p *= num(10);
}
vec<num> pref;
num s(0);
pref.pb(s);
for(auto i : v){
s += i;
pref.pb(s);
}
sort(pref.begin(), pref.end());
ll idx = 0;
ll ans = 0;
while(idx != sz(pref)){
ll nxt = idx;
while(nxt != sz(pref) && pref[nxt] == pref[idx]) nxt++;
ll dif = nxt-idx;
ans += dif*(dif-1)/2;
idx = nxt;
}
cout << ans << endl;
}
int main()
{
// cin.tie(0); cout.tie(0);
// ios_base::sync_with_stdio(0);
srand(time(NULL));
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 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 | #include <bits/stdc++.h> using namespace std; #define fors(u, n, s) for(int u = (s); u<(n); u++) #define foru(u, n) fors(u, n, 0) #define f first #define s second #define vec vector #define pb push_back #define sz(x) ((int)x.size()) typedef long long ll; #define debug(x) cout << __LINE__ << " | " << x << endl // #define debug(x) {} const int P = 4; const ll primes[P] = { 10000019, 10000169, 10000223, 10000379, }; struct num{ ll a[P]; num(ll x) { foru(i, P) a[i] = (x+primes[i])%primes[i]; } friend num operator*(const num &lhs, const num &rhs){ num out(0); foru(i, P) out.a[i] = (lhs.a[i]*rhs.a[i])%primes[i]; return out; } friend void operator*=(num &lhs, const num &rhs){ lhs = (lhs*rhs); } friend num operator+(const num &lhs, const num &rhs){ num out(0); foru(i, P) out.a[i] = (lhs.a[i]+rhs.a[i])%primes[i]; return out; } friend void operator+=(num &lhs, const num &rhs){ lhs = (lhs+rhs); } friend bool operator<(const num &lhs, const num &rhs){ foru(i, P){ if(lhs.a[i] < rhs.a[i]) return true; if(lhs.a[i] > rhs.a[i]) return false; } return false; } friend bool operator==(const num &lhs, const num &rhs){ return !(lhs < rhs || rhs < lhs); } }; void solve() { string a, b, c; cin >> a >> b >> c; int n = a.length(); num p(1); vec<num> v; for(int i = n-1; i>=0; i--){ ll va = a[i]-'0'; ll vb = b[i]-'0'; ll vc = c[i]-'0'; ll val = va+vb-vc; v.pb(num(val)*p); p *= num(10); } vec<num> pref; num s(0); pref.pb(s); for(auto i : v){ s += i; pref.pb(s); } sort(pref.begin(), pref.end()); ll idx = 0; ll ans = 0; while(idx != sz(pref)){ ll nxt = idx; while(nxt != sz(pref) && pref[nxt] == pref[idx]) nxt++; ll dif = nxt-idx; ans += dif*(dif-1)/2; idx = nxt; } cout << ans << endl; } int main() { // cin.tie(0); cout.tie(0); // ios_base::sync_with_stdio(0); srand(time(NULL)); solve(); return 0; } |
English