Очень часто встречаю задачи на эту прекрасную тему и хочу поинтересоваться о различных тактиках и фишках XOR-а
| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3603 |
| 4 | jiangly | 3583 |
| 5 | turmax | 3559 |
| 6 | tourist | 3541 |
| 7 | strapple | 3515 |
| 8 | ksun48 | 3461 |
| 9 | dXqwq | 3436 |
| 10 | Otomachi_Una | 3413 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 157 |
| 2 | adamant | 153 |
| 3 | Um_nik | 147 |
| 4 | Proof_by_QED | 146 |
| 5 | Dominater069 | 145 |
| 6 | errorgorn | 142 |
| 7 | cry | 139 |
| 8 | YuukiS | 135 |
| 9 | TheScrasse | 134 |
| 10 | chromate00 | 133 |
| Название |
|---|



в прочем обычные фишки, вроде, x ^ x = 0, или же XOR отрезка от 1 до N в О(1)
а как работает второе?
https://www.geeksforgeeks.org/calculate-xor-1-n/
Idiot, try again
is there a mistake somewhere?
Fan fact — XOR linked list
$$$a \oplus b \lt 2n \text{ for }a,b \le n$$$
XOR can be represented using OR and AND
$$$a \oplus b = (a \text{ OR } b) - (a \text{ AND } b)$$$
XOR is self-inverse:
$$$a \oplus b = c \Longleftrightarrow a=c \oplus b$$$
Problems related to XOR often can be solved bitwise (for all of the bits independently)
XOR is in fact addition in $$${Z}_{2}^{d}$$$ space, so many facts from linear algebra can be used in XOR related stuff (XOR basis is a good example)
Wow that’s really interesting i will try to keep this at the back of my head when i encounter xor problems
a Xor b <= a|b as well
Can u prove that last fact please
surprised this never got mentioned.
a^=b b^=a a^=b
is a valid way to swap a and b if a and b are integer types.
at the end of the day isn't everything is integer.
It won't work when $$$a=b$$$.
if a=b we don't need to swap
I mean that a and b refer to the same variable, not only the values are same.
Haven't solved much harder problems. But ig I used these +(observation) mostly.
a+b = a^b +2(a&b)probably the most used formula :)a^b<=2*max(a,b)vector<long long> _p = {n, 1, n + 1, 0}; return _p[n % 4];a^=b,b^=a,a^=bSecond one does not look correct
I think you forgot to multiply by 2 the right part
see here
$$$1\oplus2\oplus3\oplus\dots\oplus(4n-1)=0$$$ for all $$$n\in\mathbb Z^+.$$$
A corollary:
If $$$n$$$ $$$=$$$ $$$0$$$ $$$mod$$$ $$$4$$$ then XOR from $$$1$$$ to $$$n$$$ would be $$$n$$$
If $$$n$$$ $$$=$$$ $$$1$$$ $$$mod$$$ $$$4$$$ then XOR from $$$1$$$ to $$$n$$$ would be $$$1$$$
If $$$n$$$ $$$=$$$ $$$2$$$ $$$mod$$$ $$$4$$$ then XOR from $$$1$$$ to $$$n$$$ would be $$$n+1$$$
If $$$n$$$ $$$=$$$ $$$3$$$ $$$mod$$$ $$$4$$$ then XOR from $$$1$$$ to $$$n$$$ would be $$$0$$$
And here is a problem related to this formula :
https://atcoder.jp/contests/abc121/tasks/abc121_d
Also this one:
2036F
1208C - Magic Grid
The above problem is also based on this..
$$$a-b \le a\oplus b \le a+b$$$
Ask Coffee_zzz, who stands for the GCD-XOR-MEX group.
Idk if it's obvious or not but,
If addition of a and b is a odd number then their XOR will also be an odd number, And if addition is even then XOR will be even too.
(Yeah that 1st bit is all what it is)
deleted
a+b=a⊕b+2⋅(a&b)
Luogu P10857.
given a edge-weighted tree, after calculating xor[v]=xor of weights on path from root to v, xor[u]^xor[v]= xor of weights on path from u to v
XOR is an addition and subtraction without carryovers!
0 , 1 , 2 , 3 forms the Klein four group under the binary operator XOR which is an abelion group.
1^0 = 1
2^0 = 2
3^0 = 3
1^2^3 = 0
1^2 = 3
2^3 = 1
1^3 = 2
1^1 = 2^2 = 3^3 = 0
XOR is the opposite of if and only if
care to explain wdym ser?
So for an if and only if statement to work, if one thing is true, the other thing has to be true, and if the other thing is true, the first thing has to be true. Conversely, if one thing is false, the other thing has to be false, and vice versa. So if we let our things be bits, we get $$$1 \iff 1 = 1$$$ and $$$0 \iff 0 = 1$$$. This also means that $$$1 \iff 0 = 0$$$ and $$$0 \iff 1 = 0$$$, because they both violate the conditions. Thus, the if and only if operator returns the opposite of what xor would return.
never have i ever thought about it. damn.
i don't know where i might use it but its cool af.
XOR is a kind of addition without carry
Thats why a+b = a^b means a&b=0
You can find this logic used in multiple CF Div. 2 C/D bitwise questions (✿◠‿◠)
use xor to solve Nim.
let $$$a_1, \dots a_n \gt = k$$$ and $$$n$$$ be even.
Then either:
$$$a_1 \oplus a_2 \oplus \dots \oplus a_n \gt = k$$$
or
$$$\exists j$$$ such that $$$a_1 \oplus a_2 \oplus \dots a_{j-1} \oplus a_{j+1} \oplus \dots \oplus a_n \gt = k$$$
Proof for this?
If the xor of all values is greater that $$$k$$$ we are done.
Let $$$t$$$ be the first active bit in $$$a$$$ such that is not in $$$k$$$. If we choose that value, from $$$a_i \ge k$$$ we get that all bits more significant that $$$t$$$ in $$$k$$$ are also active in $$$a_i$$$. From parity and the fact that the xor of all values is less than $$$k$$$ we get that the number of values in $$$a$$$ with this bit on is even. So if we choose this value, this bit will become active in the xor of all remaining numbers and so the more significant bits in $$$k$$$.
If $$$t$$$ does not exists, it means that $$$a_i = k$$$ for all $$$i$$$ and you can subtract any value.
https://mirror.codeforces.com/gym/105046/problem/E
A boolean can be toggled by XORing with
true.Also if an integer is either
0orx(xis another integer) it can be toggled between0andxby XORing withx.most of the task with xor can be solved independently for each bit
This is a meaningful problem because i have met many problems using these knowledge,though i haven't understand it
A pretty fun fact that I have heard of but never actually been able to learn: xor is also known as Nimber addition. There's also something called Nimber multiplication, which also has commutativity property, associativity property, and distributive property with xor. Together, they form a complete field, also known as the Nimber field.
There are some pretty cool problems that could be solved with these operations, but it seems too advanced for me to understand =))))
Let $$$a = [a_1, a_2, \ldots, a_n]$$$ be non-decreasing array of integers, then $$$\min_{i \neq j}(a_i \oplus a_j) = \min_{1 \leq i \lt n}(a_i \oplus a_{i + 1})$$$
Problem using this: 1625D - Binary Spiders
It seems that any even number which greater than 2 can be decomposed into the XOR of two prime numbers.
Can anyone prove it? It looks like the XOR version of Goldbach's Conjecture.
Any source for where this comes from?
Just wrote C++ and confirmed this is true when $$$n\le10^9$$$.
Well, unless someone manage to prove this, I believe you are in a similar situation as in the original conjecture, it hasn't been proven but it is likely true.
Assume that primes are randomly distributed, given a pair of numbers such that their xor (or their sum) is $$$n$$$, the probability of both being primes is $$$O(1/log^2(n))$$$, there are $$$O(n)$$$ pairs that sum $$$n$$$. If you discard small values with a bruteforce, the probability that the conjecture is true quickly tends to 1.
Xor of n-bit numbers acts like addition of n-dimensional vectors over $$$\mathbb Z/2\mathbb Z$$$, which means you can use various linear algebra properties with it. Such as finding a "basis", i.e. for a collection of numbers, you can a minimal set of numbers out of a set that generate all the others with xor's.