Thanks for participating in our contest! We hope you enjoyed it.
Contest Link : AlgoThugs-2025
Problem A : Pairs
Idea : arnavra3
Check what all are the possible values for a particular bit in a and b corresponding to that bit being 1 and 0 in k. You get the following table:
| a | b | k |
|---|---|---|
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 1 | 1 |
| 0 | 0 | 0 |
1 in k we have 3 possible options and for each bit that is 0 we have just 1 possible option. So answer would be simply $$$3^{\text{(number of bits set in } k)}$$$.Problem B : XoRRY Permutation
Idea : Edward4762 jat.arc2004 arnavra3
Let's break it into $$$4$$$ cases, where $$$n \bmod 4$$$ is $$$0$$$, $$$1$$$, $$$2$$$, or $$$3$$$.
Case 1: $$$n \equiv 1 \pmod{4}$$$ or $$$n \equiv 3 \pmod{4}$$$
Since for any $$$x$$$ the shortest possible subarray length is 2 and the largest is $$$x+1$$$, we can force for all $$$1 \le x \le n-1$$$:
Because 2 appears an even number of times, the XOR becomes:
We can construct the permutation like this:
Case 2: $$$n \equiv 2 \pmod{4}$$$
Use the identity permutation but swap positions of 3 and 4:
Then the lengths become:
Since for $$$n \equiv 2 \pmod{4}$$$ we know:
The total XOR is:
Special case: when $$$n = 2$$$, minimum XOR = 2.
Case 3: $$$n \equiv 0 \pmod{4}$$$
Use the permutation:
For $$$x = 1$$$ to $$$4$$$, the length is $$$2$$$ due to the arrangement of the last $$$5$$$ elements.
For $$$x = 5$$$, the length is $$$3$$$.
For $$$x = 6$$$ to $$$n-3$$$, the lengths go from $$$4$$$ to $$$n-5$$$ because the element $$$n-1$$$ is at the $$$n-4$$$ -th position.
XOR from $$$1$$$ to $$$n-5$$$ will be $$$0$$$ , since $$$n-5 \equiv 3 \pmod{4}$$$ , and XOR of $$$ 1 \oplus 2 \oplus 3 =0 $$$ , Hence this resulting XOR from $$$4$$$ to $$$n-5$$$ will be $$$0$$$.
For $$$x = n-2$$$, length is $$$n-4$$$
For $$$x = n-1$$$, length is $$$n-1$$$
So total XOR is:
And since $$$n$$$ is divisible by $$$4$$$, $$$(n-4) \oplus (n-1) = 3$$$, so total is $$$3 \oplus 3 = 0$$$.
Special case: when $$$n = 4$$$, minimum XOR = 2.
Problem C : Binary Array
Idea : arnavra3
Let's first check the upper bound on number of queries. Since it is approximately half the maximum n, we get an idea that somehow we need to guess two elements of the array in each query.
Now consider any subarray of length 3 (let their indices be l, l+1, l+2) :
Claim: If you know the leftmost or rightmost element of this subarray, then after a single query you can surely predict the other 2.
Proof:
Case 1: Leftmost Element = 1
Possible Responses to Queries:
- 1 1 (Subarray would be 1 0 0)
- 2 0 (Subarray would be 1 0 1)
- 2 1 (Subarray would be 1 1 0)
- 3 1 (Subarray would be 1 1 1)
Case 2: Leftmost Element = 0
Possible Responses to Queries:
- 0 1 (Subarray would be 0 0 0)
- 1 0 (Subarray would be 0 1 0)
- 1 1 (Subarray would be 0 0 1)
- 2 1 (Subarray would be 0 1 1)
Similarly it holds true if we know the rightmost element.
So possible pattern of Queries would be
- ? 1 1
- ? 1 3
- ? 3 5
- ...
- ? n-2 n
Hence our solution takes at most $$$\left\lfloor\frac{n+2}{2}\right\rfloor$$$ queries which fits well within the given upper bound.
Problem D : Fossil Rarity
Idea : jat.arc2004
Let the current fossil type at a node be F and after a query on this node let its fossil type be P.
We observe that after a query on a particular node, only the ancestors of the current node get affected as they will have one less occurrence of fossil type F and one extra occurrence of fossil type P.
Given the queries are not persistent, we can maintain a prefix sum for each of the m fossils at each node with three possibilities:
- Actual Number of Occurrences of Fossil
- Actual Number of Occurrences of Fossil $$$-1$$$
- Actual Number of Occurrences of Fossil $$$+1$$$
Maintaining prefix sum for each of these possibilities we can simply get our answer.
Problem E : Satyam and IMO
Idea : Edward4762
Instead of iterating over all subsets, we consider what contribution each point does to the answer.
Let’s say a point $$$P$$$ is contained in some $$$M$$$ intervals.
The answer for this can simply be calculated as:
$$$C(N, K) - C(N - M, K)$$$
— basically all subsets where at least one segment contains the point $$$P$$$.
Now since the points range from $$$-10^9$$$ to $$$10^9$$$, we can't brute force it. Rather we use a sweep line to process intervals where all points in them occur in some $$$X$$$ segments.
We basically sort the start and end points of segments which helps us keep track of the points.
We precompute factorials and inverse factorials for faster computation.
Take a look at the code for better understanding.
Time Complexity: $$$O(N \log N)$$$ due to sorting
Problem F : Minimize Again
Idea : arnavra3
So, every prime number can be represented by itself — meaning it requires only 1 prime to represent it.
Every even number (greater than 2) can be expressed as the sum of 2 primes, according to the Goldbach Conjecture.
Now, for odd numbers:
- If an odd number is of the form $$$2 + p$$$ (where $$$p$$$ is a prime), then it can also be represented using 2 primes.
- For the remaining odd numbers, they can be expressed as an even number plus $$$3$$$.
Since the even number can be written as the sum of 2 primes, and $$$3$$$ itself is a prime, the total number of primes required is 3.
So, any number can be written as the sum of at most 3 prime numbers, which forms the basis of our approach.
Now sort the array in order of the minimum number of primes required to represent each number. Then apply the first operation on the last $$$K$$$ elements and convert them into prime numbers themselves. After this, the answer can be calculated easily.
This complexity is achieved due to precomputation of primes, where $$$\text{MaxN} = 10^7$$$.
Problem G : Faulty-SET
Idea : jat.arc2004
The question basically requires us to keep a track of what all characters appear odd number of times in the path from node $$$x$$$ to node $$$y$$$ (Note: LCA occurs twice on the path as due to redefinition).
It can be done using 2 approaches:
- Approach 1:
Maintain a prefix sum for each character up to each node. Then the frequency of each character can be found by:
- Approach 2:
Maintain a bitmask of length $$$26$$$ up to each node. On transition from a parent to a child,
if the $$$(s[\text{par}] - \text{'a'})$$$-th bit is set in the parent’s bitmask, it gets unset in the child’s bitmask, and vice versa.
Now to get the parity of the number of times the $$$i^{\text{th}}$$$ character occurs, just take the Bitwise XOR of $$$\text{Bitmask}(x)$$$ and $$$\text{Bitmask}(y)$$$.
Second step for each query is to just calculate the number of strings, which can be done using simple math or Digit-DP.
Here, $$$S_{\text{max}}$$$ is the sum of lengths of strings over all queries, which has an upper bound of $$$2 \cdot 10^5$$$.








