We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Xor-sequence
Xor-sequence
Sort by
recency
|
154 Discussions
|
Please Login in order to post a comment
This is my Own Signature Solution!!! I spent 4 days just for this, and it surpassed the editorial's speed. The implementatoin is straightforward and highly intuitive. Hope you like it.
include
using namespace std;
int main(){ int t; scanf("%d", &t); long long a, b, dts, mod, ret;
}
Here is my Python code!
The same modulus 8 solution in JavaScript/Node.js:
function getXor(x) { let n = x % 8; if (n === 0 || n === 1) return x; if (n === 2 || n === 3) return 2; if (n === 4 || n === 5) return x + 2; return 0; }
function xorSequence(l, r) { return BigInt(getXor(r)) ^ BigInt(getXor(l - 1)); }
There is a pattern of 4 consecutive elements in the array A, for example: A[0] -> A[3], and so on. The first number of the sequence = it's index, the 2nd number is 1, the third = index of the fourth and the fourth = 0. And, when you xor all 4 together, you always get 2. When you xor 2 sequences, you get 0, so it doesn't affect the remaining xor. Base on that, you can just find the effective xor, which are from left to the next start sequence index and from right back to the index after the last 8-element sequence.
Haskell
So, if you look at the sequence of running XORs over the XORs, there's a pattern. I didn't give it much thought after that -- just implement the lookup function.