Beingamanforever

Sum of XOR Functions, Contribution Technique

Jan 19th, 2025
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. #define all(x) (x).begin(), (x).end()
  5. typedef vector<int> vi;
  6. typedef vector<vi> vvi;
  7. typedef vector<pair<int, int>> vpi;
  8. typedef pair<int, int> pi;
  9. #define f first
  10. #define s second
  11. #define pb push_back
  12. #define endl "\n"
  13. #define yes cout << "YES" << endl
  14. #define no cout << "NO" << endl
  15. #define init(x, a) memset(x, a, sizeof(x))
  16. const int mod1 = 1e9 + 7, mod2 = 998244353, INF = 2e18, N = 2e5 + 5, L = 19;
  17. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  18. // -----------------------------------------------------------------------------
  19. void solve()
  20. {
  21.     int n;
  22.     cin >> n;
  23.     vi a(n);
  24.     for (int i = 0; i < n; i++)
  25.     {
  26.         cin >> a[i];
  27.     }
  28.     int sum = 0;
  29.     // sum of (r-l+1) = r * (cnt[different bit]) - summation of idxs(where different bit is there)
  30.     for (int i = 0; i < 31; i++)
  31.     {
  32.         vi cnt(2, 0), sumidx(2, 0);
  33.         cnt[0] = 1; // else starting from 1 wont be counted
  34.         int cur = 0, pref = 0;
  35.         for (int j = 0; j < n; j++)
  36.         {
  37.             int bit = (a[j] >> i) & 1;
  38.             pref ^= bit; // prefix bit
  39.             int contriR = (cnt[pref ^ 1] * (j + 1)) % mod2;
  40.             cur = (cur + contriR - sumidx[pref ^ 1] + mod2) % mod2;
  41.             cnt[pref]++;
  42.             sumidx[pref] = (sumidx[pref] + (j + 1)) % mod2;
  43.         }
  44.         sum = (sum + ((1LL << i) * cur) % mod2) % mod2;
  45.     }
  46.     cout << sum << endl;
  47.     return;
  48. }
  49.  
  50. signed main()
  51. {
  52.     ios_base::sync_with_stdio(false);
  53.     cin.tie(NULL);
  54.     cout.tie(NULL);
  55.     int t = 1;
  56.     // cin >> t;
  57.     while (t--)
  58.     {
  59.         solve();
  60.     }
  61.     return 0;
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment