Guest User

HackerRank Week of Code-16 E

a guest
Jul 13th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define forn(i,n) for (int i = 0; (i) < (n); ++i)
  6. #define forab(i,a,b) for (int i = (a); (i) < (b); ++i)
  7.  
  8. typedef long long LL;
  9.  
  10. #define FNAME ""
  11.                                        
  12. const int MAXN = 1e4, MAX = 64;
  13.  
  14. int a[MAXN], b[MAXN], c[MAXN], d[MAXN], cnt[MAXN], cur[MAXN], cur2[MAXN];
  15. LL ans[MAXN];
  16.  
  17. inline int add(int a, int m)
  18. {
  19.     if (a >= m)
  20.         a -= m;
  21.     return a;
  22. }
  23.  
  24. int main()
  25. {
  26. #ifdef LOCAL    
  27.     freopen(FNAME".in", "r", stdin);
  28.     freopen(FNAME".out", "w", stdout);
  29. #endif    
  30.     int n, m;
  31.     scanf("%d%d", &n, &m);
  32.     forn (i, n)
  33.         scanf("%d", &a[i]);
  34.     forn (i, n)
  35.         scanf("%d", &b[i]), c[i] = a[i] ^ b[i];
  36.     forn (i, n)
  37.         d[i + 1] = d[i] ^ a[i];
  38.     forn (i, n)
  39.     {
  40.         forn (j, MAX)
  41.             cnt[j] = 0, cur[j] = 0, cur2[j] = 0;
  42.         cur[0] = 1;
  43.         int two = 1;
  44.         forab (j, i, n)
  45.         {
  46.             cnt[c[j]]++;
  47.             if (cnt[c[j]] == 1)
  48.             {
  49.                 forn (g, MAX)
  50.                     cur[g] = (cur[g] * 1ll * two) % m;
  51.                 forn (g, MAX)
  52.                     cur2[g] = add(cur[g] + cur[g ^ c[j]], m);
  53.                 forn (g, MAX)
  54.                     cur[g] = cur2[g], cur2[g] = 0;
  55.                 ans[j - i + 1] += cur[d[j + 1] ^ d[i]];
  56.                 two = 1;
  57.             }
  58.             else
  59.             {
  60.                 two = add(two * 2, m);
  61.                 ans[j - i + 1] = (ans[j - i + 1] + two * 1ll * cur[d[j + 1] ^ d[i]]);
  62.             }
  63.         }
  64.     }
  65.     forab (i, 1, n + 1)
  66.         printf("%d\n", (int) (ans[i] % m));
  67.     return 0;
  68. }
Add Comment
Please, Sign In to add comment