Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- // #include <atcoder/all>
- using namespace std;
- #define int long long
- #define all(x) (x).begin(), (x).end()
- typedef vector<int> vi;
- typedef vector<vi> vvi;
- typedef vector<pair<int, int>> vpi;
- typedef pair<int, int> pi;
- #define f first
- #define s second
- #define pb push_back
- #define endl "\n"
- #define yes cout << "YES" << endl
- #define no cout << "NO" << endl
- #define init(x, a) memset(x, a, sizeof(x))
- const int mod1 = 1e9 + 7, mod2 = 998244353, INF = 2e18, N = 2e5 + 5, L = 19;
- int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
- // -----------------------------------------------------------------------------
- void solve()
- {
- int n;
- cin >> n;
- vi a(n); // a[i] < 2^20
- vi prefxor(n + 1, 0), prefsum(n + 1, 0);
- for (int i = 0; i < n; i++)
- {
- cin >> a[i];
- prefxor[i + 1] = prefxor[i] ^ a[i];
- prefsum[i + 1] = prefsum[i] + a[i];
- }
- // acc to pigeonhole principle, we can have subarray size of atmost 20
- // if it's more than 20, then 1 bit would he repeated, so carry over would be there so XOR != SUM
- int ans = 0;
- for (int k = 0; k < 25; k++) // subarray length
- {
- for (int i = 0; i < n; i++)
- {
- if (i + k >= n)
- {
- break;
- }
- int x = prefxor[i] ^ prefxor[i + k + 1];
- int y = prefsum[i + k + 1] - prefsum[i];
- if (x == y)
- {
- ans++;
- }
- }
- }
- cout << ans << endl;
- return;
- }
- signed main()
- {
- // __START__;
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- int t = 1;
- // cin >> t;
- while (t--)
- {
- solve();
- }
- // __END__;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment